1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.student.cm.decision.controller;
18
19 import org.apache.commons.lang.StringUtils;
20 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
21 import org.kuali.rice.kim.api.identity.Person;
22 import org.kuali.rice.kim.api.identity.PersonService;
23 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
24 import org.kuali.rice.krad.web.controller.KsUifControllerBase;
25 import org.kuali.rice.krad.web.controller.MethodAccessible;
26 import org.kuali.rice.krad.web.form.UifFormBase;
27 import org.kuali.student.cm.common.util.CurriculumManagementConstants;
28 import org.kuali.student.cm.decision.form.CMDecisionForm;
29 import org.kuali.student.cm.decision.form.wrapper.CMDecisionWrapper;
30 import org.kuali.student.common.util.security.ContextUtils;
31 import org.kuali.student.r1.common.rice.StudentIdentityConstants;
32 import org.kuali.student.r2.common.util.date.DateFormatters;
33 import org.kuali.student.r2.core.comment.dto.CommentInfo;
34 import org.kuali.student.r2.core.comment.service.CommentService;
35 import org.kuali.student.r2.core.constants.CommentServiceConstants;
36 import org.kuali.student.r2.core.constants.ProposalServiceConstants;
37 import org.kuali.student.r2.core.proposal.dto.ProposalInfo;
38 import org.kuali.student.r2.core.proposal.service.ProposalService;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.stereotype.Controller;
42 import org.springframework.web.bind.annotation.ModelAttribute;
43 import org.springframework.web.bind.annotation.RequestMapping;
44 import org.springframework.web.servlet.ModelAndView;
45
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48 import javax.xml.namespace.QName;
49 import java.util.Collections;
50 import java.util.HashMap;
51 import java.util.List;
52 import java.util.Map;
53
54
55
56
57
58
59 @Controller
60 @RequestMapping(value = CurriculumManagementConstants.ControllerRequestMappings.CM_DECISION)
61 public class CMDecisionController extends KsUifControllerBase {
62
63 private static final Logger LOG = LoggerFactory.getLogger(CMDecisionController.class);
64
65 protected PersonService personService;
66 protected CommentService commentService;
67 protected ProposalService proposalService;
68
69 @Override
70 protected UifFormBase createInitialForm(HttpServletRequest request) {
71 return new CMDecisionForm();
72 }
73
74 @MethodAccessible
75 @RequestMapping(params = "methodToCall=start")
76 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, HttpServletRequest request,
77 HttpServletResponse response) {
78
79 CMDecisionForm decisionForm = (CMDecisionForm) form;
80
81 String proposalId = request.getParameter("proposalId");
82
83 if (StringUtils.isBlank(proposalId)) {
84 throw new RuntimeException("Missing proposal Id");
85 }
86
87 try {
88 ProposalInfo proposalInfo = getProposalService().getProposal(proposalId, ContextUtils.createDefaultContextInfo());
89 decisionForm.setProposal(proposalInfo);
90 retrieveDecisions(decisionForm);
91 } catch (Exception e) {
92 throw new RuntimeException("Invalid Proposal [id=" + proposalId + "]",e);
93 }
94
95 retrieveDecisions(decisionForm);
96
97 return super.start(form, request, response);
98 }
99
100 protected void retrieveDecisions(CMDecisionForm form) {
101
102 ProposalInfo proposal = form.getProposal();
103
104 LOG.debug("Retrieving decisions for - " + proposal.getId());
105
106 List<CommentInfo> decisions;
107
108 form.getDecisions().clear();
109
110 try {
111 decisions = getCommentService().getCommentsByRefObject(proposal.getId(), StudentIdentityConstants.QUALIFICATION_PROPOSAL_REF_TYPE, ContextUtils.createDefaultContextInfo());
112 } catch (Exception e) {
113 throw new RuntimeException("Error retrieving decision(s) for the proposal [id=" + proposal.getId() + "]", e);
114 }
115
116 Map<String,String> personId2DisplayName = new HashMap<>();
117
118 for (CommentInfo comment : decisions) {
119
120 if (isWorkflowDecision(comment.getTypeKey())){
121 CMDecisionWrapper wrapper = new CMDecisionWrapper(comment);
122 if (!personId2DisplayName.containsKey(comment.getCommenterId())) {
123 personId2DisplayName.put(comment.getCommenterId(), getDisplayNameForPrincipalId(comment.getCommenterId()));
124 }
125 wrapper.setActor(personId2DisplayName.get(comment.getCommenterId()));
126 wrapper.setDate(DateFormatters.MONTH_DAY_YEAR_DATE_FORMATTER.format(comment.getMeta().getCreateTime()));
127 wrapper.setDecision(CommentServiceConstants.WORKFLOW_DECISIONS.getByType(comment.getTypeKey()).getLabel());
128
129 wrapper.setRationale(comment.getCommentText().getPlain());
130 form.getDecisions().add(wrapper);
131 }
132 }
133
134 if (!form.getDecisions().isEmpty()){
135 Collections.sort(form.getDecisions());
136 }
137
138 LOG.debug("There are " + form.getDecisions().size() + " decisions for proposal " + proposal.getId());
139
140
141 }
142
143 protected String getDisplayNameForPrincipalId(String principalId) {
144 Person person = getPersonService().getPerson(principalId);
145 if (person == null) {
146 throw new RuntimeException("Error fetching person for principal id '" + principalId + "'");
147 }
148 return (new StringBuilder(person.getFirstName())).append(" ").append(person.getLastName()).toString();
149 }
150
151 protected boolean isWorkflowDecision(String typeKey){
152 for(CommentServiceConstants.WORKFLOW_DECISIONS workflowDecision : CommentServiceConstants.WORKFLOW_DECISIONS.values()){
153 if(StringUtils.equalsIgnoreCase(workflowDecision.getType(),typeKey)){
154 return true;
155 }
156 }
157 return false;
158 }
159
160 protected PersonService getPersonService() {
161 if (personService == null) {
162 personService = KimApiServiceLocator.getPersonService();
163 }
164 return personService;
165 }
166
167 protected CommentService getCommentService() {
168 if (commentService == null) {
169 commentService = (CommentService) GlobalResourceLoader.getService(new QName(CommentServiceConstants.NAMESPACE, CommentService.class.getSimpleName()));
170 }
171 return commentService;
172 }
173
174 protected ProposalService getProposalService() {
175 if (proposalService == null) {
176 proposalService = (ProposalService) GlobalResourceLoader.getService(new QName(ProposalServiceConstants.NAMESPACE, ProposalServiceConstants.SERVICE_NAME_LOCAL_PART));
177 }
178 return proposalService;
179 }
180 }