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 @ 47347
History | View | Annotate | Download (7.47 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.FileInputStream; |
28 |
import java.io.FileNotFoundException; |
29 |
import java.io.IOException; |
30 |
import java.io.InputStream; |
31 |
import java.io.Reader; |
32 |
import java.nio.charset.Charset; |
33 |
import java.util.Locale; |
34 |
import java.util.logging.Level; |
35 |
import java.util.logging.Logger; |
36 |
import org.apache.commons.io.FilenameUtils; |
37 |
import org.apache.commons.io.IOUtils; |
38 |
import org.apache.commons.lang3.StringUtils; |
39 |
import org.cresques.cts.IProjection; |
40 |
import org.gvsig.fmap.dal.DALLocator; |
41 |
import org.gvsig.fmap.dal.DataManager; |
42 |
import org.gvsig.fmap.dal.DataStoreParameters; |
43 |
import org.gvsig.fmap.dal.DatabaseWorkspaceManager; |
44 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
45 |
import org.gvsig.fmap.dal.feature.FeatureType; |
46 |
import org.gvsig.fmap.dal.store.h2.H2SpatialUtils; |
47 |
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
48 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
49 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
50 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
51 |
import org.gvsig.tools.ToolsLocator; |
52 |
import org.gvsig.tools.i18n.I18nManager; |
53 |
import org.gvsig.tools.task.SimpleTaskStatus; |
54 |
import org.gvsig.tools.util.HasAFile; |
55 |
import org.gvsig.xml2db.lib.api.Xml2dbManager; |
56 |
import org.gvsig.xml2db.lib.api.xmlinfo.XMLInfo; |
57 |
import org.gvsig.xml2db.lib.api.xmlinfo.XMLTableInfo; |
58 |
|
59 |
|
60 |
@SuppressWarnings("UseSpecificCatch") |
61 |
public class DefaultXml2dbManager implements Xml2dbManager { |
62 |
|
63 |
public DefaultXml2dbManager() {
|
64 |
|
65 |
} |
66 |
|
67 |
@Override
|
68 |
public XMLInfo extractStructure(File xml, Charset encoding, IProjection projection, Locale locale, SimpleTaskStatus status) throws IOException, FileNotFoundException { |
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(InputStream xml, Charset encoding, IProjection projection, Locale locale, SimpleTaskStatus status) throws IOException { |
76 |
StructureExtractorImpl extractor = new StructureExtractorImpl();
|
77 |
XMLInfo xmlinfo = extractor.extractStructure(xml, encoding, projection, locale, SimpleTaskStatus.get(status)); |
78 |
return xmlinfo;
|
79 |
} |
80 |
|
81 |
@Override
|
82 |
public XMLInfo extractStructure(Reader xml, IProjection projection, Locale locale, SimpleTaskStatus status) throws IOException { |
83 |
StructureExtractorImpl extractor = new StructureExtractorImpl();
|
84 |
XMLInfo xmlinfo = extractor.extractStructure(xml, projection, locale, SimpleTaskStatus.get(status)); |
85 |
return xmlinfo;
|
86 |
} |
87 |
|
88 |
@Override
|
89 |
public void copyXml2Db(File 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 void copyXml2Db(InputStream xml, Charset encoding, JDBCServerExplorerParameters dbparams, SimpleTaskStatus taskStatus) { |
97 |
CopyXML2dbImpl helper = new CopyXML2dbImpl();
|
98 |
taskStatus = SimpleTaskStatus.get(taskStatus); |
99 |
helper.copyData(xml, encoding, dbparams, taskStatus); |
100 |
} |
101 |
|
102 |
@Override
|
103 |
public JDBCServerExplorerParameters createDatabase(File dbfile, XMLInfo xmlinfo, SimpleTaskStatus taskStatus) { |
104 |
try {
|
105 |
taskStatus = SimpleTaskStatus.get(taskStatus); |
106 |
|
107 |
DataManager dataManager = DALLocator.getDataManager(); |
108 |
String repoID = xmlinfo.getRepositoryID();
|
109 |
if( StringUtils.isBlank(repoID) ) {
|
110 |
repoID = FilenameUtils.getBaseName(dbfile.getName()); |
111 |
} |
112 |
|
113 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
114 |
|
115 |
taskStatus.message(i18n.getTranslation("_Creating_database"));
|
116 |
taskStatus.setIndeterminate(); |
117 |
|
118 |
JDBCServerExplorerParameters serverParams = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(FeatureStore.H2SPATIAL_PROVIDER_NAME); |
119 |
((HasAFile)serverParams).setFile(dbfile); |
120 |
|
121 |
DatabaseWorkspaceManager dbworkspace = dataManager.createDatabaseWorkspaceManager(serverParams); |
122 |
JDBCServerExplorer server = (JDBCServerExplorer) dbworkspace.getServerExplorer(); |
123 |
if( !H2SpatialUtils.existsH2db(dbfile) ) {
|
124 |
server.dropCaches(); |
125 |
} |
126 |
|
127 |
dbworkspace.create(repoID, null);
|
128 |
|
129 |
|
130 |
taskStatus.message(i18n.getTranslation("_Creating_tables"));
|
131 |
taskStatus.setRangeOfValues(0, xmlinfo.size());
|
132 |
for (XMLTableInfo tableInfo : xmlinfo) {
|
133 |
String tablename = tableInfo.getName();
|
134 |
FeatureType ftype = tableInfo.getFeatureType(); |
135 |
JDBCNewStoreParameters tableParams = server.getAddParameters(); |
136 |
tableParams.setTable(tablename); |
137 |
tableParams.setDefaultFeatureType(ftype); |
138 |
server.add(tableParams.getProviderName(), tableParams, false);
|
139 |
taskStatus.incrementCurrentValue(); |
140 |
} |
141 |
taskStatus.message(i18n.getTranslation("_Registering_tables_in_the_repository"));
|
142 |
taskStatus.setRangeOfValues(0, xmlinfo.size());
|
143 |
for (DataStoreParameters storeParams : server.list()) {
|
144 |
JDBCStoreParameters featureStoreParams = (JDBCStoreParameters)storeParams; |
145 |
if( xmlinfo.get(featureStoreParams.getTable())==null ) { |
146 |
continue;
|
147 |
} |
148 |
dbworkspace.writeStoresRepositoryEntry(featureStoreParams.getTable(), featureStoreParams); |
149 |
taskStatus.incrementCurrentValue(); |
150 |
} |
151 |
return serverParams;
|
152 |
} catch (Exception ex) { |
153 |
throw new RuntimeException("Can't create databse '"+dbfile.getAbsolutePath()+"'.", ex); |
154 |
} |
155 |
} |
156 |
|
157 |
@Override
|
158 |
public Charset detectCharset(InputStream is) { |
159 |
return Xml2dbCommons.detectCharset(is);
|
160 |
} |
161 |
|
162 |
@Override
|
163 |
public Charset detectCharset(File file) { |
164 |
FileInputStream is = null; |
165 |
try {
|
166 |
is = new FileInputStream(file); |
167 |
return detectCharset(is);
|
168 |
} catch (Exception ex) { |
169 |
return null; |
170 |
} finally {
|
171 |
IOUtils.closeQuietly(is); |
172 |
} |
173 |
} |
174 |
|
175 |
public IProjection detectProjection(File file, SimpleTaskStatus status) { |
176 |
return new ProjectionExtractorImpl().extractProjection(file, null, status); |
177 |
} |
178 |
} |