svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.geodb.app / org.gvsig.geodb.app.mainplugin / src / main / java / org / gvsig / geodb / workspace / DefaultWorkspaceManager.java @ 44297
History | View | Annotate | Download (6.68 KB)
1 | 44297 | jjdelcerro | package org.gvsig.geodb.workspace; |
---|---|---|---|
2 | |||
3 | import org.apache.commons.lang3.StringUtils; |
||
4 | import org.gvsig.fmap.dal.DALLocator; |
||
5 | import org.gvsig.fmap.dal.DataManager; |
||
6 | import static org.gvsig.fmap.dal.DataManager.RESOURCES_FIELD_NAME; |
||
7 | import static org.gvsig.fmap.dal.DataManager.RESOURCES_FIELD_RESOURCE; |
||
8 | import static org.gvsig.fmap.dal.DataManager.RESOURCES_TABLE_NAME; |
||
9 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
10 | import org.gvsig.fmap.dal.DataTypes; |
||
11 | import org.gvsig.fmap.dal.StoresRepository; |
||
12 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
13 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
14 | import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
||
15 | import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer; |
||
16 | import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
||
17 | import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
||
18 | import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
||
19 | import org.slf4j.Logger; |
||
20 | import org.slf4j.LoggerFactory; |
||
21 | |||
22 | /**
|
||
23 | *
|
||
24 | * @author jjdelcerro
|
||
25 | */
|
||
26 | @SuppressWarnings("UseSpecificCatch") |
||
27 | public class DefaultWorkspaceManager implements WorkspaceManager { |
||
28 | |||
29 | private static final Logger LOGGER = LoggerFactory.getLogger(DefaultWorkspaceManager.class); |
||
30 | |||
31 | private static final String REPOSITORY_TABLE_NAME = "gvsigd_repository"; |
||
32 | private static final String CONFIG_TABLE_NAME = "gvsigd_config"; |
||
33 | |||
34 | private final JDBCServerExplorerParameters connection; |
||
35 | |||
36 | public DefaultWorkspaceManager(JDBCServerExplorerParameters connection) {
|
||
37 | this.connection = connection;
|
||
38 | } |
||
39 | |||
40 | @Override
|
||
41 | public JDBCServerExplorerParameters getConnection() {
|
||
42 | return this.connection; |
||
43 | } |
||
44 | |||
45 | private boolean existsTable(String tableName) { |
||
46 | try {
|
||
47 | DataManager dataManager = DALLocator.getDataManager(); |
||
48 | JDBCServerExplorer server = (JDBCServerExplorer) dataManager.openServerExplorer( |
||
49 | this.connection.getExplorerName(),
|
||
50 | this.connection
|
||
51 | ); |
||
52 | for (DataStoreParameters params : server.list()) {
|
||
53 | String theTableName = ((JDBCStoreParameters)params).getTable();
|
||
54 | if( StringUtils.equals(theTableName, tableName) ) {
|
||
55 | return true; |
||
56 | } |
||
57 | } |
||
58 | } catch (Exception ex) { |
||
59 | LOGGER.warn("Can't check if the table '"+tableName+"' exists.",ex); |
||
60 | } |
||
61 | return false; |
||
62 | } |
||
63 | |||
64 | private FeatureStore getTable(String tableName) { |
||
65 | try {
|
||
66 | DataManager dataManager = DALLocator.getDataManager(); |
||
67 | JDBCServerExplorer server = (JDBCServerExplorer) dataManager.openServerExplorer( |
||
68 | this.connection.getExplorerName(),
|
||
69 | this.connection
|
||
70 | ); |
||
71 | DataStoreParameters params = server.get(tableName); |
||
72 | if( params!=null ) { |
||
73 | FeatureStore store = (FeatureStore) dataManager.openStore( |
||
74 | params.getDataStoreName(), |
||
75 | params |
||
76 | ); |
||
77 | return store;
|
||
78 | } |
||
79 | } catch (Exception ex) { |
||
80 | LOGGER.warn("Can't open table '"+tableName+"'.",ex); |
||
81 | } |
||
82 | return null; |
||
83 | } |
||
84 | |||
85 | private void dropTable(String tableName) { |
||
86 | try {
|
||
87 | DataManager dataManager = DALLocator.getDataManager(); |
||
88 | JDBCServerExplorer server = (JDBCServerExplorer) dataManager.openServerExplorer( |
||
89 | this.connection.getExplorerName(),
|
||
90 | this.connection
|
||
91 | ); |
||
92 | DataStoreParameters params = server.get(tableName); |
||
93 | if( params!=null ) { |
||
94 | server.remove(params); |
||
95 | } |
||
96 | } catch (Exception ex) { |
||
97 | LOGGER.warn("Can't drop table '"+tableName+"'.",ex); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | @Override
|
||
102 | public boolean existsResourcesTable() { |
||
103 | return this.existsTable(RESOURCES_TABLE_NAME); |
||
104 | } |
||
105 | |||
106 | @Override
|
||
107 | public void createResourcesTable() { |
||
108 | String tableName = RESOURCES_TABLE_NAME;
|
||
109 | try {
|
||
110 | DataManager dataManager = DALLocator.getDataManager(); |
||
111 | JDBCServerExplorer server = (JDBCServerExplorer) dataManager.openServerExplorer( |
||
112 | this.connection.getExplorerName(),
|
||
113 | this.connection
|
||
114 | ); |
||
115 | JDBCNewStoreParameters params = server.getAddParameters(); |
||
116 | params.setTable(tableName); |
||
117 | EditableFeatureType ft = params.getDefaultFeatureType(); |
||
118 | ft.add(RESOURCES_FIELD_NAME, DataTypes.STRING, 150)
|
||
119 | .setAllowNull(false)
|
||
120 | .setIsPrimaryKey(true);
|
||
121 | ft.add(RESOURCES_FIELD_RESOURCE, DataTypes.BYTEARRAY) |
||
122 | .setAllowNull(true);
|
||
123 | server.add(tableName, params, false);
|
||
124 | } catch (Exception ex) { |
||
125 | LOGGER.warn("Can't create table '"+tableName+"'.",ex); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | @Override
|
||
130 | public FeatureStore getResourcesTable() {
|
||
131 | return this.getTable(RESOURCES_TABLE_NAME); |
||
132 | } |
||
133 | |||
134 | @Override
|
||
135 | public void dropResourcesTable() { |
||
136 | this.dropTable(RESOURCES_TABLE_NAME);
|
||
137 | } |
||
138 | |||
139 | @Override
|
||
140 | public String get(String name) { |
||
141 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
||
142 | } |
||
143 | |||
144 | @Override
|
||
145 | public void set(String name, String value) { |
||
146 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
||
147 | } |
||
148 | |||
149 | @Override
|
||
150 | public StoresRepository getStoresRepository() {
|
||
151 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
||
152 | } |
||
153 | |||
154 | @Override
|
||
155 | public boolean canAnonymousUserWriteInTheTables() { |
||
156 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
||
157 | } |
||
158 | |||
159 | @Override
|
||
160 | public FeatureStore getRepositoryTable() {
|
||
161 | return this.getTable(REPOSITORY_TABLE_NAME); |
||
162 | } |
||
163 | |||
164 | @Override
|
||
165 | public FeatureStore getConfigurationTable() {
|
||
166 | return this.getTable(CONFIG_TABLE_NAME); |
||
167 | } |
||
168 | |||
169 | @Override
|
||
170 | public void createRepositoryTable() { |
||
171 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
||
172 | } |
||
173 | |||
174 | @Override
|
||
175 | public void createConfigurationTable() { |
||
176 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
||
177 | } |
||
178 | |||
179 | @Override
|
||
180 | public boolean existsRepositoryTable() { |
||
181 | return this.existsTable(REPOSITORY_TABLE_NAME); |
||
182 | } |
||
183 | |||
184 | @Override
|
||
185 | public boolean existsConfigurationTable() { |
||
186 | return this.existsTable(CONFIG_TABLE_NAME); |
||
187 | } |
||
188 | |||
189 | } |