1
|
1 package de.mpiwg.itgroup.ismi.search.beans;
|
|
2
|
|
3 import java.io.Serializable;
|
|
4 import java.util.ArrayList;
|
|
5 import java.util.Collections;
|
|
6 import java.util.HashMap;
|
|
7 import java.util.List;
|
|
8 import java.util.Map;
|
|
9
|
|
10 import javax.faces.event.ActionEvent;
|
|
11 import javax.faces.event.ValueChangeEvent;
|
|
12 import javax.faces.model.SelectItem;
|
|
13
|
|
14 import org.apache.commons.lang.StringUtils;
|
|
15 import org.apache.log4j.Logger;
|
|
16 import org.mpi.openmind.repository.bo.Attribute;
|
|
17 import org.mpi.openmind.repository.bo.Entity;
|
|
18 import org.mpi.openmind.repository.bo.Relation;
|
|
19 import org.mpi.openmind.repository.bo.utils.EntitySortByNormalizedOwnValue;
|
|
20 import org.mpi.openmind.repository.services.utils.AttributeFilter;
|
|
21
|
|
22 import de.mpiwg.itgroup.ismi.auxObjects.ResultSet;
|
|
23 import de.mpiwg.itgroup.ismi.entry.beans.AbstractISMIBean;
|
|
24 import de.mpiwg.itgroup.ismi.entry.beans.SessionBean;
|
|
25
|
|
26 public class SimpleSearchBean extends AbstractISMIBean implements Serializable{
|
|
27
|
|
28 private static final long serialVersionUID = -1363042229412197533L;
|
|
29
|
|
30 private static Logger logger = Logger.getLogger(SimpleSearchBean.class);
|
|
31
|
|
32 public static String NO_ROLE_PERSON = "Person without role";
|
|
33
|
|
34 private String inputMethod = "latin";
|
|
35 private String searchTerm;
|
|
36
|
|
37 public static List<SelectItem> modeList = new ArrayList<SelectItem>();
|
|
38 public static String AUTHORS_TITLES = "People and Titles";
|
|
39 public static String TITLES = "Titles";
|
|
40 public static String AUTHORS = "People";
|
|
41
|
|
42 public static List<AttributeFilter> filters = new ArrayList<AttributeFilter>();
|
|
43 public static List<AttributeFilter> titlesFilters = new ArrayList<AttributeFilter>();
|
|
44 public static List<AttributeFilter> authorsFilters = new ArrayList<AttributeFilter>();
|
|
45 private static AttributeFilter filter1 = new AttributeFilter();
|
|
46 private static AttributeFilter filter2 = new AttributeFilter();
|
|
47 private static AttributeFilter filter3 = new AttributeFilter();
|
|
48 private static AttributeFilter filter4 = new AttributeFilter();
|
|
49 private static AttributeFilter filter5 = new AttributeFilter();
|
|
50 private static AttributeFilter filter6 = new AttributeFilter();
|
|
51 private static AttributeFilter filter7 = new AttributeFilter();
|
|
52
|
|
53 private Map<String, ResultSet> resultMap;
|
|
54 private List<String> resultSetNames;
|
|
55
|
|
56 public String selectedMode;
|
|
57
|
|
58 static{
|
|
59 modeList.add(new SelectItem(AUTHORS));
|
|
60 modeList.add(new SelectItem(TITLES));
|
|
61 modeList.add(new SelectItem(AUTHORS_TITLES));
|
|
62 }
|
|
63
|
|
64 public SimpleSearchBean() {
|
|
65 this.selectedMode = modeList.get(0).getValue().toString();
|
|
66 }
|
|
67
|
|
68
|
|
69 public void listenerSearch(ActionEvent event){
|
|
70 try {
|
|
71 this.simpleSearchAction();
|
|
72 } catch (Exception e) {
|
|
73 logger.error(e.getMessage(), e);
|
|
74 }
|
|
75
|
|
76 }
|
|
77
|
|
78 public void listenerModeValueChange(ValueChangeEvent event){
|
|
79 this.selectedMode = (String)event.getNewValue();
|
|
80 }
|
|
81
|
|
82 public void setInputMethod(String inputMethod) {
|
|
83 this.inputMethod = inputMethod;
|
|
84 }
|
|
85 public String getInputMethod() {
|
|
86 return inputMethod;
|
|
87 }
|
|
88 public void setSearchTerm(String searchTerm) {
|
|
89 this.searchTerm = searchTerm;
|
|
90 }
|
|
91 public String getSearchTerm() {
|
|
92 return searchTerm;
|
|
93 }
|
|
94
|
|
95 public void simpleSearchAction() throws Exception{
|
|
96 search0(getSearchTerm(), getSelectedMode());
|
|
97 }
|
|
98
|
|
99 public List<SelectItem> getModeList() {
|
|
100 return modeList;
|
|
101 }
|
|
102
|
|
103 public String getSelectedMode() {
|
|
104 return selectedMode;
|
|
105 }
|
|
106 public void setSelectedMode(String selectedMode) {
|
|
107 this.selectedMode = selectedMode;
|
|
108 }
|
|
109
|
|
110 //######################################################
|
|
111 //######################################################
|
|
112 //######################################################
|
|
113
|
|
114 public static Long SEC_05 = new Long(5000);
|
|
115 public static Long SEC_10 = new Long(10000);
|
|
116 public static Long SEC_20 = new Long(20000);
|
|
117 public static Long SEC_30 = new Long(30000);
|
|
118
|
|
119 private int maxResult = -1 ;
|
|
120 private int counter = 0;
|
|
121
|
|
122 public void search0(String term, String mode) throws Exception{
|
|
123
|
|
124 if (StringUtils.isNotEmpty(term)) {
|
|
125 StringBuilder sb = new StringBuilder();
|
|
126 long start = System.currentTimeMillis();
|
|
127 this.resultMap = new HashMap<String, ResultSet>();
|
|
128 List<Long> usedIdList = new ArrayList<Long>();
|
|
129 this.counter = 0;
|
|
130
|
|
131 boolean includeTitles = (SimpleSearchBean.TITLES.equals(mode) || SimpleSearchBean.AUTHORS_TITLES.equals(mode)) ? true : false;
|
|
132 boolean includeAuthors = (SimpleSearchBean.AUTHORS.equals(mode) || SimpleSearchBean.AUTHORS_TITLES.equals(mode) ? true : false);
|
|
133
|
|
134 Map<Entity, Attribute> map = new HashMap<Entity, Attribute>();
|
|
135 if(includeAuthors && includeTitles){
|
129
|
136 map = getWrapper().searchEntityByAttributeFilter(term, filters, -1);
|
1
|
137 }else if(includeAuthors){
|
129
|
138 map = getWrapper().searchEntityByAttributeFilter(term, authorsFilters, -1);
|
1
|
139 }else if(includeTitles){
|
129
|
140 map = getWrapper().searchEntityByAttributeFilter(term, titlesFilters, -1);
|
1
|
141 }
|
129
|
142 sb.append("searchEntityByAttributeFilter0 took " + (System.currentTimeMillis() - start) + "ms. ");
|
1
|
143
|
|
144 for(Entity ent : map.keySet()){
|
|
145 if(counter == 1000 || (System.currentTimeMillis() - start) > SEC_20){
|
|
146 break;
|
|
147 }
|
|
148 Attribute att = map.get(ent);
|
|
149 if(ent.getObjectClass().equals(ALIAS)){
|
|
150 AliasStructure aliasStructure = getAliasStructure(ent, att);
|
|
151 if(aliasStructure.target != null && !usedIdList.contains(aliasStructure.target.getId())){
|
|
152 usedIdList.add(aliasStructure.target.getId());
|
|
153 if(includeAuthors && aliasStructure.target.getObjectClass().equals(PERSON)){
|
|
154 this.insertPerson(aliasStructure.target, att, aliasStructure.description);
|
|
155 }else if(includeTitles && aliasStructure.target.getObjectClass().equals(TEXT)){
|
|
156 this.insertTitle(aliasStructure.target, att, aliasStructure.description);
|
|
157 }
|
|
158 }
|
|
159 }else if(!usedIdList.contains(ent.getId())){
|
|
160 usedIdList.add(ent.getId());
|
|
161 String d = att.getObjectClass() + "=" + att.getOwnValue();
|
|
162 if(ent.getObjectClass().equals(PERSON)){
|
|
163 this.insertPerson(ent, att, d);
|
|
164 }else if(ent.getObjectClass().equals(TEXT)){
|
|
165 this.insertTitle(ent, att, d);
|
|
166 }
|
|
167 }
|
|
168 }
|
|
169
|
|
170 for(String key : resultMap.keySet()){
|
|
171 ResultSet rs = resultMap.get(key);
|
|
172 //System.out.println(key + "= " + rs.getResults().size());
|
|
173 Collections.sort(rs.getResults(), new EntitySortByNormalizedOwnValue());
|
|
174 }
|
|
175
|
|
176 this.resultSetNames = new ArrayList<String>(getResultMap().keySet());
|
|
177 Collections.sort(this.resultSetNames);
|
|
178
|
|
179
|
|
180 long end = System.currentTimeMillis();
|
129
|
181 sb.append("Word=" + term + " ");
|
|
182 sb.append("includeTitles=" + includeTitles + " ");
|
|
183 sb.append("includeAuthors=" + includeAuthors + " ");
|
|
184 sb.append("Simple search took " + (end - start) + "ms");
|
1
|
185 logger.info(sb.toString());
|
|
186
|
|
187 }
|
|
188 }
|
|
189
|
|
190 private void insertTitle(Entity ent, Attribute att, String description){
|
|
191 if(resultMap.get("Title") == null){
|
|
192 resultMap.put("Title", new ResultSet("Title"));
|
|
193 }
|
|
194 resultMap.get("Title").setTuple(ent, att);
|
|
195 resultMap.get("Title").setDescription(ent, description);
|
|
196 counter++;
|
|
197 }
|
|
198
|
|
199 private void insertPerson(Entity ent, Attribute att, String description){
|
|
200 List<String> roleNameList = getCache().getRoleList(ent.getId());
|
|
201 for(String roleName : getCache().getRoleList(ent.getId())){
|
|
202 if(resultMap.get(roleName) == null){
|
|
203 resultMap.put(roleName, new ResultSet(roleName));
|
|
204 }
|
|
205 resultMap.get(roleName).setTuple(ent, att);
|
|
206 resultMap.get(roleName).setDescription(ent, description);
|
|
207 }
|
|
208 if(roleNameList.isEmpty()){
|
|
209 if(resultMap.get(NO_ROLE_PERSON) == null){
|
|
210 resultMap.put(NO_ROLE_PERSON, new ResultSet(NO_ROLE_PERSON));
|
|
211 }
|
|
212 resultMap.get(NO_ROLE_PERSON).setTuple(ent, att);
|
|
213 resultMap.get(NO_ROLE_PERSON).setDescription(ent, description);
|
|
214 }
|
|
215 counter++;
|
|
216 }
|
|
217
|
|
218 public List<String> getResultSetNames(){
|
|
219 return this.resultSetNames;
|
|
220 }
|
|
221
|
|
222 /**
|
|
223 * TODO do it more clever!
|
|
224 * rules:
|
|
225 * ----Text
|
|
226 * is_prime_alias_title_of
|
|
227 * is_alias_title_of
|
|
228 * is_alias_incipit_of
|
|
229 * is_alias_explicit_of
|
|
230 * ----Person
|
|
231 * is_prime_alias_name_of
|
|
232 * is_alias_name_of
|
|
233 * @param alias
|
|
234 * @return
|
|
235 */
|
|
236 private AliasStructure getAliasStructure(Entity alias, Attribute att) throws Exception{
|
|
237 AliasStructure structure = new AliasStructure();
|
|
238 structure.aliasAtt = att;
|
|
239 List<Relation> list = null;
|
|
240 list = getWrapper().getSourceRelations(alias, "is_prime_alias_title_of", "TEXT", 1);
|
|
241 if(list.size() > 0)
|
|
242 structure.setRelation(list.get(0));
|
|
243 list = getWrapper().getSourceRelations(alias, "is_alias_title_of", "TEXT", 1);
|
|
244 if(list.size() > 0)
|
|
245 structure.setRelation(list.get(0));
|
|
246 list = getWrapper().getSourceRelations(alias, "is_alias_incipit_of", "TEXT", 1);
|
|
247 if(list.size() > 0)
|
|
248 structure.setRelation(list.get(0));
|
|
249 list = getWrapper().getSourceRelations(alias, "is_alias_explicit_of", "TEXT", 1);
|
|
250 if(list.size() > 0)
|
|
251 structure.setRelation(list.get(0));
|
|
252 list = getWrapper().getSourceRelations(alias, "is_prime_alias_name_of", "PERSON", 1);
|
|
253 if(list.size() > 0)
|
|
254 structure.setRelation(list.get(0));
|
|
255 list = getWrapper().getSourceRelations(alias, "is_alias_name_of", "PERSON", 1);
|
|
256 if(list.size() > 0)
|
|
257 structure.setRelation(list.get(0));
|
|
258 return structure;
|
|
259 }
|
|
260
|
|
261 public int getMaxResult() {
|
|
262 return maxResult;
|
|
263 }
|
|
264
|
|
265 public void setMaxResult(int maxResult) {
|
|
266 this.maxResult = maxResult;
|
|
267 }
|
|
268 public int getCounter() {
|
|
269 return counter;
|
|
270 }
|
|
271
|
|
272 public void setCounter(int counter) {
|
|
273 this.counter = counter;
|
|
274 }
|
|
275
|
|
276
|
|
277 public Map<String, ResultSet> getResultMap() {
|
|
278 return resultMap;
|
|
279 }
|
|
280
|
|
281
|
|
282 public void setResultMap(Map<String, ResultSet> resultMap) {
|
|
283 this.resultMap = resultMap;
|
|
284 }
|
|
285
|
|
286 private class AliasStructure{
|
|
287 public Entity target = null;
|
|
288 public Entity alias = null;
|
|
289 public Relation rel = null;
|
|
290 public String description = "";
|
|
291 public Attribute aliasAtt = null;
|
|
292
|
|
293 public void setRelation(Relation rel){
|
|
294 this.rel = rel;
|
|
295 this.target = rel.getTarget();
|
|
296 this.alias = rel.getSource();
|
|
297 this.description = rel.getOwnValue() + " <- ALIAS [alias=" + aliasAtt.getOwnValue() + "]";
|
|
298 }
|
|
299 }
|
|
300
|
|
301 static{
|
|
302 filter1.setEntObjectClass("PERSON");
|
|
303 filter1.setName("name");
|
|
304 //filter1.setNormalize(true);
|
|
305
|
|
306 filter2.setEntObjectClass("TEXT");
|
|
307 filter2.setName("title");
|
|
308 //filter2.setNormalize(true);
|
|
309
|
|
310 filter3.setEntObjectClass("PERSON");
|
|
311 filter3.setName("name_translit");
|
|
312
|
|
313 //filter3.setNormalize(true);
|
|
314
|
|
315 filter4.setEntObjectClass("TEXT");
|
|
316 filter4.setName("title_translit");
|
|
317
|
|
318 //filter4.setNormalize(true);
|
|
319
|
|
320 filter5.setEntObjectClass("TEXT");
|
|
321 filter5.setName("full_title");
|
|
322
|
|
323 //filter4.setNormalize(true);
|
|
324
|
|
325 filter6.setEntObjectClass("TEXT");
|
|
326 filter6.setName("full_title_translit");
|
|
327
|
|
328
|
|
329 //filers for alias
|
|
330 filter7.setEntObjectClass("ALIAS");
|
|
331 filter7.setName("alias");
|
|
332
|
|
333 authorsFilters.add(filter1);
|
|
334 authorsFilters.add(filter3);
|
|
335 authorsFilters.add(filter7);
|
|
336
|
|
337 titlesFilters.add(filter2);
|
|
338 titlesFilters.add(filter4);
|
|
339 titlesFilters.add(filter5);
|
|
340 titlesFilters.add(filter6);
|
|
341 titlesFilters.add(filter7);
|
|
342
|
|
343 /*
|
|
344 filter1.setOwnValue(search);
|
|
345 filter2.setOwnValue(search);
|
|
346 filter3.setOwnValue(search);
|
|
347 filter4.setOwnValue(search);
|
|
348 filter5.setOwnValue(search);
|
|
349 filter6.setOwnValue(search);
|
|
350 filter7.setOwnValue(search);
|
|
351 */
|
|
352
|
|
353 filters.add(filter1);
|
|
354 filters.add(filter2);
|
|
355 filters.add(filter3);
|
|
356 filters.add(filter4);
|
|
357 filters.add(filter5);
|
|
358 filters.add(filter6);
|
|
359 filters.add(filter7);
|
|
360 }
|
|
361 }
|