1 package org.kuali.student.ap.plannerreview.controller;
2
3 import org.kuali.rice.krad.web.form.UifFormBase;
4 import org.kuali.student.ap.plannerreview.infc.Conversation;
5 import org.kuali.student.ap.plannerreview.infc.ConversationComment;
6 import org.kuali.student.ap.plannerreview.util.ConversationConstants;
7 import org.kuali.student.ap.plannerreview.dto.ConversationAdvisorInfo;
8 import org.kuali.student.ap.plannerreview.dto.ConversationCommentInfo;
9 import org.kuali.student.ap.plannerreview.dto.ConversationDateCommentInfo;
10 import org.kuali.student.ap.plannerreview.form.ConversationViewForm;
11 import org.kuali.student.r2.common.dto.RichTextInfo;
12 import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
13 import org.kuali.student.r2.common.infc.RichText;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16 import org.springframework.stereotype.Controller;
17 import org.springframework.validation.BindingResult;
18 import org.springframework.web.bind.annotation.ModelAttribute;
19 import org.springframework.web.bind.annotation.RequestMapping;
20 import org.springframework.web.bind.annotation.RequestMethod;
21 import org.springframework.web.servlet.ModelAndView;
22
23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33
34 @Controller
35 @RequestMapping(value = "/reviewView")
36 public class ConversationViewController extends ConversationControllerBase {
37
38 private static final Logger LOG = LoggerFactory.getLogger(ConversationViewController.class);
39
40 public static final String CONVO_FORM = "Conversation-FormView";
41
42 @Override
43 protected UifFormBase createInitialForm(HttpServletRequest request) {
44 return new ConversationViewForm();
45 }
46
47 @RequestMapping(method = RequestMethod.GET)
48 public ModelAndView get(@ModelAttribute("KualiForm") ConversationViewForm form,
49 HttpServletRequest request,
50 HttpServletResponse response) throws IOException {
51 super.start(form, request, response);
52 try {
53 initialize(form);
54 } catch (PermissionDeniedException e) {
55 String errorMessage = String.format("User %s is not permitted to view conversations.", request.getRemoteUser());
56 LOG.warn(errorMessage, e);
57 response.sendError(HttpServletResponse.SC_FORBIDDEN, errorMessage);
58 }
59 LOG.debug("CONVO_FORM: {}", form);
60 form.setViewId(CONVO_FORM);
61 form.setView(super.getViewService().getViewById(CONVO_FORM));
62 return getUIFModelAndView(form);
63 }
64
65 @RequestMapping(params = "methodToCall=send")
66 public ModelAndView submit(
67 @ModelAttribute("KualiForm") ConversationViewForm form,
68 BindingResult result, HttpServletRequest request,
69 HttpServletResponse response) throws IOException, ServletException {
70
71 if (!result.hasErrors()) {
72 RichText richMessage = new RichTextInfo(form.getNewComment(), form.getNewComment());
73 try {
74 ConversationComment newComment = getLearningPlanReviewStrategy().addCommentToConversation(form.getLearningPlanId(), false, richMessage);
75 addNewCommentToForm(newComment, form);
76 form.setNewComment("");
77 } catch (PermissionDeniedException e) {
78 throw new ServletException("Unexpected authorization failure", e);
79 }
80 }
81 return getUIFModelAndView(form);
82 }
83
84
85
86
87
88 private void initialize(ConversationViewForm form) throws PermissionDeniedException {
89
90 getLearningPlanReviewStrategy().markAllConversationCommentsAsRead(form.getLearningPlanId());
91
92 Conversation conversation = getLearningPlanReviewStrategy().getConversation(form.getLearningPlanId());
93
94 List<ConversationComment> comments = conversation.getComments();
95
96 Collections.sort(comments, Collections.reverseOrder());
97
98 form.setConversation(conversation);
99 List<ConversationDateCommentInfo> commentsByDate = new ArrayList<ConversationDateCommentInfo>();
100 Map<String, ConversationAdvisorInfo> advisorMap = new HashMap<String, ConversationAdvisorInfo>();
101 for (ConversationComment comment : conversation.getComments()) {
102 ConversationCommentInfo commentInfo = (ConversationCommentInfo) comment;
103 int day = commentInfo.getCommentCreationDay();
104 ConversationDateCommentInfo cdci = getCommentInfoListByDay(day, commentsByDate);
105 if (cdci == null) {
106 cdci = new ConversationDateCommentInfo();
107 cdci.setDayOfYear(day);
108 cdci.setDateDisplay(commentInfo.getCommentCreationDayDisplay());
109 cdci.addComment(commentInfo, false);
110 commentsByDate.add(cdci);
111 }
112 else {
113 cdci.addComment(commentInfo, false);
114 }
115
116
117
118 String role = comment.isByAdvisor() ? conversation.getAdvisor().getAdvisorRoleId() : "STUDENT";
119 String type = comment.isByAdvisor() ? conversation.getAdvisor().getAdvisorType() : ConversationConstants.CONV_COMMENTER_TYPE_STUDENT;
120 addAdvisorToMap(advisorMap, comment.getMeta().getCreateId(), role, type);
121 }
122
123
124 addAdvisorToMap(advisorMap, getUserId(), "STUDENT", ConversationConstants.CONV_COMMENTER_TYPE_STUDENT);
125 form.setAdvisorMap(advisorMap);
126
127
128 Collections.sort(commentsByDate, Collections.reverseOrder());
129 form.setCommentsByDate(commentsByDate);
130 }
131
132
133
134
135
136
137 private void addNewCommentToForm(ConversationComment comment, ConversationViewForm form) {
138 List<ConversationDateCommentInfo> commentsByDate = form.getCommentsByDate();
139 ConversationCommentInfo commentInfo = (ConversationCommentInfo) comment;
140 int day = commentInfo.getCommentCreationDay();
141 ConversationDateCommentInfo cdci = getCommentInfoListByDay(day, commentsByDate);
142 if (cdci == null) {
143 cdci = new ConversationDateCommentInfo();
144 cdci.setDayOfYear(day);
145 cdci.setDateDisplay(commentInfo.getCommentCreationDayDisplay());
146 cdci.addComment(commentInfo, true);
147 commentsByDate.add(0, cdci);
148 }
149 else {
150 cdci.addComment(commentInfo, true);
151 }
152
153
154 }
155
156
157
158
159
160
161
162 private ConversationDateCommentInfo getCommentInfoListByDay(Integer day, List<ConversationDateCommentInfo> commentsByDate) {
163 for (ConversationDateCommentInfo cdci : commentsByDate) {
164 if (day.equals(cdci.getDayOfYear()))
165 return cdci;
166 }
167 return null;
168 }
169
170 }