Statistics
| Revision:

gvsig-projects-pool / org.gvsig.online / trunk / org.gvsig.online / org.gvsig.online.lib / org.gvsig.online.lib.impl / src / main / java / org / gvsig / online / lib / impl / OnlineUtils.java @ 9591

History | View | Annotate | Download (22.4 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.online.lib.impl;
24

    
25
import java.io.IOException;
26
import java.io.InputStream;
27
import java.net.MalformedURLException;
28
import java.net.URL;
29
import java.nio.charset.StandardCharsets;
30
import java.security.MessageDigest;
31
import java.util.ArrayList;
32
import java.util.Collections;
33
import java.util.HashMap;
34
import java.util.List;
35
import java.util.Map;
36
import java.util.function.Predicate;
37
import javax.json.JsonObject;
38
import org.apache.commons.io.IOUtils;
39
import org.apache.commons.lang3.ArrayUtils;
40
import org.apache.commons.lang3.StringUtils;
41
import org.apache.commons.text.StringSubstitutor;
42
import org.apache.commons.text.lookup.StringLookup;
43
import org.gvsig.fmap.dal.feature.Feature;
44
import org.gvsig.json.Json;
45
import org.gvsig.json.JsonObjectBuilder;
46
import org.gvsig.online.lib.api.OnlineManager;
47
import org.gvsig.online.lib.api.workingcopy.OnlineEntity;
48
import org.gvsig.online.lib.api.workingcopy.OnlineEntityEditable;
49
import org.gvsig.tools.ToolsLocator;
50
import org.gvsig.tools.util.MapBuilder;
51
import org.slf4j.Logger;
52
import org.slf4j.LoggerFactory;
53

    
54
/**
55
 *
56
 * @author gvSIG Team
57
 */
58
@SuppressWarnings("UseSpecificCatch")
59
public class OnlineUtils {
60
    
61
    private static final Logger LOGGER = LoggerFactory.getLogger(OnlineUtils.class);
62

    
63
    
64
    public static final String ENTITY_CODE = "EntityCode";
65
    public static final String ENTITY_NAME = "EntityName";
66
    public static final String ENTITY_DESCRIPTION = "Description";
67
    public static final String ENTITY_FEATUREIDFIELDNAME = "FeatureIdFieldName";
68
    public static final String ENTITY_FEATURETYPEASJSON = "FeatureTypeAsJson";
69
    public static final String ENTITY_FIELDFORLABEL = "FieldForLabel";
70
    public static final String ENTITY_GEOMETRYFIELDNAME = "GeometryFieldName";
71
    public static final String ENTITY_LABEL = "Label";
72
    public static final String ENTITY_DATA_MODELS = "DataModels";
73
    public static final String ENTITY_CRS = "CRS";
74
    public static final String ENTITY_CUSTOMDATA = "customdata";
75
    
76
    private static final Map<Integer,String> ERRMSGS = new MapBuilder<>()
77
//            .add(ERR_NO_ERROR, "_No_errors")
78
//            .add(ERR_DBFILE_IS_NULL, "_dbfile_is_null")
79
//            .add(ERR_WSEXPLORER_IS_NULL, "_workingcopy_explorer_is_null")
80
//            .add(ERR_LABEL_IS_NULL, "_label_is_null")
81
//            .add(ERR_CANT_OPEN_WORKSPACE, "_Cant_open_workingcopy")
82
//            .add(ERR_CANT_OPEN_WORKSPACE_SERVEREXPLORER, "_Cant_open_workingcopy")
83
//            .add(ERR_STORE_NOT_IN_VERSION_CONTROL, "_Store_not_in_control_version")
84
//            .add(ERR_CANT_CREATE_TABLE, "_Cant_create_table")
85
//            .add(ERR_CANT_CREATE_TABLE_CONFIG, "_Cant_create_config_table")
86
//            .add(ERR_CANT_CREATE_TABLE_ENTITIES, "_Cant_create_table_entities")
87
//            .add(ERR_CANT_CREATE_TABLE_CHANGES, "_Cant_create_table_changes")
88
//            .add(ERR_ENTITY_ALREADY_EXISTS, "_Entity_already_exists")
89
//            .add(ERR_ENTITY_NOT_HAS_FEATURECODE, "_Entity_has_not_VCSGISCODE")
90
//            .add(ERR_CANT_OPEN_ENTITIES, "_Cant_open_entities")
91
//            .add(ERR_CANT_INSERT_ENTITIES, "_Cant_insert_entities")
92
//            .add(ERR_CANT_OPEN_CHANGES, "_Cant_open_changes")
93
//            .add(ERR_CANT_INSERT_CHANGE, "_Cant_insert_change")
94
//            .add(ERR_CANT_OPEN_STORE, "_Cant_open_store")
95
//            .add(ERR_CANT_RETRIEVE_SOURCE_FEATURES, "_Cant_retrieve_source_features")
96
//            .add(ERR_CANT_INSERT_FEATURES, "_Cant_insert_features")
97
//            .add(ERR_CANT_COMMIT, "_Cant_commit")
98
//            .add(ERR_CANT_COMMIT_WORKSPACE_OUTDATED, "_Cant_commit_workingcopy_is_out_of_date")
99
//            .add(ERR_CANT_CHECKOUT, "_Cant_checkout")
100
//            .add(ERR_CANT_UPDATE_ENTITIES, "_Cant_update_entities")
101
//            .add(ERR_CANT_PREPARE_UPDATE, "_Cant_prepare_update")
102
//            .add(ERR_CANT_UPDATE, "_Cant_update")
103
//            .add(ERR_CANT_EXECUTE_REQUEST, "_Cant_execute_request")
104
//            .add(ERR_CANT_BUILD_REQUEST_RESPONSE,"_Cant_build_request_response")
105
//            .add(ERR_CANT_HANDLE_REQUEST,"_Cant_handle_request")
106
//            .add(ERR_CANT_UPDATE_CLEAN,"_Cant_update_clean")
107
//            .add(ERR_CANT_COMMIT_WITH_FEATURE_CODE_BLANK,"_Cant_commit_with_feature_code_blank")
108
//            .add(ERR_CANT_ADD_ENTITY_WITHOUT_PRIMARY_KEY,"Cant_add_entity_without_primary_key")
109
//            .add(ERR_CANT_INITIALIZE_REPOSITORY, "_Cant_initialize_repository")
110
//            .add(ERR_INVALID_DATA, "_Data_can_be_empty")
111
//            .add(ERR_INVALID_REPOSITORY, "_Invalid_repository")
112
//            .add(ERR_INVALID_REVISION, "_Invalid_revision")
113
//            .add(ERR_INVALID_REVISION_FOR_ENTITY, "_Invalid_revision_for_entity")
114
//            .add(ERR_CANT_RETRIEVE_ENTITIES, "_Cant_retrieve_entities")
115
//            .add(ERR_CANT_RETRIEVE_USERS, "_Cant_retrieve_users")
116
//            .add(ERR_CANT_RETRIEVE_TOPOLOGYPLANS, "_Cant_retrieve_topologyplans")
117
//            .add(ERR_CANT_ADD_LAYER, "_Cant_add_layer_to_view")
118
//            .add(ERR_CANT_REMOVE_ENTITY, "_Cant_remove_entity")
119
//            .add(ERR_CANT_REMOVE_CHANGES, "_Cant_remove_change")
120
            
121
            .add(OnlineManager.ERR_CANT_AUTHENTICATE_USER, "_Cant_authenticate_user")
122
            .add(OnlineManager.ERR_INVALID_AUTHENTICATION_TOKEN, "_Invalid_authentication_token")
123
            .add(OnlineManager.ERR_USER_NOT_AUTHORIZED, "_User_not_authorized")
124
            .add(OnlineManager.ERR_AUTHENTICATION_EXPIRED,"_Authentication_expired")
125
            .add(OnlineManager.ERR_INVALID_USERCODE, "_Invalid_user_code")
126
//            .add(ERR_CANT_REVERT, "_Cant_revert")
127
//            .add(ERR_INVALID_RECORDCODE, "_Invalid_record_code")
128
//            .add(ERR_DATA_RECORD_NOT_FOUND, "_Data_record_not_found")
129
//            .add(ERR_EXCEPTION, "_Exception_during_operation")
130
//            .add(ERR_CANT_PREPARE_WORKSPACE, "_Cant_prepare_workspace")
131
//            .add(ERR_REPOSITORY_URL_NOT_DEFINED, "_Repository_url_not_defined")
132
//            .add(ERR_WORKING_COPIES_FOLDER_NOT_DEFINED, "_Working_copies_folder_not_defined")
133
//            .add(ERR_EXCEPTION, "_Exception_during_operation")
134
//            .add(ERR_CANT_COMMIT_WITH_FEATURE_CODE_BLANK, "_Cant_commit_feature_code_is_blank")
135
//            .add(ERR_CANT_COMMIT_WITH_FEATURE_DATA_BLANK, "_Cant_commit_feature_data_is_blank")
136
//            .add(ERR_OPERATION_NOT_ALLOWED_WITH_LINKEDTABLE,"_Operation_not_allowed_with_linked_table")
137
//            .add(ERR_TOO_MANY_REPOSITORIES,"_Too_many_repositories_only_one_allowed")
138
            .build();
139
    
140
    private static final Map<Integer,String> OPERATION_LABELS = new MapBuilder<Integer,String>()
141
            .add(OnlineManager.OP_DELETE, "Delete")
142
            .add(OnlineManager.OP_UPDATE, "Update")
143
            .add(OnlineManager.OP_INSERT, "Insert")
144
//            .add(OnlineManager.OP_ADD_ENTITY, "Add entity")
145
            .build();
146
    
147
    private static final Map<Integer,String> STATE_LABELS = new MapBuilder<Integer,String>()
148
        .add(OnlineManager.STATE_LOCAL_UNMODIFIED, "Unmodified")
149
        .add(OnlineManager.STATE_LOCAL_NEW, "New")
150
        .add(OnlineManager.STATE_LOCAL_MODIFIED, "Modified")
151
        .add(OnlineManager.STATE_LOCAL_OUTDATED, "Outdated")
152
        .add(OnlineManager.STATE_LOCAL_OUTDATED_AND_MODIFIED, "Outdated and modified")
153
        .add(OnlineManager.STATE_CONFLICT, "Conflict")
154
        .add(OnlineManager.STATE_DISCONNECTED, "Disconnected")
155
        .build();
156
    
157
    private static final Map<String, Map<String, Template>> SQL_TEMPLATES_BY_PROVIDER = new HashMap<>();
158
    
159
    public static String getErrorMessage(int errcode) {
160
        String msg = ERRMSGS.get(errcode);
161
        msg = ToolsLocator.getI18nManager().getTranslation(msg);
162
        return msg;
163
    }
164
    
165
    public static void copy(OnlineEntity source, OnlineEntityEditable target, Predicate<String> excludes) {
166
        if (excludes == null || !excludes.test(ENTITY_DESCRIPTION)) {
167
            target.setDescription(source.getDescription());
168
        }
169
        if (excludes == null || !excludes.test(ENTITY_CODE)) {
170
            target.setEntityCode(source.getEntityCode());
171
        }
172
        if (excludes == null || !excludes.test(ENTITY_NAME)) {
173
            target.setEntityName(source.getEntityName());
174
        }
175
        if (excludes == null || !excludes.test(ENTITY_FEATUREIDFIELDNAME)) {
176
            target.setFeatureIdFieldName(source.getFeatureIdFieldName());
177
        }
178
        if (excludes == null || !excludes.test(ENTITY_FEATURETYPEASJSON)) {
179
            target.setFeatureTypeAsJson(source.getFeatureTypeAsJson());
180
        }
181
        if (excludes == null || !excludes.test(ENTITY_FIELDFORLABEL)) {
182
            target.setFieldForLabel(source.getFieldForLabel());
183
        }
184
        if (excludes == null || !excludes.test(ENTITY_GEOMETRYFIELDNAME)) {
185
            target.setGeometryFieldName(source.getGeometryFieldName());
186
        }
187
        if (excludes == null || !excludes.test(ENTITY_LABEL)) {
188
            target.setLabel(source.getLabel());
189
        }
190
        if (excludes == null || !excludes.test(ENTITY_DATA_MODELS)) {
191
            target.setDataModels(source.getDataModels());
192
        }
193
        if (excludes == null || !excludes.test(ENTITY_CRS)) {
194
            target.setCRS(source.getCRS());
195
        }
196
    }
197

    
198
    public static JsonObjectBuilder toJsonBuilder(OnlineEntity entity, Predicate<String> excludes) {
199
        JsonObjectBuilder jsonEntity = Json.createObjectBuilder();
200
        if( excludes==null || !excludes.test(ENTITY_DESCRIPTION) ) {
201
            jsonEntity.add(ENTITY_DESCRIPTION, entity.getDescription());
202
        }
203
        if( excludes==null || !excludes.test(ENTITY_CODE) ) {
204
            jsonEntity.add(ENTITY_CODE, entity.getEntityCode());
205
        }
206
        if( excludes==null || !excludes.test(ENTITY_NAME) ) {
207
            jsonEntity.add(ENTITY_NAME, entity.getEntityName());
208
        }
209
        if( excludes==null || !excludes.test(ENTITY_FEATUREIDFIELDNAME) ) {
210
            jsonEntity.add(ENTITY_FEATUREIDFIELDNAME, entity.getFeatureIdFieldName());
211
        }
212
        if( excludes==null || !excludes.test(ENTITY_FEATURETYPEASJSON) ) {
213
            jsonEntity.add(ENTITY_FEATURETYPEASJSON, entity.getFeatureTypeAsJson());
214
        }
215
        if( excludes==null || !excludes.test(ENTITY_FIELDFORLABEL) ) {
216
            jsonEntity.add(ENTITY_FIELDFORLABEL, entity.getFieldForLabel());
217
        }
218
        if( excludes==null || !excludes.test(ENTITY_GEOMETRYFIELDNAME) ) {
219
            jsonEntity.add(ENTITY_GEOMETRYFIELDNAME, entity.getGeometryFieldName());
220
        }
221
        if( excludes==null || !excludes.test(ENTITY_LABEL) ) {
222
            jsonEntity.add(ENTITY_LABEL, entity.getLabel());
223
        }
224
        if( excludes==null || !excludes.test(ENTITY_DATA_MODELS) ) {
225
            jsonEntity.add(ENTITY_DATA_MODELS, entity.getDataModels());
226
        }
227
        
228
        if( excludes==null || !excludes.test(ENTITY_CRS) ) {
229
            if( entity.getCRS()!=null ) {
230
                jsonEntity.add(ENTITY_CRS, entity.getCRS());
231
            }
232
        }
233
        if( excludes==null || !excludes.test(ENTITY_CUSTOMDATA) ) {
234
            if( StringUtils.isNotBlank(entity.getCustomData()) ) {
235
                jsonEntity.add(ENTITY_CUSTOMDATA, entity.getCustomData());
236
            }
237
        }
238
        return jsonEntity;
239
    }
240

    
241
    public static void fromJson(OnlineEntityEditable entity, JsonObject jsonEntity) {
242
        if( jsonEntity.containsKey(ENTITY_DESCRIPTION) ) {
243
            entity.setDescription(jsonEntity.getString(ENTITY_DESCRIPTION, null));
244
        }
245
        if( jsonEntity.containsKey(ENTITY_CODE) ) {
246
            entity.setEntityCode(jsonEntity.getString(ENTITY_CODE, null));
247
        }
248
        if( jsonEntity.containsKey(ENTITY_NAME) ) {
249
            entity.setEntityName(jsonEntity.getString(ENTITY_NAME, null));
250
        }
251
        if( jsonEntity.containsKey(ENTITY_FEATUREIDFIELDNAME) ) {
252
            entity.setFeatureIdFieldName(jsonEntity.getString(ENTITY_FEATUREIDFIELDNAME, null));
253
        }
254
        if( jsonEntity.containsKey(ENTITY_FEATURETYPEASJSON) ) {
255
            entity.setFeatureTypeAsJson(jsonEntity.getString(ENTITY_FEATURETYPEASJSON, null));
256
        }
257
        if( jsonEntity.containsKey(ENTITY_FIELDFORLABEL) ) {
258
            entity.setFieldForLabel(jsonEntity.getString(ENTITY_FIELDFORLABEL, null));
259
        }
260
        if( jsonEntity.containsKey(ENTITY_GEOMETRYFIELDNAME) ) {
261
            entity.setGeometryFieldName(jsonEntity.getString(ENTITY_GEOMETRYFIELDNAME, null));
262
        }
263
        if( jsonEntity.containsKey(ENTITY_LABEL) ) {
264
            entity.setLabel(jsonEntity.getString(ENTITY_LABEL, null));                
265
        }
266
        if( jsonEntity.containsKey(ENTITY_DATA_MODELS) ) {
267
            entity.setDataModels(jsonEntity.getString(ENTITY_DATA_MODELS, null));                
268
        }
269
        if( jsonEntity.containsKey(ENTITY_CRS) ) {
270
            entity.setCRS(jsonEntity.getString(ENTITY_CRS, null));                
271
        }
272
        if( jsonEntity.containsKey(ENTITY_CUSTOMDATA) ) {
273
            entity.setCustomData(jsonEntity.getString(ENTITY_CUSTOMDATA, null));                
274
        }
275
    }
276

    
277

    
278
    public static String getOperationLabel(int operation) {
279
        return OPERATION_LABELS.getOrDefault(operation, "unknown");
280
    }
281

    
282
    public static String getStateLabel(int state) {
283
        return STATE_LABELS.getOrDefault(state, "unknown");
284
    }
285
    
286
    private static class Template {
287
        List<String> parameters;
288
        Map<String,String> constants;
289
        String code;
290
        String name;
291
        
292
        public Template(String name) {
293
            this.name = name;
294
            this.parameters = new ArrayList<>();
295
            this.constants = new HashMap<>();
296
            this.code = null;
297
        }
298
        
299
        public void setCode(String code) {
300
            this.code = code;
301
        }
302
        
303
        public void addParameter(String parameter) {
304
            this.parameters.add(parameter);
305
        }
306
        
307
        public void addConstant(String name, String value) {
308
            this.constants.put(name, value);
309
        }
310
        
311
        public String getName() {
312
            return this.name;
313
        }
314
        
315
        public String getCode(Object...params) {
316
            if( this.parameters.isEmpty() ) {
317
                return String.format(this.code, params);
318
            }
319
            String s = this.code;
320
            for (int i = 0; i < parameters.size(); i++) {
321
                s = s.replace("%"+parameters.get(i)+"$", "%"+(i+1)+"$");
322
            }
323
            for (Map.Entry<String, String> entry : constants.entrySet()) {
324
                String cname = entry.getKey();
325
                String cvalue = entry.getValue();
326
                s = s.replace("%"+cname+"$s", cvalue);
327
            }
328
            if( ArrayUtils.isEmpty(params) ) {
329
                return s;
330
            }
331
            s = String.format(s, params);
332
            return s;
333
        }
334

    
335
        @Override
336
        public String toString() {
337
            return this.name;
338
        }
339
        
340
    }
341
    
342
    @SuppressWarnings("null")
343
    private static Map<String,Template> getSqlTemplates(String name) {
344
      final int SEARCHING_ITEM = 0;
345
      final int READING_ITEM = 1;
346
      
347
      InputStream sqls = OnlineUtils.class.getResourceAsStream("/org/gvsig/online/lib/"+name+".sql"); 
348
      Map<String,Template> expecteds = new HashMap<>();
349
      List<String> lines;
350
        try {
351
            lines = IOUtils.readLines(sqls);
352
        } catch (IOException ex) {
353
            throw new RuntimeException("Can't read sql templates for provider "+name, ex);
354
        }
355

    
356
      StringBuilder sb = null;
357
      Template currentItem = null;
358
      int lineno = 1;
359
      int state = SEARCHING_ITEM;
360
      for (String line : lines) {
361
          line = line + "\n";
362
          switch(state) {
363
            case SEARCHING_ITEM:
364
                if( line.toLowerCase().startsWith("@begin ") ) {
365
                    currentItem = new Template(line.substring(7).trim());
366
                    sb = new StringBuilder();
367
                    state = READING_ITEM;
368
                } else if( line.toLowerCase().startsWith("-- ") || line.toLowerCase().startsWith("@rem") ) {
369
                    // do nothing skip comment
370
                } else if( !StringUtils.isBlank(line) ) {
371
                    throw new IllegalStateException("Syntax error at '"+name+"', line "+lineno+ ".");
372
                }
373
                break;
374
            case READING_ITEM:
375
                if( line.toLowerCase().startsWith("@end ") ) {
376
                    String found = line.substring(5).trim();
377
                    if( !StringUtils.equals(currentItem.getName(),found) ) {
378
                      throw new IllegalStateException("Syntax error at '"+name+"', line "+lineno+ ", expected @end "+currentItem.getName()+", and found "+line+".");
379
                    }
380
                    String s = sb.toString();
381
                    if( s.endsWith("\n") ) {
382
                        s = s.substring(0, s.length()-1);
383
                    }
384
                    currentItem.setCode(s);
385
                    expecteds.put(currentItem.getName(), currentItem);
386
                    state = SEARCHING_ITEM;
387
                    
388
                } else if( line.toLowerCase().startsWith("-- ") || line.toLowerCase().startsWith("@rem") ) {
389
                    // do nothing skip comment
390
                } else if( line.toLowerCase().startsWith("@param ") ) {
391
                    currentItem.addParameter(line.substring(7).trim());
392
                } else if( line.toLowerCase().startsWith("@const ") ) {
393
                    String s = line.substring(7).trim();
394
                    String[] ss = StringUtils.split(s, "=");
395
                    currentItem.addConstant(ss[0].trim(),ss[1].trim());
396
                } else {
397
                    sb.append(line);
398
                }
399
                break;
400
        }
401
        lineno++;
402
      }
403
      if( state!=SEARCHING_ITEM ) {
404
          throw new IllegalStateException("Syntax error at '"+name+"', expected @end "+currentItem.getName()+" and found EOF .");
405
      }
406
      return expecteds;
407
    }
408
    
409
    public static String getSqlTemplate(String providerName, String sqlId, Object... args){
410
        if(SQL_TEMPLATES_BY_PROVIDER.get(providerName) == null) {
411
            SQL_TEMPLATES_BY_PROVIDER.put(providerName, getSqlTemplates(providerName));
412
        }
413
        Template template = SQL_TEMPLATES_BY_PROVIDER.get(providerName).get(sqlId);
414
        if( template==null ) {
415
            return null;
416
        }
417
        return template.getCode(args);
418
    }
419
    
420
    public static void updateMessageDigest(MessageDigest md, Feature f, String fieldName) {
421
        String s = f.getString(fieldName);
422
        if( s!=null ) {
423
            md.update(s.getBytes(StandardCharsets.UTF_8));
424
        }
425
    }
426
    
427
    public static String getFirst(String s) {
428
        if( StringUtils.isBlank(s) ) {
429
            return null;
430
        }
431
        try{
432
            String[] ss = StringUtils.split(s, ",");
433
            if( ss.length<1 ) {
434
                return null;
435
            }
436
            return ss[0];
437
        } catch (Exception ex) {
438
            return null;
439
        }
440
    }
441
    
442
    public static List<String> getAsList(String s) {
443
        return getAsList(s, true);
444
    }
445
    
446
    public static List<String> getAsList(String s, boolean toLowerCase) {
447
        if( StringUtils.isBlank(s) ) {
448
            return Collections.EMPTY_LIST;
449
        }
450
        try{
451
            List<String> l = new ArrayList<>();
452
            String[] ss = StringUtils.split(s, ",");
453
            for (String s1 : ss) {
454
                if (StringUtils.isBlank(s1)) {
455
                    continue;
456
                }
457
                if (toLowerCase) {
458
                    l.add(s1.trim().toLowerCase());
459
                } else {
460
                    l.add(s1.trim());
461
                }
462
            }
463
            return l;
464
        } catch (Exception ex) {
465
            return Collections.EMPTY_LIST;
466
        }
467
    }
468

    
469
    public static String getLabelOrName(OnlineEntity entity) {
470
        String label = entity.getLabel();
471
        if (StringUtils.isBlank(label)) {
472
            label = entity.getEntityName();
473
        }
474
        return label;
475
    }
476

    
477
    public static String getFormatedLabel(OnlineEntity entity, String template) {
478
        try {
479
            StringSubstitutor sub = new StringSubstitutor(new StringLookup() {
480
                @Override
481
                public String lookup(String key) {
482
                    if (StringUtils.equalsIgnoreCase("label", key)) {
483
                        return entity.getLabel();
484
                    }
485
                    if (StringUtils.equalsIgnoreCase("name", key)) {
486
                        return entity.getEntityName();
487
                    }
488
                    if (StringUtils.equalsIgnoreCase("code", key)) {
489
                        return entity.getEntityCode();
490
                    }
491
                    if (StringUtils.equalsIgnoreCase("datamodels", key)) {
492
                        return entity.getDataModels();
493
                    }
494
                    if (StringUtils.equalsIgnoreCase("description", key)) {
495
                        return entity.getDescription();
496
                    }
497
                    return "";
498
                }
499
            });
500
            String s = sub.replace(template);
501
            if( StringUtils.isBlank(s) ) {
502
                s = getLabelOrName(entity);
503
            }
504
            return s;
505
        } catch (Throwable th) {
506
            return getLabelOrName(entity);
507
        }
508
    }
509

    
510
    public static URL getURL(URL base, String s) {
511
        return getURL(base.toString(),s);
512
    }
513
    
514
    public static URL getURL(String base, String s) {
515
        try {
516
            String base_s = StringUtils.removeEnd(base.toString(), "/");
517
            if( StringUtils.startsWith(s, "/") ) {
518
                return new URL(base_s+s);
519
            }
520
            return new URL(base_s+"/"+s);
521
        } catch (MalformedURLException ex) {
522
            throw new RuntimeException("Can't get URL",ex);
523
        }
524
    }
525

    
526
    
527
}