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