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 / lib / VCSGisUtils.java @ 3459
History | View | Annotate | Download (24.6 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.lib; |
24 |
|
25 |
import java.io.IOException; |
26 |
import java.io.InputStream; |
27 |
import java.security.MessageDigest; |
28 |
import java.util.ArrayList; |
29 |
import java.util.Collections; |
30 |
import java.util.HashMap; |
31 |
import java.util.Iterator; |
32 |
import java.util.List; |
33 |
import java.util.Map; |
34 |
import java.util.function.Predicate; |
35 |
import javax.json.JsonArray; |
36 |
import javax.json.JsonObject; |
37 |
import javax.json.JsonString; |
38 |
import javax.json.JsonValue; |
39 |
import org.apache.commons.io.IOUtils; |
40 |
import org.apache.commons.lang3.StringUtils; |
41 |
import org.gvsig.fmap.dal.feature.Feature; |
42 |
import org.gvsig.json.Json; |
43 |
import org.gvsig.json.JsonObjectBuilder; |
44 |
import org.gvsig.tools.ToolsLocator; |
45 |
import org.gvsig.tools.util.MapBuilder; |
46 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_AUTHENTICATION_EXPIRED; |
47 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_ADD_ENTITY_WITHOUT_PRIMARY_KEY; |
48 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_ADD_LAYER; |
49 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_AUTHENTICATE_USER; |
50 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_BUILD_REQUEST_RESPONSE; |
51 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_CHECKOUT; |
52 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_COMMIT; |
53 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_COMMIT_WITH_FEATURE_CODE_BLANK; |
54 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_COMMIT_WORKSPACE_OUTDATED; |
55 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_CREATE_TABLE; |
56 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_CREATE_TABLE_CHANGES; |
57 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_CREATE_TABLE_CONFIG; |
58 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_CREATE_TABLE_ENTITIES; |
59 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_EXECUTE_REQUEST; |
60 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_HANDLE_REQUEST; |
61 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_INITIALIZE_REPOSITORY; |
62 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_INSERT_CHANGE; |
63 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_INSERT_ENTITIES; |
64 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_INSERT_FEATURES; |
65 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_OPEN_CHANGES; |
66 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_OPEN_ENTITIES; |
67 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_OPEN_STORE; |
68 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_OPEN_WORKSPACE; |
69 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_OPEN_WORKSPACE_SERVEREXPLORER; |
70 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_PREPARE_UPDATE; |
71 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_REMOVE_CHANGES; |
72 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_REMOVE_ENTITY; |
73 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_RETRIEVE_ENTITIES; |
74 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_RETRIEVE_SOURCE_FEATURES; |
75 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_RETRIEVE_TOPOLOGYPLANS; |
76 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_RETRIEVE_USERS; |
77 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_REVERT; |
78 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_UPDATE; |
79 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_UPDATE_CLEAN; |
80 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_UPDATE_ENTITIES; |
81 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_DBFILE_IS_NULL; |
82 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_ENTITY_ALREADY_EXISTS; |
83 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_ENTITY_NOT_HAS_VCSGISCODE; |
84 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_INVALID_AUTHENTICATION_TOKEN; |
85 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_INVALID_DATA; |
86 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_INVALID_REPOSITORY; |
87 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_INVALID_REVISION; |
88 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_INVALID_REVISION_FOR_ENTITY; |
89 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_INVALID_USERCODE; |
90 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_LABEL_IS_NULL; |
91 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_NO_ERROR; |
92 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_STORE_NOT_IN_VERSION_CONTROL; |
93 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_USER_NOT_AUTHORIZED; |
94 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_WSEXPLORER_IS_NULL; |
95 |
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_ADD_ENTITY; |
96 |
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_DELETE; |
97 |
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_INSERT; |
98 |
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_UPDATE; |
99 |
import static org.gvsig.vcsgis.lib.VCSGisManager.STATE_CONFLICT; |
100 |
import static org.gvsig.vcsgis.lib.VCSGisManager.STATE_LOCAL_MODIFIED; |
101 |
import static org.gvsig.vcsgis.lib.VCSGisManager.STATE_LOCAL_NEW; |
102 |
import static org.gvsig.vcsgis.lib.VCSGisManager.STATE_LOCAL_OUTDATED; |
103 |
import static org.gvsig.vcsgis.lib.VCSGisManager.STATE_LOCAL_OUTDATED_AND_MODIFIED; |
104 |
import static org.gvsig.vcsgis.lib.VCSGisManager.STATE_LOCAL_UNMODIFIED; |
105 |
import static org.gvsig.vcsgis.lib.VCSGisManager.TOPOLOGYPLAN_MODE_RECOMMENDED; |
106 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceEntity; |
107 |
|
108 |
/**
|
109 |
*
|
110 |
* @author gvSIG Team
|
111 |
*/
|
112 |
public class VCSGisUtils { |
113 |
|
114 |
public static final String ENTITY_CODE = "EntityCode"; |
115 |
public static final String ENTITY_NAME = "EntityName"; |
116 |
public static final String ENTITY_DATATABLENAME = "DataTableName"; |
117 |
public static final String ENTITY_DESCRIPTION = "Description"; |
118 |
public static final String ENTITY_FEATUREIDFIELDNAME = "FeatureIdFieldName"; |
119 |
public static final String ENTITY_FEATURETYPEASJSON = "FeatureTypeAsJson"; |
120 |
public static final String ENTITY_FIELDFORLABEL = "FieldForLabel"; |
121 |
public static final String ENTITY_GEOMETRYFIELDNAME = "GeometryFieldName"; |
122 |
public static final String ENTITY_REVISIONCODE = "RevisionCode"; |
123 |
public static final String ENTITY_TOPOLOGYPLANCODE = "TopologyPlanCode"; |
124 |
public static final String ENTITY_USERCODE = "UserCode"; |
125 |
public static final String ENTITY_CATEGORY = "Category"; |
126 |
public static final String ENTITY_LABEL = "Label"; |
127 |
public static final String ENTITY_AUTHORIZATIONS = "Authorizations"; |
128 |
public static final String ENTITY_TOPOLOGYPLANMODE = "TopologyPlanMode"; |
129 |
public static final String ENTITY_LOCALREVISIONCODE = "LocalRevisionCode"; |
130 |
public static final String ENTITY_RESOURCES = "Resources"; |
131 |
public static final String ENTITY_DATA_MODELS = "DataModels"; |
132 |
|
133 |
// private static final String[] JSONENTITY_ATTRNAMES = new String[] {
|
134 |
// ENTITY_DATATABLENAME,
|
135 |
// ENTITY_DESCRIPTION, ENTITY_CODE,
|
136 |
// ENTITY_NAME,
|
137 |
// ENTITY_FEATUREIDFIELDNAME,
|
138 |
// ENTITY_FEATURETYPEASJSON,
|
139 |
// ENTITY_FIELDFORLABEL,
|
140 |
// ENTITY_GEOMETRYFIELDNAME,
|
141 |
// ENTITY_REVISIONCODE,
|
142 |
// ENTITY_TOPOLOGYPLANCODE,
|
143 |
// ENTITY_USERCODE,
|
144 |
// ENTITY_CATEGORY,
|
145 |
// ENTITY_LABEL
|
146 |
// };
|
147 |
|
148 |
private static final Map<Integer,String> ERRMSGS = new MapBuilder<>() |
149 |
.add(ERR_NO_ERROR, "_No_errors")
|
150 |
.add(ERR_DBFILE_IS_NULL, "_dbfile_is_null")
|
151 |
.add(ERR_WSEXPLORER_IS_NULL, "_workspace_explorer_is_null")
|
152 |
.add(ERR_LABEL_IS_NULL, "_label_is_null")
|
153 |
.add(ERR_CANT_OPEN_WORKSPACE, "_Cant_open_workspace")
|
154 |
.add(ERR_CANT_OPEN_WORKSPACE_SERVEREXPLORER, "_Cant_open_workspace")
|
155 |
.add(ERR_STORE_NOT_IN_VERSION_CONTROL, "_Store_not_in_control_version")
|
156 |
.add(ERR_CANT_CREATE_TABLE, "_Cant_create_table")
|
157 |
.add(ERR_CANT_CREATE_TABLE_CONFIG, "_Cant_create_config_table")
|
158 |
.add(ERR_CANT_CREATE_TABLE_ENTITIES, "_Cant_create_table_entities")
|
159 |
.add(ERR_CANT_CREATE_TABLE_CHANGES, "_Cant_create_table_changes")
|
160 |
.add(ERR_ENTITY_ALREADY_EXISTS, "_Entity_already_exists")
|
161 |
.add(ERR_ENTITY_NOT_HAS_VCSGISCODE, "_Entity_has_not_VCSGISCODE")
|
162 |
.add(ERR_CANT_OPEN_ENTITIES, "_Cant_open_entities")
|
163 |
.add(ERR_CANT_INSERT_ENTITIES, "_Cant_insert_entities")
|
164 |
.add(ERR_CANT_OPEN_CHANGES, "_Cant_open_changes")
|
165 |
.add(ERR_CANT_INSERT_CHANGE, "_Cant_insert_change")
|
166 |
.add(ERR_CANT_OPEN_STORE, "_Cant_open_store")
|
167 |
.add(ERR_CANT_RETRIEVE_SOURCE_FEATURES, "_Cant_retrieve_source_features")
|
168 |
.add(ERR_CANT_INSERT_FEATURES, "_Cant_insert_features")
|
169 |
.add(ERR_CANT_COMMIT, "_Cant_commit")
|
170 |
.add(ERR_CANT_COMMIT_WORKSPACE_OUTDATED, "_Cant_commit_workspace_is_out_of_date")
|
171 |
.add(ERR_CANT_CHECKOUT, "_Cant_checkout")
|
172 |
.add(ERR_CANT_UPDATE_ENTITIES, "_Cant_update_entities")
|
173 |
.add(ERR_CANT_PREPARE_UPDATE, "_Cant_prepare_update")
|
174 |
.add(ERR_CANT_UPDATE, "_Cant_update")
|
175 |
.add(ERR_CANT_EXECUTE_REQUEST, "_Cant_execute_request")
|
176 |
.add(ERR_CANT_BUILD_REQUEST_RESPONSE,"_Cant_build_request_response")
|
177 |
.add(ERR_CANT_HANDLE_REQUEST,"_Cant_handle_request")
|
178 |
.add(ERR_CANT_UPDATE_CLEAN,"_Cant_update_clean")
|
179 |
.add(ERR_CANT_COMMIT_WITH_FEATURE_CODE_BLANK,"_Cant_commit_with_feature_code_blank")
|
180 |
.add(ERR_CANT_ADD_ENTITY_WITHOUT_PRIMARY_KEY,"Cant_add_entity_without_primary_key")
|
181 |
.add(ERR_CANT_INITIALIZE_REPOSITORY, "_Cant_initialize_repository")
|
182 |
.add(ERR_INVALID_DATA, "_Data_can_be_empty")
|
183 |
.add(ERR_INVALID_REPOSITORY, "_Invalid_repository")
|
184 |
.add(ERR_INVALID_REVISION, "_Invalid_revision")
|
185 |
.add(ERR_INVALID_REVISION_FOR_ENTITY, "_Invalid_revision_for_entity")
|
186 |
.add(ERR_CANT_RETRIEVE_ENTITIES, "_Cant_retrieve_entities")
|
187 |
.add(ERR_CANT_RETRIEVE_USERS, "_Cant_retrieve_users")
|
188 |
.add(ERR_CANT_RETRIEVE_TOPOLOGYPLANS, "_Cant_retrieve_topologyplans")
|
189 |
.add(ERR_CANT_ADD_LAYER, "_Cant_add_layer_to_view")
|
190 |
.add(ERR_CANT_REMOVE_ENTITY, "_Cant_remove_entity")
|
191 |
.add(ERR_CANT_REMOVE_CHANGES, "_Cant_remove_change")
|
192 |
|
193 |
.add(ERR_CANT_AUTHENTICATE_USER, "_Cant_authenticate_user")
|
194 |
.add(ERR_INVALID_AUTHENTICATION_TOKEN, "_Invalid_authentication_token")
|
195 |
.add(ERR_USER_NOT_AUTHORIZED, "_User_not_authorized")
|
196 |
.add(ERR_AUTHENTICATION_EXPIRED,"_Authentication_expired")
|
197 |
.add(ERR_INVALID_USERCODE, "_Invalid_user_code")
|
198 |
.add(ERR_CANT_REVERT, "_Cant_revert")
|
199 |
.build(); |
200 |
|
201 |
private static final Map<Integer,String> OPERATION_LABELS = new MapBuilder<Integer,String>() |
202 |
.add(OP_DELETE, "Delete")
|
203 |
.add(OP_UPDATE, "Update")
|
204 |
.add(OP_INSERT, "Insert")
|
205 |
.add(OP_ADD_ENTITY, "Add entity")
|
206 |
.build(); |
207 |
|
208 |
private static final Map<Integer,String> STATE_LABELS = new MapBuilder<Integer,String>() |
209 |
.add(STATE_LOCAL_UNMODIFIED, "Unmodified")
|
210 |
.add(STATE_LOCAL_NEW, "New")
|
211 |
.add(STATE_LOCAL_MODIFIED, "Modified")
|
212 |
.add(STATE_LOCAL_OUTDATED, "Outdated")
|
213 |
.add(STATE_LOCAL_OUTDATED_AND_MODIFIED, "Outdated and modified")
|
214 |
.add(STATE_CONFLICT, "Conflict")
|
215 |
.build(); |
216 |
|
217 |
private static final Map<String, Map<String, String>> SQL_TEMPLATES_BY_PROVIDER = new HashMap<>(); |
218 |
|
219 |
public static String getErrorMessage(int errcode) { |
220 |
String msg = ERRMSGS.get(errcode);
|
221 |
msg = ToolsLocator.getI18nManager().getTranslation(msg); |
222 |
return msg;
|
223 |
} |
224 |
|
225 |
public static void copy(VCSGisEntity source, VCSGisEntityEditable target, Predicate<String> excludes) { |
226 |
target.setDataTableName(source.getDataTableName()); |
227 |
target.setDescription(source.getDescription()); |
228 |
target.setEntityCode(source.getEntityCode()); |
229 |
target.setEntityName(source.getEntityName()); |
230 |
target.setFeatureIdFieldName(source.getFeatureIdFieldName()); |
231 |
target.setFeatureTypeAsJson(source.getFeatureTypeAsJson()); |
232 |
target.setFieldForLabel(source.getFieldForLabel()); |
233 |
target.setGeometryFieldName(source.getGeometryFieldName()); |
234 |
target.setRepositoryRevisionCode(source.getRepositoryRevisionCode()); |
235 |
target.setTopologyPlanCode(source.getTopologyPlanCode()); |
236 |
target.setUserCode(source.getUserCode()); |
237 |
target.setCategory(source.getCategory()); |
238 |
target.setLabel(source.getLabel()); |
239 |
target.setAuthorizations(source.getAuthorizations()); |
240 |
target.setTopologyPlanMode(source.getTopologyPlanMode()); |
241 |
target.setResources(source.getResources()); |
242 |
target.setDataModels(source.getDataModels()); |
243 |
if(source instanceof VCSGisWorkspaceEntity && target instanceof VCSGisWorkspaceEntity){ |
244 |
((VCSGisWorkspaceEntity)target).setLocalRevisionCode(((VCSGisWorkspaceEntity)source).getLocalRevisionCode()); |
245 |
} |
246 |
} |
247 |
public static JsonObjectBuilder toJsonBuilder(VCSGisEntity entity, Predicate<String> excludes) { |
248 |
JsonObjectBuilder jsonEntity = Json.createObjectBuilder(); |
249 |
if( excludes==null || !excludes.test(ENTITY_DATATABLENAME) ) { |
250 |
jsonEntity.add(ENTITY_DATATABLENAME, entity.getDataTableName()); |
251 |
} |
252 |
if( excludes==null || !excludes.test(ENTITY_DESCRIPTION) ) { |
253 |
jsonEntity.add(ENTITY_DESCRIPTION, entity.getDescription()); |
254 |
} |
255 |
if( excludes==null || !excludes.test(ENTITY_CODE) ) { |
256 |
jsonEntity.add(ENTITY_CODE, entity.getEntityCode()); |
257 |
} |
258 |
if( excludes==null || !excludes.test(ENTITY_NAME) ) { |
259 |
jsonEntity.add(ENTITY_NAME, entity.getEntityName()); |
260 |
} |
261 |
if( excludes==null || !excludes.test(ENTITY_FEATUREIDFIELDNAME) ) { |
262 |
jsonEntity.add(ENTITY_FEATUREIDFIELDNAME, entity.getFeatureIdFieldName()); |
263 |
} |
264 |
if( excludes==null || !excludes.test(ENTITY_FEATURETYPEASJSON) ) { |
265 |
jsonEntity.add(ENTITY_FEATURETYPEASJSON, entity.getFeatureTypeAsJson()); |
266 |
} |
267 |
if( excludes==null || !excludes.test(ENTITY_FIELDFORLABEL) ) { |
268 |
jsonEntity.add(ENTITY_FIELDFORLABEL, entity.getFieldForLabel()); |
269 |
} |
270 |
if( excludes==null || !excludes.test(ENTITY_GEOMETRYFIELDNAME) ) { |
271 |
jsonEntity.add(ENTITY_GEOMETRYFIELDNAME, entity.getGeometryFieldName()); |
272 |
} |
273 |
if( excludes==null || !excludes.test(ENTITY_REVISIONCODE) ) { |
274 |
jsonEntity.add(ENTITY_REVISIONCODE, entity.getRepositoryRevisionCode()); |
275 |
} |
276 |
if( excludes==null || !excludes.test(ENTITY_TOPOLOGYPLANCODE) ) { |
277 |
jsonEntity.add(ENTITY_TOPOLOGYPLANCODE, entity.getTopologyPlanCode()); |
278 |
} |
279 |
if( excludes==null || !excludes.test(ENTITY_USERCODE) ) { |
280 |
jsonEntity.add(ENTITY_USERCODE, entity.getUserCode()); |
281 |
} |
282 |
if( excludes==null || !excludes.test(ENTITY_CATEGORY) ) { |
283 |
jsonEntity.add(ENTITY_CATEGORY, entity.getCategory()); |
284 |
} |
285 |
if( excludes==null || !excludes.test(ENTITY_LABEL) ) { |
286 |
jsonEntity.add(ENTITY_LABEL, entity.getLabel()); |
287 |
} |
288 |
if( excludes==null || !excludes.test(ENTITY_AUTHORIZATIONS) ) { |
289 |
jsonEntity.add(ENTITY_AUTHORIZATIONS, entity.getAuthorizations()); |
290 |
} |
291 |
if( excludes==null || !excludes.test(ENTITY_TOPOLOGYPLANMODE) ) { |
292 |
jsonEntity.add(ENTITY_TOPOLOGYPLANMODE, entity.getTopologyPlanMode()); |
293 |
} |
294 |
if( excludes==null || !excludes.test(ENTITY_RESOURCES) ) { |
295 |
jsonEntity.add(ENTITY_RESOURCES, entity.getResources()); |
296 |
} |
297 |
if( excludes==null || !excludes.test(ENTITY_DATA_MODELS) ) { |
298 |
jsonEntity.add(ENTITY_DATA_MODELS, entity.getDataModels()); |
299 |
} |
300 |
|
301 |
if( entity instanceof VCSGisWorkspaceEntity ) { |
302 |
if( excludes==null || !excludes.test(ENTITY_LOCALREVISIONCODE) ) { |
303 |
jsonEntity.add(ENTITY_LOCALREVISIONCODE, ((VCSGisWorkspaceEntity) entity).getLocalRevisionCode()); |
304 |
} |
305 |
} |
306 |
|
307 |
|
308 |
return jsonEntity;
|
309 |
} |
310 |
|
311 |
public static void fromJson(VCSGisEntityEditable entity, JsonObject jsonEntity) { |
312 |
if( jsonEntity.containsKey(ENTITY_DATATABLENAME) ) {
|
313 |
entity.setDataTableName(jsonEntity.getString(ENTITY_DATATABLENAME)); |
314 |
} |
315 |
if( jsonEntity.containsKey(ENTITY_DESCRIPTION) ) {
|
316 |
entity.setDescription(jsonEntity.getString(ENTITY_DESCRIPTION, null));
|
317 |
} |
318 |
if( jsonEntity.containsKey(ENTITY_CODE) ) {
|
319 |
entity.setEntityCode(jsonEntity.getString(ENTITY_CODE, null));
|
320 |
} |
321 |
if( jsonEntity.containsKey(ENTITY_NAME) ) {
|
322 |
entity.setEntityName(jsonEntity.getString(ENTITY_NAME, null));
|
323 |
} |
324 |
if( jsonEntity.containsKey(ENTITY_FEATUREIDFIELDNAME) ) {
|
325 |
entity.setFeatureIdFieldName(jsonEntity.getString(ENTITY_FEATUREIDFIELDNAME, null));
|
326 |
} |
327 |
if( jsonEntity.containsKey(ENTITY_FEATURETYPEASJSON) ) {
|
328 |
entity.setFeatureTypeAsJson(jsonEntity.getString(ENTITY_FEATURETYPEASJSON, null));
|
329 |
} |
330 |
if( jsonEntity.containsKey(ENTITY_FIELDFORLABEL) ) {
|
331 |
entity.setFieldForLabel(jsonEntity.getString(ENTITY_FIELDFORLABEL, null));
|
332 |
} |
333 |
if( jsonEntity.containsKey(ENTITY_GEOMETRYFIELDNAME) ) {
|
334 |
entity.setGeometryFieldName(jsonEntity.getString(ENTITY_GEOMETRYFIELDNAME, null));
|
335 |
} |
336 |
if( jsonEntity.containsKey(ENTITY_REVISIONCODE) ) {
|
337 |
entity.setRepositoryRevisionCode(jsonEntity.getString(ENTITY_REVISIONCODE, null));
|
338 |
} |
339 |
if( jsonEntity.containsKey(ENTITY_TOPOLOGYPLANCODE) ) {
|
340 |
entity.setTopologyPlanCode(jsonEntity.getString(ENTITY_TOPOLOGYPLANCODE, null));
|
341 |
} |
342 |
if( jsonEntity.containsKey(ENTITY_USERCODE) ) {
|
343 |
entity.setUserCode(jsonEntity.getString(ENTITY_USERCODE, null));
|
344 |
} |
345 |
if( jsonEntity.containsKey(ENTITY_CATEGORY) ) {
|
346 |
entity.setCategory(jsonEntity.getString(ENTITY_CATEGORY, null));
|
347 |
} |
348 |
if( jsonEntity.containsKey(ENTITY_LABEL) ) {
|
349 |
entity.setLabel(jsonEntity.getString(ENTITY_LABEL, null));
|
350 |
} |
351 |
if( jsonEntity.containsKey(ENTITY_AUTHORIZATIONS) ) {
|
352 |
entity.setAuthorizations(jsonEntity.getString(ENTITY_AUTHORIZATIONS, null));
|
353 |
} |
354 |
if( jsonEntity.containsKey(ENTITY_TOPOLOGYPLANMODE) ) {
|
355 |
entity.setTopologyPlanMode(jsonEntity.getInt(ENTITY_TOPOLOGYPLANMODE, TOPOLOGYPLAN_MODE_RECOMMENDED)); |
356 |
} |
357 |
if( jsonEntity.containsKey(ENTITY_RESOURCES) ) {
|
358 |
entity.setResources(jsonEntity.getString(ENTITY_RESOURCES, null));
|
359 |
} |
360 |
if( jsonEntity.containsKey(ENTITY_DATA_MODELS) ) {
|
361 |
entity.setDataModels(jsonEntity.getString(ENTITY_DATA_MODELS, null));
|
362 |
} |
363 |
if( entity instanceof VCSGisWorkspaceEntity ) { |
364 |
if( jsonEntity.containsKey(ENTITY_LOCALREVISIONCODE) ) {
|
365 |
((VCSGisWorkspaceEntity) entity).setLocalRevisionCode(jsonEntity.getString(ENTITY_LOCALREVISIONCODE, null));
|
366 |
} |
367 |
} |
368 |
} |
369 |
|
370 |
public static boolean isAuthorized(VCSGisEntity entity, String operation, VCSGisUser user) { |
371 |
String userid = user.getIdentifier().toLowerCase();
|
372 |
String s = entity.getAuthorizations();
|
373 |
if( StringUtils.isBlank(s) ) {
|
374 |
return true; |
375 |
} |
376 |
JsonObject authorizations = Json.createObject(s); |
377 |
if( authorizations==null ) { |
378 |
return false; |
379 |
} |
380 |
if( StringUtils.isBlank(userid) || StringUtils.isBlank(operation)) {
|
381 |
return false; |
382 |
} |
383 |
JsonArray op = authorizations.getJsonArray(operation.toLowerCase()); |
384 |
if( op == null ) { |
385 |
return false; |
386 |
} |
387 |
Iterator<JsonValue> it = op.iterator();
|
388 |
while (it.hasNext()) {
|
389 |
JsonValue value = it.next(); |
390 |
String userOrRole = ((JsonString)value).getString();
|
391 |
if(StringUtils.equalsIgnoreCase(userOrRole,"*")){ |
392 |
return true; |
393 |
} |
394 |
if(StringUtils.equalsIgnoreCase(userOrRole,userid)){
|
395 |
return true; |
396 |
} |
397 |
for (String role : user.getRolesAsList()) { |
398 |
if(StringUtils.equalsIgnoreCase(userOrRole,"$"+role)){ |
399 |
return true; |
400 |
} |
401 |
|
402 |
} |
403 |
} |
404 |
|
405 |
// if( op.contains(userid) ) {
|
406 |
// if( op.getValuesAs(String.class).toString().contains(userid) ) {
|
407 |
// return true;
|
408 |
// }
|
409 |
return false; |
410 |
} |
411 |
|
412 |
public static String getOperationLabel(int operation) { |
413 |
return OPERATION_LABELS.getOrDefault(operation, "unknown"); |
414 |
} |
415 |
public static String getStateLabel(int state) { |
416 |
return STATE_LABELS.getOrDefault(state, "unknown"); |
417 |
} |
418 |
|
419 |
@SuppressWarnings("null") |
420 |
private static Map<String,String> getSqlTemplates(String name) { |
421 |
final int SEARCHING_ITEM = 0; |
422 |
final int READING_ITEM = 1; |
423 |
|
424 |
InputStream sqls = VCSGisUtils.class.getResourceAsStream("/org/gvsig/vcsgis/lib/"+name+".sql"); // org.gvsig.vcsgis.lib |
425 |
Map<String,String> expecteds = new HashMap<>(); |
426 |
List<String> lines; |
427 |
try {
|
428 |
lines = IOUtils.readLines(sqls); |
429 |
} catch (IOException ex) { |
430 |
throw new RuntimeException("Can't read sql templates for provider "+name, ex); |
431 |
} |
432 |
|
433 |
StringBuilder sb = null; |
434 |
String currentItem = null; |
435 |
int lineno = 1; |
436 |
int state = SEARCHING_ITEM;
|
437 |
for (String line : lines) { |
438 |
line = line + "\n";
|
439 |
switch(state) {
|
440 |
case SEARCHING_ITEM:
|
441 |
if( line.toLowerCase().startsWith("@begin ") ) { |
442 |
currentItem = line.substring(7).trim();
|
443 |
sb = new StringBuilder(); |
444 |
state = READING_ITEM; |
445 |
} else if( line.toLowerCase().startsWith("-- ") ) { |
446 |
// do nothing skip comment
|
447 |
} else if( !StringUtils.isBlank(line) ) { |
448 |
throw new IllegalStateException("Syntax error at '"+name+"', line "+lineno+ "."); |
449 |
} |
450 |
break;
|
451 |
case READING_ITEM:
|
452 |
if( line.toLowerCase().startsWith("@end ") ) { |
453 |
if( !StringUtils.equals(currentItem,line.substring(5).trim()) ) { |
454 |
throw new IllegalStateException("Syntax error at '"+name+"', line "+lineno+ ", expected @end "+currentItem+", and found "+line+"."); |
455 |
} |
456 |
String s = sb.toString();
|
457 |
if( s.endsWith("\n") ) { |
458 |
s = s.substring(0, s.length()-1); |
459 |
} |
460 |
expecteds.put(currentItem, s); |
461 |
state = SEARCHING_ITEM; |
462 |
|
463 |
} else if( line.toLowerCase().startsWith("-- ") ) { |
464 |
// do nothing skip comment
|
465 |
} else {
|
466 |
sb.append(line); |
467 |
} |
468 |
break;
|
469 |
} |
470 |
lineno++; |
471 |
} |
472 |
if( state!=SEARCHING_ITEM ) {
|
473 |
throw new IllegalStateException("Syntax error at '"+name+"', expected @end "+currentItem+" and found EOF ."); |
474 |
} |
475 |
return expecteds;
|
476 |
} |
477 |
|
478 |
public static String getSqlTemplate(String providerName, String sqlId){ |
479 |
if(SQL_TEMPLATES_BY_PROVIDER.get(providerName) == null) { |
480 |
SQL_TEMPLATES_BY_PROVIDER.put(providerName, getSqlTemplates(providerName)); |
481 |
} |
482 |
return SQL_TEMPLATES_BY_PROVIDER.get(providerName).get(sqlId);
|
483 |
} |
484 |
|
485 |
public static void updateMessageDigest(MessageDigest md, Feature f, String fieldName) { |
486 |
String s = f.getString(fieldName);
|
487 |
if( s!=null ) { |
488 |
md.update(s.getBytes()); |
489 |
} |
490 |
} |
491 |
|
492 |
public static List<String> getAsList(String s) { |
493 |
if( StringUtils.isBlank(s) ) {
|
494 |
return Collections.EMPTY_LIST; |
495 |
} |
496 |
List<String> l = new ArrayList<>(); |
497 |
String[] ss = StringUtils.split(s, ","); |
498 |
for (String s1 : ss) { |
499 |
if( StringUtils.isBlank(s1) ) {
|
500 |
continue;
|
501 |
} |
502 |
l.add(s1.trim().toLowerCase()); |
503 |
} |
504 |
return l;
|
505 |
} |
506 |
|
507 |
|
508 |
} |