View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  /**
24   * This work was partially supported by the European Commission, under the 6th
25   * Framework Programme, contract IST-2-004688-STP. This library is free
26   * software; you can redistribute it and/or modify it under the terms of the GNU
27   * Lesser General Public License as published by the Free Software Foundation;
28   * either version 2.1 of the License, or (at your option) any later version.
29   * This library is distributed in the hope that it will be useful, but WITHOUT
30   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
31   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
32   * details. You should have received a copy of the GNU Lesser General Public
33   * License along with this library; if not, write to the Free Software
34   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35   */
36  package com.gisgraphy.hibernate.projection;
37  
38  import java.beans.Introspector;
39  import java.beans.PropertyDescriptor;
40  import java.util.ArrayList;
41  import java.util.Arrays;
42  import java.util.List;
43  
44  import org.hibernate.criterion.ProjectionList;
45  import org.hibernate.criterion.Projections;
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  /**
50   * 
51   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
52   */
53  public class ProjectionBean extends ProjectionList {
54  
55      private static Logger logger = LoggerFactory
56  	    .getLogger(ProjectionBean.class);
57  
58      /**
59       * @param fieldList
60       *                a list of fields to retrieve <b>Important Note</b> : the
61       *                projection are automatically aliased with the same name,
62       *                you could have some problems when using restriction
63       *                (column yX_ doesn't exists) see
64       *                http://opensource.atlassian.com/projects/hibernate/browse/HHH-817.
65       *                To avoid this problem you could disable aliasing .<br/>
66       *                activate auto-aliasing if you want to use aliasToBean
67       *                Transformers. disable auto-aliasing if you want to add a
68       *                restriction on a field that is in the fields list
69       * @return a new ProjectionList
70       */
71      public static ProjectionList fieldList(List<String> fieldList,
72  	    boolean autoaliasing) {
73  	return new ProjectionBean(fieldList, autoaliasing).projectionList;
74      }
75  
76      /**
77       * @param clazz
78       *                the class to inspect to retrieve the fields
79       * @param ignoreFields
80       *                an array of fields that should be ignore
81       * @param autoaliasing
82       *                if the fields should be auto-aliased <b>See important note
83       *                above</b>
84       * @return a new ProjectionList
85       */
86      public static ProjectionList beanFieldList(Class<?> clazz,
87  	    String[] ignoreFields, boolean autoaliasing) {
88  	return new ProjectionBean(inspectBean(clazz, ignoreFields),
89  		autoaliasing).projectionList;
90      }
91  
92      private static List<String> inspectBean(Class<?> clazz,
93  	    String[] ignoreFields) {
94  	if (clazz == null) {
95  	    throw new IllegalArgumentException("Can not inspect a null bean");
96  	}
97  	List<String> fieldNames = new ArrayList<String>();
98  	PropertyDescriptor[] att = null;
99  	try {
100 	    att = Introspector.getBeanInfo(clazz).getPropertyDescriptors();
101 	    List<String> ignoreFieldsList = Arrays.asList(ignoreFields);
102 	    for (int i = 0; i < att.length; i++) {
103 		String fieldName = att[i].getDisplayName();
104 		if (!"class".equals(fieldName)
105 			&& !isIgnoreFields(ignoreFieldsList, fieldName)) {
106 		    fieldNames.add(fieldName);
107 		}
108 	    }
109 	} catch (Exception e) {
110 	    logger.error("can not inspect bean " + clazz.getSimpleName()+" : "+e.getMessage(),e);
111 	}
112 
113 	return fieldNames;
114     }
115 
116     private static boolean isIgnoreFields(List<String> ignoreFieldsList,
117 	    String fieldName) {
118 	return ignoreFieldsList.contains(fieldName);
119     }
120 
121     /**
122      * Default SerialId
123      */
124     private static final long serialVersionUID = -5528427011925538257L;
125 
126     private ProjectionList projectionList = Projections.projectionList();
127 
128     /**
129      * 
130      */
131     /**
132      * @param fields
133      *                the list of the field names
134      * @param autoAliasing
135      *                if the fields should be autoaliased. <b>See important note
136      *                above</b>
137      */
138     public ProjectionBean(List<String> fields, boolean autoAliasing) {
139 	super();
140 	for (String field : fields) {
141 	    if (autoAliasing) {
142 		projectionList.add(Projections.property(field).as(field));
143 	    } else {
144 		projectionList.add(Projections.property(field));
145 	    }
146 	}
147     }
148 
149 }