gvsig-projects-pool / org.gvsig.vcsgis / trunk / org.gvsig.vcsgis / org.gvsig.vcsgis.lib / org.gvsig.vcsgis.lib.impl / src / main / java / org / gvsig / vcsgis / repository / localdb / VCSGisRepositoryLocaldb.java @ 2697
History | View | Annotate | Download (8.19 KB)
1 |
/*
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 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, see <https://www.gnu.org/licenses/>.
|
18 |
*
|
19 |
* For any additional information, do not hesitate to contact us
|
20 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
21 |
*/
|
22 |
|
23 |
package org.gvsig.vcsgis.repository.localdb; |
24 |
|
25 |
import java.util.HashMap; |
26 |
import java.util.Map; |
27 |
import java.util.UUID; |
28 |
import javax.json.JsonObject; |
29 |
import org.gvsig.fmap.dal.DALLocator; |
30 |
import org.gvsig.fmap.dal.DataManager; |
31 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
32 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
33 |
import org.gvsig.fmap.dal.feature.FeatureType; |
34 |
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
35 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
36 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
37 |
import org.gvsig.json.Json; |
38 |
import org.gvsig.json.JsonObjectBuilder; |
39 |
import org.gvsig.tools.ToolsLocator; |
40 |
import org.gvsig.tools.dispose.DisposeUtils; |
41 |
import org.gvsig.tools.task.SimpleTaskStatus; |
42 |
import org.gvsig.vcsgis.lib.client.VCSGisWorkspaceEntity; |
43 |
import org.gvsig.vcsgis.lib.repository.VCSGisRepository; |
44 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisCheckoutRequest; |
45 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisCommitRequest; |
46 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisEntitiesRequest; |
47 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisLogRequest; |
48 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisUpdateRequest; |
49 |
import org.gvsig.vcsgis.repository.localdb.requests.CheckoutRequestLocaldb; |
50 |
import org.gvsig.vcsgis.repository.localdb.requests.CommitRequestLocaldb; |
51 |
import org.gvsig.vcsgis.repository.localdb.requests.EntitiesRequestLocaldb; |
52 |
import org.gvsig.vcsgis.repository.localdb.requests.LogRequestLocaldb; |
53 |
import org.gvsig.vcsgis.repository.localdb.requests.UpdateRequestLocaldb; |
54 |
import org.gvsig.vcsgis.repository.localdb.tables.DataRepoTable; |
55 |
import org.gvsig.vcsgis.repository.localdb.tables.EntitiesRepoTable; |
56 |
import org.gvsig.vcsgis.repository.localdb.tables.HooksRepoTable; |
57 |
import org.gvsig.vcsgis.repository.localdb.tables.RevisionsRepoTable; |
58 |
import org.gvsig.vcsgis.repository.localdb.tables.ServerConfigRepoTable; |
59 |
import org.gvsig.vcsgis.repository.localdb.tables.TopologyplanRepoTable; |
60 |
import org.gvsig.vcsgis.repository.localdb.tables.UsersRepoTable; |
61 |
import org.slf4j.Logger; |
62 |
import org.slf4j.LoggerFactory; |
63 |
|
64 |
/**
|
65 |
*
|
66 |
* @author gvSIG Team
|
67 |
*/
|
68 |
@SuppressWarnings("UseSpecificCatch") |
69 |
public class VCSGisRepositoryLocaldb implements VCSGisRepository { |
70 |
private static final Logger LOGGER = LoggerFactory.getLogger(VCSGisRepositoryLocaldb.class); |
71 |
|
72 |
|
73 |
public static int create(JDBCServerExplorer server, SimpleTaskStatus status) { |
74 |
if( status == null ) { |
75 |
status = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("vcsgis init");
|
76 |
status.add(); |
77 |
} |
78 |
int r = 3; |
79 |
status.push(); |
80 |
try {
|
81 |
status.setTitle("vcsgis init");
|
82 |
FeatureType[] tables = new FeatureType[] { |
83 |
ServerConfigRepoTable.featureType(), |
84 |
UsersRepoTable.featureType(), |
85 |
TopologyplanRepoTable.featureType(), |
86 |
EntitiesRepoTable.featureType(), |
87 |
RevisionsRepoTable.featureType(), |
88 |
DataRepoTable.featureType(), |
89 |
HooksRepoTable.featureType(), |
90 |
}; |
91 |
r = 4;
|
92 |
status.setRangeOfValues(0, tables.length);
|
93 |
int step = 1; |
94 |
for (FeatureType table : tables) {
|
95 |
status.message(table.getLabel()); |
96 |
status.setCurValue(step++); |
97 |
r = 4+step;
|
98 |
JDBCNewStoreParameters table_params = server.getAddParameters(); |
99 |
table_params.setDefaultFeatureType(table.getEditable()); |
100 |
table_params.setTable(table.getTags().getString("ID"));
|
101 |
server.add(server.getProviderName(), table_params, false);
|
102 |
} |
103 |
status.terminate(); |
104 |
} catch(Exception ex) { |
105 |
status.abort(); |
106 |
LOGGER.warn("Can't init repository.",ex);
|
107 |
status.message(ex.getMessage()); |
108 |
return r;
|
109 |
} finally {
|
110 |
status.pop(); |
111 |
} |
112 |
return 0; |
113 |
} |
114 |
|
115 |
private JDBCServerExplorer server;
|
116 |
private Map<String,FeatureStore> stores; |
117 |
|
118 |
public VCSGisRepositoryLocaldb() {
|
119 |
// For persistence
|
120 |
this.stores = null; |
121 |
this.server = null; |
122 |
} |
123 |
|
124 |
public VCSGisRepositoryLocaldb(JDBCServerExplorer server) {
|
125 |
this.stores = null; |
126 |
this.server = server;
|
127 |
} |
128 |
|
129 |
@Override
|
130 |
public String getLabel() { |
131 |
return this.server.getParameters().getUrl(); |
132 |
} |
133 |
|
134 |
public JDBCServerExplorer getServerExplorer() {
|
135 |
return this.server; |
136 |
} |
137 |
|
138 |
public static void selfRegister() { |
139 |
Json.registerSerializer(VCSGisRepositoryLocaldb.class); |
140 |
} |
141 |
|
142 |
@Override
|
143 |
public VCSGisCommitRequest createCommitRequest() {
|
144 |
return new CommitRequestLocaldb(this); |
145 |
} |
146 |
|
147 |
@Override
|
148 |
public VCSGisUpdateRequest createUpdateRequest(VCSGisWorkspaceEntity localEntity) {
|
149 |
return new UpdateRequestLocaldb(this, localEntity.getEntityName(), localEntity.getLocalRevisionCode()); |
150 |
} |
151 |
|
152 |
|
153 |
@Override
|
154 |
public VCSGisCheckoutRequest createCheckoutRequest(String entityName) { |
155 |
return new CheckoutRequestLocaldb(this, entityName); |
156 |
} |
157 |
|
158 |
@Override
|
159 |
public void fromJson(JsonObject json) { |
160 |
try {
|
161 |
DataManager dataManager = DALLocator.getDataManager(); |
162 |
DataServerExplorerParameters explorerParams = (DataServerExplorerParameters) Json.toObject(json, "explorerParams");
|
163 |
this.server = (JDBCServerExplorer) dataManager.openServerExplorer(
|
164 |
explorerParams.getProviderName(), |
165 |
explorerParams |
166 |
); |
167 |
this.stores = new HashMap<>(); |
168 |
} catch (Exception ex) { |
169 |
throw new RuntimeException("Can't restore repository from json object.", ex); |
170 |
} |
171 |
} |
172 |
|
173 |
@Override
|
174 |
public JsonObject toJson() {
|
175 |
return this.toJsonBuilder().build(); |
176 |
} |
177 |
|
178 |
@Override
|
179 |
public JsonObjectBuilder toJsonBuilder() {
|
180 |
JsonObjectBuilder builder = Json.createObjectBuilder(); |
181 |
builder.add_class(this);
|
182 |
builder.add("explorerParams", this.server.getParameters()); |
183 |
return builder;
|
184 |
} |
185 |
|
186 |
public String createUniqueID() { |
187 |
UUID x = UUID.randomUUID(); |
188 |
String s = x.toString().replace("-", ""); |
189 |
return s;
|
190 |
} |
191 |
|
192 |
public FeatureStore getFeatureStore(String tableName) { |
193 |
if( this.stores==null ) { |
194 |
this.stores = new HashMap<>(); |
195 |
} |
196 |
FeatureStore store = this.stores.get(tableName);
|
197 |
if( store==null ) { |
198 |
store = this.openFeatureStore(tableName, null); |
199 |
this.stores.put(tableName, store);
|
200 |
} |
201 |
DisposeUtils.bind(store); |
202 |
return store;
|
203 |
} |
204 |
|
205 |
public FeatureStore openFeatureStore(String tableName, String sql) { |
206 |
FeatureStore store; |
207 |
try {
|
208 |
JDBCStoreParameters params = this.server.get(tableName);
|
209 |
params.setSQL(sql); |
210 |
store = (FeatureStore) DALLocator.getDataManager().openStore(params.getProviderName(), params); |
211 |
return store;
|
212 |
} catch (Exception ex) { |
213 |
throw new RuntimeException("Can't open store '"+tableName+"'.", ex); |
214 |
} |
215 |
} |
216 |
|
217 |
@Override
|
218 |
public VCSGisEntitiesRequest createEntitiesRequest() {
|
219 |
return new EntitiesRequestLocaldb(this); |
220 |
} |
221 |
|
222 |
@Override
|
223 |
public VCSGisLogRequest createLogRequest(String entityName) { |
224 |
return new LogRequestLocaldb(this, entityName); |
225 |
} |
226 |
|
227 |
} |