Mercurial > hg > ismi-richfaces
comparison src/main/java/de/mpiwg/itgroup/ismi/search/beans/SimpleSearchBean.java @ 1:2e911857a759
(none)
author | jurzua |
---|---|
date | Wed, 29 Oct 2014 14:00:28 +0000 |
parents | |
children | 42193ad7b28c |
comparison
equal
deleted
inserted
replaced
0:74df02964906 | 1:2e911857a759 |
---|---|
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){ | |
136 map = getWrapper().searchEntityByAttributeFilter0(term, filters, -1); | |
137 }else if(includeAuthors){ | |
138 map = getWrapper().searchEntityByAttributeFilter0(term, authorsFilters, -1); | |
139 }else if(includeTitles){ | |
140 map = getWrapper().searchEntityByAttributeFilter0(term, titlesFilters, -1); | |
141 } | |
142 //System.out.println("getWrapper().searchEntityByAttributeFilter0= " + (System.currentTimeMillis() - start)); | |
143 sb.append("\n\n***********************\n"); | |
144 sb.append("getWrapper().searchEntityByAttributeFilter0= " + (System.currentTimeMillis() - start) + "\n"); | |
145 | |
146 for(Entity ent : map.keySet()){ | |
147 if(counter == 1000 || (System.currentTimeMillis() - start) > SEC_20){ | |
148 break; | |
149 } | |
150 Attribute att = map.get(ent); | |
151 if(ent.getObjectClass().equals(ALIAS)){ | |
152 AliasStructure aliasStructure = getAliasStructure(ent, att); | |
153 if(aliasStructure.target != null && !usedIdList.contains(aliasStructure.target.getId())){ | |
154 usedIdList.add(aliasStructure.target.getId()); | |
155 if(includeAuthors && aliasStructure.target.getObjectClass().equals(PERSON)){ | |
156 this.insertPerson(aliasStructure.target, att, aliasStructure.description); | |
157 }else if(includeTitles && aliasStructure.target.getObjectClass().equals(TEXT)){ | |
158 this.insertTitle(aliasStructure.target, att, aliasStructure.description); | |
159 } | |
160 } | |
161 }else if(!usedIdList.contains(ent.getId())){ | |
162 usedIdList.add(ent.getId()); | |
163 String d = att.getObjectClass() + "=" + att.getOwnValue(); | |
164 if(ent.getObjectClass().equals(PERSON)){ | |
165 this.insertPerson(ent, att, d); | |
166 }else if(ent.getObjectClass().equals(TEXT)){ | |
167 this.insertTitle(ent, att, d); | |
168 } | |
169 } | |
170 } | |
171 | |
172 for(String key : resultMap.keySet()){ | |
173 ResultSet rs = resultMap.get(key); | |
174 //System.out.println(key + "= " + rs.getResults().size()); | |
175 Collections.sort(rs.getResults(), new EntitySortByNormalizedOwnValue()); | |
176 } | |
177 | |
178 this.resultSetNames = new ArrayList<String>(getResultMap().keySet()); | |
179 Collections.sort(this.resultSetNames); | |
180 | |
181 | |
182 long end = System.currentTimeMillis(); | |
183 sb.append("Word: " + term + "\n"); | |
184 sb.append("includeTitles: " + includeTitles + "\n"); | |
185 sb.append("includeAuthors: " + includeAuthors + "\n"); | |
186 sb.append("Simple search time execution= " + (end - start) + "\n"); | |
187 sb.append("***********************\n"); | |
188 sb.append("\n"); | |
189 logger.info(sb.toString()); | |
190 | |
191 } | |
192 } | |
193 | |
194 private void insertTitle(Entity ent, Attribute att, String description){ | |
195 if(resultMap.get("Title") == null){ | |
196 resultMap.put("Title", new ResultSet("Title")); | |
197 } | |
198 resultMap.get("Title").setTuple(ent, att); | |
199 resultMap.get("Title").setDescription(ent, description); | |
200 counter++; | |
201 } | |
202 | |
203 private void insertPerson(Entity ent, Attribute att, String description){ | |
204 List<String> roleNameList = getCache().getRoleList(ent.getId()); | |
205 for(String roleName : getCache().getRoleList(ent.getId())){ | |
206 if(resultMap.get(roleName) == null){ | |
207 resultMap.put(roleName, new ResultSet(roleName)); | |
208 } | |
209 resultMap.get(roleName).setTuple(ent, att); | |
210 resultMap.get(roleName).setDescription(ent, description); | |
211 } | |
212 if(roleNameList.isEmpty()){ | |
213 if(resultMap.get(NO_ROLE_PERSON) == null){ | |
214 resultMap.put(NO_ROLE_PERSON, new ResultSet(NO_ROLE_PERSON)); | |
215 } | |
216 resultMap.get(NO_ROLE_PERSON).setTuple(ent, att); | |
217 resultMap.get(NO_ROLE_PERSON).setDescription(ent, description); | |
218 } | |
219 counter++; | |
220 } | |
221 | |
222 public List<String> getResultSetNames(){ | |
223 return this.resultSetNames; | |
224 } | |
225 | |
226 /** | |
227 * TODO do it more clever! | |
228 * rules: | |
229 * ----Text | |
230 * is_prime_alias_title_of | |
231 * is_alias_title_of | |
232 * is_alias_incipit_of | |
233 * is_alias_explicit_of | |
234 * ----Person | |
235 * is_prime_alias_name_of | |
236 * is_alias_name_of | |
237 * @param alias | |
238 * @return | |
239 */ | |
240 private AliasStructure getAliasStructure(Entity alias, Attribute att) throws Exception{ | |
241 AliasStructure structure = new AliasStructure(); | |
242 structure.aliasAtt = att; | |
243 List<Relation> list = null; | |
244 list = getWrapper().getSourceRelations(alias, "is_prime_alias_title_of", "TEXT", 1); | |
245 if(list.size() > 0) | |
246 structure.setRelation(list.get(0)); | |
247 list = getWrapper().getSourceRelations(alias, "is_alias_title_of", "TEXT", 1); | |
248 if(list.size() > 0) | |
249 structure.setRelation(list.get(0)); | |
250 list = getWrapper().getSourceRelations(alias, "is_alias_incipit_of", "TEXT", 1); | |
251 if(list.size() > 0) | |
252 structure.setRelation(list.get(0)); | |
253 list = getWrapper().getSourceRelations(alias, "is_alias_explicit_of", "TEXT", 1); | |
254 if(list.size() > 0) | |
255 structure.setRelation(list.get(0)); | |
256 list = getWrapper().getSourceRelations(alias, "is_prime_alias_name_of", "PERSON", 1); | |
257 if(list.size() > 0) | |
258 structure.setRelation(list.get(0)); | |
259 list = getWrapper().getSourceRelations(alias, "is_alias_name_of", "PERSON", 1); | |
260 if(list.size() > 0) | |
261 structure.setRelation(list.get(0)); | |
262 return structure; | |
263 } | |
264 | |
265 public int getMaxResult() { | |
266 return maxResult; | |
267 } | |
268 | |
269 public void setMaxResult(int maxResult) { | |
270 this.maxResult = maxResult; | |
271 } | |
272 public int getCounter() { | |
273 return counter; | |
274 } | |
275 | |
276 public void setCounter(int counter) { | |
277 this.counter = counter; | |
278 } | |
279 | |
280 | |
281 public Map<String, ResultSet> getResultMap() { | |
282 return resultMap; | |
283 } | |
284 | |
285 | |
286 public void setResultMap(Map<String, ResultSet> resultMap) { | |
287 this.resultMap = resultMap; | |
288 } | |
289 | |
290 private class AliasStructure{ | |
291 public Entity target = null; | |
292 public Entity alias = null; | |
293 public Relation rel = null; | |
294 public String description = ""; | |
295 public Attribute aliasAtt = null; | |
296 | |
297 public void setRelation(Relation rel){ | |
298 this.rel = rel; | |
299 this.target = rel.getTarget(); | |
300 this.alias = rel.getSource(); | |
301 this.description = rel.getOwnValue() + " <- ALIAS [alias=" + aliasAtt.getOwnValue() + "]"; | |
302 } | |
303 } | |
304 | |
305 static{ | |
306 filter1.setEntObjectClass("PERSON"); | |
307 filter1.setName("name"); | |
308 //filter1.setNormalize(true); | |
309 | |
310 filter2.setEntObjectClass("TEXT"); | |
311 filter2.setName("title"); | |
312 //filter2.setNormalize(true); | |
313 | |
314 filter3.setEntObjectClass("PERSON"); | |
315 filter3.setName("name_translit"); | |
316 | |
317 //filter3.setNormalize(true); | |
318 | |
319 filter4.setEntObjectClass("TEXT"); | |
320 filter4.setName("title_translit"); | |
321 | |
322 //filter4.setNormalize(true); | |
323 | |
324 filter5.setEntObjectClass("TEXT"); | |
325 filter5.setName("full_title"); | |
326 | |
327 //filter4.setNormalize(true); | |
328 | |
329 filter6.setEntObjectClass("TEXT"); | |
330 filter6.setName("full_title_translit"); | |
331 | |
332 | |
333 //filers for alias | |
334 filter7.setEntObjectClass("ALIAS"); | |
335 filter7.setName("alias"); | |
336 | |
337 authorsFilters.add(filter1); | |
338 authorsFilters.add(filter3); | |
339 authorsFilters.add(filter7); | |
340 | |
341 titlesFilters.add(filter2); | |
342 titlesFilters.add(filter4); | |
343 titlesFilters.add(filter5); | |
344 titlesFilters.add(filter6); | |
345 titlesFilters.add(filter7); | |
346 | |
347 /* | |
348 filter1.setOwnValue(search); | |
349 filter2.setOwnValue(search); | |
350 filter3.setOwnValue(search); | |
351 filter4.setOwnValue(search); | |
352 filter5.setOwnValue(search); | |
353 filter6.setOwnValue(search); | |
354 filter7.setOwnValue(search); | |
355 */ | |
356 | |
357 filters.add(filter1); | |
358 filters.add(filter2); | |
359 filters.add(filter3); | |
360 filters.add(filter4); | |
361 filters.add(filter5); | |
362 filters.add(filter6); | |
363 filters.add(filter7); | |
364 } | |
365 } |