gvsig-projects-pool / org.gvsig.vcsgis / trunk / org.gvsig.vcsgis / org.gvsig.vcsgis.lib / org.gvsig.vcsgis.lib.impl / src / main / java / org / gvsig / vcsgis / lib / VCSGisManagerImpl.java @ 3308
History | View | Annotate | Download (19.3 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright (c) 2007-2015 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.vcsgis.lib; |
24 |
|
25 |
import java.io.File; |
26 |
import java.net.URL; |
27 |
import java.util.Collections; |
28 |
import java.util.HashMap; |
29 |
import java.util.List; |
30 |
import java.util.Map; |
31 |
import org.apache.commons.lang3.StringUtils; |
32 |
import org.gvsig.fmap.dal.DALLocator; |
33 |
import org.gvsig.fmap.dal.DataManager; |
34 |
import org.gvsig.fmap.dal.DataServerExplorer; |
35 |
import org.gvsig.fmap.dal.DataStore; |
36 |
import org.gvsig.fmap.dal.DataStoreParameters; |
37 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_CONFIGURATION_NAME; |
38 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_RESOURCES_NAME; |
39 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
40 |
import org.gvsig.fmap.dal.feature.FeatureType; |
41 |
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
42 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
43 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
44 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
45 |
import org.gvsig.tools.ToolsLocator; |
46 |
import org.gvsig.tools.task.SimpleTaskStatus; |
47 |
import org.gvsig.tools.util.HasAFile; |
48 |
import org.gvsig.tools.util.ListBuilder; |
49 |
import org.gvsig.vcsgis.lib.repository.VCSGisRepository; |
50 |
import org.gvsig.vcsgis.lib.repository.localdb.VCSGisRepositoryLocaldb; |
51 |
import org.gvsig.vcsgis.lib.repository.localdb.tables.DataRepoTable; |
52 |
import org.gvsig.vcsgis.lib.repository.localdb.tables.EntitiesRepoTable; |
53 |
import org.gvsig.vcsgis.lib.repository.localdb.tables.HooksRepoTable; |
54 |
import org.gvsig.vcsgis.lib.repository.localdb.tables.RevisionsRepoTable; |
55 |
import org.gvsig.vcsgis.lib.repository.localdb.tables.ConfigRepoTable; |
56 |
import org.gvsig.vcsgis.lib.repository.localdb.tables.TopologyplanRepoTable; |
57 |
import org.gvsig.vcsgis.lib.repository.localdb.tables.UsersRepoTable; |
58 |
import org.gvsig.vcsgis.lib.repository.remoteclient.VCSGisRepositoryClient; |
59 |
import org.gvsig.vcsgis.lib.server.VCSGisServerController; |
60 |
import org.gvsig.vcsgis.lib.server.VCSGisServerControllerImpl; |
61 |
import org.gvsig.vcsgis.lib.workspace.StoreProperties; |
62 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace; |
63 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceDescriptor; |
64 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceDescriptorImpl; |
65 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceImpl; |
66 |
import org.gvsig.vcsgis.lib.workspace.tables.EntitiesTable; |
67 |
import org.gvsig.vcsgis.lib.workspace.tables.EntitiesTable.EntityRow; |
68 |
import org.gvsig.vcsgis.lib.workspace.tables.LocalRevisionsTable; |
69 |
import org.gvsig.vcsgis.lib.workspace.tables.RemoteChangesTable; |
70 |
import org.gvsig.vcsgis.lib.workspace.tables.TopologyplanTable; |
71 |
import org.gvsig.vcsgis.lib.workspace.tables.UsersTable; |
72 |
import org.gvsig.vcsgis.lib.workspace.tables.VarsTable; |
73 |
import org.gvsig.vcsgis.lib.workspace.tables.WorkspaceChangesTable; |
74 |
import org.slf4j.Logger; |
75 |
import org.slf4j.LoggerFactory; |
76 |
|
77 |
|
78 |
/**
|
79 |
* @author gvSIG Team
|
80 |
*
|
81 |
*/
|
82 |
@SuppressWarnings("UseSpecificCatch") |
83 |
public class VCSGisManagerImpl implements VCSGisManager { |
84 |
private static final Logger LOGGER = LoggerFactory.getLogger(VCSGisManagerImpl.class); |
85 |
|
86 |
|
87 |
|
88 |
public static final List<String> INTERNAL_WORKSPACE_TABLES = ListBuilder.create( |
89 |
EntitiesTable.TABLE_NAME, |
90 |
LocalRevisionsTable.TABLE_NAME, |
91 |
RemoteChangesTable.TABLE_NAME, |
92 |
VarsTable.TABLE_NAME, |
93 |
WorkspaceChangesTable.TABLE_NAME, |
94 |
TABLE_RESOURCES_NAME, |
95 |
TABLE_CONFIGURATION_NAME |
96 |
); |
97 |
|
98 |
public static final List<String> INTERNAL_REPOSITORY_TABLES = ListBuilder.create(DataRepoTable.TABLE_NAME, |
99 |
EntitiesRepoTable.TABLE_NAME, |
100 |
HooksRepoTable.TABLE_NAME, |
101 |
RevisionsRepoTable.TABLE_NAME, |
102 |
ConfigRepoTable.TABLE_NAME, |
103 |
TopologyplanRepoTable.TABLE_NAME, |
104 |
UsersRepoTable.TABLE_NAME, |
105 |
|
106 |
TABLE_RESOURCES_NAME, |
107 |
TABLE_CONFIGURATION_NAME |
108 |
); |
109 |
|
110 |
|
111 |
private Map<String,VCSGisWorkspaceDescriptor> workspaces; |
112 |
private VCSGisCodeGenerator codeGenerator;
|
113 |
private VCSGisUserIdentificationRequester userIdentificationRequester;
|
114 |
|
115 |
public VCSGisManagerImpl() {
|
116 |
this.workspaces = new HashMap<>(); |
117 |
this.codeGenerator = new RandomCodeGenerator(); |
118 |
this.userIdentificationRequester = null; |
119 |
} |
120 |
|
121 |
@Override
|
122 |
public Map<String, VCSGisWorkspaceDescriptor> getWorkspaces() { |
123 |
return Collections.unmodifiableMap(this.workspaces); |
124 |
} |
125 |
|
126 |
@Override
|
127 |
public void restoreWorkspaces(Map<String, VCSGisWorkspaceDescriptor> descriptors) { |
128 |
this.workspaces = new HashMap<>(); |
129 |
if( descriptors != null ) { |
130 |
this.workspaces.putAll(descriptors);
|
131 |
} |
132 |
} |
133 |
|
134 |
@Override
|
135 |
public int initWorkspace(File wsfile, VCSGisRepository repo, String label, SimpleTaskStatus status) { |
136 |
int err = ERR_NO_ERROR;
|
137 |
if (wsfile == null) { |
138 |
return ERR_DBFILE_IS_NULL;
|
139 |
} |
140 |
if (status == null) { |
141 |
status = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("vcsgis init_workspace");
|
142 |
status.add(); |
143 |
} |
144 |
status.push(); |
145 |
DataManager dataManager = DALLocator.getDataManager(); |
146 |
try {
|
147 |
wsfile = removeH2FileExtension(wsfile); |
148 |
if (StringUtils.isBlank(label)) {
|
149 |
label = wsfile.getName(); |
150 |
} |
151 |
err = ERR_CANT_OPEN_WORKSPACE_SERVEREXPLORER; |
152 |
JDBCServerExplorerParameters explorerParams = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(DataStore.H2SPATIAL_PROVIDER_NAME); |
153 |
((HasAFile) explorerParams).setFile(wsfile); |
154 |
JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer( |
155 |
explorerParams.getProviderName(), |
156 |
explorerParams |
157 |
); |
158 |
return this.initWorkspace(explorer, repo, label, status); |
159 |
} catch (Exception ex) { |
160 |
status.abort(); |
161 |
LOGGER.warn("Can't init workspace in '" + wsfile.getAbsolutePath() + "'.", ex); |
162 |
status.message(ex.getMessage()); |
163 |
return err;
|
164 |
} finally {
|
165 |
status.pop(); |
166 |
} |
167 |
} |
168 |
|
169 |
@Override
|
170 |
public int initWorkspace(JDBCServerExplorer wsexplorer, VCSGisRepository repository, String label, SimpleTaskStatus status) { |
171 |
int err = ERR_NO_ERROR;
|
172 |
if (wsexplorer == null) { |
173 |
return ERR_WSEXPLORER_IS_NULL;
|
174 |
} |
175 |
if (StringUtils.isBlank(label)) {
|
176 |
return ERR_LABEL_IS_NULL;
|
177 |
} |
178 |
if (status == null) { |
179 |
status = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("vcsgis init_workspace");
|
180 |
status.add(); |
181 |
} |
182 |
status.push(); |
183 |
DataManager dataManager = DALLocator.getDataManager(); |
184 |
try {
|
185 |
status.setTitle("vcsgis init_workspace");
|
186 |
FeatureType[] tables = new FeatureType[]{ |
187 |
VarsTable.featureType(), |
188 |
EntitiesTable.featureType(), |
189 |
WorkspaceChangesTable.featureType(), |
190 |
RemoteChangesTable.featureType(), |
191 |
LocalRevisionsTable.featureType(), |
192 |
UsersTable.featureType(), |
193 |
TopologyplanTable.featureType() |
194 |
}; |
195 |
status.setRangeOfValues(0, tables.length);
|
196 |
int step = 1; |
197 |
for (FeatureType table : tables) {
|
198 |
status.message(table.getLabel()); |
199 |
status.setCurValue(step++); |
200 |
err = ERR_CANT_CREATE_TABLE + step; |
201 |
JDBCNewStoreParameters table_params = wsexplorer.getAddParameters(); |
202 |
table_params.setDefaultFeatureType(table.getEditable()); |
203 |
table_params.setTable(table.getTags().getString("ID"));
|
204 |
wsexplorer.add(wsexplorer.getProviderName(), table_params, false);
|
205 |
} |
206 |
try {
|
207 |
wsexplorer.execute(VCSGisUtils.getSqlTemplate(wsexplorer.getProviderName(), "createWorkspaceIndex1"));
|
208 |
wsexplorer.execute(VCSGisUtils.getSqlTemplate(wsexplorer.getProviderName(), "createWorkspaceIndex2"));
|
209 |
wsexplorer.execute(VCSGisUtils.getSqlTemplate(wsexplorer.getProviderName(), "createWorkspaceIndex3"));
|
210 |
|
211 |
} catch (Exception ex) { |
212 |
LOGGER.warn("Can't create index in workspace", ex);
|
213 |
} |
214 |
|
215 |
status.setTitle("vcsgis retrieving entities");
|
216 |
VCSGisWorkspaceImpl workspace = new VCSGisWorkspaceImpl(wsexplorer, this.codeGenerator, repository, label); |
217 |
workspace.initialize(); |
218 |
|
219 |
this.registerWorkspace(workspace);
|
220 |
status.terminate(); |
221 |
return ERR_NO_ERROR;
|
222 |
} catch (Exception ex) { |
223 |
status.abort(); |
224 |
LOGGER.warn("Can't init workspace in '" + getMessageLabel(wsexplorer) + "'.", ex); |
225 |
status.message(ex.getMessage()); |
226 |
return err;
|
227 |
} finally {
|
228 |
status.pop(); |
229 |
} |
230 |
} |
231 |
|
232 |
private String getMessageLabel(JDBCServerExplorer explorer) { |
233 |
return explorer.getParameters().getUrl();
|
234 |
} |
235 |
|
236 |
@Override
|
237 |
public VCSGisWorkspace openWorkspace(File wsfile) { |
238 |
if (wsfile == null) { |
239 |
return null; |
240 |
} |
241 |
try {
|
242 |
DataManager dataManager = DALLocator.getDataManager(); |
243 |
JDBCServerExplorerParameters explorerParams = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(DataStore.H2SPATIAL_PROVIDER_NAME); |
244 |
((HasAFile) explorerParams).setFile(wsfile); |
245 |
JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer( |
246 |
explorerParams.getProviderName(), |
247 |
explorerParams |
248 |
); |
249 |
VCSGisWorkspace workspace = openWorkspace(explorer); |
250 |
return workspace;
|
251 |
} catch (Exception ex) { |
252 |
return null; |
253 |
} |
254 |
} |
255 |
|
256 |
@Override
|
257 |
public VCSGisWorkspace openWorkspace(JDBCServerExplorer wsexplorer) {
|
258 |
if (wsexplorer == null) { |
259 |
return null; |
260 |
} |
261 |
try {
|
262 |
VCSGisWorkspace workspace; |
263 |
|
264 |
JDBCServerExplorerParameters params = wsexplorer.getParameters(); |
265 |
String url = params.getUrl();
|
266 |
for (VCSGisWorkspaceDescriptor value : this.workspaces.values()) { |
267 |
if (value.isWorkspaceInitialized() && StringUtils.equals(url, value.getExplorerParameters().getUrl())) {
|
268 |
workspace = value.getWorkspace(); |
269 |
// LOGGER.debug("===: OPEN WORKSPACE "+ hexId(workspace)+ " from registry (code='"+workspace.getCode()+"', label='"+workspace.getLabel()+"')");
|
270 |
return workspace;
|
271 |
} |
272 |
} |
273 |
|
274 |
workspace = new VCSGisWorkspaceImpl(wsexplorer, this.codeGenerator); |
275 |
workspace.setUserIdentificationRequester(this.userIdentificationRequester);
|
276 |
this.registerWorkspace(workspace);
|
277 |
return workspace;
|
278 |
} catch (Exception ex) { |
279 |
return null; |
280 |
} |
281 |
} |
282 |
|
283 |
@Override
|
284 |
public void registerWorkspace(VCSGisWorkspace workspace) { |
285 |
VCSGisWorkspaceDescriptorImpl descriptor = new VCSGisWorkspaceDescriptorImpl(workspace);
|
286 |
this.workspaces.put(descriptor.getCode(), descriptor);
|
287 |
} |
288 |
|
289 |
@Override
|
290 |
public void deregisterWorkspace(VCSGisWorkspace workspace) { |
291 |
this.deregisterWorkspace(workspace.getCode());
|
292 |
} |
293 |
|
294 |
@Override
|
295 |
public void deregisterWorkspace(String workspaceCode) { |
296 |
this.workspaces.remove(workspaceCode);
|
297 |
} |
298 |
|
299 |
@Override
|
300 |
public VCSGisWorkspace getWorkspace(FeatureStore store) {
|
301 |
try {
|
302 |
VCSGisWorkspace workspace; |
303 |
switch(StoreProperties.getVCSMode(store)) {
|
304 |
case StoreProperties.MODE_NOT_VCS:
|
305 |
return null; |
306 |
|
307 |
case StoreProperties.MODE_VCS:
|
308 |
String workspace_code = StoreProperties.getWorkspaceCode(store);
|
309 |
VCSGisWorkspaceDescriptor workspaceDescriptor = this.workspaces.get(workspace_code);
|
310 |
if( workspaceDescriptor!=null ) { |
311 |
workspace = workspaceDescriptor.getWorkspace(); |
312 |
if (workspace != null) { |
313 |
String entity_name = StoreProperties.getEntityName(store);
|
314 |
EntityRow entity = (EntityRow) workspace.getWorkspaceEntityByName(entity_name); |
315 |
if (entity == null) { |
316 |
StoreProperties.setNotVCSMode(store); |
317 |
return null; |
318 |
} |
319 |
// LOGGER.debug("===: GET WORKSPACE "+ hexId(workspace)+ " from property (code='"+workspace.getCode()+"', label='"+workspace.getLabel()+"') for store '"+store.getName()+"'");
|
320 |
return workspace;
|
321 |
} |
322 |
} |
323 |
// break;
|
324 |
|
325 |
case StoreProperties.MODE_UNKNOWN:
|
326 |
default:
|
327 |
DataStoreParameters params = store.getParameters(); |
328 |
if(!(params instanceof JDBCStoreParameters)) { |
329 |
StoreProperties.setNotVCSMode(store); |
330 |
return null; |
331 |
} |
332 |
String entity_name = ((JDBCStoreParameters) params).getTable();
|
333 |
|
334 |
if (INTERNAL_WORKSPACE_TABLES.contains(entity_name)) {
|
335 |
StoreProperties.setNotVCSMode(store); |
336 |
return null; |
337 |
} |
338 |
|
339 |
String storeUrl = ((JDBCStoreParameters)params).getUrl();
|
340 |
for (VCSGisWorkspaceDescriptor value : this.workspaces.values()) { |
341 |
if (StringUtils.equals(storeUrl, value.getExplorerParameters().getUrl())) {
|
342 |
workspace = value.getWorkspace(); |
343 |
EntityRow entity = (EntityRow) workspace.getWorkspaceEntityByName(entity_name); |
344 |
if (entity == null) { |
345 |
StoreProperties.setNotVCSMode(store); |
346 |
return null; |
347 |
} |
348 |
StoreProperties.set(store, workspace, entity_name); |
349 |
// LOGGER.debug("===: GET WORKSPACE "+ hexId(workspace)+ " from registry (code='"+workspace.getCode()+"', label='"+workspace.getLabel()+"') for store '"+store.getName()+"'");
|
350 |
return workspace;
|
351 |
} |
352 |
|
353 |
} |
354 |
|
355 |
try {
|
356 |
workspace = openWorkspace((JDBCServerExplorer)store.getExplorer()); |
357 |
} catch (Exception e) { |
358 |
workspace = null;
|
359 |
} |
360 |
if (workspace == null) { |
361 |
StoreProperties.setNotVCSMode(store); |
362 |
return null; |
363 |
} |
364 |
this.registerWorkspace(workspace);
|
365 |
|
366 |
EntityRow entity = (EntityRow) workspace.getWorkspaceEntityByName(entity_name); |
367 |
if (entity == null) { |
368 |
StoreProperties.setNotVCSMode(store); |
369 |
return null; |
370 |
} |
371 |
StoreProperties.set(store, workspace, entity_name); |
372 |
return workspace;
|
373 |
} |
374 |
} catch (Exception ex) { |
375 |
LOGGER.warn("Can't get '" +store.getName()+ "'store's workspace ", ex); |
376 |
StoreProperties.setNotVCSMode(store); |
377 |
return null; |
378 |
} |
379 |
} |
380 |
|
381 |
@Override
|
382 |
public boolean isWorkspace(JDBCServerExplorerParameters params) { |
383 |
try {
|
384 |
DataManager dataManager = DALLocator.getDataManager(); |
385 |
DataServerExplorer explorer = dataManager.openServerExplorer(params.getProviderName(), params); |
386 |
DataStoreParameters x = explorer.get(VarsTable.TABLE_NAME); |
387 |
if(x==null) { |
388 |
return false; |
389 |
} |
390 |
return explorer.exists(x);
|
391 |
} catch (Exception ex) { |
392 |
return false; |
393 |
} |
394 |
} |
395 |
|
396 |
@Override
|
397 |
public boolean isWorkspace(File dbfile) { |
398 |
try {
|
399 |
DataManager dataManager = DALLocator.getDataManager(); |
400 |
JDBCServerExplorerParameters params = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(DataStore.H2SPATIAL_PROVIDER_NAME); |
401 |
((HasAFile)params).setFile(dbfile); |
402 |
return isWorkspace(params);
|
403 |
} catch (Exception ex) { |
404 |
return false; |
405 |
} |
406 |
} |
407 |
|
408 |
private File removeH2FileExtension(File f) { |
409 |
if (f == null) { |
410 |
return null; |
411 |
} |
412 |
String pathname = f.getAbsolutePath();
|
413 |
String lpathname = pathname.toLowerCase();
|
414 |
for (String ext : new String[]{".mv.db", ".mv", ".trace.db", ".trace"}) { |
415 |
if (lpathname.endsWith(ext)) {
|
416 |
pathname = pathname.substring(0, pathname.length() - ext.length());
|
417 |
return new File(pathname); |
418 |
} |
419 |
} |
420 |
return f;
|
421 |
} |
422 |
|
423 |
public static String hexId(Object obj) { |
424 |
if( obj==null ) { |
425 |
return "NULL"; |
426 |
} |
427 |
return Integer.toHexString(obj.hashCode()).toUpperCase(); |
428 |
} |
429 |
|
430 |
@Override
|
431 |
public VCSGisWorkspaceDescriptor getWorkspaceDescriptor(String code) { |
432 |
return this.workspaces.get(code); |
433 |
} |
434 |
|
435 |
@Override
|
436 |
public int initRepository(JDBCServerExplorerParameters repositoryParameters, SimpleTaskStatus status) { |
437 |
return VCSGisRepositoryLocaldb.create(repositoryParameters, this.codeGenerator, status); |
438 |
} |
439 |
|
440 |
@Override
|
441 |
public VCSGisRepository openRepository(JDBCServerExplorerParameters repositoryParameters) {
|
442 |
return new VCSGisRepositoryLocaldb(repositoryParameters, codeGenerator); |
443 |
} |
444 |
|
445 |
@Override
|
446 |
public VCSGisRepository openRepository(URL repository) { |
447 |
return new VCSGisRepositoryClient(repository); |
448 |
} |
449 |
|
450 |
@Override
|
451 |
public VCSGisServerController createServerController(VCSGisRepository repository) {
|
452 |
return new VCSGisServerControllerImpl(repository); |
453 |
} |
454 |
|
455 |
@Override
|
456 |
public void setCodeGenerator(VCSGisCodeGenerator generator) { |
457 |
this.codeGenerator = generator;
|
458 |
} |
459 |
|
460 |
@Override
|
461 |
public VCSGisCodeGenerator getCodeGenerator() {
|
462 |
return this.codeGenerator; |
463 |
} |
464 |
|
465 |
@Override
|
466 |
public String getErrorMessage(int errcode) { |
467 |
return VCSGisUtils.getErrorMessage(errcode);
|
468 |
} |
469 |
|
470 |
@Override
|
471 |
public void setUserIdentificationRequester(VCSGisUserIdentificationRequester userIdentificationRequester) { |
472 |
this.userIdentificationRequester = userIdentificationRequester;
|
473 |
} |
474 |
|
475 |
} |