1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.efaps.admin;
22
23 import java.io.Serializable;
24 import java.sql.PreparedStatement;
25 import java.sql.ResultSet;
26 import java.sql.SQLException;
27 import java.sql.Statement;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.UUID;
35
36 import org.apache.commons.collections4.MapUtils;
37 import org.apache.commons.lang3.builder.ToStringBuilder;
38 import org.efaps.admin.datamodel.Type;
39 import org.efaps.admin.event.EventDefinition;
40 import org.efaps.admin.event.EventType;
41 import org.efaps.admin.event.Parameter;
42 import org.efaps.admin.event.Parameter.ParameterValues;
43 import org.efaps.admin.event.Return;
44 import org.efaps.admin.ui.AbstractUserInterfaceObject;
45 import org.efaps.db.Context;
46 import org.efaps.db.transaction.ConnectionResource;
47 import org.efaps.db.wrapper.SQLPart;
48 import org.efaps.db.wrapper.SQLSelect;
49 import org.efaps.util.EFapsException;
50 import org.efaps.util.cache.CacheObjectInterface;
51 import org.efaps.util.cache.CacheReloadException;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55
56
57
58
59 public abstract class AbstractAdminObject
60 implements CacheObjectInterface, Serializable
61 {
62
63
64
65 private static final long serialVersionUID = 1L;
66
67
68
69
70 private static final Logger LOG = LoggerFactory.getLogger(AbstractAdminObject.class);
71
72
73
74
75 private static final String SELECT = new SQLSelect()
76 .column("NAME")
77 .column("VALUE")
78 .from("T_CMPROPERTY")
79 .addPart(SQLPart.WHERE)
80 .addColumnPart(null, "ABSTRACT")
81 .addPart(SQLPart.EQUAL)
82 .addValuePart("?").toString();
83
84
85
86
87
88
89 private final long id;
90
91
92
93
94
95
96
97 private final UUID uuid;
98
99
100
101
102
103
104
105 private final String name;
106
107
108
109
110
111
112 private final Map<String, String> properties = new HashMap<String, String>();
113
114
115
116
117 private final Map<EventType, List<EventDefinition>> events = new HashMap<EventType, List<EventDefinition>>();
118
119
120
121
122 private boolean eventChecked = false;
123
124
125
126
127
128 private boolean dirty = false;
129
130
131
132
133
134
135
136
137
138
139
140
141
142 protected AbstractAdminObject(final long _id,
143 final String _uuid,
144 final String _name)
145 {
146 this.id = _id;
147 this.uuid = _uuid == null || _uuid.trim().isEmpty() ? null : UUID.fromString(_uuid.trim());
148 this.name = _name == null ? null : _name.trim();
149 }
150
151
152
153
154
155
156
157
158
159
160 protected void setLinkProperty(final UUID _linkTypeUUID,
161 final long _toId,
162 final UUID _toTypeUUID,
163 final String _toName)
164 throws EFapsException
165 {
166 setDirty();
167 }
168
169
170
171
172
173
174
175
176
177
178 protected void setProperty(final String _name,
179 final String _value)
180 throws CacheReloadException
181 {
182 getProperties().put(_name, _value);
183 setDirty();
184 }
185
186
187
188
189
190
191
192
193 public String getProperty(final String _name)
194 {
195 return getProperties().get(_name);
196 }
197
198
199
200
201 public Map<String, String> getPropertyMap()
202 {
203 return MapUtils.unmodifiableMap(getProperties());
204 }
205
206
207
208
209
210
211
212
213
214 public void addEvent(final EventType _eventtype,
215 final EventDefinition _eventdef)
216 throws CacheReloadException
217 {
218 List<EventDefinition> evenList = this.events.get(_eventtype);
219 if (evenList == null) {
220 evenList = new ArrayList<EventDefinition>();
221 this.events.put(_eventtype, evenList);
222 }
223 if (!evenList.contains(_eventdef)) {
224 evenList.add(_eventdef);
225 }
226
227
228 if (evenList.size() > 1) {
229 Collections.sort(evenList, new Comparator<EventDefinition>()
230 {
231 @Override
232 public int compare(final EventDefinition _eventDef0,
233 final EventDefinition _eventDef1)
234 {
235 return Long.valueOf(_eventDef0.getIndexPos()).compareTo(Long.valueOf(_eventDef1.getIndexPos()));
236 }
237 });
238 }
239 setDirty();
240 }
241
242
243
244
245
246
247
248 public List<EventDefinition> getEvents(final EventType _eventType)
249 {
250 if (!this.eventChecked) {
251 this.eventChecked = true;
252 try {
253 EventDefinition.addEvents(this);
254 } catch (final EFapsException e) {
255 AbstractAdminObject.LOG.error("Could not read events for Name:; {}', UUID: {}", this.name, this.uuid);
256 }
257 }
258 return this.events.get(_eventType);
259 }
260
261
262
263
264
265
266
267
268 public boolean hasEvents(final EventType _eventtype)
269 {
270 if (!this.eventChecked) {
271 this.eventChecked = true;
272 try {
273 EventDefinition.addEvents(this);
274 } catch (final EFapsException e) {
275 AbstractAdminObject.LOG.error("Could not read events for Name:; {}', UUID: {}", this.name, this.uuid);
276 }
277 }
278 return this.events.get(_eventtype) != null;
279 }
280
281
282
283
284
285
286
287
288
289
290
291 public List<Return> executeEvents(final EventType _eventtype,
292 final Object... _args)
293 throws EFapsException
294 {
295 final List<Return> ret = new ArrayList<Return>();
296 if (hasEvents(_eventtype)) {
297 final Parameter param = new Parameter();
298 if (_args != null) {
299
300 for (int i = 0; i < _args.length; i += 2) {
301 if (i + 1 < _args.length && _args[i] instanceof ParameterValues) {
302 param.put((ParameterValues) _args[i], _args[i + 1]);
303 }
304 }
305 }
306 ret.addAll(executeEvents(_eventtype, param));
307 }
308 return ret;
309 }
310
311
312
313
314
315
316
317
318
319
320 public List<Return> executeEvents(final EventType _eventtype,
321 final Parameter _param)
322 throws EFapsException
323 {
324 final List<Return> ret = new ArrayList<Return>();
325 if (hasEvents(_eventtype)) {
326 if (this instanceof AbstractUserInterfaceObject) {
327
328 _param.put(ParameterValues.UIOBJECT, this);
329 }
330
331 for (final EventDefinition evenDef : this.events.get(_eventtype)) {
332 ret.add(evenDef.execute(_param));
333 }
334 }
335 return ret;
336 }
337
338
339
340
341
342
343
344
345
346 protected void readFromDB4Properties()
347 throws CacheReloadException
348 {
349 ConnectionResource con = null;
350 try {
351 con = Context.getThreadContext().getConnectionResource();
352 final PreparedStatement stmt = con.getConnection().prepareStatement(AbstractAdminObject.SELECT);
353 stmt.setObject(1, getId());
354 final ResultSet rs = stmt.executeQuery();
355 AbstractAdminObject.LOG.debug("Reading Properties for '{}'", getName());
356 while (rs.next()) {
357 final String nameStr = rs.getString(1).trim();
358 final String value = rs.getString(2).trim();
359 setProperty(nameStr, value);
360 AbstractAdminObject.LOG.debug(" Name: '{}' - Value: '{}'", new Object[] { nameStr, value });
361 }
362 rs.close();
363 stmt.close();
364 if (con.isOpened()) {
365 con.commit();
366 }
367 } catch (final SQLException e) {
368 throw new CacheReloadException("could not read properties for " + "'" + getName() + "'", e);
369 } catch (final EFapsException e) {
370 throw new CacheReloadException("could not read properties for " + "'" + getName() + "'", e);
371 } finally {
372 if (con != null && con.isOpened()) {
373 try {
374 con.abort();
375 } catch (final EFapsException e) {
376 throw new CacheReloadException("could not read properties for " + "'" + getName() + "'", e);
377 }
378 }
379 }
380 }
381
382
383
384
385
386
387
388
389 protected void readFromDB4Links()
390 throws CacheReloadException
391 {
392 ConnectionResource con = null;
393 try {
394 con = Context.getThreadContext().getConnectionResource();
395 final SQLSelect select = new SQLSelect()
396 .column(0, "TYPEID")
397 .column(0, "TOID")
398 .column(1, "TYPEID")
399 .column(1, "NAME")
400 .from("T_CMABSTRACT2ABSTRACT", 0)
401 .leftJoin("T_CMABSTRACT", 1, "ID", 0, "TOID")
402 .addPart(SQLPart.WHERE)
403 .addColumnPart(0, "FROMID")
404 .addPart(SQLPart.EQUAL)
405 .addValuePart(getId());
406
407 final Statement stmt = con.getConnection().createStatement();
408 final ResultSet rs = stmt.executeQuery(select.getSQL());
409
410 AbstractAdminObject.LOG.debug("Reading Links for '{}'", getName());
411
412 final List<Object[]> values = new ArrayList<Object[]>();
413 while (rs.next()) {
414 final long conTypeId = rs.getLong(1);
415 final long toId = rs.getLong(2);
416 final long toTypeId = rs.getLong(3);
417 final String toName = rs.getString(4);
418 values.add(new Object[] { conTypeId, toId, toTypeId, toName.trim() });
419 }
420 rs.close();
421 stmt.close();
422 con.commit();
423
424 for (final Object[] row : values) {
425 final UUID conTypeUUID = Type.getUUID4Id((Long) row[0]);
426 AbstractAdminObject.LOG.debug(" Connection Type UUID: {}", conTypeUUID);
427 final UUID toTypeUUID = Type.getUUID4Id((Long) row[2]);
428 AbstractAdminObject.LOG.debug(" To Type UUID: {}", toTypeUUID);
429 if (conTypeUUID != null && toTypeUUID != null) {
430 setLinkProperty(conTypeUUID, (Long) row[1], toTypeUUID, String.valueOf(row[3]));
431 AbstractAdminObject.LOG.debug(" ID: {}, name: {}", row[1], row[3]);
432 }
433 }
434 } catch (final SQLException e) {
435 throw new CacheReloadException("could not read db links for " + "'" + getName() + "'", e);
436
437 } catch (final RuntimeException e) {
438
439 throw new CacheReloadException("could not read db links for " + "'" + getName() + "'", e);
440 } catch (final EFapsException e) {
441 throw new CacheReloadException("could not read properties for " + "'" + getName() + "'", e);
442 } finally {
443 if (con != null && con.isOpened()) {
444 try {
445 con.abort();
446 } catch (final EFapsException e) {
447 throw new CacheReloadException("could not read properties for " + "'" + getName() + "'", e);
448 }
449 }
450 }
451 }
452
453
454
455
456
457
458
459 @Override
460 public long getId()
461 {
462 return this.id;
463 }
464
465
466
467
468
469
470
471 @Override
472 public UUID getUUID()
473 {
474 return this.uuid;
475 }
476
477
478
479
480
481
482
483
484 @Override
485 public String getName()
486 {
487 return this.name;
488 }
489
490
491
492
493
494
495
496 protected Map<String, String> getProperties()
497 {
498 return this.properties;
499 }
500
501
502
503
504
505
506
507 protected Map<EventType, List<EventDefinition>> getEvents()
508 {
509 return this.events;
510 }
511
512
513
514
515
516
517
518 public boolean isDirty()
519 {
520 return this.dirty;
521 }
522
523
524
525
526 protected void setDirty()
527 {
528 this.dirty = true;
529 }
530
531
532
533
534 protected void setUndirty()
535 {
536 this.dirty = false;
537 }
538
539
540
541
542
543
544 @Override
545 public String toString()
546 {
547 return new ToStringBuilder(this).append("name", getName()).append("uuid", getUUID()).append("id", getId())
548 .append("properties", getProperties()).append("events", this.events).toString();
549 }
550 }