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