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.file / src / main / java / org / gvsig / exportto / swing / prov / file / AbstractExporttoFileService.java @ 43503

History | View | Annotate | Download (5.57 KB)

1 40559 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40559 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5 40435 jjdelcerro
 *
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 40559 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 40435 jjdelcerro
 * 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 40559 jjdelcerro
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23 40435 jjdelcerro
 */
24
package org.gvsig.exportto.swing.prov.file;
25
26
import java.io.File;
27
28
import org.gvsig.exportto.ExporttoLocator;
29
import org.gvsig.exportto.ExporttoManager;
30
import org.gvsig.exportto.ExporttoService;
31
import org.gvsig.exportto.ExporttoServiceException;
32
import org.gvsig.exportto.ExporttoServiceFinishAction;
33
import org.gvsig.fmap.dal.DALLocator;
34
import org.gvsig.fmap.dal.DataManager;
35
import org.gvsig.fmap.dal.exception.DataException;
36
import org.gvsig.fmap.dal.exception.InitializeException;
37
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
38
import org.gvsig.fmap.dal.feature.FeatureSet;
39
import org.gvsig.fmap.dal.feature.FeatureStore;
40
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
41 43364 jjdelcerro
import org.gvsig.fmap.dal.feature.OpenFeatureStoreParameters;
42 40435 jjdelcerro
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
43
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters;
44
import org.gvsig.tools.task.TaskStatus;
45 43503 jjdelcerro
import org.gvsig.tools.util.HasAFile;
46 43502 jjdelcerro
import org.slf4j.Logger;
47
import org.slf4j.LoggerFactory;
48 40435 jjdelcerro
49
/**
50
 * @author gvSIG Team
51
 * @version $Id$
52
 *
53
 */
54
public abstract class AbstractExporttoFileService implements ExporttoService {
55
56 43502 jjdelcerro
    protected static final Logger LOG = LoggerFactory.getLogger(AbstractExporttoFileService.class);
57
58 40435 jjdelcerro
    protected static final ExporttoManager EXPORTTO_MANAGER = ExporttoLocator
59
        .getManager();
60
    protected File file;
61
    protected FeatureStore featureStore;
62
    protected ExporttoService exporttoService;
63
64
    private NewFeatureStoreParameters newFeatureStoreParameters;
65 43364 jjdelcerro
    private OpenFeatureStoreParameters openFeatureStoreParameters;
66 40435 jjdelcerro
    private ExporttoServiceFinishAction exporttoServiceFinishAction;
67
68
    public AbstractExporttoFileService(File file, FeatureStore featureStore) {
69
        super();
70
        this.featureStore = featureStore;
71
        this.file = file;
72
    }
73
74
    public void open() throws ExporttoServiceException {
75
        String path = file.getAbsolutePath();
76
77
        String extension = "." + getFileExtension();
78
79
        if (!(path.toLowerCase().endsWith(extension))) {
80
            path = path + extension;
81
        }
82
83
        File newFile = new File(path);
84
        DataManager dataManager = DALLocator.getDataManager();
85
86
        FilesystemServerExplorerParameters explorerParams;
87
        try {
88
            explorerParams =
89
                (FilesystemServerExplorerParameters) dataManager
90
                    .createServerExplorerParameters(FilesystemServerExplorer.NAME);
91 43503 jjdelcerro
        } catch (Exception e) {
92 40435 jjdelcerro
            throw new ExporttoServiceException(e);
93
        }
94
        explorerParams.setRoot(newFile.getParent());
95
96
        FilesystemServerExplorer explorer;
97
        try {
98 43396 jjdelcerro
            explorer = (FilesystemServerExplorer) dataManager.openServerExplorer(
99
                    "FilesystemExplorer", explorerParams
100
            );
101 43503 jjdelcerro
            String providerName = explorer.getProviderName(newFile);
102 43396 jjdelcerro
            openFeatureStoreParameters = (OpenFeatureStoreParameters) dataManager.createStoreParameters(providerName);
103 43503 jjdelcerro
            ((HasAFile)openFeatureStoreParameters).setFile(newFile);
104 43364 jjdelcerro
            addParameters(openFeatureStoreParameters);
105
            newFeatureStoreParameters = (NewFeatureStoreParameters) explorer.getAddParameters(newFile);
106 40435 jjdelcerro
            addParameters(newFeatureStoreParameters);
107
108 43364 jjdelcerro
            exporttoService = EXPORTTO_MANAGER.getExporttoService(
109
                    explorer,
110
                    newFeatureStoreParameters,
111
                    openFeatureStoreParameters
112
                );
113
114
115 43396 jjdelcerro
        } catch (Exception e) {
116 40435 jjdelcerro
            throw new ExporttoServiceException(e);
117
        }
118
    }
119
120
    public abstract void addParameters(
121
        NewFeatureStoreParameters newFeatureStoreParameters)
122
        throws DataException;
123
124 43364 jjdelcerro
    public abstract void addParameters(
125
        OpenFeatureStoreParameters openFeatureStoreParameters)
126
        throws DataException;
127
128 40435 jjdelcerro
    public abstract String getFileExtension();
129
130
    public void export(FeatureSet featureSet) throws ExporttoServiceException {
131
        exporttoService.export(featureSet);
132
        if (exporttoServiceFinishAction != null) {
133
            exporttoServiceFinishAction.finished(file.getName(),
134 43364 jjdelcerro
                openFeatureStoreParameters);
135 40435 jjdelcerro
        }
136
    }
137
138
    public void setFinishAction(
139
        ExporttoServiceFinishAction exporttoServiceFinishAction) {
140
        this.exporttoServiceFinishAction = exporttoServiceFinishAction;
141
    }
142
143
    public TaskStatus getTaskStatus() {
144
        return exporttoService.getTaskStatus();
145
    }
146
147
    public boolean isCancellationRequested() {
148
        return exporttoService.isCancellationRequested();
149
    }
150
151
    public void cancelRequest() {
152
        exporttoService.cancelRequest();
153
    }
154
}