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