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   * 
25   */
26  package com.gisgraphy.domain.repository;
27  
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  import javax.annotation.PostConstruct;
34  
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  import org.springframework.beans.factory.annotation.Autowired;
38  import org.springframework.stereotype.Component;
39  
40  import com.gisgraphy.domain.geoloc.entity.GisFeature;
41  import com.gisgraphy.domain.repository.exception.RepositoryException;
42  import com.gisgraphy.geoloc.GeolocSearchEngine;
43  
44  /**
45   * A class based on the stategy pattern
46   * 
47   * @see <a href="http://en.wikipedia.org/wiki/Strategy_pattern">Strategy pattern</a>
48   * 
49   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
50   */
51  @Component
52  public class RepositoryStrategy implements IRepositoryStrategy {
53  
54      @Autowired
55      protected IGisDao<? extends GisFeature>[] daos;
56  
57      /**
58       * The logger
59       */
60      protected static final Logger logger = LoggerFactory
61  	    .getLogger(GeolocSearchEngine.class);
62  
63      /**
64       * A Map with GisFeature call as keys and Dao as Values. A (Gis)dao is
65       * associated to a Class
66       */
67      public final static Map<Class<? extends GisFeature>, IGisDao<? extends GisFeature>> daoMap = new HashMap<Class<? extends GisFeature>, IGisDao<? extends GisFeature>>();
68  
69      @PostConstruct
70      public void initMap() throws Exception {
71  	logger.info("gisdao size=" + daos.length);
72  	for (int i = 0; i < daos.length; i++) {
73  	    daoMap.put((Class<? extends GisFeature>) daos[i]
74  		    .getPersistenceClass(), daos[i]);
75  	    logger.info("iGisdao[" + i + "]=" + daos[i].getPersistenceClass());
76  	}
77      }
78  
79      /*
80       * (non-Javadoc)
81       * 
82       * @see com.gisgraphy.domain.repository.IRepositoryStrategy#GetDao(java.lang.Class)
83       */
84      public IGisDao<? extends GisFeature> getDao(
85  	    Class<?> placeType) {
86  	IGisDao<? extends GisFeature> dao = daoMap.get(GisFeature.class);
87  	if (placeType != null) {
88  	    dao = daoMap.get(placeType);
89  	} else {
90  	    return dao;
91  	}
92  	if (dao == null) {
93  	    throw new RepositoryException(
94  		    "No gisFeatureDao or no placetype can be found for "
95  			    + placeType + " can be found.");
96  	}
97  	return dao;
98      }
99  
100     /*
101      * (non-Javadoc)
102      * 
103      * @see com.gisgraphy.domain.repository.IRepositoryStrategy#getAvailablesDaos()
104      */
105     public Collection<IGisDao<? extends GisFeature>> getAvailablesDaos() {
106 	if (daos == null) {
107 	    return new ArrayList<IGisDao<? extends GisFeature>>();
108 	}
109 	return RepositoryStrategy.daoMap.values();
110     }
111 }