svn-gvsig-desktop / tags / v2_0_0_Build_2055 / libraries / org.gvsig.newlayer / org.gvsig.newlayer.prov / org.gvsig.newlayer.prov.file / src / main / java / org / gvsig / newlayer / prov / file / AbstractNewLayerFileProvider.java @ 38983
History | View | Annotate | Download (7.23 KB)
1 | 37804 | jpiera | /* 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.prov.file; |
||
23 | |||
24 | import java.io.File; |
||
25 | import java.util.ArrayList; |
||
26 | import java.util.List; |
||
27 | |||
28 | import org.slf4j.Logger; |
||
29 | import org.slf4j.LoggerFactory; |
||
30 | |||
31 | import org.gvsig.fmap.dal.DALLocator; |
||
32 | import org.gvsig.fmap.dal.DataManager; |
||
33 | import org.gvsig.fmap.dal.DataServerExplorerParameters; |
||
34 | 38444 | cordinyana | import org.gvsig.fmap.dal.DataStoreParameters; |
35 | 37804 | jpiera | import org.gvsig.fmap.dal.exception.DataException; |
36 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
37 | import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
||
38 | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
||
39 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
40 | import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
||
41 | import org.gvsig.newlayer.NewLayerProviderPanel; |
||
42 | import org.gvsig.newlayer.NewLayerService; |
||
43 | import org.gvsig.newlayer.spi.AbstractNewLayerProvider; |
||
44 | import org.gvsig.tools.exception.BaseRuntimeException; |
||
45 | |||
46 | /**
|
||
47 | * NewLayer provider to create a shape file.
|
||
48 | *
|
||
49 | * @author gvSIG Team
|
||
50 | * @version $Id$
|
||
51 | */
|
||
52 | public abstract class AbstractNewLayerFileProvider extends AbstractNewLayerProvider { |
||
53 | protected static final Logger LOG = |
||
54 | LoggerFactory.getLogger(AbstractNewLayerFileProvider.class); |
||
55 | |||
56 | protected List<NewLayerProviderPanel> panels = new ArrayList(); |
||
57 | protected NewLayerFileProviderPanel fileProviderPanel = null; |
||
58 | protected NewFeatureStoreParameters storeParameters = null; |
||
59 | |||
60 | protected AbstractNewLayerFileProvider(NewLayerService service) {
|
||
61 | super(service);
|
||
62 | fileProviderPanel = new NewLayerFileProviderPanel(this); |
||
63 | panels.add(fileProviderPanel); |
||
64 | } |
||
65 | |||
66 | public List<NewLayerProviderPanel> getPanels() { |
||
67 | return panels;
|
||
68 | } |
||
69 | |||
70 | public EditableFeatureType getFeatureType() {
|
||
71 | return this.getNewStoreParameters().getDefaultFeatureType(); |
||
72 | } |
||
73 | |||
74 | protected abstract String getFileParameterName(); |
||
75 | |||
76 | protected abstract String getFileExtension(); |
||
77 | |||
78 | public NewFeatureStoreParameters getNewStoreParameters() {
|
||
79 | DataManager dataManager = DALLocator.getDataManager(); |
||
80 | |||
81 | try {
|
||
82 | if (this.explorer == null){ |
||
83 | DataServerExplorerParameters explorerParameters = |
||
84 | dataManager.createServerExplorerParameters("FilesystemExplorer");
|
||
85 | this.explorer =
|
||
86 | dataManager.openServerExplorer("FilesystemExplorer", explorerParameters);
|
||
87 | } |
||
88 | |||
89 | File selectedFile = fileProviderPanel.getSelectedFile();
|
||
90 | String fileName = selectedFile.getName();
|
||
91 | if (fileName.indexOf(".") < 0){ |
||
92 | fileName = fileName + "." + this.getFileExtension(); |
||
93 | selectedFile = new File(selectedFile.getParent() + File.separator + fileName); |
||
94 | } |
||
95 | |||
96 | //If the file has not been updated, returns the same parameters
|
||
97 | if (this.storeParameters != null){ |
||
98 | File previousFile = (File)storeParameters.getDynValue(getFileParameterName()); |
||
99 | if (previousFile.equals(selectedFile)){
|
||
100 | return storeParameters;
|
||
101 | } |
||
102 | } |
||
103 | |||
104 | storeParameters = |
||
105 | (NewFeatureStoreParameters) explorer.getAddParameters(this.getStoreName());
|
||
106 | storeParameters.setDynValue( |
||
107 | getFileParameterName(), selectedFile); |
||
108 | return storeParameters;
|
||
109 | } catch (InitializeException e) {
|
||
110 | LOG.error("Can't initialize store parameters.");
|
||
111 | throw new CantInitializeStoreParametersException(e); |
||
112 | } catch (ProviderNotRegisteredException e) {
|
||
113 | LOG.error("Can't create store parameters, provider not registered.");
|
||
114 | throw new CantCreateStoreParametersException(e); |
||
115 | } catch (DataException e) {
|
||
116 | LOG.error("Can't create store parameters, provider not registered.");
|
||
117 | throw new CantCreateStoreParametersException(e); |
||
118 | } catch (ValidateDataParametersException e) {
|
||
119 | LOG.error("Can't create the explorter.");
|
||
120 | throw new CantCreateExplorerException(e); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | 38444 | cordinyana | public DataStoreParameters getOpenStoreParameters() {
|
125 | DataManager dataManager = DALLocator.getDataManager(); |
||
126 | |||
127 | try {
|
||
128 | File selectedFile = fileProviderPanel.getSelectedFile();
|
||
129 | String fileName = selectedFile.getName();
|
||
130 | if (fileName.indexOf(".") < 0) { |
||
131 | fileName = fileName + "." + this.getFileExtension(); |
||
132 | selectedFile = new File(selectedFile.getParent() |
||
133 | + File.separator + fileName);
|
||
134 | } |
||
135 | |||
136 | DataStoreParameters storeParameters = (DataStoreParameters) dataManager |
||
137 | .createStoreParameters(this.getStoreName());
|
||
138 | storeParameters.setDynValue(getFileParameterName(), selectedFile); |
||
139 | return storeParameters;
|
||
140 | } catch (InitializeException e) {
|
||
141 | throw new CantInitializeStoreParametersException(e); |
||
142 | } catch (ProviderNotRegisteredException e) {
|
||
143 | throw new CantCreateStoreParametersException(e); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | 37804 | jpiera | class CantCreateExplorerException extends BaseRuntimeException { |
148 | private static final long serialVersionUID = -3487961532247258393L; |
||
149 | private final static String MESSAGE_FORMAT = "Can't create explorer."; |
||
150 | private final static String MESSAGE_KEY = "_Cant_create_explorer"; |
||
151 | |||
152 | public CantCreateExplorerException(Throwable cause) { |
||
153 | super(MESSAGE_FORMAT, cause, MESSAGE_KEY, serialVersionUID);
|
||
154 | } |
||
155 | } |
||
156 | |||
157 | class CantCreateStoreParametersException extends BaseRuntimeException { |
||
158 | private static final long serialVersionUID = -3487961532247258393L; |
||
159 | private final static String MESSAGE_FORMAT = "Can't create store parameters, provider not registered."; |
||
160 | private final static String MESSAGE_KEY = "_Cant_create_store_parameters_provider_not_registered"; |
||
161 | |||
162 | public CantCreateStoreParametersException(Throwable cause) { |
||
163 | super(MESSAGE_FORMAT, cause, MESSAGE_KEY, serialVersionUID);
|
||
164 | } |
||
165 | } |
||
166 | |||
167 | class CantInitializeStoreParametersException extends BaseRuntimeException { |
||
168 | private static final long serialVersionUID = -5263149018209261288L; |
||
169 | private final static String MESSAGE_FORMAT = "Can't initialize store parameters."; |
||
170 | private final static String MESSAGE_KEY = "_Cant_initialize_store_parameters"; |
||
171 | |||
172 | public CantInitializeStoreParametersException(Throwable cause) { |
||
173 | super(MESSAGE_FORMAT, cause, MESSAGE_KEY, serialVersionUID);
|
||
174 | } |
||
175 | } |
||
176 | |||
177 | } |