View Javadoc
1 /* 2 * DefaltValueListHandler.java 3 * 4 * Created on March 21, 2003, 2:55 PM 5 */ 6 7 package com.mlw.vlh.impl; 8 9 import java.util.Collections; 10 import java.util.Comparator; 11 import java.util.HashMap; 12 import java.util.Map; 13 14 import net.sf.common.config.Configuration; 15 import net.sf.common.config.ConfigurationFactory; 16 17 import org.apache.commons.beanutils.PropertyUtils; 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 21 import com.mlw.vlh.ValueList; 22 import com.mlw.vlh.ValueListAdapter; 23 import com.mlw.vlh.ValueListHandler; 24 import com.mlw.vlh.ValueListInfo; 25 import com.mlw.vlh.adapter.ConfigurationAdapter; 26 27 /*** 28 * 29 * @author Matthew Wilson 30 */ 31 public class DefaltValueListHandler extends ValueListHandler 32 { 33 /*** class logger */ 34 public static final Log log = LogFactory.getLog(DefaltValueListHandler.class); 35 36 /*** Holds all the adapters */ 37 private Map services = new HashMap(); 38 39 /*** Creates a new instance of DefaltValueListHandler 40 */ 41 public DefaltValueListHandler() 42 { 43 Configuration configuration = ConfigurationFactory.getFactory().getConfig(this.getClass()); 44 String[] keys = configuration.getKeys(); 45 46 for( int i=0, length=keys.length; i<length; i++) 47 { 48 try 49 { 50 if( log.isInfoEnabled() ) 51 { 52 log.info("Attempting to construct ("+ keys[i] +") class: " + configuration.getString(keys[i]) ); 53 } 54 services.put( keys[i], Class.forName( configuration.getString(keys[i]) ).newInstance() ); 55 } 56 catch(Exception e) 57 { 58 log.error("Error initializing class: " + configuration.getString(keys[i]), e); 59 } 60 } 61 62 services.put("ConfigurationAdapter", new ConfigurationAdapter(services) ); 63 64 } 65 66 /*** @see com.mlw.vlh.ValueListHander.getValueList(ValueListInfo info, String name) 67 */ 68 public ValueList getValueList(ValueListInfo info, String name) 69 { 70 ValueListAdapter adapter = (ValueListAdapter)services.get(name); 71 if( adapter == null ) throw new NullPointerException( "Invalid adapter name: " + name ); 72 73 ValueList valueList = adapter.getValueList(info); 74 75 if( (adapter.getAdapterType() & ValueListAdapter.DO_FILTER) == ValueListAdapter.DO_FILTER ) 76 { 77 //TODO filtering 78 } 79 if( (adapter.getAdapterType() & ValueListAdapter.DO_SORT) == ValueListAdapter.DO_SORT ) 80 { 81 Collections.sort( valueList.getList(), new BeanComparator(info.getSortColumn()[0], info.getSortDirection()[0]) ); 82 } 83 if( (adapter.getAdapterType() & ValueListAdapter.DO_PAGE) == ValueListAdapter.DO_PAGE ) 84 { 85 //TODO paging 86 } 87 88 return valueList; 89 } 90 91 private static class BeanComparator implements Comparator 92 { 93 private int direction = 0; 94 private String method = null; 95 96 /*** Creates a new instance of BeanComp 97 */ 98 public BeanComparator(String method, Integer direction) 99 { 100 this.method = method; 101 this.direction = direction.intValue(); 102 } 103 104 public int compare(Object obj1, Object obj2) 105 { 106 try 107 { 108 obj1 = PropertyUtils.getProperty(obj1,method); 109 obj2 = PropertyUtils.getProperty(obj2,method); 110 111 if(obj1==null) return direction*-1; 112 if(obj1==null) return direction* 1; 113 114 if( obj1 instanceof Comparable ) 115 { 116 return ((Comparable)obj1).compareTo(obj2)*direction; 117 } 118 } 119 catch(Exception e) 120 { 121 e.printStackTrace(); 122 } 123 return 0; 124 } 125 126 } 127 128 }

This page was automatically generated by Maven