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 @ 44555
History | View | Annotate | Download (5.92 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.apache.commons.io.FilenameUtils; |
30 |
import org.gvsig.export.ExportException; |
31 |
|
32 |
import org.gvsig.fmap.dal.exception.DataException; |
33 |
import org.gvsig.fmap.dal.feature.FeatureType; |
34 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
35 |
import org.gvsig.fmap.dal.feature.OpenFeatureStoreParameters; |
36 |
import org.gvsig.export.spi.ExportService; |
37 |
import org.gvsig.export.spi.AbstractExportService; |
38 |
import org.gvsig.export.spi.ExportServiceFactory; |
39 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
40 |
import org.gvsig.fmap.dal.DALLocator; |
41 |
import org.gvsig.fmap.dal.DataManager; |
42 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
43 |
import org.gvsig.fmap.dal.OpenDataStoreParameters; |
44 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
45 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
46 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
47 |
import org.gvsig.tools.util.HasAFile; |
48 |
|
49 |
/**
|
50 |
* @author gvSIG Team
|
51 |
* @version $Id$
|
52 |
*
|
53 |
*/
|
54 |
public class ExportDBFService |
55 |
extends AbstractExportService
|
56 |
implements ExportService {
|
57 |
|
58 |
public static final int MAX_FIELD_NAME_LENGTH = 10; |
59 |
public File targetFile = null; |
60 |
|
61 |
protected ExportDBFService(ExportServiceFactory factory, ExportDBFParameters parameters) {
|
62 |
super(factory, parameters);
|
63 |
} |
64 |
|
65 |
@Override
|
66 |
public ExportDBFParameters getParameters() {
|
67 |
return (ExportDBFParameters) super.getParameters(); |
68 |
} |
69 |
|
70 |
@Override
|
71 |
protected FilesystemServerExplorer createServerExplorer() throws ExportException { |
72 |
|
73 |
DataManager dataManager = DALLocator.getDataManager(); |
74 |
|
75 |
FilesystemServerExplorerParameters explorerParams; |
76 |
try {
|
77 |
explorerParams |
78 |
= (FilesystemServerExplorerParameters) dataManager |
79 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
80 |
} catch (Exception e) { |
81 |
throw new ExportException(e); |
82 |
} |
83 |
explorerParams.setRoot(this.getTargetFile().getParent());
|
84 |
|
85 |
FilesystemServerExplorer explorer; |
86 |
try {
|
87 |
explorer = (FilesystemServerExplorer) dataManager.openServerExplorer( |
88 |
"FilesystemExplorer", explorerParams
|
89 |
); |
90 |
return explorer;
|
91 |
} catch (Exception e) { |
92 |
throw new ExportException(e); |
93 |
} |
94 |
} |
95 |
|
96 |
@Override
|
97 |
protected NewDataStoreParameters createTargetNewStoreParameters() throws ExportException { |
98 |
try {
|
99 |
FilesystemServerExplorer explorer = this.createServerExplorer();
|
100 |
NewFeatureStoreParameters newStoreParameters = (NewFeatureStoreParameters) explorer.getAddParameters( |
101 |
this.getTargetFile()
|
102 |
); |
103 |
newStoreParameters.setDynValue("Encoding", this.getParameters().getEncoding()); |
104 |
|
105 |
// AttributeNamesTranslator nameTranslator = this.getFactory().createAttributeNamesTranslator();
|
106 |
// ExportAttributes exportAttr = this.getParameters().getExportAttributes();
|
107 |
// exportAttr.setNamesTranslator(nameTranslator);
|
108 |
|
109 |
FeatureType ft = this.getParameters().getExportAttributes().getTargetFeatureType();
|
110 |
if (ft == null) { |
111 |
ft = this.getParameters().getSourceFeatureType();
|
112 |
} |
113 |
|
114 |
// FIXME: check types of fields in ft (remove Geometry?)
|
115 |
newStoreParameters.setDefaultFeatureType(ft.getEditable()); |
116 |
return newStoreParameters;
|
117 |
} catch (DataException ex) {
|
118 |
throw new ExportException(ex); |
119 |
} |
120 |
} |
121 |
|
122 |
@Override
|
123 |
public OpenDataStoreParameters createTargetOpenStoreParameters() throws ExportException { |
124 |
try {
|
125 |
DataManager dataManager = DALLocator.getDataManager(); |
126 |
OpenFeatureStoreParameters openStoreParameters = (OpenFeatureStoreParameters) dataManager.createStoreParameters("DBF");
|
127 |
((HasAFile) openStoreParameters).setFile(this.getTargetFile());
|
128 |
openStoreParameters.setDynValue("Encoding", getParameters().getEncoding());
|
129 |
return openStoreParameters;
|
130 |
} catch (DataException ex) {
|
131 |
throw new ExportException(ex); |
132 |
} |
133 |
} |
134 |
|
135 |
public File getTargetFile() { |
136 |
if (this.targetFile == null) { |
137 |
this.targetFile = ExpressionUtils.evaluateFilename(this.getParameters().getFile()); |
138 |
this.targetFile = new File(FilenameUtils.removeExtension(this.targetFile.getAbsolutePath()) + ".dbf"); |
139 |
return this.targetFile; |
140 |
} |
141 |
return this.targetFile; |
142 |
} |
143 |
@Override
|
144 |
public List<OpenDataStoreParameters> getTargetOpenStoreParameters() throws ExportException { |
145 |
List<OpenDataStoreParameters> r = new ArrayList<>(); |
146 |
r.add(this.createTargetOpenStoreParameters());
|
147 |
return r;
|
148 |
} |
149 |
|
150 |
@Override
|
151 |
public void export(FeatureSet featureSet) throws ExportException { |
152 |
this.targetFile = null; |
153 |
super.export(featureSet);
|
154 |
} |
155 |
|
156 |
} |