Statistics
| Revision:

root / org.gvsig.wfs.app / trunk / org.gvsig.wfs.app / org.gvsig.wfs.app.mainplugin / src / main / java / org / gvsig / fmap / dal / store / wfs / WFSTTransactionBuilder.java @ 10

History | View | Annotate | Download (5.18 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.fmap.dal.store.wfs;
23

    
24
import java.io.ByteArrayOutputStream;
25
import java.io.OutputStream;
26

    
27
import org.gvsig.fmap.dal.DataTypes;
28
import org.gvsig.fmap.dal.exception.DataException;
29
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
30
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
31
import org.gvsig.fmap.geom.Geometry;
32
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
33
import org.gvsig.fmap.geom.operation.GeometryOperationException;
34
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
35
import org.gvsig.gpe.lib.api.GPELocator;
36
import org.gvsig.gpe.lib.api.GPEManager;
37
import org.gvsig.gpe.lib.api.exceptions.WriterHandlerCreationException;
38
import org.gvsig.gpe.lib.api.writer.IGPEWriterHandler;
39
import org.gvsig.remoteclient.wfs.WFSClient;
40
import org.gvsig.remoteclient.wfs.WFSStatus;
41
import org.gvsig.remoteclient.wfs.edition.WFSTAttributesOperation;
42
import org.gvsig.remoteclient.wfs.edition.WFSTInsertOperation;
43
import org.gvsig.remoteclient.wfs.edition.WFSTTransaction;
44
import org.gvsig.remoteclient.wfs.edition.WFSTUpdateOperation;
45

    
46

    
47
/**
48
 * @author gvSIG Team
49
 * @version $Id$
50
 *
51
 */
52
public class WFSTTransactionBuilder {
53
    private WFSStatus wfsStatus;
54
    private WFSClient wfsClient;
55
    private static final GPEManager gpeManager = GPELocator.getGPEManager();
56
    private WFSTTransaction transaction = null;
57
    
58
    public WFSTTransactionBuilder(WFSStatus wfsStatus, WFSClient wfsClient) {
59
        super();
60
        this.wfsStatus = wfsStatus;
61
        this.wfsClient = wfsClient;
62
        transaction = wfsStatus.createNewTransaction();
63
    }  
64
    
65
    private void addFeatureAttributes(WFSTAttributesOperation attributesOperation, FeatureProvider featureProvider) throws WriterHandlerCreationException, GeometryOperationNotSupportedException, GeometryOperationException{
66
        for (int i=0 ; i<featureProvider.getType().size() ; i++){
67
            FeatureAttributeDescriptor featureAttributeDescriptor = featureProvider.getType().getAttributeDescriptor(i);
68
            String attributeValue = "";
69
            if (featureAttributeDescriptor.getType() != DataTypes.GEOMETRY){
70
                if (featureProvider.get(i) != null){
71
                    attributeValue = featureProvider.get(i).toString();
72
                }
73
            }else{
74
                Geometry geometry = (Geometry)featureProvider.get(i);
75
                attributeValue = toGML(geometry, featureAttributeDescriptor.getSRS().getAbrev());               
76
            }  
77
            attributesOperation.addAttribute(featureAttributeDescriptor.getName(), attributeValue);
78
        }
79
    }
80
    
81
    private String toGML(Geometry geometry, String srs) throws GeometryOperationNotSupportedException, GeometryOperationException, WriterHandlerCreationException{
82
        OutputStream os = new ByteArrayOutputStream();
83
        IGPEWriterHandler writerHandler = gpeManager.createWriterByMimeType("text/xml; subtype=gml/3.1.2");  
84
        writerHandler.setOutputStream(os);
85
        writerHandler.initialize();       
86
        GeometryOperationContext geometryOperationContext = new GeometryOperationContext();
87
        geometryOperationContext.setAttribute("writerHandler", writerHandler);
88
        geometryOperationContext.setAttribute("srs", srs);
89
        geometryOperationContext.setAttribute("id", null);
90
        geometry.invokeOperation("writeGml2", geometryOperationContext);
91
        writerHandler.close(); 
92
        //Remove the xml header
93
        String gml = os.toString();
94
        return gml.substring(38, gml.length());
95
    }
96
    
97
    public void addInsertFeature(FeatureProvider featureProvider) throws WriterHandlerCreationException, GeometryOperationNotSupportedException, GeometryOperationException{
98
        WFSTInsertOperation insertOperation = transaction.createInsertOperation();
99
        addFeatureAttributes(insertOperation, featureProvider);
100
    } 
101

    
102
    public void addUpdateFeature(FeatureProvider featureProvider) throws WriterHandlerCreationException, GeometryOperationNotSupportedException, GeometryOperationException{
103
        WFSTUpdateOperation updateOperation = transaction.createUpdateOperation(featureProvider.getOID().toString());
104
        addFeatureAttributes(updateOperation, featureProvider);
105
    }
106
    
107
    public void addDeleteFeature(String id) throws DataException{
108
        transaction.createDeleteOperation(id);      
109
    }
110
}