svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / impl / DefaultDatabaseWorkspaceManager.java @ 44304
History | View | Annotate | Download (12.4 KB)
1 |
package org.gvsig.fmap.dal.impl; |
---|---|
2 |
|
3 |
import org.gvsig.fmap.dal.DatabaseWorkspaceManager; |
4 |
import org.apache.commons.lang3.StringUtils; |
5 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
6 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
7 |
import org.gvsig.fmap.dal.DALLocator; |
8 |
import org.gvsig.fmap.dal.DataManager; |
9 |
import org.gvsig.fmap.dal.DataServerExplorer; |
10 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
11 |
import org.gvsig.fmap.dal.DataStoreParameters; |
12 |
import org.gvsig.fmap.dal.DataTypes; |
13 |
import org.gvsig.fmap.dal.StoresRepository; |
14 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
15 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
16 |
import org.gvsig.fmap.dal.feature.Feature; |
17 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
18 |
//import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters;
|
19 |
//import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer;
|
20 |
//import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
|
21 |
//import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
|
22 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_REPOSITORY_NAME; |
23 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
24 |
import org.slf4j.Logger; |
25 |
import org.slf4j.LoggerFactory; |
26 |
|
27 |
/**
|
28 |
*
|
29 |
* @author jjdelcerro
|
30 |
*/
|
31 |
@SuppressWarnings("UseSpecificCatch") |
32 |
public class DefaultDatabaseWorkspaceManager implements DatabaseWorkspaceManager { |
33 |
|
34 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultDatabaseWorkspaceManager.class); |
35 |
|
36 |
private final DataServerExplorerParameters connection; |
37 |
private Boolean existsConfiguration = null; |
38 |
private StoresRepository storesRepository;
|
39 |
|
40 |
public DefaultDatabaseWorkspaceManager(DataServerExplorerParameters connection) {
|
41 |
this.connection = connection;
|
42 |
} |
43 |
|
44 |
@Override
|
45 |
public boolean existsTable(int tableid) { |
46 |
switch(tableid) {
|
47 |
case TABLE_RESOURCES:
|
48 |
return this.existsTable(TABLE_RESOURCES_NAME); |
49 |
case TABLE_REPOSITORY:
|
50 |
return this.existsTable(TABLE_REPOSITORY_NAME); |
51 |
case TABLE_CONFIGURATION:
|
52 |
return this.existsTable(TABLE_CONFIGURATION_NAME); |
53 |
default:
|
54 |
throw new IllegalArgumentException("Invalid table identitier "+tableid); |
55 |
} |
56 |
} |
57 |
|
58 |
@Override
|
59 |
public void createTable(int tableid) { |
60 |
switch(tableid) {
|
61 |
case TABLE_RESOURCES:
|
62 |
createTableResources(); |
63 |
break;
|
64 |
case TABLE_REPOSITORY:
|
65 |
createTableRepository(); |
66 |
break;
|
67 |
case TABLE_CONFIGURATION:
|
68 |
createTableConfiguration(); |
69 |
break;
|
70 |
default:
|
71 |
throw new IllegalArgumentException("Invalid table identitier "+tableid); |
72 |
} |
73 |
} |
74 |
|
75 |
@Override
|
76 |
public void dropTable(int tableid) { |
77 |
switch(tableid) {
|
78 |
case TABLE_RESOURCES:
|
79 |
this.dropTable(TABLE_RESOURCES_NAME);
|
80 |
break;
|
81 |
case TABLE_REPOSITORY:
|
82 |
this.dropTable(TABLE_REPOSITORY_NAME);
|
83 |
break;
|
84 |
case TABLE_CONFIGURATION:
|
85 |
this.dropTable(TABLE_CONFIGURATION_NAME);
|
86 |
break;
|
87 |
default:
|
88 |
throw new IllegalArgumentException("Invalid table identitier "+tableid); |
89 |
} |
90 |
} |
91 |
|
92 |
@Override
|
93 |
public FeatureStore getTable(int tableid) { |
94 |
switch(tableid) {
|
95 |
case TABLE_RESOURCES:
|
96 |
return this.getTable(TABLE_RESOURCES_NAME); |
97 |
case TABLE_REPOSITORY:
|
98 |
return this.getTable(TABLE_REPOSITORY_NAME); |
99 |
case TABLE_CONFIGURATION:
|
100 |
return this.getTable(TABLE_CONFIGURATION_NAME); |
101 |
default:
|
102 |
throw new IllegalArgumentException("Invalid table identitier "+tableid); |
103 |
} |
104 |
} |
105 |
|
106 |
private boolean existsTable(String tableName) { |
107 |
try {
|
108 |
DataManager dataManager = DALLocator.getDataManager(); |
109 |
DataServerExplorer server = dataManager.openServerExplorer( |
110 |
this.connection.getExplorerName(),
|
111 |
this.connection
|
112 |
); |
113 |
for (DataStoreParameters params : server.list()) {
|
114 |
String theTableName = (String) params.getDynValue("Table"); |
115 |
if( StringUtils.equals(theTableName, tableName) ) {
|
116 |
return true; |
117 |
} |
118 |
} |
119 |
} catch (Exception ex) { |
120 |
LOGGER.warn("Can't check if the table '"+tableName+"' exists.",ex); |
121 |
} |
122 |
return false; |
123 |
} |
124 |
|
125 |
private FeatureStore getTable(String tableName) { |
126 |
try {
|
127 |
DataManager dataManager = DALLocator.getDataManager(); |
128 |
DataServerExplorer server = dataManager.openServerExplorer( |
129 |
this.connection.getExplorerName(),
|
130 |
this.connection
|
131 |
); |
132 |
DataStoreParameters params = server.get(tableName); |
133 |
if( params!=null ) { |
134 |
FeatureStore store = (FeatureStore) dataManager.openStore( |
135 |
params.getDataStoreName(), |
136 |
params |
137 |
); |
138 |
return store;
|
139 |
} |
140 |
} catch (Exception ex) { |
141 |
LOGGER.warn("Can't open table '"+tableName+"'.",ex); |
142 |
} |
143 |
return null; |
144 |
} |
145 |
|
146 |
private void dropTable(String tableName) { |
147 |
try {
|
148 |
DataManager dataManager = DALLocator.getDataManager(); |
149 |
DataServerExplorer server = dataManager.openServerExplorer( |
150 |
this.connection.getExplorerName(),
|
151 |
this.connection
|
152 |
); |
153 |
DataStoreParameters params = server.get(tableName); |
154 |
if( params!=null ) { |
155 |
server.remove(params); |
156 |
} |
157 |
} catch (Exception ex) { |
158 |
LOGGER.warn("Can't drop table '"+tableName+"'.",ex); |
159 |
} |
160 |
} |
161 |
|
162 |
private void createTableResources() { |
163 |
String tableName = TABLE_RESOURCES_NAME;
|
164 |
try {
|
165 |
DataManager dataManager = DALLocator.getDataManager(); |
166 |
DataServerExplorer server = dataManager.openServerExplorer( |
167 |
this.connection.getExplorerName(),
|
168 |
this.connection
|
169 |
); |
170 |
NewFeatureStoreParameters params = (NewFeatureStoreParameters) server.getAddParameters(tableName); |
171 |
EditableFeatureType ft = params.getDefaultFeatureType(); |
172 |
ft.add(FIELD_RESOURCES_NAME, DataTypes.STRING, 150)
|
173 |
.setAllowNull(false)
|
174 |
.setIsPrimaryKey(true);
|
175 |
ft.add(FIELD_RESOURCES_RESOURCE, DataTypes.BYTEARRAY) |
176 |
.setAllowNull(true);
|
177 |
server.add(tableName, params, false);
|
178 |
} catch (Exception ex) { |
179 |
LOGGER.warn("Can't create table '"+tableName+"'.",ex); |
180 |
} |
181 |
} |
182 |
|
183 |
private void createTableRepository() { |
184 |
String tableName = TABLE_REPOSITORY_NAME;
|
185 |
try {
|
186 |
DataManager dataManager = DALLocator.getDataManager(); |
187 |
DataServerExplorer server = dataManager.openServerExplorer( |
188 |
this.connection.getExplorerName(),
|
189 |
this.connection
|
190 |
); |
191 |
NewFeatureStoreParameters params = (NewFeatureStoreParameters) server.getAddParameters(tableName); |
192 |
EditableFeatureType ft = params.getDefaultFeatureType(); |
193 |
ft.add(FIELD_REPOSITORY_NAME, DataTypes.STRING, 150)
|
194 |
.setAllowNull(false)
|
195 |
.setIsPrimaryKey(true);
|
196 |
ft.add(FIELD_REPOSITORY_PARAMETERS, DataTypes.BYTEARRAY) |
197 |
.setAllowNull(true);
|
198 |
server.add(tableName, params, false);
|
199 |
} catch (Exception ex) { |
200 |
LOGGER.warn("Can't create table '"+tableName+"'.",ex); |
201 |
} |
202 |
} |
203 |
|
204 |
private void createTableConfiguration() { |
205 |
String tableName = TABLE_CONFIGURATION_NAME;
|
206 |
try {
|
207 |
DataManager dataManager = DALLocator.getDataManager(); |
208 |
DataServerExplorer server = dataManager.openServerExplorer( |
209 |
this.connection.getExplorerName(),
|
210 |
this.connection
|
211 |
); |
212 |
NewFeatureStoreParameters params = (NewFeatureStoreParameters) server.getAddParameters(tableName); |
213 |
EditableFeatureType ft = params.getDefaultFeatureType(); |
214 |
ft.add(FIELD_CONFIGURATION_NAME, DataTypes.STRING, 200)
|
215 |
.setAllowNull(false)
|
216 |
.setIsPrimaryKey(true);
|
217 |
ft.add(FIELD_CONFIGURATION_VALUE, DataTypes.STRING, 200)
|
218 |
.setAllowNull(true);
|
219 |
server.add(tableName, params, false);
|
220 |
} catch (Exception ex) { |
221 |
LOGGER.warn("Can't create table '"+tableName+"'.",ex); |
222 |
} |
223 |
} |
224 |
|
225 |
private boolean existsConfiguration() { |
226 |
if( this.existsConfiguration==null ) { |
227 |
this.existsConfiguration = this.existsTable(TABLE_CONFIGURATION_NAME); |
228 |
} |
229 |
return this.existsConfiguration; |
230 |
} |
231 |
|
232 |
@Override
|
233 |
public String get(String name) { |
234 |
try {
|
235 |
if( !this.existsConfiguration() ) { |
236 |
return null; |
237 |
} |
238 |
FeatureStore store = this.getTable(TABLE_CONFIGURATION);
|
239 |
if( store == null ) { |
240 |
return null; |
241 |
} |
242 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
243 |
String filter = builder.eq(
|
244 |
builder.column(FIELD_CONFIGURATION_NAME), |
245 |
builder.constant(name) |
246 |
).toString(); |
247 |
Feature feature = store.findFirst(filter); |
248 |
if( feature == null ) { |
249 |
return null; |
250 |
} |
251 |
String value = feature.getString(FIELD_CONFIGURATION_VALUE);
|
252 |
return value;
|
253 |
|
254 |
} catch (Exception ex) { |
255 |
LOGGER.warn("Can't read configuration value '"+name+"'", ex); |
256 |
return null; |
257 |
} |
258 |
} |
259 |
|
260 |
@Override
|
261 |
public void set(String name, String value) { |
262 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
263 |
} |
264 |
|
265 |
@Override
|
266 |
public StoresRepository getStoresRepository() {
|
267 |
if( this.storesRepository==null ) { |
268 |
String id = this.get(CONFIG_NAME_STORESREPOSITORYID); |
269 |
String label = this.get(CONFIG_NAME_STORESREPOSITORYLABEL); |
270 |
this.storesRepository = new DatabaseWorkspaceStoresRepository( |
271 |
id, |
272 |
label, |
273 |
this.connection
|
274 |
); |
275 |
} |
276 |
return this.storesRepository; |
277 |
} |
278 |
|
279 |
@Override
|
280 |
public boolean canAnonymousUserWriteInTheTables() { |
281 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
282 |
} |
283 |
|
284 |
@Override
|
285 |
public boolean storesRepositoryWriteEntry(String name, DataStoreParameters parameters) { |
286 |
try {
|
287 |
byte[] data = parameters.toByteArray(); |
288 |
if( data == null ) { |
289 |
throw new RuntimeException("Can't convert parameters to byte array."); |
290 |
} |
291 |
FeatureStore store = this.getTable(TABLE_REPOSITORY);
|
292 |
store.edit(); |
293 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
294 |
String filter = builder.eq(
|
295 |
builder.column(FIELD_REPOSITORY_NAME), |
296 |
builder.constant(name) |
297 |
).toString(); |
298 |
Feature feature = store.findFirst(filter); |
299 |
EditableFeature efeature; |
300 |
if (feature == null) { |
301 |
efeature = store.createNewFeature(); |
302 |
efeature.set(FIELD_REPOSITORY_NAME, name); |
303 |
efeature.set(FIELD_REPOSITORY_PARAMETERS, data); |
304 |
store.insert(efeature); |
305 |
} else {
|
306 |
efeature = feature.getEditable(); |
307 |
efeature.set(FIELD_REPOSITORY_PARAMETERS, data); |
308 |
store.update(efeature); |
309 |
} |
310 |
store.finishEditing(); |
311 |
return true; |
312 |
|
313 |
} catch (Exception ex) { |
314 |
LOGGER.warn("Can't save entry '"+name+"' in repository information", ex); |
315 |
return false; |
316 |
} |
317 |
} |
318 |
|
319 |
@Override
|
320 |
public boolean isValidStoresRepository() { |
321 |
String id = this.get(CONFIG_NAME_STORESREPOSITORYID); |
322 |
if( StringUtils.isBlank(id) ) {
|
323 |
return false; |
324 |
} |
325 |
if( !this.existsTable(TABLE_REPOSITORY_NAME) ) { |
326 |
return false; |
327 |
} |
328 |
return true; |
329 |
|
330 |
} |
331 |
} |