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 @ 47611
History | View | Annotate | Download (9.2 KB)
1 | 44304 | jjdelcerro | package org.gvsig.fmap.dal.impl; |
---|---|---|---|
2 | |||
3 | 44397 | jjdelcerro | import java.io.File; |
4 | 44304 | jjdelcerro | import org.gvsig.fmap.dal.DatabaseWorkspaceManager; |
5 | import java.util.HashMap; |
||
6 | import java.util.Map; |
||
7 | 45338 | omartinez | import org.apache.commons.lang3.StringUtils; |
8 | 44397 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionUtils; |
9 | 44304 | jjdelcerro | import org.gvsig.fmap.dal.AbstractStoresRepository; |
10 | import org.gvsig.fmap.dal.DALLocator; |
||
11 | import org.gvsig.fmap.dal.DataManager; |
||
12 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
13 | 44397 | jjdelcerro | import org.gvsig.fmap.dal.DataTypes; |
14 | 45208 | omartinez | import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_REPOSITORY_FLAGS; |
15 | 44304 | jjdelcerro | import org.gvsig.fmap.dal.StoresRepository; |
16 | import org.gvsig.fmap.dal.feature.Feature; |
||
17 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
18 | import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_REPOSITORY_NAME; |
||
19 | import org.gvsig.tools.exception.BaseException; |
||
20 | import org.gvsig.tools.util.UnmodifiableBasicSet; |
||
21 | import org.gvsig.tools.util.UnmodifiableBasicSetAdapter; |
||
22 | import org.gvsig.tools.visitor.VisitCanceledException; |
||
23 | import org.gvsig.tools.visitor.Visitor; |
||
24 | import org.slf4j.Logger; |
||
25 | import org.slf4j.LoggerFactory; |
||
26 | import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_REPOSITORY_PARAMETERS; |
||
27 | 44399 | omartinez | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
28 | 45100 | jjdelcerro | import org.gvsig.tools.dispose.DisposeUtils; |
29 | 44397 | jjdelcerro | import org.gvsig.tools.dynobject.DynClass; |
30 | import org.gvsig.tools.dynobject.DynField; |
||
31 | 45208 | omartinez | import org.gvsig.tools.util.Bitmask; |
32 | 44399 | omartinez | import org.gvsig.tools.util.HasAFile; |
33 | 44304 | jjdelcerro | |
34 | /**
|
||
35 | *
|
||
36 | * @author jjdelcerro
|
||
37 | */
|
||
38 | @SuppressWarnings("UseSpecificCatch") |
||
39 | public class DatabaseWorkspaceStoresRepository |
||
40 | extends AbstractStoresRepository
|
||
41 | implements StoresRepository
|
||
42 | { |
||
43 | private static final Logger LOGGER = LoggerFactory.getLogger(DefaultDatabaseWorkspaceManager.class); |
||
44 | |||
45 | 46352 | jjdelcerro | private final DefaultDatabaseWorkspaceManager workspace; |
46 | 44304 | jjdelcerro | private Map<String,DataStoreParameters> repository; |
47 | 45208 | omartinez | |
48 | 46381 | fdiaz | public static final int BIT_CHANGE_PORT_HOST = 0; |
49 | public static final int BIT_CHANGE_USER = 1; |
||
50 | public static final int BIT_CHANGE_DBNAME_CATALOG_SCHEMA = 2; |
||
51 | 44304 | jjdelcerro | |
52 | 46357 | fdiaz | /* Copied from DBParameters */
|
53 | private static final String HOST_PARAMTER_NAME = "host"; |
||
54 | private static final String PORT_PARAMTER_NAME = "port"; |
||
55 | private static final String DBNAME_PARAMTER_NAME = "dbname"; |
||
56 | private static final String USER_PARAMTER_NAME = "dbuser"; |
||
57 | private static final String PASSWORD_PARAMTER_NAME = "password"; |
||
58 | 46700 | fdiaz | private static final String URL_PARAMTER_NAME = "url"; |
59 | 46357 | fdiaz | |
60 | /* Copied from JDBCConnectionParameters */
|
||
61 | private static final String CATALOG_PARAMTER_NAME = "catalog"; |
||
62 | private static final String SCHEMA_PARAMTER_NAME = "schema"; |
||
63 | |||
64 | |||
65 | |||
66 | 44397 | jjdelcerro | public DatabaseWorkspaceStoresRepository(String name, String label, DatabaseWorkspaceManager workspace) { |
67 | 44304 | jjdelcerro | super(name, label);
|
68 | 46352 | jjdelcerro | this.workspace = (DefaultDatabaseWorkspaceManager) workspace;
|
69 | 44304 | jjdelcerro | this.repository = null; |
70 | 46915 | jjdelcerro | this.setServerParameters(this.workspace.getServerExplorerParameters()); |
71 | 44304 | jjdelcerro | } |
72 | |||
73 | 44397 | jjdelcerro | private void evaluateExpressionsInFiles(DataStoreParameters parameters) { |
74 | DynClass dynClass = parameters.getDynClass(); |
||
75 | if( dynClass == null ) { |
||
76 | return;
|
||
77 | } |
||
78 | File theBaseFolder = this.workspace.getBaseFolder(); |
||
79 | 46352 | jjdelcerro | File theWorkspaceFile = this.workspace.getWorkspaceFile(); |
80 | if( theBaseFolder==null && theWorkspaceFile==null ) { |
||
81 | 44397 | jjdelcerro | return;
|
82 | } |
||
83 | for (DynField dynField : dynClass.getDynFields()) {
|
||
84 | switch(dynField.getType()) {
|
||
85 | case DataTypes.FILE:
|
||
86 | case DataTypes.FOLDER:
|
||
87 | File f = (File) parameters.getDynValue(dynField.getName()); |
||
88 | if( ExpressionUtils.isDynamicFilename(f) ) {
|
||
89 | parameters.setDynValue(dynField.getName(), ExpressionUtils.evaluateFilename(this.workspace, f));
|
||
90 | } |
||
91 | break;
|
||
92 | 44304 | jjdelcerro | } |
93 | } |
||
94 | 44399 | omartinez | try {
|
95 | if (parameters instanceof HasAFile) { |
||
96 | HasAFile params = (HasAFile) parameters; |
||
97 | params.setFile(ExpressionUtils.evaluateFilename(params.getFile())); |
||
98 | } |
||
99 | parameters.validate(); |
||
100 | } catch (ValidateDataParametersException ex) {
|
||
101 | LOGGER.warn("Not been able to validate parameters", ex);
|
||
102 | } |
||
103 | 44304 | jjdelcerro | } |
104 | |||
105 | private void load() { |
||
106 | 45100 | jjdelcerro | FeatureStore store = null;
|
107 | 44304 | jjdelcerro | try {
|
108 | this.repository = new HashMap<>(); |
||
109 | final DataManager dataManager = DALLocator.getDataManager();
|
||
110 | 45100 | jjdelcerro | store = this.workspace.getTable(DatabaseWorkspaceManager.TABLE_REPOSITORY);
|
111 | 47611 | fdiaz | if(store == null) { |
112 | return;
|
||
113 | } |
||
114 | 45208 | omartinez | DataStoreParameters storeParameters = store.getParameters(); |
115 | 44304 | jjdelcerro | store.accept(new Visitor() {
|
116 | @Override
|
||
117 | public void visit(Object obj) throws VisitCanceledException, BaseException { |
||
118 | Feature f = (Feature) obj; |
||
119 | String name = f.getString(FIELD_REPOSITORY_NAME);
|
||
120 | byte[] data = f.getByteArray(FIELD_REPOSITORY_PARAMETERS); |
||
121 | 45208 | omartinez | // Compatibilidad con antiguos repositorios sin falgs
|
122 | int flags = 0; |
||
123 | if (f.getType().getAttributeDescriptor(FIELD_REPOSITORY_FLAGS)!=null) { |
||
124 | flags = f.getInt(FIELD_REPOSITORY_FLAGS); |
||
125 | } |
||
126 | 44304 | jjdelcerro | DataStoreParameters params = dataManager.createStoreParameters(data); |
127 | if( params==null ) { |
||
128 | LOGGER.warn("Can't restore parameters from repository entry '"+name+"'."); |
||
129 | return;
|
||
130 | } |
||
131 | 45208 | omartinez | processParameters(params, storeParameters, Bitmask.createBitmask(flags)); |
132 | 44397 | jjdelcerro | evaluateExpressionsInFiles(params); |
133 | 44304 | jjdelcerro | repository.put(name, params); |
134 | } |
||
135 | }); |
||
136 | } catch (Exception ex) { |
||
137 | 47611 | fdiaz | if(store == null) { |
138 | LOGGER.warn("Can't open repository information");
|
||
139 | LOGGER.debug("Can't open repository information", ex);
|
||
140 | } else {
|
||
141 | LOGGER.warn("Can't load repository information", ex);
|
||
142 | } |
||
143 | 45100 | jjdelcerro | } finally {
|
144 | DisposeUtils.disposeQuietly(store); |
||
145 | 44304 | jjdelcerro | } |
146 | } |
||
147 | 45208 | omartinez | |
148 | private void processParameters(DataStoreParameters params, DataStoreParameters storeParameters, Bitmask mask) { |
||
149 | if (mask.isEmpty()) {
|
||
150 | return;
|
||
151 | } |
||
152 | 46357 | fdiaz | |
153 | 46381 | fdiaz | if (mask.isSetBit(BIT_CHANGE_PORT_HOST)) {
|
154 | 45208 | omartinez | try {
|
155 | 46357 | fdiaz | Object host = storeParameters.getDynValue(HOST_PARAMTER_NAME);
|
156 | Object port = storeParameters.getDynValue(PORT_PARAMTER_NAME);
|
||
157 | params.setDynValue(HOST_PARAMTER_NAME, host); |
||
158 | params.setDynValue(PORT_PARAMTER_NAME, port); |
||
159 | 46700 | fdiaz | params.setDynValue(URL_PARAMTER_NAME, null);
|
160 | 45208 | omartinez | } catch (Exception ex) { |
161 | throw new RuntimeException("Unable to set host and port from actual connection", ex); |
||
162 | } |
||
163 | } |
||
164 | 46381 | fdiaz | if (mask.isSetBit(BIT_CHANGE_USER)) {
|
165 | 46357 | fdiaz | try {
|
166 | Object dbuser = storeParameters.getDynValue(USER_PARAMTER_NAME);
|
||
167 | Object password = storeParameters.getDynValue(PASSWORD_PARAMTER_NAME);
|
||
168 | params.setDynValue(USER_PARAMTER_NAME, dbuser); |
||
169 | params.setDynValue(PASSWORD_PARAMTER_NAME, password); |
||
170 | } catch (Exception ex) { |
||
171 | throw new RuntimeException("Unable to set user and passwrod from actual connection", ex); |
||
172 | } |
||
173 | } |
||
174 | 46381 | fdiaz | if (mask.isSetBit(BIT_CHANGE_DBNAME_CATALOG_SCHEMA)) {
|
175 | 46357 | fdiaz | try {
|
176 | Object dbname = storeParameters.getDynValue(DBNAME_PARAMTER_NAME);
|
||
177 | Object catalog = storeParameters.getDynValue(CATALOG_PARAMTER_NAME);
|
||
178 | Object schema = storeParameters.getDynValue(SCHEMA_PARAMTER_NAME);
|
||
179 | params.setDynValue(DBNAME_PARAMTER_NAME, dbname); |
||
180 | params.setDynValue(CATALOG_PARAMTER_NAME, catalog); |
||
181 | params.setDynValue(SCHEMA_PARAMTER_NAME, schema); |
||
182 | 46700 | fdiaz | params.setDynValue(URL_PARAMTER_NAME, null);
|
183 | 46357 | fdiaz | } catch (Exception ex) { |
184 | throw new RuntimeException("Unable to set dbname and catalog, schema from actual connection", ex); |
||
185 | } |
||
186 | } |
||
187 | 45208 | omartinez | } |
188 | 44304 | jjdelcerro | |
189 | @Override
|
||
190 | protected DataStoreParameters getMyParameters(String name) { |
||
191 | if( this.repository==null ) { |
||
192 | this.load();
|
||
193 | } |
||
194 | 45338 | omartinez | DataStoreParameters params = this.repository.get(name);
|
195 | if (params!=null) { |
||
196 | return params;
|
||
197 | } |
||
198 | for (String key : this.repository.keySet()) { |
||
199 | if (StringUtils.equalsIgnoreCase(key, name)) {
|
||
200 | return this.repository.get(key); |
||
201 | } |
||
202 | } |
||
203 | return null; |
||
204 | 44304 | jjdelcerro | } |
205 | |||
206 | @Override
|
||
207 | protected boolean isEmptyMyRepository() { |
||
208 | if( this.repository==null ) { |
||
209 | this.load();
|
||
210 | } |
||
211 | return this.repository.isEmpty(); |
||
212 | } |
||
213 | |||
214 | @Override
|
||
215 | protected int getMySize() { |
||
216 | if( this.repository==null ) { |
||
217 | this.load();
|
||
218 | } |
||
219 | return this.repository.size(); |
||
220 | } |
||
221 | |||
222 | @Override
|
||
223 | protected UnmodifiableBasicSet<String> getMyKeySet() { |
||
224 | if( this.repository==null ) { |
||
225 | this.load();
|
||
226 | } |
||
227 | return new UnmodifiableBasicSetAdapter<>(this.repository.keySet()); |
||
228 | } |
||
229 | |||
230 | |||
231 | } |