Statistics
| Revision:

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 @ 44262

History | View | Annotate | Download (10.1 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.tools.ToolsLocator;
18
import org.gvsig.tools.complement.AbstractComplement;
19
import org.gvsig.tools.complement.ComplementFactory;
20
import org.gvsig.tools.dispose.DisposeUtils;
21
import org.gvsig.tools.dynobject.Tagged;
22
import org.slf4j.Logger;
23
import org.slf4j.LoggerFactory;
24

    
25
/**
26
 *
27
 * @author jjdelcerro
28
 */
29
@SuppressWarnings("UseSpecificCatch")
30
public class RelatedFeaturesImpl
31
        extends AbstractComplement<Tagged>
32
        implements RelatedFeatures {
33

    
34
    protected static final Logger LOGGER = LoggerFactory.getLogger(RelatedFeaturesImpl.class);
35

    
36
    private class DefaultContextRelatedFeatures implements ContextRelatedFeatures {
37

    
38
        private FeatureStore featureStore = null;
39
        private StoresRepository storesRepository = null;
40
        private int refs;
41

    
42
        public DefaultContextRelatedFeatures() {
43
            this.refs = 1;
44
        }
45

    
46
        public void addRef() {
47
            this.refs++;
48
        }
49

    
50
        public void release() {
51
            this.refs--;
52
        }
53

    
54
        @Override
55
        public void dispose() {
56
            DisposeUtils.disposeQuietly(featureStore);
57
            this.featureStore = null;
58
            this.storesRepository = null;
59
        }
60

    
61
        @Override
62
        public ContextRelatedFeatures setStoresRepository(StoresRepository repository) {
63
            this.storesRepository = repository;
64
            return this;
65
        }
66

    
67
        @Override
68
        public StoresRepository getStoresRepository() {
69
            if (this.storesRepository == null) {
70
                FeatureStore store = null;
71
                if (getObject() instanceof FeatureAttributeDescriptor) {
72
                    store = ((FeatureAttributeDescriptor) getObject()).getStore();
73
                } else if (getObject() instanceof FeatureType) {
74
                    store = ((FeatureType) getObject()).getStore();
75
                }
76
                if (store == null) {
77
                    this.storesRepository = DALLocator.getDataManager().getStoresRepository();
78
                } else {
79
                    this.storesRepository = store.getStoresRepository();
80
                }
81
            }
82
            return this.storesRepository;
83
        }
84

    
85
        @Override
86
        public FeatureStore getFeatureStore() {
87
            if (this.featureStore == null) {
88
                if ( getObject() instanceof FeatureAttributeDescriptor) {
89
                    FeatureStore store = ((FeatureAttributeDescriptor) getObject()).getStore();
90
                    if( store != null ) {
91
                        StoresRepository repository = store.getStoresRepository();
92
                        if( repository.containsKey(getTableName()) ) {
93
                            this.featureStore = (FeatureStore) repository.get(getTableName());
94
                            if( this.featureStore!=null) {
95
                                return this.featureStore;
96
                            }
97
                        }
98
                    }
99
                } 
100
                StoresRepository repository = this.getStoresRepository();
101
                this.featureStore = (FeatureStore) repository.get(getTableName());
102
                if (this.featureStore == null) {
103
                    LOGGER.warn("Can't locate store '" + getTableName() + "'.");
104
                    return null;
105
                }
106
            }
107
            return this.featureStore;
108
        }
109

    
110
        @Override
111
        public FeatureType getFeatureType() {
112
            try {
113
                FeatureStore store = this.getFeatureStore();
114
                return store.getDefaultFeatureType();
115
            } catch (Exception ex) {
116
                return null;
117
            }
118
        }
119

    
120
    }
121

    
122
    public RelatedFeaturesImpl(ComplementFactory<Tagged> factory, Tagged object) {
123
        super(factory, object);
124
    }
125

    
126
    @Override
127
    public boolean isRelatedFeatures() {
128
        return isRelatedFeatures(null);
129
    }
130

    
131
    @Override
132
    public boolean isRelatedFeatures(ContextRelatedFeatures context) {
133
        context = createLocalContextIfNull(context);
134
        try {
135
            String tableName = this.getTableName();
136
            if (StringUtils.isBlank(tableName)
137
                    || StringUtils.isBlank(this.getUniqueKeyName())) {
138
                return false;
139
            }
140
            
141
            Tagged attribute = this.getObject();
142
            if (!(attribute instanceof FeatureAttributeDescriptor)) {
143
                return true;
144
            }
145
            FeatureStore store = ((FeatureAttributeDescriptor) attribute).getStore();
146
            if (store == null) {
147
                return true;
148
            }
149
            StoresRepository repository = store.getStoresRepository();
150
            return repository.containsKey(tableName);
151
        } finally {
152
            disposeIfLocalContext(context);
153
        }
154
    }
155

    
156
    @Override
157
    public String getUniqueKeyName() {
158
        Tagged attribute = this.getObject();
159
        String codeName = attribute.getTags().getString(DAL_RELATED_UNIQUE_FIELD_NAME, null);
160
        return codeName;
161
    }
162

    
163
//    @Override
164
//    public String getForeingKeyName() {
165
//        Tagged attribute = this.getObject();
166
//        String codeName = attribute.getTags().getString(DAL_RELATED_FOREING_KEY_NAME, null);
167
//        return codeName;
168
//    }
169

    
170
    @Override
171
    public String getTableName() {
172
        Tagged attribute = this.getObject();
173
        String tableName = attribute.getTags().getString(DAL_RELATED_TABLE, null);
174
        return tableName;
175
    }
176
    
177
    @Override
178
    public ContextRelatedFeatures createContext() {
179
        return new DefaultContextRelatedFeatures();
180
    }
181

    
182
    private void disposeIfLocalContext(ContextRelatedFeatures context) {
183
        DefaultContextRelatedFeatures c = (DefaultContextRelatedFeatures) context;
184
        c.release();
185
        if (c.refs == 0) {
186
            context.dispose();
187
        }
188
    }
189

    
190
    private ContextRelatedFeatures createLocalContextIfNull(ContextRelatedFeatures context) {
191
        if (context == null) {
192
            return new DefaultContextRelatedFeatures();
193
        }
194
        DefaultContextRelatedFeatures c = (DefaultContextRelatedFeatures) context;
195
        c.addRef();
196
        return c;
197
    }
198

    
199
    @Override
200
    public FeatureStore getFeatureStore(ContextRelatedFeatures context) {
201
        context = createLocalContextIfNull(context);
202
        try {
203
            return context.getFeatureStore();
204
        } finally {
205
            disposeIfLocalContext(context);
206
        }
207
    }
208
    
209
//    @Override
210
//    public Object getForeingKey(ContextRelatedFeatures context, Feature feature) {
211
////        context = createLocalContextIfNull(context);
212
//        try {
213
//            return feature.get(getForeingKeyName());
214
//        } finally {
215
////            disposeIfLocalContext(context);
216
//        }
217
//    }
218

    
219
    @Override
220
    public Object getUniqueKey(ContextRelatedFeatures context, Feature feature) {
221
//        context = createLocalContextIfNull(context);
222
        try {
223
            return feature.get(getUniqueKeyName());
224
        } finally {
225
//            disposeIfLocalContext(context);
226
        }
227
    }
228

    
229
//    @Override
230
//    public FeatureQuery getForeingKeyQuery(ContextRelatedFeatures context, Object codeValue) {
231
//        return this.getQuery(context, this.getForeingKeyName(), codeValue);
232
//    }
233

    
234
    @Override
235
    public FeatureQuery getUniqueKeyQuery(ContextRelatedFeatures context, Object codeValue) {
236
        return this.getQuery(context, this.getUniqueKeyName(), codeValue);
237
    }
238

    
239
    public FeatureQuery getQuery(ContextRelatedFeatures context, String fieldName, Object codeValue) {
240
        context = createLocalContextIfNull(context);
241
        try {
242
            FeatureStore store = context.getFeatureStore();
243
            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
244
            FeatureQuery query = store.createFeatureQuery();
245
            query.setFilter(builder.eq(
246
                    builder.variable(fieldName),
247
                    builder.constant(codeValue)
248
            ).toString()
249
            );
250
            query.retrievesAllAttributes();
251
            return query;
252
        } finally {
253
            disposeIfLocalContext(context);
254
        }
255
    }
256

    
257
    @Override
258
    public List<String> getColumns(ContextRelatedFeatures context) {
259
        context = createLocalContextIfNull(context);
260
        try {
261
            Tagged attribute = this.getObject();
262
            List<String> columnNames;
263
            String columns = attribute.getTags().getString(DAL_RELATED_COLUMNS, null);
264
            if (StringUtils.isBlank(columns)) {
265
                FeatureType featureType = context.getFeatureType();
266
                if (featureType == null) {
267
                    return null;
268
                }
269
                Search search = (Search) ToolsLocator.getComplementsManager().get(
270
                        Search.COMPLEMENT_MANE, featureType
271
                );
272
                columnNames = search.getResultColumnNames();
273
            } else {
274
                columnNames = Arrays.asList(split(columns, ":/;,|-"));
275
            }
276
            return columnNames;
277
        } finally {
278
            disposeIfLocalContext(context);
279
        }
280
    }
281

    
282
    private String[] split(String value, String separators) {
283
        int firstSeparatorPosition = 1000000;
284
        Character sep = null;
285
        for (char ch : separators.toCharArray()) {
286
            int pos = value.indexOf(ch);
287
            if (pos > 0 && pos < firstSeparatorPosition) {
288
                sep = ch;
289
                firstSeparatorPosition = pos;
290
            }
291
        }
292
        if (sep == null) {
293
            return new String[]{value};
294
        }
295
        return value.split("[" + sep + "]");
296
    }
297

    
298
}