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.CmsPropertyDefinition; 37 import org.opencms.file.CmsResource; 38 import org.opencms.main.CmsException; 39 import org.opencms.util.CmsStringUtil; 40 41 import java.text.Collator ; 42 import java.util.Comparator ; 43 import java.util.HashMap ; 44 import java.util.List ; 45 import java.util.Map ; 46 47 59 public class CmsPriorityTitleResourceComparator implements Comparator { 60 61 62 private CmsObject m_cms; 63 64 65 private Map m_keys; 66 67 68 private int m_priority; 69 70 71 private String m_title; 72 73 78 public CmsPriorityTitleResourceComparator(CmsObject cms) { 79 80 m_cms = cms; 81 m_keys = new HashMap (); 82 } 83 84 92 private static CmsPriorityTitleResourceComparator create(CmsResource resource, CmsObject cms) { 93 94 CmsPriorityTitleResourceComparator result = new CmsPriorityTitleResourceComparator(null); 95 result.init(resource, cms); 96 return result; 97 } 98 99 102 public int compare(Object arg0, Object arg1) { 103 104 if ((arg0 == arg1) || !(arg0 instanceof CmsResource) || !(arg1 instanceof CmsResource)) { 105 return 0; 106 } 107 108 CmsResource res0 = (CmsResource)arg0; 109 CmsResource res1 = (CmsResource)arg1; 110 111 CmsPriorityTitleResourceComparator key0 = (CmsPriorityTitleResourceComparator)m_keys.get(res0.getStructureId()); 112 CmsPriorityTitleResourceComparator key1 = (CmsPriorityTitleResourceComparator)m_keys.get(res1.getStructureId()); 113 114 if (key0 == null) { 115 key0 = CmsPriorityTitleResourceComparator.create(res0, m_cms); 117 m_keys.put(res0.getStructureId(), key0); 118 } 119 if (key1 == null) { 120 key1 = CmsPriorityTitleResourceComparator.create(res1, m_cms); 122 m_keys.put(res1.getStructureId(), key1); 123 } 124 125 if (key0.getPriority() > key1.getPriority()) { 126 return -1; 127 } 128 129 if (key0.getPriority() < key1.getPriority()) { 130 return 1; 131 } 132 133 Collator collator = Collator.getInstance(m_cms.getRequestContext().getLocale()); 135 return collator.compare(key0.getTitle(), key1.getTitle()); 136 } 137 138 143 public int getPriority() { 144 145 return m_priority; 146 } 147 148 153 public String getTitle() { 154 155 return m_title; 156 } 157 158 164 private void init(CmsResource resource, CmsObject cms) { 165 166 List properties; 167 168 try { 169 properties = cms.readPropertyObjects(resource, false); 170 } catch (CmsException e) { 171 m_priority = 0; 172 m_title = ""; 173 return; 174 } 175 176 try { 177 m_priority = Integer.parseInt(CmsProperty.get(CmsPriorityResourceCollector.PROPERTY_PRIORITY, properties).getValue()); 178 } catch (NumberFormatException e) { 179 m_priority = CmsPriorityResourceCollector.PRIORITY_STANDARD; 180 } 181 182 m_title = CmsProperty.get(CmsPropertyDefinition.PROPERTY_TITLE, properties).getValue(); 183 if (CmsStringUtil.isEmpty(m_title)) { 184 m_title = ""; 185 } 186 } 187 } | Popular Tags |