svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.exportto / org.gvsig.exportto.lib / org.gvsig.exportto.lib.api / src / main / java / org / gvsig / export / spi / AbstractExportService.java @ 46608
History | View | Annotate | Download (9.73 KB)
1 |
package org.gvsig.export.spi; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.HashSet; |
5 |
import java.util.List; |
6 |
import java.util.Set; |
7 |
import org.apache.commons.lang3.StringUtils; |
8 |
import org.gvsig.export.ExportAttributes; |
9 |
import org.gvsig.export.ExportException; |
10 |
import org.gvsig.export.ExportLocator; |
11 |
import org.gvsig.export.ExportParameters; |
12 |
import org.gvsig.export.ExportParametersGeometry; |
13 |
import org.gvsig.export.spi.ExportServiceManager.FixGeometryStatus; |
14 |
import org.gvsig.fmap.dal.DALLocator; |
15 |
import org.gvsig.fmap.dal.DataManager; |
16 |
import org.gvsig.fmap.dal.DataServerExplorer; |
17 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
18 |
import org.gvsig.fmap.dal.OpenDataStoreParameters; |
19 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
20 |
import org.gvsig.fmap.dal.feature.Feature; |
21 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
22 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
23 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
24 |
import org.gvsig.fmap.dal.feature.FeatureType; |
25 |
import org.gvsig.tools.ToolsLocator; |
26 |
import org.gvsig.tools.dispose.DisposableIterator; |
27 |
import org.gvsig.tools.dispose.DisposeUtils; |
28 |
import org.gvsig.tools.task.SimpleTaskStatus; |
29 |
import org.slf4j.Logger; |
30 |
import org.slf4j.LoggerFactory; |
31 |
|
32 |
/**
|
33 |
*
|
34 |
* @author jjdelcerro
|
35 |
*/
|
36 |
public abstract class AbstractExportService |
37 |
implements ExportService {
|
38 |
|
39 |
protected static final Logger LOG = LoggerFactory.getLogger(AbstractExportService.class); |
40 |
|
41 |
private final ExportParameters parameters; |
42 |
private final Set<ExportFinishListener> finishListeners; |
43 |
// protected AttributeNamesTranslator attributeNamesTranslator;
|
44 |
private SimpleTaskStatus taskStatus;
|
45 |
private final ExportServiceFactory factory; |
46 |
|
47 |
protected AbstractExportService(ExportServiceFactory factory, ExportParameters parameters) {
|
48 |
this.factory = factory;
|
49 |
this.parameters = parameters;
|
50 |
this.finishListeners = new HashSet<>(); |
51 |
} |
52 |
|
53 |
@Override
|
54 |
public ExportParameters getParameters() {
|
55 |
return this.parameters; |
56 |
} |
57 |
|
58 |
@Override
|
59 |
public ExportServiceFactory getFactory() {
|
60 |
return factory;
|
61 |
} |
62 |
|
63 |
@Override
|
64 |
public void addFinishListener(ExportFinishListener listener) { |
65 |
this.finishListeners.add(listener);
|
66 |
} |
67 |
|
68 |
protected void fireFinishedListener() { |
69 |
for (ExportFinishListener listener : finishListeners) {
|
70 |
try {
|
71 |
listener.finished(this);
|
72 |
} catch (Exception ex) { |
73 |
|
74 |
} |
75 |
} |
76 |
} |
77 |
|
78 |
protected void fireCancelledListeners() { |
79 |
for (ExportFinishListener listener : finishListeners) {
|
80 |
try {
|
81 |
listener.cancelled(this);
|
82 |
} catch (Exception ex) { |
83 |
|
84 |
} |
85 |
} |
86 |
} |
87 |
|
88 |
@Override
|
89 |
public SimpleTaskStatus getTaskStatus() {
|
90 |
if (this.taskStatus == null) { |
91 |
this.taskStatus = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("Export"); |
92 |
} |
93 |
return this.taskStatus; |
94 |
} |
95 |
|
96 |
@Override
|
97 |
public void setTaskStatus(SimpleTaskStatus taskStatus) { |
98 |
this.taskStatus = taskStatus;
|
99 |
} |
100 |
|
101 |
@Override
|
102 |
public boolean isCancellationRequested() { |
103 |
return this.getTaskStatus().isCancellationRequested(); |
104 |
} |
105 |
|
106 |
@Override
|
107 |
public void cancelRequest() { |
108 |
this.getTaskStatus().cancelRequest();
|
109 |
} |
110 |
|
111 |
// @Override
|
112 |
// public AttributeNamesTranslator getAttributeNamesTranslator() {
|
113 |
// if (attributeNamesTranslator == null) {
|
114 |
// this.attributeNamesTranslator = ExportLocator.getServiceManager().createAttributeNamesTranslator();
|
115 |
// }
|
116 |
// return this.attributeNamesTranslator;
|
117 |
// }
|
118 |
@Override
|
119 |
public List<OpenDataStoreParameters> getTargetOpenStoreParameters() throws ExportException { |
120 |
List<OpenDataStoreParameters> r = new ArrayList<>(); |
121 |
r.add(this.createTargetOpenStoreParameters());
|
122 |
return r;
|
123 |
} |
124 |
|
125 |
abstract protected DataServerExplorer createServerExplorer() throws ExportException; |
126 |
|
127 |
abstract protected NewDataStoreParameters createTargetNewStoreParameters() throws ExportException; |
128 |
|
129 |
abstract protected OpenDataStoreParameters createTargetOpenStoreParameters() throws ExportException; |
130 |
|
131 |
protected static class InvalidGeometryException extends ExportException { |
132 |
|
133 |
public InvalidGeometryException(Feature feature, String checkMessage) { |
134 |
super(checkMessage, feature);
|
135 |
} |
136 |
} |
137 |
|
138 |
@Override
|
139 |
public void export(FeatureSet featureSet) throws ExportException { |
140 |
DataServerExplorer explorer = createServerExplorer(); |
141 |
NewDataStoreParameters newStoreParameters = createTargetNewStoreParameters(); |
142 |
OpenDataStoreParameters openStoreParameters = createTargetOpenStoreParameters(); |
143 |
|
144 |
String providerName = newStoreParameters.getDataStoreName();
|
145 |
String explorerName = explorer.getProviderName();
|
146 |
|
147 |
DisposableIterator it = null;
|
148 |
FeatureStore target = null;
|
149 |
EditableFeature targetFeature = null;
|
150 |
try {
|
151 |
this.getTaskStatus().setRangeOfValues(0, featureSet.getSize()); |
152 |
|
153 |
DataManager dataManager = DALLocator.getDataManager(); |
154 |
|
155 |
dataManager.newStore(explorerName, providerName, newStoreParameters, true);
|
156 |
target = (FeatureStore) dataManager.openStore(providerName, openStoreParameters); |
157 |
|
158 |
FeatureType theTargetFeatureType; |
159 |
if (this.getParameters().getExportAttributes().isActive()) { |
160 |
theTargetFeatureType = this.getParameters().getExportAttributes().getTargetFeatureType();
|
161 |
} else {
|
162 |
theTargetFeatureType = target.getDefaultFeatureType(); |
163 |
} |
164 |
FeatureType theSourceFeatureType = featureSet.getDefaultFeatureType(); |
165 |
|
166 |
ExportGeometryHelper geomHelper = null;
|
167 |
if (this.getParameters() instanceof ExportParametersGeometry) { |
168 |
geomHelper = ExportLocator.getServiceManager().createGeometryHelper( |
169 |
(ExportParametersGeometry) this.getParameters(),
|
170 |
theTargetFeatureType, |
171 |
theSourceFeatureType |
172 |
); |
173 |
if (!geomHelper.canProcessGeometry()) {
|
174 |
geomHelper = null;
|
175 |
} |
176 |
} |
177 |
// ============================================
|
178 |
|
179 |
target.edit(FeatureStore.MODE_APPEND); |
180 |
it = featureSet.fastIterator(); |
181 |
|
182 |
long featureCount = 1; |
183 |
while (it.hasNext()) {
|
184 |
this.getTaskStatus().setCurValue(featureCount);
|
185 |
|
186 |
Feature sourceFeature = (Feature) it.next(); |
187 |
targetFeature = target.createNewFeature(true); //theTargetFeatureType, true); |
188 |
copyValues(sourceFeature, targetFeature); |
189 |
|
190 |
if (geomHelper != null) { |
191 |
switch (geomHelper.copyGeometry(sourceFeature, targetFeature)) {
|
192 |
case FixGeometryStatus.STATE_OK:
|
193 |
break;
|
194 |
case FixGeometryStatus.STATE_SKIP:
|
195 |
continue;
|
196 |
case FixGeometryStatus.STATE_ABORT:
|
197 |
throw new InvalidGeometryException(targetFeature, geomHelper.getLastErrorMessage()); |
198 |
} |
199 |
} |
200 |
|
201 |
target.insert(targetFeature); |
202 |
|
203 |
featureCount++; |
204 |
|
205 |
if (this.getTaskStatus().isCancellationRequested()) { |
206 |
return;
|
207 |
} |
208 |
} |
209 |
target.finishEditing(); |
210 |
this.getTaskStatus().terminate();
|
211 |
this.getTaskStatus().remove();
|
212 |
fireFinishedListener(); |
213 |
} catch (Exception e) { |
214 |
FeatureStore.cancelEditingQuietly(target); |
215 |
fireCancelledListeners(); |
216 |
throw new ExportException(e, targetFeature); |
217 |
} finally {
|
218 |
DisposeUtils.dispose(it); |
219 |
DisposeUtils.dispose(featureSet); |
220 |
DisposeUtils.dispose(target); |
221 |
} |
222 |
} |
223 |
|
224 |
protected void copyValues(Feature source, EditableFeature target) { |
225 |
FeatureType sourceType = source.getType(); |
226 |
FeatureType targetType = target.getType(); |
227 |
ExportAttributes exp = this.getParameters().getExportAttributes();
|
228 |
boolean isActive = exp.isActive();
|
229 |
for (FeatureAttributeDescriptor targetAttr : targetType) {
|
230 |
if (targetAttr.isReadOnly() || targetAttr.isComputed()) {
|
231 |
continue;
|
232 |
} |
233 |
String targetAttrName = targetAttr.getName();
|
234 |
//translate field
|
235 |
FeatureAttributeDescriptor sourceAttr; |
236 |
String sourceAttrName;
|
237 |
if (isActive) {
|
238 |
sourceAttrName = exp.getSourceName(targetAttrName); |
239 |
for (ExportAttributes.ExportAttribute exportAttribute : exp) {
|
240 |
if (StringUtils.equals(exportAttribute.getNewName(), targetAttrName)) {
|
241 |
sourceAttrName = exportAttribute.getName(); |
242 |
break;
|
243 |
} |
244 |
} |
245 |
|
246 |
sourceAttr = sourceType.getAttributeDescriptorFromAll(sourceAttrName); |
247 |
} else {
|
248 |
sourceAttrName = targetAttrName; |
249 |
sourceAttr = sourceType.getAttributeDescriptorFromAll(targetAttrName); |
250 |
} |
251 |
if (sourceAttr != null) { |
252 |
Object value = source.get(sourceAttrName);
|
253 |
if (value == null) { |
254 |
if (targetAttr.allowNull()) {
|
255 |
target.set(targetAttrName, null);
|
256 |
} |
257 |
} else {
|
258 |
target.set(targetAttrName, value); |
259 |
} |
260 |
} else if (sourceAttr==null && source.hasValue(sourceAttrName)) { |
261 |
Object value = source.get(sourceAttrName);
|
262 |
target.set(targetAttrName, value); |
263 |
} |
264 |
} |
265 |
} |
266 |
} |
267 |
|
268 |
|