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