Mercurial > hg > openmind
annotate src/main/java/org/mpi/openmind/cache/WrapperService.java @ 54:7f9033d44a87
small cleanup.
author | casties |
---|---|
date | Fri, 04 Nov 2016 18:16:04 +0100 |
parents | 3a3362f78d48 |
children | b7a8db041f68 |
rev | line source |
---|---|
1 | 1 package org.mpi.openmind.cache; |
2 | |
3 import java.io.Serializable; | |
4 import java.lang.management.ManagementFactory; | |
5 import java.util.ArrayList; | |
6 import java.util.Collection; | |
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.mpi.openmind.repository.bo.Attribute; | |
15 import org.mpi.openmind.repository.bo.Entity; | |
16 import org.mpi.openmind.repository.bo.Node; | |
17 import org.mpi.openmind.repository.bo.Relation; | |
18 import org.mpi.openmind.repository.bo.ViewerAttribute; | |
19 import org.mpi.openmind.repository.bo.ViewerPage; | |
20 import org.mpi.openmind.repository.bo.utils.EntitySortByNormalizedOwnValue; | |
21 import org.mpi.openmind.repository.bo.utils.EntitySortByVersion; | |
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.PersistenceService; | |
25 import org.mpi.openmind.repository.services.utils.AttributeFilter; | |
26 import org.mpi.openmind.repository.utils.ImportOM3Util; | |
27 import org.mpi.openmind.repository.utils.NormalizerUtils; | |
7 | 28 import org.mpi.openmind.repository.utils.RomanizationLoC; |
1 | 29 |
30 public class WrapperService implements Serializable{ | |
31 | |
32 private static final long serialVersionUID = 2578074607841626396L; | |
33 | |
34 public static final String IS_TYPE_OF = "is_type_of"; | |
35 | |
36 private static Logger logger = Logger.getLogger(WrapperService.class); | |
37 | |
45 | 38 private static Logger txLog = Logger.getLogger(PersistenceService.TRANSACTION_LOGGER); |
39 | |
40 private transient CacheService cache; | |
1 | 41 |
42 public long getSourceRelationsCount(Entity entity, String relationName, | |
43 String tarObjClass) { | |
44 return this.getPS().getSourceRelationsCount(entity, relationName, | |
45 tarObjClass); | |
46 } | |
47 | |
48 public Long getTargetRelationsCount(Entity entity, String relationName, | |
49 String srcObjClass) { | |
50 return this.getPS().getTargetRelationsCount(entity, relationName, | |
51 srcObjClass); | |
52 } | |
53 | |
35
1cd9d9a67cad
added regular expression match option to AttributeFilter and searchEntityByAttribute.
casties
parents:
33
diff
changeset
|
54 /** |
1cd9d9a67cad
added regular expression match option to AttributeFilter and searchEntityByAttribute.
casties
parents:
33
diff
changeset
|
55 * Search entities matching a list of attribute filters. |
1cd9d9a67cad
added regular expression match option to AttributeFilter and searchEntityByAttribute.
casties
parents:
33
diff
changeset
|
56 * |
1cd9d9a67cad
added regular expression match option to AttributeFilter and searchEntityByAttribute.
casties
parents:
33
diff
changeset
|
57 * Returns a Map of Entities and Attributes. |
1cd9d9a67cad
added regular expression match option to AttributeFilter and searchEntityByAttribute.
casties
parents:
33
diff
changeset
|
58 * |
1cd9d9a67cad
added regular expression match option to AttributeFilter and searchEntityByAttribute.
casties
parents:
33
diff
changeset
|
59 * @param filters |
1cd9d9a67cad
added regular expression match option to AttributeFilter and searchEntityByAttribute.
casties
parents:
33
diff
changeset
|
60 * @param maxResult |
1cd9d9a67cad
added regular expression match option to AttributeFilter and searchEntityByAttribute.
casties
parents:
33
diff
changeset
|
61 * @return |
1cd9d9a67cad
added regular expression match option to AttributeFilter and searchEntityByAttribute.
casties
parents:
33
diff
changeset
|
62 */ |
1cd9d9a67cad
added regular expression match option to AttributeFilter and searchEntityByAttribute.
casties
parents:
33
diff
changeset
|
63 public Map<Entity, Attribute> searchEntityByAttributeFilter ( |
1 | 64 List<AttributeFilter> filters, int maxResult) { |
65 return this.getPS().searchEntityByAttributeFilter(filters, maxResult); | |
66 } | |
67 | |
68 public Map<Attribute, Entity> searchAttEntityByAttributeFilter( | |
69 List<AttributeFilter> filters, int maxResult) { | |
70 return this.getPS() | |
71 .searchAttEntityByAttributeFilter(filters, maxResult); | |
72 } | |
73 | |
74 public List<Entity> searchEntityByOwnValue(String oc, String term){ | |
75 List<Entity> rs = new ArrayList<Entity>(); | |
76 if(StringUtils.isEmpty(term)) | |
77 return rs; | |
78 | |
37 | 79 List<Entity> entList = this.cache.loadEntitiesByDef(oc); |
1 | 80 term = NormalizerUtils.normalize(term); |
81 | |
82 for(Entity ent : entList){ | |
83 if(StringUtils.isNotEmpty(ent.getNormalizedOwnValue()) && | |
84 StringUtils.contains(ent.getNormalizedOwnValue(), term)){ | |
85 rs.add(ent); | |
86 } | |
87 } | |
88 return rs; | |
89 } | |
90 | |
91 // ************************************************************ | |
92 // ************************************************************ | |
93 // ************************************************************ | |
94 // ************************************************************ | |
95 | |
96 public List<Entity> getPreviousEntitiesById(Long id) { | |
97 List<Entity> list = this.getPS().getEntities(id, | |
98 Node.SYS_STATUS_PREVIOUS_VERSION, null, null); | |
99 Collections.sort(list, new EntitySortByVersion()); | |
100 return list; | |
101 } | |
102 | |
103 public void initCache() { | |
104 logger.info("##### Initializing Cache #####"); | |
105 logger.info(ManagementFactory.getRuntimeMXBean().getName()); | |
106 | |
107 try { | |
108 int mb = 1024 * 1024; | |
109 | |
110 // Getting the runtime reference from system | |
111 Runtime runtime = Runtime.getRuntime(); | |
112 | |
113 logger.info("##### Heap utilization statistics [MB] #####"); | |
114 | |
115 // Print used memory | |
116 logger.info("Used Memory:" | |
117 + (runtime.totalMemory() - runtime.freeMemory()) / mb); | |
118 | |
119 // Print free memory | |
120 logger.info("Free Memory:" + runtime.freeMemory() / mb); | |
121 | |
122 // Print total available memory | |
123 logger.info("Total Memory:" + runtime.totalMemory() / mb); | |
124 | |
125 // Print Maximum available memory | |
126 logger.info("Max Memory:" + runtime.maxMemory() / mb + "\n"); | |
127 } catch (Exception e) { | |
38 | 128 logger.error(e); |
1 | 129 } |
130 | |
131 if (!this.cache.areDefsLoaded()) { | |
132 try { | |
38 | 133 this.cache.initDefinitions(getPS().getLWDefinitions(), |
134 getPS().getDefAttributes(), | |
135 getPS().getDefRelations()); | |
1 | 136 } catch (Exception e) { |
38 | 137 logger.error(e); |
1 | 138 } |
139 } | |
140 } | |
141 | |
51 | 142 public void removeCurrentVersionEntity(Entity entity, String user) throws Exception { |
143 logger.info("removeCurrentVersionEntity ("+ user + ") " + entity); | |
144 txLog.debug("** START remove entity: user="+user+" entity="+entity.toSmallString()); | |
145 // delete entity from cache | |
1 | 146 this.cache.deleteEntity(entity.getId(), entity.getObjectClass()); |
51 | 147 // delete entity from DB |
1 | 148 this.getPS().removeEntCurrentVersion(entity.getId(), entity.getType()); |
51 | 149 txLog.debug("** END remove entity: user="+user+" entity="+entity.toSmallString()); |
1 | 150 } |
151 | |
152 public Map<Entity, Attribute> searchEntityByAttributeFilter0(String term, | |
153 List<AttributeFilter> filters, int maxResults) { | |
154 Map<Entity, Attribute> map = new HashMap<Entity, Attribute>(); | |
155 List<Long> usedIds = new ArrayList<Long>(); | |
156 | |
157 boolean mustBreak = false; | |
158 int count = 0; | |
159 if (StringUtils.isNotEmpty(term)) { | |
160 String normalizedTerm = NormalizerUtils.normalize(term); | |
161 for (AttributeFilter filter : filters) { | |
162 if (mustBreak) { | |
163 break; | |
164 } | |
165 for (Attribute att : getAttributesByDefByAttName( | |
166 filter.getEntObjectClass(), filter.getName(), -1)) { | |
167 if (!usedIds.contains(att.getSourceId()) | |
168 && StringUtils.isNotEmpty(att | |
169 .getNormalizedOwnValue()) | |
170 && att.getNormalizedOwnValue().contains( | |
171 normalizedTerm)) { | |
172 map.put(getEntityById(att.getSourceId()), att); | |
173 usedIds.add(att.getSourceId()); | |
174 count++; | |
175 if (maxResults > 0 && count == maxResults) { | |
176 mustBreak = true; | |
177 break; | |
178 } | |
179 } | |
180 } | |
181 } | |
182 } | |
183 return map; | |
184 } | |
185 | |
186 public int getEntitiesCount(String def) { | |
187 int count = this.cache.getEntitiesCount(def); | |
188 return count; | |
189 } | |
190 | |
53 | 191 /** |
192 * Returns a List of Entities of the given definition (object_class). | |
193 * | |
194 * All Entities are sorted by normalized own-value first. Then the | |
195 * part between fromIndex and toIndex is returned. | |
196 * | |
197 * @param def | |
198 * @param fromIndex | |
199 * @param toIndex | |
200 * @return | |
201 */ | |
202 public List<Entity> getEntityByDefSubList(String def, int fromIndex, int toIndex) { | |
203 List<Entity> list = new ArrayList<Entity>(); | |
204 List<Entity> all = getEntitiesByDef(def); | |
205 if (all != null && all.size() >= toIndex) { | |
1 | 206 |
53 | 207 long start = System.currentTimeMillis(); |
208 Collections.sort(list, new EntitySortByNormalizedOwnValue()); | |
209 long diff = System.currentTimeMillis() - start; | |
210 logger.debug("Sorting entities time[ms] " + diff); | |
211 | |
212 for (Entity ent : all.subList(fromIndex, toIndex)) { | |
213 list.add(ent); | |
214 } | |
215 } | |
216 return list; | |
217 } | |
218 | |
219 /** | |
220 * Returns a List of all Entities of the given definition (object_class). | |
221 * | |
222 * @param def | |
223 * @return | |
224 */ | |
1 | 225 public List<Entity> getEntitiesByDef(String def) { |
37 | 226 List<Entity> list = this.cache.loadEntitiesByDef(def); |
1 | 227 return list; |
228 } | |
229 | |
230 public List<Entity> getEntityByDefAndOW(String def, String ow, | |
231 int maxResults) { | |
232 List<Entity> result = new ArrayList<Entity>(); | |
233 if (StringUtils.isNotEmpty(ow)) { | |
37 | 234 List<Entity> list = this.cache.loadEntitiesByDef(def); |
1 | 235 |
236 int count = 0; | |
237 ow = NormalizerUtils.normalize(ow); | |
238 for (Entity e : list) { | |
239 // String eow = (StringUtils.isNotEmpty(e.getOwnValue())) ? | |
240 // e.getOwnValue().toLowerCase() : ""; | |
241 if (StringUtils.isNotBlank(e.getNormalizedOwnValue()) | |
242 && e.getNormalizedOwnValue().contains(ow)) { | |
243 result.add((Entity) e.clone()); | |
244 count++; | |
245 } | |
246 | |
247 if (maxResults > 0 && maxResults == count) { | |
248 break; | |
249 } | |
250 } | |
251 } | |
252 return result; | |
253 } | |
254 | |
255 /** | |
256 * this method does not execute: - ownValue generation - new version | |
257 * generation This method only save the entity in the current state. | |
258 * | |
259 * @param nodeList | |
260 * @param user | |
261 */ | |
262 public void saveEntityListAsNode(List<Entity> entList, String user) { | |
263 for (Entity ent : entList) { | |
264 ent.setUser(user); | |
265 } | |
266 getPS().saveEntityListAsNode(entList); | |
267 for (Entity ent : entList) { | |
268 cache.saveEntity(ent); | |
269 } | |
270 } | |
271 | |
272 public void saveEntityListAsNodeWithoutContent(List<Entity> nodeList, | |
273 String user) throws Exception { | |
274 this.cache.saveEntityListAsNodeWithoutContent(nodeList); | |
275 } | |
276 | |
277 public boolean existEntity(Long entId) { | |
278 return getCache().getEntityByIdReadOnly(entId) != null; | |
279 } | |
280 | |
281 public boolean existRelation(Long srcId, Long tarId, String relName) { | |
282 | |
283 try { | |
284 if (srcId == null || tarId == null) { | |
285 throw new Exception("srcId and tarId can not be null."); | |
286 } | |
287 | |
288 List<Relation> relList = this.cache.getRelsBySrcId(srcId); | |
289 | |
290 for (Relation rel : relList) { | |
291 if (rel.getTargetId().equals(tarId) | |
292 && rel.getOwnValue().equals(relName)) | |
293 return true; | |
294 } | |
295 } catch (Exception e) { | |
296 e.printStackTrace(); | |
297 } | |
298 | |
299 return false; | |
300 } | |
301 | |
302 public void saveRelationAsNode(Relation rel, String user) throws Exception { | |
303 | |
304 if (rel.getAttributes() != null && rel.getAttributes().size() > 0) { | |
305 throw new Exception( | |
306 "This method can be only used if the relation does not contains attributes, this is not the case."); | |
307 } | |
308 | |
309 rel.setUser(user); | |
310 this.getPS().saveNode(rel); | |
311 this.cache.updateRelationAsNode(rel); | |
312 } | |
313 | |
314 public List<Entity> searchEntityByAttributeOfTarRelation(String objClass, | |
315 String relName, String objClassSrc, String attName, | |
316 String attValue, int maxResults) throws Exception { | |
317 List<Entity> list = new ArrayList<Entity>(); | |
318 | |
319 List<Attribute> attList = getAttributesByDefByAttName(objClassSrc, | |
320 attName, attValue, -1); | |
321 int count = 0; | |
322 for (Attribute att : attList) { | |
323 Relation rel = getFirstRelationByTargetOCByName( | |
324 cache.getRelsBySrcId(att.getSourceId()), objClass, relName); | |
325 if (rel != null) { | |
326 list.add(getEntityById(rel.getTargetId())); | |
327 count++; | |
328 if (maxResults > 0 && maxResults == count) { | |
329 break; | |
330 } | |
331 } | |
332 } | |
333 | |
334 return list; | |
335 } | |
336 | |
337 private static Relation getFirstRelationByTargetOCByName( | |
338 List<Relation> list, String tarOC, String name) { | |
339 if (StringUtils.isNotEmpty(name)) { | |
340 name = name.toLowerCase(); | |
341 for (Relation rel : list) { | |
342 if (rel.getTargetObjectClass().equals(tarOC)) { | |
343 if (StringUtils.isNotEmpty(rel.getOwnValue()) | |
344 && name.equals(rel.getOwnValue().toLowerCase())) { | |
345 return rel; | |
346 } | |
347 } | |
348 } | |
349 } | |
350 return null; | |
351 } | |
352 | |
353 public List<Attribute> getAttributeByEntId(Long entId) { | |
354 List<Attribute> list = cache.getAttsBySrcId(entId); | |
355 return list; | |
356 } | |
357 | |
358 public Attribute getAttributeByName(Long entId, String name) { | |
359 List<Attribute> list = cache.getAttsBySrcId(entId); | |
360 | |
361 for (Attribute att : list) { | |
362 if (att.getName().equals(name)) { | |
363 return (Attribute) att.clone(); | |
364 } | |
365 } | |
366 return null; | |
367 } | |
368 | |
7 | 369 public String getAttributeOVByName(Long entId, String name, boolean useRomanization){ |
370 Attribute att = getAttributeByName(entId, name); | |
371 if(att != null){ | |
372 return (useRomanization) ? RomanizationLoC.convert(att.getOwnValue()) : att.getOwnValue(); | |
373 } | |
374 return ""; | |
375 } | |
376 | |
1 | 377 public List<Attribute> searchAttribute(String firstName, String firstValue, |
378 String secondName, String secondValue, String def, int maxResults) { | |
379 List<Attribute> list = new ArrayList<Attribute>(); | |
380 | |
381 List<Attribute> firstAttList = cache.getAttsByOCAndName(def, firstName); | |
382 | |
383 firstValue = StringUtils.isNotEmpty(firstValue) ? NormalizerUtils | |
384 .normalize(firstValue) : null; | |
385 secondValue = StringUtils.isNotEmpty(secondValue) ? NormalizerUtils | |
386 .normalize(secondValue) : null; | |
387 | |
388 if (StringUtils.isNotEmpty(firstValue)) { | |
389 for (Attribute firstAtt : firstAttList) { | |
390 String attValue1 = (StringUtils.isNotEmpty(firstAtt | |
391 .getNormalizedOwnValue())) ? firstAtt | |
392 .getNormalizedOwnValue() : ""; | |
393 // (StringUtils.isNotEmpty(firstAtt.getValue())) ? | |
394 // firstAtt.getValue().toLowerCase() : ""; | |
395 if (StringUtils.isNotEmpty(firstValue) | |
396 && attValue1.contains(firstValue)) { | |
397 Attribute secondAtt = getAttributeByName( | |
398 firstAtt.getSourceId(), secondName); | |
399 if (secondAtt != null) { | |
400 String attValue2 = (StringUtils.isNotEmpty(secondAtt | |
401 .getNormalizedOwnValue())) ? secondAtt | |
402 .getNormalizedOwnValue() : ""; | |
403 // (StringUtils.isNotEmpty(secondAtt.getValue())) ? | |
404 // secondAtt.getValue().toLowerCase() : ""; | |
405 if (StringUtils.isNotEmpty(secondValue) | |
406 && attValue2.contains(attValue2)) { | |
407 list.add((Attribute) firstAtt.clone()); | |
408 } | |
409 } | |
410 } | |
411 } | |
412 } | |
413 | |
414 return list; | |
415 } | |
416 | |
417 private List<Attribute> getAttributesByDefByAttName(String def, | |
418 String attName, int maxResults) { | |
419 List<Attribute> list = cache.getAttsByOCAndName(def, attName); | |
420 return list; | |
421 } | |
422 | |
423 /** | |
424 * Returns a list of attributes by objectClass of the entity, the att Name | |
425 * and its value. | |
426 * | |
427 * @param def | |
428 * @param attName | |
429 * @param attValue | |
430 * sub string and ignores case sensitive. | |
431 * @param maxResults | |
432 * @return | |
433 */ | |
434 public List<Attribute> getAttributesByDefByAttName(String def, | |
435 String attName, String attValue, int maxResults) { | |
436 | |
437 List<Attribute> list = getAttributesByDefByAttName0(def, attName, attValue, maxResults); | |
438 Collections.sort(list); | |
439 return list; | |
440 } | |
441 | |
442 public List<Attribute> getAttributes(String oc, String attName){ | |
443 return cache.getAttsByOCAndName(oc, attName); | |
444 } | |
445 | |
446 /** | |
447 * | |
448 * @param attName | |
449 * @param attValue | |
450 * @return | |
451 */ | |
452 public List<Attribute> getAttributesByExactValue(String attName, String attValue){ | |
453 List<Attribute> list = new ArrayList<Attribute>(); | |
454 | |
455 if(StringUtils.isNotEmpty(attValue)){ | |
456 for(Entity def : cache.getLWDefinitions()){ | |
457 | |
458 List<Attribute> attList = cache.getAttsByOCAndName(def.getOwnValue(), attName); | |
459 attValue = NormalizerUtils.normalize(attValue); | |
460 | |
461 for(Attribute att : attList){ | |
462 if(StringUtils.equals(attValue, att.getNormalizedOwnValue())){ | |
463 list.add(att); | |
464 } | |
465 } | |
466 | |
467 } | |
468 Collections.sort(list); | |
469 } | |
470 return list; | |
471 } | |
472 | |
473 private List<Attribute> getAttributesByDefByAttName0(String def, | |
474 String attName, String attValue, int maxResults) { | |
475 List<Attribute> list = new ArrayList<Attribute>(); | |
476 List<Attribute> attList = cache.getAttsByOCAndName(def, attName); | |
477 attValue = NormalizerUtils.normalize(attValue); | |
478 int count = 0; | |
479 if (StringUtils.isEmpty(attValue)) { | |
480 for (Attribute att : attList) { | |
481 list.add((Attribute) att.clone()); | |
482 count++; | |
483 if (maxResults > 0 && maxResults == count) { | |
484 break; | |
485 } | |
486 } | |
487 } else { | |
488 for (Attribute att : attList) { | |
489 String attValue0 = StringUtils.isNotEmpty(att | |
490 .getNormalizedOwnValue()) ? att.getNormalizedOwnValue() | |
491 : ""; | |
492 if (StringUtils.isNotEmpty(attValue) | |
493 && attValue0.contains(attValue)) { | |
494 list.add((Attribute) att.clone()); | |
495 count++; | |
496 if (maxResults > 0 && maxResults == count) { | |
497 break; | |
498 } | |
499 } | |
500 } | |
501 } | |
502 return list; | |
503 } | |
504 | |
505 /** | |
506 * Returns a list of entities. | |
507 * | |
508 * @param def | |
509 * @param attName | |
510 * @param attValue | |
511 * ignores case sensitive | |
512 * @param maxResults | |
513 * @param subString | |
514 * if true the attValue is searching as substring | |
515 * @return | |
516 */ | |
517 public List<Entity> getEntitiesByAtt(String def, String attName, | |
518 String attValue, int maxResults, boolean subString) { | |
519 List<Entity> list = new ArrayList<Entity>(); | |
520 | |
521 List<Attribute> attList = cache.getAttsByOCAndName(def, attName); | |
522 | |
523 int count = 0; | |
524 if (StringUtils.isEmpty(attValue)) { | |
525 for (Attribute att : attList) { | |
526 list.add(getEntityById(att.getSourceId())); | |
527 count++; | |
528 if (maxResults > 0 && maxResults == count) { | |
529 break; | |
530 } | |
531 } | |
532 } else { | |
533 attValue = (StringUtils.isNotEmpty(attValue)) ? NormalizerUtils | |
534 .normalize(attValue) : ""; | |
535 for (Attribute att : attList) { | |
536 String attValue0 = (StringUtils.isNotEmpty(att | |
537 .getNormalizedOwnValue())) ? att | |
538 .getNormalizedOwnValue() : ""; | |
539 if ((subString && attValue0.contains(attValue)) | |
540 || (!subString && attValue.equals(attValue0))) { | |
541 list.add(getEntityById(att.getSourceId())); | |
542 count++; | |
543 if (maxResults > 0 && maxResults == count) { | |
544 break; | |
545 } | |
546 } | |
547 } | |
548 } | |
549 return list; | |
550 } | |
551 | |
552 public void removeDefAttribute(Attribute att) throws Exception { | |
553 this.cache.deleteDefAttribute(att); | |
554 att.setSystemStatus(Node.SYS_STATUS_PREVIOUS_VERSION); | |
555 this.getPS().saveNode(att); | |
556 } | |
557 | |
558 public void removeDefRelation(Relation rel) throws Exception { | |
559 this.cache.deleteDefRelation(rel); | |
560 rel.setSystemStatus(Node.SYS_STATUS_PREVIOUS_VERSION); | |
561 this.getPS().saveNode(rel); | |
562 } | |
563 | |
564 public Entity saveLWDefinition(Entity def, String user) throws Exception { | |
565 boolean isNew = !def.isPersistent(); | |
566 def.setUser(user); | |
567 def.increaseVersion(); | |
568 def.setModificationTime(System.currentTimeMillis()); | |
569 def.setType(Node.TYPE_TBOX); | |
570 def.setObjectClass(Node.TYPE_TBOX); | |
571 def.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); | |
572 this.getPS().saveNode(def); | |
573 | |
574 if (isNew) { | |
575 this.cache.createLWDefinition(def); | |
576 } else { | |
577 this.cache.saveLWDefinition(def); | |
578 } | |
579 | |
580 return def; | |
581 } | |
582 | |
583 public Attribute saveDefAttribute(Attribute att, String user) | |
584 throws Exception { | |
585 att.setUser(user); | |
586 att.increaseVersion(); | |
587 att.setModificationTime(System.currentTimeMillis()); | |
588 att.setObjectClass(Node.TYPE_TBOX); | |
589 att.setType(Node.TYPE_TBOX); | |
590 att.getPossibleValues(); | |
591 this.getPS().saveNode(att); | |
592 this.cache.saveDefAttribute(att); | |
593 return att; | |
594 } | |
595 | |
596 public Relation saveDefRelation(Relation rel, String user) throws Exception { | |
597 rel.setUser(user); | |
598 rel.increaseVersion(); | |
599 rel.setModificationTime(System.currentTimeMillis()); | |
600 rel.setObjectClass(Node.TYPE_TBOX); | |
601 rel.setType(Node.TYPE_TBOX); | |
602 this.getPS().saveNode(rel); | |
603 this.cache.saveDefRelation(rel); | |
604 return rel; | |
605 } | |
606 | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
607 /** |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
608 * Save the given entity as a separate new entity with a new ID. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
609 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
610 * Keeps attributes and relations as copies. |
1 | 611 * |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
612 * @param entity |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
613 * @param user |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
614 * @return |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
615 * @throws Exception |
1 | 616 */ |
617 public Entity saveEntityAsNew(Entity entity, String user) throws Exception { | |
618 long start = System.currentTimeMillis(); | |
619 entity.resetRowId(); | |
620 entity.resetId(); | |
621 entity.setType(Node.TYPE_ABOX); | |
622 entity.setUser(user); | |
45 | 623 |
624 txLog.debug("** START save entity as new: user="+user+" entity="+entity.toSmallString()); | |
625 // save in database | |
1 | 626 this.getPS().saveEntity(entity); |
45 | 627 // save in cache |
1 | 628 cache.saveEntity(entity); |
45 | 629 txLog.debug("** END save entity as new: user="+user+" entity="+entity.toSmallString()); |
630 | |
631 logger.debug("[U=" + user + "] SaveEntityAsNew - execution time[ms]: " + (System.currentTimeMillis() - start)); | |
1 | 632 return (Entity) entity.clone(); |
633 } | |
634 | |
36 | 635 /** |
636 * Save the given entity to the database. | |
637 * | |
638 * Creates a new version of the entity. | |
639 * | |
640 * @param entity | |
641 * @param user | |
642 * @return | |
643 * @throws Exception | |
644 */ | |
1 | 645 public Entity saveEntity(Entity entity, String user) throws Exception { |
646 long start = System.currentTimeMillis(); | |
647 if (StringUtils.isEmpty(entity.getType())) { | |
648 entity.setType(Node.TYPE_ABOX); | |
649 } | |
650 entity.setUser(user); | |
651 | |
652 entity = removeWrongRelations(entity); | |
653 entity = removeEmptyAttributes(entity); | |
654 | |
45 | 655 txLog.debug("** START save entity: user="+user+" entity="+entity.toSmallString()); |
656 // save in database | |
1 | 657 this.getPS().saveEntity(entity); |
45 | 658 // save in cache |
1 | 659 cache.saveEntity(entity); |
45 | 660 txLog.debug("** END save entity: user="+user+" entity="+entity.toSmallString()); |
661 | |
1 | 662 System.gc(); |
45 | 663 logger.debug("[U=" + user + "] SaveEntity - execution time[ms]: " + (System.currentTimeMillis() - start)); |
1 | 664 Entity clone = (Entity) entity.clone(); |
665 // logger.info(clone.toString()); | |
666 return clone; | |
667 } | |
668 | |
669 public void saveEntityList(List<Entity> list, String user) throws Exception{ | |
670 long start = System.currentTimeMillis(); | |
671 for(Entity entity : list){ | |
672 if (StringUtils.isEmpty(entity.getType())) { | |
673 entity.setType(Node.TYPE_ABOX); | |
674 } | |
675 entity.setUser(user); | |
676 | |
677 entity = removeWrongRelations(entity); | |
678 entity = removeEmptyAttributes(entity); | |
679 } | |
45 | 680 txLog.debug("** START save entity list: user="+user+" list="+list); |
1 | 681 this.getPS().saveEntityList(list); |
682 for(Entity entity : list){ | |
683 cache.saveEntity(entity); | |
684 } | |
45 | 685 txLog.debug("** END save entity list: user="+user+" list="+list); |
686 logger.debug("[U=" + user + "] SaveEntityList - execution time[ms]: " + (System.currentTimeMillis() - start)); | |
1 | 687 } |
688 | |
689 private Entity removeEmptyAttributes(Entity ent){ | |
690 | |
691 for(Attribute att : new ArrayList<Attribute>(ent.getAttributes())){ | |
692 if(StringUtils.isEmpty(att.getValue())){ | |
693 ent.getAttributes().remove(att); | |
694 } | |
695 } | |
696 return ent; | |
697 | |
698 } | |
699 | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
700 /** |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
701 * Returns if this entity has wrong relations. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
702 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
703 * Wrong are source relations whose target doesn't exist. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
704 * Similarly for target relations. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
705 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
706 * @param ent |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
707 * @return |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
708 */ |
1 | 709 private boolean hasWrongRelations(Entity ent) { |
710 for (Relation srcRel : ent.getSourceRelations()) { | |
711 if (getEntityById(srcRel.getTargetId()) == null) { | |
712 return true; | |
713 } | |
714 } | |
715 | |
716 for (Relation tarRel : ent.getTargetRelations()) { | |
717 if (getEntityById(tarRel.getSourceId()) == null) { | |
718 return true; | |
719 } | |
720 } | |
721 return false; | |
722 } | |
723 | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
724 /** |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
725 * Remove wrong relations from this entity. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
726 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
727 * Wrong are source relations whose target is null or doesn't exist. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
728 * Similarly for target relations. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
729 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
730 * @param ent |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
731 * @return |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
732 */ |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
733 private Entity removeWrongRelations(Entity ent) { |
1 | 734 |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
735 for (Relation srcRel : new ArrayList<Relation>(ent.getSourceRelations())) { |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
736 if (srcRel.getTargetId() == null || getEntityByIdReadOnly(srcRel.getTargetId()) == null) { |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
737 ent.getSourceRelations().remove(srcRel); |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
738 logger.error("Inconsistency detected saving entity [" + ent.getId() + "] " + srcRel.toString()); |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
739 } |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
740 } |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
741 |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
742 for (Relation tarRel : new ArrayList<Relation>(ent.getTargetRelations())) { |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
743 if (tarRel.getSourceId() == null || getEntityByIdReadOnly(tarRel.getSourceId()) == null) { |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
744 ent.getTargetRelations().remove(tarRel); |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
745 logger.error("Inconsistency detected saving entity [" + ent.getId() + "] " + tarRel.toString()); |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
746 } |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
747 } |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
748 |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
749 return ent; |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
750 } |
1 | 751 |
752 public Attribute getDefAttributeByOwnValue(String defOC, String attOW) { | |
753 for (Attribute att : this.cache.getDefAttributes(defOC)) { | |
754 if (att.getOwnValue().equals(attOW)) { | |
755 return att; | |
756 } | |
757 } | |
758 return null; | |
759 } | |
760 | |
761 public List<Attribute> getDefRelationAttributes(Long id) { | |
762 return this.cache.getDefAttributesById(id); | |
763 } | |
38 | 764 |
765 /** | |
766 * Returns the attributes of the definition of the given type. | |
767 * | |
768 * @param objectClass | |
769 * @return | |
770 */ | |
771 public List<Attribute> getDefAttributes(String objectClass) { | |
772 return this.cache.getDefAttributes(objectClass); | |
1 | 773 } |
774 | |
38 | 775 /** |
776 * Returns the source relations of the definition of the given type. | |
777 * | |
778 * @param objectClass | |
779 * @return | |
780 */ | |
781 public List<Relation> getDefSourceRelations(String objectClass) { | |
782 return this.cache.getDefSourceRelations(objectClass); | |
1 | 783 } |
784 | |
38 | 785 /** |
786 * Returns the target relations of the definition of the given type. | |
787 * | |
788 * @param objectClass | |
789 * @return | |
790 */ | |
791 public List<Relation> getDefTargetRelations(String objectClass) { | |
792 return this.cache.getDefTargetRelations(objectClass); | |
1 | 793 } |
794 | |
795 public Entity getDefinitionById(Long id) { | |
796 return this.cache.getLWDefinitionById(id); | |
797 } | |
798 | |
38 | 799 /** |
800 * Returns all (lightweight) definition entities. | |
801 * | |
802 * @return | |
803 */ | |
1 | 804 public List<Entity> getLWDefinitions() { |
805 return this.cache.getLWDefinitions(); | |
806 } | |
807 | |
38 | 808 /** |
809 * Returns the (lightweight) definition entity for the given type. | |
810 * | |
811 * @param objectClass | |
812 * @return | |
813 */ | |
814 public Entity getDefinition(String objectClass) { | |
815 return this.cache.getLWDefinition(objectClass); | |
1 | 816 } |
817 | |
818 public List<Relation> getRelation(String name, String srcOC, String tarOC) | |
819 throws Exception { | |
820 // TODO read only??? | |
821 List<Relation> list = new ArrayList<Relation>(); | |
822 if (StringUtils.isNotEmpty(name)) { | |
823 List<Relation> list0 = cache.getRelsByName(name); | |
824 if (StringUtils.isEmpty(srcOC) && StringUtils.isEmpty(tarOC)) { | |
825 list = list0; | |
826 } else { | |
827 for (Relation rel : list0) { | |
828 if ((StringUtils.isEmpty(srcOC) || srcOC.equals(rel | |
829 .getSourceObjectClass())) | |
830 && (StringUtils.isEmpty(tarOC) || tarOC.equals(rel | |
831 .getTargetObjectClass()))) { | |
832 list.add(rel); | |
833 } | |
834 } | |
835 } | |
836 } | |
837 | |
838 return list; | |
839 } | |
840 | |
841 public List<Entity> getSourcesForTargetRelation(Entity tar, | |
842 String relationName, String srcObjClass, int maxResult) { | |
843 return getSourcesForTargetRelation(tar.getId(), relationName, | |
844 srcObjClass, maxResult); | |
845 } | |
846 | |
847 public List<Entity> getSourcesForTargetRelation(Long tarId, | |
848 String relationName, String srcObjClass, int maxResult) { | |
849 List<Entity> rs = new ArrayList<Entity>(); | |
850 | |
851 List<Relation> tarRelList = this.cache.getRelsByTarId(tarId); | |
852 | |
853 long start = System.currentTimeMillis(); | |
854 int count = 0; | |
855 for (Relation rel : tarRelList) { | |
856 if (stringEquals(relationName, rel.getOwnValue()) | |
857 && stringEquals(srcObjClass, rel.getSourceObjectClass())) { | |
858 Entity ent = getEntityByIdReadOnly(rel.getSourceId()); | |
859 if (ent != null) { | |
860 rs.add(ent); | |
861 count++; | |
862 if (maxResult > 0 && count == maxResult) { | |
863 break; | |
864 } | |
865 } | |
866 } | |
867 } | |
868 logger.debug("getSourcesForTargetRelation (loading sources) - execution time[ms]: " | |
869 + (System.currentTimeMillis() - start)); | |
870 Collections.sort(rs, new EntitySortByNormalizedOwnValue()); | |
871 return rs; | |
872 } | |
873 | |
874 public List<Entity> getTargetsForSourceRelation(Entity src, | |
875 String relationName, String tarObjClass, int maxResult) { | |
876 return getTargetsForSourceRelation(src.getId(), relationName, | |
877 tarObjClass, maxResult); | |
878 } | |
879 | |
880 public List<Entity> getTargetsForSourceRelation(Long srcId, | |
881 String relationName, String tarObjClass, int maxResult) { | |
882 List<Entity> rs = new ArrayList<Entity>(); | |
883 | |
884 try { | |
885 Collection<Relation> srcRelList = this.cache.getRelsBySrcId(srcId); | |
886 | |
887 int count = 0; | |
888 for (Relation rel : srcRelList) { | |
889 if (stringEquals(relationName, rel.getOwnValue()) | |
890 && stringEquals(tarObjClass, rel.getTargetObjectClass())) { | |
891 rs.add(getEntityByIdReadOnly(rel.getTargetId())); | |
892 count++; | |
893 if (maxResult > 0 && count == maxResult) { | |
894 break; | |
895 } | |
896 } | |
897 } | |
898 Collections.sort(rs, new EntitySortByNormalizedOwnValue()); | |
899 } catch (Exception e) { | |
900 logger.error(e.getMessage(), e); | |
901 } | |
902 | |
903 return rs; | |
904 } | |
905 | |
906 public List<Relation> getTargetRelations(Entity target, | |
907 String relationName, String srcObjClass, int maxResult) { | |
908 long start = System.currentTimeMillis(); | |
909 List<Relation> list = new ArrayList<Relation>(); | |
910 | |
911 List<Relation> tarRelList = this.cache.getRelsByTarId(target.getId()); | |
912 /* | |
913 * if(coll == null){ coll = this.getPS().getTargetRelations(target, | |
914 * null, null, -1, false); cache.setRelsForTar(target.getId(), coll); } | |
915 */ | |
916 | |
917 int count = 0; | |
918 for (Relation rel : tarRelList) { | |
919 if (stringEquals(relationName, rel.getOwnValue()) | |
920 && stringEquals(srcObjClass, rel.getSourceObjectClass())) { | |
921 Entity source = getEntityById(rel.getSourceId()); | |
922 if (source != null) { | |
923 rel.setTarget(target); | |
924 rel.setSource(source); | |
925 list.add(rel); | |
926 count++; | |
927 if (maxResult > 0 && count == maxResult) { | |
928 break; | |
929 } | |
930 } else { | |
931 logger.error("#########ERROR: Relation without source " | |
932 + rel.toString()); | |
933 } | |
934 | |
935 } | |
936 } | |
937 Collections.sort(list, new RelationSortBySourceOW()); | |
938 logger.debug("getTargetRelations - execution time[ms]: " | |
939 + (System.currentTimeMillis() - start)); | |
940 return list; | |
941 } | |
942 | |
943 /** | |
944 * Returns a list of relations found by entity source. | |
945 * | |
946 * @param source | |
947 * @param relationName | |
948 * @param tarObjClass | |
949 * @param maxResult | |
950 * @return | |
951 */ | |
952 public List<Relation> getSourceRelations(Entity source, | |
953 String relationName, String tarObjClass, int maxResult) | |
954 throws Exception { | |
955 | |
956 long start = System.currentTimeMillis(); | |
957 List<Relation> list = new ArrayList<Relation>(); | |
958 | |
959 // the collection coll should not be modified | |
960 // otherwise java.util.ConcurrentModificationException will be thrown. | |
961 // be carefully using getEntityContent, cause it changes the cache | |
962 | |
963 List<Relation> srcRelList = this.cache.getRelsBySrcId(source.getId()); | |
964 | |
965 int count = 0; | |
966 for (Relation rel : srcRelList) { | |
967 if (stringEquals(relationName, rel.getOwnValue()) | |
968 && stringEquals(tarObjClass, rel.getTargetObjectClass())) { | |
969 rel.setSource(source); | |
970 rel.setTarget(getEntityById(rel.getTargetId())); | |
971 list.add(rel); | |
972 count++; | |
973 if (maxResult > 0 && count == maxResult) { | |
974 break; | |
975 } | |
976 } | |
977 } | |
978 Collections.sort(list, new RelationSortByTargetOW()); | |
979 logger.debug("getSourceRelations - execution time[ms]: " | |
980 + (System.currentTimeMillis() - start)); | |
981 | |
982 return list; | |
983 } | |
984 | |
985 /** | |
54 | 986 * Load and attach the Attributes and Relations of the given Entity. |
987 * | |
988 * Tries to loads from cache first and then from DB, saving to cache. | |
989 * | |
990 * Returns a cloned Entity. | |
991 * | |
1 | 992 * This method should not be used inside this Wrapper class, because it |
54 | 993 * could throw a java.util.ConcurrentModificationException. |
1 | 994 * |
995 * @param ent | |
996 * @return | |
997 */ | |
998 public Entity getEntityContent(Entity ent) { | |
999 try { | |
1000 if (ent.isLightweight()) { | |
1001 Entity e = this.cache.getEntityContent(ent); | |
1002 if (e == null) { | |
1003 e = this.getPS().getEntityContent(ent); | |
1004 this.cache.saveEntity(e); | |
1005 } else { | |
1006 ent = e; | |
1007 } | |
1008 } | |
1009 return (Entity) ent.clone(); | |
1010 } catch (Exception e) { | |
1011 logger.error(e.getMessage(), e); | |
1012 } | |
1013 return null; | |
1014 } | |
1015 | |
1016 public Entity getEntityContentReadOnly(Entity ent) throws Exception { | |
1017 if (ent.isLightweight()) { | |
1018 Entity e = this.cache.getEntityContent(ent); | |
1019 if (e == null) { | |
1020 e = this.getPS().getEntityContent(ent); | |
1021 this.cache.saveEntity(e); | |
1022 } else { | |
1023 ent = e; | |
1024 } | |
1025 } | |
1026 return ent; | |
1027 } | |
1028 | |
1029 /** | |
39 | 1030 * Return an entity with the given id from the cache. |
1031 * | |
1 | 1032 * The entity returned could be LW or not, it depends on the cache. |
1033 * additionally, it will be always a clone of the version in cache, | |
1034 * therefore it method is thought for editing's issues. | |
1035 * | |
1036 * @param id | |
1037 * @return | |
1038 */ | |
1039 public Entity getEntityById(Long id) { | |
1040 Entity ent = this.cache.getEntityById(id); | |
1041 return ent; | |
1042 } | |
1043 | |
39 | 1044 /** |
1045 * Return an entity with the given id from the cache. | |
1046 * | |
1047 * Do not modify the entity! | |
1048 * | |
1049 * @param id | |
1050 * @return | |
1051 */ | |
1 | 1052 public Entity getEntityByIdReadOnly(Long id) { |
1053 Entity ent = this.cache.getEntityByIdReadOnly(id); | |
1054 return ent; | |
1055 } | |
1056 | |
1057 public Entity getClonedEntityById(Long id) { | |
1058 return (Entity) getEntityById(id).clone(); | |
1059 } | |
1060 | |
1061 /** | |
1062 * This method should not be used inside this Wrapper class, because it | |
1063 * could throws an java.util.ConcurrentModificationException. | |
1064 * | |
1065 * @param id | |
1066 * @return | |
1067 */ | |
1068 public Entity getEntityByIdWithContent(Long id) { | |
1069 try { | |
1070 Entity ent = getEntityById(id); | |
1071 if (ent != null && ent.isLightweight()) { | |
1072 ent = getEntityContent(ent); | |
1073 } | |
1074 return ent; | |
1075 } catch (Exception e) { | |
1076 logger.error(e.getMessage(), e); | |
1077 } | |
1078 return null; | |
1079 } | |
1080 | |
1081 public Entity getEntityByIdWithContentReadOnly(Long id) throws Exception { | |
1082 Entity ent = getEntityByIdReadOnly(id); | |
1083 if (ent.isLightweight()) { | |
1084 ent = getEntityContentReadOnly(ent); | |
1085 } | |
1086 return ent; | |
1087 } | |
1088 | |
1089 public static boolean stringEquals(String term, String ow) { | |
1090 if (StringUtils.isEmpty(term)) | |
1091 return true; | |
1092 return term.equals(ow); | |
1093 } | |
1094 | |
1095 // ////**** | |
1096 public Entity getLightweightEntityById(Long id) { | |
1097 Entity entity = null; | |
1098 if (id != null) { | |
1099 List<Entity> list = this.getPS().getLightweightEntities( | |
1100 Node.SYS_STATUS_CURRENT_VERSION, id, null, null, null, | |
1101 true, -1); | |
1102 if (list.size() > 0) { | |
1103 entity = list.get(0); | |
1104 } | |
1105 } | |
1106 return entity; | |
1107 } | |
1108 | |
1109 public List<Entity> getLightweightAssertions(String objectClass, | |
1110 String ownValue, int maxResult) { | |
1111 return getPS().getLightweightEntities(Node.SYS_STATUS_CURRENT_VERSION, | |
1112 null, Node.TYPE_ABOX, objectClass, ownValue, true, maxResult); | |
1113 } | |
1114 | |
38 | 1115 /** |
1116 * Save the entity. Calls {@link #saveEntity(String, String)}. | |
1117 * | |
1118 * @param entity | |
1119 * @param user | |
1120 * @throws Exception | |
1121 */ | |
1 | 1122 public void saveAssertion(Entity entity, String user) throws Exception { |
1123 this.saveEntity(entity, user); | |
1124 } | |
1125 | |
1126 /** | |
1127 * <p> | |
1128 * This method saves a non-persistent entity. | |
1129 * </p> | |
1130 * <p> | |
1131 * The reason of this is that nodes are stored only in the DB and not in the | |
1132 * cache. | |
1133 * </p> | |
1134 * <p> | |
1135 * Later, when the nodes are required, they will be loaded from the BD to | |
1136 * the cache automatically. However there are some structures like | |
1137 * attributes by entId that become inconsistent using this method. | |
1138 * </p> | |
1139 * | |
1140 * @param node | |
1141 * non-persistent node. | |
1142 * @param user | |
1143 * @throws Exception | |
1144 */ | |
1145 public void saveNodeOnlyForScripts(Node node, String user) throws Exception { | |
1146 if (node.isPersistent()) { | |
1147 throw new Exception( | |
1148 "This method can save only non-persistent nodes. Because the nodes are stored only in the DB in not in the Cache."); | |
1149 } | |
1150 if (StringUtils.isEmpty(node.getType())) { | |
1151 throw new Exception("The type of the node can not be empty"); | |
1152 } | |
1153 node.setUser(user); | |
1154 this.getPS().saveNode(node); | |
1155 | |
1156 // if(StringUtils.isNotEmpty(node.getType())){ | |
1157 // if(node.getType().equals(Node.TYPE_ABOX)){ | |
1158 // | |
1159 // }else if(node.getType().equals(Node.TYPE_TBOX)){ | |
1160 // | |
1161 // } | |
1162 // } | |
1163 } | |
1164 | |
1165 public void removeNode(Node node) { | |
1166 this.getPS().removeNode(node); | |
1167 } | |
1168 | |
1169 public Map<Long, Long> saveEntityListAsNew(List<Entity> entities, String user, | |
1170 boolean testWrongRelations) throws Exception { | |
1171 logger.info("\n ### Making persistent Entities size total " | |
1172 + entities.size() + " ### \n"); | |
1173 int sizePart = 1000; | |
1174 List<Entity> tmpList = null; | |
1175 | |
1176 Map<Long, Long> idMap = new HashMap<Long, Long>(); | |
1177 | |
1178 for (int j = 0; j <= (entities.size() / sizePart); j++) { | |
1179 tmpList = new ArrayList<Entity>(sizePart); | |
1180 for (int i = (j * sizePart); i < ((j + 1) * sizePart); i++) { | |
1181 | |
1182 if (i < entities.size()) { | |
1183 if (StringUtils.isNotEmpty(user)) { | |
1184 entities.get(i).setUser(user); | |
1185 } | |
1186 | |
1187 if (testWrongRelations | |
1188 && hasWrongRelations(entities.get(i))) { | |
1189 throw new Exception("Wrong relations " | |
1190 + entities.get(i)); | |
1191 } | |
1192 | |
1193 tmpList.add(entities.get(i)); | |
1194 } else { | |
1195 break; | |
1196 } | |
1197 } | |
1198 | |
1199 logger.info("\nMaking persistent Entities part " + (j + 1) + " of " | |
1200 + ((entities.size() / sizePart) + 1) + " size=" | |
1201 + tmpList.size()); | |
1202 idMap = this.getPS().saveEntityListAsNew(tmpList, idMap); | |
1203 } | |
1204 return idMap; | |
1205 } | |
1206 | |
1207 /* | |
1208 public void saveEntityList(List<Entity> entities, String user, | |
1209 boolean testWrongRelations) throws Exception { | |
1210 logger.info("\n ### Making persistent Entities size total " | |
1211 + entities.size() + " ### \n"); | |
1212 int sizePart = 1000; | |
1213 List<Entity> tmpList = null; | |
1214 | |
1215 for (int j = 0; j <= (entities.size() / sizePart); j++) { | |
1216 tmpList = new ArrayList<Entity>(sizePart); | |
1217 for (int i = (j * sizePart); i < ((j + 1) * sizePart); i++) { | |
1218 | |
1219 if (i < entities.size()) { | |
1220 if (StringUtils.isNotEmpty(user)) { | |
1221 entities.get(i).setUser(user); | |
1222 } | |
1223 | |
1224 if (testWrongRelations | |
1225 && hasWrongRelations(entities.get(i))) { | |
1226 throw new Exception("Wrong relations " | |
1227 + entities.get(i)); | |
1228 } | |
1229 | |
1230 tmpList.add(entities.get(i)); | |
1231 } else { | |
1232 break; | |
1233 } | |
1234 } | |
1235 | |
1236 logger.info("\nMaking persistent Entities part " + (j + 1) + " of " | |
1237 + ((entities.size() / sizePart) + 1) + " size=" | |
1238 + tmpList.size()); | |
1239 this.getPS().saveEntityList(tmpList); | |
1240 | |
1241 for(Entity ent : tmpList){ | |
1242 logger.info("[U=" + user + "] SaveEntity " + ent.toSmallString()); | |
1243 } | |
1244 | |
1245 } | |
1246 }*/ | |
1247 | |
1248 public void saveConcept(Entity entity) throws Exception { | |
1249 entity.setType(Node.TYPE_TBOX); | |
1250 entity.setObjectClass(Node.TYPE_TBOX); | |
1251 this.getPS().saveEntity(entity); | |
1252 } | |
1253 | |
1254 public void saveNodeListOnlyForScripts(List<Node> nodeList, String user) | |
1255 throws Exception { | |
1256 logger.debug("### Making persistent Nodes size total " | |
1257 + nodeList.size() + " ###"); | |
1258 List<Node> list = null; | |
1259 int sizePart = 1000; | |
1260 for (int j = 0; j <= (nodeList.size() / sizePart); j++) { | |
1261 list = new ArrayList<Node>(); | |
1262 for (int i = (j * sizePart); i < ((j + 1) * sizePart); i++) { | |
1263 if (i < nodeList.size()) { | |
1264 nodeList.get(i).setUser(user); | |
1265 list.add(nodeList.get(i)); | |
1266 } | |
1267 } | |
1268 logger.debug("Making persistent Nodes part " + (j + 1) + " of " | |
1269 + ((nodeList.size() / sizePart) + 1) + " size=" | |
1270 + list.size()); | |
1271 this.getPS().saveNodeList(list); | |
1272 } | |
1273 } | |
1274 | |
1275 public void saveNodeListOnlyForScripts(List<Node> nodeList) | |
1276 throws Exception { | |
1277 logger.info("\n ### Making persistent Nodes size total " | |
1278 + nodeList.size() + " ### \n"); | |
1279 int sizePart = 1000; | |
1280 List<Node> list = null; | |
1281 for (int j = 0; j <= (nodeList.size() / sizePart); j++) { | |
1282 list = new ArrayList<Node>(); | |
1283 for (int i = (j * sizePart); i < ((j + 1) * sizePart); i++) { | |
1284 if (i < nodeList.size()) { | |
1285 list.add(nodeList.get(i)); | |
1286 } | |
1287 } | |
1288 logger.info("Making persistent Nodes part " + (j + 1) + " of " | |
1289 + ((nodeList.size() / sizePart) + 1) + " size=" | |
1290 + list.size()); | |
1291 this.getPS().saveNodeList(list); | |
1292 } | |
1293 // this.persistenceService.saveNodeList(nodeList); | |
1294 } | |
1295 | |
1296 public List<Entity> getConcepts() { | |
1297 return this.getPS().getEntities(null, Node.SYS_STATUS_CURRENT_VERSION, | |
1298 Node.TYPE_TBOX, null); | |
1299 } | |
1300 | |
1301 public List<Entity> getAssertion() { | |
1302 return this.getPS().getEntities(null, Node.SYS_STATUS_CURRENT_VERSION, | |
1303 Node.TYPE_ABOX, null); | |
1304 } | |
1305 | |
1306 public List<Entity> getLightweightAssertionsByExactOwnValue( | |
1307 String objectClass, String ownValue, int maxResult) { | |
1308 return getPS().getLightweightEntities(Node.SYS_STATUS_CURRENT_VERSION, | |
1309 null, Node.TYPE_ABOX, objectClass, ownValue, false, maxResult); | |
1310 } | |
1311 | |
1312 public void removeNodeList(List<Node> nodeList) { | |
1313 System.out.println("\n ### Deleting Nodes size total " | |
1314 + nodeList.size() + " ### \n"); | |
1315 this.getPS().removeNodeList(nodeList); | |
1316 } | |
1317 | |
1318 public void deleteAllConcepts() { | |
1319 // persistenceService.deleteEntities(null, Node.TYPE_TBOX, true); | |
1320 getPS().dropDefinitions(); | |
1321 } | |
1322 | |
1323 public void importOM3Concepts(String fileName, Boolean dropConcepts) { | |
1324 getPS().setImportModus(true); | |
1325 ImportOM3Util.importConcepts(this, fileName, dropConcepts); | |
1326 getPS().setImportModus(false); | |
1327 } | |
1328 | |
1329 // /// | |
1330 | |
1331 public PersistenceService getPS() { | |
1332 return this.cache.getPs(); | |
1333 } | |
1334 | |
1335 public CacheService getCache() { | |
1336 return cache; | |
1337 } | |
1338 | |
1339 public void setCache(CacheService cache) { | |
1340 this.cache = cache; | |
1341 } | |
1342 | |
1343 public List<ViewerAttribute> getViewerAttributes(Long page){ | |
1344 List<ViewerAttribute> list = cache.getViewerAttributes(page); | |
1345 Collections.sort(list); | |
1346 return list; | |
1347 } | |
1348 | |
1349 public List<ViewerAttribute> getViewerAttributes4Edition(Long page) throws CloneNotSupportedException{ | |
1350 List<ViewerAttribute> list = new ArrayList<ViewerAttribute>(); | |
1351 for(ViewerAttribute att : cache.getViewerAttributes(page)){ | |
1352 list.add((ViewerAttribute)att.clone()); | |
1353 } | |
1354 Collections.sort(list); | |
1355 return list; | |
1356 } | |
1357 | |
1358 public Collection<ViewerPage> getViewerPages(){ | |
1359 return cache.getViewerPages(); | |
1360 } | |
1361 | |
1362 public ViewerPage getViewerPage(Long id){ | |
1363 return cache.getViewerPageMap().get(id); | |
1364 } | |
1365 | |
1366 public ViewerPage getViewerPage(String definition){ | |
1367 for(ViewerPage page : cache.getViewerPages()){ | |
1368 if(page.getDefinition().equals(definition)){ | |
1369 return page; | |
1370 } | |
1371 } | |
1372 return null; | |
1373 } | |
1374 | |
1375 public ViewerPage getViewerPage4Edition(Long id) throws CloneNotSupportedException{ | |
1376 ViewerPage page = cache.getViewerPageMap().get(id); | |
1377 return (page != null) ? (ViewerPage) page.clone() : null; | |
1378 } | |
1379 | |
1380 public ViewerPage saveViewerPage(ViewerPage page, String user){ | |
1381 page.setUser(user); | |
1382 return cache.saveViewerPage(page); | |
1383 | |
1384 } | |
1385 | |
1386 public ViewerAttribute saveViewerAttribute(ViewerPage page, ViewerAttribute att, String user) throws Exception{ | |
1387 att.setUser(user); | |
1388 return cache.saveViewerAttribute(page, att); | |
1389 } | |
1390 | |
1391 /** | |
1392 * Removes a page and all attributes associated with it. | |
1393 * @param page | |
1394 * @return | |
1395 */ | |
1396 public int removeViewerPage(ViewerPage page){ | |
1397 | |
1398 return (page.getId() != null) ? cache.removeViewerPage(page.getId()) : 0; | |
1399 } | |
1400 | |
1401 public boolean removeViewerAnttribute(ViewerAttribute att){ | |
1402 return (att.getId() != null) ? cache.removeAtt(att.getId()) : false; | |
1403 } | |
1404 } |