Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / org.gvsig.newlayer / org.gvsig.newlayer.lib / org.gvsig.newlayer.lib.impl / src / main / java / org / gvsig / newlayer / impl / DefaultNewLayerService.java @ 38560

History | View | Annotate | Download (7.26 KB)

1 36022 fdiaz
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.newlayer.impl;
23
24
import java.util.ArrayList;
25
import java.util.Iterator;
26
import java.util.List;
27
28
import org.gvsig.fmap.dal.DALLocator;
29
import org.gvsig.fmap.dal.DataManager;
30
import org.gvsig.fmap.dal.DataServerExplorer;
31 36220 fdiaz
import org.gvsig.fmap.dal.DataStore;
32 38444 cordinyana
import org.gvsig.fmap.dal.DataStoreParameters;
33 36022 fdiaz
import org.gvsig.fmap.dal.exception.DataException;
34 36220 fdiaz
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
35 36022 fdiaz
import org.gvsig.fmap.dal.feature.EditableFeatureType;
36
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
37 36220 fdiaz
import org.gvsig.fmap.mapcontext.MapContext;
38
import org.gvsig.fmap.mapcontext.MapContextLocator;
39
import org.gvsig.fmap.mapcontext.MapContextManager;
40
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
41
import org.gvsig.fmap.mapcontext.layers.FLayer;
42 36022 fdiaz
import org.gvsig.newlayer.NewLayerManager;
43
import org.gvsig.newlayer.NewLayerProvider;
44
import org.gvsig.newlayer.NewLayerProviderFactory;
45
import org.gvsig.newlayer.NewLayerService;
46
import org.gvsig.newlayer.NewLayerServiceException;
47 37891 cordinyana
import org.gvsig.tools.service.ServiceException;
48 36022 fdiaz
49
/**
50
 * Default {@link NewLayerService} implementation.
51
 *
52
 * @author gvSIG Team
53
 * @version $Id$
54
 */
