comparison src/main/java/de/mpiwg/web/jsp/SearchPage.java @ 0:3e62083dbcbf

First commit. This project comes from LGServer. We removed the framework icefaces. Now, LGServices uses just JSP and jquery.
author "jurzua <jurzua@mpiwg-berlin.mpg.de>"
date Thu, 23 Apr 2015 15:46:01 +0200
parents
children 1af9d7db348e
comparison
equal deleted inserted replaced
-1:000000000000 0:3e62083dbcbf
1 package de.mpiwg.web.jsp;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 import org.apache.commons.lang.StringUtils;
13 import org.apache.log4j.Logger;
14
15 import de.mpiwg.gazetteer.bo.LGBranch;
16 import de.mpiwg.gazetteer.db.DBSection;
17 import de.mpiwg.gazetteer.utils.DBService;
18 import de.mpiwg.gazetteer.utils.DataProvider;
19 import de.mpiwg.web.search.SortSectionByAdminType;
20 import de.mpiwg.web.search.SortSectionByAuthor;
21 import de.mpiwg.web.search.SortSectionByBookId;
22 import de.mpiwg.web.search.SortSectionByBookName;
23 import de.mpiwg.web.search.SortSectionByDynasty;
24 import de.mpiwg.web.search.SortSectionByEdition;
25 import de.mpiwg.web.search.SortSectionById;
26 import de.mpiwg.web.search.SortSectionByLevel1;
27 import de.mpiwg.web.search.SortSectionByPeriod;
28 import de.mpiwg.web.search.SortSectionByStartPage;
29 import de.mpiwg.web.search.SortSectionByVolume;
30
31 public class SearchPage extends AbstractJSPPage{
32
33 private static Logger logger = Logger.getLogger(SearchPage.class);
34
35 public static String bean = "searchBean";
36 public static String page = "pages/search.jsp";
37
38 private static Integer SEARCH_IN_SECTION_NAME = 0;
39 private static Integer SEARCH_IN_BOOK_NAME = 1;
40
41
42
43 private Map<Long, List<LGBranch>> branchesMap;
44 private List<DBSection> completeSectionList;
45 private List<DBSection> filteredSectionList;
46 private List<DBSection> displaySectionList;
47
48 private String searchTerm = new String();
49 private Integer searchIn = SEARCH_IN_SECTION_NAME;
50
51 private String dynastyFilter = new String();
52 private String adminTypeFilter = new String();
53 private String level1Filter = new String();
54
55 private DataPaginator paginator = new DataPaginator();
56 private String searchMessage;
57 private String filteringMessage;
58
59 @Override
60 public void init(){
61 super.init();
62 }
63
64 public void loadParameters(HttpServletRequest request, HttpServletResponse response){
65 this.request = request;
66 this.response = response;
67
68 this.searchTerm = getParameter("searchTerm");
69 this.dynastyFilter = getParameter("dynastyFilter");
70 this.adminTypeFilter = getParameter("adminTypeFilter");
71 this.level1Filter = getParameter("level1Filter");
72 this.searchIn = getIntParameter("searchIn");
73 }
74
75 public void search(){
76 logger.debug("Searching: " + this.searchTerm);
77
78 this.dynastyFilter = new String();
79 this.level1Filter = new String();
80 this.adminTypeFilter = new String();
81
82 if(StringUtils.isNotEmpty(this.searchTerm)){
83 this.loadBranches();
84 try {
85 List<String> terms = splitTerms();
86
87 if(SEARCH_IN_SECTION_NAME.equals(this.searchIn)){
88 System.out.println("Search in Section Name");
89 this.completeSectionList = DBService.searchSection(terms);
90 }else if(SEARCH_IN_BOOK_NAME.equals(this.searchIn)){
91 System.out.println("Search in Book Name");
92 this.completeSectionList = DBService.searchBook(terms, "name");
93 }
94
95 Collections.sort(this.completeSectionList);
96
97 filter();
98
99
100 } catch (Exception e) {
101 internalError(e);
102 }
103 }
104 }
105
106 public void filter(){
107 this.filteredSectionList = new ArrayList<DBSection>();
108 for(DBSection section : this.completeSectionList){
109 if(!this.filteredSectionList.contains(section)){
110
111 if( (StringUtils.isEmpty(dynastyFilter) || StringUtils.startsWith(section.getBook().getDynasty(), dynastyFilter)) &&
112 (StringUtils.isEmpty(level1Filter) || StringUtils.startsWith(section.getBook().getLevel1(), level1Filter)) &&
113 (StringUtils.isEmpty(adminTypeFilter) || StringUtils.startsWith(section.getBook().getAdmin_type(), adminTypeFilter))
114 ){
115 this.filteredSectionList.add(section);
116 }
117 }
118 }
119
120 if(completeSectionList.size() > 0){
121 this.searchMessage = completeSectionList.size() + " section(s) found for the term(s): " + this.searchTerm;
122 this.filteringMessage = this.filteredSectionList.size() + " section(s) listed after the filtering";
123 this.paginator.setCurrentPage(0);
124 this.paginator.resetNumberOfPages(filteredSectionList.size());
125 this.updateCurrentSections();
126 }else{
127 this.searchMessage = "No sections found for the term(s): " + this.searchTerm;
128 this.filteredSectionList = null;
129 }
130 }
131
132 private void updateCurrentSections() {
133 this.paginator.initCount();
134 int startRecord = this.paginator.getCurrentPage()
135 * this.paginator.getItemsPerPage();
136 if(this.paginator.getNumberOfPages() == 0){
137 this.displaySectionList = new ArrayList<DBSection>();
138 }else if((this.paginator.getCurrentPage() + 1) == this.paginator.getNumberOfPages()){
139 int mod = this.filteredSectionList.size() % paginator.getItemsPerPage();
140 if(mod == 0){
141 this.displaySectionList = filteredSectionList.subList(startRecord, startRecord + this.paginator.getItemsPerPage());
142 }else{
143 this.displaySectionList = filteredSectionList.subList(startRecord, startRecord + mod);
144 }
145
146 }else{
147 this.displaySectionList = filteredSectionList.subList(startRecord, startRecord + this.paginator.getItemsPerPage());
148 }
149
150 for(DBSection section : this.displaySectionList){
151 section.setBranches(this.branchesMap.get(section.getId()));
152 }
153 }
154
155 private void loadBranches(){
156 this.branchesMap = new HashMap<Long, List<LGBranch>>();
157 List<LGBranch> list = DataProvider.getInstance().getBranches(getSessionBean().getUser().getId());
158 for(LGBranch branch : list){
159 branch.loadTransientData();
160 if(this.branchesMap.get(branch.getSectionId()) == null){
161 this.branchesMap.put(branch.getSectionId(), new ArrayList<LGBranch>());
162 }
163 this.branchesMap.get(branch.getSectionId()).add(branch);
164 }
165 }
166
167 private List<String> splitTerms(){
168 List<String> rs = new ArrayList<String>();
169 String[] array = this.searchTerm.split(",");
170
171 for(String tmp : array){
172 tmp = tmp.replace(" ", "");
173 if(StringUtils.isNotEmpty(tmp)){
174 rs.add(tmp);
175 }
176 }
177 return rs;
178 }
179
180 public List<String> suggestDynasty(String term, int limit){
181 List<String> list = new ArrayList<String>();
182 for(DBSection section : this.completeSectionList){
183 String dynasty = section.getBook().getDynasty();
184 if(!list.contains(dynasty) && dynasty.startsWith(term)){
185 list.add(dynasty);
186 }
187 if(limit == list.size()){
188 break;
189 }
190 }
191 return list;
192 }
193
194 public List<String> suggestLevel1(String term, int limit){
195 List<String> list = new ArrayList<String>();
196 for(DBSection section : this.completeSectionList){
197 String level1 = section.getBook().getLevel1();
198 if(!list.contains(level1) && level1.startsWith(term)){
199 list.add(level1);
200 }
201 if(limit == list.size()){
202 break;
203 }
204 }
205 return list;
206 }
207
208 public List<String> suggestAdminType(String term, int limit){
209 List<String> list = new ArrayList<String>();
210 for(DBSection section : this.completeSectionList){
211 String adminType = section.getBook().getAdmin_type();
212 if(!list.contains(adminType) && adminType.startsWith(term)){
213 list.add(adminType);
214 }
215 if(limit == list.size()){
216 break;
217 }
218 }
219 return list;
220 }
221
222
223 public static Integer getSEARCH_IN_SECTION_NAME() {
224 return SEARCH_IN_SECTION_NAME;
225 }
226
227
228 public static void setSEARCH_IN_SECTION_NAME(Integer sEARCH_IN_SECTION_NAME) {
229 SEARCH_IN_SECTION_NAME = sEARCH_IN_SECTION_NAME;
230 }
231
232
233 public Map<Long, List<LGBranch>> getBranchesMap() {
234 return branchesMap;
235 }
236
237
238 public void setBranchesMap(Map<Long, List<LGBranch>> branchesMap) {
239 this.branchesMap = branchesMap;
240 }
241
242
243 public List<DBSection> getCompleteSectionList() {
244 return completeSectionList;
245 }
246
247
248 public void setCompleteSectionList(List<DBSection> completeSectionList) {
249 this.completeSectionList = completeSectionList;
250 }
251
252
253 public String getSearchTerm() {
254 return searchTerm;
255 }
256
257
258 public void setSearchTerm(String searchTerm) {
259 this.searchTerm = searchTerm;
260 }
261
262
263 public Integer getSearchIn() {
264 return searchIn;
265 }
266
267
268 public void setSearchIn(Integer searchIn) {
269 this.searchIn = searchIn;
270 }
271
272 public List<DBSection> getFilteredSectionList() {
273 return filteredSectionList;
274 }
275
276 public void setFilteredSectionList(List<DBSection> filteredSectionList) {
277 this.filteredSectionList = filteredSectionList;
278 }
279
280 public List<DBSection> getDisplaySectionList() {
281 return displaySectionList;
282 }
283
284 public void setDisplaySectionList(List<DBSection> displaySectionList) {
285 this.displaySectionList = displaySectionList;
286 }
287
288 public DataPaginator getPaginator() {
289 return paginator;
290 }
291
292 public void setPaginator(DataPaginator paginator) {
293 this.paginator = paginator;
294 }
295
296 public void firstPage() {
297 this.paginator.first();
298 this.updateCurrentSections();
299 }
300
301 public void lastPage() {
302 this.paginator.last();
303 this.updateCurrentSections();
304 }
305
306 public void fastForward() {
307 this.paginator.fastForward();
308 this.updateCurrentSections();
309 }
310
311 public void fastRewind() {
312 this.paginator.fastRewind();
313 this.updateCurrentSections();
314 }
315
316 public void previousPage() {
317 this.paginator.previous();
318 this.updateCurrentSections();
319 }
320
321 public void nextPage() {
322 this.paginator.next();
323 this.updateCurrentSections();
324 }
325
326 public String getSearchMessage() {
327 return searchMessage;
328 }
329
330 public void setSearchMessage(String searchMessage) {
331 this.searchMessage = searchMessage;
332 }
333
334 public String getFilteringMessage() {
335 return filteringMessage;
336 }
337
338 public void setFilteringMessage(String filteringMessage) {
339 this.filteringMessage = filteringMessage;
340 }
341
342 /////// Sorting
343
344 public String getDynastyFilter() {
345 return dynastyFilter;
346 }
347
348 public void setDynastyFilter(String dynastyFilter) {
349 this.dynastyFilter = dynastyFilter;
350 }
351
352 public void sortByBookNameUp(){
353 Collections.sort(this.completeSectionList, new SortSectionByBookName());
354 filter();
355 }
356
357 public void sortByBookNameDown(){
358 Collections.sort(this.completeSectionList, new SortSectionByBookName());
359 Collections.reverse(this.completeSectionList);
360 filter();
361 }
362
363 public void sortBySectionNameUp(){
364 Collections.sort(this.completeSectionList);
365 filter();
366 }
367
368 public void sortBySectionNameDown(){
369 Collections.sort(this.completeSectionList);
370 Collections.reverse(this.completeSectionList);
371 filter();
372 }
373
374 public void sortByAuthorUp(){
375 Collections.sort(this.completeSectionList, new SortSectionByAuthor());
376 filter();
377 }
378
379 public void sortByAuthorDown(){
380 Collections.sort(this.completeSectionList, new SortSectionByAuthor());
381 Collections.reverse(this.completeSectionList);
382 filter();
383 }
384
385 public void sortByPeriodUp(){
386 Collections.sort(this.completeSectionList, new SortSectionByPeriod());
387 filter();
388 }
389
390 public void sortByPeriodDown(){
391 Collections.sort(this.completeSectionList, new SortSectionByPeriod());
392 Collections.reverse(this.completeSectionList);
393 filter();
394 }
395
396 public void sortByVolumeUp(){
397 Collections.sort(this.completeSectionList, new SortSectionByVolume());
398 filter();
399 }
400
401 public void sortByVolumeDown(){
402 Collections.sort(this.completeSectionList, new SortSectionByVolume());
403 Collections.reverse(this.completeSectionList);
404 filter();
405 }
406
407
408 public void sortBySectionIdUp(){
409 Collections.sort(this.completeSectionList, new SortSectionById());
410 this.filter();
411 }
412
413 public void sortBySectionIdDown(){
414 Collections.sort(this.completeSectionList, new SortSectionById());
415 Collections.reverse(completeSectionList);
416 this.filter();
417 }
418
419 public void sortByEditionUp(){
420 Collections.sort(this.completeSectionList, new SortSectionByEdition());
421 filter();
422 }
423
424 public void sortByEditionDown(){
425 Collections.sort(this.completeSectionList, new SortSectionByEdition());
426 Collections.reverse(completeSectionList);
427 filter();
428 }
429
430 public void sortByDynastyUp(){
431 Collections.sort(this.completeSectionList, new SortSectionByDynasty());
432 filter();
433 }
434
435 public void sortByDynastyDown(){
436 Collections.sort(this.completeSectionList, new SortSectionByDynasty());
437 Collections.reverse(completeSectionList);
438 filter();
439 }
440
441 public void sortByBookIdUp(){
442 Collections.sort(this.completeSectionList, new SortSectionByBookId());
443 filter();
444 }
445
446 public void sortByBookIdDown(){
447 Collections.sort(this.completeSectionList, new SortSectionByBookId());
448 Collections.reverse(completeSectionList);
449 filter();
450 }
451
452 public void sortByLevel1Up(){
453 Collections.sort(this.completeSectionList, new SortSectionByLevel1());
454 filter();
455 }
456
457 public void sortByLevel1Down(){
458 Collections.sort(this.completeSectionList, new SortSectionByLevel1());
459 Collections.reverse(completeSectionList);
460 filter();
461 }
462
463 public void sortByAdminTypeUp(){
464 Collections.sort(this.completeSectionList, new SortSectionByAdminType());
465 filter();
466 }
467
468 public void sortByAdminTypeDown(){
469 Collections.sort(this.completeSectionList, new SortSectionByAdminType());
470 Collections.reverse(completeSectionList);
471 filter();
472 }
473
474 public void sortByStartPageUp(){
475 Collections.sort(this.completeSectionList, new SortSectionByStartPage());
476 filter();
477 }
478
479 public void sortByStartPageDown(){
480 Collections.sort(this.completeSectionList, new SortSectionByStartPage());
481 Collections.reverse(completeSectionList);
482 filter();
483 }
484
485 public String getAdminTypeFilter() {
486 return adminTypeFilter;
487 }
488
489 public void setAdminTypeFilter(String adminTypeFilter) {
490 this.adminTypeFilter = adminTypeFilter;
491 }
492
493 public String getLevel1Filter() {
494 return level1Filter;
495 }
496
497 public void setLevel1Filter(String level1Filter) {
498 this.level1Filter = level1Filter;
499 }
500
501
502 }