Revision 44831 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.mapcontext/org.gvsig.fmap.mapcontext.api/src/main/java/org/gvsig/fmap/mapcontext/layers/LayerFactory.java

View differences:

LayerFactory.java
23 23
 */
24 24
package org.gvsig.fmap.mapcontext.layers;
25 25

  
26
import java.util.HashMap;
27
import java.util.Iterator;
28
import java.util.Map;
29
import java.util.Map.Entry;
30

  
31
import org.cresques.cts.IProjection;
32
import org.gvsig.fmap.dal.DALLocator;
33 26
import org.gvsig.fmap.dal.DataManager;
34 27
import org.gvsig.fmap.dal.DataStore;
35 28
import org.gvsig.fmap.dal.DataStoreParameters;
36
import org.gvsig.fmap.dal.exception.InitializeException;
37
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
38
import org.gvsig.fmap.dal.feature.FeatureStore;
29
import org.gvsig.fmap.mapcontext.MapContextLocator;
39 30
import org.gvsig.fmap.mapcontext.MapContextManager;
40 31
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
41 32
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer;
......
44 35
 * Crea un adaptador del driver que se le pasa como par?metro en los m?todos
45 36
 * createLayer. Si hay memoria suficiente se crea un FLyrMemory que pasa todas
46 37
 * las features del driver a memoria
38
 * 
39
 * @deprecated use MapContextManager
47 40
 */
