1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.efaps.update.schema.dbproperty;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.MalformedURLException;
27 import java.net.URL;
28 import java.util.ArrayList;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Map.Entry;
33 import java.util.Properties;
34 import java.util.Set;
35
36 import org.apache.commons.jexl2.JexlContext;
37 import org.apache.commons.lang3.BooleanUtils;
38 import org.efaps.admin.datamodel.Type;
39 import org.efaps.ci.CIAdmin;
40 import org.efaps.db.Insert;
41 import org.efaps.db.Instance;
42 import org.efaps.db.InstanceQuery;
43 import org.efaps.db.QueryBuilder;
44 import org.efaps.db.Update;
45 import org.efaps.update.IUpdate;
46 import org.efaps.update.Profile;
47 import org.efaps.update.UpdateLifecycle;
48 import org.efaps.update.util.InstallationException;
49 import org.efaps.util.EFapsException;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53
54
55
56
57
58
59
60
61
62
63
64 public class DBPropertiesUpdate
65 implements IUpdate
66 {
67
68
69
70 private static final String TYPE_PROPERTIES = "Admin_Common_DBProperties";
71
72
73
74
75 private static final String TYPE_PROPERTIES_BUNDLE = "Admin_Common_DBPropertiesBundle";
76
77
78
79
80 private static final String TYPE_PROPERTIES_LOCAL = "Admin_Common_DBPropertiesLocal";
81
82
83
84
85 private static final Logger LOG = LoggerFactory.getLogger(DBPropertiesUpdate.class);
86
87
88
89
90 private String bundlename;
91
92
93
94
95 private String bundeluuid;
96
97
98
99
100 private long bundleid;
101
102
103
104
105 private String bundlesequence;
106
107
108
109
110 private boolean cacheOnStart;
111
112
113
114
115 private final String root;
116
117
118
119
120 private final List<Resource> resources = new ArrayList<Resource>();
121
122
123
124
125 private String fileApplication;
126
127
128
129
130
131 private Resource curResource;
132
133
134
135
136 private final URL url;
137
138
139
140
141 public DBPropertiesUpdate(final URL _url)
142 {
143 this.url = _url;
144 final String urlStr = _url.toString();
145 this.root = urlStr.substring(0, urlStr.lastIndexOf(File.separator) + 1);
146 }
147
148
149
150
151
152
153 private Long getLanguageId(final String _language)
154 {
155 Long ret = null;
156 try {
157 final QueryBuilder queryBldr = new QueryBuilder(CIAdmin.Language);
158 queryBldr.addWhereAttrEqValue(CIAdmin.Language.Language, _language);
159 final InstanceQuery query = queryBldr.getQuery();
160 query.executeWithoutAccessCheck();
161 if (query.next()) {
162 ret = query.getCurrentValue().getId();
163 } else {
164 ret = insertNewLanguage(_language);
165 }
166 } catch (final EFapsException e) {
167 DBPropertiesUpdate.LOG.error("getLanguageId()", e);
168 }
169 return ret;
170 }
171
172
173
174
175
176
177
178 private long insertNewLanguage(final String _language)
179 {
180 Long ret = null;
181 try {
182 final Insert insert = new Insert(CIAdmin.Language);
183 insert.add(CIAdmin.Language.Language, _language);
184 insert.executeWithoutAccessCheck();
185 ret = insert.getId();
186 insert.close();
187 } catch (final EFapsException e) {
188 DBPropertiesUpdate.LOG.error("insertNewLanguage()", e);
189 }
190 return ret;
191 }
192
193
194
195
196
197
198 private long insertNewBundle()
199 {
200 Long ret = null;
201 try {
202 final Insert insert = new Insert(DBPropertiesUpdate.TYPE_PROPERTIES_BUNDLE);
203 insert.add("Name", this.bundlename);
204 insert.add("UUID", this.bundeluuid);
205 insert.add("Sequence", this.bundlesequence);
206 insert.add("CacheOnStart", this.cacheOnStart);
207 insert.executeWithoutAccessCheck();
208
209 ret = insert.getId();
210 insert.close();
211
212 } catch (final EFapsException e) {
213 DBPropertiesUpdate.LOG.error("insertNewBundle()", e);
214 }
215 return ret;
216 }
217
218
219
220
221
222
223
224
225 private void importFromProperties(final URL _url)
226 {
227 try {
228 final InputStream propInFile = _url.openStream();
229 final Properties props = new Properties();
230 props.load(propInFile);
231 final Iterator<Entry<Object, Object>> iter = props.entrySet().iterator();
232
233 while (iter.hasNext()) {
234 final Entry<Object, Object> element = iter.next();
235 final Instance existing = getExistingKey(element.getKey().toString());
236 if (existing == null) {
237 insertNewProp(element.getKey().toString(), element.getValue().toString());
238 } else {
239 updateDefault(existing, element.getValue().toString());
240 }
241 }
242
243 } catch (final IOException e) {
244 DBPropertiesUpdate.LOG.error("ImportFromProperties() - I/O failed.", e);
245 }
246 }
247
248
249
250
251
252
253
254
255
256 private void importFromProperties(final URL _url,
257 final String _language)
258 {
259 try {
260 final InputStream propInFile = _url.openStream();
261 final Properties props = new Properties();
262 props.load(propInFile);
263 final Iterator<Entry<Object, Object>> iter = props.entrySet().iterator();
264
265 while (iter.hasNext()) {
266 final Entry<Object, Object> element = iter.next();
267 Instance propInstance = getExistingKey(element.getKey().toString());
268 if (propInstance == null || !propInstance.isValid()) {
269 propInstance = insertNewProp(element.getKey().toString(), element.getValue().toString());
270 }
271
272 final Instance localInstance = getExistingLocale(propInstance.getId(), _language);
273 if (localInstance == null || !localInstance.isValid()) {
274 insertNewLocal(propInstance.getId(), element.getValue().toString(), _language);
275 } else {
276 updateLocale(localInstance, element.getValue().toString());
277 }
278 }
279
280 } catch (final IOException e) {
281 DBPropertiesUpdate.LOG.error("ImportFromProperties() - I/O failed.", e);
282 }
283 }
284
285
286
287
288
289
290
291
292 private Instance getExistingLocale(final long _propertyid,
293 final String _language)
294 {
295 Instance ret = null;
296 try {
297 final QueryBuilder queryBldr = new QueryBuilder(Type.get(DBPropertiesUpdate.TYPE_PROPERTIES_LOCAL));
298 queryBldr.addWhereAttrEqValue("PropertyID", _propertyid);
299 queryBldr.addWhereAttrEqValue("LanguageID", getLanguageId(_language));
300 final InstanceQuery query = queryBldr.getQuery();
301 query.executeWithoutAccessCheck();
302 if (query.next()) {
303 ret = query.getCurrentValue();
304 }
305 } catch (final EFapsException e) {
306 DBPropertiesUpdate.LOG.error("getExistingLocale(String)", e);
307 }
308 return ret;
309 }
310
311
312
313
314
315
316
317
318 private void insertNewLocal(final long _propertyid,
319 final String _value,
320 final String _language)
321 {
322 try {
323 final Insert insert = new Insert(DBPropertiesUpdate.TYPE_PROPERTIES_LOCAL);
324 insert.add("Value", _value);
325 insert.add("PropertyID", _propertyid);
326 insert.add("LanguageID", getLanguageId(_language));
327 insert.executeWithoutAccessCheck();
328 insert.close();
329 } catch (final EFapsException e) {
330 DBPropertiesUpdate.LOG.error("insertNewLocal(String)", e);
331 }
332 }
333
334
335
336
337
338
339
340 private void updateLocale(final Instance _localeInst,
341 final String _value)
342 {
343 try {
344 final Update update = new Update(_localeInst);
345 update.add("Value", _value);
346 update.execute();
347 } catch (final EFapsException e) {
348 DBPropertiesUpdate.LOG.error("updateLocale(String, String)", e);
349 }
350 }
351
352
353
354
355
356
357
358 private Instance getExistingKey(final String _key)
359 {
360 Instance ret = null;
361 try {
362 final QueryBuilder queryBldr = new QueryBuilder(Type.get(DBPropertiesUpdate.TYPE_PROPERTIES));
363 queryBldr.addWhereAttrEqValue("Key", _key);
364 queryBldr.addWhereAttrEqValue("BundleID", this.bundleid);
365 final InstanceQuery query = queryBldr.getQuery();
366 query.executeWithoutAccessCheck();
367 if (query.next()) {
368 ret = query.getCurrentValue();
369 }
370 } catch (final EFapsException e) {
371 DBPropertiesUpdate.LOG.error("getExisting()", e);
372 }
373 return ret;
374 }
375
376
377
378
379
380
381
382 private void updateDefault(final Instance _inst,
383 final String _value)
384 {
385 try {
386 final Update update = new Update(_inst);
387 update.add("Default", _value);
388 update.execute();
389 } catch (final EFapsException e) {
390 DBPropertiesUpdate.LOG.error("updateDefault(String, String)", e);
391 }
392 }
393
394
395
396
397
398
399
400
401 private Instance insertNewProp(final String _key,
402 final String _value)
403 {
404 Instance ret = null;
405 try {
406 final Insert insert = new Insert(DBPropertiesUpdate.TYPE_PROPERTIES);
407 insert.add("BundleID", this.bundleid);
408 insert.add("Key", _key);
409 insert.add("Default", _value);
410 insert.executeWithoutAccessCheck();
411 ret = insert.getInstance();
412 insert.close();
413
414 } catch (final EFapsException e) {
415 DBPropertiesUpdate.LOG.error("InsertNew(String, String)", e);
416 }
417 return ret;
418 }
419
420
421
422
423
424
425
426 private Long getExistingBundle(final String _uuid)
427 {
428 Long ret = null;
429 try {
430 final QueryBuilder queryBldr = new QueryBuilder(Type.get(DBPropertiesUpdate.TYPE_PROPERTIES_BUNDLE));
431 queryBldr.addWhereAttrEqValue("UUID", _uuid);
432 final InstanceQuery query = queryBldr.getQuery();
433 query.executeWithoutAccessCheck();
434 if (query.next()) {
435 ret = query.getCurrentValue().getId();
436 }
437 } catch (final EFapsException e) {
438 DBPropertiesUpdate.LOG.error("getExistingBundle(String)", e);
439 }
440 return ret;
441 }
442
443
444
445
446 @Override
447 public String getFileApplication()
448 {
449 return this.fileApplication;
450 }
451
452
453
454
455 @Override
456 public void updateInDB(final JexlContext _jexlContext,
457 final UpdateLifecycle _step,
458 final Set<Profile> _profile)
459 throws InstallationException
460 {
461 if (_step == UpdateLifecycle.DBPROPERTIES_UPDATE) {
462
463 if (DBPropertiesUpdate.LOG.isInfoEnabled()) {
464 DBPropertiesUpdate.LOG.info("Importing Properties '" + this.bundlename + "'");
465 }
466
467 final Long bundleID = getExistingBundle(this.bundeluuid);
468
469 if (bundleID == null) {
470 this.bundleid = insertNewBundle();
471 } else {
472 this.bundleid = bundleID;
473 }
474 try {
475 for (final Resource resource : this.resources) {
476 if ("Properties".equals(resource.type)) {
477 if (resource.language == null || resource.language.length() < 1) {
478 importFromProperties(new URL(this.root + resource.filename));
479 } else {
480 importFromProperties(new URL(this.root + resource.filename), resource.language);
481 }
482 }
483 }
484 } catch (final MalformedURLException e) {
485 DBPropertiesUpdate.LOG.error("The URL given for one File of the DBProperties is invalid", e);
486 }
487 }
488 }
489
490
491
492
493 @Override
494 public void readXML(final List<String> _tags,
495 final Map<String, String> _attributes,
496 final String _text)
497 {
498 final String value = _tags.get(0);
499
500 if ("uuid".equals(value)) {
501 this.bundeluuid = _text;
502 } else if ("file-application".equals(value)) {
503 this.fileApplication = _text;
504 } else if ("bundle".equals(value)) {
505 this.bundlename = _attributes.get("name");
506 this.bundlesequence = _attributes.get("sequence");
507 this.cacheOnStart = BooleanUtils.toBoolean(_attributes.get("cacheOnStart"));
508 } else if ("resource".equals(value)) {
509 if (_tags.size() == 1) {
510 this.curResource = new Resource();
511 this.resources.add(this.curResource);
512 } else {
513 this.curResource.readXML(_tags.subList(1, _tags.size()), _attributes, _text);
514 }
515 }
516 }
517
518
519
520
521 @Override
522 public URL getURL()
523 {
524 return this.url;
525 }
526
527
528
529
530 public static class Resource
531 {
532
533
534
535
536 private String type;
537
538
539
540
541 private String language;
542
543
544
545
546 private String filename;
547
548
549
550
551
552
553
554
555 public void readXML(final List<String> _tags,
556 final Map<String, String> _attributes,
557 final String _text)
558 {
559 final String value = _tags.get(0);
560 if ("type".equals(value)) {
561 this.type = _text;
562 } else if ("language".equals(value)) {
563 this.language = _text;
564 } else if ("file".equals(value)) {
565 this.filename = _text;
566 }
567 }
568 }
569 }