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