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 @ 44386
History | View | Annotate | Download (6.8 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 | * @version $Id$
|
||
49 | *
|
||
50 | */
|
||
51 | public class ExportCSVService |
||
52 | extends AbstractExportService
|
||
53 | implements ExportService {
|
||
54 | |||
55 | public static final int MAX_FIELD_NAME_LENGTH = 50; |
||
56 | private FeatureType actualFeatureType;
|
||
57 | |||
58 | protected ExportCSVService(ExportServiceFactory factory, ExportCSVParameters parameters) {
|
||
59 | super(factory, parameters);
|
||
60 | } |
||
61 | |||
62 | @Override
|
||
63 | public ExportCSVParameters getParameters() {
|
||
64 | return (ExportCSVParameters) super.getParameters(); |
||
65 | } |
||
66 | |||
67 | @Override
|
||
68 | protected FilesystemServerExplorer createServerExplorer() throws ExportException { |
||
69 | |||
70 | DataManager dataManager = DALLocator.getDataManager(); |
||
71 | |||
72 | FilesystemServerExplorerParameters explorerParams; |
||
73 | try {
|
||
74 | explorerParams |
||
75 | = (FilesystemServerExplorerParameters) dataManager |
||
76 | .createServerExplorerParameters(FilesystemServerExplorer.NAME); |
||
77 | } catch (Exception e) { |
||
78 | throw new ExportException(e); |
||
79 | } |
||
80 | explorerParams.setRoot(this.getParameters().getFile().getParent());
|
||
81 | |||
82 | FilesystemServerExplorer explorer; |
||
83 | try {
|
||
84 | explorer = (FilesystemServerExplorer) dataManager.openServerExplorer( |
||
85 | "FilesystemExplorer", explorerParams
|
||
86 | ); |
||
87 | return explorer;
|
||
88 | } catch (Exception e) { |
||
89 | throw new ExportException(e); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | @Override
|
||
94 | protected NewDataStoreParameters createTargetNewStoreParameters() throws ExportException { |
||
95 | try {
|
||
96 | FilesystemServerExplorer explorer = this.createServerExplorer();
|
||
97 | NewFeatureStoreParameters newStoreParameters = (NewFeatureStoreParameters) explorer.getAddParameters( |
||
98 | this.getParameters().getFile()
|
||
99 | ); |
||
100 | |||
101 | newStoreParameters.setDynValue("profile", this.getParameters().getProfile()); |
||
102 | newStoreParameters.setDynValue("quotePolicy", this.getParameters().getQuotePolicy()); |
||
103 | newStoreParameters.setDynValue("locale", this.getParameters().getLocale()); |
||
104 | newStoreParameters.setDynValue("recordSeparator", this.getParameters().getRecordSeparator()); |
||
105 | newStoreParameters.setDynValue("delimiter", this.getParameters().getDelimiter()); |
||
106 | newStoreParameters.setDynValue("quoteCharacter", this.getParameters().getQuoteCharacter()); |
||
107 | newStoreParameters.setDynValue("commentStartMarker", this.getParameters().getCommentStartMarker()); |
||
108 | newStoreParameters.setDynValue("escapeCharacter", this.getParameters().getEscapeCharacter()); |
||
109 | |||
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 | ((HasAFile) openStoreParameters).setFile(getParameters().getFile()); |
||
137 | 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 | |||
147 | return openStoreParameters;
|
||
148 | } catch (DataException ex) {
|
||
149 | throw new ExportException(ex); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | @Override
|
||
154 | public List<OpenDataStoreParameters> getTargetOpenStoreParameters() throws ExportException { |
||
155 | List<OpenDataStoreParameters> r = new ArrayList<>(); |
||
156 | r.add(this.createTargetOpenStoreParameters());
|
||
157 | return r;
|
||
158 | } |
||
159 | |||
160 | } |