Mercurial > hg > openmind
annotate src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java @ 58:153a0232270b
cleanup and better comments.
author | casties |
---|---|
date | Fri, 09 Dec 2016 21:19:21 +0100 |
parents | e08f3dd127a5 |
children | c11fd8ca5c0a |
rev | line source |
---|---|
1 | 1 package org.mpi.openmind.repository.services; |
2 | |
3 import java.text.DecimalFormat; | |
4 import java.util.ArrayList; | |
5 import java.util.List; | |
6 import java.util.Map; | |
7 | |
8 import org.apache.commons.lang.StringUtils; | |
9 import org.apache.log4j.Logger; | |
10 import org.hibernate.Query; | |
11 import org.hibernate.Session; | |
12 import org.mpi.openmind.configuration.ConfigurationService; | |
13 import org.mpi.openmind.repository.bo.Attribute; | |
14 import org.mpi.openmind.repository.bo.Entity; | |
15 import org.mpi.openmind.repository.bo.Node; | |
16 import org.mpi.openmind.repository.bo.Relation; | |
17 import org.mpi.openmind.repository.bo.View; | |
18 import org.mpi.openmind.repository.bo.utils.Sequence; | |
19 import org.mpi.openmind.repository.utils.HibernateUtil; | |
20 import org.mpi.openmind.repository.utils.OwnValueGenerator; | |
21 | |
22 /** | |
23 * | |
24 * @author jurzua | |
25 */ | |
26 public abstract class AbstractPersistenceService { | |
27 | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
28 private ConfigurationService configurationService; |
1 | 29 |
30 private OwnValueGenerator ownValueGenerator; | |
31 | |
32 private final static String NODE_SEQUENCE = "nodeSequence"; | |
33 | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
34 private static Logger logger = Logger.getLogger(AbstractPersistenceService.class); |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
35 |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
36 public static final String TRANSACTION_LOGGER = "openmind.transactionlog"; |
1 | 37 |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
38 private static Logger txLog = Logger.getLogger(TRANSACTION_LOGGER); |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
39 |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
40 private boolean importMode = false; |
1 | 41 |
42 public AbstractPersistenceService() { | |
43 } | |
44 | |
45 public String generateOwnValue(Entity entity) { | |
46 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
47 String ownValue = null; | |
48 | |
49 try { | |
50 session.getTransaction().begin(); | |
51 ownValue = this.ownValueGenerator.generateOwnValue(entity, session); | |
52 } catch (Exception e) { | |
53 logger.error(e.getMessage(), e); | |
54 // e.printStackTrace(); | |
55 } finally { | |
56 session.getTransaction().commit(); | |
57 } | |
58 | |
59 return ownValue; | |
60 } | |
61 | |
62 public int dropAssertions() { | |
63 int row = 0; | |
64 try { | |
65 Session session = HibernateUtil.getSessionFactory() | |
66 .getCurrentSession(); | |
67 session.beginTransaction(); | |
68 String sql = "DELETE FROM Node WHERE type = 'ABox'"; | |
69 Query query = session.createQuery(sql); | |
70 row = query.executeUpdate(); | |
71 | |
72 logger.info("Drop assertions - rows deleted= " + row); | |
73 | |
74 session.getTransaction().commit(); | |
75 } catch (Exception e) { | |
76 logger.error(e.getMessage(), e); | |
77 // e.printStackTrace(); | |
78 } | |
79 return row; | |
80 } | |
81 | |
82 public int dropDefinitions() { | |
83 int row = 0; | |
84 try { | |
85 Session session = HibernateUtil.getSessionFactory() | |
86 .getCurrentSession(); | |
87 session.beginTransaction(); | |
88 String sql = "DELETE FROM Node WHERE type = 'TBox'"; | |
89 Query query = session.createQuery(sql); | |
90 row = query.executeUpdate(); | |
91 | |
92 logger.info("Drop definitions - rows deleted= " + row); | |
93 | |
94 session.getTransaction().commit(); | |
95 } catch (Exception e) { | |
96 logger.error(e.getMessage(), e); | |
97 // e.printStackTrace(); | |
98 } | |
99 return row; | |
100 } | |
101 | |
102 public void saveOrUpdateObject(Object obj) { | |
103 try { | |
104 Session session = HibernateUtil.getSessionFactory() | |
105 .getCurrentSession(); | |
106 session.getTransaction().begin(); | |
107 session.saveOrUpdate(obj); | |
108 session.getTransaction().commit(); | |
109 } catch (Exception e) { | |
110 logger.error(e.getMessage(), e); | |
111 } | |
112 } | |
113 | |
114 private void saveNode0(Session session, Node node, Sequence idSequence){ | |
115 node.autoNormalize(); | |
116 node.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); | |
117 | |
118 if(node.getModificationTime() == null){ | |
119 node.setModificationTime(System.currentTimeMillis()); | |
120 } | |
121 if(node.getVersion() == null){ | |
122 node.increaseVersion(); | |
123 } | |
124 | |
125 if (node.getId() == null){ | |
126 node.setId(idSequence.generateId()); | |
127 } | |
128 | |
129 if (node.getRowId() == null){ | |
34
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
130 logger.debug("Save node: "+node.toString()); |
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
131 txLog.info("save node: "+node.toEncString()); |
1 | 132 session.save(node); |
133 }else{ | |
34
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
134 txLog.info("merge node: "+node.toEncString()); |
1 | 135 session.merge(node); |
136 } | |
137 | |
138 logger.info("Saved node\t" + node); | |
139 } | |
140 | |
141 public void saveNode(Node node) throws Exception { | |
142 | |
143 logger.info("Saving node\t" + node); | |
144 | |
145 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
146 session.getTransaction().begin(); | |
147 | |
148 Sequence idSequence = this.getIdSequence(session); | |
149 | |
150 this.saveNode0(session, node, idSequence); | |
151 | |
152 session.save(idSequence); | |
153 | |
154 session.getTransaction().commit(); | |
155 logger.info("Node saved\t" + node); | |
156 } | |
157 | |
158 public void saveNodeList(List<Node> nodeList) throws Exception{ | |
159 | |
160 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
161 session.getTransaction().begin(); | |
162 | |
163 Sequence idSequence = this.getIdSequence(session); | |
164 /* | |
165 long start = System.currentTimeMillis(); | |
166 DecimalFormat df = new DecimalFormat("#.##"); | |
167 int counter = 0; | |
168 */ | |
169 logger.info("##### Saving Node List: " + nodeList.size() + " #####"); | |
170 | |
171 for (Node node : nodeList) { | |
172 this.saveNode0(session, node, idSequence); | |
173 /* | |
174 double percent = ((double) counter / (double) nodeList.size()) * 100.0; | |
175 long diff = System.currentTimeMillis() - start; | |
176 double min = (double) diff / (double) (60 * 1000); | |
177 | |
178 if ((percent % 10) < 0.005) { | |
179 logger.debug("\n[" + df.format(percent) + " %] counter: " | |
180 + counter + " / " + nodeList.size()); | |
181 | |
182 logger.debug("Tempo: " + (double) counter / min); | |
183 } | |
184 counter++; | |
185 */ | |
186 } | |
187 | |
188 session.save(idSequence); | |
189 session.getTransaction().commit(); | |
190 } | |
191 | |
192 /** | |
193 * <p> | |
194 * This method delete the nodes and its history. | |
195 * </p> | |
196 * | |
197 * @param nodeList | |
198 */ | |
199 public void removeNode(Node node) { | |
200 try { | |
201 Session session = HibernateUtil.getSessionFactory() | |
202 .getCurrentSession(); | |
203 session.getTransaction().begin(); | |
204 | |
205 this.removeNodesById(node.getId(), session); | |
206 | |
207 session.getTransaction().commit(); | |
208 } catch (Exception e) { | |
209 logger.error(e.getMessage(), e); | |
210 // e.printStackTrace(); | |
211 } | |
212 } | |
213 | |
214 /** | |
215 * <p> | |
216 * This method delete the nodes and its history. | |
217 * </p> | |
218 * | |
219 * @param nodeList | |
220 */ | |
221 public void removeNodeList(List<Node> nodeList) { | |
222 try { | |
223 Session session = HibernateUtil.getSessionFactory() | |
224 .getCurrentSession(); | |
225 session.getTransaction().begin(); | |
226 | |
227 logger.debug("##### Deleting Node List: " + nodeList.size() | |
228 + " #####"); | |
229 | |
230 for (Node node : nodeList) { | |
231 this.removeNodesById(node.getId(), session); | |
232 } | |
233 | |
234 session.getTransaction().commit(); | |
235 } catch (Exception e) { | |
236 logger.error(e.getMessage(), e); | |
237 // e.printStackTrace(); | |
238 } | |
239 } | |
240 | |
241 /** | |
242 * Save every part of the entity but not generate new versions nor ownValue | |
243 * | |
244 * @param nodeList | |
245 */ | |
246 public void saveEntityListAsNode(List<Entity> entList) { | |
247 int entitiesSize = 0; | |
248 int nodeSize = 0; | |
249 long start = System.currentTimeMillis(); | |
250 try { | |
251 Session session = HibernateUtil.getSessionFactory() | |
252 .getCurrentSession(); | |
253 session.getTransaction().begin(); | |
254 | |
255 Sequence idSequence = this.getIdSequence(session); | |
256 | |
257 DecimalFormat df = new DecimalFormat("#.##"); | |
258 int counter = 0; | |
259 | |
260 List<Node> nodeList = generateNodeList(entList); | |
261 logger.debug("##### Saving Node List #####"); | |
262 logger.debug("Entities: " + entList.size() + " #####"); | |
263 logger.debug("Nodes: " + nodeList.size() + " #####"); | |
264 | |
265 entitiesSize = entList.size(); | |
266 nodeSize = nodeList.size(); | |
267 | |
268 for (Node node : nodeList) { | |
269 | |
270 node.autoNormalize(); | |
271 | |
272 if (node.getId() == null) | |
273 node.setId(idSequence.generateId()); | |
34
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
274 if (node.getRowId() == null) { |
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
275 logger.debug("Save node from EntityListAsNode: "+node); |
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
276 txLog.info("save node: "+node.toEncString()); |
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
277 session.save(node); |
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
278 } else { |
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
279 txLog.info("merge node: "+node.toEncString()); |
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
280 session.merge(node); |
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
281 |
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
282 } |
1 | 283 |
284 double percent = ((double) counter / (double) nodeList.size()) * 100.0; | |
285 long diff = System.currentTimeMillis() - start; | |
286 double min = (double) diff / (double) (60 * 1000); | |
287 | |
288 if ((percent % 10) < 0.005) { | |
289 logger.debug("\n[" + df.format(percent) + " %] counter: " | |
290 + counter + " / " + nodeList.size()); | |
291 | |
292 logger.debug("Tempo: " + (double) counter / min); | |
293 } | |
294 counter++; | |
295 } | |
296 | |
297 session.save(idSequence); | |
298 session.getTransaction().commit(); | |
299 | |
300 StringBuilder sb = new StringBuilder(); | |
301 sb.append("\n\t#### saveEntityListAsNode ####\n"); | |
302 sb.append("\tentitiesSize=\t" + entitiesSize + "\n"); | |
303 sb.append("\tnodeSize=\t" + nodeSize + "\n"); | |
304 sb.append("\ttime[ms]=\t" + (System.currentTimeMillis() - start) | |
305 + "\n"); | |
306 logger.info(sb.toString()); | |
307 | |
308 } catch (Exception e) { | |
309 logger.error(e.getMessage(), e); | |
310 e.printStackTrace(); | |
311 } | |
312 } | |
313 | |
314 private List<Node> generateNodeList(List<Entity> list) { | |
315 List<Node> list0 = new ArrayList<Node>(); | |
316 for (Entity ent : list) { | |
317 list0.add(ent); | |
318 for (Attribute att : ent.getAttributes()) { | |
319 list0.add(att); | |
320 } | |
321 for (Relation rel : ent.getSourceRelations()) { | |
322 list0.add(rel); | |
323 } | |
324 for (Relation rel : ent.getTargetRelations()) { | |
325 list0.add(rel); | |
326 } | |
327 } | |
328 return list0; | |
329 } | |
330 | |
331 /** | |
38 | 332 * Saves all given entities without attributes and relations without creating new versions. |
333 * | |
1 | 334 * @param nodeList |
335 */ | |
38 | 336 public void saveEntityListAsNodeWithoutContent(List<Entity> nodeList) throws Exception { |
337 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
338 try { | |
339 session.getTransaction().begin(); | |
340 Sequence idSequence = this.getIdSequence(session); | |
341 | |
342 logger.debug("START saving Node list of size " + nodeList.size()); | |
1 | 343 |
38 | 344 for (Entity node : nodeList) { |
345 node.autoNormalize(); | |
346 if (node.getId() == null) { | |
347 node.setId(idSequence.generateId()); | |
348 } | |
349 if (node.getRowId() == null) { | |
350 // node is new | |
351 logger.debug("Saving node from EntityListAsNodeWithoutContent: " + node); | |
352 txLog.info("save node: " + node.toEncString()); | |
353 session.save(node); | |
354 } else { | |
355 txLog.info("merge node: " + node.toEncString()); | |
356 session.merge(node); | |
357 } | |
358 } | |
359 // update sequence | |
360 session.save(idSequence); | |
361 | |
362 logger.debug("END saving Node list."); | |
363 | |
364 } catch (Exception e) { | |
365 logger.error(e); | |
366 } finally { | |
367 session.getTransaction().commit(); | |
368 } | |
1 | 369 } |
370 | |
371 public void deleteEntityList(List<Entity> entities) { | |
372 try { | |
373 Session session = HibernateUtil.getSessionFactory() | |
374 .getCurrentSession(); | |
375 session.getTransaction().begin(); | |
376 | |
377 for (Entity ent : entities) { | |
378 this.removePersistenceEntity(ent, session); | |
379 } | |
380 | |
381 session.getTransaction().commit(); | |
382 } catch (Exception e) { | |
383 logger.error(e.getMessage(), e); | |
384 // e.printStackTrace(); | |
385 } | |
386 } | |
387 | |
388 public void saveEntityList(List<Entity> entities) throws Exception { | |
389 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
390 session.getTransaction().begin(); | |
391 | |
392 for (Entity entity : entities) { | |
393 saveEntity0(session, entity); | |
394 } | |
395 | |
396 session.getTransaction().commit(); | |
397 } | |
398 | |
399 public Map<Long, Long> saveEntityListAsNew(List<Entity> entities, | |
400 Map<Long, Long> idMap) throws Exception { | |
401 | |
402 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
403 session.getTransaction().begin(); | |
404 Sequence idSequence = this.getIdSequence(session); | |
405 | |
406 for (Entity entity : entities) { | |
407 | |
408 Long oldId = entity.getId(); | |
409 entity.resetId(); | |
410 entity.resetRowId(); | |
411 this.saveEntity0(session, entity); | |
412 idMap.put(oldId, entity.getId()); | |
413 } | |
414 | |
415 session.save(idSequence); | |
416 session.getTransaction().commit(); | |
417 return idMap; | |
418 } | |
419 | |
420 /** | |
421 * <p> | |
422 * This method deletes all entities by type (TBox/ABox), it means the method | |
423 * deletes either all concepts or all assertions. | |
424 * </p> | |
425 * <p> | |
426 * The history of every deleted entity will be removed too. | |
427 * </p> | |
428 * | |
429 * @param type | |
430 * Node.TYPE_TBOX or Node.TYPE_ABOX | |
431 */ | |
432 @Deprecated | |
433 public void deleteEntities(Long id, String type, Boolean deleteHistory) { | |
434 if (!(Node.TYPE_ABOX.equals(type) || Node.TYPE_TBOX.equals(type))) { | |
435 throw new IllegalArgumentException( | |
436 "The parameter 'type' should be either: " + Node.TYPE_ABOX | |
437 + " or " + Node.TYPE_TBOX + ", but was: " + type); | |
438 } | |
439 | |
440 List<Entity> entList = this.getEntities(id, | |
441 Node.SYS_STATUS_CURRENT_VERSION, type, null); | |
442 | |
443 // loading previous versions | |
444 List<Entity> historyEntList = new ArrayList<Entity>(); | |
445 if (deleteHistory) { | |
446 for (Entity ent : entList) { | |
447 historyEntList.addAll(this.getEntities(ent.getId(), | |
448 Node.SYS_STATUS_PREVIOUS_VERSION, type, null)); | |
449 } | |
450 } | |
451 | |
452 try { | |
453 Session session = HibernateUtil.getSessionFactory() | |
454 .getCurrentSession(); | |
455 session.getTransaction().begin(); | |
456 | |
457 for (Entity ent : entList) { | |
458 this.removePersistenceEntity(ent, session); | |
459 } | |
460 if (deleteHistory) { | |
461 for (Entity ent : historyEntList) { | |
462 this.removePersistenceEntity(ent, session); | |
463 } | |
464 } | |
465 | |
466 session.getTransaction().commit(); | |
467 } catch (Exception e) { | |
468 logger.error(e.getMessage(), e); | |
469 // e.printStackTrace(); | |
470 } | |
471 } | |
472 | |
473 /** | |
474 * This deleting is made using JDBC and not hibernate. The history is too | |
475 * removed by this method. TODO test what could happen with the hibernate | |
476 * cache | |
477 * | |
478 * @param ent | |
479 * @param session | |
480 */ | |
481 private void removePersistenceEntity(Entity ent, Session session) { | |
482 for (Attribute att : ent.getAttributes()) { | |
483 // session.delete(att); | |
484 removeNodesById(att.getId(), session); | |
485 for (View view : att.getViews()) { | |
486 // session.delete(view); | |
487 removeNodesById(view.getId(), session); | |
488 } | |
489 } | |
490 for (Relation rel : ent.getSourceRelations()) { | |
491 // session.delete(rel); | |
492 removeNodesById(rel.getId(), session); | |
493 for (View view : rel.getViews()) { | |
494 // session.delete(view); | |
495 removeNodesById(view.getId(), session); | |
496 } | |
497 } | |
498 for (Relation rel : ent.getTargetRelations()) { | |
499 // session.delete(rel); | |
500 removeNodesById(rel.getId(), session); | |
501 for (View view : rel.getViews()) { | |
502 // session.delete(view); | |
503 removeNodesById(view.getId(), session); | |
504 } | |
505 } | |
506 for (View view : ent.getViews()) { | |
507 // session.delete(view); | |
508 removeNodesById(view.getId(), session); | |
509 } | |
510 // session.delete(ent); | |
511 removeNodesById(ent.getId(), session); | |
512 } | |
513 | |
514 private int removeNodesById(Long id, Session session) { | |
515 String sql = "DELETE FROM Node WHERE id = :id"; | |
516 Query query = session.createQuery(sql); | |
517 query.setLong("id", id); | |
518 return query.executeUpdate(); | |
519 } | |
520 | |
521 /** | |
522 * <p> | |
523 * Changes the system status of the entity from CURRENT_VERSION to | |
524 * PREVIOUS_VERSION. | |
525 * </p> | |
526 * | |
527 * <p> | |
528 * It means, the entity will stay in the DB, but the it will not be visible | |
529 * by the ordinary methods. | |
530 * </p> | |
531 * | |
532 * @param entity | |
533 */ | |
51 | 534 public void removeEntCurrentVersion(Long entId, String type) throws Exception { |
535 logger.info("Deleting entity [ID=" + entId + ", type=" + type + "]. But keeping history in DB."); | |
536 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
537 try { | |
538 session.getTransaction().begin(); | |
1 | 539 |
51 | 540 if (entId != null) { |
541 /* | |
542 * this method get the current version. Related to the relation, | |
543 * the time_modification is not consider, because it it | |
544 * producing problems. | |
545 */ | |
546 List<Entity> previousEntityList = this.getEntities(session, entId, Node.SYS_STATUS_CURRENT_VERSION, | |
547 type, null, false); | |
548 if (previousEntityList.size() > 0) { | |
549 if (previousEntityList.size() > 1) { | |
550 logger.error("Found more than one current entities!"); | |
551 } | |
552 Entity previousEntity = previousEntityList.get(0); | |
553 logger.info("Saving previous entity: " + previousEntity); | |
554 this.persistEntityAsPrevious(session, previousEntity); | |
555 } | |
556 } | |
557 } catch (Exception e) { | |
558 logger.error(e); | |
559 } finally { | |
560 session.getTransaction().commit(); | |
561 } | |
562 } | |
1 | 563 |
564 /** | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
565 * Save the entity to the database. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
566 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
567 * Creates a new version of the entity. Runs in a transaction. |
1 | 568 * |
569 * @param entity | |
570 * @return | |
571 */ | |
572 public void saveEntity(Entity entity) throws Exception { | |
36 | 573 // start transaction |
1 | 574 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); |
575 session.getTransaction().begin(); | |
36 | 576 // save entity |
1 | 577 saveEntity0(session, entity); |
36 | 578 // end transaction |
1 | 579 session.getTransaction().commit(); |
580 } | |
581 | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
582 /** |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
583 * Save the entity to the database using the given session. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
584 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
585 * Creates a new version and sets the existing entity to PREVIOUS_VERSION. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
586 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
587 * @param session |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
588 * @param entity |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
589 * @throws Exception |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
590 */ |
1 | 591 private void saveEntity0(Session session, Entity entity) throws Exception { |
592 if (entity.getId() != null) { | |
36 | 593 // get the still current entity(s) |
1 | 594 List<Entity> previousEntityList = this.getEntities(session, entity.getId(), Node.SYS_STATUS_CURRENT_VERSION, entity.getType(), null, false); |
595 if (previousEntityList.size() > 0) { | |
596 if (previousEntityList.size() > 1) { | |
51 | 597 logger.error("Found more than one current entities!"); |
1 | 598 } |
36 | 599 // set the first current entity as previous |
1 | 600 Entity previousEntity = previousEntityList.get(0); |
40
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
39
diff
changeset
|
601 // compare old and new entity |
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
39
diff
changeset
|
602 if (! previousEntity.equalsStructure(entity)) { |
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
39
diff
changeset
|
603 logger.warn("Entity to save has different structure! old="+previousEntity+" new="+entity); |
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
39
diff
changeset
|
604 } |
1 | 605 logger.info("Saving previous entity: " + previousEntity); |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
606 this.persistEntityAsPrevious(session, previousEntity); |
1 | 607 } |
608 } | |
609 this.saveCurrentEntity(session, entity, null); | |
610 } | |
611 | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
612 /** |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
613 * Save the entity as (new) current version using the given session. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
614 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
615 * Sets system_status to CURRENT_VERSION and updates row_id, version, modification_time, |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
616 * etc. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
617 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
618 * @param session |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
619 * @param entity |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
620 * @param idSequence |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
621 * @throws Exception |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
622 */ |
36 | 623 private void saveCurrentEntity(Session session, Entity entity, Sequence idSequence) throws Exception { |
1 | 624 Long time = System.currentTimeMillis(); |
57 | 625 refreshEntityRelations(session, entity); |
1 | 626 entity.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); |
627 entity.resetRowId(); | |
628 entity.increaseVersion(); | |
629 entity.setObjectClass(entity.getObjectClass()); | |
630 entity.setModificationTime(time); | |
631 entity.setType(entity.getType()); | |
632 entity.setUser(entity.getUser()); | |
633 entity.autoNormalize(); | |
634 // generating of id, connecting rels, atts and views to the entity | |
635 this.prepareEntity(session, entity, idSequence); | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
636 this.persistEntity(session, entity); |
1 | 637 } |
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
638 |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
639 /** |
57 | 640 * Updates the endpoints of the relations of this entity. |
641 * | |
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
642 * This class must be called, |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
643 * because sometimes the current entity is related to other entities that |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
644 * have been changed in parallel to the edition of this entity. |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
645 * For this reason, some relations of the current entity can have old information of the related entities (like the modification time). |
57 | 646 * This method updates the relations of the current entities getting the actual information of the related entities directly from the DB. |
647 * | |
648 * @author jurzua | |
649 * | |
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
650 * @param session |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
651 * @param entity |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
652 * @throws Exception |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
653 */ |
57 | 654 private void refreshEntityRelations(Session session, Entity entity) throws Exception { |
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
655 for(Relation rel : entity.getSourceRelations()){ |
57 | 656 refreshRelationTarget(session, rel); |
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
657 } |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
658 |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
659 for(Relation rel : entity.getTargetRelations()){ |
57 | 660 refreshRelationSource(session, rel); |
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
661 } |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
662 } |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
663 |
57 | 664 /** |
665 * Update the target of the relation. | |
666 * | |
667 * @author jurzua | |
668 * | |
669 * @param session | |
670 * @param rel | |
671 * @throws Exception | |
672 */ | |
673 private void refreshRelationTarget(Session session, Relation rel) throws Exception { | |
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
674 long targetModifOld = rel.getTargetModif(); |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
675 StringBuilder sb = new StringBuilder("refreshTarget\n" + rel.toString() + "\n"); |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
676 sb.append(rel.printTarget() + "\n"); |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
677 List<Entity> entityList = this.getEntities(session, rel.getTargetId(), Node.SYS_STATUS_CURRENT_VERSION, rel.getType(), null, false); |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
678 if(entityList.isEmpty()){ |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
679 throw new Exception("Saving entity " + rel.getSourceId() + ", the system found a relation without target. " + rel.toString()); |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
680 } |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
681 Entity target = entityList.get(0); |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
682 rel.setTarget(target); |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
683 sb.append(rel.printTarget() + "\n"); |
57 | 684 // the refresh is logged only if we detect a difference between the relation and the target entity. |
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
685 if(targetModifOld != rel.getTargetModif()){ |
57 | 686 logger.warn(sb.toString()); |
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
687 } |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
688 } |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
689 |
57 | 690 /** |
691 * Update the source of the relation. | |
692 * | |
693 * @author jurzua | |
694 * | |
695 * @param session | |
696 * @param rel | |
697 * @throws Exception | |
698 */ | |
699 private void refreshRelationSource(Session session, Relation rel) throws Exception { | |
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
700 long sourceModifOld = rel.getSourceModif(); |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
701 StringBuilder sb = new StringBuilder("refreshSource\n" + rel.toString() + "\n"); |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
702 sb.append(rel.printSource() + "\n"); |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
703 List<Entity> entityList = this.getEntities(session, rel.getSourceId(), Node.SYS_STATUS_CURRENT_VERSION, rel.getType(), null, false); |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
704 if(entityList.isEmpty()){ |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
705 throw new Exception("Saving entity " + rel.getTargetId() + ", the system found a relation without source. " + rel.toString()); |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
706 } |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
707 Entity source = entityList.get(0); |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
708 rel.setSource(source); |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
709 sb.append(rel.printSource() + "\n"); |
57 | 710 // the refresh is logged only if we detect a difference between the relation and the source entity. |
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
711 if(sourceModifOld != rel.getSourceModif()){ |
57 | 712 logger.warn(sb.toString()); |
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
713 } |
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
714 } |
1 | 715 |
716 /** | |
39 | 717 * Update the entity to prepare it for persistence using the session. |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
718 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
719 * If the ID is null new IDs will be assigned for the entity and its |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
720 * attributes and relations. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
721 * The modification time is propagated from the entity to its |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
722 * attributes and relations. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
723 * Own-values are normalized. |
1 | 724 * |
725 * @param session | |
726 * @param entity | |
727 * @return | |
728 */ | |
36 | 729 private Entity prepareEntity(Session session, Entity entity, Sequence idSequence) { |
1 | 730 |
36 | 731 /* |
732 * update id | |
733 */ | |
1 | 734 if (entity.getId() == null) { |
735 if (idSequence == null) | |
736 entity.setId(this.generateId(session)); | |
737 else | |
738 entity.setId(idSequence.generateId()); | |
739 } | |
740 | |
36 | 741 /* |
742 * update attributes | |
743 */ | |
1 | 744 for (Attribute att : entity.getAttributes()) { |
745 if (att.getId() == null) { | |
746 if (idSequence == null) | |
747 att.setId(this.generateId(session)); | |
748 else | |
749 att.setId(idSequence.generateId()); | |
750 } | |
751 att.setSourceId(entity.getId()); | |
752 att.setSourceModif(entity.getModificationTime()); | |
753 att.setSourceObjectClass(entity.getObjectClass()); | |
754 att.autoNormalize(); | |
755 } | |
756 | |
36 | 757 /* |
758 * update source relations | |
759 */ | |
1 | 760 for (Relation rel : entity.getSourceRelations()) { |
761 if (rel.getId() == null) { | |
762 if (idSequence == null) | |
763 rel.setId(this.generateId(session)); | |
764 else | |
765 rel.setId(idSequence.generateId()); | |
766 } | |
767 rel.setSourceId(entity.getId()); | |
768 rel.setSourceModif(entity.getModificationTime()); | |
769 rel.setSourceObjectClass(entity.getObjectClass()); | |
36 | 770 // TODO: update relation target too? |
771 rel.autoNormalize(); // TODO: normalize relation? | |
772 if(StringUtils.equals(entity.getType(), Node.TYPE_ABOX)) { | |
1 | 773 rel.setObjectClass(rel.getOwnValue()); |
36 | 774 } else if(StringUtils.equals(entity.getType(), Node.TYPE_TBOX)) { |
1 | 775 rel.setObjectClass(Node.TYPE_TBOX); |
776 } | |
777 | |
36 | 778 /* |
779 * update attributes for relations | |
780 */ | |
1 | 781 for (Attribute att : rel.getAttributes()) { |
782 if (att.getId() == null) { | |
783 if (idSequence == null) | |
784 att.setId(this.generateId(session)); | |
785 else | |
786 att.setId(idSequence.generateId()); | |
787 } | |
788 att.setSourceId(rel.getId()); | |
789 att.setSourceModif(rel.getModificationTime()); | |
790 att.setSourceObjectClass(rel.getOwnValue()); | |
791 att.autoNormalize(); | |
792 } | |
793 } | |
794 | |
36 | 795 /* |
796 * update target relations | |
797 */ | |
1 | 798 for (Relation rel : entity.getTargetRelations()) { |
799 if (rel.getId() == null) { | |
800 if (idSequence == null) | |
801 rel.setId(this.generateId(session)); | |
802 else | |
803 rel.setId(idSequence.generateId()); | |
804 } | |
805 rel.setTargetId(entity.getId()); | |
806 rel.setTargetModif(entity.getModificationTime()); | |
807 rel.setTargetObjectClass(entity.getObjectClass()); | |
36 | 808 // TODO: update relation source too? |
809 rel.autoNormalize(); // TODO: normalize? | |
1 | 810 if(StringUtils.equals(entity.getType(), Node.TYPE_ABOX)){ |
811 rel.setObjectClass(rel.getOwnValue()); | |
812 }else if(StringUtils.equals(entity.getType(), Node.TYPE_TBOX)){ | |
813 rel.setObjectClass(Node.TYPE_TBOX); | |
814 } | |
36 | 815 /* |
816 * update relation attributes | |
817 */ | |
1 | 818 for (Attribute att : rel.getAttributes()) { |
819 if (att.getId() == null) { | |
820 if (idSequence == null) | |
821 att.setId(this.generateId(session)); | |
822 else | |
823 att.setId(idSequence.generateId()); | |
824 } | |
825 att.setSourceId(rel.getId()); | |
826 att.setSourceModif(rel.getModificationTime()); | |
827 att.setSourceObjectClass(rel.getOwnValue()); | |
828 att.autoNormalize(); | |
829 } | |
830 } | |
36 | 831 |
832 /* | |
833 * update views(?) | |
834 */ | |
1 | 835 for (View view : entity.getViews()) { |
836 if (view.getId() == null) { | |
837 if (idSequence == null) | |
838 view.setId(this.generateId(session)); | |
839 else | |
840 view.setId(idSequence.generateId()); | |
841 } | |
842 view.setSourceId(entity.getId()); | |
843 view.setSourceModif(entity.getModificationTime()); | |
844 view.setSourceObjectClass(entity.getObjectClass()); | |
845 view.autoNormalize(); | |
846 } | |
36 | 847 |
1 | 848 return entity; |
849 } | |
850 | |
851 /** | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
852 * Set an entities' system state to PREVIOUS_VERSION and persist it. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
853 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
854 * Also persists the entities' attributes and relations. |
1 | 855 * |
856 * @param session | |
857 * @param entity | |
858 */ | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
859 private void persistEntityAsPrevious(Session session, Entity entity) { |
1 | 860 entity.setSystemStatus(Node.SYS_STATUS_PREVIOUS_VERSION); |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
861 txLog.debug("* START save previous entity..."); |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
862 txLog.info("save previous entity: "+entity.toEncString()); |
1 | 863 session.save(entity); |
864 for (Attribute attribute : entity.getAttributes()) { | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
865 txLog.info("save previous entity attribute: "+attribute.toEncString()); |
1 | 866 session.save(attribute); |
867 } | |
868 for (Relation rel : entity.getSourceRelations()) { | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
869 txLog.info("save previous source relation: "+rel.toEncString()); |
1 | 870 session.save(rel); |
871 for (Attribute att : rel.getAttributes()) { | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
872 txLog.info("save previous source relation attribute: "+att.toEncString()); |
1 | 873 session.save(att); |
874 } | |
875 } | |
876 for (Relation rel : entity.getTargetRelations()) { | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
877 txLog.info("save previous target relation: "+rel.toEncString()); |
1 | 878 session.save(rel); |
25
c23ae718fdd3
clean up and more logging when saving attributes.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
879 for (Attribute att : rel.getAttributes()) { |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
880 txLog.info("save previous target relation attribute: "+att.toEncString()); |
25
c23ae718fdd3
clean up and more logging when saving attributes.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
881 session.save(att); |
c23ae718fdd3
clean up and more logging when saving attributes.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
882 } |
1 | 883 } |
884 for (View view : entity.getViews()) { | |
885 session.save(view); | |
886 } | |
36 | 887 txLog.debug("* END ...save previous entity"); |
1 | 888 } |
889 | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
890 /** |
39 | 891 * Persist a (current) entity and its attributes and relations using the session. |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
892 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
893 * @param session |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
894 * @param entity |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
895 * @throws Exception |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
896 */ |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
897 private void persistEntity(Session session, Entity entity) throws Exception { |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
898 txLog.info("* START save entity..."); |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
899 txLog.info("save entity: "+entity.toEncString()); |
1 | 900 session.save(entity); |
901 for (Attribute attribute : entity.getAttributes()) { | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
902 txLog.info("save entity attribute: "+attribute.toEncString()); |
1 | 903 session.save(attribute); |
904 } | |
905 for (Relation rel : entity.getSourceRelations()) { | |
906 rel.setSource(entity); | |
907 if (rel.getSourceId() == null || rel.getTargetId() == null) { | |
908 throw new IllegalStateException( | |
909 "Either the sourceId or the targetId was not initialized to the source relation: " | |
910 + rel.getOwnValue()); | |
911 } | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
912 txLog.info("save source relation: "+rel.toEncString()); |
1 | 913 session.save(rel); |
914 for (Attribute att : rel.getAttributes()) { | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
915 txLog.info("save source relation attribute: "+att.toEncString()); |
1 | 916 session.save(att); |
917 } | |
918 } | |
919 for (Relation rel : entity.getTargetRelations()) { | |
920 rel.setTarget(entity); | |
921 if (rel.getSourceId() == null || rel.getTargetId() == null) { | |
922 throw new IllegalStateException( | |
25
c23ae718fdd3
clean up and more logging when saving attributes.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
923 "Either the sourceId or the targetId was not initialized to the target relation: " |
1 | 924 + rel.getOwnValue()); |
925 } | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
926 txLog.info("save target relation: "+rel.toEncString()); |
1 | 927 session.save(rel); |
25
c23ae718fdd3
clean up and more logging when saving attributes.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
928 for (Attribute att : rel.getAttributes()) { |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
929 txLog.info("save target relation attribute: "+att.toEncString()); |
25
c23ae718fdd3
clean up and more logging when saving attributes.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
930 session.save(att); |
c23ae718fdd3
clean up and more logging when saving attributes.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
931 } |
1 | 932 } |
933 | |
934 // Call of ownValue Generator. | |
935 // TODO: the method should be used always? what would happen if the | |
936 // ownValue returns null? | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
937 if (!isImportMode()) { |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
938 String ownValue = this.getOwnValueGenerator().generateOwnValue(entity, session); |
1 | 939 if (StringUtils.isNotEmpty(ownValue)) { |
940 entity.setOwnValue(ownValue); | |
941 entity.autoNormalize(); | |
36 | 942 txLog.info("save entity (new ov): "+entity.toEncString()); |
1 | 943 session.save(entity); |
944 } | |
945 } | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
946 txLog.debug("* ...END save entity"); |
1 | 947 } |
948 | |
949 public OwnValueGenerator getOwnValueGenerator() { | |
950 return ownValueGenerator; | |
951 } | |
952 | |
953 public void setOwnValueGenerator(OwnValueGenerator ownValueGenerator) { | |
954 this.ownValueGenerator = ownValueGenerator; | |
955 } | |
956 | |
957 /** | |
39 | 958 * Returns entities with their content from the database using the session. |
36 | 959 * |
960 * If a parameter is null then the condition is omitted from the query | |
961 * returning all entities matching the remaining conditions. | |
1 | 962 * |
963 * @param session | |
964 * @param id | |
965 * @param systemStatus | |
966 * @param type | |
967 * @param ownValue | |
36 | 968 * @param considerTimeModif |
1 | 969 * @return |
970 */ | |
971 private List<Entity> getEntities(Session session, Long id, | |
972 String systemStatus, String type, String ownValue, boolean considerTimeModif) { | |
973 | |
39 | 974 if (!(systemStatus.equals(Node.SYS_STATUS_PREVIOUS_VERSION) |
975 || systemStatus.equals(Node.SYS_STATUS_CURRENT_VERSION))) { | |
976 throw new IllegalArgumentException("Invalid input systemStatus: " + systemStatus); | |
977 } | |
1 | 978 |
979 if (StringUtils.isNotEmpty(type) | |
980 && !(type.equals(Node.TYPE_ABOX) || type.equals(Node.TYPE_TBOX))) { | |
981 throw new IllegalArgumentException("Invalid input type: " + type); | |
982 } | |
983 | |
984 List<Entity> entities; | |
985 | |
986 String hqlEntities = "from Entity where systemStatus = :systemStatus"; | |
987 | |
988 if (StringUtils.isNotEmpty(type)) { | |
989 hqlEntities += " AND type = :type"; | |
990 } | |
991 | |
992 if (id != null) { | |
993 hqlEntities += " AND id = :id"; | |
994 } | |
995 | |
996 if (StringUtils.isNotEmpty(ownValue)) { | |
997 hqlEntities += " AND ownValue = :ownValue"; | |
998 } | |
999 | |
1000 Query queryEntities = session.createQuery(hqlEntities); | |
1001 queryEntities.setString("systemStatus", systemStatus); | |
1002 | |
1003 if (StringUtils.isNotEmpty(type)) { | |
1004 queryEntities.setString("type", type); | |
1005 } | |
1006 | |
1007 if (StringUtils.isNotEmpty(ownValue)) { | |
1008 queryEntities.setString("ownValue", ownValue); | |
1009 } | |
1010 | |
1011 if (id != null) { | |
1012 queryEntities.setLong("id", id); | |
1013 } | |
1014 entities = queryEntities.list(); | |
1015 | |
1016 for (Entity entity : entities) { | |
1017 entity.setLightweight(true); | |
1018 entity = this.getEntityContent(session, entity, considerTimeModif); | |
1019 } | |
1020 | |
1021 return entities; | |
1022 } | |
1023 | |
1024 /** | |
39 | 1025 * Returns entities with their content from the database. |
1 | 1026 * |
36 | 1027 * If a parameter is null then the condition is omitted from the query |
1028 * returning all entities matching the remaining conditions. | |
1029 * | |
1 | 1030 * @param id |
1031 * @param systemStatus | |
1032 * @param type | |
1033 * @param ownValue | |
1034 * @return | |
1035 */ | |
36 | 1036 public List<Entity> getEntities(Long id, String systemStatus, String type, String ownValue) { |
1037 // logger.debug("GET ENTITIES Entities [id=" + id + ", type=" + type + ", ownValue=" + ownValue + "]"); | |
1 | 1038 |
1039 if (!(systemStatus.equals(Node.SYS_STATUS_PREVIOUS_VERSION) || systemStatus | |
1040 .equals(Node.SYS_STATUS_CURRENT_VERSION))) { | |
1041 throw new IllegalArgumentException("Invalid input systemStatus: " | |
1042 + systemStatus); | |
1043 } | |
1044 | |
1045 if (StringUtils.isNotEmpty(type) | |
1046 && !(type.equals(Node.TYPE_ABOX) || type.equals(Node.TYPE_TBOX))) { | |
1047 throw new IllegalArgumentException("Invalid input type: " + type); | |
1048 } | |
1049 | |
1050 List<Entity> entities = null; | |
1051 Session session = null; | |
1052 try { | |
36 | 1053 //long start = System.currentTimeMillis(); |
1 | 1054 session = HibernateUtil.getSessionFactory().getCurrentSession(); |
1055 session.getTransaction().begin(); | |
1056 | |
36 | 1057 entities = this.getEntities(session, id, systemStatus, type, ownValue, true); |
1 | 1058 |
36 | 1059 /* long dif = System.currentTimeMillis() - start; |
1 | 1060 String s = "Found=\n"; |
1061 for (Entity e : entities) { | |
1062 s += e.toString() + "\n"; | |
1063 } | |
1064 s += "time used= " + dif + "[ms]\n\n"; | |
36 | 1065 logger.debug(s); */ |
1 | 1066 |
1067 } catch (Exception e) { | |
1068 logger.error(e.getMessage(), e); | |
1069 // e.printStackTrace(); | |
1070 } finally { | |
1071 session.getTransaction().commit(); | |
1072 } | |
1073 return entities; | |
1074 } | |
1075 | |
1076 private String whereUpdate(String where) { | |
1077 if (StringUtils.isEmpty(where)) | |
1078 where = " where "; | |
1079 else | |
1080 where += " AND "; | |
1081 return where; | |
1082 } | |
1083 | |
36 | 1084 /** |
1085 * Returns a list of Nodes. | |
1086 * | |
1087 * If a parameter is null then the condition is omitted from the query | |
1088 * returning all entities matching the remaining conditions. | |
1089 * | |
1090 * @param session | |
1091 * @param id | |
1092 * @param systemStatus | |
1093 * @param sourceId | |
1094 * @param srcModif | |
1095 * @param targetId | |
1096 * @param tarModif | |
1097 * @return | |
1098 */ | |
1 | 1099 public List<Node> getNodes(Session session, Long id, String systemStatus, |
1100 Long sourceId, Long srcModif, Long targetId, Long tarModif) { | |
1101 List<Node> nodes = null; | |
1102 | |
36 | 1103 /* |
1104 * create HQL query string | |
1105 */ | |
1 | 1106 String from = "from Node "; |
1107 String where = ""; | |
1108 | |
1109 if (id != null) { | |
1110 where = this.whereUpdate(where); | |
1111 where += " id = :id "; | |
36 | 1112 } |
1 | 1113 |
1114 if (StringUtils.isNotEmpty(systemStatus)) { | |
1115 where = this.whereUpdate(where); | |
1116 where += " systemStatus = :systemStatus "; | |
1117 } | |
36 | 1118 |
1 | 1119 if (sourceId != null) { |
1120 where = this.whereUpdate(where); | |
1121 where += " sourceId = :sourceId "; | |
1122 } | |
36 | 1123 |
1 | 1124 if (srcModif != null) { |
1125 where = this.whereUpdate(where); | |
1126 where += " sourceModif = :sourceModif "; | |
1127 } | |
1128 | |
1129 if (targetId != null) { | |
1130 where = this.whereUpdate(where); | |
1131 where += " targetId = :targetId "; | |
1132 } | |
36 | 1133 |
1 | 1134 if (tarModif != null) { |
1135 where = this.whereUpdate(where); | |
1136 where += " targetModif = :targetModif "; | |
1137 } | |
1138 | |
1139 String hql = from + where; | |
36 | 1140 // create query object |
1 | 1141 Query query = session.createQuery(hql); |
1142 | |
36 | 1143 /* |
1144 * add query parameters | |
1145 */ | |
1 | 1146 if (id != null) |
1147 query.setLong("id", id); | |
36 | 1148 |
1 | 1149 if (StringUtils.isNotEmpty(systemStatus)) |
1150 query.setString("systemStatus", systemStatus); | |
1151 | |
1152 if (sourceId != null) | |
1153 query.setLong("sourceId", sourceId); | |
36 | 1154 |
1 | 1155 if (srcModif != null) |
1156 query.setLong("sourceModif", srcModif); | |
1157 | |
1158 if (targetId != null) | |
1159 query.setLong("targetId", targetId); | |
36 | 1160 |
1 | 1161 if (tarModif != null) |
1162 query.setLong("targetModif", tarModif); | |
1163 | |
36 | 1164 /* |
1165 * run the query | |
1166 */ | |
1 | 1167 nodes = query.list(); |
1168 | |
1169 return nodes; | |
1170 } | |
1171 | |
36 | 1172 /** |
1173 * Populate a lightweight entity with attributes and relations. | |
1174 * | |
1175 * @param session | |
1176 * @param entity | |
1177 * @param considerTimeModif | |
1178 * @return | |
1179 */ | |
1 | 1180 private Entity getEntityContent(Session session, Entity entity, boolean considerTimeModif) { |
1181 if (entity != null && entity.isLightweight()) { | |
1182 entity.setLightweight(false); | |
1183 entity.setAttributes(new ArrayList<Attribute>()); | |
1184 entity.setSourceRelations(new ArrayList<Relation>()); | |
1185 entity.setTargetRelations(new ArrayList<Relation>()); | |
1186 | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1187 /* |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1188 * Danger: getNodes finds all attributes in the db and tries to attach them to |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1189 * this entity if id=null! |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1190 */ |
26
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1191 if (entity.getId() == null) { |
27 | 1192 logger.error("Entity with id=null! Abort loading attributes."); |
26
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1193 return entity; |
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1194 } |
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1195 |
36 | 1196 /* |
1197 * get all nodes with source_id = entity.id | |
1198 * i.e. Attributes and SourceRelations. | |
1199 */ | |
26
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1200 List<Node> nodes = null; |
36 | 1201 if (considerTimeModif) { |
1 | 1202 nodes = this.getNodes(session, null, |
1203 entity.getSystemStatus(), entity.getId(), | |
1204 entity.getModificationTime(), null, null); | |
36 | 1205 } else { |
1 | 1206 nodes = this.getNodes(session, null, |
1207 entity.getSystemStatus(), entity.getId(), | |
1208 null, null, null); | |
1209 } | |
1210 | |
36 | 1211 /* |
1212 * add Attributes and SourceRelations | |
1213 */ | |
1 | 1214 for (Node node : nodes) { |
1215 if (node instanceof Attribute) { | |
1216 entity.addAttribute((Attribute) node); | |
36 | 1217 |
1 | 1218 } else if (node instanceof Relation) { |
1219 Relation rel = (Relation) node; | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1220 /* |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1221 * Danger: getNodes finds all attributes in the db and tries to attach them to |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1222 * this relation if id=null! |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1223 */ |
26
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1224 if (rel.getId() == null) { |
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1225 logger.error("Relation with id=null! Abort loading attributes."); |
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1226 continue; |
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1227 } |
36 | 1228 /* |
1229 * get all nodes with source_id = rel.id | |
1230 * i.e. relation Attributes | |
1231 */ | |
1 | 1232 List<Node> attrs = this.getNodes(session, null, |
1233 rel.getSystemStatus(), rel.getId(), | |
1234 rel.getModificationTime(), null, null); | |
1235 for (Node attNode : attrs) { | |
1236 if (attNode instanceof Attribute) { | |
1237 rel.addAttribute((Attribute) attNode); | |
1238 } | |
1239 } | |
36 | 1240 // TODO: ??? |
1241 if (considerTimeModif) { | |
1 | 1242 entity.addSourceRelation(rel); |
36 | 1243 } else { |
1 | 1244 entity.getSourceRelations().add(rel); |
1245 } | |
36 | 1246 |
1 | 1247 } else if (node instanceof View) { |
1248 entity.getViews().add((View) node); | |
36 | 1249 |
1 | 1250 } else { |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1251 throw new IllegalArgumentException("Invalid node found: " + node); |
1 | 1252 } |
1253 } | |
1254 | |
36 | 1255 /* |
1256 * get all nodes with target_id = entity.id | |
1257 * i.e. TargetRelations | |
1258 */ | |
1 | 1259 List<Node> tarRels = null; |
1260 | |
36 | 1261 if (considerTimeModif) { |
1 | 1262 tarRels = this.getNodes(session, null, |
1263 entity.getSystemStatus(), null, null, entity.getId(), | |
1264 entity.getModificationTime()); | |
36 | 1265 } else { |
1 | 1266 tarRels = this.getNodes(session, null, |
1267 entity.getSystemStatus(), null, null, entity.getId(), | |
1268 null); | |
1269 } | |
1270 | |
36 | 1271 /* |
1272 * add TargetRelations | |
1273 */ | |
1 | 1274 for (Node node : tarRels) { |
1275 if (node instanceof Relation) { | |
1276 Relation rel = (Relation) node; | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1277 /* |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1278 * Danger: getNodes finds all attributes in the db and tries to attach them to |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1279 * this relation if id=null! |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1280 */ |
26
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1281 if (rel.getId() == null) { |
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1282 logger.error("Relation with id=null! Abort loading attributes."); |
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1283 continue; |
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1284 } |
36 | 1285 /* |
1286 * get all nodes with source_id = rel.id | |
1287 * i.e. relation Attributes | |
1288 */ | |
1 | 1289 List<Node> attrs = this.getNodes(session, null, |
1290 rel.getSystemStatus(), rel.getId(), | |
1291 rel.getModificationTime(), null, null); | |
1292 for (Node attNode : attrs) { | |
1293 if (attNode instanceof Attribute) { | |
1294 rel.addAttribute((Attribute) attNode); | |
1295 } | |
1296 } | |
36 | 1297 // TODO: ??? |
1298 if (considerTimeModif) { | |
1 | 1299 entity.addTargetRelation(rel); |
36 | 1300 } else { |
1 | 1301 entity.getTargetRelations().add(rel); |
1302 } | |
36 | 1303 |
1 | 1304 } else { |
36 | 1305 throw new IllegalArgumentException("Invalid node found: " + node); |
1 | 1306 } |
1307 } | |
1308 } | |
1309 return entity; | |
1310 } | |
1311 | |
36 | 1312 /** |
1313 * Populate a lightweight entity with attributes and relations. | |
1314 * | |
1315 * @param entity | |
1316 * @return | |
1317 */ | |
1318 public Entity getEntityContent(Entity entity) { | |
1319 if (entity != null && entity.isLightweight()) { | |
1320 Session session = null; | |
1321 try { | |
1322 /* logger.debug("GET ENTITY CONTENT [objClass=" + entity.getObjectClass() + ", id=" + entity.getId() | |
1323 + ", ownValue=" + entity.getOwnValue() + "]"); */ | |
1324 //long start = System.currentTimeMillis(); | |
1325 | |
1326 // start transaction | |
1327 session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
1328 session.getTransaction().begin(); | |
1329 // get entity content | |
1330 entity = this.getEntityContent(session, entity, true); | |
1331 | |
1332 /* long diff = System.currentTimeMillis() - start; | |
1333 logger.debug("Time to get content=" + diff + "(s)"); */ | |
1334 } catch (Exception e) { | |
1335 logger.error(e.getMessage(), e); | |
1336 } finally { | |
1337 session.getTransaction().commit(); | |
1338 } | |
1339 } | |
1340 return entity; | |
1341 } | |
1 | 1342 |
36 | 1343 public List<String> getObjecClassSuggestion(String objectClass, Class nodeClass, int maxResults) { |
1 | 1344 objectClass += "%"; |
1345 try { | |
1346 | |
1347 List<String> suggestions = new ArrayList<String>(); | |
1348 Session session = HibernateUtil.getSessionFactory() | |
1349 .getCurrentSession(); | |
1350 session.getTransaction().begin(); | |
1351 String hql = "from "; | |
1352 if (nodeClass.equals(Entity.class)) { | |
1353 hql += "Entity "; | |
1354 } | |
1355 if (nodeClass.equals(Relation.class)) { | |
1356 hql += "Relation "; | |
1357 } | |
1358 if (nodeClass.equals(Attribute.class)) { | |
1359 hql += "Attribute "; | |
1360 } | |
1361 if (nodeClass.equals(Node.class)) { | |
1362 hql += "Node "; | |
1363 } | |
1364 if (nodeClass.equals(View.class)) { | |
1365 hql += "View "; | |
1366 } | |
1367 hql += " where objectClass like :objectClass " | |
1368 + "group by objectClass"; | |
1369 | |
1370 Query query = session.createQuery(hql); | |
1371 query.setString("objectClass", objectClass); | |
1372 query.setMaxResults(maxResults); | |
1373 List<Node> list = query.list(); | |
1374 for (Node node : list) { | |
1375 suggestions.add(node.getObjectClass()); | |
1376 } | |
1377 return suggestions; | |
1378 | |
1379 } catch (Exception e) { | |
1380 logger.error(e.getMessage(), e); | |
1381 // e.printStackTrace(); | |
1382 } | |
1383 return null; | |
1384 } | |
1385 | |
36 | 1386 public List<String> getOwnValueSuggestion(String ownValue, Class nodeClass, int maxResults) { |
1 | 1387 ownValue += "%"; |
1388 try { | |
1389 | |
1390 List<String> suggestions = new ArrayList<String>(); | |
1391 Session session = HibernateUtil.getSessionFactory() | |
1392 .getCurrentSession(); | |
1393 session.getTransaction().begin(); | |
1394 String hql = "from "; | |
1395 if (nodeClass.equals(Entity.class)) { | |
1396 hql += "Entity "; | |
1397 } | |
1398 if (nodeClass.equals(Relation.class)) { | |
1399 hql += "Relation "; | |
1400 } | |
1401 if (nodeClass.equals(Attribute.class)) { | |
1402 hql += "Attribute "; | |
1403 } | |
1404 if (nodeClass.equals(Node.class)) { | |
1405 hql += "Node "; | |
1406 } | |
1407 if (nodeClass.equals(View.class)) { | |
1408 hql += "View "; | |
1409 } | |
1410 hql += " where ownValue like :ownValue " + "group by ownValue"; | |
1411 | |
1412 Query query = session.createQuery(hql); | |
1413 query.setString("ownValue", ownValue); | |
1414 query.setMaxResults(maxResults); | |
1415 List<Node> list = query.list(); | |
1416 for (Node node : list) { | |
1417 suggestions.add(node.getOwnValue()); | |
1418 } | |
1419 return suggestions; | |
1420 | |
1421 } catch (Exception e) { | |
1422 logger.error(e.getMessage(), e); | |
1423 // e.printStackTrace(); | |
1424 } | |
1425 return null; | |
1426 } | |
1427 | |
1428 public Entity loadEntitiesForTargetRelation(Entity entity) { | |
1429 try { | |
1430 // where parent_id = :parent_id AND type = :type | |
1431 Session session = HibernateUtil.getSessionFactory() | |
1432 .getCurrentSession(); | |
1433 session.getTransaction().begin(); | |
1434 | |
1435 for (Relation rel : entity.getTargetRelations()) { | |
1436 List<Entity> entities = this.getEntities(session, | |
1437 rel.getSourceId(), Node.SYS_STATUS_CURRENT_VERSION, | |
1438 null, null, true); | |
1439 if (entities.size() > 0) { | |
1440 Entity source = entities.get(0); | |
1441 rel.setSource(source); | |
1442 } | |
1443 } | |
1444 | |
1445 session.getTransaction().commit(); | |
1446 return entity; | |
1447 } catch (Exception e) { | |
1448 logger.error(e.getMessage(), e); | |
1449 // e.printStackTrace(); | |
1450 } | |
1451 return entity; | |
1452 } | |
1453 | |
1454 /** | |
39 | 1455 * Return all (lightweight) entities matching the query. |
1456 * | |
1457 * Any parameter that is null is omitted as a query condition matching | |
1458 * all values in this parameter. | |
1459 * | |
1460 * Own value is searched as substring if ownValueSubString=true. | |
1461 * | |
1462 * @param session | |
1 | 1463 * @param systemStatus |
1464 * @param id | |
1465 * @param type | |
1466 * @param objectClass | |
1467 * @param ownValue | |
39 | 1468 * @param ownValueSubString |
1 | 1469 * @param maxResult |
1470 * @return | |
1471 */ | |
39 | 1472 protected List<Entity> getLightweightEntities(Session session, String systemStatus, Long id, String type, |
1473 String objectClass, String ownValue, boolean ownValueSubString, int maxResult) { | |
1474 | |
1475 /* | |
1476 * build query string | |
1477 */ | |
1478 String hqlEntities = "from Entity where "; | |
1479 if (StringUtils.isNotEmpty(ownValue)) { | |
1480 if (ownValueSubString) | |
1481 hqlEntities += "ownValue like :ownValue AND "; | |
1482 else | |
1483 hqlEntities += "ownValue = :ownValue AND "; | |
1484 } | |
1485 if (id != null) { | |
1486 hqlEntities += "id = :id AND "; | |
1487 } | |
1488 if (StringUtils.isNotEmpty(objectClass)) { | |
1489 hqlEntities += "objectClass = :objectClass AND "; | |
1490 } | |
1491 if (StringUtils.isNotEmpty(type)) { | |
1492 hqlEntities += "type = :type AND "; | |
1493 } | |
1 | 1494 |
39 | 1495 hqlEntities += "systemStatus = :systemStatus "; |
1496 | |
1497 /* | |
1498 * create query object | |
1499 */ | |
1500 Query queryEntities = session.createQuery(hqlEntities); | |
1501 | |
1502 /* | |
1503 * add query parameters | |
1504 */ | |
1505 queryEntities.setString("systemStatus", systemStatus); | |
1506 if (StringUtils.isNotEmpty(ownValue)) { | |
1507 if (ownValueSubString) { | |
1508 // substring search | |
1509 queryEntities.setString("ownValue", "%" + ownValue + "%"); | |
1510 //logger.debug("ownValue=%" + ownValue + "%"); | |
1511 } else { | |
1512 queryEntities.setString("ownValue", ownValue); | |
1513 //logger.debug("ownValue=" + ownValue); | |
1514 } | |
1515 } | |
1516 if (maxResult > 0) { | |
1517 queryEntities.setMaxResults(maxResult); | |
1518 } | |
1 | 1519 |
39 | 1520 if (StringUtils.isNotEmpty(type)) { |
1521 queryEntities.setString("type", type); | |
1522 } | |
1523 if (id != null) { | |
1524 queryEntities.setLong("id", id); | |
1525 } | |
1526 if (StringUtils.isNotEmpty(objectClass)) { | |
1527 queryEntities.setString("objectClass", objectClass); | |
1528 } | |
1529 | |
1530 /* | |
1531 * run query and return results | |
1532 */ | |
1533 List<Entity> entities = queryEntities.list(); | |
1 | 1534 |
39 | 1535 for (Entity ent : entities) { |
1536 ent.setLightweight(true); | |
1537 } | |
1538 | |
1539 return entities; | |
1540 } | |
1 | 1541 |
39 | 1542 /** |
1543 * Return all (lightweight) entities matching the query. | |
1544 * | |
1545 * Any parameter that is null is omitted as a query condition matching | |
1546 * all values in this parameter. | |
1547 * | |
1548 * Own value is searched as substring if ownValueSubString=true. | |
1549 * | |
1550 * @param systemStatus | |
1551 * @param id | |
1552 * @param type | |
1553 * @param objectClass | |
1554 * @param ownValue | |
1555 * @param ownValueSubString | |
1556 * @param maxResult | |
1557 * @return | |
1558 */ | |
1559 public List<Entity> getLightweightEntities(String systemStatus, Long id, String type, String objectClass, | |
1560 String ownValue, boolean ownValueSubString, int maxResult) { | |
1561 logger.debug("GET LW ENTITIES [type=" + type + " id=" + id + ", objectClass=" + objectClass + ", ownValue=" | |
1562 + ownValue + "]"); | |
1 | 1563 |
39 | 1564 List<Entity> entities = new ArrayList<Entity>(); |
1565 /* | |
1566 * check inputs | |
1567 */ | |
1568 if (!(systemStatus.equals(Node.SYS_STATUS_PREVIOUS_VERSION) | |
1569 || systemStatus.equals(Node.SYS_STATUS_CURRENT_VERSION))) { | |
1570 throw new IllegalArgumentException("Invalid input systemStatus: " + systemStatus); | |
1571 } | |
1572 if (StringUtils.isNotEmpty(type) && !(type.equals(Node.TYPE_ABOX) || type.equals(Node.TYPE_TBOX))) { | |
1573 throw new IllegalArgumentException("Invalid input type: " + type); | |
1574 } | |
1575 | |
1576 /* | |
1577 * call getLightweightEntities with transaction | |
1578 */ | |
1579 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
1580 try { | |
1581 session.getTransaction().begin(); | |
1582 | |
1583 entities = this.getLightweightEntities(session, systemStatus, id, type, objectClass, ownValue, | |
1584 ownValueSubString, maxResult); | |
1585 | |
1586 } catch (Exception e) { | |
1587 logger.error(e); | |
1588 } finally { | |
1589 session.getTransaction().commit(); | |
1590 } | |
1591 return entities; | |
1592 } | |
1 | 1593 |
1594 private Sequence getIdSequence(Session session) { | |
1595 Sequence sequence = null; | |
1596 String hqlJoin = "from Sequence where name = :name"; | |
1597 Query query = session.createQuery(hqlJoin); | |
1598 query.setString("name", NODE_SEQUENCE); | |
1599 List<Sequence> sequences = query.list(); | |
1600 if (sequences.size() > 0) | |
1601 sequence = sequences.get(0); | |
1602 if (sequence == null) { | |
1603 sequence = new Sequence(NODE_SEQUENCE, new Long(0)); | |
1604 } | |
1605 return sequence; | |
1606 } | |
1607 | |
1608 protected Long generateId(Session session) { | |
1609 Long id = null; | |
1610 Sequence sequence = null; | |
1611 String hqlJoin = "from Sequence where name = :name"; | |
1612 Query query = session.createQuery(hqlJoin); | |
1613 query.setString("name", NODE_SEQUENCE); | |
1614 List<Sequence> sequences = query.list(); | |
1615 if (sequences.size() > 0) { | |
1616 sequence = sequences.get(0); | |
1617 } else { | |
1618 sequence = new Sequence(NODE_SEQUENCE, new Long(0)); | |
1619 } | |
1620 id = sequence.generateId(); | |
1621 session.save(sequence); | |
1622 return id; | |
1623 } | |
1624 | |
1625 public ConfigurationService getConfigurationService() { | |
1626 return configurationService; | |
1627 } | |
1628 | |
1629 public void setConfigurationService( | |
1630 ConfigurationService configurationService) { | |
1631 this.configurationService = configurationService; | |
1632 } | |
1633 | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1634 public boolean isImportMode() { |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1635 return importMode; |
1 | 1636 } |
1637 | |
1638 public void setImportModus(boolean importModus) { | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1639 this.importMode = importModus; |
1 | 1640 } |
1641 | |
38 | 1642 /** |
1643 * Returns all definition relations. | |
1644 * | |
1645 * @return | |
1646 * @throws Exception | |
1647 */ | |
1 | 1648 public List<Relation> getDefRelations() { |
1649 List<Relation> list = new ArrayList<Relation>(); | |
1650 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
1651 try { | |
1652 session.getTransaction().begin(); | |
1653 | |
1654 String hql = "from Relation where systemStatus = :systemStatus and type = :type"; | |
1655 Query query = session.createQuery(hql); | |
1656 | |
1657 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
1658 query.setString("type", Node.TYPE_TBOX); | |
1659 | |
1660 list = query.list(); | |
1661 } catch (Exception e) { | |
38 | 1662 logger.error(e); |
1 | 1663 } finally { |
1664 session.getTransaction().commit(); | |
1665 } | |
1666 return list; | |
1667 } | |
1668 | |
38 | 1669 /** |
1670 * Returns all definition attributes. | |
1671 * | |
1672 * @return | |
1673 * @throws Exception | |
1674 */ | |
1 | 1675 public List<Attribute> getDefAttributes() { |
1676 List<Attribute> list = new ArrayList<Attribute>(); | |
1677 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
1678 try { | |
1679 session.getTransaction().begin(); | |
1680 | |
1681 String hql = "from Attribute where systemStatus = :systemStatus and type = :type"; | |
1682 Query query = session.createQuery(hql); | |
1683 | |
1684 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
1685 query.setString("type", Node.TYPE_TBOX); | |
1686 | |
1687 list = query.list(); | |
1688 | |
1689 } catch (Exception e) { | |
38 | 1690 logger.error(e); |
1 | 1691 } finally { |
1692 session.getTransaction().commit(); | |
1693 } | |
1694 return list; | |
1695 } | |
1696 | |
38 | 1697 /** |
1698 * Returns all (lightweight) definition entities. | |
1699 * | |
1700 * @return | |
1701 * @throws Exception | |
1702 */ | |
1703 public List<Entity> getLWDefinitions() throws Exception { | |
1704 List<Entity> list = new ArrayList<Entity>(); | |
1705 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
1706 try { | |
1707 session.getTransaction().begin(); | |
1 | 1708 |
38 | 1709 String hql = "from Entity where systemStatus = :systemStatus and type = :type"; |
1710 Query query = session.createQuery(hql); | |
1 | 1711 |
38 | 1712 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); |
1713 query.setString("type", Node.TYPE_TBOX); | |
1 | 1714 |
38 | 1715 list = query.list(); |
1716 for (Entity def : list) { | |
1717 def.setLightweight(true); | |
1718 } | |
1719 } catch (Exception e) { | |
1720 logger.error(e); | |
1721 } finally { | |
1722 session.getTransaction().commit(); | |
1723 } | |
1 | 1724 |
38 | 1725 return list; |
1726 } | |
1 | 1727 |
1728 // ################################################################# | |
1729 // ################################################################# | |
1730 // ################################################################# | |
1731 | |
1732 public Long getEntityCount(String objectClass) { | |
1733 Long count = null; | |
1734 try { | |
1735 Session session = HibernateUtil.getSessionFactory() | |
1736 .getCurrentSession(); | |
1737 session.getTransaction().begin(); | |
1738 | |
1739 String hql = "select count(*) from Entity where "; | |
1740 | |
1741 if (StringUtils.isNotEmpty(objectClass)) { | |
1742 hql += "objectClass = :objectClass AND "; | |
1743 } else { | |
1744 hql += "objectClass != :objectClass AND "; | |
1745 } | |
1746 hql += "systemStatus = :systemStatus "; | |
1747 | |
1748 Query query = session.createQuery(hql); | |
1749 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
1750 if (StringUtils.isNotEmpty(objectClass)) { | |
1751 query.setString("objectClass", objectClass); | |
1752 } else { | |
1753 query.setString("objectClass", Node.TYPE_TBOX); | |
1754 } | |
1755 count = (Long) query.uniqueResult(); | |
1756 | |
1757 session.getTransaction().commit(); | |
1758 } catch (Exception e) { | |
1759 logger.error(e.getMessage(), e); | |
1760 } | |
1761 return count; | |
1762 } | |
1763 | |
1764 /** | |
1765 * | |
1766 * @param objectClass | |
1767 * if it is null, it will be returned all entities (no | |
1768 * definition). To get the definitions objectClass should be: | |
1769 * Node.TYPE_TBOX | |
1770 * @param startRecord | |
1771 * @param endRecord | |
1772 * @return | |
1773 */ | |
1774 public List<Entity> getEntityPage(String objectClass, | |
1775 final int startRecord, final int endRecord) { | |
1776 | |
1777 List<Entity> entities = new ArrayList<Entity>(); | |
1778 try { | |
1779 Session session = HibernateUtil.getSessionFactory() | |
1780 .getCurrentSession(); | |
1781 session.getTransaction().begin(); | |
1782 | |
1783 String hql = "from Entity where "; | |
1784 | |
1785 if (StringUtils.isNotEmpty(objectClass)) { | |
1786 hql += "objectClass = :objectClass AND "; | |
1787 } else { | |
1788 hql += "objectClass != :objectClass AND "; | |
1789 } | |
1790 | |
1791 hql += "systemStatus = :systemStatus order by ownValue"; | |
1792 | |
1793 Query query = session.createQuery(hql); | |
1794 query.setFirstResult(startRecord); | |
1795 query.setMaxResults(endRecord); | |
1796 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
1797 if (StringUtils.isNotEmpty(objectClass)) { | |
1798 query.setString("objectClass", objectClass); | |
1799 } else { | |
1800 query.setString("objectClass", Node.TYPE_TBOX); | |
1801 } | |
1802 entities = query.list(); | |
1803 | |
1804 session.getTransaction().commit(); | |
1805 } catch (Exception e) { | |
1806 logger.error(e.getMessage(), e); | |
1807 // e.printStackTrace(); | |
1808 } | |
1809 return entities; | |
1810 } | |
1811 | |
1812 public static void main(String[] args) { | |
1813 /* | |
1814 * ServiceRegistry sr = new ServiceRegistry(); Long count = | |
1815 * sr.getPersistenceService().getEntityCount("CODEX"); | |
1816 * logger.info("count codex " + count); | |
1817 * | |
1818 * List<Entity> list = sr.getPersistenceService().getEntityPage("CODEX", | |
1819 * count.intValue() - 100, count.intValue() - 50); for(Entity e : list){ | |
1820 * logger.info(e.toString()); } | |
1821 */ | |
1822 } | |
1823 | |
1824 } |