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