48 41
public class LayerFactory {
49 42
//	final static private Logger logger = LoggerFactory.getLogger(LayerFactory.class);
50 43

  
51 44
	private static LayerFactory instance = null;
52 45

  
53

  
54

  
55 46
	public static LayerFactory getInstance() {
56 47
		if (instance == null) {
57 48
			instance = new LayerFactory();
......
64 55
	 *
65 56
	 * como clave usamos el nombre de registro del dataStore
66 57
	 */
67
	private Map layersToUseForStore = new HashMap();
58
//	private Map layersToUseForStore = new HashMap();
68 59

  
69 60
	/**
70 61
	 * Registra que clase tiene que usar para un {@link DataStore} determinado. <br>
......
79 70
	 * @return
80 71
	 */
81 72

  
82
	public boolean registerLayerToUseForStore(String dataStoreName,
83
			Class layerClassToUse) {
84

  
85
		DataManager dm = DALLocator.getDataManager();
86
		DataStoreParameters dsparams;
87
		try {
88
			dsparams = dm.createStoreParameters(dataStoreName);
89
		} catch (InitializeException e) {
90
			e.printStackTrace();
91
			return false;
92
		} catch (ProviderNotRegisteredException e) {
93
			e.printStackTrace();
94
			return false;
95
		}
96
		if (!layerClassToUse.isAssignableFrom(SingleLayer.class)) {
97
			return false;
98
		}
99
		this.layersToUseForStore.put(dsparams.getDataStoreName(),
100
				layerClassToUse);
101

  
102
		return true;
73
	public boolean registerLayerToUseForStore(
74
          String dataStoreName,
75
          Class layerClassToUse
76
      ) {
77
    throw new UnsupportedOperationException("This method is deprecated use MapContextMananer.registerLayer");
78
//		DataManager dm = DALLocator.getDataManager();
79
//		DataStoreParameters dsparams;
80
//    try {
81
//			dsparams = dm.createStoreParameters(dataStoreName);
82
//		} catch (InitializeException e) {
83
//			e.printStackTrace();
84
//			return false;
85
//		} catch (ProviderNotRegisteredException e) {
86
//			e.printStackTrace();
87
//			return false;
88
//		}
89
//		if (!layerClassToUse.isAssignableFrom(SingleLayer.class)) {
90
//			return false;
91
//		}
92
//		this.layersToUseForStore.put(dsparams.getDataStoreName(),
93
//				layerClassToUse);
94
//
95
//		return true;
103 96
	}
104 97

  
105
	public boolean registerLayerToUseForStore(Class storeClass,
106
			Class layerClassToUse) {
107

  
108
//		DataManager dm = DALLocator.getDataManager();
109
		if (!DataStore.class.isAssignableFrom(storeClass)) {
110
			return false;
111
		}
112

  
113
		if (!SingleLayer.class.isAssignableFrom(layerClassToUse)
114
				|| !FLayer.class.isAssignableFrom(layerClassToUse)) {
115
			return false;
116
		}
117
		this.layersToUseForStore.put(storeClass,
118
				layerClassToUse);
119

  
98
	public boolean registerLayerToUseForStore(
99
          Class storeClass,
100
          Class layerClassToUse) 
101
      {
102
    MapContextManager manager = MapContextLocator.getMapContextManager();
103
    manager.registerLayer(storeClass, layerClassToUse);
104
////		DataManager dm = DALLocator.getDataManager();
105
//		if (!DataStore.class.isAssignableFrom(storeClass)) {
106
//			return false;
107
//		}
108
//
109
//		if (!SingleLayer.class.isAssignableFrom(layerClassToUse)
110
//				|| !FLayer.class.isAssignableFrom(layerClassToUse)) {
111
//			return false;
112
//		}
113
//		this.layersToUseForStore.put(storeClass,
114
//				layerClassToUse);
115
//
120 116
		return true;
121 117
	}
122 118

  
123
	private Class getLayerClassFor(DataStore store) {
124
		Class result = (Class) this.layersToUseForStore.get(store.getName());
125
		if (result == null) {
126
			Iterator iter = this.layersToUseForStore.entrySet().iterator();
127
			Map.Entry entry;
128
			Class key;
129
			while (iter.hasNext()) {
130
				entry = (Entry) iter.next();
131
				if (entry.getKey() instanceof Class) {
132
					key = (Class) entry.getKey();
133
					if (key.isAssignableFrom(store.getClass())) {
134
						result = (Class) entry.getValue();
135
						break;
136
					}
137
				}
138
			}
139
		}
140
		return result;
119
//	private Class getLayerClassFor(DataStore store) {
120
//		Class result = (Class) this.layersToUseForStore.get(store.getName());
121
//		if (result == null) {
122
//			Iterator iter = this.layersToUseForStore.entrySet().iterator();
123
//			Map.Entry entry;
124
//			Class key;
125
//			while (iter.hasNext()) {
126
//				entry = (Entry) iter.next();
127
//				if (entry.getKey() instanceof Class) {
128
//					key = (Class) entry.getKey();
129
//					if (key.isAssignableFrom(store.getClass())) {
130
//						result = (Class) entry.getValue();
131
//						break;
132
//					}
133
//				}
134
//			}
135
//		}
136
//		return result;
137
//
138
//	}
141 139

  
142
	}
143 140

  
144 141

  
145

  
146 142
	/**
143
   * @param layerName
144
   * @param storeParameters
145
   * @return 
146
   * @throws org.gvsig.fmap.mapcontext.exceptions.LoadLayerException 
147 147
	 * @deprecated to be removed in gvSIG 2.1
148 148
	 * @see {@link MapContextManager}.
149 149
	 */
150
	public FLayer createLayer(String layerName,
151
			DataStoreParameters storeParameters) throws LoadLayerException {
152
		// Se obtiene el driver que lee
153
		try{
154
			DataManager dataManager=DALLocator.getDataManager();
155
			DataStore dataStore=dataManager.openStore(storeParameters.getDataStoreName(), storeParameters);
156
			return createLayer(layerName, dataStore);
157
		}catch (Exception e) {
158
			throw new LoadLayerException(layerName,e);
159
		}
150
	public FLayer createLayer(
151
          String layerName,
152
          DataStoreParameters storeParameters
153
    ) throws LoadLayerException {
154
    MapContextManager manager = MapContextLocator.getMapContextManager();
155
    return manager.createLayer(layerName, storeParameters);
156
//    // Se obtiene el driver que lee
157
//		try{
158
//			DataManager dataManager=DALLocator.getDataManager();
159
//			DataStore dataStore=dataManager.openStore(storeParameters.getDataStoreName(), storeParameters);
160
//			return createLayer(layerName, dataStore);
161
//		}catch (Exception e) {
162
//			throw new LoadLayerException(layerName,e);
163
//		}
160 164
	}
161 165

  
162 166
	/**
167
   * @param layerName
168
   * @param dataStore
169
   * @return 
170
   * @throws org.gvsig.fmap.mapcontext.exceptions.LoadLayerException 
163 171
	 * @deprecated to be removed in gvSIG 2.1
164 172
	 * @see {@link MapContextManager}.
165 173
	 */
166
	public FLayer createLayer(String layerName, DataStore dataStore) throws LoadLayerException{
167
		try{	
168
			Class layerClass = this.getLayerClassFor(dataStore);
169
			if (layerClass == null) {
170
				throw new CantRetrieveLayerByStoreException(layerName,dataStore.getName());
171
			}
172
			FLayer layer;
173
			try {
174
				layer = (FLayer) layerClass.newInstance();
175
			} catch (InstantiationException e) {
176
				throw new LoadLayerException(layerName, e);
177
			} catch (IllegalAccessException e) {
178
				throw new LoadLayerException(layerName, e);
179
			}
180

  
181
			layer.setName(layerName);
182
			((SingleLayer) layer).setDataStore(dataStore);
183
			IProjection proj = (IProjection)dataStore.getDynValue(FeatureStore.METADATA_CRS);
184
			if (proj != null) {
185
				layer.setProjection(proj);
186
			}
187
			layer.load();
188
			return layer;
189
		} catch (Exception e) {
190
			throw new LoadLayerException(layerName,e);
191
		}
174
	public FLayer createLayer(
175
          String layerName, 
176
          DataStore dataStore
177
    ) throws LoadLayerException{
178
    MapContextManager manager = MapContextLocator.getMapContextManager();
179
    return manager.createLayer(layerName, dataStore);
180
//		try{	
181
//			Class layerClass = this.getLayerClassFor(dataStore);
182
//			if (layerClass == null) {
183
//				throw new CantRetrieveLayerByStoreException(layerName,dataStore.getName());
184
//			}
185
//			FLayer layer;
186
//			try {
187
//				layer = (FLayer) layerClass.newInstance();
188
//			} catch (InstantiationException e) {
189
//				throw new LoadLayerException(layerName, e);
190
//			} catch (IllegalAccessException e) {
191
//				throw new LoadLayerException(layerName, e);
192
//			}
193
//
194
//			layer.setName(layerName);
195
//			((SingleLayer) layer).setDataStore(dataStore);
196
//			IProjection proj = (IProjection)dataStore.getDynValue(FeatureStore.METADATA_CRS);
197
//			if (proj != null) {
198
//				layer.setProjection(proj);
199
//			}
200
//			layer.load();
201
//			return layer;
202
//		} catch (Exception e) {
203
//			throw new LoadLayerException(layerName,e);
204
//		}
192 205
	}
193 206
	
194 207
	private class CantRetrieveLayerByStoreException extends LoadLayerException {

Also available in: Unified diff