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.csv / src / main / java / org / gvsig / export / csv / service / ExportCSVService.java @ 45818
History | View | Annotate | Download (7.48 KB)
1 | 44310 | omartinez | /**
|
---|---|---|---|
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.csv.service; |
||
25 | |||
26 | import java.util.ArrayList; |
||
27 | import java.util.List; |
||
28 | import org.apache.commons.lang3.StringUtils; |
||
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.fmap.dal.DALLocator; |
||
39 | import org.gvsig.fmap.dal.DataManager; |
||
40 | import org.gvsig.fmap.dal.NewDataStoreParameters; |
||
41 | import org.gvsig.fmap.dal.OpenDataStoreParameters; |
||
42 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
||
43 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
||
44 | import org.gvsig.tools.util.HasAFile; |
||
45 | |||
46 | /**
|
||
47 | * @author gvSIG Team
|
||
48 | *
|
||
49 | */
|
||
50 | 45818 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
51 | 44310 | omartinez | public class ExportCSVService |
52 | extends AbstractExportService
|
||
53 | implements ExportService {
|
||
54 | |||
55 | 44573 | jjdelcerro | // public static final int MAX_FIELD_NAME_LENGTH = 50;
|
56 | 44310 | omartinez | |
57 | protected ExportCSVService(ExportServiceFactory factory, ExportCSVParameters parameters) {
|
||
58 | super(factory, parameters);
|
||
59 | } |
||
60 | |||
61 | @Override
|
||
62 | public ExportCSVParameters getParameters() {
|
||
63 | return (ExportCSVParameters) super.getParameters(); |
||
64 | } |
||
65 | |||
66 | @Override
|
||
67 | protected FilesystemServerExplorer createServerExplorer() throws ExportException { |
||
68 | |||
69 | DataManager dataManager = DALLocator.getDataManager(); |
||
70 | |||
71 | FilesystemServerExplorerParameters explorerParams; |
||
72 | try {
|
||
73 | explorerParams |
||
74 | = (FilesystemServerExplorerParameters) dataManager |
||
75 | .createServerExplorerParameters(FilesystemServerExplorer.NAME); |
||
76 | } catch (Exception e) { |
||
77 | throw new ExportException(e); |
||
78 | } |
||
79 | 44573 | jjdelcerro | explorerParams.setRoot(this.getParameters().getEvaluatedFile().getParent());
|
80 | 44310 | omartinez | |
81 | FilesystemServerExplorer explorer; |
||
82 | try {
|
||
83 | explorer = (FilesystemServerExplorer) dataManager.openServerExplorer( |
||
84 | "FilesystemExplorer", explorerParams
|
||
85 | ); |
||
86 | return explorer;
|
||
87 | } catch (Exception e) { |
||
88 | throw new ExportException(e); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | @Override
|
||
93 | protected NewDataStoreParameters createTargetNewStoreParameters() throws ExportException { |
||
94 | try {
|
||
95 | FilesystemServerExplorer explorer = this.createServerExplorer();
|
||
96 | 44588 | omartinez | NewFeatureStoreParameters newStoreParameters = (NewFeatureStoreParameters) explorer.getAddParameters("CSV");
|
97 | ((HasAFile) newStoreParameters).setFile(this.getParameters().getEvaluatedFile());
|
||
98 | 44310 | omartinez | newStoreParameters.setDynValue("profile", this.getParameters().getProfile()); |
99 | newStoreParameters.setDynValue("quotePolicy", this.getParameters().getQuotePolicy()); |
||
100 | newStoreParameters.setDynValue("locale", this.getParameters().getLocale()); |
||
101 | newStoreParameters.setDynValue("recordSeparator", this.getParameters().getRecordSeparator()); |
||
102 | newStoreParameters.setDynValue("delimiter", this.getParameters().getDelimiter()); |
||
103 | newStoreParameters.setDynValue("quoteCharacter", this.getParameters().getQuoteCharacter()); |
||
104 | newStoreParameters.setDynValue("commentStartMarker", this.getParameters().getCommentStartMarker()); |
||
105 | newStoreParameters.setDynValue("escapeCharacter", this.getParameters().getEscapeCharacter()); |
||
106 | 44395 | omartinez | newStoreParameters.setDynValue("charset", this.getParameters().getCharset()); |
107 | 45182 | omartinez | newStoreParameters.setDynValue("includeMetadataInHeader", this.getParameters().getIncludeMetadataInHeader()); |
108 | 45818 | jjdelcerro | newStoreParameters.setDynValue("GeometryFormat", this.getParameters().getGeometryFormat()); |
109 | 44310 | omartinez | |
110 | FeatureType ft = this.getParameters().getExportAttributes().getTargetFeatureType();
|
||
111 | if (ft == null) { |
||
112 | ft = this.getParameters().getSourceFeatureType();
|
||
113 | } |
||
114 | |||
115 | // FIXME: check types of fields in ft (remove Geometry?)
|
||
116 | newStoreParameters.setDefaultFeatureType(ft.getEditable()); |
||
117 | return newStoreParameters;
|
||
118 | } catch (DataException ex) {
|
||
119 | throw new ExportException(ex); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | private String checkEmptyStringParam(String param) { |
||
124 | if (StringUtils.isEmpty(param)) {
|
||
125 | return null; |
||
126 | } else {
|
||
127 | return param;
|
||
128 | } |
||
129 | } |
||
130 | |||
131 | @Override
|
||
132 | public OpenDataStoreParameters createTargetOpenStoreParameters() throws ExportException { |
||
133 | try {
|
||
134 | DataManager dataManager = DALLocator.getDataManager(); |
||
135 | OpenFeatureStoreParameters openStoreParameters = (OpenFeatureStoreParameters) dataManager.createStoreParameters("CSV");
|
||
136 | 44573 | jjdelcerro | ((HasAFile) openStoreParameters).setFile(this.getParameters().getEvaluatedFile());
|
137 | 44310 | omartinez | openStoreParameters.setDynValue("profile", this.getParameters().getProfile()); |
138 | openStoreParameters.setDynValue("quotePolicy", this.getParameters().getQuotePolicy()); |
||
139 | openStoreParameters.setDynValue("locale", this.getParameters().getLocale()); |
||
140 | openStoreParameters.setDynValue("recordSeparator", checkEmptyStringParam(this.getParameters().getRecordSeparator())); |
||
141 | openStoreParameters.setDynValue("delimiter", checkEmptyStringParam(this.getParameters().getDelimiter())); |
||
142 | openStoreParameters.setDynValue("quoteCharacter", checkEmptyStringParam(this.getParameters().getQuoteCharacter())); |
||
143 | openStoreParameters.setDynValue("commentStartMarker", checkEmptyStringParam(this.getParameters().getCommentStartMarker())); |
||
144 | openStoreParameters.setDynValue("escapeCharacter", checkEmptyStringParam(this.getParameters().getEscapeCharacter())); |
||
145 | openStoreParameters.setDynValue("firstLineHeader", true); |
||
146 | 44395 | omartinez | openStoreParameters.setDynValue("charset", checkEmptyStringParam(this.getParameters().getCharset())); |
147 | 45182 | omartinez | openStoreParameters.setDynValue("includeMetadataInHeader", this.getParameters().getIncludeMetadataInHeader()); |
148 | 45818 | jjdelcerro | openStoreParameters.setDynValue("GeometryFormat", this.getParameters().getGeometryFormat()); |
149 | 44310 | omartinez | |
150 | return openStoreParameters;
|
||
151 | } catch (DataException ex) {
|
||
152 | throw new ExportException(ex); |
||
153 | } |
||
154 | } |
||
155 | 44469 | omartinez | |
156 | 44310 | omartinez | @Override
|
157 | public List<OpenDataStoreParameters> getTargetOpenStoreParameters() throws ExportException { |
||
158 | List<OpenDataStoreParameters> r = new ArrayList<>(); |
||
159 | r.add(this.createTargetOpenStoreParameters());
|
||
160 | return r;
|
||
161 | } |
||
162 | } |