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