comparison src/main/java/de/mpiwg/itgroup/ismi/search/beans/Witness4TitleQuery.java @ 1:2e911857a759

(none)
author jurzua
date Wed, 29 Oct 2014 14:00:28 +0000
parents
children 8ae989269f51
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.List;
7
8 import org.apache.commons.lang.StringUtils;
9 import org.mpi.openmind.repository.bo.Entity;
10 import org.mpi.openmind.repository.utils.NormalizerUtils;
11 import org.mpi.openmind.search.utils.ResultEntry;
12 import org.mpi.openmind.search.utils.SAttributeMultipleName;
13 import org.mpi.openmind.search.utils.SAttributeUniqueName;
14 import org.mpi.openmind.search.utils.SEntity;
15 import org.mpi.openmind.search.utils.SRelLongKey;
16 import org.mpi.openmind.search.utils.SRelation;
17 import org.mpi.openmind.search.utils.SRelationMultipleName;
18 import org.mpi.openmind.search.utils.SRelationUniqueName;
19
20 import de.mpiwg.itgroup.ismi.entry.beans.ApplicationBean;
21
22 /**
23 * 2) List of all witnesses for a title
24 * (the list should distinguish which witnesses go with a particular title version)
25 * @author jurzua
26 *
27 */
28 public class Witness4TitleQuery extends AbstractQuery implements Serializable{
29
30 private static final long serialVersionUID = -1460003809678104919L;
31
32 private String titleName;
33 private List<SRelLongKey> titleWitnessKeyList;
34
35 public Witness4TitleQuery(ApplicationBean appBean){
36 super(appBean);
37 this.exportUrlRoot = ApplicationBean.urlISMIExportServiceTitles;
38 }
39
40 @Override
41 public void reset(){
42 super.reset();
43 this.titleName = null;
44 this.rs = null;
45 this.titleWitnessKeyList = null;
46 }
47
48 @Override
49 protected void search(){
50
51 List<ResultEntry> rs0 = this.execute01(titleName);
52 List<ResultEntry> rs1 = null;
53 if(StringUtils.isNotEmpty(titleName)){
54 rs1 = this.execute02(titleName);
55 }
56 rs = loadResultSet(rs0, rs1);
57
58 List<Long> idLongList = new ArrayList<Long>();
59 for(Object e : rs){
60 ResultEntry02 entry = (ResultEntry02)e;
61 if(!idLongList.contains(entry.titleId))
62 idLongList.add(entry.titleId);
63 }
64 this.idList = ApplicationBean.generateIdList(idLongList);
65
66 this.exportDirkUrl = ApplicationBean.generateExportURL(exportUrlRoot, idList, "xml");
67 }
68
69 private List<ResultEntry02> loadResultSet(List<ResultEntry> rs0, List<ResultEntry> rs1){
70 List<ResultEntry02> currentRs = new ArrayList<ResultEntry02>();
71 titleWitnessKeyList = new ArrayList<SRelLongKey>();
72
73 int count = 0;
74
75 for(ResultEntry re : rs0){
76 Entity title = getOm().getEntityById(re.getEntMap().get(0));
77 Entity witness = getOm().getEntityById(re.getEntMap().get(1));
78
79 List<Entity> subjectList = getOm().getTargetsForSourceRelation(title.getId(), "has_subject", "SUBJECT", 1);
80 String subject = (subjectList.size() == 0) ? null : subjectList.get(0).getOwnValue();
81
82 currentRs.add(new ResultEntry02(title, witness, subject));
83 putTitleWitnessKeyList(title.getId(), witness.getId());
84 count++;
85 if(count >= MAX_RS){
86 break;
87 }
88 }
89
90 if(rs1 != null && count < MAX_RS){
91 for(ResultEntry re : rs1){
92 Entity title = getOm().getEntityById(re.getEntMap().get(0));
93 Entity witness = getOm().getEntityById(re.getEntMap().get(1));
94 if(!containsTitleWitnessKeyList(title.getId(), witness.getId())){
95
96 Entity alias = getOm().getEntityById(re.getEntMap().get(1));
97 String alias2Person = re.getRel(1, 0);
98 List<Entity> subjectList = getOm().getTargetsForSourceRelation(title.getId(), "has_subject", "SUBJECT", 1);
99 String subject = (subjectList.size() == 0) ? null : subjectList.get(0).getOwnValue();
100
101 currentRs.add(new ResultEntry02(title, witness, subject, alias.getOwnValue(), alias2Person));
102 putTitleWitnessKeyList(title.getId(), witness.getId());
103 count++;
104 if(count >= MAX_RS){
105 break;
106 }
107 }
108 }
109 }
110
111 Collections.sort(currentRs);
112
113 return currentRs;
114 }
115
116 private void putTitleWitnessKeyList(Long titleId, Long witness){
117 titleWitnessKeyList.add(new SRelLongKey(titleId, witness));
118 }
119
120 private boolean containsTitleWitnessKeyList(Long titleId, Long witnessId){
121 return titleWitnessKeyList.contains(new SRelLongKey(titleId, witnessId));
122 }
123
124 private List<ResultEntry> execute01(String titleName){
125
126 List<SEntity> entFilters = new ArrayList<SEntity>();
127 List<SRelation> relFilters = new ArrayList<SRelation>();
128
129
130 SEntity text = new SEntity(0, "TEXT");
131 if(StringUtils.isNotEmpty(titleName)){
132 text.addAtt(new SAttributeMultipleName(titleName, "full_title_translit", "full_title"));
133 }
134 entFilters.add(text);
135
136 SEntity witness = new SEntity(1, "WITNESS");
137 entFilters.add(witness);
138
139 SRelationUniqueName is_exemplar_of = new SRelationUniqueName(witness, text, "is_exemplar_of");
140 relFilters.add(is_exemplar_of);
141
142 return this.appBean.getSS().search(entFilters, relFilters);
143 }
144
145 private List<ResultEntry> execute02(String titleName){
146
147 List<SEntity> entFilters = new ArrayList<SEntity>();
148 List<SRelation> relFilters = new ArrayList<SRelation>();
149
150
151 SEntity text = new SEntity(0, "TEXT");
152 entFilters.add(text);
153
154 SEntity alias = new SEntity(1, "ALIAS");
155 alias.addAtt(new SAttributeUniqueName("alias", titleName));
156 entFilters.add(alias);
157
158 SEntity witness = new SEntity(2, "WITNESS");
159 entFilters.add(witness);
160
161 SRelationMultipleName is_alias = new SRelationMultipleName(alias, text,
162 "is_prime_alias_title_of", "is_alias_title_of",
163 "is_alias_explicit_of", "is_alias_incipit_of");
164 SRelationUniqueName is_exemplar_of = new SRelationUniqueName(witness, text, "is_exemplar_of");
165 relFilters.add(is_exemplar_of);
166 relFilters.add(is_alias);
167
168 return this.appBean.getSS().search(entFilters, relFilters);
169 }
170
171 public class ResultEntry02 implements Comparable<ResultEntry02>, Serializable{
172 private static final long serialVersionUID = -2672042198162179468L;
173
174 public Long titleId;
175 public String titleOv;
176 public String titleNOv;
177 public String alias;
178 public String alias2Title;
179 public Long witnessId;
180 public String witnessOv;
181 public String witnessNOv;
182 public String subject;
183
184 public ResultEntry02(Entity title, Entity witness, String subject){
185 this.set(title, witness, subject);
186
187 }
188
189 public ResultEntry02(Entity title, Entity witness, String subject, String alias, String alias2Title){
190 this.set(title, witness, subject);
191 this.alias = alias;
192 this.alias2Title = alias2Title;
193 }
194
195 private void set(Entity title, Entity witness, String subject){
196 this.subject = subject;
197
198 this.titleId = title.getId();
199 this.titleOv = title.getOwnValue();
200 this.titleNOv = title.getNormalizedOwnValue();
201
202 this.witnessId = witness.getId();
203 this.witnessOv = witness.getOwnValue();
204 this.witnessNOv = witness.getNormalizedOwnValue();
205 }
206
207 @Override
208 public int compareTo(ResultEntry02 o) {
209 if(!this.titleId.equals(o.titleId)){
210
211 //comparing subject
212 if(StringUtils.isNotEmpty(subject) && StringUtils.isNotEmpty(o.subject)){
213 int comparisonSubject = this.subject.compareTo(o.subject);
214 if(comparisonSubject != 0){
215 return comparisonSubject;
216 }
217 }else{
218 if(StringUtils.isNotEmpty(subject)){
219 return -1;
220 }else if(StringUtils.isNotEmpty(o.subject)){
221 return 1;
222 }
223 }
224
225 //comparing title
226 int comparisonTitle = NormalizerUtils.normalizedToCompare(titleNOv).compareTo(
227 NormalizerUtils.normalizedToCompare(o.titleNOv));
228 if(comparisonTitle != 0){
229 return comparisonTitle;
230 }
231 }else{
232 if(!this.witnessId.equals(o.witnessId)){
233 //comparing witness
234 int comparisonWitness = NormalizerUtils.normalizedToCompare(titleNOv).compareTo(
235 NormalizerUtils.normalizedToCompare(o.titleNOv));
236 return comparisonWitness;
237 }
238 }
239 return 0;
240 }
241
242 public Long getTitleId() {
243 return titleId;
244 }
245
246 public String getTitleOv() {
247 return titleOv;
248 }
249
250 public String getTitleNOv() {
251 return titleNOv;
252 }
253
254 public String getAlias() {
255 return alias;
256 }
257
258 public String getAlias2Title() {
259 return alias2Title;
260 }
261
262 public Long getWitnessId() {
263 return witnessId;
264 }
265
266 public String getWitnessOv() {
267 return witnessOv;
268 }
269
270 public String getWitnessNOv() {
271 return witnessNOv;
272 }
273
274 public String getSubject() {
275 return subject;
276 }
277 }
278
279 public String getTitleName() {
280 return titleName;
281 }
282
283 public void setTitleName(String titleName) {
284 this.titleName = titleName;
285 }
286 }