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 / complements / relatedfeatures / RelatedFeaturesImpl.java @ 44457
History | View | Annotate | Download (10.7 KB)
1 |
package org.gvsig.fmap.dal.complements.relatedfeatures; |
---|---|
2 |
|
3 |
import java.util.Arrays; |
4 |
import java.util.List; |
5 |
import org.apache.commons.lang3.StringUtils; |
6 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
7 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
8 |
import org.gvsig.fmap.dal.DALLocator; |
9 |
import org.gvsig.fmap.dal.StoresRepository; |
10 |
import org.gvsig.fmap.dal.complements.RelatedFeatures; |
11 |
import org.gvsig.fmap.dal.complements.Search; |
12 |
import org.gvsig.fmap.dal.feature.Feature; |
13 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
14 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
15 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
16 |
import org.gvsig.fmap.dal.feature.FeatureType; |
17 |
import org.gvsig.fmap.dal.feature.ForeingKey; |
18 |
import org.gvsig.tools.ToolsLocator; |
19 |
import org.gvsig.tools.complement.AbstractComplement; |
20 |
import org.gvsig.tools.complement.ComplementFactory; |
21 |
import org.gvsig.tools.dispose.DisposeUtils; |
22 |
import org.gvsig.tools.dynobject.Tagged; |
23 |
import org.slf4j.Logger; |
24 |
import org.slf4j.LoggerFactory; |
25 |
|
26 |
/**
|
27 |
*
|
28 |
* @author jjdelcerro
|
29 |
*/
|
30 |
@SuppressWarnings("UseSpecificCatch") |
31 |
public class RelatedFeaturesImpl |
32 |
extends AbstractComplement<Tagged>
|
33 |
implements RelatedFeatures {
|
34 |
|
35 |
protected static final Logger LOGGER = LoggerFactory.getLogger(RelatedFeaturesImpl.class); |
36 |
|
37 |
private class DefaultContextRelatedFeatures implements ContextRelatedFeatures { |
38 |
|
39 |
private FeatureStore featureStore = null; |
40 |
private StoresRepository storesRepository = null; |
41 |
private int refs; |
42 |
|
43 |
public DefaultContextRelatedFeatures() {
|
44 |
this.refs = 1; |
45 |
} |
46 |
|
47 |
public void addRef() { |
48 |
this.refs++;
|
49 |
} |
50 |
|
51 |
public void release() { |
52 |
this.refs--;
|
53 |
} |
54 |
|
55 |
@Override
|
56 |
public void dispose() { |
57 |
DisposeUtils.disposeQuietly(featureStore); |
58 |
this.featureStore = null; |
59 |
this.storesRepository = null; |
60 |
} |
61 |
|
62 |
@Override
|
63 |
public ContextRelatedFeatures setStoresRepository(StoresRepository repository) {
|
64 |
this.storesRepository = repository;
|
65 |
return this; |
66 |
} |
67 |
|
68 |
@Override
|
69 |
public StoresRepository getStoresRepository() {
|
70 |
if (this.storesRepository == null) { |
71 |
FeatureStore store = null;
|
72 |
if (getObject() instanceof FeatureAttributeDescriptor) { |
73 |
store = ((FeatureAttributeDescriptor) getObject()).getStore(); |
74 |
} else if (getObject() instanceof FeatureType) { |
75 |
store = ((FeatureType) getObject()).getStore(); |
76 |
} |
77 |
if (store == null) { |
78 |
this.storesRepository = DALLocator.getDataManager().getStoresRepository();
|
79 |
} else {
|
80 |
this.storesRepository = store.getStoresRepository();
|
81 |
} |
82 |
} |
83 |
return this.storesRepository; |
84 |
} |
85 |
|
86 |
@Override
|
87 |
public FeatureStore getFeatureStore() {
|
88 |
if (this.featureStore == null) { |
89 |
if ( getObject() instanceof FeatureAttributeDescriptor) { |
90 |
FeatureStore store = ((FeatureAttributeDescriptor) getObject()).getStore(); |
91 |
if( store != null ) { |
92 |
StoresRepository repository = store.getStoresRepository(); |
93 |
if( repository.containsKey(getTableName()) ) {
|
94 |
this.featureStore = (FeatureStore) repository.getStore(getTableName());
|
95 |
if( this.featureStore!=null) { |
96 |
return this.featureStore; |
97 |
} |
98 |
} |
99 |
} |
100 |
} |
101 |
StoresRepository repository = this.getStoresRepository();
|
102 |
this.featureStore = (FeatureStore) repository.getStore(getTableName());
|
103 |
if (this.featureStore == null) { |
104 |
LOGGER.warn("Can't locate store '" + getTableName() + "'."); |
105 |
return null; |
106 |
} |
107 |
} |
108 |
return this.featureStore; |
109 |
} |
110 |
|
111 |
@Override
|
112 |
public FeatureType getFeatureType() {
|
113 |
try {
|
114 |
FeatureStore store = this.getFeatureStore();
|
115 |
return store.getDefaultFeatureType();
|
116 |
} catch (Exception ex) { |
117 |
return null; |
118 |
} |
119 |
} |
120 |
|
121 |
} |
122 |
|
123 |
public RelatedFeaturesImpl(ComplementFactory<Tagged> factory, Tagged object) {
|
124 |
super(factory, object);
|
125 |
} |
126 |
|
127 |
@Override
|
128 |
public boolean isRelatedFeatures() { |
129 |
return isRelatedFeatures(null); |
130 |
} |
131 |
|
132 |
@Override
|
133 |
public boolean isRelatedFeatures(ContextRelatedFeatures context) { |
134 |
context = createLocalContextIfNull(context); |
135 |
try {
|
136 |
String tableName = this.getTableName(); |
137 |
if (StringUtils.isBlank(tableName)
|
138 |
|| StringUtils.isBlank(this.getUniqueKeyName())) {
|
139 |
return false; |
140 |
} |
141 |
|
142 |
Tagged attribute = this.getObject();
|
143 |
if (!(attribute instanceof FeatureAttributeDescriptor)) { |
144 |
return true; |
145 |
} |
146 |
FeatureStore store = ((FeatureAttributeDescriptor) attribute).getStore(); |
147 |
if (store == null) { |
148 |
return true; |
149 |
} |
150 |
StoresRepository repository = store.getStoresRepository(); |
151 |
return repository.containsKey(tableName);
|
152 |
} finally {
|
153 |
disposeIfLocalContext(context); |
154 |
} |
155 |
} |
156 |
|
157 |
@Override
|
158 |
public String getUniqueKeyName() { |
159 |
Tagged attribute = this.getObject();
|
160 |
String codeName = attribute.getTags().getString(DAL_RELATED_UNIQUE_FIELD_NAME, null); |
161 |
return codeName;
|
162 |
} |
163 |
|
164 |
// @Override
|
165 |
// public String getForeingKeyName() {
|
166 |
// Tagged attribute = this.getObject();
|
167 |
// String codeName = attribute.getTags().getString(DAL_RELATED_FOREING_KEY_NAME, null);
|
168 |
// return codeName;
|
169 |
// }
|
170 |
|
171 |
@Override
|
172 |
public String getTableName() { |
173 |
Tagged attribute = this.getObject();
|
174 |
String tableName = attribute.getTags().getString(DAL_RELATED_TABLE, null); |
175 |
return tableName;
|
176 |
} |
177 |
|
178 |
@Override
|
179 |
public ContextRelatedFeatures createContext() {
|
180 |
return new DefaultContextRelatedFeatures(); |
181 |
} |
182 |
|
183 |
private void disposeIfLocalContext(ContextRelatedFeatures context) { |
184 |
DefaultContextRelatedFeatures c = (DefaultContextRelatedFeatures) context; |
185 |
c.release(); |
186 |
if (c.refs == 0) { |
187 |
context.dispose(); |
188 |
} |
189 |
} |
190 |
|
191 |
private ContextRelatedFeatures createLocalContextIfNull(ContextRelatedFeatures context) {
|
192 |
if (context == null) { |
193 |
return new DefaultContextRelatedFeatures(); |
194 |
} |
195 |
DefaultContextRelatedFeatures c = (DefaultContextRelatedFeatures) context; |
196 |
c.addRef(); |
197 |
return c;
|
198 |
} |
199 |
|
200 |
@Override
|
201 |
public FeatureStore getFeatureStore(ContextRelatedFeatures context) {
|
202 |
context = createLocalContextIfNull(context); |
203 |
try {
|
204 |
return context.getFeatureStore();
|
205 |
} finally {
|
206 |
disposeIfLocalContext(context); |
207 |
} |
208 |
} |
209 |
|
210 |
// @Override
|
211 |
// public Object getForeingKey(ContextRelatedFeatures context, Feature feature) {
|
212 |
//// context = createLocalContextIfNull(context);
|
213 |
// try {
|
214 |
// return feature.get(getForeingKeyName());
|
215 |
// } finally {
|
216 |
//// disposeIfLocalContext(context);
|
217 |
// }
|
218 |
// }
|
219 |
|
220 |
@Override
|
221 |
public Object getUniqueKey(ContextRelatedFeatures context, Feature feature) { |
222 |
// context = createLocalContextIfNull(context);
|
223 |
try {
|
224 |
return feature.get(getUniqueKeyName());
|
225 |
} finally {
|
226 |
// disposeIfLocalContext(context);
|
227 |
} |
228 |
} |
229 |
|
230 |
@Override
|
231 |
public FeatureQuery getForeingKeyQuery(ContextRelatedFeatures context, Object codeValue) { |
232 |
if( !(this.getObject() instanceof FeatureAttributeDescriptor) ) { |
233 |
return null; |
234 |
} |
235 |
String me = ((FeatureAttributeDescriptor)this.getObject()).getStore().getName(); |
236 |
FeatureType ftype = context.getFeatureType(); |
237 |
for (FeatureAttributeDescriptor attr : ftype) {
|
238 |
if( attr.isForeingKey() ) {
|
239 |
ForeingKey fkey = attr.getForeingKey(); |
240 |
if( StringUtils.equals(fkey.getTableName(), me) ) {
|
241 |
return this.getQuery(context, fkey.getCodeName(), codeValue); |
242 |
} |
243 |
} |
244 |
} |
245 |
return null; |
246 |
} |
247 |
|
248 |
@Override
|
249 |
public FeatureQuery getUniqueKeyQuery(ContextRelatedFeatures context, Object codeValue) { |
250 |
return this.getQuery(context, this.getUniqueKeyName(), codeValue); |
251 |
} |
252 |
|
253 |
public FeatureQuery getQuery(ContextRelatedFeatures context, String fieldName, Object codeValue) { |
254 |
context = createLocalContextIfNull(context); |
255 |
try {
|
256 |
FeatureStore store = context.getFeatureStore(); |
257 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
258 |
FeatureQuery query = store.createFeatureQuery(); |
259 |
query.setFilter(builder.eq( |
260 |
builder.variable(fieldName), |
261 |
builder.constant(codeValue) |
262 |
).toString() |
263 |
); |
264 |
query.retrievesAllAttributes(); |
265 |
return query;
|
266 |
} finally {
|
267 |
disposeIfLocalContext(context); |
268 |
} |
269 |
} |
270 |
|
271 |
@Override
|
272 |
public List<String> getColumns(ContextRelatedFeatures context) { |
273 |
context = createLocalContextIfNull(context); |
274 |
try {
|
275 |
Tagged attribute = this.getObject();
|
276 |
List<String> columnNames; |
277 |
String columns = attribute.getTags().getString(DAL_RELATED_COLUMNS, null); |
278 |
if (StringUtils.isBlank(columns)) {
|
279 |
FeatureType featureType = context.getFeatureType(); |
280 |
if (featureType == null) { |
281 |
return null; |
282 |
} |
283 |
Search search = (Search) ToolsLocator.getComplementsManager().get( |
284 |
Search.COMPLEMENT_MANE, featureType |
285 |
); |
286 |
columnNames = search.getResultColumnNames(); |
287 |
} else {
|
288 |
columnNames = Arrays.asList(split(columns, ":/;,|-")); |
289 |
} |
290 |
return columnNames;
|
291 |
} finally {
|
292 |
disposeIfLocalContext(context); |
293 |
} |
294 |
} |
295 |
|
296 |
private String[] split(String value, String separators) { |
297 |
int firstSeparatorPosition = 1000000; |
298 |
Character sep = null; |
299 |
for (char ch : separators.toCharArray()) { |
300 |
int pos = value.indexOf(ch);
|
301 |
if (pos > 0 && pos < firstSeparatorPosition) { |
302 |
sep = ch; |
303 |
firstSeparatorPosition = pos; |
304 |
} |
305 |
} |
306 |
if (sep == null) { |
307 |
return new String[]{value}; |
308 |
} |
309 |
return value.split("[" + sep + "]"); |
310 |
} |
311 |
|
312 |
} |