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