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