svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / searchPostProcess / distinctOn / DistinctOn.java @ 45715
History | View | Annotate | Download (9.57 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.fmap.dal.swing.impl.searchPostProcess.distinctOn; |
7 |
|
8 |
|
9 |
import java.io.File; |
10 |
import java.util.Collection; |
11 |
import java.util.HashMap; |
12 |
import java.util.Map; |
13 |
import java.util.Objects; |
14 |
import java.util.UUID; |
15 |
import javax.json.JsonObject; |
16 |
|
17 |
import org.gvsig.fmap.dal.DALLocator; |
18 |
import org.gvsig.fmap.dal.DataManager; |
19 |
import org.gvsig.fmap.dal.DataStoreParameters; |
20 |
import org.gvsig.fmap.dal.exception.DataException; |
21 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
22 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
23 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
24 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
25 |
import org.gvsig.fmap.dal.feature.Feature; |
26 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
27 |
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator; |
28 |
import org.gvsig.fmap.dal.feature.FeatureExtraColumns; |
29 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
30 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
31 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
32 |
import org.gvsig.fmap.dal.feature.FeatureType; |
33 |
import org.gvsig.fmap.dal.feature.impl.DefaultEditableFeatureAttributeDescriptor; |
34 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureAttributeDescriptor; |
35 |
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
36 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer; |
37 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
38 |
import org.gvsig.fmap.dal.swing.impl.searchPostProcess.distinctOn.AggregateOperation.AggregateOperationFactory; |
39 |
import org.gvsig.fmap.dal.swing.searchPostProcess.AbstractSearchPostProcess; |
40 |
import org.gvsig.fmap.dal.swing.searchPostProcess.SearchPostProcessFactory; |
41 |
import org.gvsig.tools.ToolsLocator; |
42 |
import org.gvsig.tools.dataTypes.DataType; |
43 |
import org.gvsig.tools.dynobject.DynObject; |
44 |
import org.gvsig.tools.dynobject.DynStruct; |
45 |
import org.gvsig.tools.folders.FoldersManager; |
46 |
import org.gvsig.tools.util.HasAFile; |
47 |
import org.slf4j.LoggerFactory; |
48 |
|
49 |
|
50 |
/**
|
51 |
*
|
52 |
* @author jovivas
|
53 |
*/
|
54 |
public class DistinctOn extends AbstractSearchPostProcess{ |
55 |
|
56 |
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DistinctOnFactory.class); |
57 |
|
58 |
public DistinctOn (
|
59 |
SearchPostProcessFactory factory, |
60 |
FeatureStore input, |
61 |
FeatureQuery query, |
62 |
DynObject parameters |
63 |
|
64 |
){ |
65 |
super( factory, input, query, parameters);
|
66 |
|
67 |
} |
68 |
|
69 |
|
70 |
@Override
|
71 |
public SearchPostProcessResult execute (FeatureStore inputStore, FeatureQuery query, DynObject parameters){
|
72 |
String uniqueFieldName;
|
73 |
|
74 |
uniqueFieldName = (String) parameters.getDynValue("field"); |
75 |
|
76 |
try {
|
77 |
// crear ruta de archivo temporal
|
78 |
FoldersManager foldersManager = ToolsLocator.getFoldersManager(); |
79 |
File tempFile = foldersManager.getUniqueTemporaryFile("distinctOn_process_"+ UUID.randomUUID().toString()); |
80 |
|
81 |
// crear SERVER STORE
|
82 |
DataManager dataManager = DALLocator.getDataManager(); |
83 |
JDBCServerExplorerParameters serverParameters = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters("H2Spatial");
|
84 |
((HasAFile) serverParameters).setFile(tempFile); |
85 |
JDBCServerExplorer serverExplorer = (JDBCServerExplorer) dataManager.openServerExplorer("H2Spatial",serverParameters);
|
86 |
|
87 |
|
88 |
//Crear tablas en server store
|
89 |
JDBCNewStoreParameters parametersResults = serverExplorer.getAddParameters(); |
90 |
parametersResults.setDynValue("Table", "results"); |
91 |
FeatureType ft_inputStore = inputStore.getDefaultFeatureType(); |
92 |
EditableFeatureType ft_inputStoreNoContainer = dataManager.createFeatureType(); |
93 |
ft_inputStoreNoContainer.addAll(ft_inputStore); |
94 |
for (FeatureAttributeDescriptor attr : ft_inputStore) {
|
95 |
DataType dataType = attr.getDataType(); |
96 |
EditableFeatureAttributeDescriptor eattr = ft_inputStoreNoContainer.getEditableAttributeDescriptor(attr.getName()); |
97 |
if (dataType.isContainer()){
|
98 |
ft_inputStoreNoContainer.remove(attr.getName()); |
99 |
} |
100 |
if (eattr.isComputed()){
|
101 |
eattr.setFeatureAttributeEmulator((FeatureAttributeEmulator) null);
|
102 |
} |
103 |
if (eattr.isPrimaryKey()){
|
104 |
eattr.setIsPrimaryKey(false);
|
105 |
} |
106 |
if (eattr.allowIndexDuplicateds()){
|
107 |
eattr.setAllowIndexDuplicateds(true);
|
108 |
} |
109 |
} |
110 |
Map<String, EditableFeatureAttributeDescriptor> allExtraColumns = new HashMap<>(); |
111 |
for (EditableFeatureAttributeDescriptor column : ft_inputStore.getExtraColumns().getColumns()) {
|
112 |
allExtraColumns.put(column.getName(), column); |
113 |
} |
114 |
for (EditableFeatureAttributeDescriptor column : query.getExtraColumn().getColumns()) {
|
115 |
allExtraColumns.put(column.getName(), column); |
116 |
} |
117 |
if (allExtraColumns.size()>0) { |
118 |
for (EditableFeatureAttributeDescriptor column : allExtraColumns.values()) {
|
119 |
DataType dataType = column.getDataType(); |
120 |
if (!dataType.isContainer()){
|
121 |
ft_inputStoreNoContainer.add(column.getName(), column.getType(), column.getSize()); |
122 |
} |
123 |
} |
124 |
} |
125 |
|
126 |
parametersResults.setDefaultFeatureType(ft_inputStoreNoContainer); |
127 |
|
128 |
serverExplorer.add("H2Spatial", parametersResults, true); |
129 |
|
130 |
|
131 |
DataStoreParameters storeParametersResults = dataManager.createStoreParameters("H2Spatial");
|
132 |
storeParametersResults.setDynValue("database_file",tempFile);
|
133 |
storeParametersResults.setDynValue("Table","results"); |
134 |
|
135 |
//Creaci?n del store con los resultados
|
136 |
FeatureStore storeResults = (FeatureStore) dataManager.openStore("H2Spatial",storeParametersResults);
|
137 |
|
138 |
// Creaci?n del Map con las funciones de agragado
|
139 |
Map <String, AggregateOperation> aggregates = new HashMap <>(); |
140 |
for (FeatureAttributeDescriptor attr : ft_inputStoreNoContainer) {
|
141 |
if (parameters.hasDynValue(attr.getName())){
|
142 |
String operation = (String) parameters.getDynValue(attr.getName()); |
143 |
aggregates.put( |
144 |
attr.getName(), |
145 |
(AggregateOperation) aggregatesOperationFactories.get(operation).create() |
146 |
); |
147 |
} |
148 |
} |
149 |
|
150 |
// distictOn
|
151 |
|
152 |
storeResults.edit(FeatureStore.MODE_APPEND); |
153 |
|
154 |
FeatureSet features; |
155 |
if (query != null){ |
156 |
features = inputStore.getFeatureSet(query); |
157 |
}else{
|
158 |
features = inputStore.getFeatureSet(); |
159 |
} |
160 |
EditableFeature lastFeature = null;
|
161 |
for (Feature feature : features) {
|
162 |
if (lastFeature == null){ |
163 |
lastFeature=storeResults.createNewFeature(feature); |
164 |
for (Map.Entry<String, AggregateOperation> entry : aggregates.entrySet()) { |
165 |
String name = entry.getKey();
|
166 |
AggregateOperation operation = entry.getValue(); |
167 |
operation.reset(); |
168 |
Object value = feature.get(name);
|
169 |
if (operation.isApplicable(value)){
|
170 |
operation.perform(value); |
171 |
} |
172 |
lastFeature.set(name, operation.getValue()); |
173 |
} |
174 |
} |
175 |
if ( Objects.equals(feature.get(uniqueFieldName), lastFeature.get(uniqueFieldName))){
|
176 |
for (Map.Entry<String, AggregateOperation> entry : aggregates.entrySet()) { |
177 |
String name = entry.getKey();
|
178 |
AggregateOperation operation = entry.getValue(); |
179 |
Object value = feature.get(name);
|
180 |
if (operation.isApplicable(value)){
|
181 |
operation.perform(value); |
182 |
} |
183 |
lastFeature.set(name, operation.getValue()); |
184 |
} |
185 |
}else{
|
186 |
storeResults.insert(lastFeature); |
187 |
lastFeature = null;
|
188 |
} |
189 |
} |
190 |
|
191 |
storeResults.insert(lastFeature); |
192 |
|
193 |
storeResults.finishEditing(); |
194 |
// storeUValues.finishEditing();
|
195 |
|
196 |
SearchPostProcessResult searchPostProcessResult = new DefaultSearchPostProcessResult(storeResults,null); |
197 |
return searchPostProcessResult;
|
198 |
|
199 |
} catch (DataException | ValidateDataParametersException ex) {
|
200 |
LOGGER.warn("Can't execute distinct on search post process.", ex);
|
201 |
} |
202 |
|
203 |
return null; |
204 |
} |
205 |
|
206 |
private static Map<String, AggregateOperationFactory> aggregatesOperationFactories = new HashMap<>(); |
207 |
public static void registerAggregateOperation(AggregateOperationFactory aggregateOperationFactory){ |
208 |
aggregatesOperationFactories.put(aggregateOperationFactory.getName(), aggregateOperationFactory); |
209 |
} |
210 |
|
211 |
public static Collection<AggregateOperationFactory> getAggregatesOperationFactories(){ |
212 |
return aggregatesOperationFactories.values();
|
213 |
} |
214 |
|
215 |
|
216 |
|
217 |
} |
218 |
|