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 @ 47673
History | View | Annotate | Download (9.2 KB)
1 |
package org.gvsig.fmap.dal.impl; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import org.gvsig.fmap.dal.DatabaseWorkspaceManager; |
5 |
import java.util.HashMap; |
6 |
import java.util.Map; |
7 |
import org.apache.commons.lang3.StringUtils; |
8 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
9 |
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 |
import org.gvsig.fmap.dal.DataTypes; |
14 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_REPOSITORY_FLAGS; |
15 |
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 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
28 |
import org.gvsig.tools.dispose.DisposeUtils; |
29 |
import org.gvsig.tools.dynobject.DynClass; |
30 |
import org.gvsig.tools.dynobject.DynField; |
31 |
import org.gvsig.tools.util.Bitmask; |
32 |
import org.gvsig.tools.util.HasAFile; |
33 |
|
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 |
private final DefaultDatabaseWorkspaceManager workspace; |
46 |
private Map<String,DataStoreParameters> repository; |
47 |
|
48 |
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 |
|
52 |
/* 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 |
private static final String URL_PARAMTER_NAME = "url"; |
59 |
|
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 |
public DatabaseWorkspaceStoresRepository(String name, String label, DatabaseWorkspaceManager workspace) { |
67 |
super(name, label);
|
68 |
this.workspace = (DefaultDatabaseWorkspaceManager) workspace;
|
69 |
this.repository = null; |
70 |
this.setServerParameters(this.workspace.getServerExplorerParameters()); |
71 |
} |
72 |
|
73 |
private void evaluateExpressionsInFiles(DataStoreParameters parameters) { |
74 |
DynClass dynClass = parameters.getDynClass(); |
75 |
if( dynClass == null ) { |
76 |
return;
|
77 |
} |
78 |
File theBaseFolder = this.workspace.getBaseFolder(); |
79 |
File theWorkspaceFile = this.workspace.getWorkspaceFile(); |
80 |
if( theBaseFolder==null && theWorkspaceFile==null ) { |
81 |
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 |
} |
93 |
} |
94 |
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 |
} |
104 |
|
105 |
private void load() { |
106 |
FeatureStore store = null;
|
107 |
try {
|
108 |
this.repository = new HashMap<>(); |
109 |
final DataManager dataManager = DALLocator.getDataManager();
|
110 |
store = this.workspace.getTable(DatabaseWorkspaceManager.TABLE_REPOSITORY);
|
111 |
if(store == null) { |
112 |
return;
|
113 |
} |
114 |
DataStoreParameters storeParameters = store.getParameters(); |
115 |
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 |
// 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 |
DataStoreParameters params = dataManager.createStoreParameters(data); |
127 |
if( params==null ) { |
128 |
LOGGER.warn("Can't restore parameters from repository entry '"+name+"'."); |
129 |
return;
|
130 |
} |
131 |
processParameters(params, storeParameters, Bitmask.createBitmask(flags)); |
132 |
evaluateExpressionsInFiles(params); |
133 |
repository.put(name, params); |
134 |
} |
135 |
}); |
136 |
} catch (Exception ex) { |
137 |
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 |
} finally {
|
144 |
DisposeUtils.disposeQuietly(store); |
145 |
} |
146 |
} |
147 |
|
148 |
private void processParameters(DataStoreParameters params, DataStoreParameters storeParameters, Bitmask mask) { |
149 |
if (mask.isEmpty()) {
|
150 |
return;
|
151 |
} |
152 |
|
153 |
if (mask.isSetBit(BIT_CHANGE_PORT_HOST)) {
|
154 |
try {
|
155 |
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 |
params.setDynValue(URL_PARAMTER_NAME, null);
|
160 |
} catch (Exception ex) { |
161 |
throw new RuntimeException("Unable to set host and port from actual connection", ex); |
162 |
} |
163 |
} |
164 |
if (mask.isSetBit(BIT_CHANGE_USER)) {
|
165 |
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 |
if (mask.isSetBit(BIT_CHANGE_DBNAME_CATALOG_SCHEMA)) {
|
175 |
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 |
params.setDynValue(URL_PARAMTER_NAME, null);
|
183 |
} catch (Exception ex) { |
184 |
throw new RuntimeException("Unable to set dbname and catalog, schema from actual connection", ex); |
185 |
} |
186 |
} |
187 |
} |
188 |
|
189 |
@Override
|
190 |
protected DataStoreParameters getMyParameters(String name) { |
191 |
if( this.repository==null ) { |
192 |
this.load();
|
193 |
} |
194 |
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 |
} |
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 |
} |