Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.exportto / org.gvsig.exportto.swing / org.gvsig.exportto.swing.prov / org.gvsig.exportto.swing.prov.dbf / src / main / java / org / gvsig / export / dbf / service / ExportDBFService.java @ 44469

History | View | Annotate | Download (5.56 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.export.dbf.service;
25

    
26
import java.io.File;
27
import java.util.ArrayList;
28
import java.util.List;
29
import org.gvsig.export.ExportException;
30

    
31
import org.gvsig.fmap.dal.exception.DataException;
32
import org.gvsig.fmap.dal.feature.FeatureType;
33
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
34
import org.gvsig.fmap.dal.feature.OpenFeatureStoreParameters;
35
import org.gvsig.export.spi.ExportService;
36
import org.gvsig.export.spi.AbstractExportService;
37
import org.gvsig.export.spi.ExportServiceFactory;
38
import org.gvsig.expressionevaluator.ExpressionUtils;
39
import org.gvsig.fmap.dal.DALLocator;
40
import org.gvsig.fmap.dal.DataManager;
41
import org.gvsig.fmap.dal.NewDataStoreParameters;
42
import org.gvsig.fmap.dal.OpenDataStoreParameters;
43
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
44
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters;
45
import org.gvsig.tools.util.HasAFile;
46

    
47
/**
48
 * @author gvSIG Team
49
 * @version $Id$
50
 *
51
 */
52
public class ExportDBFService
53
        extends AbstractExportService
54
        implements ExportService {
55

    
56
    public static final int MAX_FIELD_NAME_LENGTH = 10;
57
    public File targetFile = null;
58

    
59
    protected ExportDBFService(ExportServiceFactory factory, ExportDBFParameters parameters) {
60
        super(factory, parameters);
61
    }
62

    
63
    @Override
64
    public ExportDBFParameters getParameters() {
65
        return (ExportDBFParameters) super.getParameters();
66
    }
67

    
68
    @Override
69
    protected FilesystemServerExplorer createServerExplorer() throws ExportException {
70

    
71
        DataManager dataManager = DALLocator.getDataManager();
72

    
73
        FilesystemServerExplorerParameters explorerParams;
74
        try {
75
            explorerParams
76
                    = (FilesystemServerExplorerParameters) dataManager
77
                            .createServerExplorerParameters(FilesystemServerExplorer.NAME);
78
        } catch (Exception e) {
79
            throw new ExportException(e);
80
        }
81
        explorerParams.setRoot(this.getTargetFile().getParent());
82

    
83
        FilesystemServerExplorer explorer;
84
        try {
85
            explorer = (FilesystemServerExplorer) dataManager.openServerExplorer(
86
                    "FilesystemExplorer", explorerParams
87
            );
88
            return explorer;
89
        } catch (Exception e) {
90
            throw new ExportException(e);
91
        }
92
    }
93

    
94
    @Override
95
    protected NewDataStoreParameters createTargetNewStoreParameters() throws ExportException {
96
        try {
97
            FilesystemServerExplorer explorer = this.createServerExplorer();
98
            NewFeatureStoreParameters newStoreParameters = (NewFeatureStoreParameters) explorer.getAddParameters(
99
                    this.getTargetFile()
100
            );
101
            newStoreParameters.setDynValue("Encoding", this.getParameters().getEncoding());
102
            
103
//            AttributeNamesTranslator nameTranslator = this.getFactory().createAttributeNamesTranslator();
104
//            ExportAttributes exportAttr = this.getParameters().getExportAttributes();
105
//            exportAttr.setNamesTranslator(nameTranslator);
106

    
107
            FeatureType ft = this.getParameters().getExportAttributes().getTargetFeatureType();
108
            if (ft == null) {
109
                ft = this.getParameters().getSourceFeatureType();
110
            }
111

    
112
            // FIXME: check types of fields in ft (remove Geometry?)
113
            newStoreParameters.setDefaultFeatureType(ft.getEditable());
114
            return newStoreParameters;
115
        } catch (DataException ex) {
116
            throw new ExportException(ex);
117
        }
118
    }
119

    
120
    @Override
121
    public OpenDataStoreParameters createTargetOpenStoreParameters() throws ExportException {
122
        try {
123
            DataManager dataManager = DALLocator.getDataManager();
124
            OpenFeatureStoreParameters openStoreParameters = (OpenFeatureStoreParameters) dataManager.createStoreParameters("DBF");
125
            ((HasAFile) openStoreParameters).setFile(this.getTargetFile());
126
            openStoreParameters.setDynValue("Encoding", getParameters().getEncoding());
127
            return openStoreParameters;
128
        } catch (DataException ex) {
129
            throw new ExportException(ex);
130
        }
131
    }
132
    
133
    public File getTargetFile() {
134
        if (this.targetFile == null) {
135
            this.targetFile = ExpressionUtils.evaluateFilename(this.getParameters().getFile());
136
            return this.targetFile;
137
        }
138
        return this.targetFile;
139
    }
140
    @Override
141
    public List<OpenDataStoreParameters> getTargetOpenStoreParameters() throws ExportException {
142
        List<OpenDataStoreParameters> r = new ArrayList<>();
143
        r.add(this.createTargetOpenStoreParameters());
144
        return r;
145
    }
146

    
147
}