Mercurial > hg > openmind
annotate src/main/java/org/mpi/openmind/repository/services/PersistenceService.java @ 22:165b1efb85cd
remove group-by clause for attribute search filter because it gave hibernate errors :-(
author | casties |
---|---|
date | Mon, 27 Jun 2016 19:09:37 -0400 |
parents | 615d27dce9b3 |
children | 5e24413d355b |
rev | line source |
---|---|
1 | 1 package org.mpi.openmind.repository.services; |
2 | |
3 import java.sql.Connection; | |
4 import java.sql.ResultSet; | |
5 import java.sql.Statement; | |
6 import java.util.ArrayList; | |
7 import java.util.Collections; | |
8 import java.util.HashMap; | |
9 import java.util.List; | |
10 import java.util.Map; | |
11 | |
12 import org.apache.commons.lang.StringUtils; | |
13 import org.apache.log4j.Logger; | |
14 import org.hibernate.Query; | |
15 import org.hibernate.Session; | |
16 import org.mpi.openmind.repository.bo.Attribute; | |
17 import org.mpi.openmind.repository.bo.Entity; | |
18 import org.mpi.openmind.repository.bo.Node; | |
19 import org.mpi.openmind.repository.bo.Relation; | |
20 import org.mpi.openmind.repository.bo.ViewerAttribute; | |
21 import org.mpi.openmind.repository.bo.ViewerPage; | |
22 import org.mpi.openmind.repository.bo.utils.RelationSortBySourceOW; | |
23 import org.mpi.openmind.repository.bo.utils.RelationSortByTargetOW; | |
24 import org.mpi.openmind.repository.services.utils.AttributeFilter; | |
25 import org.mpi.openmind.repository.services.utils.RelationFilter; | |
26 import org.mpi.openmind.repository.services.utils.RelationResultEntry; | |
27 import org.mpi.openmind.repository.utils.HibernateUtil; | |
28 import org.mpi.openmind.repository.utils.JDBCUtils; | |
29 | |
30 /** | |
31 * | |
32 * @author jurzua | |
33 */ | |
34 public class PersistenceService extends AbstractPersistenceService { | |
35 | |
36 private static Logger logger = Logger.getLogger(PersistenceService.class); | |
37 public static String GROUP_BY_SOURCE = "group_by_source"; | |
38 public static String GROUP_BY_TARGET = "group_by_target"; | |
39 public static String GROUP_BY_RELATION = "group_by_relation"; | |
40 | |
41 public Long getSourceRelationsCount(Entity entity, String relationName, | |
42 String tarObjClass) { | |
43 Long count = null; | |
44 try { | |
45 Session session = HibernateUtil.getSessionFactory() | |
46 .getCurrentSession(); | |
47 session.getTransaction().begin(); | |
48 | |
49 String hql = "select count(*) from Relation rel " + "where " | |
50 + "rel.sourceId = :id AND " | |
51 + "rel.sourceModif = :modif AND " | |
52 + "rel.ownValue = :relationName AND " | |
53 + "rel.systemStatus = :systemStatus "; | |
54 | |
55 if (StringUtils.isNotEmpty(tarObjClass)) { | |
56 hql += " AND rel.targetObjectClass = :tarObjClass "; | |
57 } | |
58 | |
59 Query query = session.createQuery(hql); | |
60 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
61 query.setLong("id", entity.getId()); | |
62 query.setLong("modif", entity.getModificationTime()); | |
63 query.setString("relationName", relationName); | |
64 if (StringUtils.isNotEmpty(tarObjClass)) { | |
65 query.setString("tarObjClass", tarObjClass); | |
66 } | |
67 | |
68 count = (Long) query.uniqueResult(); | |
69 | |
70 session.getTransaction().commit(); | |
71 } catch (Exception e) { | |
72 logger.error(e.getMessage()); | |
73 e.printStackTrace(); | |
74 } | |
75 return count; | |
76 } | |
77 | |
78 public List<Relation> getRelationBySrcOCFromDB(String srcOC, int maxResult) | |
79 throws Exception { | |
80 List<Relation> relations = new ArrayList<Relation>(); | |
81 long start = System.currentTimeMillis(); | |
82 | |
83 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
84 session.getTransaction().begin(); | |
85 | |
86 String hql = "from Relation rel " + "where " | |
87 + "rel.systemStatus = :systemStatus AND " | |
88 + "rel.sourceObjectClass = :srcOC "; | |
89 | |
90 Query query = session.createQuery(hql); | |
91 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
92 query.setString("srcOC", srcOC); | |
93 | |
94 if (maxResult > 0) { | |
95 query.setMaxResults(maxResult); | |
96 } | |
97 | |
98 List<Relation> list = query.list(); | |
99 | |
100 // TODO loading attributes | |
101 /* | |
102 * for(Relation rel : relations){ List<Node> attrs = | |
103 * this.getNodes(session, null, rel.getSystemStatus(), rel.getId(), | |
104 * rel.getModificationTime(), null, null); for(Node attNode : attrs){ | |
105 * if(attNode instanceof Attribute){ | |
106 * rel.addAttribute((Attribute)attNode); } } } | |
107 */ | |
108 | |
109 session.getTransaction().commit(); | |
110 | |
111 logger.info("getRelationBySrcOC [" + srcOC + "] - execution time[ms]: " | |
112 + (System.currentTimeMillis() - start)); | |
113 return relations; | |
114 } | |
115 | |
116 public List<Relation> getRelationByTarOCFromDB(String tarOC, int maxResult) { | |
117 List<Relation> relations = new ArrayList<Relation>(); | |
118 long start = System.currentTimeMillis(); | |
119 | |
120 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
121 session.getTransaction().begin(); | |
122 | |
123 String hql = "from Relation rel " + "where " | |
124 + "rel.systemStatus = :systemStatus AND " | |
125 + "rel.targetObjectClass = :tarOC "; | |
126 | |
127 Query query = session.createQuery(hql); | |
128 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
129 query.setString("tarOC", tarOC); | |
130 | |
131 if (maxResult > 0) { | |
132 query.setMaxResults(maxResult); | |
133 } | |
134 | |
135 List<Relation> list = query.list(); | |
136 | |
137 // TODO loading attributes | |
138 /* | |
139 * for(Relation rel : relations){ List<Node> attrs = | |
140 * this.getNodes(session, null, rel.getSystemStatus(), rel.getId(), | |
141 * rel.getModificationTime(), null, null); for(Node attNode : attrs){ | |
142 * if(attNode instanceof Attribute){ | |
143 * rel.addAttribute((Attribute)attNode); } } } | |
144 */ | |
145 | |
146 session.getTransaction().commit(); | |
147 | |
148 logger.info("getRelationByTarOC [" + tarOC + "] - execution time[ms]: " | |
149 + (System.currentTimeMillis() - start)); | |
150 return relations; | |
151 } | |
152 | |
153 /** | |
154 * Returns relation by name, where the parameter name is not substring. | |
155 * | |
156 * @param name | |
157 * @param maxResult | |
158 * @return | |
159 */ | |
160 public List<Relation> getRelationByNameDB(String name, int maxResult) | |
161 throws Exception { | |
162 List<Relation> relations = new ArrayList<Relation>(); | |
163 long start = System.currentTimeMillis(); | |
164 | |
165 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
166 session.getTransaction().begin(); | |
167 | |
168 String hql = "from Relation rel " + "where " | |
169 + "rel.systemStatus = :systemStatus " | |
170 + " AND rel.ownValue = :name "; | |
171 | |
172 Query query = session.createQuery(hql); | |
173 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
174 query.setString("name", name); | |
175 | |
176 if (maxResult > 0) { | |
177 query.setMaxResults(maxResult); | |
178 } | |
179 | |
180 relations = query.list(); | |
181 | |
182 // loading attributes | |
183 /* | |
184 * for(Relation rel : relations){ List<Node> attrs = | |
185 * this.getNodes(session, null, rel.getSystemStatus(), rel.getId(), | |
186 * rel.getModificationTime(), null, null); for(Node attNode : attrs){ | |
187 * if(attNode instanceof Attribute){ | |
188 * rel.addAttribute((Attribute)attNode); } } } | |
189 */ | |
190 | |
191 session.getTransaction().commit(); | |
192 | |
193 logger.debug("getRelationByName - execution time[ms]: " | |
194 + (System.currentTimeMillis() - start)); | |
195 return relations; | |
196 } | |
197 | |
198 public List<Relation> getSourceRelationsJDBCFromDB(Entity entity, | |
199 String relationName, String tarObjClass, int maxResult, | |
200 boolean includeEntities) { | |
201 | |
202 List<Relation> relations = null; | |
203 long start = System.currentTimeMillis(); | |
204 try { | |
205 | |
206 Connection conn = JDBCUtils.getConn(); | |
207 | |
208 String sql = "select * from node " + "where " + "source_id = '" | |
209 + entity.getId() + "' AND " + "source_modif = '" | |
210 + entity.getModificationTime() + "' AND " | |
211 + "system_status = '" + Node.SYS_STATUS_CURRENT_VERSION | |
212 + "' "; | |
213 | |
214 if (StringUtils.isNotEmpty(relationName)) { | |
215 sql += " AND own_value = '" + relationName + "' "; | |
216 } | |
217 if (StringUtils.isNotEmpty(tarObjClass)) { | |
218 sql += " AND target_obj_class = '" + tarObjClass + "' "; | |
219 } | |
220 | |
221 // logger.info(sql); | |
222 Statement st = conn.createStatement(); | |
223 ResultSet rs = st.executeQuery(sql); | |
224 relations = JDBCUtils.rs2Rels(rs); | |
225 | |
226 conn.close(); | |
227 | |
228 } catch (Exception e) { | |
229 logger.error(e.getMessage(), e); | |
230 e.printStackTrace(); | |
231 } | |
232 | |
233 logger.debug("getSourceRelations JDBC - execution time[ms]: " | |
234 + (System.currentTimeMillis() - start)); | |
235 return relations; | |
236 } | |
237 | |
238 public List<Relation> getSourceRelationsFromDB(Entity entity, | |
239 String relationName, String tarObjClass, int maxResult, | |
240 boolean includeEntities) throws Exception { | |
241 | |
242 List<Relation> relations = new ArrayList<Relation>(); | |
243 long start = System.currentTimeMillis(); | |
244 | |
245 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
246 session.getTransaction().begin(); | |
247 | |
248 String hql = "from Relation rel " + "where " | |
249 + "rel.sourceId = :id AND " | |
250 + "rel.sourceModif = :modif AND " // TODO: previous versions are same without considering the modif | |
251 + "rel.systemStatus = :systemStatus "; | |
252 | |
253 if(StringUtils.equals(entity.getSystemStatus(), Node.SYS_STATUS_PREVIOUS_VERSION)){ | |
254 hql += " AND rel.sourceModif = :modif "; | |
255 } | |
256 if (StringUtils.isNotEmpty(relationName)) { | |
257 hql += " AND rel.ownValue = :relationName "; | |
258 } | |
259 if (StringUtils.isNotEmpty(tarObjClass)) { | |
260 hql += " AND rel.targetObjectClass = :tarObjClass "; | |
261 } | |
262 | |
263 Query query = session.createQuery(hql); | |
264 query.setLong("id", entity.getId()); | |
265 query.setLong("modif", entity.getModificationTime()); | |
266 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
267 | |
268 | |
269 //if(StringUtils.equals(entity.getSystemStatus(), Node.SYS_STATUS_PREVIOUS_VERSION)){ | |
270 // query.setLong("modif", entity.getModificationTime()); | |
271 //} | |
272 if (StringUtils.isNotEmpty(relationName)) { | |
273 query.setString("relationName", relationName); | |
274 } | |
275 if (StringUtils.isNotEmpty(tarObjClass)) { | |
276 query.setString("tarObjClass", tarObjClass); | |
277 } | |
278 if (maxResult > 0) { | |
279 query.setMaxResults(maxResult); | |
280 } | |
281 | |
282 List<Relation> list = query.list(); | |
283 if (includeEntities) { | |
284 for (Relation rel : list) { | |
285 List<Entity> tarList = getLightweightEntities(session, | |
286 Node.SYS_STATUS_CURRENT_VERSION, rel.getTargetId(), | |
287 null, null, null, false, 1); | |
288 if (tarList.size() > 0) { | |
289 rel.setSource(entity); | |
290 rel.setTarget(tarList.get(0)); | |
291 relations.add(rel); | |
292 } | |
293 } | |
294 | |
295 Collections.sort(relations, new RelationSortByTargetOW()); | |
296 } else { | |
297 relations = list; | |
298 } | |
299 | |
300 // loading attributes | |
301 for (Relation rel : relations) { | |
302 List<Node> attrs = this.getNodes(session, null, | |
303 rel.getSystemStatus(), rel.getId(), | |
304 rel.getModificationTime(), null, null); | |
305 for (Node attNode : attrs) { | |
306 if (attNode instanceof Attribute) { | |
307 rel.addAttribute((Attribute) attNode); | |
308 } | |
309 } | |
310 } | |
311 | |
312 session.getTransaction().commit(); | |
313 | |
314 logger.debug("getSourceRelations - execution time[ms]: " | |
315 + (System.currentTimeMillis() - start)); | |
316 return relations; | |
317 } | |
318 | |
319 /** | |
320 * <p> | |
321 * Returns the relations directly for the given entity from the DB, which | |
322 * conforms the given input. | |
323 * </p> | |
324 * <p> | |
325 * Only the variable tarObjClass can be null. | |
326 * </p> | |
327 * <p> | |
328 * If its expected all existing element from the DB, the variable maxResult | |
329 * should be -1, otherwise this value limits the returned result-set. | |
330 * </p> | |
331 * | |
332 * @param entity | |
333 * @param relationName | |
334 * @param tarObjClass | |
335 * @param maxResult | |
336 * @return | |
337 */ | |
338 public List<Relation> getSourceRelationsError(Entity entity, | |
339 String relationName, String tarObjClass, int maxResult) | |
340 throws Exception { | |
341 | |
342 List<Relation> relations = new ArrayList<Relation>(); | |
343 | |
344 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
345 session.getTransaction().begin(); | |
346 | |
347 String hql = "from Relation rel, Entity tar " + "where " | |
348 + "rel.sourceId = :id AND " + "rel.sourceModif = :mod AND " | |
349 + "rel.ownValue = :relationName AND " | |
350 + "rel.systemStatus = :systemStatus AND " | |
351 + "tar.systemStatus = :systemStatus AND " | |
352 + "tar.id = rel.targetId "; | |
353 | |
354 if (StringUtils.isNotEmpty(tarObjClass)) { | |
355 hql += " AND rel.targetObjectClass = :tarObjClass "; | |
356 } | |
357 | |
358 hql += " order by tar.ownValue "; | |
359 | |
360 Query query = session.createQuery(hql); | |
361 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
362 query.setLong("id", entity.getId()); | |
363 query.setLong("mod", entity.getModificationTime()); | |
364 query.setString("relationName", relationName); | |
365 if (StringUtils.isNotEmpty(tarObjClass)) { | |
366 query.setString("tarObjClass", tarObjClass); | |
367 } | |
368 if (maxResult > 0) { | |
369 query.setMaxResults(maxResult); | |
370 } | |
371 | |
372 List<Object> list = query.list(); | |
373 | |
374 for (Object o : list) { | |
375 Object[] array = (Object[]) o; | |
376 if (array.length > 0) { | |
377 Relation rel = (Relation) array[0]; | |
378 Entity tar = (Entity) array[1]; | |
379 tar.setLightweight(true); | |
380 rel.setSource(entity); | |
381 rel.setTarget(tar); | |
382 relations.add(rel); | |
383 } | |
384 } | |
385 session.getTransaction().commit(); | |
386 | |
387 return relations; | |
388 } | |
389 | |
390 public Long getTargetRelationsCount(Entity entity, String relationName, | |
391 String srcObjClass) { | |
392 Long count = null; | |
393 try { | |
394 Session session = HibernateUtil.getSessionFactory() | |
395 .getCurrentSession(); | |
396 session.getTransaction().begin(); | |
397 | |
398 String hql = "select count(*) from Relation rel " + "where " | |
399 + "rel.targetId = :id AND " | |
400 + "rel.targetModif = :modif AND " | |
401 + "rel.ownValue = :relationName AND " | |
402 + "rel.systemStatus = :systemStatus "; | |
403 | |
404 if (StringUtils.isNotEmpty(srcObjClass)) { | |
405 hql += " AND rel.sourceObjectClass = :srcObjClass "; | |
406 } | |
407 | |
408 Query query = session.createQuery(hql); | |
409 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
410 query.setLong("id", entity.getId()); | |
411 query.setLong("modif", entity.getModificationTime()); | |
412 if (StringUtils.isNotEmpty(relationName)) { | |
413 query.setString("relationName", relationName); | |
414 } | |
415 if (StringUtils.isNotEmpty(srcObjClass)) { | |
416 query.setString("srcObjClass", srcObjClass); | |
417 } | |
418 | |
419 count = (Long) query.uniqueResult(); | |
420 | |
421 session.getTransaction().commit(); | |
422 } catch (Exception e) { | |
423 logger.error(e.getMessage()); | |
424 e.printStackTrace(); | |
425 } | |
426 return count; | |
427 } | |
428 | |
429 public List<Relation> getTargetRelationsJDBCFromDB(Entity entity, | |
430 String relationName, String srcObjClass, int maxResult, | |
431 boolean includeEntities) { | |
432 | |
433 List<Relation> relations = null; | |
434 long start = System.currentTimeMillis(); | |
435 try { | |
436 | |
437 Connection conn = JDBCUtils.getConn(); | |
438 | |
439 String sql = "select * from node " + "where " + "target_id = '" | |
440 + entity.getId() + "' AND " + "target_modif = '" | |
441 + entity.getModificationTime() + "' AND " | |
442 + "system_status = '" + Node.SYS_STATUS_CURRENT_VERSION | |
443 + "' "; | |
444 | |
445 if (StringUtils.isNotEmpty(relationName)) { | |
446 sql += " AND own_value = '" + relationName + "' "; | |
447 } | |
448 if (StringUtils.isNotEmpty(srcObjClass)) { | |
449 sql += " AND source_obj_class = '" + srcObjClass + "' "; | |
450 } | |
451 | |
452 // logger.info(sql); | |
453 Statement st = conn.createStatement(); | |
454 ResultSet rs = st.executeQuery(sql); | |
455 relations = JDBCUtils.rs2Rels(rs); | |
456 | |
457 conn.close(); | |
458 } catch (Exception e) { | |
459 logger.error(e.getMessage(), e); | |
460 e.printStackTrace(); | |
461 } | |
462 | |
463 logger.debug("GetTargetRelations JDBC - execution time[ms]: " | |
464 + (System.currentTimeMillis() - start)); | |
465 return relations; | |
466 } | |
467 | |
468 public List<Relation> getTargetRelationsFromDB(Entity entity, | |
469 String relationName, String srcObjClass, int maxResult, | |
470 boolean includeEntities) { | |
471 List<Relation> relations = new ArrayList<Relation>(); | |
472 long start = System.currentTimeMillis(); | |
473 try { | |
474 | |
475 Session session = HibernateUtil.getSessionFactory() | |
476 .getCurrentSession(); | |
477 session.getTransaction().begin(); | |
478 | |
479 String hql = "from Relation rel " + "where " | |
480 + "rel.targetId = :id AND " | |
481 + "rel.targetModif = :modif AND " | |
482 + "rel.systemStatus = :systemStatus "; | |
483 | |
484 if(StringUtils.equals(entity.getSystemStatus(), Node.SYS_STATUS_PREVIOUS_VERSION)){ | |
485 hql += " AND rel.targetModif = :modif "; | |
486 } | |
487 if (StringUtils.isNotEmpty(relationName)) { | |
488 hql += " AND rel.ownValue = :relationName "; | |
489 } | |
490 if (StringUtils.isNotEmpty(srcObjClass)) { | |
491 hql += " AND rel.sourceObjectClass = :srcObjClass "; | |
492 } | |
493 | |
494 Query query = session.createQuery(hql); | |
495 query.setLong("id", entity.getId()); | |
496 query.setLong("modif", entity.getModificationTime()); | |
497 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
498 | |
499 //if(StringUtils.equals(entity.getSystemStatus(), Node.SYS_STATUS_PREVIOUS_VERSION)){ | |
500 // query.setLong("modif", entity.getModificationTime()); | |
501 //} | |
502 if (StringUtils.isNotEmpty(relationName)) { | |
503 query.setString("relationName", relationName); | |
504 } | |
505 if (StringUtils.isNotEmpty(srcObjClass)) { | |
506 query.setString("srcObjClass", srcObjClass); | |
507 } | |
508 if (maxResult > 0) { | |
509 query.setMaxResults(maxResult); | |
510 } | |
511 | |
512 List<Relation> list = query.list(); | |
513 if (includeEntities) { | |
514 for (Relation rel : list) { | |
515 List<Entity> srcList = getLightweightEntities(session, | |
516 Node.SYS_STATUS_CURRENT_VERSION, rel.getSourceId(), | |
517 null, null, null, false, 1); | |
518 if (srcList.size() > 0) { | |
519 rel.setSource(srcList.get(0)); | |
520 rel.setTarget(entity); | |
521 relations.add(rel); | |
522 } | |
523 } | |
524 | |
525 Collections.sort(relations, new RelationSortBySourceOW()); | |
526 } else { | |
527 relations = list; | |
528 } | |
529 // loading attributes | |
530 for (Relation rel : relations) { | |
531 List<Node> attrs = this.getNodes(session, null, | |
532 rel.getSystemStatus(), rel.getId(), | |
533 rel.getModificationTime(), null, null); | |
534 for (Node attNode : attrs) { | |
535 if (attNode instanceof Attribute) { | |
536 rel.addAttribute((Attribute) attNode); | |
537 } | |
538 } | |
539 } | |
540 | |
541 session.getTransaction().commit(); | |
542 | |
543 } catch (Exception e) { | |
544 logger.error(e.getMessage(), e); | |
545 e.printStackTrace(); | |
546 } | |
547 long end = System.currentTimeMillis(); | |
548 if (end - start > 500) { | |
549 logger.debug("GetTargetRelations - execution time[ms]: " | |
550 + (end - start)); | |
551 } | |
552 return relations; | |
553 } | |
554 | |
555 /** | |
556 * <p> | |
557 * Returns the relations directly for the given entity from the DB, which | |
558 * conforms the given input. | |
559 * </p> | |
560 * <p> | |
561 * Only the variable srcObjClass can be null. | |
562 * </p> | |
563 * <p> | |
564 * If every existing element from the DB is expected, the variable maxResult | |
565 * should be -1, otherwise this value limits the returned result-set. | |
566 * </p> | |
567 * | |
568 * @param entity | |
569 * @param relationName | |
570 * @param tarObjClass | |
571 * @param maxResult | |
572 * @return | |
573 */ | |
574 public List<Relation> getTargetRelationsError(Entity entity, | |
575 String relationName, String srcObjClass, int maxResult) { | |
576 List<Relation> relations = new ArrayList<Relation>(); | |
577 try { | |
578 logger.debug("### get Target Relations ###"); | |
579 | |
580 Session session = HibernateUtil.getSessionFactory() | |
581 .getCurrentSession(); | |
582 session.getTransaction().begin(); | |
583 | |
584 String hql = "from Relation rel, Entity src " + "where " | |
585 + "rel.targetId = :id AND " + "rel.targetModif = :mod AND " | |
586 + "rel.ownValue = :relationName AND " | |
587 + "rel.systemStatus = :systemStatus AND " | |
588 + "src.systemStatus = :systemStatus AND " | |
589 + "src.id = rel.sourceId "; | |
590 | |
591 if (StringUtils.isNotEmpty(srcObjClass)) { | |
592 hql += " AND rel.sourceObjectClass = :srcObjClass "; | |
593 } | |
594 | |
595 hql += " order by src.ownValue "; | |
596 | |
597 Query query = session.createQuery(hql); | |
598 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
599 query.setLong("id", entity.getId()); | |
600 query.setLong("mod", entity.getModificationTime()); | |
601 query.setString("relationName", relationName); | |
602 if (StringUtils.isNotEmpty(srcObjClass)) { | |
603 query.setString("srcObjClass", srcObjClass); | |
604 } | |
605 if (maxResult > 0) { | |
606 query.setMaxResults(maxResult); | |
607 } | |
608 | |
609 List<Object> list = query.list(); | |
610 | |
611 for (Object o : list) { | |
612 Object[] array = (Object[]) o; | |
613 if (array.length > 0) { | |
614 Relation rel = (Relation) array[0]; | |
615 Entity src = (Entity) array[1]; | |
616 src.setLightweight(true); | |
617 rel.setSource(src); | |
618 rel.setTarget(entity); | |
619 relations.add(rel); | |
620 } | |
621 } | |
622 | |
623 session.getTransaction().commit(); | |
624 | |
625 } catch (Exception e) { | |
626 logger.error(e.getMessage()); | |
627 e.printStackTrace(); | |
628 } | |
629 return relations; | |
630 } | |
631 | |
632 /** | |
633 * <p> | |
634 * Returns the entities of class "tarObjClass" directly from the DB, which | |
635 * are connected to the given entity by a relation of name "relationName". | |
636 * </p> | |
637 * <p> | |
638 * Only the variable tarObjClass can be null. | |
639 * </p> | |
640 * <p> | |
641 * If its expected all existing element from the DB, the variable maxResult | |
642 * should be -1, otherwise this value limits the returned result-set. | |
643 * </p> | |
644 * | |
645 * @param entity | |
646 * @param relationName | |
647 * @param tarObjClass | |
648 * @param maxResult | |
649 * @return | |
650 */ | |
651 public List<Entity> getTargetsForSourceRelation(Entity entity, | |
652 String relationName, String tarObjClass, int maxResult) { | |
653 List<Entity> entities = new ArrayList<Entity>(); | |
654 try { | |
655 logger.debug("### get Targets For Source Relation ###"); | |
656 | |
657 Session session = HibernateUtil.getSessionFactory() | |
658 .getCurrentSession(); | |
659 session.getTransaction().begin(); | |
660 Long start = System.currentTimeMillis(); | |
661 | |
662 String hql = "from Relation rel, Entity tar " + "where " | |
663 + "rel.sourceId = :id AND " + "rel.sourceModif = :mod AND " | |
664 + "rel.ownValue = :relationName AND " | |
665 + "rel.systemStatus = :systemStatus AND " | |
666 + "tar.systemStatus = :systemStatus AND " | |
667 + "tar.id = rel.targetId "; | |
668 | |
669 if (StringUtils.isNotEmpty(tarObjClass)) { | |
670 hql += " AND rel.targetObjectClass = :tarObjClass "; | |
671 } | |
672 | |
673 hql += " order by tar.ownValue "; | |
674 | |
675 Query query = session.createQuery(hql); | |
676 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
677 query.setLong("id", entity.getId()); | |
678 query.setLong("mod", entity.getModificationTime()); | |
679 query.setString("relationName", relationName); | |
680 if (StringUtils.isNotEmpty(tarObjClass)) { | |
681 query.setString("tarObjClass", tarObjClass); | |
682 } | |
683 if (maxResult > 0) { | |
684 query.setMaxResults(maxResult); | |
685 } | |
686 | |
687 List<Object> list = query.list(); | |
688 | |
689 for (Object o : list) { | |
690 Object[] array = (Object[]) o; | |
691 if (array.length > 0) { | |
692 Entity ent = (Entity) array[1]; | |
693 ent.setLightweight(true); | |
694 entities.add(ent); | |
695 } | |
696 } | |
697 | |
698 session.getTransaction().commit(); | |
699 Long end = System.currentTimeMillis(); | |
700 Long diff = end - start; | |
701 logger.debug("Target For SourceRelation - Time execution: " + diff | |
702 / (60 * 60 * 1000) + "[hours] " + diff / (60 * 1000) | |
703 + "[min] " + diff / 1000 + "[sec]"); | |
704 | |
705 } catch (Exception e) { | |
706 logger.error(e.getMessage()); | |
707 e.printStackTrace(); | |
708 } | |
709 return entities; | |
710 } | |
711 | |
712 /** | |
713 * <p> | |
714 * Returns the entities of class "srcObjClass" directly from the DB, which | |
715 * are connected to the given entity by a relation of name "relationName". | |
716 * </p> | |
717 * <p> | |
718 * Only the variable srcObjClass can be null. | |
719 * </p> | |
720 * <p> | |
721 * If its expected all existing element from the DB, the variable maxResult | |
722 * should be -1, otherwise this value limits the returned result-set. | |
723 * </p> | |
724 * | |
725 * @param entity | |
726 * @param relationName | |
727 * @param tarObjClass | |
728 * @param maxResult | |
729 * @return | |
730 */ | |
731 public List<Entity> getSourcesForTargetRelation(Entity entity, | |
732 String relationName, String srcObjClass, int maxResult) { | |
733 List<Entity> entities = new ArrayList<Entity>(); | |
734 try { | |
735 logger.debug("### get Sources For TargetRelation ###"); | |
736 | |
737 Session session = HibernateUtil.getSessionFactory() | |
738 .getCurrentSession(); | |
739 session.getTransaction().begin(); | |
740 Long start = System.currentTimeMillis(); | |
741 | |
742 String hql = "from Relation rel, Entity src " + "where " | |
743 + "rel.targetId = :id AND " + "rel.targetModif = :mod AND " | |
744 + "rel.ownValue = :relationName AND " | |
745 + "rel.systemStatus = :systemStatus AND " | |
746 + "src.systemStatus = :systemStatus AND " | |
747 + "src.id = rel.sourceId "; | |
748 | |
749 if (StringUtils.isNotEmpty(srcObjClass)) { | |
750 hql += " AND rel.sourceObjectClass = :srcObjClass "; | |
751 } | |
752 | |
753 hql += " order by src.ownValue "; | |
754 | |
755 Query query = session.createQuery(hql); | |
756 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
757 query.setLong("id", entity.getId()); | |
758 query.setLong("mod", entity.getModificationTime()); | |
759 query.setString("relationName", relationName); | |
760 if (StringUtils.isNotEmpty(srcObjClass)) { | |
761 query.setString("srcObjClass", srcObjClass); | |
762 } | |
763 if (maxResult > 0) { | |
764 query.setMaxResults(maxResult); | |
765 } | |
766 | |
767 List<Object> list = query.list(); | |
768 | |
769 for (Object o : list) { | |
770 Object[] array = (Object[]) o; | |
771 if (array.length > 0) { | |
772 Entity ent = (Entity) array[1]; | |
773 ent.setLightweight(true); | |
774 entities.add(ent); | |
775 } | |
776 } | |
777 | |
778 session.getTransaction().commit(); | |
779 Long end = System.currentTimeMillis(); | |
780 Long diff = end - start; | |
781 logger.debug("Sources For TargetRelation - Time execution: " + diff | |
782 / (60 * 60 * 1000) + "[hours] " + diff / (60 * 1000) | |
783 + "[min] " + diff / 1000 + "[sec]"); | |
784 | |
785 } catch (Exception e) { | |
786 logger.error(e.getMessage()); | |
787 e.printStackTrace(); | |
788 } | |
789 return entities; | |
790 } | |
791 | |
792 public ViewerAttribute saveViewerAttribute(ViewerAttribute att){ | |
793 try { | |
794 logger.debug("### saveViewerAttribute ###"); | |
795 | |
796 Session session = HibernateUtil.getSessionFactory() | |
797 .getCurrentSession(); | |
798 session.getTransaction().begin(); | |
799 session.saveOrUpdate(att); | |
800 session.getTransaction().commit(); | |
801 | |
802 } catch (Exception e) { | |
803 logger.error(e.getMessage()); | |
804 e.printStackTrace(); | |
805 } | |
806 return att; | |
807 } | |
808 | |
809 public ViewerPage saveViewerPage(ViewerPage page){ | |
810 try { | |
811 logger.debug("### saveViewerPage ###"); | |
812 | |
813 Session session = HibernateUtil.getSessionFactory() | |
814 .getCurrentSession(); | |
815 session.getTransaction().begin(); | |
816 session.saveOrUpdate(page); | |
817 session.getTransaction().commit(); | |
818 | |
819 } catch (Exception e) { | |
820 logger.error(e.getMessage()); | |
821 e.printStackTrace(); | |
822 } | |
823 return page; | |
824 } | |
825 | |
826 public int removeViewerAttribute(Long id){ | |
827 int rows = 0; | |
828 try { | |
829 logger.debug("$$$ removeViewerAttribute [ID=" + id + "]"); | |
830 | |
831 Session session = HibernateUtil.getSessionFactory() | |
832 .getCurrentSession(); | |
833 session.beginTransaction(); | |
834 String sql = "DELETE FROM ViewerAttribute WHERE id = :id"; | |
835 Query query = session.createQuery(sql); | |
836 query.setLong("id", id); | |
837 | |
838 rows = query.executeUpdate(); | |
839 session.getTransaction().commit(); | |
840 | |
841 } catch (Exception e) { | |
842 logger.error(e.getMessage()); | |
843 e.printStackTrace(); | |
844 } | |
845 return rows; | |
846 } | |
847 | |
848 public int removeViewerPage(Long id){ | |
849 int rows = 0; | |
850 try { | |
851 logger.debug("$$$ removeViewerPage [ID=" + id + "]"); | |
852 | |
853 Session session = HibernateUtil.getSessionFactory() | |
854 .getCurrentSession(); | |
855 session.beginTransaction(); | |
856 String sql = "DELETE FROM ViewerPage WHERE id = :id"; | |
857 Query query = session.createQuery(sql); | |
858 query.setLong("id", id); | |
859 | |
860 rows = query.executeUpdate(); | |
861 session.getTransaction().commit(); | |
862 | |
863 } catch (Exception e) { | |
864 logger.error(e.getMessage()); | |
865 e.printStackTrace(); | |
866 } | |
867 return rows; | |
868 } | |
869 | |
870 public List<ViewerAttribute> getViewerAttributes(){ | |
871 List<ViewerAttribute> list = new ArrayList<ViewerAttribute>(); | |
872 try { | |
873 logger.debug("$$$ getViewerAttributes"); | |
874 | |
875 Session session = HibernateUtil.getSessionFactory() | |
876 .getCurrentSession(); | |
877 session.getTransaction().begin(); | |
878 Long start = System.currentTimeMillis(); | |
879 | |
880 String hql = "from ViewerAttribute"; | |
881 Query query = session.createQuery(hql); | |
882 list = query.list(); | |
883 | |
884 session.getTransaction().commit(); | |
885 Long end = System.currentTimeMillis(); | |
886 Long diff = end - start; | |
887 logger.debug("getViewerAttributes - Time execution: " + diff | |
888 / (60 * 60 * 1000) + "[hours] " + diff / (60 * 1000) | |
889 + "[min] " + diff / 1000 + "[sec]"); | |
890 } catch (Exception e) { | |
891 logger.error(e.getMessage()); | |
892 e.printStackTrace(); | |
893 } | |
894 | |
895 return list; | |
896 } | |
897 | |
898 public List<ViewerPage> getViewerPages(){ | |
899 List<ViewerPage> list = new ArrayList<ViewerPage>(); | |
900 try { | |
901 logger.info("$$$$ getViewerPages"); | |
902 | |
903 Session session = HibernateUtil.getSessionFactory() | |
904 .getCurrentSession(); | |
905 session.getTransaction().begin(); | |
906 Long start = System.currentTimeMillis(); | |
907 | |
908 String hql = "from ViewerPage"; | |
909 Query query = session.createQuery(hql); | |
910 list = query.list(); | |
911 | |
912 session.getTransaction().commit(); | |
913 Long end = System.currentTimeMillis(); | |
914 Long diff = end - start; | |
915 logger.info("getViewerPages - Time execution: " + diff | |
916 / (60 * 60 * 1000) + "[hours] " + diff / (60 * 1000) | |
917 + "[min] " + diff / 1000 + "[sec]"); | |
918 } catch (Exception e) { | |
919 logger.error(e.getMessage()); | |
920 e.printStackTrace(); | |
921 } | |
922 | |
923 return list; | |
924 } | |
925 | |
926 /* | |
927 * public List<Entity> searchEntityStartingBy(String objectClass, | |
928 * List<String> ownValueList , int maxResult){ List<Entity> entities = new | |
929 * ArrayList<Entity>(); try { | |
930 * logger.debug("### Starting Search Entity Starting by " + ownValueList + | |
931 * " ###"); Long start = System.currentTimeMillis(); | |
932 * | |
933 * Session session = HibernateUtil.getSessionFactory() .getCurrentSession(); | |
934 * session.getTransaction().begin(); | |
935 * | |
936 * String hql = "from Entity " + "where " + | |
937 * "objectClass = :objectClass AND " + "ownValue REGEXP '^[0-9]' AND " + | |
938 * "systemStatus = :systemStatus "; | |
939 * | |
940 * | |
941 * if(ownValueList.size() > 0){ hql += " AND ( "; } for(int i=0; i < | |
942 * ownValueList.size(); i++){ if( i > 0) hql += " OR "; hql += | |
943 * "ownValue like :ownValue" + i + " "; } if(ownValueList.size() > 0){ hql | |
944 * += " ) "; } hql += "GROUP by id " + "ORDER by ownValue "; | |
945 * | |
946 * Query query = session.createQuery(hql); if(maxResult > 0){ | |
947 * query.setMaxResults(maxResult); } query.setString("systemStatus", | |
948 * Node.SYS_STATUS_CURRENT_VERSION); query.setString("objectClass", | |
949 * objectClass); | |
950 * | |
951 * for(int i=0; i < ownValueList.size(); i++){ query.setString("ownValue" + | |
952 * i, ownValueList.get(i)); } | |
953 * | |
954 * entities = query.list(); | |
955 * | |
956 * Long end = System.currentTimeMillis(); Long diff = end - start; | |
957 * logger.info("Search Entity Starting By - Time execution: " + diff | |
958 * / (60 * 60 * 1000) + "[hours] " + diff / (60 * 1000) + "[min] " + diff / | |
959 * 1000 + "[sec]"); | |
960 * | |
961 * session.getTransaction().commit(); } catch (Exception e) { | |
962 * logger.error(e.getMessage()); e.printStackTrace(); } return entities; } | |
963 */ | |
964 | |
965 /** | |
966 * <p> | |
967 * This method returns a list of entities with same attributes and same own | |
968 * value than the given entity | |
969 * </p> | |
970 * | |
971 * @param entity | |
972 * @param substring | |
973 * @param maxResults | |
974 * @return | |
975 */ | |
976 public List<Entity> searchEntity(Entity entity, boolean substring, | |
977 int maxResult) { | |
978 List<Entity> entities = new ArrayList<Entity>(); | |
979 try { | |
980 logger.debug("### Starting Search Entity ###"); | |
981 Session session = HibernateUtil.getSessionFactory() | |
982 .getCurrentSession(); | |
983 session.getTransaction().begin(); | |
984 Long start = System.currentTimeMillis(); | |
985 | |
986 String div = (substring) ? " like " : " = "; | |
987 | |
988 String hql = "from Entity ent "; | |
989 | |
990 for (int i = 0; i < entity.getAttributes().size(); i++) { | |
991 hql += ", Attribute att" + i + " "; | |
992 } | |
993 | |
994 hql += " where " + "ent.systemStatus = :systemStatus AND " | |
995 + "ent.objectClass = :objectClass AND " + "ent.ownValue " | |
996 + div + " :ownValue "; | |
997 | |
998 if (entity.getAttributes().size() > 0) | |
999 hql += "AND "; | |
1000 | |
1001 for (int i = 0; i < entity.getAttributes().size(); i++) { | |
1002 hql += "att" + i + ".systemStatus = :systemStatus AND "; | |
1003 hql += "att" + i + ".sourceId = ent.id AND "; | |
1004 hql += "att" + i + ".objectClass = :objectClass" + i + " AND "; | |
1005 hql += "att" + i + ".ownValue " + div + " :ownValue" + i + " "; | |
1006 if (i < (entity.getAttributes().size() - 1)) | |
1007 hql += "AND "; | |
1008 } | |
1009 hql += " GROUP by ent.id"; | |
1010 | |
1011 Query query = session.createQuery(hql); | |
1012 if (maxResult > 0) { | |
1013 query.setMaxResults(maxResult); | |
1014 } | |
1015 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
1016 query.setString("objectClass", entity.getObjectClass()); | |
1017 query.setString( | |
1018 "ownValue", | |
1019 (substring) ? "%" + entity.getOwnValue() + "%" : entity | |
1020 .getOwnValue()); | |
1021 | |
1022 int i = 0; | |
1023 for (Attribute att : entity.getAttributes()) { | |
1024 query.setString("objectClass" + i, att.getObjectClass()); | |
1025 query.setString( | |
1026 "ownValue" + i, | |
1027 (substring) ? "%" + att.getOwnValue() + "%" : att | |
1028 .getOwnValue()); | |
1029 i++; | |
1030 } | |
1031 | |
1032 List<Object> list = query.list(); | |
1033 | |
1034 for (Object o : list) { | |
1035 Object[] array = (Object[]) o; | |
1036 if (array.length > 0) { | |
1037 Entity ent = (Entity) array[0]; | |
1038 ent.setLightweight(true); | |
1039 entities.add(ent); | |
1040 } | |
1041 } | |
1042 | |
1043 session.getTransaction().commit(); | |
1044 Long end = System.currentTimeMillis(); | |
1045 Long diff = end - start; | |
1046 logger.info("Search Entity - Time execution: " + diff | |
1047 / (60 * 60 * 1000) + "[hours] " + diff / (60 * 1000) | |
1048 + "[min] " + diff / 1000 + "[sec]"); | |
1049 | |
1050 } catch (Exception e) { | |
1051 logger.error(e.getMessage()); | |
1052 e.printStackTrace(); | |
1053 } | |
1054 return entities; | |
1055 } | |
1056 | |
1057 public List<Entity> searchEntity(String objectClass, String ownValue, | |
1058 int maxResult) { | |
1059 try { | |
1060 logger.info("### Starting Search Entity ###"); | |
1061 Session session = HibernateUtil.getSessionFactory() | |
1062 .getCurrentSession(); | |
1063 session.getTransaction().begin(); | |
1064 Long start = System.currentTimeMillis(); | |
1065 | |
1066 String hql = "from Entity " + "where " | |
1067 + "systemStatus = :systemStatus AND " | |
1068 + "objectClass like :objectClass "; | |
1069 | |
1070 if (StringUtils.isNotEmpty(ownValue)) { | |
1071 hql += "AND ownValue like :ownValue "; | |
1072 } | |
1073 | |
1074 Query query = session.createQuery(hql); | |
1075 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
1076 query.setString("objectClass", objectClass); | |
1077 if (StringUtils.isNotEmpty(ownValue)) { | |
1078 query.setString("ownValue", "%" + ownValue + "%"); | |
1079 } | |
1080 | |
1081 if (maxResult > 0) { | |
1082 query.setMaxResults(maxResult); | |
1083 } | |
1084 | |
1085 List<Entity> list = query.list(); | |
1086 | |
1087 session.getTransaction().commit(); | |
1088 Long end = System.currentTimeMillis(); | |
1089 Long diff = end - start; | |
1090 logger.info("Search Entity - Time execution: " + diff | |
1091 / (60 * 60 * 1000) + "[hours] " + diff / (60 * 1000) | |
1092 + "[min] " + diff / 1000 + "[sec]"); | |
1093 return list; | |
1094 } catch (Exception e) { | |
1095 logger.error(e.getMessage()); | |
1096 e.printStackTrace(); | |
1097 } | |
1098 return null; | |
1099 } | |
1100 | |
1101 public List<Attribute> getAttributeByDefByName(String def, String name, | |
1102 int maxResults) { | |
1103 List<Attribute> list = null; | |
1104 try { | |
1105 Session session = HibernateUtil.getSessionFactory() | |
1106 .getCurrentSession(); | |
1107 session.getTransaction().begin(); | |
1108 | |
1109 String hql = "from Attribute " + "where " | |
1110 + "objectClass = :name AND " | |
1111 + "systemStatus = :systemStatus AND " | |
1112 + "sourceObjectClass = :sourceObjectClass "; | |
1113 | |
1114 Query query = session.createQuery(hql); | |
1115 query.setString("name", name); | |
1116 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
1117 query.setString("sourceObjectClass", def); | |
1118 if (maxResults > 0) { | |
1119 query.setMaxResults(maxResults); | |
1120 } | |
1121 | |
1122 list = query.list(); | |
1123 | |
1124 session.getTransaction().commit(); | |
1125 } catch (Exception e) { | |
1126 logger.error(e.getMessage()); | |
1127 e.printStackTrace(); | |
1128 } | |
1129 return list; | |
1130 } | |
1131 | |
1132 /** | |
1133 * Returns an attribute directly from the DB no implemented yet! | |
1134 * | |
1135 * @param entity | |
1136 * @param name | |
1137 * @return | |
1138 */ | |
1139 public Attribute getAttributeByName(Entity entity, String name) { | |
1140 Attribute att = null; | |
1141 try { | |
1142 Session session = HibernateUtil.getSessionFactory() | |
1143 .getCurrentSession(); | |
1144 session.getTransaction().begin(); | |
1145 | |
1146 String hql = "from Attribute " + "where " | |
1147 + "objectClass = :name AND " | |
1148 + "systemStatus = :systemStatus AND " + "sourceId = :id "; | |
1149 | |
1150 Query query = session.createQuery(hql); | |
1151 query.setMaxResults(1); | |
1152 query.setString("name", name); | |
1153 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
1154 query.setLong("id", entity.getId()); | |
1155 | |
1156 List<Attribute> list = query.list(); | |
1157 if (list.size() > 0) { | |
1158 att = (Attribute) list.get(0); | |
1159 } | |
1160 | |
1161 session.getTransaction().commit(); | |
1162 } catch (Exception e) { | |
1163 logger.error(e.getMessage()); | |
1164 e.printStackTrace(); | |
1165 } | |
1166 return att; | |
1167 } | |
1168 | |
1169 public List<Attribute> getAllAttributesJDBC(Long entId, int maxResult) { | |
1170 List<Attribute> atts = null; | |
1171 long start = System.currentTimeMillis(); | |
1172 try { | |
1173 Connection conn = JDBCUtils.getConn(); | |
1174 | |
1175 String sql = "select * from node " + "where " | |
1176 + "system_status = '" + Node.SYS_STATUS_CURRENT_VERSION | |
1177 + "' AND " + "source_id = '" + entId + "' " | |
1178 + "order by object_class"; | |
1179 | |
1180 if (maxResult > 0) { | |
1181 // query.setMaxResults(maxResult); | |
1182 } | |
1183 | |
1184 Statement st = conn.createStatement(); | |
1185 ResultSet rs = st.executeQuery(sql); | |
1186 atts = JDBCUtils.rs2Atts(rs); | |
1187 | |
1188 conn.close(); | |
1189 } catch (Exception e) { | |
1190 logger.error(e.getMessage()); | |
1191 e.printStackTrace(); | |
1192 } | |
1193 | |
1194 logger.debug("getAllAttributes JDBC - execution time[ms]: " | |
1195 + (System.currentTimeMillis() - start)); | |
1196 return atts; | |
1197 } | |
1198 | |
1199 /** | |
1200 * Returns a list of all attribute for an entity directly from the DB no | |
1201 * implemented yet! | |
1202 * | |
1203 * @param entity | |
1204 * @param name | |
1205 * @return | |
1206 */ | |
1207 public List<Attribute> getAllAttributes(Long entId, int maxResult) { | |
1208 List<Attribute> atts = null; | |
1209 long start = System.currentTimeMillis(); | |
1210 try { | |
1211 Session session = HibernateUtil.getSessionFactory() | |
1212 .getCurrentSession(); | |
1213 session.getTransaction().begin(); | |
1214 | |
1215 String hql = "from Attribute " + "where " | |
1216 + "systemStatus = :systemStatus AND " + "sourceId = :id " | |
1217 + "order by objectClass"; | |
1218 | |
1219 Query query = session.createQuery(hql); | |
1220 if (maxResult > 0) { | |
1221 query.setMaxResults(maxResult); | |
1222 } | |
1223 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
1224 query.setLong("id", entId); | |
1225 | |
1226 atts = query.list(); | |
1227 | |
1228 session.getTransaction().commit(); | |
1229 | |
1230 } catch (Exception e) { | |
1231 logger.error(e.getMessage(), e); | |
1232 e.printStackTrace(); | |
1233 } | |
1234 logger.debug("getAllAttributes - execution time[ms]: " | |
1235 + (System.currentTimeMillis() - start)); | |
1236 return atts; | |
1237 } | |
1238 | |
1239 /** | |
1240 * Here the entities can more than one time in the result set. | |
1241 * | |
1242 * @param filters | |
1243 * @param maxResult | |
1244 * @return | |
1245 */ | |
1246 public Map<Attribute, Entity> searchAttEntityByAttributeFilter( | |
1247 List<AttributeFilter> filters, int maxResult) { | |
1248 Map<Attribute, Entity> result = new HashMap<Attribute, Entity>(); | |
1249 try { | |
1250 Session session = HibernateUtil.getSessionFactory() | |
1251 .getCurrentSession(); | |
1252 session.getTransaction().begin(); | |
1253 | |
1254 String hql = "from Entity as ent, Attribute as att " + "where " | |
1255 + "att.systemStatus = :systemStatus AND " | |
1256 + "ent.systemStatus = :systemStatus AND " | |
1257 + "ent.id = att.sourceId AND ("; | |
1258 | |
1259 int count = 0; | |
1260 for (AttributeFilter filter : filters) { | |
1261 hql += " ( "; | |
1262 if (StringUtils.isNotEmpty(filter.getEntObjectClass())) { | |
1263 hql += "att.sourceObjectClass = :sourceObjectClass" + count | |
1264 + " "; | |
1265 if (StringUtils.isNotEmpty(filter.getName()) | |
1266 || StringUtils.isNotEmpty(filter.getOwnValue())) | |
1267 hql += " AND "; | |
1268 } | |
1269 if (StringUtils.isNotEmpty(filter.getName())) { | |
1270 hql += "att.objectClass = :name" + count + " "; | |
1271 if (StringUtils.isNotEmpty(filter.getOwnValue())) | |
1272 hql += " AND "; | |
1273 } | |
1274 if (StringUtils.isNotEmpty(filter.getOwnValue())) { | |
1275 if (filter.isNormalize()) { | |
1276 hql += "att.normalizedOwnValue like :ownValue" + count | |
1277 + " "; | |
1278 } else { | |
1279 hql += "att.ownValue like :ownValue" + count + " "; | |
1280 } | |
1281 } | |
1282 | |
1283 hql += " ) "; | |
1284 count++; | |
1285 if (filters.size() > count) | |
1286 hql += " OR "; | |
1287 } | |
1288 | |
1289 hql += " ) group by att.id "; | |
1290 // I removed this line because the buffer of MyISAM is to | |
1291 // small and it could be a reason to return lesser rows. I hope so! | |
1292 // order by ent.ownValue | |
1293 | |
1294 Query query = session.createQuery(hql); | |
1295 if (maxResult > 0) | |
1296 query.setMaxResults(maxResult); | |
1297 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
1298 | |
1299 count = 0; | |
1300 for (AttributeFilter filter : filters) { | |
1301 if (StringUtils.isNotEmpty(filter.getName())) | |
1302 query.setString("name" + count, filter.getName()); | |
1303 if (StringUtils.isNotEmpty(filter.getOwnValue())) | |
1304 query.setString("ownValue" + count, | |
1305 "%" + filter.getOwnValue() + "%"); | |
1306 if (StringUtils.isNotEmpty(filter.getEntObjectClass())) | |
1307 query.setString("sourceObjectClass" + count, | |
1308 filter.getEntObjectClass()); | |
1309 count++; | |
1310 } | |
1311 | |
1312 List<Object> listO = query.list(); | |
1313 for (Object o : listO) { | |
1314 Object[] array = (Object[]) o; | |
1315 if (array.length > 1) { | |
1316 Entity ent = (Entity) array[0]; | |
1317 ent.setLightweight(true); | |
1318 Attribute att = (Attribute) array[1]; | |
1319 result.put(att, ent); | |
1320 } | |
1321 } | |
1322 | |
1323 session.getTransaction().commit(); | |
1324 } catch (Exception e) { | |
1325 logger.error(e.getMessage()); | |
1326 e.printStackTrace(); | |
1327 } | |
1328 return result; | |
1329 } | |
1330 | |
1331 /** | |
1332 * here the entities not repeated | |
1333 * | |
1334 * @param filters | |
1335 * @param maxResult | |
1336 * @return | |
1337 */ | |
1338 public Map<Entity, Attribute> searchEntityByAttributeFilter( | |
1339 List<AttributeFilter> filters, int maxResult) { | |
1340 Map<Entity, Attribute> result = new HashMap<Entity, Attribute>(); | |
1341 try { | |
1342 Session session = HibernateUtil.getSessionFactory() | |
1343 .getCurrentSession(); | |
1344 session.getTransaction().begin(); | |
1345 | |
22
165b1efb85cd
remove group-by clause for attribute search filter because it gave hibernate errors :-(
casties
parents:
1
diff
changeset
|
1346 String hql = "FROM Entity AS ent, Attribute AS att " |
165b1efb85cd
remove group-by clause for attribute search filter because it gave hibernate errors :-(
casties
parents:
1
diff
changeset
|
1347 + "WHERE " |
1 | 1348 + "att.systemStatus = :systemStatus AND " |
1349 + "ent.systemStatus = :systemStatus AND " | |
1350 + "ent.id = att.sourceId AND ("; | |
1351 | |
1352 int count = 0; | |
1353 for (AttributeFilter filter : filters) { | |
1354 hql += " ( "; | |
1355 if (StringUtils.isNotEmpty(filter.getEntObjectClass())) { | |
1356 hql += "att.sourceObjectClass = :sourceObjectClass" + count | |
1357 + " "; | |
1358 if (StringUtils.isNotEmpty(filter.getName()) | |
1359 || StringUtils.isNotEmpty(filter.getOwnValue())) | |
1360 hql += " AND "; | |
1361 } | |
1362 if (StringUtils.isNotEmpty(filter.getName())) { | |
1363 hql += "att.objectClass = :name" + count + " "; | |
1364 if (StringUtils.isNotEmpty(filter.getOwnValue())) | |
1365 hql += " AND "; | |
1366 } | |
1367 if (StringUtils.isNotEmpty(filter.getOwnValue())) { | |
1368 if (filter.isNormalize()) { | |
1369 hql += "att.normalizedOwnValue like :ownValue" + count | |
1370 + " "; | |
1371 } else { | |
1372 hql += "att.ownValue like :ownValue" + count + " "; | |
1373 } | |
1374 } | |
1375 | |
1376 hql += " ) "; | |
1377 count++; | |
1378 if (filters.size() > count) | |
1379 hql += " OR "; | |
1380 } | |
1381 | |
22
165b1efb85cd
remove group-by clause for attribute search filter because it gave hibernate errors :-(
casties
parents:
1
diff
changeset
|
1382 /* ROC: led to hibernate errors |
165b1efb85cd
remove group-by clause for attribute search filter because it gave hibernate errors :-(
casties
parents:
1
diff
changeset
|
1383 * hql += " ) group by att.sourceId order by ent.ownValue"; |
165b1efb85cd
remove group-by clause for attribute search filter because it gave hibernate errors :-(
casties
parents:
1
diff
changeset
|
1384 */ |
165b1efb85cd
remove group-by clause for attribute search filter because it gave hibernate errors :-(
casties
parents:
1
diff
changeset
|
1385 hql += " ) order by ent.ownValue"; |
1 | 1386 |
1387 Query query = session.createQuery(hql); | |
1388 if (maxResult > 0) | |
1389 query.setMaxResults(maxResult); | |
1390 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
1391 | |
1392 count = 0; | |
1393 for (AttributeFilter filter : filters) { | |
1394 if (StringUtils.isNotEmpty(filter.getName())) | |
1395 query.setString("name" + count, filter.getName()); | |
1396 if (StringUtils.isNotEmpty(filter.getOwnValue())) | |
1397 query.setString("ownValue" + count, | |
1398 "%" + filter.getOwnValue() + "%"); | |
1399 if (StringUtils.isNotEmpty(filter.getEntObjectClass())) | |
1400 query.setString("sourceObjectClass" + count, | |
1401 filter.getEntObjectClass()); | |
1402 count++; | |
1403 } | |
1404 | |
1405 List<Object> listO = query.list(); | |
1406 for (Object o : listO) { | |
1407 Object[] array = (Object[]) o; | |
1408 if (array.length > 1) { | |
1409 Entity ent = (Entity) array[0]; | |
1410 ent.setLightweight(true); | |
1411 Attribute att = (Attribute) array[1]; | |
1412 result.put(ent, att); | |
1413 } | |
1414 } | |
1415 | |
1416 session.getTransaction().commit(); | |
1417 } catch (Exception e) { | |
1418 logger.error(e.getMessage()); | |
1419 e.printStackTrace(); | |
1420 } | |
1421 return result; | |
1422 } | |
1423 | |
1424 /** | |
1425 * | |
1426 * @param filters | |
1427 * @param maxResults | |
1428 * @return | |
1429 */ | |
1430 public List<Attribute> searchAttribute(List<AttributeFilter> filters, | |
1431 int maxResult) { | |
1432 try { | |
1433 // logger.info(""); | |
1434 // logger.info("### Starting Search Attribute [entObjClass="+entObjClass+", name="+name+", ownValue="+ownValue+", contentType="+contentType+"]###"); | |
1435 Session session = HibernateUtil.getSessionFactory() | |
1436 .getCurrentSession(); | |
1437 session.getTransaction().begin(); | |
1438 Long start = System.currentTimeMillis(); | |
1439 | |
1440 String hql = "from Attribute " + "where " | |
1441 + "systemStatus = :systemStatus AND ("; | |
1442 | |
1443 int count = 0; | |
1444 for (AttributeFilter filter : filters) { | |
1445 hql += " ( "; | |
1446 if (StringUtils.isNotEmpty(filter.getEntObjectClass())) { | |
1447 hql += "sourceObjectClass = :sourceObjectClass" + count | |
1448 + " "; | |
1449 if (StringUtils.isNotEmpty(filter.getName()) | |
1450 || StringUtils.isNotEmpty(filter.getOwnValue())) | |
1451 hql += " AND "; | |
1452 } | |
1453 if (StringUtils.isNotEmpty(filter.getName())) { | |
1454 hql += "objectClass like :name" + count + " "; | |
1455 if (StringUtils.isNotEmpty(filter.getOwnValue())) | |
1456 hql += " AND "; | |
1457 } | |
1458 if (StringUtils.isNotEmpty(filter.getOwnValue())) { | |
1459 if (filter.isNormalize()) { | |
1460 hql += "normalizedOwnValue like :ownValue" + count | |
1461 + " "; | |
1462 } else { | |
1463 hql += "ownValue like :ownValue" + count + " "; | |
1464 } | |
1465 } | |
1466 | |
1467 hql += " ) "; | |
1468 count++; | |
1469 if (filters.size() > count) | |
1470 hql += " OR "; | |
1471 } | |
1472 | |
1473 hql += " ) group by sourceId"; | |
1474 | |
1475 Query query = session.createQuery(hql); | |
1476 if (maxResult > 0) | |
1477 query.setMaxResults(maxResult); | |
1478 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
1479 | |
1480 count = 0; | |
1481 for (AttributeFilter filter : filters) { | |
1482 if (StringUtils.isNotEmpty(filter.getName())) | |
1483 query.setString("name" + count, filter.getName() + "%"); | |
1484 if (StringUtils.isNotEmpty(filter.getOwnValue())) | |
1485 query.setString("ownValue" + count, | |
1486 "%" + filter.getOwnValue() + "%"); | |
1487 if (StringUtils.isNotEmpty(filter.getEntObjectClass())) | |
1488 query.setString("sourceObjectClass" + count, | |
1489 filter.getEntObjectClass()); | |
1490 count++; | |
1491 } | |
1492 | |
1493 List<Attribute> list = query.list(); | |
1494 | |
1495 session.getTransaction().commit(); | |
1496 Long end = System.currentTimeMillis(); | |
1497 Long diff = end - start; | |
1498 logger.info("Found " + list.size() + " elements"); | |
1499 logger.info("Search Attribute - Time execution: " + diff | |
1500 / (60 * 60 * 1000) + "[hours] " + diff / (60 * 1000) | |
1501 + "[min] " + diff / 1000 + "[sec]\n"); | |
1502 return list; | |
1503 } catch (Exception e) { | |
1504 logger.error(e.getMessage()); | |
1505 e.printStackTrace(); | |
1506 } | |
1507 return null; | |
1508 } | |
1509 | |
1510 /** | |
1511 * <p> | |
1512 * Returns only an attribute searched by its name, its value and by an | |
1513 * attribute which belong to the same entity | |
1514 * </p> | |
1515 * <p> | |
1516 * For example to find all Country by which contains in its value "Germa" | |
1517 * the input should be so: | |
1518 * </p> | |
1519 * <p> | |
1520 * firstName = "name" firstValue = "Germ" secondName = "type" secondValue = | |
1521 * "country" internally both attributes are binding by the unique entity of | |
1522 * type "PLACE". Only the attribute of the first-name and -value will be | |
1523 * returned. | |
1524 * </p> | |
1525 * | |
1526 * @param name | |
1527 * @param ownValue | |
1528 * @param entObjClass | |
1529 * @param maxResults | |
1530 * @return | |
1531 */ | |
1532 public List<Attribute> searchAttribute(String firstName, String firstValue, | |
1533 String secondName, String secondValue, String entObjClass, | |
1534 int maxResults) { | |
1535 List<Attribute> list = new ArrayList<Attribute>(); | |
1536 Session session = null; | |
1537 try { | |
1538 logger.info("Starting Search Attribute " + "[entObjClass=" | |
1539 + entObjClass + ", firstName=" + firstName | |
1540 + ", firstValue=" + firstValue + ", secondName=" | |
1541 + secondName + ", secondValue=" + secondValue + "]"); | |
1542 | |
1543 session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
1544 session.getTransaction().begin(); | |
1545 Long start = System.currentTimeMillis(); | |
1546 | |
1547 String hql = "from Attribute as a1, Attribute as a2 " + "where " | |
1548 | |
1549 + "a1.sourceId = a2.sourceId AND " | |
1550 | |
1551 + "a1.systemStatus = :systemStatus AND " | |
1552 + "a2.systemStatus = :systemStatus AND " | |
1553 | |
1554 + "a1.objectClass like :firstName AND " | |
1555 + "a2.objectClass like :secondName AND " | |
1556 | |
1557 + "a1.ownValue like :firstValue AND " | |
1558 + "a2.ownValue like :secondValue AND " | |
1559 | |
1560 + "a1.sourceObjectClass = :entObjClass AND " | |
1561 + "a2.sourceObjectClass = :entObjClass "; | |
1562 | |
1563 hql += "group by a1.sourceId "; | |
1564 | |
1565 Query query = session.createQuery(hql); | |
1566 query.setString("firstName", "%" + firstName + "%"); | |
1567 query.setString("firstValue", "%" + firstValue + "%"); | |
1568 query.setString("secondName", "%" + secondName + "%"); | |
1569 query.setString("secondValue", "%" + secondValue + "%"); | |
1570 query.setString("entObjClass", entObjClass); | |
1571 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
1572 if (maxResults > 0) { | |
1573 query.setMaxResults(maxResults); | |
1574 } | |
1575 | |
1576 List<Object> listO = query.list(); | |
1577 | |
1578 for (Object o : listO) { | |
1579 Object[] array = (Object[]) o; | |
1580 if (array.length > 0) { | |
1581 Attribute firstAtt = (Attribute) array[0]; | |
1582 list.add(firstAtt); | |
1583 } | |
1584 } | |
1585 | |
1586 } catch (Exception e) { | |
1587 logger.error(e.getMessage()); | |
1588 e.printStackTrace(); | |
1589 } finally { | |
1590 session.getTransaction().commit(); | |
1591 } | |
1592 return list; | |
1593 } | |
1594 | |
1595 /** | |
1596 * <p> | |
1597 * only the ownValue of the attribute and of the entity can be null | |
1598 * </p> | |
1599 * <p> | |
1600 * The ownValue, the attName and the attOwnValue are searched as substring. | |
1601 * </p> | |
1602 * | |
1603 * @param objectClass | |
1604 * @param ownValue | |
1605 * @param attName | |
1606 * @param attOwnValue | |
1607 * @param maxResult | |
1608 * @return | |
1609 */ | |
1610 public List<Entity> searchEntityByAttribute(String objectClass, | |
1611 String ownValue, String attName, String attOwnValue, int maxResult) { | |
1612 List<Entity> entities = new ArrayList<Entity>(); | |
1613 try { | |
1614 | |
1615 Session session = HibernateUtil.getSessionFactory() | |
1616 .getCurrentSession(); | |
1617 session.getTransaction().begin(); | |
1618 Long start = System.currentTimeMillis(); | |
1619 | |
1620 String hql = "from Entity as ent, Attribute as att " + "where " | |
1621 + "ent.systemStatus = :systemStatus AND " | |
1622 + "att.systemStatus = :systemStatus AND " | |
1623 + "ent.objectClass = :objectClass AND "; | |
1624 | |
1625 if (StringUtils.isNotEmpty(ownValue)) { | |
1626 hql += "ent.ownValue like :ownValue AND "; | |
1627 } | |
1628 | |
1629 if (StringUtils.isNotEmpty(attOwnValue)) { | |
1630 hql += "att.ownValue like :attOwnValue AND "; | |
1631 } | |
1632 | |
1633 hql += "att.objectClass like :attName AND " | |
1634 + "ent.id = att.sourceId " + "group by ent.id "; | |
1635 | |
1636 Query query = session.createQuery(hql); | |
1637 if (maxResult > 0) { | |
1638 query.setMaxResults(maxResult); | |
1639 } | |
1640 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
1641 query.setString("objectClass", objectClass); | |
1642 if (StringUtils.isNotEmpty(ownValue)) | |
1643 query.setString("ownValue", "%" + ownValue + "%"); | |
1644 query.setString("attName", "%" + attName + "%"); | |
1645 if (StringUtils.isNotEmpty(attOwnValue)) | |
1646 query.setString("attOwnValue", "%" + attOwnValue + "%"); | |
1647 | |
1648 List<Object> listO = query.list(); | |
1649 | |
1650 for (Object o : listO) { | |
1651 Object[] array = (Object[]) o; | |
1652 if (array.length > 0) { | |
1653 Entity ent = (Entity) array[0]; | |
1654 ent.setLightweight(true); | |
1655 entities.add(ent); | |
1656 } | |
1657 } | |
1658 | |
1659 session.getTransaction().commit(); | |
1660 | |
1661 return entities; | |
1662 } catch (Exception e) { | |
1663 logger.error(e.getMessage()); | |
1664 e.printStackTrace(); | |
1665 } | |
1666 return null; | |
1667 } | |
1668 | |
1669 /** | |
1670 * <p> | |
1671 * This method searches attributes, which match the input's parameters. | |
1672 * </p> | |
1673 * <p> | |
1674 * Any parameter must be empty, excepted contentType. | |
1675 * </p> | |
1676 * | |
1677 * @param name | |
1678 * @param ownValue | |
1679 * @param entObjClass | |
1680 * @param maxResults | |
1681 * @return list of attributes filtered by input's parameters | |
1682 */ | |
1683 public List<Attribute> searchAttribute(String name, String ownValue, | |
1684 String entObjClass, String contentType, int maxResults) { | |
1685 List<Attribute> list = new ArrayList<Attribute>(); | |
1686 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
1687 try { | |
1688 // logger.info(""); | |
1689 // logger.info("### Starting Search Attribute [entObjClass=" | |
1690 // + entObjClass + ", name=" + name + ", ownValue=" + ownValue | |
1691 // + ", contentType=" + contentType + "]###"); | |
1692 | |
1693 session.getTransaction().begin(); | |
1694 // Long start = System.currentTimeMillis(); | |
1695 | |
1696 String hql = "from Attribute " + "where " | |
1697 + "systemStatus = :systemStatus AND " | |
1698 + "objectClass like :name AND "; | |
1699 if (StringUtils.isNotEmpty(ownValue)) { | |
1700 hql += "ownValue like :ownValue AND "; | |
1701 } | |
1702 if (StringUtils.isNotEmpty(contentType)) { | |
1703 hql += "contentType like :contentType AND "; | |
1704 } | |
1705 hql += "sourceObjectClass = :sourceObjectClass "; | |
1706 hql += "group by id order by ownValue"; | |
1707 | |
1708 Query query = session.createQuery(hql); | |
1709 if (maxResults > 0) { | |
1710 query.setMaxResults(maxResults); | |
1711 } | |
1712 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
1713 query.setString("name", name + "%"); | |
1714 if (StringUtils.isNotEmpty(ownValue)) { | |
1715 query.setString("ownValue", "%" + ownValue + "%"); | |
1716 } | |
1717 query.setString("sourceObjectClass", entObjClass); | |
1718 if (StringUtils.isNotEmpty(contentType)) { | |
1719 query.setString("contentType", contentType); | |
1720 } | |
1721 | |
1722 list = query.list(); | |
1723 | |
1724 // Long end = System.currentTimeMillis(); | |
1725 // Long diff = end - start; | |
1726 // logger.info("Found " + list.size() + " elements"); | |
1727 // logger.info("Search Attribute - Time execution: " + diff | |
1728 // / (60 * 60 * 1000) + "[hours] " + diff / (60 * 1000) | |
1729 // + "[min] " + diff / 1000 + "[sec]\n"); | |
1730 | |
1731 } catch (Exception e) { | |
1732 logger.error(e.getMessage()); | |
1733 e.printStackTrace(); | |
1734 } finally { | |
1735 session.getTransaction().commit(); | |
1736 } | |
1737 return list; | |
1738 } | |
1739 | |
1740 /** | |
1741 * | |
1742 * @param objClass | |
1743 * @param relName | |
1744 * @param objClassSrc | |
1745 * @param attName | |
1746 * @param attValue | |
1747 * @param maxResults | |
1748 * @return | |
1749 */ | |
1750 public List<Entity> searchEntityByAttributeOfTarRelation(String objClass, | |
1751 String relName, String objClassSrc, String attName, | |
1752 String attValue, int maxResults) { | |
1753 List<Entity> entityList = new ArrayList<Entity>(); | |
1754 Session session = null; | |
1755 try { | |
1756 | |
1757 if (StringUtils.isEmpty(objClass) || StringUtils.isEmpty(relName) | |
1758 || StringUtils.isEmpty(objClassSrc) | |
1759 || StringUtils.isEmpty(attName) | |
1760 || StringUtils.isEmpty(attValue)) { | |
1761 throw new Exception( | |
1762 "At least one of these parameters was empty: objClass, relName, objClassSrc, attName, attValue."); | |
1763 } | |
1764 | |
1765 session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
1766 session.getTransaction().begin(); | |
1767 Long start = System.currentTimeMillis(); | |
1768 | |
1769 String hql = "from Relation as rel, Attribute as att " | |
1770 + "where " | |
1771 + "att.sourceObjectClass = :objClassSrc AND " | |
1772 + | |
1773 // "att.sourceObjectClass = rel.sourceObjectClass AND " + | |
1774 "rel.targetObjectClass = :objClass AND " | |
1775 + "rel.ownValue = :relName AND " | |
1776 + "att.ownValue like :attValue AND " | |
1777 + "att.objectClass = :attName AND " | |
1778 + "att.systemStatus = :systemStatus AND " | |
1779 + "rel.systemStatus = :systemStatus AND " | |
1780 + "rel.sourceId = att.sourceId " + "group by att.sourceId"; | |
1781 | |
1782 Query query = session.createQuery(hql); | |
1783 if (maxResults > 0) { | |
1784 query.setMaxResults(maxResults); | |
1785 } | |
1786 | |
1787 query.setString("objClass", objClass); | |
1788 query.setString("relName", relName); | |
1789 query.setString("objClassSrc", objClassSrc); | |
1790 query.setString("attName", attName); | |
1791 query.setString("attValue", "%" + attValue + "%"); | |
1792 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
1793 | |
1794 List<Object> list = query.list(); | |
1795 List<Long> idList = new ArrayList<Long>(); | |
1796 for (Object o : list) { | |
1797 Object[] array = (Object[]) o; | |
1798 Relation rel = (Relation) array[0]; | |
1799 idList.add(rel.getTargetId()); | |
1800 } | |
1801 | |
1802 for (Long id : idList) { | |
1803 List<Entity> l = getLightweightEntities(session, | |
1804 Node.SYS_STATUS_CURRENT_VERSION, id, null, null, null, | |
1805 true, -1); | |
1806 if (l.size() > 0) { | |
1807 entityList.add(l.get(0)); | |
1808 } | |
1809 } | |
1810 | |
1811 Long end = System.currentTimeMillis(); | |
1812 Long diff = end - start; | |
1813 logger.info("searchEntityByAttributeOfTarRelation - Time execution: " | |
1814 + diff | |
1815 / (60 * 60 * 1000) | |
1816 + "[hours] " | |
1817 + diff | |
1818 / (60 * 1000) + "[min] " + diff / 1000 + "[sec]"); | |
1819 } catch (Exception e) { | |
1820 logger.error(e.getMessage()); | |
1821 } finally { | |
1822 session.getTransaction().commit(); | |
1823 } | |
1824 return entityList; | |
1825 } | |
1826 | |
1827 /** | |
1828 * returns the attribute found and the corresponding entity | |
1829 * | |
1830 * @param objClass | |
1831 * @param relName | |
1832 * @param objClassSrc | |
1833 * @param attName | |
1834 * @param attValue | |
1835 * @param maxResults | |
1836 * @return | |
1837 */ | |
1838 public Map<Entity, Attribute> searchEntityByAttributeOfTarRelation2( | |
1839 String objClass, String relName, String objClassSrc, | |
1840 String attName, String attValue, int maxResults) { | |
1841 Map<Entity, Attribute> entityMap = new HashMap<Entity, Attribute>(); | |
1842 Session session = null; | |
1843 try { | |
1844 | |
1845 if (StringUtils.isEmpty(objClass) || StringUtils.isEmpty(relName) | |
1846 || StringUtils.isEmpty(objClassSrc) | |
1847 || StringUtils.isEmpty(attName) | |
1848 || StringUtils.isEmpty(attValue)) { | |
1849 throw new Exception( | |
1850 "At least one of these parameters was empty: objClass, relName, objClassSrc, attName, attValue."); | |
1851 } | |
1852 | |
1853 session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
1854 session.getTransaction().begin(); | |
1855 Long start = System.currentTimeMillis(); | |
1856 | |
1857 String hql = "from Relation as rel, Attribute as att " | |
1858 + "where " | |
1859 + "att.sourceObjectClass = :objClassSrc AND " | |
1860 + | |
1861 // "att.sourceObjectClass = rel.sourceObjectClass AND " + | |
1862 "rel.targetObjectClass = :objClass AND " | |
1863 + "rel.ownValue = :relName AND " | |
1864 + "att.ownValue like :attValue AND " | |
1865 + "att.objectClass = :attName AND " | |
1866 + "att.systemStatus = :systemStatus AND " | |
1867 + "rel.systemStatus = :systemStatus AND " | |
1868 + "rel.sourceId = att.sourceId " + "group by att.sourceId"; | |
1869 | |
1870 Query query = session.createQuery(hql); | |
1871 if (maxResults > 0) { | |
1872 query.setMaxResults(maxResults); | |
1873 } | |
1874 | |
1875 query.setString("objClass", objClass); | |
1876 query.setString("relName", relName); | |
1877 query.setString("objClassSrc", objClassSrc); | |
1878 query.setString("attName", attName); | |
1879 query.setString("attValue", "%" + attValue + "%"); | |
1880 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
1881 | |
1882 List<Object> list = query.list(); | |
1883 Map<Long, Attribute> map = new HashMap<Long, Attribute>(); | |
1884 for (Object o : list) { | |
1885 Object[] array = (Object[]) o; | |
1886 Relation rel = (Relation) array[0]; | |
1887 Attribute att = (Attribute) array[1]; | |
1888 map.put(rel.getTargetId(), att); | |
1889 } | |
1890 | |
1891 for (Long id : map.keySet()) { | |
1892 List<Entity> l = getLightweightEntities(session, | |
1893 Node.SYS_STATUS_CURRENT_VERSION, id, null, null, null, | |
1894 true, -1); | |
1895 if (l.size() > 0) { | |
1896 entityMap.put(l.get(0), map.get(id)); | |
1897 } | |
1898 } | |
1899 | |
1900 Long end = System.currentTimeMillis(); | |
1901 Long diff = end - start; | |
1902 logger.info("searchEntityByAttributeOfTarRelation - Time execution: " | |
1903 + diff | |
1904 / (60 * 60 * 1000) | |
1905 + "[hours] " | |
1906 + diff | |
1907 / (60 * 1000) + "[min] " + diff / 1000 + "[sec]"); | |
1908 } catch (Exception e) { | |
1909 logger.error(e.getMessage()); | |
1910 } finally { | |
1911 session.getTransaction().commit(); | |
1912 } | |
1913 return entityMap; | |
1914 } | |
1915 | |
1916 /** | |
1917 * TODO | |
1918 * | |
1919 * @param objClass | |
1920 * @param relName | |
1921 * @param objClassTar | |
1922 * @param attName | |
1923 * @param attValue | |
1924 * @param maxResults | |
1925 * @return | |
1926 */ | |
1927 public List<Entity> searchEntityByAttributeOfSrcRelation(String objClass, | |
1928 String relName, String objClassTar, String attName, | |
1929 String attValue, int maxResults) { | |
1930 try { | |
1931 | |
1932 } catch (Exception e) { | |
1933 e.printStackTrace(); | |
1934 } | |
1935 return null; | |
1936 } | |
1937 | |
1938 /** | |
1939 * This method search a | |
1940 * | |
1941 * @param filter | |
1942 * RelationFilter | |
1943 * @param maxResults | |
1944 * @return | |
1945 */ | |
1946 public List<RelationResultEntry> searchByRelation(RelationFilter filter, | |
1947 String grouping, int maxResults) { | |
1948 try { | |
1949 Session session = HibernateUtil.getSessionFactory() | |
1950 .getCurrentSession(); | |
1951 session.getTransaction().begin(); | |
1952 Long start = System.currentTimeMillis(); | |
1953 | |
1954 String hql = "from "; | |
1955 if (filter.srcInclude) { | |
1956 if (filter.srcAttInclude) { | |
1957 hql += "Attribute as srcAtt, "; | |
1958 hql += "NodeJoin as joinSrcAtt, "; | |
1959 } | |
1960 hql += "Entity as src, "; | |
1961 } | |
1962 hql += "Relation as rel "; | |
1963 if (filter.tarInclude) { | |
1964 hql += ", "; | |
1965 hql += "Entity as tar "; | |
1966 if (filter.tarAttInclude) { | |
1967 hql += ", "; | |
1968 hql += "NodeJoin as joinTarAtt, "; | |
1969 hql += "Attribute as tarAtt "; | |
1970 } | |
1971 } | |
1972 | |
1973 hql += "where "; | |
1974 if (filter.srcInclude) { | |
1975 if (filter.srcAttInclude) { | |
1976 hql += "srcAtt.systemStatus = :systemStatus AND "; | |
1977 hql += "joinSrcAtt.sourceSystemStatus = :systemStatus AND joinSrcAtt.targetSystemStatus = :systemStatus AND "; | |
1978 hql += "joinSrcAtt.sourceId = src.id AND joinSrcAtt.targetId = srcAtt.id AND "; | |
1979 hql += "srcAtt.ownValue like :srcAttOwnValue AND "; | |
1980 hql += "srcAtt.objectClass like :srcAttName AND "; | |
1981 } | |
1982 hql += "src.systemStatus = :systemStatus AND "; | |
1983 hql += "rel.sourceId = src.id AND "; | |
1984 hql += "src.ownValue like :srcOwnValue AND "; | |
1985 hql += "src.objectClass like :srcObjectClass AND "; | |
1986 } | |
1987 if (filter.tarInclude) { | |
1988 if (filter.tarAttInclude) { | |
1989 hql += "tarAtt.systemStatus = :systemStatus AND "; | |
1990 hql += "joinTarAtt.sourceSystemStatus = :systemStatus AND joinTarAtt.targetSystemStatus = :systemStatus AND "; | |
1991 hql += "joinTarAtt.sourceId = tar.id AND joinTarAtt.targetId = tarAtt.id AND "; | |
1992 hql += "tarAtt.ownValue like :tarAttOwnValue AND "; | |
1993 hql += "tarAtt.objectClass like :tarAttName AND "; | |
1994 } | |
1995 hql += "tar.systemStatus = :systemStatus AND "; | |
1996 hql += "rel.targetId = tar.id AND "; | |
1997 hql += "tar.ownValue like :tarOwnValue AND "; | |
1998 hql += "tar.objectClass like :tarObjectClass AND "; | |
1999 } | |
2000 hql += "rel.systemStatus = :systemStatus AND "; | |
2001 hql += "rel.ownValue like :relOwnValue AND "; | |
2002 hql += "rel.objectClass like :relObjectClass "; | |
2003 | |
2004 hql += ") "; | |
2005 | |
2006 // setup of the field, which is used to group the result entries. | |
2007 if (GROUP_BY_SOURCE.equals(grouping) && filter.srcInclude) { | |
2008 hql += " group by src.id"; | |
2009 } else { | |
2010 if (GROUP_BY_TARGET.equals(grouping) && filter.tarInclude) { | |
2011 hql += " group by tar.id"; | |
2012 } else { | |
2013 hql += " group by rel.id"; | |
2014 } | |
2015 } | |
2016 | |
2017 Query query = session.createQuery(hql); | |
2018 if (maxResults > 0) { | |
2019 query.setMaxResults(maxResults); | |
2020 } | |
2021 | |
2022 // setting the values of the filter in the query. | |
2023 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
2024 if (filter.srcInclude) { | |
2025 if (filter.srcAttInclude) { | |
2026 if (StringUtils.isNotEmpty(filter.srcAttOwnValue)) { | |
2027 query.setString("srcAttOwnValue", "%" | |
2028 + filter.srcAttOwnValue); | |
2029 } | |
2030 if (StringUtils.isNotEmpty(filter.srcAttName)) { | |
2031 query.setString("srcAttName", "%" + filter.srcAttName); | |
2032 } | |
2033 } | |
2034 if (StringUtils.isNotEmpty(filter.srcOwnValue)) { | |
2035 query.setString("srcOwnValue", "%" + filter.srcOwnValue); | |
2036 } | |
2037 if (StringUtils.isNotEmpty(filter.srcObjectClass)) { | |
2038 query.setString("srcObjectClass", "%" | |
2039 + filter.srcObjectClass); | |
2040 } | |
2041 } | |
2042 if (filter.tarInclude) { | |
2043 if (filter.tarAttInclude) { | |
2044 if (StringUtils.isNotEmpty(filter.tarAttOwnValue)) { | |
2045 query.setString("tarAttOwnValue", filter.tarAttOwnValue); | |
2046 } | |
2047 if (StringUtils.isNotEmpty(filter.tarAttName)) { | |
2048 query.setString("tarAttName", filter.tarAttName); | |
2049 } | |
2050 } | |
2051 if (StringUtils.isNotEmpty(filter.tarObjectClass)) { | |
2052 query.setString("tarObjectClass", filter.tarObjectClass); | |
2053 } | |
2054 if (StringUtils.isNotEmpty(filter.tarOwnValue)) { | |
2055 query.setString("tarOwnValue", filter.tarOwnValue); | |
2056 } | |
2057 } | |
2058 if (StringUtils.isNotEmpty(filter.relOwnValue)) { | |
2059 query.setString("relOwnValue", filter.relOwnValue); | |
2060 } | |
2061 if (StringUtils.isNotEmpty(filter.relObjectClass)) { | |
2062 query.setString("relObjectClass", filter.relObjectClass); | |
2063 } | |
2064 | |
2065 List<Object> list = query.list(); | |
2066 List<RelationResultEntry> resultSet = new ArrayList<RelationResultEntry>(); | |
2067 RelationResultEntry entry; | |
2068 for (Object o : list) { | |
2069 entry = new RelationResultEntry(); | |
2070 int index = 0; | |
2071 Object[] array = (Object[]) o; | |
2072 // Object item = (Object) array[0]; | |
2073 if (filter.srcInclude) { | |
2074 if (filter.srcAttInclude) { | |
2075 entry.srcAttribute = (Attribute) array[index]; | |
2076 index++; | |
2077 index++; | |
2078 // hql += "Attribute as srcAtt, "; | |
2079 // hql += "NodeJoin as joinSrcAtt, "; | |
2080 } | |
2081 entry.source = (Entity) array[index]; | |
2082 index++; | |
2083 // hql += "Entity as src, "; | |
2084 } | |
2085 // hql += "Relation as rel "; | |
2086 entry.relation = (Relation) array[index]; | |
2087 index++; | |
2088 if (filter.tarInclude) { | |
2089 // hql += "Entity as tar "; | |
2090 entry.target = (Entity) array[index]; | |
2091 index++; | |
2092 if (filter.tarAttInclude) { | |
2093 index++; | |
2094 entry.tarAttribute = (Attribute) array[index]; | |
2095 // hql += "NodeJoin as joinTarAtt, "; | |
2096 // hql += "Attribute as tarAtt "; | |
2097 } | |
2098 } | |
2099 resultSet.add(entry); | |
2100 } | |
2101 // ////////////////////////////////////////// | |
2102 // ////////////////////////////////////////// | |
2103 // ////////////////////////////////////////// | |
2104 session.getTransaction().commit(); | |
2105 Long end = System.currentTimeMillis(); | |
2106 Long diff = end - start; | |
2107 logger.info("GetLightweightEntities BY RELATION - Time execution: " | |
2108 + diff | |
2109 / (60 * 60 * 1000) | |
2110 + "[hours] " | |
2111 + diff | |
2112 / (60 * 1000) + "[min] " + diff / 1000 + "[sec]"); | |
2113 return resultSet; | |
2114 } catch (Exception e) { | |
2115 logger.error(e.getMessage()); | |
2116 e.printStackTrace(); | |
2117 } | |
2118 return null; | |
2119 } | |
2120 | |
2121 /** | |
2122 * <p> | |
2123 * Returns a list of entities filtered by the following rules: | |
2124 * </p> | |
2125 * if <b>ownValue</b> is no empty, the entities returned have into ownValue | |
2126 * the given substring. | |
2127 * | |
2128 * @param ownValue | |
2129 * @param objectClass | |
2130 * @param filters | |
2131 * @return | |
2132 */ | |
2133 | |
2134 /** | |
2135 * TODO: what will be returned if it is searching a null value eg: | |
2136 * identifier=null? | |
2137 * | |
2138 * @param ownValue | |
2139 * @param objectClass | |
2140 * @param attObjClass | |
2141 * @param attOwnValue | |
2142 * @param assertions | |
2143 * @param maxResults | |
2144 * @return | |
2145 */ | |
2146 public List<Entity> getLightweightEntitiesByAttribute(String ownValue, | |
2147 String objectClass, String attObjClass, String attOwnValue, | |
2148 Boolean assertions, int maxResults, boolean substring) { | |
2149 logger.debug("Get LW Entities By Attribute [maxResults: " + maxResults | |
2150 + "]"); | |
2151 | |
2152 List<Entity> entities = new ArrayList<Entity>(); | |
2153 | |
2154 if (substring) { | |
2155 ownValue = (StringUtils.isEmpty(ownValue)) ? "" : "%" + ownValue | |
2156 + "%"; | |
2157 objectClass = (StringUtils.isEmpty(objectClass)) ? "" : "%" | |
2158 + objectClass + "%"; | |
2159 attOwnValue = (StringUtils.isEmpty(attOwnValue)) ? "" : "%" | |
2160 + attOwnValue + "%"; | |
2161 attObjClass = (StringUtils.isEmpty(attObjClass)) ? "" : "%" | |
2162 + attObjClass + "%"; | |
2163 } | |
2164 | |
2165 String type = (assertions) ? Node.TYPE_ABOX : Node.TYPE_TBOX; | |
2166 String equalityOperator = (substring) ? " like " : " = "; | |
2167 | |
2168 try { | |
2169 Session session = HibernateUtil.getSessionFactory() | |
2170 .getCurrentSession(); | |
2171 session.getTransaction().begin(); | |
2172 Long start = System.currentTimeMillis(); | |
2173 | |
2174 String hqlEntities = "from Entity as ent, Attribute as att " | |
2175 + "where ent.systemStatus = :systemStatus AND att.systemStatus = :systemStatus AND " | |
2176 + "att.sourceId = ent.id AND "; | |
2177 | |
2178 if (StringUtils.isNotEmpty(ownValue)) { | |
2179 hqlEntities += "ent.ownValue " + equalityOperator | |
2180 + " :ownValue AND "; | |
2181 } | |
2182 if (StringUtils.isNotEmpty(objectClass)) { | |
2183 hqlEntities += "ent.objectClass " + equalityOperator | |
2184 + " :objectClass AND "; | |
2185 } | |
2186 if (StringUtils.isNotEmpty(attOwnValue)) { | |
2187 hqlEntities += "att.ownValue " + equalityOperator | |
2188 + " :attOwnValue AND "; | |
2189 } | |
2190 if (StringUtils.isNotEmpty(attObjClass)) { | |
2191 hqlEntities += "att.objectClass " + equalityOperator | |
2192 + " :attObjClass AND "; | |
2193 } | |
2194 hqlEntities += "ent.type = :type " | |
2195 + "group by ent.id order by ent.ownValue"; | |
2196 | |
2197 Query queryEntities = session.createQuery(hqlEntities); | |
2198 queryEntities.setString("systemStatus", | |
2199 Node.SYS_STATUS_CURRENT_VERSION); | |
2200 queryEntities.setString("type", type); | |
2201 if (StringUtils.isNotEmpty(ownValue)) { | |
2202 queryEntities.setString("ownValue", ownValue); | |
2203 } | |
2204 if (StringUtils.isNotEmpty(objectClass)) { | |
2205 queryEntities.setString("objectClass", objectClass); | |
2206 } | |
2207 if (StringUtils.isNotEmpty(attOwnValue)) { | |
2208 queryEntities.setString("attOwnValue", attOwnValue); | |
2209 } | |
2210 if (StringUtils.isNotEmpty(attObjClass)) { | |
2211 queryEntities.setString("attObjClass", attObjClass); | |
2212 } | |
2213 if (maxResults > 0) { | |
2214 queryEntities.setMaxResults(maxResults); | |
2215 } | |
2216 | |
2217 entities = new ArrayList<Entity>(); | |
2218 List<Object> list = queryEntities.list(); | |
2219 for (Object o : list) { | |
2220 Object[] array = (Object[]) o; | |
2221 Object item = (Object) array[0]; | |
2222 if (item instanceof Entity) { | |
2223 // logger.info("Entity= " + item); | |
2224 Entity ent = (Entity) item; | |
2225 ent.setLightweight(true); | |
2226 entities.add(ent); | |
2227 } | |
2228 } | |
2229 | |
2230 session.getTransaction().commit(); | |
2231 Long end = System.currentTimeMillis(); | |
2232 Long diff = end - start; | |
2233 logger.debug("GetLightweightEntitiesByAttribute - Time execution: " | |
2234 + diff / (60 * 60 * 1000) + "[hours] " + diff / (60 * 1000) | |
2235 + "[min] " + diff / 1000 + "[sec]"); | |
2236 } catch (Exception e) { | |
2237 logger.error(e.getMessage()); | |
2238 e.printStackTrace(); | |
2239 } | |
2240 return entities; | |
2241 } | |
2242 | |
2243 } |