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