Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.xml2db / org.gvsig.xml2db.lib / org.gvsig.xml2db.lib.impl / src / main / java / org / gvsig / xml2db / lib / impl / DefaultXml2dbManager.java @ 47336

History | View | Annotate | Download (6.38 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2023 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.xml2db.lib.impl;
25

    
26
import java.io.File;
27
import java.io.FileNotFoundException;
28
import java.io.IOException;
29
import java.io.InputStream;
30
import java.io.Reader;
31
import java.nio.charset.Charset;
32
import java.util.Locale;
33
import org.apache.commons.io.FilenameUtils;
34
import org.apache.commons.lang3.StringUtils;
35
import org.cresques.cts.IProjection;
36
import org.gvsig.fmap.dal.DALLocator;
37
import org.gvsig.fmap.dal.DataManager;
38
import org.gvsig.fmap.dal.DataStoreParameters;
39
import org.gvsig.fmap.dal.DatabaseWorkspaceManager;
40
import org.gvsig.fmap.dal.feature.FeatureStore;
41
import org.gvsig.fmap.dal.feature.FeatureType;
42
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters;
43
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
44
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
45
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer;
46
import org.gvsig.tools.task.SimpleTaskStatus;
47
import org.gvsig.tools.util.HasAFile;
48
import org.gvsig.xml2db.lib.api.Xml2dbManager;
49
import org.gvsig.xml2db.lib.api.xmlinfo.XMLInfo;
50
import org.gvsig.xml2db.lib.api.xmlinfo.XMLTableInfo;
51

    
52

    
53
@SuppressWarnings("UseSpecificCatch")
54
public class DefaultXml2dbManager implements Xml2dbManager {
55

    
56
    public DefaultXml2dbManager() {
57
        
58
    }
59

    
60
    @Override
61
    public XMLInfo extractStructure(File xml, Charset encoding, IProjection projection, Locale locale, SimpleTaskStatus status) throws IOException, FileNotFoundException {
62
        StructureExtractorImpl extractor = new StructureExtractorImpl();
63
        XMLInfo xmlinfo = extractor.extractStructure(xml, encoding, projection, locale, SimpleTaskStatus.get(status));
64
        return xmlinfo;
65
    }
66
    
67
    @Override
68
    public XMLInfo extractStructure(InputStream xml, Charset encoding, IProjection projection, Locale locale, SimpleTaskStatus status) throws IOException  {
69
        StructureExtractorImpl extractor = new StructureExtractorImpl();
70
        XMLInfo xmlinfo = extractor.extractStructure(xml, encoding, projection, locale, SimpleTaskStatus.get(status));
71
        return xmlinfo;
72
    }
73
    
74
    @Override
75
    public XMLInfo extractStructure(Reader xml, IProjection projection, Locale locale, SimpleTaskStatus status)  throws IOException {
76
        StructureExtractorImpl extractor = new StructureExtractorImpl();
77
        XMLInfo xmlinfo = extractor.extractStructure(xml, projection, locale, SimpleTaskStatus.get(status));
78
        return xmlinfo;
79
    }
80
    
81
    @Override
82
    public void copyXml2Db(File xml, Charset encoding, JDBCServerExplorerParameters dbparams, SimpleTaskStatus taskStatus) {
83
        CopyXML2dbImpl helper = new CopyXML2dbImpl();
84
        taskStatus = SimpleTaskStatus.get(taskStatus);
85
        helper.copyData(xml, encoding, dbparams, taskStatus);
86
    }
87
    
88
    @Override
89
    public void copyXml2Db(InputStream xml, Charset encoding, JDBCServerExplorerParameters dbparams, SimpleTaskStatus taskStatus) {
90
        CopyXML2dbImpl helper = new CopyXML2dbImpl();
91
        taskStatus = SimpleTaskStatus.get(taskStatus);
92
        helper.copyData(xml, encoding, dbparams, taskStatus);
93
    }
94
    
95
    @Override
96
    public JDBCServerExplorerParameters createDatabase(File dbfile, XMLInfo xmlinfo, SimpleTaskStatus taskStatus) {
97
        try {
98
            taskStatus = SimpleTaskStatus.get(taskStatus);
99
            
100
            DataManager dataManager = DALLocator.getDataManager();
101
            String repoID = xmlinfo.getRepositoryID();
102
            if( StringUtils.isBlank(repoID) ) {
103
                repoID = FilenameUtils.getBaseName(dbfile.getName());
104
            }
105
            
106
            taskStatus.message("Creating database...");
107
            taskStatus.setIndeterminate();
108
            
109
            JDBCServerExplorerParameters serverParams = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(FeatureStore.H2SPATIAL_PROVIDER_NAME);
110
            ((HasAFile)serverParams).setFile(dbfile);
111
            
112
            DatabaseWorkspaceManager dbworkspace = dataManager.createDatabaseWorkspaceManager(serverParams);
113
            dbworkspace.create(repoID, null);
114
            
115
            JDBCServerExplorer server = (JDBCServerExplorer) dbworkspace.getServerExplorer();
116

    
117
            taskStatus.message("Creating tables...");
118
            taskStatus.setRangeOfValues(0, xmlinfo.size());
119
            for (XMLTableInfo tableInfo : xmlinfo) {
120
                String tablename = tableInfo.getName();
121
                FeatureType ftype = tableInfo.getFeatureType();
122
                JDBCNewStoreParameters tableParams = server.getAddParameters();
123
                tableParams.setTable(tablename);
124
                tableParams.setDefaultFeatureType(ftype);
125
                server.add(tableParams.getProviderName(), tableParams, false);
126
                taskStatus.incrementCurrentValue();
127
            }
128
            taskStatus.message("Registering tables in the repository...");
129
            taskStatus.setRangeOfValues(0, xmlinfo.size());
130
            for (DataStoreParameters storeParams : server.list()) {
131
                JDBCStoreParameters featureStoreParams = (JDBCStoreParameters)storeParams;
132
                if( xmlinfo.get(featureStoreParams.getTable())==null ) {
133
                    continue;
134
                }
135
                dbworkspace.writeStoresRepositoryEntry(featureStoreParams.getTable(), featureStoreParams);
136
                taskStatus.incrementCurrentValue();
137
            }
138
            return serverParams;
139
        } catch (Exception ex) {
140
            throw new RuntimeException("Can't create databse '"+dbfile.getAbsolutePath()+"'.", ex);
141
        }
142
    }
143
    
144
}