View Javadoc
1   /**
2    * ResultTransformerUtil.java
3    * 
4    * Mercer Inc.
5    * JBossMHR
6    * Copyright 2008 All Rights Reserved
7    * @since 1.0 May 14, 2008
8    * =============================================================================================
9    * $Id: ResultTransformerUtil.java,v 1.1 2008/05/14 14:44:23 abhishekm Exp $
10   * =============================================================================================
11   */
12  package com.gisgraphy.hibernate.criterion;
13  
14  import java.util.ArrayList;
15  import java.util.Iterator;
16  import java.util.List;
17  import java.util.Map;
18  import java.util.Set;
19  
20  import org.hibernate.transform.AliasToBeanResultTransformer;
21  import org.hibernate.transform.ResultTransformer;
22  
23  import com.gisgraphy.domain.valueobject.GisFeatureDistance;
24  import com.gisgraphy.domain.valueobject.GisFeatureDistanceFactory;
25  import com.gisgraphy.domain.valueobject.StreetDistance;
26  
27  /**
28   * The Class ResultTransformerUtil.
29   * 
30   * @author Abhishek Mirge
31   */
32  public class ResultTransformerUtil<T> {
33  
34      
35      	protected static GisFeatureDistanceFactory gisFeatureDistanceFactory = new GisFeatureDistanceFactory();
36  	/**
37  	 * Transform to bean. See bug
38  	 * http://opensource.atlassian.com/projects/hibernate/browse/HHH-2463
39  	 * 
40  	 * @param aliasList
41  	 *                the alias list
42  	 * @param resultList
43  	 *                the result list
44  	 * 
45  	 * @return the list of GisFeatureDistance
46  	 */
47  	//TODO tests zip test
48  	public static List<GisFeatureDistance> transformToGisFeatureDistance(String aliasList[], List<?> resultList, Map<Long, Set<String>> featureIdToZipCodesMap,Class clazz) {
49  		List<GisFeatureDistance> results = new ArrayList<GisFeatureDistance>();
50  		if (aliasList != null && !resultList.isEmpty()) {
51  			ResultTransformer tr = new AliasToBeanResultTransformer(GisFeatureDistance.class);
52  			Iterator<?> it = resultList.iterator();
53  			Object[] obj;
54  			GisFeatureDistance gisFeatureDistance;
55  			while (it.hasNext()) {
56  				obj = (Object[]) it.next();
57  				gisFeatureDistance = (GisFeatureDistance) tr.transformTuple(obj, aliasList);
58  				int indexInList = results.indexOf(gisFeatureDistance);
59  				if (indexInList == -1) {
60  				    gisFeatureDistanceFactory.updateFields(gisFeatureDistance,clazz);
61  					results.add(gisFeatureDistance);
62  					if (featureIdToZipCodesMap != null){
63  					    gisFeatureDistance.setZipCodes(featureIdToZipCodesMap.get(gisFeatureDistance.getId()));
64  					}
65  				}
66  			}
67  		}
68  
69  		return results;
70  	}
71  
72  	/**
73  	 * Transform to bean. See bug
74  	 * http://opensource.atlassian.com/projects/hibernate/browse/HHH-2463
75  	 * 
76  	 * @param aliasList
77  	 *                the alias list
78  	 * @param resultList
79  	 *                the result list
80  	 * 
81  	 * @return the list of {@link StreetDistance}
82  	 */
83  	public static List<StreetDistance> transformToStreetDistance(String aliasList[], List<?> resultList) {
84  		List<StreetDistance> transformList = new ArrayList<StreetDistance>();
85  		if (aliasList != null && !resultList.isEmpty()) {
86  			AliasToBeanResultTransformer tr = new AliasToBeanResultTransformer(StreetDistance.class);
87  			Iterator<?> it = resultList.iterator();
88  			Object[] obj;
89  			while (it.hasNext()) {
90  				obj = (Object[]) it.next();
91  				StreetDistance streetDistance = (StreetDistance) tr.transformTuple(obj, aliasList);
92  				streetDistance.updateFields();
93  				transformList.add(streetDistance);
94  			}
95  		}
96  		return transformList;
97  	}
98  
99  }