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 @ 45338
History | View | Annotate | Download (6.67 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 java.util.logging.Level; |
8 |
import org.apache.commons.lang3.StringUtils; |
9 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
10 |
import org.gvsig.fmap.dal.AbstractStoresRepository; |
11 |
import org.gvsig.fmap.dal.DALLocator; |
12 |
import org.gvsig.fmap.dal.DataManager; |
13 |
import org.gvsig.fmap.dal.DataStoreParameters; |
14 |
import org.gvsig.fmap.dal.DataTypes; |
15 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_REPOSITORY_FLAGS; |
16 |
import org.gvsig.fmap.dal.StoresRepository; |
17 |
import org.gvsig.fmap.dal.feature.Feature; |
18 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
19 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_REPOSITORY_NAME; |
20 |
import org.gvsig.tools.exception.BaseException; |
21 |
import org.gvsig.tools.util.UnmodifiableBasicSet; |
22 |
import org.gvsig.tools.util.UnmodifiableBasicSetAdapter; |
23 |
import org.gvsig.tools.visitor.VisitCanceledException; |
24 |
import org.gvsig.tools.visitor.Visitor; |
25 |
import org.slf4j.Logger; |
26 |
import org.slf4j.LoggerFactory; |
27 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_REPOSITORY_PARAMETERS; |
28 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
29 |
import org.gvsig.tools.dispose.DisposeUtils; |
30 |
import org.gvsig.tools.dynobject.DynClass; |
31 |
import org.gvsig.tools.dynobject.DynField; |
32 |
import org.gvsig.tools.util.Bitmask; |
33 |
import org.gvsig.tools.util.HasAFile; |
34 |
|
35 |
/**
|
36 |
*
|
37 |
* @author jjdelcerro
|
38 |
*/
|
39 |
@SuppressWarnings("UseSpecificCatch") |
40 |
public class DatabaseWorkspaceStoresRepository |
41 |
extends AbstractStoresRepository
|
42 |
implements StoresRepository
|
43 |
{ |
44 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultDatabaseWorkspaceManager.class); |
45 |
|
46 |
private final DatabaseWorkspaceManager workspace; |
47 |
private Map<String,DataStoreParameters> repository; |
48 |
|
49 |
public static final int FLAG_CHANGE_PORT_HOST = 1; |
50 |
|
51 |
public DatabaseWorkspaceStoresRepository(String name, String label, DatabaseWorkspaceManager workspace) { |
52 |
super(name, label);
|
53 |
this.workspace = workspace;
|
54 |
this.repository = null; |
55 |
} |
56 |
|
57 |
private void evaluateExpressionsInFiles(DataStoreParameters parameters) { |
58 |
DynClass dynClass = parameters.getDynClass(); |
59 |
if( dynClass == null ) { |
60 |
return;
|
61 |
} |
62 |
File theBaseFolder = this.workspace.getBaseFolder(); |
63 |
if( theBaseFolder==null ) { |
64 |
return;
|
65 |
} |
66 |
for (DynField dynField : dynClass.getDynFields()) {
|
67 |
switch(dynField.getType()) {
|
68 |
case DataTypes.FILE:
|
69 |
case DataTypes.FOLDER:
|
70 |
File f = (File) parameters.getDynValue(dynField.getName()); |
71 |
if( ExpressionUtils.isDynamicFilename(f) ) {
|
72 |
parameters.setDynValue(dynField.getName(), ExpressionUtils.evaluateFilename(this.workspace, f));
|
73 |
} |
74 |
break;
|
75 |
} |
76 |
} |
77 |
try {
|
78 |
if (parameters instanceof HasAFile) { |
79 |
HasAFile params = (HasAFile) parameters; |
80 |
params.setFile(ExpressionUtils.evaluateFilename(params.getFile())); |
81 |
} |
82 |
parameters.validate(); |
83 |
} catch (ValidateDataParametersException ex) {
|
84 |
LOGGER.warn("Not been able to validate parameters", ex);
|
85 |
} |
86 |
} |
87 |
|
88 |
private void load() { |
89 |
FeatureStore store = null;
|
90 |
try {
|
91 |
this.repository = new HashMap<>(); |
92 |
final DataManager dataManager = DALLocator.getDataManager();
|
93 |
store = this.workspace.getTable(DatabaseWorkspaceManager.TABLE_REPOSITORY);
|
94 |
DataStoreParameters storeParameters = store.getParameters(); |
95 |
store.accept(new Visitor() {
|
96 |
@Override
|
97 |
public void visit(Object obj) throws VisitCanceledException, BaseException { |
98 |
Feature f = (Feature) obj; |
99 |
String name = f.getString(FIELD_REPOSITORY_NAME);
|
100 |
byte[] data = f.getByteArray(FIELD_REPOSITORY_PARAMETERS); |
101 |
// Compatibilidad con antiguos repositorios sin falgs
|
102 |
int flags = 0; |
103 |
if (f.getType().getAttributeDescriptor(FIELD_REPOSITORY_FLAGS)!=null) { |
104 |
flags = f.getInt(FIELD_REPOSITORY_FLAGS); |
105 |
} |
106 |
DataStoreParameters params = dataManager.createStoreParameters(data); |
107 |
if( params==null ) { |
108 |
LOGGER.warn("Can't restore parameters from repository entry '"+name+"'."); |
109 |
return;
|
110 |
} |
111 |
processParameters(params, storeParameters, Bitmask.createBitmask(flags)); |
112 |
evaluateExpressionsInFiles(params); |
113 |
repository.put(name, params); |
114 |
} |
115 |
}); |
116 |
} catch (Exception ex) { |
117 |
LOGGER.warn("Can't load repository information", ex);
|
118 |
} finally {
|
119 |
DisposeUtils.disposeQuietly(store); |
120 |
} |
121 |
} |
122 |
|
123 |
private void processParameters(DataStoreParameters params, DataStoreParameters storeParameters, Bitmask mask) { |
124 |
if (mask.isEmpty()) {
|
125 |
return;
|
126 |
} |
127 |
if (mask.isSetBit(FLAG_CHANGE_PORT_HOST)) {
|
128 |
try {
|
129 |
Object host = storeParameters.getDynValue("host"); |
130 |
Object port = storeParameters.getDynValue("port"); |
131 |
params.setDynValue("host", host);
|
132 |
params.setDynValue("port", port);
|
133 |
} catch (Exception ex) { |
134 |
throw new RuntimeException("Unable to set host and port from actual connection", ex); |
135 |
} |
136 |
} |
137 |
} |
138 |
|
139 |
@Override
|
140 |
protected DataStoreParameters getMyParameters(String name) { |
141 |
if( this.repository==null ) { |
142 |
this.load();
|
143 |
} |
144 |
DataStoreParameters params = this.repository.get(name);
|
145 |
if (params!=null) { |
146 |
return params;
|
147 |
} |
148 |
for (String key : this.repository.keySet()) { |
149 |
if (StringUtils.equalsIgnoreCase(key, name)) {
|
150 |
return this.repository.get(key); |
151 |
} |
152 |
} |
153 |
return null; |
154 |
} |
155 |
|
156 |
@Override
|
157 |
protected boolean isEmptyMyRepository() { |
158 |
if( this.repository==null ) { |
159 |
this.load();
|
160 |
} |
161 |
return this.repository.isEmpty(); |
162 |
} |
163 |
|
164 |
@Override
|
165 |
protected int getMySize() { |
166 |
if( this.repository==null ) { |
167 |
this.load();
|
168 |
} |
169 |
return this.repository.size(); |
170 |
} |
171 |
|
172 |
@Override
|
173 |
protected UnmodifiableBasicSet<String> getMyKeySet() { |
174 |
if( this.repository==null ) { |
175 |
this.load();
|
176 |
} |
177 |
return new UnmodifiableBasicSetAdapter<>(this.repository.keySet()); |
178 |
} |
179 |
|
180 |
|
181 |
} |