55
public class DefaultNewLayerService implements NewLayerService {
56
57
    private NewLayerManager newLayerManager;
58
    private NewLayerProviderFactory type;
59
    private NewLayerProvider provider;
60 36220 fdiaz
    private boolean addLayerToView = true;
61
    private MapContext mapContext;
62 36022 fdiaz
63
    /**
64 37891 cordinyana
     * {@link DefaultNewLayerService} constructor
65 36022 fdiaz
     */
66 37891 cordinyana
    public DefaultNewLayerService(NewLayerManager manager, MapContext mapContext) {
67 36022 fdiaz
        this.newLayerManager = manager;
68 36220 fdiaz
        this.mapContext = mapContext;
69 36022 fdiaz
    }
70
71 37891 cordinyana
    public NewLayerProvider getProvider() {
72
        return provider;
73 36022 fdiaz
    }
74 36220 fdiaz
75 37891 cordinyana
    public void createLayer() throws NewLayerServiceException {
76
        try {
77
            this.getExplorer().add(this.getStoreName(),
78
                this.getNewStoreParameters(), true);
79
        } catch (DataException e) {
80
            throw new CantCreateNewLayerException(e);
81
        }
82
    }
83 36220 fdiaz
84 37891 cordinyana
    public void loadLayer() throws NewLayerServiceException {
85
        try {
86
            String storeName = this.getStoreName();
87 38444 cordinyana
            DataStoreParameters storeParameters =
88
                this.getOpenStoreParameters();
89 37891 cordinyana
            DataManager dataManager = DALLocator.getDataManager();
90
            DataStore store = dataManager.openStore(storeName, storeParameters);
91
            String layerName = store.getName();
92 36220 fdiaz
93 37891 cordinyana
            MapContextManager mapContextManager =
94
                MapContextLocator.getMapContextManager();
95
            FLayer lyr = mapContextManager.createLayer(layerName, store);
96
            lyr.setActive(true);
97
            lyr.setVisible(true);
98
            mapContext.getLayers().addLayer(lyr);
99 36220 fdiaz
100 37891 cordinyana
            lyr.dispose();
101
        } catch (DataException e) {
102
            throw new CantOpenStoreException(e);
103
        } catch (ValidateDataParametersException e) {
104
            throw new CantOpenStoreException(e);
105
        } catch (LoadLayerException e) {
106
            throw new CantLoadNewLayerException(e);
107
        }
108
109
    }
110
111
    public class CantCreateNewLayerException extends NewLayerServiceException {
112
113
        /**
114 36022 fdiaz
                 *
115
                 */
116 37891 cordinyana
        private static final long serialVersionUID = 4208215791054246118L;
117 36022 fdiaz
118 37891 cordinyana
        public CantCreateNewLayerException(Throwable cause) {
119
            super("Can't create the layer", cause, "_cant_create_the_layer",
120
                serialVersionUID);
121
        }
122
    }
123 36022 fdiaz
124 37891 cordinyana
    public class CantOpenStoreException extends NewLayerServiceException {
125 36220 fdiaz
126 37891 cordinyana
        /**
127 36220 fdiaz
                 *
128
                 */
129 37891 cordinyana
        private static final long serialVersionUID = -2245228621032918630L;
130 36220 fdiaz
131 37891 cordinyana
        public CantOpenStoreException(Throwable cause) {
132
            super("Can't open store", cause, "_cant_open_store",
133
                serialVersionUID);
134
        }
135
    }
136 36220 fdiaz
137 37891 cordinyana
    public class CantLoadNewLayerException extends NewLayerServiceException {
138 36220 fdiaz
139 37891 cordinyana
        /**
140 36220 fdiaz
                 *
141
                 */
142 37891 cordinyana
        private static final long serialVersionUID = -1711950651766745963L;
143 36220 fdiaz
144 37891 cordinyana
        public CantLoadNewLayerException(Throwable cause) {
145
            super("Can't load the new layer", cause,
146
                "_cant_load_the_new_layer", serialVersionUID);
147
        }
148
    }
149 36220 fdiaz
150 37891 cordinyana
    public void setType(String type) {
151
        try {
152
            setProviderFactory(this.newLayerManager
153
                .getNewLayerProviderFactory(type));
154
        } catch (ServiceException e) {
155
            throw new RuntimeException(e);
156
        }
157
    }
158 36022 fdiaz
159 37891 cordinyana
    public DataServerExplorer getExplorer() {
160
        return getProvider().getExplorer();
161
    }
162 36022 fdiaz
163 37891 cordinyana
    public String getStoreName() {
164
        return getProvider().getStoreName();
165
    }
166 36022 fdiaz
167 37891 cordinyana
    public EditableFeatureType getFeatureType() {
168
        return getProvider().getFeatureType();
169
    }
170 36022 fdiaz
171 37891 cordinyana
    public NewFeatureStoreParameters getNewStoreParameters() {
172
        return getProvider().getNewStoreParameters();
173
    }
174 36022 fdiaz
175 38444 cordinyana
    public DataStoreParameters getOpenStoreParameters() {
176
        return getProvider().getOpenStoreParameters();
177
    }
178
179 37891 cordinyana
    public List<String> getTypes() {
180
        List<String> types = new ArrayList<String>();
181
        List<NewLayerProviderFactory> providers = getProviderFactories();
182
        Iterator<NewLayerProviderFactory> it = providers.iterator();
183
        while (it.hasNext()) {
184
            NewLayerProviderFactory newLayerProviderFactory = it.next();
185
            types.add(newLayerProviderFactory.getName());
186
        }
187
        return types;
188
    }
189 36022 fdiaz
190 37891 cordinyana
    public String getType() {
191
        return this.type.getName();
192
    }
193 36022 fdiaz
194 37891 cordinyana
    public void addLayerToView(boolean b) {
195
        this.addLayerToView = b;
196
    }
197 36220 fdiaz
198 37891 cordinyana
    public MapContext getMapContext() {
199
        return mapContext;
200
    }
201 36721 fdiaz
202 37891 cordinyana
    public void setProviderFactory(NewLayerProviderFactory type) {
203
        this.type = type;
204
        this.provider = type.create(this);
205
    }
206
207
    public NewLayerProviderFactory getProviderFactory() {
208
        return type;
209
    }
210
211
    public List<NewLayerProviderFactory> getProviderFactories() {
212
213
        List<NewLayerProviderFactory> providers =
214
            new ArrayList<NewLayerProviderFactory>(
215
                this.newLayerManager
216
                    .getProviders(NewLayerManager.STORETYPE.ANY));
217
218
        for (Iterator<NewLayerProviderFactory> iterator = providers.iterator(); iterator
219
            .hasNext();) {
220
            NewLayerProviderFactory newLayerProviderFactory = iterator.next();
221
            if (!newLayerProviderFactory.isEnabled()) {
222
                iterator.remove();
223
            }
224
        }
225
226
        return providers;
227
    }
228
229 36022 fdiaz
}