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