View Javadoc
1   /***
2    * Copyright (c) 2003 held jointly by the individual authors.            
3    *                                                                          
4    * This library is free software; you can redistribute it and/or modify it    
5    * under the terms of the GNU Lesser General Public License as published      
6    * by the Free Software Foundation; either version 2.1 of the License, or 
7    * (at your option) any later version.                                            
8    *                                                                            
9    * This library is distributed in the hope that it will be useful, but 
10   * WITHOUT ANY WARRANTY; with out even the implied warranty of 
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
12   * GNU Lesser General Public License for more details.                                                  
13   *                                                                           
14   * You should have received a copy of the GNU Lesser General Public License   
15   * along with this library;  if not, write to the Free Software Foundation,   
16   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.              
17   *                                                                            
18   * > http://www.gnu.org/copyleft/lesser.html                                  
19   * > http://www.opensource.org/licenses/lgpl-license.php
20   */
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  /***
31   * This default implementation of the ValueListHandler service used Jakartas
32   * digester to create a Configuration object.
33   * 
34   * @author Matthew L. Wilson, Andrej Zachar
35   * @version $Revision: 1.15 $ $Date: 2006/03/29 19:47:49 $
36   */
37  public class DefaultValueListHandlerImpl implements ValueListHandler
38  {
39     /***
40      * Compares two beans.
41      * 
42      * @author Matthew L. Wilson
43      * @version $Revision: 1.15 $ $Date: 2006/03/29 19:47:49 $
44      */
45     public static class BeanComparator implements Comparator
46     {
47        /*** The direction to sort. */
48        private int direction = -1;
49  
50        /*** The method to sort upon. */
51        private String method = null;
52  
53        /***
54         * Default constructor.
55         * 
56         * @param pmethod
57         *            The method to sort upon.
58         * @param pdirection
59         *            The direction to sort.
60         */
61        public BeanComparator(String pmethod, int pdirection)
62        {
63           this.method = pmethod;
64           this.direction = pdirection;
65        }
66  
67        /***
68         * @see java.util.Comparator.compare(Object, Object)
69         */
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              // TODO: do not swallow this!
93              e.printStackTrace();
94           }
95  
96           return 0;
97        }
98     }
99  
100    /*** Commons logger. */
101    private static final Log LOGGER = LogFactory.getLog(DefaultValueListHandlerImpl.class);
102 
103    /*** The configuration for this implementation. * */
104    private Configuration config = new Configuration();
105 
106    /***
107     * Creates a new instance of ValueListHandler
108     */
109    public DefaultValueListHandlerImpl()
110    {}
111 
112    /***
113     * @param name
114     *            of adapter
115     * @return ValueListAdapter
116     */
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    /***
128     * @return Returns the config.
129     */
130    public Configuration getConfig()
131    {
132       return config;
133    }
134 
135    /***
136     * @see com.mlw.vlh.ValueListHander.getValueList(ValueListInfo info, String
137     *      name)
138     * 
139     */
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 }