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 @ 3316
History | View | Annotate | Download (19.4 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 |
UsersTable.TABLE_NAME, |
95 |
TopologyplanTable.TABLE_NAME, |
96 |
TABLE_RESOURCES_NAME, |
97 |
TABLE_CONFIGURATION_NAME |
98 |
); |
99 |
|
100 |
public static final List<String> INTERNAL_REPOSITORY_TABLES = ListBuilder.create(DataRepoTable.TABLE_NAME, |
101 |
EntitiesRepoTable.TABLE_NAME, |
102 |
HooksRepoTable.TABLE_NAME, |
103 |
RevisionsRepoTable.TABLE_NAME, |
104 |
ConfigRepoTable.TABLE_NAME, |
105 |
TopologyplanRepoTable.TABLE_NAME, |
106 |
UsersRepoTable.TABLE_NAME, |
107 |
|
108 |
TABLE_RESOURCES_NAME, |
109 |
TABLE_CONFIGURATION_NAME |
110 |
); |
111 |
|
112 |
|
113 |
private Map<String,VCSGisWorkspaceDescriptor> workspaces; |
114 |
private VCSGisCodeGenerator codeGenerator;
|
115 |
private VCSGisUserIdentificationRequester userIdentificationRequester;
|
116 |
|
117 |
public VCSGisManagerImpl() {
|
118 |
this.workspaces = new HashMap<>(); |
119 |
this.codeGenerator = new RandomCodeGenerator(); |
120 |
this.userIdentificationRequester = null; |
121 |
} |
122 |
|
123 |
@Override
|
124 |
public Map<String, VCSGisWorkspaceDescriptor> getWorkspaces() { |
125 |
return Collections.unmodifiableMap(this.workspaces); |
126 |
} |
127 |
|
128 |
@Override
|
129 |
public void restoreWorkspaces(Map<String, VCSGisWorkspaceDescriptor> descriptors) { |
130 |
this.workspaces = new HashMap<>(); |
131 |
if( descriptors != null ) { |
132 |
this.workspaces.putAll(descriptors);
|
133 |
} |
134 |
} |
135 |
|
136 |
@Override
|
137 |
public int initWorkspace(File wsfile, VCSGisRepository repo, String label, SimpleTaskStatus status) { |
138 |
int err = ERR_NO_ERROR;
|
139 |
if (wsfile == null) { |
140 |
return ERR_DBFILE_IS_NULL;
|
141 |
} |
142 |
if (status == null) { |
143 |
status = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("vcsgis init_workspace");
|
144 |
status.add(); |
145 |
} |
146 |
status.push(); |
147 |
DataManager dataManager = DALLocator.getDataManager(); |
148 |
try {
|
149 |
wsfile = removeH2FileExtension(wsfile); |
150 |
if (StringUtils.isBlank(label)) {
|
151 |
label = wsfile.getName(); |
152 |
} |
153 |
err = ERR_CANT_OPEN_WORKSPACE_SERVEREXPLORER; |
154 |
JDBCServerExplorerParameters explorerParams = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(DataStore.H2SPATIAL_PROVIDER_NAME); |
155 |
((HasAFile) explorerParams).setFile(wsfile); |
156 |
JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer( |
157 |
explorerParams.getProviderName(), |
158 |
explorerParams |
159 |
); |
160 |
return this.initWorkspace(explorer, repo, label, status); |
161 |
} catch (Exception ex) { |
162 |
status.abort(); |
163 |
LOGGER.warn("Can't init workspace in '" + wsfile.getAbsolutePath() + "'.", ex); |
164 |
status.message(ex.getMessage()); |
165 |
return err;
|
166 |
} finally {
|
167 |
status.pop(); |
168 |
} |
169 |
} |
170 |
|
171 |
@Override
|
172 |
public int initWorkspace(JDBCServerExplorer wsexplorer, VCSGisRepository repository, String label, SimpleTaskStatus status) { |
173 |
int err = ERR_NO_ERROR;
|
174 |
if (wsexplorer == null) { |
175 |
return ERR_WSEXPLORER_IS_NULL;
|
176 |
} |
177 |
if (StringUtils.isBlank(label)) {
|
178 |
return ERR_LABEL_IS_NULL;
|
179 |
} |
180 |
if (status == null) { |
181 |
status = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("vcsgis init_workspace");
|
182 |
status.add(); |
183 |
} |
184 |
status.push(); |
185 |
DataManager dataManager = DALLocator.getDataManager(); |
186 |
try {
|
187 |
status.setTitle("vcsgis init_workspace");
|
188 |
FeatureType[] tables = new FeatureType[]{ |
189 |
VarsTable.featureType(), |
190 |
EntitiesTable.featureType(), |
191 |
WorkspaceChangesTable.featureType(), |
192 |
RemoteChangesTable.featureType(), |
193 |
LocalRevisionsTable.featureType(), |
194 |
UsersTable.featureType(), |
195 |
TopologyplanTable.featureType() |
196 |
}; |
197 |
status.setRangeOfValues(0, tables.length);
|
198 |
int step = 1; |
199 |
for (FeatureType table : tables) {
|
200 |
status.message(table.getLabel()); |
201 |
status.setCurValue(step++); |
202 |
err = ERR_CANT_CREATE_TABLE + step; |
203 |
JDBCNewStoreParameters table_params = wsexplorer.getAddParameters(); |
204 |
table_params.setDefaultFeatureType(table.getEditable()); |
205 |
table_params.setTable(table.getTags().getString("ID"));
|
206 |
wsexplorer.add(wsexplorer.getProviderName(), table_params, false);
|
207 |
} |
208 |
try {
|
209 |
wsexplorer.execute(VCSGisUtils.getSqlTemplate(wsexplorer.getProviderName(), "createWorkspaceIndex1"));
|
210 |
wsexplorer.execute(VCSGisUtils.getSqlTemplate(wsexplorer.getProviderName(), "createWorkspaceIndex2"));
|
211 |
wsexplorer.execute(VCSGisUtils.getSqlTemplate(wsexplorer.getProviderName(), "createWorkspaceIndex3"));
|
212 |
|
213 |
} catch (Exception ex) { |
214 |
LOGGER.warn("Can't create index in workspace", ex);
|
215 |
} |
216 |
|
217 |
status.setTitle("vcsgis retrieving entities");
|
218 |
VCSGisWorkspaceImpl workspace = new VCSGisWorkspaceImpl(wsexplorer, this.codeGenerator, repository, label); |
219 |
workspace.initialize(); |
220 |
|
221 |
this.registerWorkspace(workspace);
|
222 |
status.terminate(); |
223 |
return ERR_NO_ERROR;
|
224 |
} catch (Exception ex) { |
225 |
status.abort(); |
226 |
LOGGER.warn("Can't init workspace in '" + getMessageLabel(wsexplorer) + "'.", ex); |
227 |
status.message(ex.getMessage()); |
228 |
return err;
|
229 |
} finally {
|
230 |
status.pop(); |
231 |
} |
232 |
} |
233 |
|
234 |
private String getMessageLabel(JDBCServerExplorer explorer) { |
235 |
return explorer.getParameters().getUrl();
|
236 |
} |
237 |
|
238 |
@Override
|
239 |
public VCSGisWorkspace openWorkspace(File wsfile) { |
240 |
if (wsfile == null) { |
241 |
return null; |
242 |
} |
243 |
try {
|
244 |
DataManager dataManager = DALLocator.getDataManager(); |
245 |
JDBCServerExplorerParameters explorerParams = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(DataStore.H2SPATIAL_PROVIDER_NAME); |
246 |
((HasAFile) explorerParams).setFile(wsfile); |
247 |
JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer( |
248 |
explorerParams.getProviderName(), |
249 |
explorerParams |
250 |
); |
251 |
VCSGisWorkspace workspace = openWorkspace(explorer); |
252 |
return workspace;
|
253 |
} catch (Exception ex) { |
254 |
return null; |
255 |
} |
256 |
} |
257 |
|
258 |
@Override
|
259 |
public VCSGisWorkspace openWorkspace(JDBCServerExplorer wsexplorer) {
|
260 |
if (wsexplorer == null) { |
261 |
return null; |
262 |
} |
263 |
try {
|
264 |
VCSGisWorkspace workspace; |
265 |
|
266 |
JDBCServerExplorerParameters params = wsexplorer.getParameters(); |
267 |
String url = params.getUrl();
|
268 |
for (VCSGisWorkspaceDescriptor value : this.workspaces.values()) { |
269 |
if (value.isWorkspaceInitialized() && StringUtils.equals(url, value.getExplorerParameters().getUrl())) {
|
270 |
workspace = value.getWorkspace(); |
271 |
// LOGGER.debug("===: OPEN WORKSPACE "+ hexId(workspace)+ " from registry (code='"+workspace.getCode()+"', label='"+workspace.getLabel()+"')");
|
272 |
return workspace;
|
273 |
} |
274 |
} |
275 |
|
276 |
workspace = new VCSGisWorkspaceImpl(wsexplorer, this.codeGenerator); |
277 |
workspace.setUserIdentificationRequester(this.userIdentificationRequester);
|
278 |
this.registerWorkspace(workspace);
|
279 |
return workspace;
|
280 |
} catch (Exception ex) { |
281 |
return null; |
282 |
} |
283 |
} |
284 |
|
285 |
@Override
|
286 |
public void registerWorkspace(VCSGisWorkspace workspace) { |
287 |
VCSGisWorkspaceDescriptorImpl descriptor = new VCSGisWorkspaceDescriptorImpl(workspace);
|
288 |
this.workspaces.put(descriptor.getCode(), descriptor);
|
289 |
} |
290 |
|
291 |
@Override
|
292 |
public void deregisterWorkspace(VCSGisWorkspace workspace) { |
293 |
this.deregisterWorkspace(workspace.getCode());
|
294 |
} |
295 |
|
296 |
@Override
|
297 |
public void deregisterWorkspace(String workspaceCode) { |
298 |
this.workspaces.remove(workspaceCode);
|
299 |
} |
300 |
|
301 |
@Override
|
302 |
public VCSGisWorkspace getWorkspace(FeatureStore store) {
|
303 |
try {
|
304 |
VCSGisWorkspace workspace; |
305 |
switch(StoreProperties.getVCSMode(store)) {
|
306 |
case StoreProperties.MODE_NOT_VCS:
|
307 |
return null; |
308 |
|
309 |
case StoreProperties.MODE_VCS:
|
310 |
String workspace_code = StoreProperties.getWorkspaceCode(store);
|
311 |
VCSGisWorkspaceDescriptor workspaceDescriptor = this.workspaces.get(workspace_code);
|
312 |
if( workspaceDescriptor!=null ) { |
313 |
workspace = workspaceDescriptor.getWorkspace(); |
314 |
if (workspace != null) { |
315 |
String entity_name = StoreProperties.getEntityName(store);
|
316 |
EntityRow entity = (EntityRow) workspace.getWorkspaceEntityByName(entity_name); |
317 |
if (entity == null) { |
318 |
StoreProperties.setNotVCSMode(store); |
319 |
return null; |
320 |
} |
321 |
// LOGGER.debug("===: GET WORKSPACE "+ hexId(workspace)+ " from property (code='"+workspace.getCode()+"', label='"+workspace.getLabel()+"') for store '"+store.getName()+"'");
|
322 |
return workspace;
|
323 |
} |
324 |
} |
325 |
// break;
|
326 |
|
327 |
case StoreProperties.MODE_UNKNOWN:
|
328 |
default:
|
329 |
DataStoreParameters params = store.getParameters(); |
330 |
if(!(params instanceof JDBCStoreParameters)) { |
331 |
StoreProperties.setNotVCSMode(store); |
332 |
return null; |
333 |
} |
334 |
String entity_name = ((JDBCStoreParameters) params).getTable();
|
335 |
|
336 |
if (INTERNAL_WORKSPACE_TABLES.contains(entity_name)) {
|
337 |
StoreProperties.setNotVCSMode(store); |
338 |
return null; |
339 |
} |
340 |
|
341 |
String storeUrl = ((JDBCStoreParameters)params).getUrl();
|
342 |
for (VCSGisWorkspaceDescriptor value : this.workspaces.values()) { |
343 |
if (StringUtils.equals(storeUrl, value.getExplorerParameters().getUrl())) {
|
344 |
workspace = value.getWorkspace(); |
345 |
EntityRow entity = (EntityRow) workspace.getWorkspaceEntityByName(entity_name); |
346 |
if (entity == null) { |
347 |
StoreProperties.setNotVCSMode(store); |
348 |
return null; |
349 |
} |
350 |
StoreProperties.set(store, workspace, entity_name); |
351 |
// LOGGER.debug("===: GET WORKSPACE "+ hexId(workspace)+ " from registry (code='"+workspace.getCode()+"', label='"+workspace.getLabel()+"') for store '"+store.getName()+"'");
|
352 |
return workspace;
|
353 |
} |
354 |
|
355 |
} |
356 |
|
357 |
try {
|
358 |
workspace = openWorkspace((JDBCServerExplorer)store.getExplorer()); |
359 |
} catch (Exception e) { |
360 |
workspace = null;
|
361 |
} |
362 |
if (workspace == null) { |
363 |
StoreProperties.setNotVCSMode(store); |
364 |
return null; |
365 |
} |
366 |
this.registerWorkspace(workspace);
|
367 |
|
368 |
EntityRow entity = (EntityRow) workspace.getWorkspaceEntityByName(entity_name); |
369 |
if (entity == null) { |
370 |
StoreProperties.setNotVCSMode(store); |
371 |
return null; |
372 |
} |
373 |
StoreProperties.set(store, workspace, entity_name); |
374 |
return workspace;
|
375 |
} |
376 |
} catch (Exception ex) { |
377 |
LOGGER.warn("Can't get '" +store.getName()+ "'store's workspace ", ex); |
378 |
StoreProperties.setNotVCSMode(store); |
379 |
return null; |
380 |
} |
381 |
} |
382 |
|
383 |
@Override
|
384 |
public boolean isWorkspace(JDBCServerExplorerParameters params) { |
385 |
try {
|
386 |
DataManager dataManager = DALLocator.getDataManager(); |
387 |
DataServerExplorer explorer = dataManager.openServerExplorer(params.getProviderName(), params); |
388 |
DataStoreParameters x = explorer.get(VarsTable.TABLE_NAME); |
389 |
if(x==null) { |
390 |
return false; |
391 |
} |
392 |
return explorer.exists(x);
|
393 |
} catch (Exception ex) { |
394 |
return false; |
395 |
} |
396 |
} |
397 |
|
398 |
@Override
|
399 |
public boolean isWorkspace(File dbfile) { |
400 |
try {
|
401 |
DataManager dataManager = DALLocator.getDataManager(); |
402 |
JDBCServerExplorerParameters params = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(DataStore.H2SPATIAL_PROVIDER_NAME); |
403 |
((HasAFile)params).setFile(dbfile); |
404 |
return isWorkspace(params);
|
405 |
} catch (Exception ex) { |
406 |
return false; |
407 |
} |
408 |
} |
409 |
|
410 |
private File removeH2FileExtension(File f) { |
411 |
if (f == null) { |
412 |
return null; |
413 |
} |
414 |
String pathname = f.getAbsolutePath();
|
415 |
String lpathname = pathname.toLowerCase();
|
416 |
for (String ext : new String[]{".mv.db", ".mv", ".trace.db", ".trace"}) { |
417 |
if (lpathname.endsWith(ext)) {
|
418 |
pathname = pathname.substring(0, pathname.length() - ext.length());
|
419 |
return new File(pathname); |
420 |
} |
421 |
} |
422 |
return f;
|
423 |
} |
424 |
|
425 |
public static String hexId(Object obj) { |
426 |
if( obj==null ) { |
427 |
return "NULL"; |
428 |
} |
429 |
return Integer.toHexString(obj.hashCode()).toUpperCase(); |
430 |
} |
431 |
|
432 |
@Override
|
433 |
public VCSGisWorkspaceDescriptor getWorkspaceDescriptor(String code) { |
434 |
return this.workspaces.get(code); |
435 |
} |
436 |
|
437 |
@Override
|
438 |
public int initRepository(JDBCServerExplorerParameters repositoryParameters, SimpleTaskStatus status) { |
439 |
return VCSGisRepositoryLocaldb.create(repositoryParameters, this.codeGenerator, status); |
440 |
} |
441 |
|
442 |
@Override
|
443 |
public VCSGisRepository openRepository(JDBCServerExplorerParameters repositoryParameters) {
|
444 |
return new VCSGisRepositoryLocaldb(repositoryParameters, codeGenerator); |
445 |
} |
446 |
|
447 |
@Override
|
448 |
public VCSGisRepository openRepository(URL repository) { |
449 |
return new VCSGisRepositoryClient(repository); |
450 |
} |
451 |
|
452 |
@Override
|
453 |
public VCSGisServerController createServerController(VCSGisRepository repository) {
|
454 |
return new VCSGisServerControllerImpl(repository); |
455 |
} |
456 |
|
457 |
@Override
|
458 |
public void setCodeGenerator(VCSGisCodeGenerator generator) { |
459 |
this.codeGenerator = generator;
|
460 |
} |
461 |
|
462 |
@Override
|
463 |
public VCSGisCodeGenerator getCodeGenerator() {
|
464 |
return this.codeGenerator; |
465 |
} |
466 |
|
467 |
@Override
|
468 |
public String getErrorMessage(int errcode) { |
469 |
return VCSGisUtils.getErrorMessage(errcode);
|
470 |
} |
471 |
|
472 |
@Override
|
473 |
public void setUserIdentificationRequester(VCSGisUserIdentificationRequester userIdentificationRequester) { |
474 |
this.userIdentificationRequester = userIdentificationRequester;
|
475 |
} |
476 |
|
477 |
} |