svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.newlayer / org.gvsig.newlayer.prov / org.gvsig.newlayer.prov.jdbc / src / main / java / org / gvsig / newlayer / prov / jdbc / NewLayerJDBCProvider.java @ 46542
History | View | Annotate | Download (7.86 KB)
1 |
/**
|
---|---|
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.newlayer.prov.jdbc; |
25 |
|
26 |
import java.util.ArrayList; |
27 |
import java.util.List; |
28 |
|
29 |
|
30 |
import org.gvsig.fmap.dal.DALLocator; |
31 |
import org.gvsig.fmap.dal.DataManager; |
32 |
import org.gvsig.fmap.dal.DataServerExplorer; |
33 |
import org.gvsig.fmap.dal.DataStore; |
34 |
import org.gvsig.fmap.dal.DataStoreParameters; |
35 |
import org.gvsig.fmap.dal.DataTypes; |
36 |
import org.gvsig.fmap.dal.exception.DataException; |
37 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
38 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
39 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
40 |
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
41 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer; |
42 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
43 |
import org.gvsig.newlayer.NewLayerException; |
44 |
import org.gvsig.newlayer.NewLayerProviderPanel; |
45 |
import org.gvsig.newlayer.NewLayerService; |
46 |
import org.gvsig.newlayer.NewLayerWizard; |
47 |
import org.gvsig.newlayer.prov.jdbc.panels.GeometryIndexPanel; |
48 |
import org.gvsig.newlayer.prov.jdbc.panels.IdentifiersOptionsPanel; |
49 |
import org.gvsig.newlayer.prov.jdbc.panels.JDBCConnectionPanel; |
50 |
import org.gvsig.newlayer.prov.jdbc.panels.PermissionsPanel; |
51 |
import org.gvsig.newlayer.prov.jdbc.panels.PostCreatingStatementPanel; |
52 |
import org.gvsig.newlayer.prov.jdbc.panels.SelectTableNamePanel; |
53 |
import org.gvsig.newlayer.spi.AbstractNewLayerProvider; |
54 |
|
55 |
/**
|
56 |
* NewLayer provider to create a JDBC table.
|
57 |
*
|
58 |
* @author gvSIG Team
|
59 |
*/
|
60 |
@SuppressWarnings("UseSpecificCatch") |
61 |
public class NewLayerJDBCProvider extends AbstractNewLayerProvider { |
62 |
|
63 |
private final NewLayerJDBCProviderParameters parameters; |
64 |
|
65 |
protected NewLayerJDBCProvider(NewLayerService service) {
|
66 |
super(service);
|
67 |
this.parameters = new NewLayerJDBCProviderParameters(); |
68 |
} |
69 |
|
70 |
@Override
|
71 |
public List<NewLayerProviderPanel> createPanels(NewLayerWizard wizard) { |
72 |
List<NewLayerProviderPanel> panels = new ArrayList<>(); |
73 |
panels.add(new IdentifiersOptionsPanel(wizard, this.parameters)); |
74 |
panels.add(new JDBCConnectionPanel(wizard, this.parameters)); |
75 |
panels.add(new SelectTableNamePanel(wizard, this.parameters)); |
76 |
panels.add(wizard.createFeatureTypePanel()); |
77 |
panels.add(new GeometryIndexPanel(wizard, this.parameters)); |
78 |
panels.add(new PermissionsPanel(wizard, this.parameters)); |
79 |
panels.add(new PostCreatingStatementPanel(wizard, this.parameters)); |
80 |
return panels;
|
81 |
} |
82 |
|
83 |
@Override
|
84 |
public EditableFeatureType getFeatureType() {
|
85 |
return this.parameters.getFeatureType(); |
86 |
} |
87 |
|
88 |
public NewLayerJDBCProviderParameters getParameters() {
|
89 |
return this.parameters; |
90 |
} |
91 |
|
92 |
private JDBCServerExplorer createServerExplorer() throws NewLayerException { |
93 |
try {
|
94 |
DataManager dataManager = DALLocator.getDataManager(); |
95 |
|
96 |
JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer( |
97 |
this.getParameters().getExplorerParameters().getExplorerName(),
|
98 |
this.getParameters().getExplorerParameters()
|
99 |
); |
100 |
return explorer;
|
101 |
} catch (Exception e) { |
102 |
throw new NewLayerException(e); |
103 |
} |
104 |
} |
105 |
|
106 |
@Override
|
107 |
public NewFeatureStoreParameters getNewStoreParameters() throws NewLayerException { |
108 |
try {
|
109 |
EditableFeatureType featureType = this.parameters.getFeatureType();
|
110 |
|
111 |
JDBCServerExplorer explorer = this.createServerExplorer();
|
112 |
|
113 |
String pkname = getParameters().getPrimaryKey();
|
114 |
if (pkname != null) { |
115 |
// pkname it's not part of exportattributes list.
|
116 |
EditableFeatureAttributeDescriptor pk = (EditableFeatureAttributeDescriptor) featureType.get(pkname); |
117 |
if (pk == null) { |
118 |
pk = featureType.add(pkname, DataTypes.LONG); |
119 |
pk.setIsPrimaryKey(true);
|
120 |
pk.setIsAutomatic(true);
|
121 |
} else {
|
122 |
pk.setIsPrimaryKey(true);
|
123 |
} |
124 |
} |
125 |
|
126 |
EditableFeatureAttributeDescriptor attrdescriptor = (EditableFeatureAttributeDescriptor) featureType.getDefaultGeometryAttribute(); |
127 |
if (attrdescriptor != null && this.getParameters().getCreateIndexInGeometryRow()) { |
128 |
attrdescriptor.setIsIndexed(true);
|
129 |
} |
130 |
|
131 |
// ======================================================
|
132 |
// Reprojection: set SRS of geometry field to target SRS
|
133 |
if (attrdescriptor != null) { |
134 |
attrdescriptor.setSRS(this.getParameters().getProjection());
|
135 |
} |
136 |
|
137 |
// ======================================
|
138 |
JDBCNewStoreParameters createTableParams = (JDBCNewStoreParameters) explorer.getAddParameters(); |
139 |
|
140 |
createTableParams.setSelectRole(this.getParameters().getSelectRole());
|
141 |
createTableParams.setInsertRole(this.getParameters().getInsertRole());
|
142 |
createTableParams.setUpdateRole(this.getParameters().getUpdateRole());
|
143 |
createTableParams.setDeleteRole(this.getParameters().getDeleteRole());
|
144 |
createTableParams.setTruncateRole(this.getParameters().getTruncateRole());
|
145 |
createTableParams.setReferenceRole(this.getParameters().getReferenceRole());
|
146 |
createTableParams.setTriggerRole(this.getParameters().getTriggerRole());
|
147 |
createTableParams.setAllRole(this.getParameters().getAllRole());
|
148 |
|
149 |
createTableParams.setSchema(this.getParameters().getSchema());
|
150 |
createTableParams.setPostCreatingStatement(this.getParameters().getPostCreatingStatement());
|
151 |
|
152 |
createTableParams.setDefaultFeatureType(featureType); |
153 |
createTableParams.setTable(this.getParameters().getTableName());
|
154 |
|
155 |
return createTableParams;
|
156 |
|
157 |
} catch (Exception ex) { |
158 |
throw new NewLayerException(ex); |
159 |
} |
160 |
} |
161 |
|
162 |
@Override
|
163 |
public DataStoreParameters getOpenStoreParameters() throws NewLayerException { |
164 |
try {
|
165 |
DataManager dataManager = DALLocator.getDataManager(); |
166 |
JDBCServerExplorer explorer = this.createServerExplorer();
|
167 |
|
168 |
JDBCStoreParameters openStoreParameters = (JDBCStoreParameters) explorer.getOpenParameters(); |
169 |
openStoreParameters.setSchema(this.getParameters().getSchema());
|
170 |
openStoreParameters.setTable(this.getParameters().getTableName());
|
171 |
openStoreParameters.setCRS(this.getParameters().getProjection());
|
172 |
openStoreParameters.setDefaultGeometryField( |
173 |
this.getParameters().getFeatureType().getDefaultGeometryAttributeName()
|
174 |
); |
175 |
return openStoreParameters;
|
176 |
} catch (DataException ex) {
|
177 |
throw new NewLayerException(ex); |
178 |
} |
179 |
} |
180 |
|
181 |
@Override
|
182 |
public DataServerExplorer getExplorer() throws NewLayerException { |
183 |
return this.createServerExplorer(); |
184 |
} |
185 |
|
186 |
@Override
|
187 |
public String getStoreName() { |
188 |
return this.parameters.getExplorerParameters().getProviderName(); |
189 |
} |
190 |
|
191 |
} |