1 31 32 package org.opencms.file.collectors; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.file.CmsProperty; 36 import org.opencms.file.CmsResource; 37 import org.opencms.main.CmsException; 38 39 import java.util.Comparator ; 40 import java.util.HashMap ; 41 import java.util.List ; 42 import java.util.Map ; 43 44 57 public class CmsPriorityDateResourceComparator implements Comparator { 58 59 60 public static final String PROPERTY_DATE = "collector.date"; 61 62 63 private boolean m_asc; 64 65 66 private CmsObject m_cms; 67 68 69 private long m_date; 70 71 72 private Map m_keys; 73 74 75 private int m_priority; 76 77 83 public CmsPriorityDateResourceComparator(CmsObject cms, boolean asc) { 84 85 m_cms = cms; 86 m_asc = asc; 87 m_keys = new HashMap (); 88 } 89 90 98 private static CmsPriorityDateResourceComparator create(CmsResource resource, CmsObject cms) { 99 100 CmsPriorityDateResourceComparator result = new CmsPriorityDateResourceComparator(null, false); 101 result.init(resource, cms); 102 return result; 103 } 104 105 108 public int compare(Object arg0, Object arg1) { 109 110 if ((arg0 == arg1) || !(arg0 instanceof CmsResource) || !(arg1 instanceof CmsResource)) { 111 return 0; 112 } 113 114 CmsResource res0 = (CmsResource)arg0; 115 CmsResource res1 = (CmsResource)arg1; 116 117 CmsPriorityDateResourceComparator key0 = (CmsPriorityDateResourceComparator)m_keys.get(res0.getStructureId()); 118 CmsPriorityDateResourceComparator key1 = (CmsPriorityDateResourceComparator)m_keys.get(res1.getStructureId()); 119 120 if (key0 == null) { 121 key0 = CmsPriorityDateResourceComparator.create(res0, m_cms); 123 m_keys.put(res0.getStructureId(), key0); 124 } 125 if (key1 == null) { 126 key1 = CmsPriorityDateResourceComparator.create(res1, m_cms); 128 m_keys.put(res1.getStructureId(), key1); 129 } 130 131 if (key0.getPriority() > key1.getPriority()) { 133 return -1; 134 } 135 136 if (key0.getPriority() < key1.getPriority()) { 137 return 1; 138 } 139 140 if (m_asc) { 141 if (key0.getDate() > key1.getDate()) { 143 return 1; 144 } 145 if (key0.getDate() < key1.getDate()) { 146 return -1; 147 } 148 } else { 149 if (key0.getDate() > key1.getDate()) { 151 return -1; 152 } 153 if (key0.getDate() < key1.getDate()) { 154 return 1; 155 } 156 } 157 158 return 0; 159 } 160 161 166 public long getDate() { 167 168 return m_date; 169 } 170 171 176 public int getPriority() { 177 178 return m_priority; 179 } 180 181 187 private void init(CmsResource resource, CmsObject cms) { 188 189 List properties; 190 191 try { 192 properties = cms.readPropertyObjects(resource, false); 193 } catch (CmsException e) { 194 m_priority = 0; 195 m_date = 0; 196 return; 197 } 198 199 try { 200 m_priority = Integer.parseInt(CmsProperty.get(CmsPriorityResourceCollector.PROPERTY_PRIORITY, properties).getValue()); 201 } catch (NumberFormatException e) { 202 m_priority = CmsPriorityResourceCollector.PRIORITY_STANDARD; 203 } 204 205 try { 206 m_date = Long.parseLong(CmsProperty.get(PROPERTY_DATE, properties).getValue()); 207 } catch (NumberFormatException e) { 208 m_date = 0; 209 } 210 } 211 212 } | Popular Tags |