Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.annotation / org.gvsig.annotation.lib / org.gvsig.annotation.lib.impl / src / main / java / org / gvsig / annotation / impl / DefaultAnnotationCreationService.java @ 41254

History | View | Annotate | Download (16.1 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.annotation.impl;
25

    
26
import java.io.File;
27
import java.io.FileOutputStream;
28
import java.io.IOException;
29
import java.io.InputStream;
30
import java.io.OutputStream;
31

    
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34

    
35
import org.gvsig.annotation.AnnotationCreationException;
36
import org.gvsig.annotation.AnnotationCreationFinishAction;
37
import org.gvsig.annotation.AnnotationCreationService;
38
import org.gvsig.annotation.AnnotationDataTypes;
39
import org.gvsig.annotation.AnnotationManager;
40
import org.gvsig.annotation.calculator.AnnotationPositionCalculationException;
41
import org.gvsig.annotation.calculator.AnnotationPositionCalculator;
42
import org.gvsig.annotation.calculator.AnnotationPositionCalculatorCreationException;
43
import org.gvsig.fmap.dal.DALLocator;
44
import org.gvsig.fmap.dal.DataManager;
45
import org.gvsig.fmap.dal.DataTypes;
46
import org.gvsig.fmap.dal.exception.DataException;
47
import org.gvsig.fmap.dal.exception.InitializeException;
48
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
49
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
50
import org.gvsig.fmap.dal.feature.EditableFeature;
51
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
52
import org.gvsig.fmap.dal.feature.EditableFeatureType;
53
import org.gvsig.fmap.dal.feature.Feature;
54
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
55
import org.gvsig.fmap.dal.feature.FeatureSet;
56
import org.gvsig.fmap.dal.feature.FeatureStore;
57
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
58
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
59
import org.gvsig.fmap.geom.Geometry;
60
import org.gvsig.tools.dispose.DisposableIterator;
61
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
62
import org.gvsig.tools.task.AbstractMonitorableTask;
63

    
64

    
65
/**
66
 * Default {@link AnnotationCreationService} implementation.
67
 * 
68
 * @author gvSIG Team
69
 * @version $Id$
70
 */
71
public class DefaultAnnotationCreationService extends AbstractMonitorableTask implements AnnotationCreationService {
72
        private static final Logger LOG = LoggerFactory.getLogger(DefaultAnnotationCreationService.class);
73
        private static final String TEMPLATE_NAME = "template.gvslab";
74
        private static DataManager dataManager = DALLocator.getDataManager();
75

    
76
        private AnnotationManager manager;
77
        private FeatureStore sourceStore;
78

    
79
        private int sourceFontTypeAttribute = -1;
80
        private int sourceFontStyleAttribute = -1;
81
        private int sourceFontColorAttribute = -1;
82
        private int sourceRotationAttribute = -1;
83
        private int sourceHeigthAttribute = -1;
84
        private AnnotationPositionCalculator annotationPositionCalculator = null;
85

    
86
        private String destinationGeometryAttributeName = null;
87

    
88
        private AnnotationCreationFinishAction annotationCreationFinishAction = null; 
89

    
90
        /**
91
         * {@link DefaultAnnotationCreationService} constructor with a
92
         * {@link AnnotationManager}.
93
         * 
94
         * @param manager
95
         *            to use in the service
96
         * @throws DataException 
97
         */
98
        public DefaultAnnotationCreationService(FeatureStore featureStore, AnnotationManager manager) throws DataException {
99
                super("annotation");
100
            this.sourceStore = featureStore; 
101
                this.manager = manager;
102
                destinationGeometryAttributeName = featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName();
103
        }
104

    
105
        public AnnotationManager getManager() {
106
                return this.manager;
107
        }
108

    
109
        public FeatureStore createAnnotationStore(String destinationShapeFile, int textValueAttribute)
110
        throws AnnotationCreationException {                
111
                try {
112
                        if (destinationShapeFile == null){
113
                                throw new AnnotationCreationException("File can not be null");
114
                        }        
115

    
116
                        String destinationShapeFileWithoutExtension = destinationShapeFile.toLowerCase().replaceAll("\\.shp", "");
117
                        if (destinationShapeFileWithoutExtension.length() == destinationShapeFile.length()-4){
118
                                destinationShapeFileWithoutExtension =  destinationShapeFile.substring(0, destinationShapeFile.length()-4);
119
                        }else{
120
                                destinationShapeFileWithoutExtension = destinationShapeFile;
121
                        }                        
122

    
123
                        NewFeatureStoreParameters newFeatureStoreParameters = (NewFeatureStoreParameters)dataManager.createNewStoreParameters("FilesystemExplorer", "Shape");
124
                        newFeatureStoreParameters.setDynValue("shpfile", destinationShapeFileWithoutExtension + ".shp");
125
                        newFeatureStoreParameters.setDynValue("dbffile", destinationShapeFileWithoutExtension + ".dbf");
126
                        newFeatureStoreParameters.setDynValue("shxfile", destinationShapeFileWithoutExtension + ".shx");
127
                        newFeatureStoreParameters.setDynValue("crs", sourceStore.getDefaultFeatureType().getDefaultSRS());
128

    
129
                        EditableFeatureType editableFeatureType = sourceStore.getDefaultFeatureType().getEditable();//newFeatureStoreParameters.getDefaultFeatureType().getEditable();
130
                        FeatureAttributeDescriptor[] featureAttributeDescriptors = editableFeatureType.getAttributeDescriptors();
131
                        for (int i=featureAttributeDescriptors.length-1  ; i>=0 ; i--){
132
                                editableFeatureType.remove(i);
133
                        }
134
                        initializeFeatureType(editableFeatureType);
135
                        newFeatureStoreParameters.setDefaultFeatureType(editableFeatureType);
136

    
137
                        dataManager.newStore("FilesystemExplorer", "Shape", newFeatureStoreParameters, true);
138

    
139
                        //If there is not an annotationPositionCalculator it gets the default 
140
                        if (annotationPositionCalculator == null){
141
                                annotationPositionCalculator = manager.getDefaultAnnotationPositionCalculator();
142
                        }        
143

    
144
                        FeatureStore destinationStore = (FeatureStore) dataManager.openStore("Shape", newFeatureStoreParameters);
145

    
146
                        copyFeatureStore(sourceStore, 
147
                                        destinationStore,
148
                                        textValueAttribute);        
149

    
150
                        try {
151
                                copyLegend(destinationStore);
152
                        } catch (IOException e) {
153
                                LOG.error("Error copying the legend");
154
                        }
155

    
156
                        if (annotationCreationFinishAction != null){
157
                                annotationCreationFinishAction.finished(destinationStore);
158
                        }
159

    
160
                        return destinationStore;
161
                } catch (InitializeException e) {
162
                        throw new AnnotationCreationException(e);
163
                } catch (ProviderNotRegisteredException e) {
164
                        throw new AnnotationCreationException(e);
165
                } catch (ValidateDataParametersException e) {
166
                        throw new AnnotationCreationException(e);
167
                } catch (DynFieldNotFoundException e) {
168
                        throw new AnnotationCreationException(e);
169
                } catch (DataException e) {
170
                        throw new AnnotationCreationException(e);
171
                } catch (AnnotationPositionCalculatorCreationException e) {
172
                        throw new AnnotationCreationException(e);
173
                }                         
174
        }        
175

    
176
        public FeatureStore createAnnotationStore(String destinationShapeFile,
177
                        String textValueAttributeName) throws AnnotationCreationException {
178
                try {
179
                        return createAnnotationStore(destinationShapeFile, getIndex(textValueAttributeName));
180
                } catch (DataException e) {
181
                        throw new AnnotationCreationException(e);
182
                }                        
183
        }
184

    
185
        private void initializeFeatureType(EditableFeatureType editableFeatureType) throws DataException
186
        {                
187
                EditableFeatureAttributeDescriptor geometryType = editableFeatureType.add(destinationGeometryAttributeName, DataTypes.GEOMETRY);
188
                geometryType.setAllowNull(false);                
189
                geometryType.setGeometryType(Geometry.TYPES.POINT).setGeometrySubType(Geometry.SUBTYPES.GEOM2D);
190
                geometryType.setSRS(sourceStore.getDefaultFeatureType().getDefaultSRS());
191
                geometryType.setObjectClass(Geometry.class);
192
                editableFeatureType.setDefaultGeometryAttributeName(destinationGeometryAttributeName);
193

    
194
                editableFeatureType.add(AnnotationManager.TEXTVALUE_ATTRIBUTE_NAME, AnnotationDataTypes.TEXT, 256).setAllowNull(true);
195
                editableFeatureType.add(AnnotationManager.FONTTYPE_ATTRIBUTE_NAME, AnnotationDataTypes.FONTTYPE, 30).setAllowNull(true);
196
                editableFeatureType.add(AnnotationManager.FONTSTYLE_ATTRIBUTE_NAME, AnnotationDataTypes.FONTSTYLE, 30).setAllowNull(true);
197
                editableFeatureType.add(AnnotationManager.FONTCOLOR_ATTRIBUTE_NAME, AnnotationDataTypes.FONTCOLOR, 20).setAllowNull(true);
198
                editableFeatureType.add(AnnotationManager.FONTROTATION_ATTRIBUTE_NAME, AnnotationDataTypes.FONTROTATION, 5).setAllowNull(true);
199
                editableFeatureType.add(AnnotationManager.FONTHEGTH_ATTRIBUTE_NAME, AnnotationDataTypes.FONTHEIGHT, 5).setAllowNull(true);
200
        }
201

    
202
        private AttributeInserter createInserter(int attributePosition, Object defaultValue)
203
        {
204
                if (attributePosition > -1){
205
                        return new StoreAttributeInserter(attributePosition);
206
                }else{
207
                        return new DefaultAttributeInserter(defaultValue);
208
                }
209
        }
210

    
211
        private void copyFeatureStore(FeatureStore sourceStore,
212
                        FeatureStore destinationStore, int textAttribute)
213
        throws AnnotationCreationException, DataException {                
214

    
215
                //Start the edition
216
                destinationStore.edit();
217

    
218
                //Copy data
219
                FeatureSet featureSet = sourceStore.getFeatureSet();
220
                DisposableIterator iterator = featureSet.iterator();
221
                
222
                taskStatus.setRangeOfValues(0, featureSet.getSize());
223

    
224
                //Create the attribute inserter's
225
                AttributeInserter fontTypeAttributeInserter = createInserter(sourceFontTypeAttribute, manager.getDefaultFontType());                
226
                AttributeInserter fontStyleAttributeInserter = createInserter(sourceFontStyleAttribute, manager.getDefaultFontStyle());                
227
                AttributeInserter fontColorAttributeInserter = createInserter(sourceFontColorAttribute, manager.getDefaultFontColor());                
228
                AttributeInserter fontRotationAttributeInserter = createInserter(sourceRotationAttribute, manager.getDefaultFontRotation());                
229
                AttributeInserter fontHeigthAttributeInserter = createInserter(sourceHeigthAttribute, manager.getDefaultFontHeight());                
230

    
231
                Feature sourceFeature;
232
                long featureCount = 0;
233
                while (iterator.hasNext()) {                        
234
                        sourceFeature = (Feature) iterator.next();
235

    
236
                        EditableFeature destinationFeature = destinationStore.createNewFeature().getEditable();
237
                        try {
238
                                destinationFeature.set(destinationGeometryAttributeName, annotationPositionCalculator.getAnnotationPosition(sourceFeature));
239
                        } catch (AnnotationPositionCalculationException e) {
240
                                LOG.error("Not possible to get the point for the geometry", e);                                
241
                        }
242
                        destinationFeature.set(AnnotationManager.TEXTVALUE_ATTRIBUTE_NAME, sourceFeature.get(textAttribute));
243
                        destinationFeature.set(AnnotationManager.FONTTYPE_ATTRIBUTE_NAME, fontTypeAttributeInserter.getValue(sourceFeature));
244
                        destinationFeature.set(AnnotationManager.FONTSTYLE_ATTRIBUTE_NAME, fontStyleAttributeInserter.getValue(sourceFeature));
245
                        destinationFeature.set(AnnotationManager.FONTCOLOR_ATTRIBUTE_NAME, fontColorAttributeInserter.getValue(sourceFeature));
246
                        destinationFeature.set(AnnotationManager.FONTROTATION_ATTRIBUTE_NAME, fontRotationAttributeInserter.getValue(sourceFeature));
247
                        destinationFeature.set(AnnotationManager.FONTHEGTH_ATTRIBUTE_NAME, fontHeigthAttributeInserter.getValue(sourceFeature));
248

    
249
                        destinationStore.insert(destinationFeature);
250
                    featureCount++;  
251
                    this.taskStatus.setCurValue(featureCount);
252
    
253
            if (this.taskStatus.isCancellationRequested()) {
254
                return;
255
            }
256
                }
257

    
258
                //Finish the edition
259
                destinationStore.finishEditing();
260

    
261
                //Dispose resources
262
                iterator.dispose();                
263
                featureSet.dispose();
264
                
265
                this.taskStatus.terminate();
266
        this.taskStatus.remove();                
267
        }
268

    
269
        private void copyLegend(FeatureStore destinationStore) throws ValidateDataParametersException, DataException, IOException {
270
                FilesystemServerExplorer filesystemServerExplorer = (FilesystemServerExplorer)destinationStore.getExplorer();
271
                File target = filesystemServerExplorer.getResourcePath(destinationStore, "gvslab");
272

    
273
                //Copy the template
274
                InputStream in = getClass().getClassLoader().getResourceAsStream(TEMPLATE_NAME);
275
                OutputStream out = null;
276

    
277
                out = new FileOutputStream(target);
278

    
279
                byte[] buf = new byte[1024];
280
                int len;
281
                while ((len = in.read(buf)) > 0) { 
282
                        out.write(buf, 0, len); 
283
                } 
284
                in.close();
285
                out.close();                 
286
        }
287

    
288
        private void checkAttribute(int index, int[] type) throws DataException{
289
                if (index >= sourceStore.getDefaultFeatureType().size()){
290
                        throw new IllegalArgumentException("Attribute not found");
291
                }
292
                int datatype = sourceStore.getDefaultFeatureType().getAttributeDescriptor(index).getDataType().getType();
293
                
294
                boolean validType = false;
295
                if(type != null) {
296
                        for (int i = 0; i < type.length; i++) {
297
                                if(datatype == type[i]) {
298
                                        validType = true;
299
                                }
300
                        }
301
                }
302
                
303
                if (!validType) {
304
                        throw new IllegalArgumentException("The Attribute has not have the fine type");
305
                }
306
        }
307

    
308

    
309
        public void setFontTypeAttribute(int index) throws DataException {
310
                checkAttribute(index, new int[]{AnnotationDataTypes.FONTTYPE});
311
                this.sourceFontTypeAttribute = index;                
312
        }
313

    
314
        public void setFontStyleAttribute(int index) throws DataException {
315
                checkAttribute(index, new int[]{AnnotationDataTypes.FONTSTYLE});
316
                this.sourceFontStyleAttribute = index;                
317
        }
318

    
319
        public void setFontColorAttribute(int index) throws DataException {
320
                checkAttribute(index, new int[]{AnnotationDataTypes.FONTCOLOR});
321
                this.sourceFontColorAttribute = index;                
322
        }
323

    
324
        public void setFontHeigthAttribute(int index) throws DataException {
325
                checkAttribute(index, new int[]{AnnotationDataTypes.FONTHEIGHT, DataTypes.INT, DataTypes.LONG, DataTypes.FLOAT});
326
                this.sourceHeigthAttribute = index;                
327
        }
328

    
329
        public void setFontRotationAttribute(int index) throws DataException {
330
                checkAttribute(index, new int[]{AnnotationDataTypes.FONTROTATION});
331
                this.sourceRotationAttribute = index;                
332
        }
333

    
334
        public void setAnnotationPositionCalculator(
335
                        AnnotationPositionCalculator annotationPositionCalculator) {                
336
                this.annotationPositionCalculator = annotationPositionCalculator;                
337
        }
338

    
339
        public int getIndex(String attributeName) throws DataException{
340
                FeatureAttributeDescriptor featureAttributeDescriptor = sourceStore.getDefaultFeatureType().getAttributeDescriptor(attributeName);
341
                if (featureAttributeDescriptor != null){
342
                        return featureAttributeDescriptor.getIndex();
343
                }
344
                return -1;
345
        }
346

    
347
        public void setFontColorAttribute(String attributeName) throws DataException {
348
                setFontColorAttribute(getIndex(attributeName));                
349
        }
350

    
351
        public void setFontHeigthAttribute(String attributeName) throws DataException {
352
                setFontHeigthAttribute(getIndex(attributeName));                                
353
        }
354

    
355
        public void setFontRotationAttribute(String attributeName) throws DataException {
356
                setFontRotationAttribute(getIndex(attributeName));                                
357
        }
358

    
359
        public void setFontStyleAttribute(String attributeName) throws DataException {
360
                setFontStyleAttribute(getIndex(attributeName));                                
361
        }
362

    
363
        public void setFontTypeAttribute(String attributeName) throws DataException {
364
                setFontTypeAttribute(getIndex(attributeName));                                
365
        }
366

    
367

    
368
        public FeatureStore getFeatureStore() {
369
                return sourceStore;
370
        }
371

    
372
        private interface AttributeInserter{
373
                public Object getValue(Feature feature);
374
        }
375

    
376

    
377
        private class StoreAttributeInserter implements AttributeInserter{
378
                private int attributePosition = -1;
379

    
380
                public StoreAttributeInserter(int attributePosition) {
381
                        super();
382
                        this.attributePosition = attributePosition;                        
383
                }
384

    
385
                public Object getValue(Feature feature){
386
                        return feature.get(attributePosition);
387
                }
388
        }
389

    
390
        private class DefaultAttributeInserter implements AttributeInserter{
391
                private Object defaultValue = null;
392

    
393
                public DefaultAttributeInserter(Object defaultValue) {
394
                        super();        
395
                        this.defaultValue = defaultValue;
396
                }
397

    
398
                public Object getValue(Feature feature){
399
                        return defaultValue;
400
                }
401
        }
402

    
403
        public AnnotationCreationFinishAction getAnnotationCreationFinishAction() {
404
                return annotationCreationFinishAction;
405
        }
406

    
407

    
408
        public void setAnnotationCreationFinishAction(
409
                        AnnotationCreationFinishAction annotationCreationFinishAction) {
410
                this.annotationCreationFinishAction = annotationCreationFinishAction;        
411
        }
412
}