1 21 package net.mlw.vlh; 22 23 import java.util.Collections ; 24 import java.util.Comparator ; 25 26 import org.apache.commons.beanutils.PropertyUtils; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 37 public class DefaultValueListHandlerImpl implements ValueListHandler 38 { 39 45 public static class BeanComparator implements Comparator 46 { 47 48 private int direction = -1; 49 50 51 private String method = null; 52 53 61 public BeanComparator(String pmethod, int pdirection) 62 { 63 this.method = pmethod; 64 this.direction = pdirection; 65 } 66 67 70 public int compare(Object obj1, Object obj2) 71 { 72 try 73 { 74 obj1 = PropertyUtils.getProperty(obj1, method); 75 obj2 = PropertyUtils.getProperty(obj2, method); 76 77 if (obj1 == null) 78 { 79 return direction * -1; 80 } 81 else if (obj2 == null) 82 { 83 return direction; 84 } 85 else if (obj1 instanceof Comparable ) 86 { 87 return ((Comparable ) obj1).compareTo(obj2) * direction; 88 } 89 } 90 catch (Exception e) 91 { 92 e.printStackTrace(); 94 } 95 96 return 0; 97 } 98 } 99 100 101 private static final Log LOGGER = LogFactory.getLog(DefaultValueListHandlerImpl.class); 102 103 104 private Configuration config = new Configuration(); 105 106 109 public DefaultValueListHandlerImpl() 110 {} 111 112 117 private ValueListAdapter getAdapter(String name) 118 { 119 ValueListAdapter adapter = config.getAdapter(name); 120 if (adapter == null) 121 { 122 throw new NullPointerException ("Adapter could not be located: " + name); 123 } 124 return adapter; 125 } 126 127 130 public Configuration getConfig() 131 { 132 return config; 133 } 134 135 140 public ValueList getValueList(String name, ValueListInfo info) 141 { 142 if (info == null) 143 { 144 info = new ValueListInfo(); 145 if (LOGGER.isDebugEnabled()) 146 { 147 LOGGER.debug("Creating a new ValueListInfo for the adapter '" + name + "'."); 148 } 149 } 150 151 info.getFilters().put(ValueListInfo.VALUE_LIST_NAME, name); 152 153 ValueListAdapter adapter = getAdapter(name); 154 ValueList valueList = adapter.getValueList(name, info); 155 156 if (LOGGER.isDebugEnabled()) 157 { 158 LOGGER.debug("The ValueList was loaded from the adapter '" + name + "'."); 159 } 160 161 162 if ((adapter.getAdapterType() & ValueListAdapter.DO_FOCUS) == ValueListAdapter.DO_FOCUS) 163 { 164 throw new NullPointerException ("Not yet implemented!"); 165 } 166 167 if ((adapter.getAdapterType() & ValueListAdapter.DO_FILTER) == ValueListAdapter.DO_FILTER) 168 { 169 throw new NullPointerException ("Not yet implemented!"); 170 } 171 if ((adapter.getAdapterType() & ValueListAdapter.DO_SORT) == ValueListAdapter.DO_SORT) 172 { 173 if (valueList.getValueListInfo() != null && valueList.getValueListInfo().getSortingColumn() != null 174 && valueList.getValueListInfo().getSortingDirection() != null && valueList.getList() != null) 175 { 176 Collections.sort(valueList.getList(), new BeanComparator(valueList.getValueListInfo().getSortingColumn(), valueList 177 .getValueListInfo().getSortingDirection().intValue())); 178 LOGGER.debug("The ValueList was sorted by post process."); 179 } 180 } 181 if ((adapter.getAdapterType() & ValueListAdapter.DO_PAGE) == ValueListAdapter.DO_PAGE) 182 { 183 if (valueList.getValueListInfo() != null && valueList.getList() != null) 184 { 185 int pagingPage = valueList.getValueListInfo().getPagingPage(); 186 int pagingNumberPer = valueList.getValueListInfo().getPagingNumberPer(); 187 int totalNumberOfEntries = valueList.getValueListInfo().getTotalNumberOfEntries(); 188 189 if (pagingNumberPer < totalNumberOfEntries) 190 { 191 int start = (pagingPage - 1) * pagingNumberPer; 192 if (start >= totalNumberOfEntries) 193 { 194 start = ((totalNumberOfEntries - 1) / pagingNumberPer) * pagingNumberPer; 195 } 196 int end = Math.min(start + pagingNumberPer, valueList.getList().size()); 197 valueList = new DefaultListBackedValueList(valueList.getList().subList(start, end), valueList.getValueListInfo()); 198 LOGGER.debug("The ValueList was paged by post process."); 199 } 200 } 201 } 202 203 return valueList; 204 } 205 } | Popular Tags |