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