1
|
1 package org.mpi.openmind.repository.utils;
|
|
2
|
|
3 import java.text.DecimalFormat;
|
|
4 import java.util.ArrayList;
|
|
5 import java.util.Arrays;
|
|
6 import java.util.HashSet;
|
|
7 import java.util.List;
|
|
8 import java.util.Set;
|
|
9
|
|
10 import org.apache.commons.lang.StringUtils;
|
|
11 import org.mpi.openmind.cache.WrapperService;
|
|
12 import org.mpi.openmind.repository.bo.Attribute;
|
|
13 import org.mpi.openmind.repository.bo.Entity;
|
|
14 import org.mpi.openmind.repository.bo.ViewerAttribute;
|
|
15 import org.mpi.openmind.repository.utils.ismi.ISMICalendar;
|
|
16
|
|
17 public class OMUtils {
|
|
18
|
|
19 private static String SOURCE = "source";
|
|
20 private static String TARGET = "target";
|
|
21
|
|
22
|
|
23 public static List<String> resolveQuery(Long entBaseId, String query, WrapperService ws, Integer contentType) throws Exception{
|
|
24
|
|
25 List<String> rs = new ArrayList<String>();
|
|
26 String[] array = query.split("&");
|
|
27
|
|
28 for(String element : array){
|
|
29 String[] array0 = element.split("\\|");
|
|
30 List<String> terms = new ArrayList<String>(Arrays.asList(array0));
|
|
31
|
|
32 rs.addAll(new ArrayList<String>(entityQuery(entBaseId, terms.remove(0), terms, ws, contentType)));
|
|
33 }
|
|
34
|
|
35 return rs;
|
|
36 }
|
|
37
|
|
38 private static Set<String> entityQuery(Long currentEntId, String currentTerm, List<String> terms, WrapperService ws, Integer contentType) throws Exception{
|
|
39 Set<String> rs;
|
|
40
|
|
41 if(terms.size() == 0){
|
|
42 rs = new HashSet<String>();
|
|
43
|
|
44 String[] array = currentTerm.split(":");
|
|
45 if(array.length > 1){
|
|
46 if(StringUtils.equals(array[1], "id")){
|
|
47 rs.add(currentEntId.toString());
|
|
48 }else{
|
|
49 //returns attribute
|
|
50 Attribute att = ws.getAttributeByName(currentEntId, array[1]);
|
|
51 if(att != null)
|
|
52 rs.add(att.getValue());
|
|
53 }
|
|
54 }else{
|
|
55 //returns ownvalue
|
|
56 Entity currentEntity = ws.getEntityById(currentEntId);
|
|
57 String ownValue = currentEntity.getOwnValue();
|
|
58 if(contentType == null || contentType.equals(ViewerAttribute.CONTENT_TEXT)){
|
|
59 //nothing
|
|
60 }else if(contentType.equals(ViewerAttribute.CONTENT_DATE)){
|
|
61 ISMICalendar cal = new ISMICalendar(ownValue);
|
|
62 ownValue = cal.getCalendarAsHtml();
|
|
63 }
|
|
64 rs.add(ownValue);
|
|
65
|
|
66 }
|
|
67 }else{
|
|
68
|
|
69 rs = relationQuery(currentEntId, terms.remove(0), new ArrayList<String>(terms), ws, contentType);
|
|
70 }
|
|
71 return rs;
|
|
72 }
|
|
73
|
|
74 private static Set<String> relationQuery(Long currentEntId, String currentTerm, List<String> terms, WrapperService ws, Integer contentType) throws Exception{
|
|
75 Set<String> rs = new HashSet<String>();
|
|
76
|
|
77 String[] array = currentTerm.split(":");
|
|
78 if(array.length != 2 &&
|
|
79 (!SOURCE.equals(array[0]) || !TARGET.equals(array[0]))){
|
|
80 throw new Exception("Relation item has not valid format: " + currentTerm);
|
|
81 }
|
|
82
|
|
83 if(terms.size() == 0){
|
|
84 throw new Exception("ViewerAttribute ended with a relation term: " + currentTerm);
|
|
85 }
|
|
86
|
|
87 String nextTerm = terms.get(0);
|
|
88 String classNextTerm = nextTerm.split(":")[0];
|
|
89
|
|
90 terms.remove(0);
|
|
91 List<Entity> nextList;
|
|
92 if(SOURCE.equals(array[0])){
|
|
93 nextList = ws.getTargetsForSourceRelation(currentEntId, array[1], classNextTerm, -1);
|
|
94
|
|
95 }else{
|
|
96 nextList = ws.getSourcesForTargetRelation(currentEntId, array[1], classNextTerm, -1);
|
|
97 }
|
|
98
|
|
99 for(Entity ent : nextList){
|
|
100 Set<String> rs0 = entityQuery(ent.getId(), nextTerm, new ArrayList<String>(terms), ws, contentType);
|
|
101 rs.addAll(rs0);
|
|
102 }
|
|
103
|
|
104 return rs;
|
|
105 }
|
|
106
|
38
|
107 /**
|
|
108 * Returns if the two Longs are equal or both null.
|
|
109 *
|
|
110 * @param one
|
|
111 * @param two
|
|
112 * @return
|
|
113 */
|
1
|
114 public static boolean equals(Long one, Long two){
|
|
115 if(one == null && two == null){
|
|
116 return true;
|
|
117 }
|
|
118
|
|
119 if(one == null || two == null){
|
|
120 return false;
|
|
121 }
|
|
122
|
|
123 return one.equals(two);
|
|
124 }
|
|
125
|
|
126
|
|
127 public static String percentage(int index, int total) {
|
|
128 double p = (100 * (double) index) / (double) total;
|
|
129 DecimalFormat df = new DecimalFormat("#.#");
|
|
130 return df.format(p);
|
|
131 }
|
|
132
|
|
133 }
|