1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.mobility.reporting.controllers;
17
18 import java.io.IOException;
19 import java.sql.Timestamp;
20 import java.util.ArrayList;
21 import java.util.Date;
22 import java.util.List;
23 import java.util.Locale;
24 import java.util.Properties;
25
26 import javax.annotation.Resource;
27 import javax.servlet.http.HttpServletRequest;
28
29 import org.kuali.mobility.file.entity.File;
30 import org.kuali.mobility.reporting.domain.Incident;
31 import org.kuali.mobility.reporting.entity.Submission;
32 import org.kuali.mobility.reporting.entity.SubmissionAttribute;
33 import org.kuali.mobility.reporting.service.ReportingService;
34 import org.kuali.mobility.security.user.api.User;
35 import org.kuali.mobility.shared.Constants;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.context.MessageSource;
38 import org.springframework.stereotype.Controller;
39 import org.springframework.ui.Model;
40 import org.springframework.ui.ModelMap;
41 import org.springframework.validation.BindingResult;
42 import org.springframework.validation.Errors;
43 import org.springframework.web.bind.annotation.ModelAttribute;
44 import org.springframework.web.bind.annotation.PathVariable;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.bind.annotation.RequestMethod;
47 import org.springframework.web.bind.support.SessionStatus;
48 import org.springframework.web.servlet.support.RequestContextUtils;
49
50 @Controller
51 @RequestMapping("/reporting")
52 public class ReportingController {
53
54 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ReportingController.class);
55
56 public static final String INCIDENT_TYPE = "INCIDENT";
57 public static final String INCIDENT_GROUP = "INCIDENT_GROUP";
58 public static final String SUMMARY = "SUMMARY";
59 public static final String EMAIL = "EMAIL";
60 public static final String AFFILIATION_STUDENT = "AFFILIATION_STUDENT";
61 public static final String AFFILIATION_FACULTY = "AFFILIATION_FACULTY";
62 public static final String AFFILIATION_STAFF = "AFFILIATION_STAFF";
63 public static final String AFFILIATION_OTHER = "AFFILIATION_OTHER";
64 public static final String CONTACT_ME = "CONTACT_ME";
65 public static final String ATTACHMENT = "ATTACHMENT";
66 public static final String COMMENT = "COMMENT";
67
68 @Autowired
69 private ReportingService reportingService;
70
71 @Resource(name="messageSource")
72 private MessageSource messageSource;
73
74 @Resource(name="kmeProperties")
75 private Properties kmeProperties;
76
77
78 @RequestMapping(method = RequestMethod.GET)
79 public String index(Model uiModel, HttpServletRequest request) {
80
81 if( "3".equalsIgnoreCase( getKmeProperties().getProperty("kme.uiVersion","classic") ) ) {
82 return "ui3/reporting/index";
83 } else {
84 return "reporting/index";
85 }
86 }
87
88
89
90
91
92
93 @RequestMapping(value = "/admin/index", method = RequestMethod.GET)
94 public String adminIndex(Model uiModel, HttpServletRequest request) {
95
96
97
98
99 List<Submission> submissions = reportingService.findAllSubmissions();
100
101 uiModel.addAttribute("submissions", submissions);
102 if( "3".equalsIgnoreCase( getKmeProperties().getProperty("kme.uiVersion","classic") ) ) {
103 return "ui3/reporting/adminIndex";
104 } else {
105 return "reporting/admin/index";
106 }
107 }
108
109 @RequestMapping(value = "/admin/incident/details/{id}", method = RequestMethod.GET)
110 public String adminDetails(@PathVariable("id") Long id, Model uiModel, HttpServletRequest request) {
111
112
113 prepareSubmissionById(id, uiModel, request);
114
115 return "reporting/admin/incident/details";
116 }
117
118 @RequestMapping(value = "/admin/incident/save", method = RequestMethod.POST)
119 public String saveSumission(HttpServletRequest request, ModelMap model, @ModelAttribute("file") File file, BindingResult result, SessionStatus status) {
120
121 if (file != null && file.getFile() != null) {
122 String contentType = file.getFile().getContentType();
123 String fileName = file.getFile().getOriginalFilename();
124 file.setContentType(contentType);
125 file.setFileName(fileName);
126 try {
127 file.setBytes(file.getFile().getBytes());
128 } catch (IOException e) {
129 LOG.error("File contained no bytes.", e);
130 }
131 reportingService.saveAttachment(file);
132 }
133 return "reporting/admin/index";
134
135 }
136
137 @RequestMapping(value = "/admin/incident/revisions/{id}", method = RequestMethod.GET)
138 public String revisions(@PathVariable("id") Long id, Model uiModel, HttpServletRequest request) {
139
140
141
142
143 Submission submission = reportingService.findSubmissionById(id);
144 Long parentId = submission.getParentId() == null ? submission.getId() : submission.getParentId();
145
146 List<Submission> submissions = reportingService.findAllSubmissionsByParentId(parentId);
147
148 uiModel.addAttribute("submissions", submissions);
149 return "reporting/admin/incident/revisions";
150 }
151
152 @RequestMapping(value = "/admin/incident/savefile", method = RequestMethod.POST)
153 public String addFile(HttpServletRequest request, ModelMap model, @ModelAttribute("file") Incident incident, BindingResult result, SessionStatus status) {
154
155 Submission oldSubmission = reportingService.findSubmissionById(incident.getId());
156 oldSubmission.setActive(0);
157 oldSubmission.setArchivedDate(new Timestamp(System.currentTimeMillis()));
158
159 reportingService.saveSubmission(oldSubmission);
160
161 Long parentId = oldSubmission.getParentId() == null ? oldSubmission.getId() : oldSubmission.getParentId();
162
163 Submission revisedSubmission = new Submission();
164 revisedSubmission.setActive(1);
165 revisedSubmission.setParentId(parentId);
166 revisedSubmission.setPostDate(new Timestamp(System.currentTimeMillis()));
167 revisedSubmission.setRevisionNumber(oldSubmission.getRevisionNumber() + 1);
168 revisedSubmission.setType(INCIDENT_TYPE);
169 revisedSubmission.setGroup(INCIDENT_GROUP);
170
171 Long pk = reportingService.saveSubmission(revisedSubmission);
172
173 SubmissionAttribute submissionAttributeSummary = new SubmissionAttribute();
174 submissionAttributeSummary.setKey(SUMMARY);
175 submissionAttributeSummary.setValueLargeText(incident.getSummary());
176 submissionAttributeSummary.setSubmissionId(pk);
177 submissionAttributeSummary.setSubmission(revisedSubmission);
178
179 SubmissionAttribute submissionAttributeContactMe = new SubmissionAttribute();
180 submissionAttributeContactMe.setKey(CONTACT_ME);
181 submissionAttributeContactMe.setValueNumber(incident.isContactMe() ? 1L : 0L);
182 submissionAttributeContactMe.setSubmissionId(pk);
183 submissionAttributeContactMe.setSubmission(revisedSubmission);
184
185 SubmissionAttribute submissionAttributeEmail = new SubmissionAttribute();
186 submissionAttributeEmail.setKey(EMAIL);
187 submissionAttributeEmail.setValueText(incident.getEmail());
188 submissionAttributeEmail.setSubmissionId(pk);
189 submissionAttributeEmail.setSubmission(revisedSubmission);
190
191 SubmissionAttribute submissionAttributeAffiliationStudent = new SubmissionAttribute();
192 submissionAttributeAffiliationStudent.setKey(AFFILIATION_STUDENT);
193 submissionAttributeAffiliationStudent.setValueText(incident.getAffiliationStudent());
194 submissionAttributeAffiliationStudent.setSubmissionId(pk);
195 submissionAttributeAffiliationStudent.setSubmission(revisedSubmission);
196
197 SubmissionAttribute submissionAttributeAffiliationFaculty = new SubmissionAttribute();
198 submissionAttributeAffiliationFaculty.setKey(AFFILIATION_FACULTY);
199 submissionAttributeAffiliationFaculty.setValueText(incident.getAffiliationFaculty());
200 submissionAttributeAffiliationFaculty.setSubmissionId(pk);
201 submissionAttributeAffiliationFaculty.setSubmission(revisedSubmission);
202
203 SubmissionAttribute submissionAttributeAffiliationStaff = new SubmissionAttribute();
204 submissionAttributeAffiliationStaff.setKey(AFFILIATION_STAFF);
205 submissionAttributeAffiliationStaff.setValueText(incident.getAffiliationStaff());
206 submissionAttributeAffiliationStaff.setSubmissionId(pk);
207 submissionAttributeAffiliationStaff.setSubmission(revisedSubmission);
208
209 SubmissionAttribute submissionAttributeAffiliationOther = new SubmissionAttribute();
210 submissionAttributeAffiliationOther.setKey(AFFILIATION_OTHER);
211 submissionAttributeAffiliationOther.setValueText(incident.getAffiliationOther());
212 submissionAttributeAffiliationOther.setSubmissionId(pk);
213 submissionAttributeAffiliationOther.setSubmission(revisedSubmission);
214
215 ArrayList<SubmissionAttribute> attributes = new ArrayList<SubmissionAttribute>();
216 attributes.add(submissionAttributeSummary);
217 attributes.add(submissionAttributeEmail);
218 attributes.add(submissionAttributeContactMe);
219 attributes.add(submissionAttributeAffiliationStudent);
220 attributes.add(submissionAttributeAffiliationFaculty);
221 attributes.add(submissionAttributeAffiliationStaff);
222 attributes.add(submissionAttributeAffiliationOther);
223
224
225 if (incident != null && incident.getFile() != null) {
226 SubmissionAttribute newAttachment = new SubmissionAttribute();
227 newAttachment.setKey(ATTACHMENT);
228 newAttachment.setSubmissionId(pk);
229 newAttachment.setSubmission(revisedSubmission);
230 newAttachment.setContentType(incident.getFile().getContentType());
231 newAttachment.setFileName(incident.getFile().getOriginalFilename());
232 try {
233 newAttachment.setValueBinary(incident.getFile().getBytes());
234 } catch (IOException e) {
235 LOG.error("File contained no bytes.", e);
236 }
237 attributes.add(newAttachment);
238 }
239
240
241 if (incident != null && incident.getNewComment() != null) {
242 SubmissionAttribute newComment = new SubmissionAttribute();
243 newComment.setKey(COMMENT);
244 newComment.setSubmissionId(pk);
245 newComment.setSubmission(revisedSubmission);
246 newComment.setValueLargeText(incident.getNewComment());
247 attributes.add(newComment);
248 }
249
250
251 for (SubmissionAttribute submissionAttribute : findAttributesByKey(ATTACHMENT, oldSubmission.getAttributes())) {
252 SubmissionAttribute oldAttachment = new SubmissionAttribute();
253 oldAttachment.setKey(ATTACHMENT);
254 oldAttachment.setSubmissionId(revisedSubmission.getId());
255 oldAttachment.setSubmission(revisedSubmission);
256 oldAttachment.setContentType(submissionAttribute.getContentType());
257 oldAttachment.setFileName(submissionAttribute.getFileName());
258 oldAttachment.setValueBinary(submissionAttribute.getValueBinary());
259 attributes.add(oldAttachment);
260 }
261 for (SubmissionAttribute submissionAttribute : findAttributesByKey(COMMENT, oldSubmission.getAttributes())) {
262 SubmissionAttribute oldComment = new SubmissionAttribute();
263 oldComment.setKey(COMMENT);
264 oldComment.setSubmissionId(revisedSubmission.getId());
265 oldComment.setSubmission(revisedSubmission);
266 oldComment.setValueLargeText(submissionAttribute.getValueLargeText());
267 attributes.add(oldComment);
268 }
269
270 revisedSubmission.setAttributes(attributes);
271
272 reportingService.saveSubmission(revisedSubmission);
273
274 return "reporting/admin/index";
275
276 }
277
278 @RequestMapping(value = "/admin/incident/edit/{id}", method = RequestMethod.GET)
279 public String adminEdit(@PathVariable("id") Long id, Model uiModel, HttpServletRequest request) {
280
281
282 Submission submission = reportingService.findSubmissionById(id);
283
284 Incident incident = new Incident();
285
286 prepareSubmissionById(id, uiModel, request);
287
288 String affiliationStudent = findAttributeByKey(AFFILIATION_STUDENT, submission.getAttributes()).getValueText();
289 String affiliationFaculty = findAttributeByKey(AFFILIATION_FACULTY, submission.getAttributes()).getValueText();
290 String affiliationStaff = findAttributeByKey(AFFILIATION_STAFF, submission.getAttributes()).getValueText();
291 String affiliationOther = findAttributeByKey(AFFILIATION_OTHER, submission.getAttributes()).getValueText();
292
293 incident.setSummary(findAttributeByKey(SUMMARY, submission.getAttributes()).getValueLargeText());
294 incident.setAffiliationFaculty(affiliationFaculty);
295 incident.setAffiliationStudent(affiliationStudent);
296 incident.setAffiliationStaff(affiliationStaff);
297 incident.setAffiliationOther(affiliationOther);
298
299 incident.setAttachments(findAttributesByKey(ATTACHMENT, submission.getAttributes()));
300 incident.setComments(findAttributesByKey(COMMENT, submission.getAttributes()));
301
302 uiModel.addAttribute("incident", incident);
303
304 return "reporting/admin/incident/edit";
305 }
306
307
308 private void prepareSubmissionById(Long id, Model uiModel, HttpServletRequest request) {
309 Submission submission = reportingService.findSubmissionById(id);
310
311 uiModel.addAttribute("submission", submission);
312
313 uiModel.addAttribute("summary", findAttributeByKey(SUMMARY, submission.getAttributes()).getValueLargeText());
314
315 uiModel.addAttribute("email", findAttributeByKey(EMAIL, submission.getAttributes()).getValueText());
316
317 String affiliationStudent = findAttributeByKey(AFFILIATION_STUDENT, submission.getAttributes()).getValueText();
318 String affiliationFaculty = findAttributeByKey(AFFILIATION_FACULTY, submission.getAttributes()).getValueText();
319 String affiliationStaff = findAttributeByKey(AFFILIATION_STAFF, submission.getAttributes()).getValueText();
320 String affiliationOther = findAttributeByKey(AFFILIATION_OTHER, submission.getAttributes()).getValueText();
321
322 Locale locale = RequestContextUtils.getLocale(request);
323 String msgCat_Yes = messageSource.getMessage("shared.yes", null, locale);
324 String msgCat_No = messageSource.getMessage("shared.yes", null, locale);
325
326 if (affiliationStudent != null) {
327 uiModel.addAttribute("affiliationStudent", messageSource.getMessage("reporting.affiliation.student", null, locale));
328 }
329 if (affiliationFaculty != null) {
330 uiModel.addAttribute("affiliationFaculty", messageSource.getMessage("reporting.affiliation.faculty", null, locale));
331 }
332 if (affiliationStaff != null) {
333 uiModel.addAttribute("affiliationStaff", messageSource.getMessage("reporting.affiliation.staff", null, locale));
334 }
335 if (affiliationOther != null) {
336 uiModel.addAttribute("affiliationOther", messageSource.getMessage("reporting.affiliation.other", null, locale));
337 }
338 uiModel.addAttribute("affiliations", !(affiliationStudent == null && affiliationFaculty == null && affiliationStaff == null && affiliationOther == null));
339
340 boolean contactMe = findAttributeByKey(CONTACT_ME, submission.getAttributes()).getValueNumber() == 1 ? true : false;
341 uiModel.addAttribute("contactMe", contactMe);
342 uiModel.addAttribute("contactMeText", contactMe ? msgCat_Yes : msgCat_No );
343 uiModel.addAttribute("attachments", findAttributesByKey(ATTACHMENT, submission.getAttributes()));
344 uiModel.addAttribute("comments", findAttributesByKey(COMMENT, submission.getAttributes()));
345 uiModel.addAttribute("activeText", submission.getActive() == 1 ? msgCat_Yes : msgCat_No);
346
347 }
348
349 private SubmissionAttribute findAttributeByKey(String key, List<SubmissionAttribute> attributes) {
350 if (key == null || key.trim().equals("")) {
351 return null;
352 }
353 SubmissionAttribute found = null;
354 for (SubmissionAttribute submissionAttribute : attributes) {
355 if (key.trim().equals(submissionAttribute.getKey())) {
356 found = submissionAttribute;
357 break;
358 }
359 }
360 return found;
361 }
362
363 private List<SubmissionAttribute> findAttributesByKey(String key, List<SubmissionAttribute> attributes) {
364 if (key == null || key.trim().equals("")) {
365 return null;
366 }
367 List<SubmissionAttribute> found = new ArrayList<SubmissionAttribute>();
368 for (SubmissionAttribute submissionAttribute : attributes) {
369 if (key.trim().equals(submissionAttribute.getKey())) {
370 found.add(submissionAttribute);
371 }
372 }
373 return found;
374 }
375
376
377
378
379
380
381
382 @RequestMapping(value="/incidentForm", method = RequestMethod.GET)
383 public String incidentForm(Model uiModel, HttpServletRequest request) {
384
385 Incident incident = new Incident();
386 uiModel.addAttribute("incident", incident);
387 if( "3".equalsIgnoreCase( getKmeProperties().getProperty("kme.uiVersion","classic") ) ) {
388 return "ui3/reporting/incidentIndex";
389 } else {
390 return "incident/form";
391 }
392 }
393
394 @RequestMapping(value="/incidentPost", method = RequestMethod.POST)
395 public String submitIncident(Model uiModel, HttpServletRequest request, @ModelAttribute("incident") Incident incident, BindingResult result) {
396 User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
397
398 if (isValidIncident(incident, result)) {
399 Submission submission = new Submission();
400 submission.setType(INCIDENT_TYPE);
401 submission.setActive(1);
402 submission.setGroup(INCIDENT_GROUP);
403
404 submission.setPostDate(new Timestamp(new Date().getTime()));
405 submission.setRevisionNumber(0L);
406 submission.setUserId(user.getLoginName());
407
408
409 Long pk = reportingService.saveSubmission(submission);
410
411 SubmissionAttribute submissionAttributeSummary = new SubmissionAttribute();
412 submissionAttributeSummary.setKey(SUMMARY);
413 submissionAttributeSummary.setValueLargeText(incident.getSummary());
414 submissionAttributeSummary.setSubmissionId(pk);
415 submissionAttributeSummary.setSubmission(submission);
416
417 SubmissionAttribute submissionAttributeContactMe = new SubmissionAttribute();
418 submissionAttributeContactMe.setKey(CONTACT_ME);
419 submissionAttributeContactMe.setValueNumber(incident.isContactMe() ? 1L : 0L);
420 submissionAttributeContactMe.setSubmissionId(pk);
421 submissionAttributeContactMe.setSubmission(submission);
422
423 SubmissionAttribute submissionAttributeEmail = new SubmissionAttribute();
424 submissionAttributeEmail.setKey(EMAIL);
425 submissionAttributeEmail.setValueText(incident.getEmail());
426 submissionAttributeEmail.setSubmissionId(pk);
427 submissionAttributeEmail.setSubmission(submission);
428
429 SubmissionAttribute submissionAttributeAffiliationStudent = new SubmissionAttribute();
430 submissionAttributeAffiliationStudent.setKey(AFFILIATION_STUDENT);
431 submissionAttributeAffiliationStudent.setValueText(incident.getAffiliationStudent());
432 submissionAttributeAffiliationStudent.setSubmissionId(pk);
433 submissionAttributeAffiliationStudent.setSubmission(submission);
434
435 SubmissionAttribute submissionAttributeAffiliationFaculty = new SubmissionAttribute();
436 submissionAttributeAffiliationFaculty.setKey(AFFILIATION_FACULTY);
437 submissionAttributeAffiliationFaculty.setValueText(incident.getAffiliationFaculty());
438 submissionAttributeAffiliationFaculty.setSubmissionId(pk);
439 submissionAttributeAffiliationFaculty.setSubmission(submission);
440
441 SubmissionAttribute submissionAttributeAffiliationStaff = new SubmissionAttribute();
442 submissionAttributeAffiliationStaff.setKey(AFFILIATION_STAFF);
443 submissionAttributeAffiliationStaff.setValueText(incident.getAffiliationStaff());
444 submissionAttributeAffiliationStaff.setSubmissionId(pk);
445 submissionAttributeAffiliationStaff.setSubmission(submission);
446
447 SubmissionAttribute submissionAttributeAffiliationOther = new SubmissionAttribute();
448 submissionAttributeAffiliationOther.setKey(AFFILIATION_OTHER);
449 submissionAttributeAffiliationOther.setValueText(incident.getAffiliationOther());
450 submissionAttributeAffiliationOther.setSubmissionId(pk);
451 submissionAttributeAffiliationOther.setSubmission(submission);
452
453 ArrayList<SubmissionAttribute> attributes = new ArrayList<SubmissionAttribute>();
454 attributes.add(submissionAttributeSummary);
455 attributes.add(submissionAttributeEmail);
456 attributes.add(submissionAttributeContactMe);
457 attributes.add(submissionAttributeAffiliationStudent);
458 attributes.add(submissionAttributeAffiliationFaculty);
459 attributes.add(submissionAttributeAffiliationStaff);
460 attributes.add(submissionAttributeAffiliationOther);
461
462 submission.setAttributes(attributes);
463
464 reportingService.saveSubmission(submission);
465
466 return "incident/thanks";
467 } else {
468 return "incident/form";
469 }
470 }
471
472 @RequestMapping(value="/templates/{key}")
473 public String getAngularTemplates(
474 @PathVariable("key") String key,
475 HttpServletRequest request,
476 Model uiModel ) {
477 return "ui3/reporting/templates/"+key;
478 }
479
480 @RequestMapping(value = "/js/{key}.js")
481 public String getJavaScript(
482 @PathVariable("key") String key,
483 Model uiModel) {
484 return "ui3/reporting/js/"+key;
485 }
486
487 private boolean isValidIncident(Incident incident, BindingResult result) {
488 boolean hasErrors = false;
489 Errors errors = ((Errors) result);
490 if (incident == null || incident.getSummary() == null || "".equals(incident.getSummary().trim())) {
491 errors.rejectValue("summary", "", "Please enter a summary.");
492 hasErrors = true;
493 }
494 return !hasErrors;
495 }
496
497
498 public Properties getKmeProperties() {
499 return kmeProperties;
500 }
501
502
503 public void setKmeProperties(Properties kmeProperties) {
504 this.kmeProperties = kmeProperties;
505 }
506
507 }