001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.file.collectors;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsProperty;
032import org.opencms.file.CmsPropertyDefinition;
033import org.opencms.file.CmsResource;
034import org.opencms.main.CmsException;
035import org.opencms.util.CmsStringUtil;
036import org.opencms.util.CmsUUID;
037
038import java.io.Serializable;
039import java.text.Collator;
040import java.util.Comparator;
041import java.util.HashMap;
042import java.util.List;
043import java.util.Map;
044
045/**
046 * Comparator for sorting resource objects based on priority and title.<p>
047 *
048 * Serves as {@link java.util.Comparator} for resources and as comparator key for the resource
049 * at the same time. Uses lazy initializing of comparator keys in a resource.<p>
050 *
051 * @since 6.0.0
052 */
053public class CmsPriorityTitleResourceComparator implements Serializable, Comparator<CmsResource> {
054
055    /** Serial version UID required for safe serialization. */
056    private static final long serialVersionUID = -6815638350803584422L;
057
058    /** The current OpenCms user context. */
059    private transient CmsObject m_cms;
060
061    /** The internal map of comparator keys. */
062    private Map<CmsUUID, CmsPriorityTitleResourceComparator> m_keys;
063
064    /** The priority of this comparator key. */
065    private int m_priority;
066
067    /** The title of this comparator key. */
068    private String m_title;
069
070    /**
071     * Creates a new instance of this comparator key.<p>
072     *
073     * @param cms the current OpenCms user context
074     */
075    public CmsPriorityTitleResourceComparator(CmsObject cms) {
076
077        m_cms = cms;
078        m_keys = new HashMap<CmsUUID, CmsPriorityTitleResourceComparator>();
079    }
080
081    /**
082     * Creates a new instance of this comparator key.<p>
083     *
084     * @param resource the resource to create the key for
085     * @param cms the current OpenCms user context
086     *
087     * @return a new instance of this comparator key
088     */
089    private static CmsPriorityTitleResourceComparator create(CmsResource resource, CmsObject cms) {
090
091        CmsPriorityTitleResourceComparator result = new CmsPriorityTitleResourceComparator(null);
092        result.init(resource, cms);
093        return result;
094    }
095
096    /**
097     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
098     */
099    public int compare(CmsResource res0, CmsResource res1) {
100
101        if (res0 == res1) {
102            return 0;
103        }
104
105        CmsPriorityTitleResourceComparator key0 = m_keys.get(res0.getStructureId());
106        CmsPriorityTitleResourceComparator key1 = m_keys.get(res1.getStructureId());
107
108        if (key0 == null) {
109            // initialize key if null
110            key0 = CmsPriorityTitleResourceComparator.create(res0, m_cms);
111            m_keys.put(res0.getStructureId(), key0);
112        }
113        if (key1 == null) {
114            // initialize key if null
115            key1 = CmsPriorityTitleResourceComparator.create(res1, m_cms);
116            m_keys.put(res1.getStructureId(), key1);
117        }
118
119        if (key0.getPriority() > key1.getPriority()) {
120            return -1;
121        }
122
123        if (key0.getPriority() < key1.getPriority()) {
124            return 1;
125        }
126
127        // sort by title property depending on the locale
128        Collator collator = Collator.getInstance(m_cms.getRequestContext().getLocale());
129        return collator.compare(key0.getTitle(), key1.getTitle());
130    }
131
132    /**
133     * Returns the priority of this resource comparator key.<p>
134     *
135     * @return the priority of this resource comparator key
136     */
137    public int getPriority() {
138
139        return m_priority;
140    }
141
142    /**
143     * Returns the title of this resource comparator key.<p>
144     *
145     * @return the title of this resource comparator key
146     */
147    public String getTitle() {
148
149        return m_title;
150    }
151
152    /**
153     * Initializes the comparator key based on the member variables.<p>
154     *
155     * @param resource the resource to use
156     * @param cms the current OpenCms user contxt
157     */
158    private void init(CmsResource resource, CmsObject cms) {
159
160        List<CmsProperty> properties;
161
162        try {
163            properties = cms.readPropertyObjects(resource, false);
164        } catch (CmsException e) {
165            m_priority = 0;
166            m_title = "";
167            return;
168        }
169
170        try {
171            m_priority = Integer.parseInt(
172                CmsProperty.get(CmsPriorityResourceCollector.PROPERTY_PRIORITY, properties).getValue());
173        } catch (NumberFormatException e) {
174            m_priority = CmsPriorityResourceCollector.PRIORITY_STANDARD;
175        }
176
177        m_title = CmsProperty.get(CmsPropertyDefinition.PROPERTY_TITLE, properties).getValue();
178        if (CmsStringUtil.isEmpty(m_title)) {
179            m_title = "";
180        }
181    }
182}