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 @ 43925
History | View | Annotate | Download (8.12 KB)
1 |
|
---|---|
2 |
package org.gvsig.export.spi; |
3 |
|
4 |
import java.util.ArrayList; |
5 |
import java.util.HashSet; |
6 |
import java.util.List; |
7 |
import java.util.Set; |
8 |
import org.cresques.cts.ICoordTrans; |
9 |
import org.cresques.cts.IProjection; |
10 |
import org.gvsig.export.ExportException; |
11 |
import org.gvsig.export.ExportLocator; |
12 |
import org.gvsig.export.ExportParameters; |
13 |
import org.gvsig.fmap.dal.DALLocator; |
14 |
import org.gvsig.fmap.dal.DataManager; |
15 |
import org.gvsig.fmap.dal.DataServerExplorer; |
16 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
17 |
import org.gvsig.fmap.dal.OpenDataStoreParameters; |
18 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
19 |
import org.gvsig.fmap.dal.feature.Feature; |
20 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
21 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
22 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
23 |
import org.gvsig.fmap.dal.feature.FeatureType; |
24 |
import org.gvsig.fmap.geom.Geometry; |
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 boolean isCancellationRequested() { |
98 |
return this.getTaskStatus().isCancellationRequested(); |
99 |
} |
100 |
|
101 |
@Override
|
102 |
public void cancelRequest() { |
103 |
this.getTaskStatus().cancelRequest();
|
104 |
} |
105 |
|
106 |
@Override
|
107 |
public AttributeNamesTranslator getAttributeNamesTranslator() {
|
108 |
if( attributeNamesTranslator==null ) { |
109 |
this.attributeNamesTranslator = ExportLocator.getServiceManager().createAttributeNamesTranslator();
|
110 |
} |
111 |
return this.attributeNamesTranslator; |
112 |
} |
113 |
|
114 |
@Override
|
115 |
public List<OpenDataStoreParameters> getTargetOpenStoreParameters() throws ExportException { |
116 |
List<OpenDataStoreParameters> r = new ArrayList<>(); |
117 |
r.add(this.createTargetOpenStoreParameters());
|
118 |
return r;
|
119 |
} |
120 |
|
121 |
abstract protected DataServerExplorer createServerExplorer() throws ExportException; |
122 |
|
123 |
abstract protected NewDataStoreParameters createTargetNewStoreParameters() throws ExportException; |
124 |
|
125 |
abstract protected OpenDataStoreParameters createTargetOpenStoreParameters() throws ExportException; |
126 |
|
127 |
@Override
|
128 |
public void export(FeatureSet featureSet) throws ExportException { |
129 |
DataServerExplorer explorer = createServerExplorer(); |
130 |
NewDataStoreParameters newStoreParameters = createTargetNewStoreParameters(); |
131 |
OpenDataStoreParameters openStoreParameters = createTargetOpenStoreParameters(); |
132 |
|
133 |
String providerName = newStoreParameters.getDataStoreName();
|
134 |
String explorerName = explorer.getProviderName();
|
135 |
|
136 |
DisposableIterator it = null;
|
137 |
FeatureStore target = null;
|
138 |
EditableFeature newfeature = null;
|
139 |
try {
|
140 |
this.getTaskStatus().setRangeOfValues(0, featureSet.getSize()); |
141 |
|
142 |
DataManager dataManager = DALLocator.getDataManager(); |
143 |
|
144 |
dataManager.newStore(explorerName, providerName, newStoreParameters, true);
|
145 |
target = (FeatureStore) dataManager.openStore(providerName, openStoreParameters); |
146 |
|
147 |
FeatureType targetType = target.getDefaultFeatureType(); |
148 |
|
149 |
target.edit(FeatureStore.MODE_APPEND); |
150 |
it = featureSet.fastIterator(); |
151 |
|
152 |
IProjection targetProjection; |
153 |
FeatureAttributeDescriptor defaultGeom = target.getDefaultFeatureType().getDefaultGeometryAttribute(); |
154 |
if (defaultGeom!=null) { |
155 |
targetProjection = defaultGeom.getSRS(); |
156 |
} else {
|
157 |
targetProjection = null;
|
158 |
} |
159 |
|
160 |
long featureCount = 0; |
161 |
while (it.hasNext()) {
|
162 |
Feature feature = (Feature) it.next(); |
163 |
newfeature = target.createNewFeature(targetType, true);
|
164 |
copyValues(feature, newfeature); |
165 |
// ================================================
|
166 |
// Reprojection stuff
|
167 |
Geometry reproj_geom; |
168 |
IProjection sourceProjection = feature.getDefaultSRS(); |
169 |
|
170 |
ICoordTrans coord_trans; |
171 |
// this comparison is perhaps too preventive
|
172 |
// we could have two instances of same projection
|
173 |
// so we would do more computations than needed
|
174 |
if (sourceProjection != null |
175 |
&& targetProjection != null
|
176 |
&& sourceProjection != targetProjection) { |
177 |
|
178 |
coord_trans = sourceProjection.getCT(targetProjection); |
179 |
reproj_geom = feature.getDefaultGeometry(); |
180 |
reproj_geom = reproj_geom.cloneGeometry(); |
181 |
reproj_geom.reProject(coord_trans); |
182 |
newfeature.setDefaultGeometry(reproj_geom); |
183 |
} |
184 |
// ================================================
|
185 |
|
186 |
target.insert(newfeature); |
187 |
|
188 |
featureCount++; |
189 |
this.getTaskStatus().setCurValue(featureCount);
|
190 |
|
191 |
if (this.getTaskStatus().isCancellationRequested()) { |
192 |
return;
|
193 |
} |
194 |
} |
195 |
target.finishEditing(); |
196 |
this.getTaskStatus().terminate();
|
197 |
this.getTaskStatus().remove();
|
198 |
fireFinishedListener(); |
199 |
} catch (Exception e) { |
200 |
fireCancelledListeners(); |
201 |
throw new ExportException(e, newfeature); |
202 |
} finally {
|
203 |
DisposeUtils.dispose(it); |
204 |
DisposeUtils.dispose(featureSet); |
205 |
DisposeUtils.dispose(target); |
206 |
} |
207 |
} |
208 |
|
209 |
protected void copyValues(Feature source, EditableFeature target) { |
210 |
FeatureType sourceType= source.getType(); |
211 |
FeatureType targetType = target.getType(); |
212 |
for( FeatureAttributeDescriptor targetAttr : targetType) {
|
213 |
String targetAttrName = targetAttr.getName();
|
214 |
String sourceAttrName = getAttributeNamesTranslator().getSourceName(targetAttrName);
|
215 |
FeatureAttributeDescriptor sourceAttr = sourceType.getAttributeDescriptor(sourceAttrName); |
216 |
if( sourceAttr!=null ) { |
217 |
Object value = source.get(sourceAttrName);
|
218 |
if( value == null ) { |
219 |
if( targetAttr.allowNull() ) {
|
220 |
target.set(targetAttrName, null);
|
221 |
} |
222 |
} else {
|
223 |
target.set(targetAttrName, value); |
224 |
} |
225 |
} |
226 |
} |
227 |
} |
228 |
|
229 |
|
230 |
} |