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