1 13 package info.magnolia.cms.gui.controlx.list; 14 15 16 import java.util.ArrayList ; 17 import java.util.Collection ; 18 import java.util.Collections ; 19 import java.util.Comparator ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import org.apache.commons.lang.StringUtils; 24 import org.slf4j.Logger; 25 import org.slf4j.LoggerFactory; 26 27 28 31 public abstract class AbstractListModel implements ListModel { 32 33 private static Logger log = LoggerFactory.getLogger(AbstractListModel.class); 34 35 38 public static final String DESCENDING = "DESC"; 39 40 43 public static final String ASCENDING = "ASC"; 44 45 48 protected String sortBy; 49 50 53 protected String sortByOrder; 54 55 58 protected String groupBy; 59 60 63 protected String groupByOrder; 64 65 68 private ValueProvider valueProvider; 69 70 75 public ListModelIterator getListModelIterator() { 76 try { 77 return createIterator(getResult()); 78 } 79 catch (Exception re) { 80 log.error("can't create the list model iterator, will return an empty list", re); 81 return new ListModelIteratorImpl(new ArrayList (), this.getGroupBy()); 82 } 83 } 84 85 public Iterator iterator() { 86 return getListModelIterator(); 87 } 88 89 92 protected abstract Collection getResult() throws Exception ; 93 94 99 protected ListModelIterator createIterator(Collection items) { 100 return new ListModelIteratorImpl((List ) this.doSort(items), this.getGroupBy(), this.getValueProvider()); 101 } 102 103 107 public void setSortBy(String name) { 108 this.sortBy = name; 109 } 110 111 116 public void setSortBy(String name, String order) { 117 this.sortBy = name; 118 this.sortByOrder = order; 119 } 120 121 125 public void setGroupBy(String name) { 126 this.groupBy = name; 127 } 128 129 134 public void setGroupBy(String name, String order) { 135 this.groupBy = name; 136 this.groupByOrder = order; 137 } 138 139 143 public String getSortBy() { 144 return this.sortBy; 145 } 146 147 151 public String getSortByOrder() { 152 return this.sortByOrder; 153 } 154 155 159 public String getGroupBy() { 160 return this.groupBy; 161 } 162 163 167 public String getGroupByOrder() { 168 return this.groupByOrder; 169 } 170 171 176 protected Collection doSort(Collection collection) { 177 if (StringUtils.isNotEmpty(this.getGroupBy())) { 178 ListComparator comparator = new ListComparator(); 179 comparator.setSortBy(this.getGroupBy()); 180 comparator.setOrder(this.getGroupByOrder()); 181 Collections.sort((List ) collection, comparator); 182 } 183 if (StringUtils.isNotEmpty(this.getGroupBy()) && StringUtils.isNotEmpty(this.getSortBy())) { ListComparator comparator = new ListComparator(); 185 comparator.setPreSort(this.getGroupBy()); 186 comparator.setSortBy(this.getSortBy()); 187 comparator.setOrder(this.getSortByOrder()); 188 Collections.sort((List ) collection, comparator); 189 } 190 if (StringUtils.isEmpty(this.getGroupBy()) && StringUtils.isNotEmpty(this.getSortBy())) { 191 ListComparator comparator = new ListComparator(); 192 comparator.setSortBy(this.getSortBy()); 193 comparator.setOrder(this.getSortByOrder()); 194 Collections.sort((List ) collection, comparator); 195 } 196 return collection; 197 } 198 199 202 public void setValueProvider(ValueProvider valueProvider) { 203 this.valueProvider = valueProvider; 204 } 205 206 209 public ValueProvider getValueProvider() { 210 if (valueProvider == null) { 211 valueProvider = DefaultValueProvider.getInstance(); 212 } 213 return valueProvider; 214 } 215 216 219 protected class ListComparator implements Comparator { 220 221 private String preSort; 222 223 private String sortBy; 224 225 private String order; 226 227 public int compare(Object object, Object object1) { 228 if (StringUtils.isNotEmpty(this.sortBy) && StringUtils.isEmpty(this.preSort)) { 229 return this.sort(object, object1); 230 } 231 else if (StringUtils.isNotEmpty(this.sortBy) && StringUtils.isNotEmpty(this.preSort)) { 232 return this.subSort(object, object1); 233 } 234 return 0; 235 } 236 237 242 private int sort(Object object, Object object1) { 243 Comparable firstKey = (Comparable ) getValueProvider().getValue(this.sortBy, object); 244 Comparable secondKey = (Comparable ) getValueProvider().getValue(this.sortBy, object1); 245 if (this.getOrder().equalsIgnoreCase(ASCENDING)) { 246 return firstKey.compareTo(secondKey); 247 } 248 249 return secondKey.compareTo(firstKey); 250 } 251 252 257 private int subSort(Object object, Object object1) { 258 String firstKey = (String ) getValueProvider().getValue(this.preSort, object); 259 String secondKey = (String ) getValueProvider().getValue(this.preSort, object1); 260 Comparable subSortFirstKey = (Comparable ) getValueProvider().getValue(this.sortBy, object); 261 Comparable subSortSecondKey = (Comparable ) getValueProvider().getValue(this.sortBy, object1); 262 if (firstKey.equalsIgnoreCase(secondKey)) { 263 if (this.getOrder().equalsIgnoreCase(ASCENDING)) { 264 return subSortFirstKey.compareTo(subSortSecondKey); 265 } 266 return subSortSecondKey.compareTo(subSortFirstKey); 267 } 268 return -1; 269 } 270 271 public String getPreSort() { 272 return preSort; 273 } 274 275 public void setPreSort(String preSort) { 276 this.preSort = preSort; 277 } 278 279 public String getSortBy() { 280 return sortBy; 281 } 282 283 public void setSortBy(String sortBy) { 284 this.sortBy = sortBy; 285 } 286 287 public String getOrder() { 288 if (order == null) { 289 return ASCENDING; 290 } 291 return order; 292 } 293 294 public void setOrder(String order) { 295 this.order = order; 296 } 297 298 } 299 } 300 | Popular Tags |