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.CmsResource;
033import org.opencms.main.CmsException;
034import org.opencms.util.CmsUUID;
035
036import java.io.Serializable;
037import java.util.Comparator;
038import java.util.HashMap;
039import java.util.List;
040import java.util.Map;
041
042/**
043 * Comparator for sorting resource objects based on priority and date.<p>
044 *
045 * Serves as {@link java.util.Comparator} for resources and as comparator key for the resource
046 * at the same time. Uses lazy initializing of comparator keys in a resource.<p>
047 *
048 * @since 6.0.0
049 */
050public class CmsPriorityDateResourceComparator implements Serializable, Comparator<CmsResource> {
051
052    /** The name of the date property to read. */
053    public static final String PROPERTY_DATE = "collector.date";
054
055    /** Serial version UID required for safe serialization. */
056    private static final long serialVersionUID = 5316136357328564518L;
057
058    /** The date sort order. */
059    private boolean m_asc;
060
061    /** The current OpenCms user context. */
062    private transient CmsObject m_cms;
063
064    /** The date of this comparator key. */
065    private long m_date;
066
067    /** The internal map of comparator keys. */
068    private Map<CmsUUID, CmsPriorityDateResourceComparator> m_keys;
069
070    /** The priority of this comparator key. */
071    private int m_priority;
072
073    /**
074     * Creates a new instance of this comparator key.<p>
075     *
076     * @param cms the current OpenCms user context
077     * @param asc if true, the date sort order is ascending, otherwise descending
078     */
079    public CmsPriorityDateResourceComparator(CmsObject cms, boolean asc) {
080
081        m_cms = cms;
082        m_asc = asc;
083        m_keys = new HashMap<CmsUUID, CmsPriorityDateResourceComparator>();
084    }
085
086    /**
087     * Creates a new instance of this comparator key.<p>
088     *
089     * @param resource the resource to create the key for
090     * @param cms the current OpenCms user context
091     *
092     * @return a new instance of this comparator key
093     */
094    private static CmsPriorityDateResourceComparator create(CmsResource resource, CmsObject cms) {
095
096        CmsPriorityDateResourceComparator result = new CmsPriorityDateResourceComparator(null, false);
097        result.init(resource, cms);
098        return result;
099    }
100
101    /**
102     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
103     */
104    public int compare(CmsResource res0, CmsResource res1) {
105
106        if (res0 == res1) {
107            return 0;
108        }
109
110        CmsPriorityDateResourceComparator key0 = m_keys.get(res0.getStructureId());
111        CmsPriorityDateResourceComparator key1 = m_keys.get(res1.getStructureId());
112
113        if (key0 == null) {
114            // initialize key if null
115            key0 = CmsPriorityDateResourceComparator.create(res0, m_cms);
116            m_keys.put(res0.getStructureId(), key0);
117        }
118        if (key1 == null) {
119            // initialize key if null
120            key1 = CmsPriorityDateResourceComparator.create(res1, m_cms);
121            m_keys.put(res1.getStructureId(), key1);
122        }
123
124        // check priority
125        if (key0.getPriority() > key1.getPriority()) {
126            return -1;
127        }
128
129        if (key0.getPriority() < key1.getPriority()) {
130            return 1;
131        }
132
133        if (m_asc) {
134            // sort in ascending order
135            if (key0.getDate() > key1.getDate()) {
136                return 1;
137            }
138            if (key0.getDate() < key1.getDate()) {
139                return -1;
140            }
141        } else {
142            // sort in descending order
143            if (key0.getDate() > key1.getDate()) {
144                return -1;
145            }
146            if (key0.getDate() < key1.getDate()) {
147                return 1;
148            }
149        }
150
151        return 0;
152    }
153
154    /**
155     * Returns the date of this resource comparator key.<p>
156     *
157     * @return the date of this resource comparator key
158     */
159    public long getDate() {
160
161        return m_date;
162    }
163
164    /**
165     * Returns the priority of this resource comparator key.<p>
166     *
167     * @return the priority of this resource comparator key
168     */
169    public int getPriority() {
170
171        return m_priority;
172    }
173
174    /**
175     * Initializes the comparator key based on the member variables.<p>
176     *
177     * @param resource the resource to use
178     * @param cms the current OpenCms user contxt
179     */
180    private void init(CmsResource resource, CmsObject cms) {
181
182        List<CmsProperty> properties;
183
184        try {
185            properties = cms.readPropertyObjects(resource, false);
186        } catch (CmsException e) {
187            m_priority = 0;
188            m_date = 0;
189            return;
190        }
191
192        try {
193            m_priority = Integer.parseInt(
194                CmsProperty.get(CmsPriorityResourceCollector.PROPERTY_PRIORITY, properties).getValue());
195        } catch (NumberFormatException e) {
196            m_priority = CmsPriorityResourceCollector.PRIORITY_STANDARD;
197        }
198
199        try {
200            m_date = Long.parseLong(CmsProperty.get(PROPERTY_DATE, properties).getValue());
201        } catch (NumberFormatException e) {
202            m_date = 0;
203        }
204    }
205
206}