Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / layers / LayerFactory.java @ 28665

History | View | Annotate | Download (5.3 KB)

1
package org.gvsig.fmap.mapcontext.layers;
2

    
3
import java.util.HashMap;
4
import java.util.Iterator;
5
import java.util.Map;
6
import java.util.Map.Entry;
7

    
8
import org.cresques.cts.IProjection;
9
import org.gvsig.fmap.crs.CRSFactory;
10
import org.gvsig.fmap.dal.DALLocator;
11
import org.gvsig.fmap.dal.DataManager;
12
import org.gvsig.fmap.dal.DataStore;
13
import org.gvsig.fmap.dal.DataStoreParameters;
14
import org.gvsig.fmap.dal.exception.InitializeException;
15
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
16
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
17
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer;
18
import org.slf4j.Logger;
19
import org.slf4j.LoggerFactory;
20

    
21
/**
22
 * Crea un adaptador del driver que se le pasa como par?metro en los m?todos
23
 * createLayer. Si hay memoria suficiente se crea un FLyrMemory que pasa todas
24
 * las features del driver a memoria
25
 */
26
public class LayerFactory {
27
        final static private Logger logger = LoggerFactory.getLogger(LayerFactory.class);
28

    
29
        private static LayerFactory instance = null;
30

    
31

    
32

    
33
        public static LayerFactory getInstance() {
34
                if (instance == null) {
35
                        instance = new LayerFactory();
36
                }
37
                return instance;
38
        }
39

    
40
        /**
41
         * Guarda registro de que clase de capa debe usar para un determinado Store
42
         *
43
         * como clave usamos el nombre de registro del dataStore
44
         */
45
        private Map layersToUseForStore = new HashMap();
46

    
47
        /**
48
         * Registra que clase tiene que usar para un {@link DataStore} determinado. <br>
49
         * Por defecto, si el
50
         *
51
         *
52
         * @param dataStoreName
53
         *            Nombre de registro del {@link DataStore} dentro del
54
         *            {@link DataManager}
55
         * @param layerClassToUse
56
         *            clase que implementa {@link SingleLayer}
57
         * @return
58
         */
59

    
60
        public boolean registerLayerToUseForStore(String dataStoreName,
61
                        Class layerClassToUse) {
62

    
63
                DataManager dm = DALLocator.getDataManager();
64
                DataStoreParameters dsparams;
65
                try {
66
                        dsparams = dm
67
                        .createStoreParameters(dataStoreName);
68
                } catch (InitializeException e) {
69
                        e.printStackTrace();
70
                        return false;
71
                } catch (ProviderNotRegisteredException e) {
72
                        e.printStackTrace();
73
                        return false;
74
                }
75
                if (!layerClassToUse.isAssignableFrom(SingleLayer.class)) {
76
                        return false;
77
                }
78
                this.layersToUseForStore.put(dsparams.getDataStoreName(),
79
                                layerClassToUse);
80

    
81
                return true;
82
        }
83

    
84
        public boolean registerLayerToUseForStore(Class storeClass,
85
                        Class layerClassToUse) {
86

    
87
                DataManager dm = DALLocator.getDataManager();
88
                if (!DataStore.class.isAssignableFrom(storeClass)) {
89
                        return false;
90
                }
91

    
92
                if (!SingleLayer.class.isAssignableFrom(layerClassToUse)
93
                                || !FLayer.class.isAssignableFrom(layerClassToUse)) {
94
                        return false;
95
                }
96
                this.layersToUseForStore.put(storeClass,
97
                                layerClassToUse);
98

    
99
                return true;
100
        }
101

    
102
        private Class getLayerClassFor(DataStore store) {
103
                Class result = (Class) this.layersToUseForStore.get(store.getName());
104
                if (result == null) {
105
                        Iterator iter = this.layersToUseForStore.entrySet().iterator();
106
                        Map.Entry entry;
107
                        Class key;
108
                        while (iter.hasNext()) {
109
                                entry = (Entry) iter.next();
110
                                if (entry.getKey() instanceof Class) {
111
                                        key = (Class) entry.getKey();
112
                                        if (key.isAssignableFrom(store.getClass())) {
113
                                                result = (Class) entry.getValue();
114
                                                break;
115
                                        }
116
                                }
117
                        }
118
                }
119
                return result;
120

    
121
        }
122

    
123

    
124

    
125
        /*
126
         * TODO Documentation
127
         *
128
         * @param layerName Nombre de la capa. @param driverName Nombre del driver.
129
         *
130
         * @param f fichero. @param proj Proyecci?n.
131
         *
132
         * @return FLayer. @throws DriverException
133
         *
134
         * @throws DriverException @throws DriverIOException
135
         */
136
        public FLayer createLayer(String layerName,
137
                        DataStoreParameters storeParameters) throws LoadLayerException {
138
                // Se obtiene el driver que lee
139
                try{
140
                        DataManager dataManager=DALLocator.getDataManager();
141
                        DataStore dataStore=dataManager.createStore(storeParameters);
142
                        return createLayer(layerName, dataStore);
143
                }catch (Exception e) {
144
                        throw new LoadLayerException(layerName,e);
145
                }
146
        }
147

    
148
        /**
149
         * Create a layer form a DataStore.
150
         * @param layerName
151
         * The name of the layer
152
         * @param dataStore
153
         * The datastore
154
         * @return
155
         * The layer to load
156
         * @throws LoadLayerException
157
         */
158
        public FLayer createLayer(String layerName, DataStore dataStore) throws LoadLayerException{
159
                try{        
160
                        Class layerClass = this.getLayerClassFor(dataStore);
161
                        if (layerClass == null) {
162
                                throw new LoadLayerException("No_layer_class_to_use",
163
                                                new Exception());
164
                        }
165
                        FLayer layer;
166
                        try {
167
                                layer = (FLayer) layerClass.newInstance();
168
                        } catch (InstantiationException e) {
169
                                throw new LoadLayerException(layerName, e);
170
                        } catch (IllegalAccessException e) {
171
                                throw new LoadLayerException(layerName, e);
172
                        }
173

    
174
                        ((SingleLayer) layer).setDataStore(dataStore);
175
                        layer.setName(layerName);
176
                        Object srsObj = dataStore.getDynValue("DefaultSRS");
177
                        if (srsObj != null) {
178
                                IProjection proj = null;
179
                                if (srsObj instanceof IProjection) {
180
                                        proj = (IProjection) srsObj;
181
                                } else if (srsObj instanceof String) {
182
                                        proj = CRSFactory.getCRS((String) srsObj);
183
                                }
184
                                if (proj != null) {
185
                                        layer.setProjection(proj);
186
                                }
187
                        }
188

    
189

    
190
                        return layer;
191
                } catch (Exception e) {
192
                        throw new LoadLayerException(layerName,e);
193
                }
194
        }
195

    
196
}