gvsig-projects-pool / org.gvsig.report / trunk / org.gvsig.report / org.gvsig.report.lib / org.gvsig.report.lib.impl / src / main / java / org / gvsig / report / lib / impl / DefaultReportDataSet.java @ 671
History | View | Annotate | Download (10.7 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.report.lib.impl; |
25 |
|
26 |
import java.io.File; |
27 |
import java.io.IOException; |
28 |
import java.util.List; |
29 |
import org.apache.commons.lang3.StringUtils; |
30 |
import org.gvsig.fmap.dal.DALLocator; |
31 |
import org.gvsig.fmap.dal.DataManager; |
32 |
import org.gvsig.fmap.dal.DataStoreParameters; |
33 |
import org.gvsig.fmap.dal.exception.DataException; |
34 |
import org.gvsig.fmap.dal.feature.Feature; |
35 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
36 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
37 |
import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
38 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
39 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
40 |
import org.gvsig.fmap.dal.feature.FeatureType; |
41 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
42 |
import org.gvsig.tools.visitor.Visitor; |
43 |
import org.json.JSONObject; |
44 |
import org.gvsig.report.lib.api.ReportDataSet; |
45 |
import org.gvsig.report.lib.api.ReportServices; |
46 |
import org.gvsig.tools.evaluator.Evaluator; |
47 |
import org.gvsig.tools.evaluator.EvaluatorData; |
48 |
import org.gvsig.tools.exception.BaseException; |
49 |
import org.gvsig.tools.util.UnmodifiableTuple; |
50 |
import org.gvsig.tools.util.UnmodifiableTupleListAdapter; |
51 |
import org.gvsig.tools.visitor.VisitCanceledException; |
52 |
import org.json.JSONArray; |
53 |
|
54 |
/**
|
55 |
*
|
56 |
* @author jjdelcerro
|
57 |
*/
|
58 |
@SuppressWarnings("EqualsAndHashcode") |
59 |
public class DefaultReportDataSet implements ReportDataSet { |
60 |
|
61 |
private final ReportServices services; |
62 |
private String name; |
63 |
private FeatureStore store;
|
64 |
private String fullName; |
65 |
private File path; |
66 |
private int limit; |
67 |
private String filter; |
68 |
private boolean exposeGeometry; |
69 |
private final DefaultSelectionOfFeatures selectionOfFeatures; |
70 |
|
71 |
public DefaultReportDataSet(ReportServices services, String name, FeatureStore store) { |
72 |
this.services = services;
|
73 |
this.name = name;
|
74 |
this.store = store;
|
75 |
this.selectionOfFeatures = new DefaultSelectionOfFeatures(this); |
76 |
if (store != null) { |
77 |
this.fullName = store.getFullName();
|
78 |
DataStoreParameters parameters = this.store.getParameters();
|
79 |
if (parameters instanceof FilesystemStoreParameters) { |
80 |
this.path = ((FilesystemStoreParameters) parameters).getFile();
|
81 |
} |
82 |
} |
83 |
} |
84 |
|
85 |
public DefaultReportDataSet(ReportServices services, JSONObject jsonDataSet) {
|
86 |
this.services = services;
|
87 |
this.selectionOfFeatures = new DefaultSelectionOfFeatures(this); |
88 |
this.name = jsonDataSet.getString("name"); |
89 |
this.fullName = jsonDataSet.getString("fullName"); |
90 |
this.limit = jsonDataSet.getInt("limit"); |
91 |
if( jsonDataSet.has("filter") ) { |
92 |
this.filter = jsonDataSet.getString("filter"); |
93 |
} else {
|
94 |
this.filter = null; |
95 |
} |
96 |
this.exposeGeometry = jsonDataSet.getBoolean("exposeGeometry"); |
97 |
if (jsonDataSet.has("path")) { |
98 |
this.path = this.services.derelativizeFile( |
99 |
new File(jsonDataSet.getString("path")) |
100 |
); |
101 |
} |
102 |
this.store = null; |
103 |
if( jsonDataSet.has("features") ) { |
104 |
JSONArray jsonFeatures = jsonDataSet.getJSONArray("features");
|
105 |
for (Object x : jsonFeatures) { |
106 |
String referenceCode = (String) x; |
107 |
this.selectionOfFeatures.add(referenceCode);
|
108 |
} |
109 |
} |
110 |
} |
111 |
|
112 |
JSONObject toJSON() { |
113 |
JSONObject jsonDataSet = new JSONObject();
|
114 |
jsonDataSet.put("name", this.name); |
115 |
jsonDataSet.put("limit", this.limit); |
116 |
jsonDataSet.put("filter", this.filter); |
117 |
jsonDataSet.put("fullName", this.fullName); |
118 |
jsonDataSet.put("exposeGeometry", this.exposeGeometry); |
119 |
if (this.path != null) { |
120 |
jsonDataSet.put("path", this.services.relativizeFile(this.path).getPath()); |
121 |
} |
122 |
if (!this.selectionOfFeatures.isEmpty()) { |
123 |
JSONArray jsonFeatures = new JSONArray();
|
124 |
for (int i = 0; i < this.selectionOfFeatures.size(); i++) { |
125 |
FeatureReference reference = this.selectionOfFeatures.getReference(i);
|
126 |
jsonFeatures.put(reference.getCode()); |
127 |
} |
128 |
jsonDataSet.put("features", jsonFeatures);
|
129 |
} |
130 |
return jsonDataSet;
|
131 |
} |
132 |
|
133 |
@Override
|
134 |
public boolean equals(Object obj) { |
135 |
if (!(obj instanceof DefaultReportDataSet)) { |
136 |
return false; |
137 |
} |
138 |
DefaultReportDataSet other = (DefaultReportDataSet) obj; |
139 |
if (this.store != other.store) { |
140 |
return false; |
141 |
} |
142 |
if (!StringUtils.equals(this.getName(), other.getName())) { |
143 |
return false; |
144 |
} |
145 |
return true; |
146 |
} |
147 |
|
148 |
@Override
|
149 |
public String getName() { |
150 |
return this.name; |
151 |
} |
152 |
|
153 |
@Override
|
154 |
public void setName(String name) { |
155 |
this.name = name;
|
156 |
} |
157 |
|
158 |
@Override
|
159 |
@SuppressWarnings("UseSpecificCatch") |
160 |
public String toString() { |
161 |
try {
|
162 |
FeatureAttributeDescriptor attr = this.getStore().getDefaultFeatureType().getDefaultGeometryAttribute();
|
163 |
String geomType = attr.getGeomType().getName();
|
164 |
return this.name + " (" + geomType + ")"; |
165 |
} catch (Exception ex) { |
166 |
return this.name; |
167 |
} |
168 |
} |
169 |
|
170 |
@Override
|
171 |
public FeatureStore getStore() {
|
172 |
if (this.store == null) { |
173 |
this.store = this.services.getFeatureStore(this); |
174 |
} |
175 |
if (this.store != null) { |
176 |
DataStoreParameters parameters = this.store.getParameters();
|
177 |
if (parameters instanceof FilesystemStoreParameters) { |
178 |
this.path = ((FilesystemStoreParameters) parameters).getFile();
|
179 |
} |
180 |
} |
181 |
return this.store; |
182 |
} |
183 |
|
184 |
@Override
|
185 |
public FeatureType getFeatureType() {
|
186 |
FeatureStore theStore = this.getStore();
|
187 |
try {
|
188 |
return theStore.getDefaultFeatureType();
|
189 |
} catch (DataException ex) {
|
190 |
return null; |
191 |
} |
192 |
} |
193 |
|
194 |
@Override
|
195 |
public boolean hasThisStore(FeatureStore store) { |
196 |
if (store == null) { |
197 |
return false; |
198 |
} |
199 |
if (this.path != null) { |
200 |
try {
|
201 |
DataStoreParameters parameters = store.getParameters(); |
202 |
if (parameters instanceof FilesystemStoreParameters) { |
203 |
File thePath = ((FilesystemStoreParameters) parameters).getFile();
|
204 |
if (this.path.getCanonicalPath().equals(thePath.getCanonicalPath())) { |
205 |
return true; |
206 |
} |
207 |
} |
208 |
} catch (IOException ex) { |
209 |
} |
210 |
} |
211 |
return StringUtils.equals(this.fullName, store.getFullName()); |
212 |
} |
213 |
|
214 |
@Override
|
215 |
public void accept(Visitor visitor) { |
216 |
this.accept(visitor, null, null, true); |
217 |
} |
218 |
|
219 |
@Override
|
220 |
public void accept(Visitor visitor, String filter) { |
221 |
this.accept(visitor, filter, null, true); |
222 |
} |
223 |
|
224 |
@Override
|
225 |
@SuppressWarnings("UseSpecificCatch") |
226 |
public void accept(final Visitor visitor, String filter, String sortfields, boolean ascending) { |
227 |
try {
|
228 |
if (this.selectionOfFeatures.isEmpty()) { |
229 |
FeatureQuery dataQuery = this.getQuery();
|
230 |
if (!StringUtils.isEmpty(filter)) {
|
231 |
dataQuery.addFilter(filter); |
232 |
} |
233 |
if (!StringUtils.isEmpty(sortfields)) {
|
234 |
FeatureQueryOrder order = dataQuery.getOrder(); |
235 |
for (String field : StringUtils.split(sortfields, ',')) { |
236 |
order.add(field, ascending); |
237 |
} |
238 |
} |
239 |
FeatureStore st = this.getStore();
|
240 |
st.accept(visitor, dataQuery); |
241 |
return;
|
242 |
} |
243 |
if (StringUtils.isEmpty(filter)) {
|
244 |
this.selectionOfFeatures.accept(visitor);
|
245 |
return;
|
246 |
} |
247 |
DataManager manager = DALLocator.getDataManager(); |
248 |
final Evaluator eval = manager.createExpresion(filter);
|
249 |
this.selectionOfFeatures.accept(new Visitor() { |
250 |
@Override
|
251 |
public void visit(Object o) throws VisitCanceledException, BaseException { |
252 |
Feature f = (Feature)o; |
253 |
Boolean r = (Boolean) eval.evaluate((EvaluatorData) f); |
254 |
if( r ) {
|
255 |
visitor.visit(f); |
256 |
} |
257 |
} |
258 |
}); |
259 |
} catch (Exception ex) { |
260 |
throw new RuntimeException(ex); |
261 |
} |
262 |
} |
263 |
|
264 |
@Override
|
265 |
public FeatureQuery getQuery() {
|
266 |
FeatureStore st = this.getStore();
|
267 |
FeatureQuery dataQuery = st.createFeatureQuery(); |
268 |
if (!StringUtils.isEmpty(this.filter)) { |
269 |
dataQuery.addFilter(this.filter);
|
270 |
} |
271 |
dataQuery.setLimit(limit); |
272 |
return dataQuery;
|
273 |
} |
274 |
|
275 |
@Override
|
276 |
public SelectionOfFeatures getSelectionOfFeatures() {
|
277 |
return this.selectionOfFeatures; |
278 |
} |
279 |
|
280 |
@Override
|
281 |
public UnmodifiableTuple<Feature> getFeatures() {
|
282 |
List<Feature> r;
|
283 |
if (this.selectionOfFeatures.isEmpty()) { |
284 |
FeatureStore theStore = this.getStore();
|
285 |
FeatureQuery query = this.getQuery();
|
286 |
r = theStore.getFeatures(query); |
287 |
return new UnmodifiableTupleListAdapter(r); |
288 |
} |
289 |
return this.getSelectionOfFeatures(); |
290 |
} |
291 |
|
292 |
@Override
|
293 |
public int getLimit() { |
294 |
return this.limit; |
295 |
} |
296 |
|
297 |
@Override
|
298 |
public String getFilter() { |
299 |
return this.filter; |
300 |
} |
301 |
|
302 |
@Override
|
303 |
public void setLimit(int limit) { |
304 |
this.limit = limit;
|
305 |
} |
306 |
|
307 |
@Override
|
308 |
public void setFilter(String filter) { |
309 |
this.filter = filter;
|
310 |
} |
311 |
|
312 |
@Override
|
313 |
public boolean getExposeGeometry() { |
314 |
return this.exposeGeometry; |
315 |
} |
316 |
|
317 |
@Override
|
318 |
public void setExposeGeometry(boolean selected) { |
319 |
this.exposeGeometry = selected;
|
320 |
} |
321 |
|
322 |
} |