Mercurial > hg > LGServices
comparison src/main/java/de/mpiwg/web/jsp/TopicPage.java @ 41:ba9515f22897
new: topic management and adding sections from searching result into topic
author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
---|---|
date | Thu, 17 Dec 2015 13:44:08 +0100 |
parents | |
children | 815cd86bb9ec |
comparison
equal
deleted
inserted
replaced
40:35ed4e650a53 | 41:ba9515f22897 |
---|---|
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 cl.maps.duplex.DuplexKey; | |
16 import de.mpiwg.gazetteer.bo.LGBranch; | |
17 import de.mpiwg.gazetteer.bo.LGTopic; | |
18 import de.mpiwg.gazetteer.bo.LGTopicSectionRelation; | |
19 import de.mpiwg.gazetteer.dataverse.DataverseUtils; | |
20 import de.mpiwg.gazetteer.dataverse.bo.VDCUser; | |
21 import de.mpiwg.gazetteer.db.DBSection; | |
22 import de.mpiwg.gazetteer.utils.DBService; | |
23 import de.mpiwg.gazetteer.utils.DataProvider; | |
24 import de.mpiwg.web.search.SortSectionByAdminType; | |
25 import de.mpiwg.web.search.SortSectionByBookId; | |
26 import de.mpiwg.web.search.SortSectionByBookName; | |
27 import de.mpiwg.web.search.SortSectionByDynasty; | |
28 import de.mpiwg.web.search.SortSectionById; | |
29 import de.mpiwg.web.search.SortSectionByLevel1; | |
30 import de.mpiwg.web.search.SortSectionByLevel2; | |
31 import de.mpiwg.web.search.SortSectionByPeriod; | |
32 import de.mpiwg.web.search.SortSectionByStartPage; | |
33 | |
34 | |
35 public class TopicPage extends AbstractJSPPage{ | |
36 | |
37 private static Logger logger = Logger.getLogger(TopicPage.class); | |
38 | |
39 public static String bean = "topicBean"; | |
40 public static String page = "pages/topicPage.jsp"; | |
41 | |
42 private LGTopic topic; | |
43 private Long topicId; | |
44 | |
45 private Long userId; | |
46 | |
47 private List<VDCUser> suggestionUserList; | |
48 private List<VDCUser> contributors; | |
49 | |
50 private List<DBSection> completeSectionList; | |
51 private List<DBSection> filteredSectionList; | |
52 private List<DBSection> displaySectionList; | |
53 | |
54 | |
55 private String dynastyFilter = new String(); | |
56 private String adminTypeFilter = new String(); | |
57 private String level1Filter = new String(); | |
58 private String level2Filter = new String(); | |
59 | |
60 private String bookNameFilter = new String(); | |
61 private String bookIdFilter = new String(); | |
62 private String periodFilter = new String(); | |
63 private String sectionNameFilter = new String(); | |
64 | |
65 private DataPaginator paginator = new DataPaginator(); | |
66 private String filteringMessage; | |
67 | |
68 private Map<Long, List<LGBranch>> branchesMap; | |
69 | |
70 public void loadParameters(HttpServletRequest request, HttpServletResponse response){ | |
71 | |
72 this.request = request; | |
73 this.response = response; | |
74 this.userId = getLongParameter("userId"); | |
75 this.topicId = getLongParameter("topicId"); | |
76 | |
77 this.dynastyFilter = getParameter("dynastyFilter"); | |
78 this.adminTypeFilter = getParameter("adminTypeFilter"); | |
79 this.level1Filter = getParameter("level1Filter"); | |
80 this.level2Filter = getParameter("level2Filter"); | |
81 this.periodFilter = getParameter("periodFilter"); | |
82 this.sectionNameFilter = getParameter("sectionNameFilter"); | |
83 this.bookIdFilter = getParameter("bookIdFilter"); | |
84 this.bookNameFilter = getParameter("bookNameFilter"); | |
85 | |
86 } | |
87 | |
88 | |
89 | |
90 public void forceLoadTopicSectionRelation() { | |
91 | |
92 logger.debug("forceLoadTopicSectionRelation"); | |
93 DataProvider.getInstance().setTopicSectionRelationMap(null); | |
94 | |
95 this.loadTopic(this.topic); | |
96 } | |
97 | |
98 | |
99 | |
100 public void loadTopic(String topicId0){ | |
101 try { | |
102 | |
103 this.topicId = Long.parseLong(topicId0); | |
104 | |
105 LGTopic topic = DataProvider.getInstance().getTopic(topicId); | |
106 if(topic != null){ | |
107 this.loadTopic(topic); | |
108 }else{ | |
109 addMsg("topic [id=" + topicId + "] no found."); | |
110 } | |
111 | |
112 | |
113 } catch (Exception e) { | |
114 internalError(e); | |
115 } | |
116 | |
117 } | |
118 | |
119 public void addContributor(){ | |
120 if(userId != null){ | |
121 | |
122 VDCUser user = DataverseUtils.getUser(userId); | |
123 System.out.println("Adding user: " + user.getUserName()); | |
124 | |
125 this.topic.addContributor(userId); | |
126 this.saveTopic0(); | |
127 } | |
128 } | |
129 | |
130 | |
131 | |
132 public void removeContributor(){ | |
133 if(userId != null){ | |
134 | |
135 VDCUser user = DataverseUtils.getUser(userId); | |
136 System.out.println("Removing user: " + user.getUserName()); | |
137 | |
138 this.topic.removeContributor(userId); | |
139 this.saveTopic0(); | |
140 } | |
141 } | |
142 | |
143 private void saveTopic0(){ | |
144 try { | |
145 DataProvider.getInstance().updateTopic(topic); | |
146 this.loadTopic(topic); | |
147 addMsg("The topic has been updated!"); | |
148 } catch (Exception e) { | |
149 internalError(e); | |
150 } | |
151 } | |
152 | |
153 | |
154 public void loadTopic(LGTopic topic){ | |
155 logger.info("Loading topic: " + topic.toString()); | |
156 | |
157 if(topic != null && topic.isPersistent()){ | |
158 this.loadBranches(); | |
159 try { | |
160 this.topic = (LGTopic)topic.clone(); | |
161 this.contributors = new ArrayList<VDCUser>(); | |
162 for(Long userId : this.topic.getContributorsList()){ | |
163 VDCUser user = DataverseUtils.getUser(userId); | |
164 if(user != null){ | |
165 this.contributors.add(user); | |
166 } | |
167 | |
168 } | |
169 | |
170 this.loadSuggestionUserList(); | |
171 | |
172 // all sections in the topic | |
173 this.completeSectionList = DataProvider.getInstance().getAllSectionsInTopic(topic.getId()); | |
174 | |
175 } catch (Exception e) { | |
176 internalError(e); | |
177 } | |
178 | |
179 logger.info("completeSectionList.size=" + completeSectionList.size()); | |
180 } | |
181 this.filter(); | |
182 } | |
183 | |
184 | |
185 | |
186 public void updateDescription() { | |
187 String description = getParameter("description"); | |
188 //logger.info("updateLabel: " + branchLabel + ", for branch id=" + this.getBranchId()) | |
189 this.getTopic().setDescription(description); | |
190 | |
191 this.saveTopic0(); | |
192 | |
193 } | |
194 | |
195 | |
196 | |
197 | |
198 public void deleteSection(Long sectionId) throws Exception { | |
199 // delete the record with sectionId, topicId in topicSectionRelation database table | |
200 | |
201 logger.debug("delete sectionId=" + sectionId + " in topicId=" + topicId); | |
202 | |
203 DataProvider.getInstance().deleteTopicSectionRelation(topicId, sectionId); | |
204 | |
205 | |
206 // update completeSectionList | |
207 this.completeSectionList = DataProvider.getInstance().getAllSectionsInTopic(topic.getId()); | |
208 | |
209 | |
210 } | |
211 | |
212 | |
213 | |
214 private void loadSuggestionUserList() throws Exception{ | |
215 this.suggestionUserList = new ArrayList<VDCUser>(); | |
216 for(VDCUser user : DataverseUtils.getAllUsers()){ | |
217 if(!topic.hasContributor(user.getId())){ | |
218 this.suggestionUserList.add(user); | |
219 } | |
220 } | |
221 } | |
222 | |
223 public void reset(){ | |
224 this.topic = null; | |
225 this.completeSectionList = null; | |
226 } | |
227 | |
228 public void filter(){ | |
229 this.filteredSectionList = new ArrayList<DBSection>(); | |
230 for(DBSection section : this.completeSectionList){ | |
231 if(!this.filteredSectionList.contains(section)){ | |
232 | |
233 if( (StringUtils.isEmpty(dynastyFilter) || StringUtils.startsWith(section.getBook().getDynasty(), dynastyFilter)) && | |
234 (StringUtils.isEmpty(level1Filter) || StringUtils.startsWith(section.getBook().getLevel1(), level1Filter)) && | |
235 (StringUtils.isEmpty(level2Filter) || StringUtils.startsWith(section.getBook().getLevel2(), level2Filter)) && | |
236 (StringUtils.isEmpty(bookIdFilter) || StringUtils.startsWith(section.getBookId(), bookIdFilter)) && | |
237 (StringUtils.isEmpty(bookNameFilter) || StringUtils.startsWith(section.getBook().getName(), bookNameFilter)) && | |
238 (StringUtils.isEmpty(periodFilter) || StringUtils.startsWith(section.getBook().getPeriod(), periodFilter)) && | |
239 (StringUtils.isEmpty(sectionNameFilter) || StringUtils.startsWith(section.getName(), sectionNameFilter)) && | |
240 (StringUtils.isEmpty(adminTypeFilter) || StringUtils.startsWith(section.getBook().getAdmin_type(), adminTypeFilter)) | |
241 ){ | |
242 this.filteredSectionList.add(section); | |
243 } | |
244 | |
245 } | |
246 } | |
247 | |
248 if(completeSectionList.size() > 0){ | |
249 | |
250 this.filteringMessage = this.filteredSectionList.size() + " section(s) listed after the filtering"; | |
251 | |
252 this.paginator.setCurrentPage(0); | |
253 this.paginator.resetNumberOfPages(filteredSectionList.size()); | |
254 | |
255 this.updateCurrentSections(); | |
256 | |
257 }else{ | |
258 this.filteredSectionList = null; | |
259 this.filteringMessage = ""; | |
260 | |
261 this.paginator.setCurrentPage(0); | |
262 this.paginator.resetNumberOfPages(0); | |
263 } | |
264 | |
265 | |
266 } | |
267 | |
268 | |
269 private void updateCurrentSections() { | |
270 /* developing... */ | |
271 this.displaySectionList = this.filteredSectionList; | |
272 | |
273 /* | |
274 | |
275 this.paginator.initCount(); | |
276 int startRecord = this.paginator.getCurrentPage() | |
277 * this.paginator.getItemsPerPage(); | |
278 if(this.paginator.getNumberOfPages() == 0){ | |
279 this.displaySectionList = new ArrayList<DBSection>(); | |
280 }else if((this.paginator.getCurrentPage() + 1) == this.paginator.getNumberOfPages()){ | |
281 int mod = this.filteredSectionList.size() % paginator.getItemsPerPage(); | |
282 if(mod == 0){ | |
283 this.displaySectionList = filteredSectionList.subList(startRecord, startRecord + this.paginator.getItemsPerPage()); | |
284 }else{ | |
285 this.displaySectionList = filteredSectionList.subList(startRecord, startRecord + mod); | |
286 } | |
287 | |
288 }else{ | |
289 this.displaySectionList = filteredSectionList.subList(startRecord, startRecord + this.paginator.getItemsPerPage()); | |
290 } | |
291 */ | |
292 | |
293 for(DBSection section : this.displaySectionList){ | |
294 section.setBranches(this.branchesMap.get(section.getId())); | |
295 } | |
296 | |
297 | |
298 } | |
299 | |
300 | |
301 | |
302 public void addSectionToTopic(Long _sectionId, Long _topicId) throws Exception { | |
303 logger.debug("selectedSectionId:" + _sectionId + ", selectedTopicId: " + _topicId); | |
304 | |
305 | |
306 // create a new record in TopicSectionRelation table | |
307 String status = DataProvider.getInstance().updateTopicSectionRelation(_sectionId, _topicId); | |
308 | |
309 if (StringUtils.equals(status, "added") ) { | |
310 // TODO responding with names to user | |
311 addMsg("Added section [id=" + _sectionId + "] to the topic [id=" + _topicId + "]"); | |
312 } else if (StringUtils.equals(status, "updated") ) { | |
313 addMsg("The section already in the topic"); | |
314 } | |
315 | |
316 this.reset(); | |
317 } | |
318 | |
319 | |
320 | |
321 | |
322 private void loadBranches(){ | |
323 this.branchesMap = new HashMap<Long, List<LGBranch>>(); | |
324 // List<LGBranch> list = DataProvider.getInstance().getBranches(getSessionBean().getUser().getId()); | |
325 List<LGBranch> list = DataProvider.getInstance().getAllExistingBranches(); | |
326 | |
327 for(LGBranch branch : list){ | |
328 branch.loadTransientData(); | |
329 if(this.branchesMap.get(branch.getSectionId()) == null){ | |
330 this.branchesMap.put(branch.getSectionId(), new ArrayList<LGBranch>()); | |
331 } | |
332 this.branchesMap.get(branch.getSectionId()).add(branch); | |
333 } | |
334 } | |
335 | |
336 public List<VDCUser> getContributors() { | |
337 return contributors; | |
338 } | |
339 | |
340 public void setContributors(List<VDCUser> contributors) { | |
341 this.contributors = contributors; | |
342 } | |
343 | |
344 public List<VDCUser> getSuggestionUserList() { | |
345 return suggestionUserList; | |
346 } | |
347 public void setSuggestionUserList(List<VDCUser> suggestionUserList) { | |
348 this.suggestionUserList = suggestionUserList; | |
349 } | |
350 | |
351 public Long getUserId() { | |
352 return userId; | |
353 } | |
354 | |
355 public void setUserId(Long userId) { | |
356 this.userId = userId; | |
357 } | |
358 | |
359 | |
360 public LGTopic getTopic() { | |
361 return topic; | |
362 } | |
363 | |
364 | |
365 | |
366 public void setTopic(LGTopic topic) { | |
367 this.topic = topic; | |
368 } | |
369 | |
370 | |
371 | |
372 public Long getTopicId() { | |
373 return topicId; | |
374 } | |
375 | |
376 | |
377 | |
378 public void setTopicId(Long topicId) { | |
379 this.topicId = topicId; | |
380 } | |
381 | |
382 | |
383 | |
384 public String getDynastyFilter() { | |
385 return dynastyFilter; | |
386 } | |
387 | |
388 | |
389 | |
390 public void setDynastyFilter(String dynastyFilter) { | |
391 this.dynastyFilter = dynastyFilter; | |
392 } | |
393 | |
394 | |
395 | |
396 public String getAdminTypeFilter() { | |
397 return adminTypeFilter; | |
398 } | |
399 | |
400 | |
401 | |
402 public void setAdminTypeFilter(String adminTypeFilter) { | |
403 this.adminTypeFilter = adminTypeFilter; | |
404 } | |
405 | |
406 | |
407 | |
408 public String getLevel1Filter() { | |
409 return level1Filter; | |
410 } | |
411 | |
412 | |
413 | |
414 public void setLevel1Filter(String level1Filter) { | |
415 this.level1Filter = level1Filter; | |
416 } | |
417 | |
418 | |
419 | |
420 public String getLevel2Filter() { | |
421 return level2Filter; | |
422 } | |
423 | |
424 | |
425 | |
426 public void setLevel2Filter(String level2Filter) { | |
427 this.level2Filter = level2Filter; | |
428 } | |
429 | |
430 | |
431 | |
432 public List<DBSection> getFilteredSectionList() { | |
433 return filteredSectionList; | |
434 } | |
435 | |
436 | |
437 | |
438 public void setFilteredSectionList(List<DBSection> filteredSectionList) { | |
439 this.filteredSectionList = filteredSectionList; | |
440 } | |
441 | |
442 | |
443 | |
444 public List<DBSection> getDisplaySectionList() { | |
445 return displaySectionList; | |
446 } | |
447 | |
448 | |
449 | |
450 public void setDisplaySectionList(List<DBSection> displaySectionList) { | |
451 this.displaySectionList = displaySectionList; | |
452 } | |
453 | |
454 | |
455 | |
456 public DataPaginator getPaginator() { | |
457 return paginator; | |
458 } | |
459 | |
460 | |
461 | |
462 public void setPaginator(DataPaginator paginator) { | |
463 this.paginator = paginator; | |
464 } | |
465 | |
466 | |
467 | |
468 public String getFilteringMessage() { | |
469 return filteringMessage; | |
470 } | |
471 | |
472 | |
473 | |
474 public void setFilteringMessage(String filteringMessage) { | |
475 this.filteringMessage = filteringMessage; | |
476 } | |
477 | |
478 | |
479 | |
480 public Map<Long, List<LGBranch>> getBranchesMap() { | |
481 return branchesMap; | |
482 } | |
483 | |
484 | |
485 | |
486 public void setBranchesMap(Map<Long, List<LGBranch>> branchesMap) { | |
487 this.branchesMap = branchesMap; | |
488 } | |
489 | |
490 public String getBookNameFilter() { | |
491 return bookNameFilter; | |
492 } | |
493 | |
494 | |
495 public void setBookNameFilter(String bookNameFilter) { | |
496 this.bookNameFilter = bookNameFilter; | |
497 } | |
498 | |
499 | |
500 | |
501 public String getBookIdFilter() { | |
502 return bookIdFilter; | |
503 } | |
504 | |
505 | |
506 | |
507 public void setBookIdFilter(String bookIdFilter) { | |
508 this.bookIdFilter = bookIdFilter; | |
509 } | |
510 | |
511 | |
512 | |
513 public String getPeriodFilter() { | |
514 return periodFilter; | |
515 } | |
516 | |
517 | |
518 | |
519 public void setPeriodFilter(String periodFilter) { | |
520 this.periodFilter = periodFilter; | |
521 } | |
522 | |
523 | |
524 | |
525 public String getSectionNameFilter() { | |
526 return sectionNameFilter; | |
527 } | |
528 | |
529 | |
530 | |
531 public void setSectionNameFilter(String sectionNameFilter) { | |
532 this.sectionNameFilter = sectionNameFilter; | |
533 } | |
534 | |
535 | |
536 | |
537 public List<DBSection> getCompleteSectionList() { | |
538 return completeSectionList; | |
539 } | |
540 | |
541 | |
542 | |
543 public void setCompleteSectionList(List<DBSection> completeSectionList) { | |
544 this.completeSectionList = completeSectionList; | |
545 } | |
546 | |
547 | |
548 | |
549 /////// Sorting | |
550 | |
551 | |
552 public void sortByBookNameUp(){ | |
553 Collections.sort(this.completeSectionList, new SortSectionByBookName()); | |
554 filter(); | |
555 } | |
556 | |
557 public void sortByBookNameDown(){ | |
558 Collections.sort(this.completeSectionList, new SortSectionByBookName()); | |
559 Collections.reverse(this.completeSectionList); | |
560 filter(); | |
561 } | |
562 | |
563 public void sortBySectionNameUp(){ | |
564 Collections.sort(this.completeSectionList); | |
565 filter(); | |
566 } | |
567 | |
568 public void sortBySectionNameDown(){ | |
569 Collections.sort(this.completeSectionList); | |
570 Collections.reverse(this.completeSectionList); | |
571 filter(); | |
572 } | |
573 | |
574 | |
575 public void sortByPeriodUp(){ | |
576 Collections.sort(this.completeSectionList, new SortSectionByPeriod()); | |
577 filter(); | |
578 } | |
579 | |
580 public void sortByPeriodDown(){ | |
581 Collections.sort(this.completeSectionList, new SortSectionByPeriod()); | |
582 Collections.reverse(this.completeSectionList); | |
583 filter(); | |
584 } | |
585 | |
586 | |
587 | |
588 public void sortBySectionIdUp(){ | |
589 Collections.sort(this.completeSectionList, new SortSectionById()); | |
590 this.filter(); | |
591 } | |
592 | |
593 public void sortBySectionIdDown(){ | |
594 Collections.sort(this.completeSectionList, new SortSectionById()); | |
595 Collections.reverse(completeSectionList); | |
596 this.filter(); | |
597 } | |
598 | |
599 | |
600 | |
601 public void sortByDynastyUp(){ | |
602 Collections.sort(this.completeSectionList, new SortSectionByDynasty()); | |
603 filter(); | |
604 } | |
605 | |
606 public void sortByDynastyDown(){ | |
607 Collections.sort(this.completeSectionList, new SortSectionByDynasty()); | |
608 Collections.reverse(completeSectionList); | |
609 filter(); | |
610 } | |
611 | |
612 public void sortByBookIdUp(){ | |
613 Collections.sort(this.completeSectionList, new SortSectionByBookId()); | |
614 filter(); | |
615 } | |
616 | |
617 public void sortByBookIdDown(){ | |
618 Collections.sort(this.completeSectionList, new SortSectionByBookId()); | |
619 Collections.reverse(completeSectionList); | |
620 filter(); | |
621 } | |
622 | |
623 public void sortByLevel1Up(){ | |
624 Collections.sort(this.completeSectionList, new SortSectionByLevel1()); | |
625 filter(); | |
626 } | |
627 | |
628 public void sortByLevel1Down(){ | |
629 Collections.sort(this.completeSectionList, new SortSectionByLevel1()); | |
630 Collections.reverse(completeSectionList); | |
631 filter(); | |
632 } | |
633 | |
634 public void sortByLevel2Up(){ | |
635 Collections.sort(this.completeSectionList, new SortSectionByLevel2()); | |
636 filter(); | |
637 } | |
638 | |
639 public void sortByLevel2Down(){ | |
640 Collections.sort(this.completeSectionList, new SortSectionByLevel2()); | |
641 Collections.reverse(completeSectionList); | |
642 filter(); | |
643 } | |
644 | |
645 public void sortByAdminTypeUp(){ | |
646 Collections.sort(this.completeSectionList, new SortSectionByAdminType()); | |
647 filter(); | |
648 } | |
649 | |
650 public void sortByAdminTypeDown(){ | |
651 Collections.sort(this.completeSectionList, new SortSectionByAdminType()); | |
652 Collections.reverse(completeSectionList); | |
653 filter(); | |
654 } | |
655 | |
656 public void sortByStartPageUp(){ | |
657 Collections.sort(this.completeSectionList, new SortSectionByStartPage()); | |
658 filter(); | |
659 } | |
660 | |
661 public void sortByStartPageDown(){ | |
662 Collections.sort(this.completeSectionList, new SortSectionByStartPage()); | |
663 Collections.reverse(completeSectionList); | |
664 filter(); | |
665 } | |
666 | |
667 | |
668 | |
669 | |
670 | |
671 | |
672 } |