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