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 / DatabaseWorkspaceStoresRepository.java @ 44351
History | View | Annotate | Download (4.23 KB)
1 | 44304 | jjdelcerro | package org.gvsig.fmap.dal.impl; |
---|---|---|---|
2 | |||
3 | import org.gvsig.fmap.dal.DatabaseWorkspaceManager; |
||
4 | import java.util.HashMap; |
||
5 | import java.util.Map; |
||
6 | import org.gvsig.fmap.dal.AbstractStoresRepository; |
||
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.StoresRepository; |
||
13 | import org.gvsig.fmap.dal.feature.Feature; |
||
14 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
15 | import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_REPOSITORY_NAME; |
||
16 | import org.gvsig.tools.exception.BaseException; |
||
17 | import org.gvsig.tools.util.UnmodifiableBasicSet; |
||
18 | import org.gvsig.tools.util.UnmodifiableBasicSetAdapter; |
||
19 | import org.gvsig.tools.visitor.VisitCanceledException; |
||
20 | import org.gvsig.tools.visitor.Visitor; |
||
21 | import org.slf4j.Logger; |
||
22 | import org.slf4j.LoggerFactory; |
||
23 | import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_REPOSITORY_PARAMETERS; |
||
24 | |||
25 | /**
|
||
26 | *
|
||
27 | * @author jjdelcerro
|
||
28 | */
|
||
29 | @SuppressWarnings("UseSpecificCatch") |
||
30 | public class DatabaseWorkspaceStoresRepository |
||
31 | extends AbstractStoresRepository
|
||
32 | implements StoresRepository
|
||
33 | { |
||
34 | private static final Logger LOGGER = LoggerFactory.getLogger(DefaultDatabaseWorkspaceManager.class); |
||
35 | |||
36 | private final DataServerExplorerParameters connection; |
||
37 | private Map<String,DataStoreParameters> repository; |
||
38 | |||
39 | public DatabaseWorkspaceStoresRepository(String name, String label, DataServerExplorerParameters connection) { |
||
40 | super(name, label);
|
||
41 | this.connection = connection;
|
||
42 | this.repository = null; |
||
43 | } |
||
44 | |||
45 | private FeatureStore getTable() {
|
||
46 | String tableName = DatabaseWorkspaceManager.TABLE_REPOSITORY_NAME;
|
||
47 | try {
|
||
48 | DataManager dataManager = DALLocator.getDataManager(); |
||
49 | DataServerExplorer server = dataManager.openServerExplorer( |
||
50 | this.connection.getExplorerName(),
|
||
51 | this.connection
|
||
52 | ); |
||
53 | DataStoreParameters params = server.get(tableName); |
||
54 | if( params!=null ) { |
||
55 | FeatureStore store = (FeatureStore) dataManager.openStore( |
||
56 | params.getDataStoreName(), |
||
57 | params |
||
58 | ); |
||
59 | return store;
|
||
60 | } |
||
61 | } catch (Exception ex) { |
||
62 | LOGGER.warn("Can't open table '"+tableName+"'.",ex); |
||
63 | } |
||
64 | return null; |
||
65 | } |
||
66 | |||
67 | private void load() { |
||
68 | try {
|
||
69 | this.repository = new HashMap<>(); |
||
70 | final DataManager dataManager = DALLocator.getDataManager();
|
||
71 | FeatureStore store = this.getTable();
|
||
72 | store.accept(new Visitor() {
|
||
73 | @Override
|
||
74 | public void visit(Object obj) throws VisitCanceledException, BaseException { |
||
75 | Feature f = (Feature) obj; |
||
76 | String name = f.getString(FIELD_REPOSITORY_NAME);
|
||
77 | byte[] data = f.getByteArray(FIELD_REPOSITORY_PARAMETERS); |
||
78 | DataStoreParameters params = dataManager.createStoreParameters(data); |
||
79 | if( params==null ) { |
||
80 | LOGGER.warn("Can't restore parameters from repository entry '"+name+"'."); |
||
81 | return;
|
||
82 | } |
||
83 | repository.put(name, params); |
||
84 | } |
||
85 | }); |
||
86 | } catch (Exception ex) { |
||
87 | LOGGER.warn("Can't load repository information", ex);
|
||
88 | } |
||
89 | } |
||
90 | |||
91 | @Override
|
||
92 | protected DataStoreParameters getMyParameters(String name) { |
||
93 | if( this.repository==null ) { |
||
94 | this.load();
|
||
95 | } |
||
96 | return this.repository.get(name); |
||
97 | } |
||
98 | |||
99 | @Override
|
||
100 | protected boolean isEmptyMyRepository() { |
||
101 | if( this.repository==null ) { |
||
102 | this.load();
|
||
103 | } |
||
104 | return this.repository.isEmpty(); |
||
105 | } |
||
106 | |||
107 | @Override
|
||
108 | protected int getMySize() { |
||
109 | if( this.repository==null ) { |
||
110 | this.load();
|
||
111 | } |
||
112 | return this.repository.size(); |
||
113 | } |
||
114 | |||
115 | @Override
|
||
116 | protected UnmodifiableBasicSet<String> getMyKeySet() { |
||
117 | if( this.repository==null ) { |
||
118 | this.load();
|
||
119 | } |
||
120 | return new UnmodifiableBasicSetAdapter<>(this.repository.keySet()); |
||
121 | } |
||
122 | |||
123 | |||
124 | } |