svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.newlayer / org.gvsig.newlayer.lib / org.gvsig.newlayer.lib.impl / src / main / java / org / gvsig / newlayer / impl / DefaultNewLayerService.java @ 45564
History | View | Annotate | Download (8.28 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.newlayer.impl; |
25 |
|
26 |
import java.util.ArrayList; |
27 |
import java.util.Iterator; |
28 |
import java.util.List; |
29 |
|
30 |
import org.gvsig.fmap.dal.DALLocator; |
31 |
import org.gvsig.fmap.dal.DataManager; |
32 |
import org.gvsig.fmap.dal.DataServerExplorer; |
33 |
import org.gvsig.fmap.dal.DataStore; |
34 |
import org.gvsig.fmap.dal.DataStoreParameters; |
35 |
import org.gvsig.fmap.dal.exception.DataException; |
36 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
37 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
38 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
39 |
import org.gvsig.fmap.mapcontext.MapContext; |
40 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
41 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
42 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
43 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
44 |
import org.gvsig.newlayer.NewLayerException; |
45 |
import org.gvsig.newlayer.NewLayerManager; |
46 |
import org.gvsig.newlayer.NewLayerProvider; |
47 |
import org.gvsig.newlayer.NewLayerProviderFactory; |
48 |
import org.gvsig.newlayer.NewLayerService; |
49 |
import org.gvsig.newlayer.NewLayerServiceException; |
50 |
import org.gvsig.tools.service.ServiceException; |
51 |
|
52 |
/**
|
53 |
* Default {@link NewLayerService} implementation.
|
54 |
*
|
55 |
* @author gvSIG Team
|
56 |
*/
|
57 |
@SuppressWarnings("UseSpecificCatch") |
58 |
public class DefaultNewLayerService implements NewLayerService { |
59 |
|
60 |
private final NewLayerManager newLayerManager; |
61 |
private final MapContext mapContext; |
62 |
private NewLayerProviderFactory type;
|
63 |
private NewLayerProvider provider;
|
64 |
private boolean addLayerToView = true; |
65 |
|
66 |
/**
|
67 |
* {@link DefaultNewLayerService} constructor
|
68 |
* @param manager
|
69 |
* @param mapContext
|
70 |
*/
|
71 |
public DefaultNewLayerService(NewLayerManager manager, MapContext mapContext) {
|
72 |
this.newLayerManager = manager;
|
73 |
this.mapContext = mapContext;
|
74 |
} |
75 |
|
76 |
@Override
|
77 |
public NewLayerProvider getProvider() {
|
78 |
return provider;
|
79 |
} |
80 |
|
81 |
@Override
|
82 |
public void createTable() throws NewLayerServiceException { |
83 |
try {
|
84 |
this.getExplorer().add(
|
85 |
this.getStoreName(),
|
86 |
this.getNewStoreParameters(),
|
87 |
true
|
88 |
); |
89 |
} catch (Exception e) { |
90 |
throw new CantCreateNewLayerException(e); |
91 |
} |
92 |
} |
93 |
|
94 |
@Override
|
95 |
public void loadTable() throws NewLayerServiceException { |
96 |
try {
|
97 |
if( this.getFeatureType().getDefaultGeometryAttribute()==null ) { |
98 |
return;
|
99 |
} |
100 |
String storeName = this.getStoreName(); |
101 |
DataStoreParameters storeParameters = this.getOpenStoreParameters();
|
102 |
DataManager dataManager = DALLocator.getDataManager(); |
103 |
DataStore store = dataManager.openStore(storeName, storeParameters); |
104 |
String layerName = store.getName();
|
105 |
|
106 |
MapContextManager mapContextManager = MapContextLocator.getMapContextManager(); |
107 |
FLayer lyr = mapContextManager.createLayer(layerName, store); |
108 |
lyr.setActive(true);
|
109 |
lyr.setVisible(true);
|
110 |
mapContext.getLayers().addLayer(lyr); |
111 |
|
112 |
lyr.dispose(); |
113 |
} catch (DataException | ValidateDataParametersException e) {
|
114 |
throw new CantOpenStoreException(e); |
115 |
} catch (LoadLayerException e) {
|
116 |
throw new CantLoadNewLayerException(e); |
117 |
} catch (Exception e) { |
118 |
throw new NewLayerServiceException(e); |
119 |
} |
120 |
|
121 |
} |
122 |
|
123 |
public class CantCreateNewLayerException extends NewLayerServiceException { |
124 |
|
125 |
/**
|
126 |
*
|
127 |
*/
|
128 |
private static final long serialVersionUID = 4208215791054246118L; |
129 |
|
130 |
public CantCreateNewLayerException(Throwable cause) { |
131 |
super("Can't create the layer", cause, "_cant_create_the_layer", |
132 |
serialVersionUID); |
133 |
} |
134 |
} |
135 |
|
136 |
public class CantOpenStoreException extends NewLayerServiceException { |
137 |
|
138 |
/**
|
139 |
*
|
140 |
*/
|
141 |
private static final long serialVersionUID = -2245228621032918630L; |
142 |
|
143 |
public CantOpenStoreException(Throwable cause) { |
144 |
super("Can't open store", cause, "_cant_open_store", |
145 |
serialVersionUID); |
146 |
} |
147 |
} |
148 |
|
149 |
public class CantLoadNewLayerException extends NewLayerServiceException { |
150 |
|
151 |
/**
|
152 |
*
|
153 |
*/
|
154 |
private static final long serialVersionUID = -1711950651766745963L; |
155 |
|
156 |
public CantLoadNewLayerException(Throwable cause) { |
157 |
super("Can't load the new layer", cause, |
158 |
"_cant_load_the_new_layer", serialVersionUID);
|
159 |
} |
160 |
} |
161 |
|
162 |
@Override
|
163 |
public void setType(String type) { |
164 |
try {
|
165 |
setProviderFactory(this.newLayerManager.getNewLayerProviderFactory(type));
|
166 |
} catch (ServiceException e) {
|
167 |
throw new RuntimeException(e); |
168 |
} |
169 |
} |
170 |
|
171 |
@Override
|
172 |
public DataServerExplorer getExplorer() throws NewLayerException { |
173 |
return getProvider().getExplorer();
|
174 |
} |
175 |
|
176 |
@Override
|
177 |
public String getStoreName() { |
178 |
return getProvider().getStoreName();
|
179 |
} |
180 |
|
181 |
@Override
|
182 |
public EditableFeatureType getFeatureType() {
|
183 |
return getProvider().getFeatureType();
|
184 |
} |
185 |
|
186 |
@Override
|
187 |
public NewFeatureStoreParameters getNewStoreParameters() throws NewLayerException { |
188 |
return getProvider().getNewStoreParameters();
|
189 |
} |
190 |
|
191 |
@Override
|
192 |
public DataStoreParameters getOpenStoreParameters() throws NewLayerException { |
193 |
return getProvider().getOpenStoreParameters();
|
194 |
} |
195 |
|
196 |
@Override
|
197 |
public NewFeatureStoreParameters getNewStoreParametersQuietly() {
|
198 |
try {
|
199 |
return getProvider().getNewStoreParameters();
|
200 |
} catch(Exception ex) { |
201 |
return null; |
202 |
} |
203 |
} |
204 |
|
205 |
@Override
|
206 |
public DataStoreParameters getOpenStoreParametersQuietly() {
|
207 |
try {
|
208 |
return getProvider().getOpenStoreParameters();
|
209 |
} catch(Exception ex) { |
210 |
return null; |
211 |
} |
212 |
} |
213 |
|
214 |
@Override
|
215 |
public List<String> getTypes() { |
216 |
List<String> types = new ArrayList<>(); |
217 |
List<NewLayerProviderFactory> providers = getProviderFactories();
|
218 |
Iterator<NewLayerProviderFactory> it = providers.iterator();
|
219 |
while (it.hasNext()) {
|
220 |
NewLayerProviderFactory newLayerProviderFactory = it.next(); |
221 |
types.add(newLayerProviderFactory.getName()); |
222 |
} |
223 |
return types;
|
224 |
} |
225 |
|
226 |
@Override
|
227 |
public String getType() { |
228 |
return this.type.getName(); |
229 |
} |
230 |
|
231 |
@Override
|
232 |
public void addLayerToView(boolean b) { |
233 |
this.addLayerToView = b;
|
234 |
} |
235 |
|
236 |
@Override
|
237 |
public MapContext getMapContext() {
|
238 |
return mapContext;
|
239 |
} |
240 |
|
241 |
@Override
|
242 |
public void setProviderFactory(NewLayerProviderFactory type) { |
243 |
this.type = type;
|
244 |
this.provider = type.create(this); |
245 |
} |
246 |
|
247 |
@Override
|
248 |
public NewLayerProviderFactory getProviderFactory() {
|
249 |
return type;
|
250 |
} |
251 |
|
252 |
@Override
|
253 |
public List<NewLayerProviderFactory> getProviderFactories() { |
254 |
|
255 |
List<NewLayerProviderFactory> providers = new ArrayList<>( |
256 |
this.newLayerManager.getProviders(NewLayerManager.STORETYPE.ANY)
|
257 |
); |
258 |
for (Iterator<NewLayerProviderFactory> iterator = providers.iterator(); iterator.hasNext();) { |
259 |
NewLayerProviderFactory newLayerProviderFactory = iterator.next(); |
260 |
if (!newLayerProviderFactory.isEnabled()) {
|
261 |
iterator.remove(); |
262 |
} |
263 |
} |
264 |
return providers;
|
265 |
} |
266 |
|
267 |
} |