1 package org.kuali.ole.deliver.controller;
2
3
4 import org.kuali.ole.OLEConstants;
5 import org.kuali.ole.deliver.bo.OleFixedDateTimeSpan;
6 import org.kuali.ole.deliver.bo.OleFixedDueDate;
7 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
8 import org.kuali.rice.krad.service.KRADServiceLocator;
9 import org.kuali.rice.krad.uif.UifParameters;
10 import org.kuali.rice.krad.uif.container.CollectionGroup;
11 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
12 import org.kuali.rice.krad.util.GlobalVariables;
13 import org.kuali.rice.krad.util.KRADConstants;
14 import org.kuali.rice.krad.web.controller.MaintenanceDocumentController;
15 import org.kuali.rice.krad.web.form.DocumentFormBase;
16 import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
17 import org.kuali.rice.krad.web.form.UifFormBase;
18 import org.springframework.stereotype.Controller;
19 import org.springframework.validation.BindingResult;
20 import org.springframework.web.bind.annotation.ModelAttribute;
21 import org.springframework.web.bind.annotation.RequestMapping;
22 import org.springframework.web.bind.annotation.RequestMethod;
23 import org.springframework.web.servlet.ModelAndView;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27 import java.sql.Date;
28 import java.text.SimpleDateFormat;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33
34
35
36
37
38
39
40 @Controller
41 @RequestMapping(value = "/oleFixedDueDate")
42 public class OleFixedDueDateController extends MaintenanceDocumentController {
43
44 @Override
45 @RequestMapping(params = "methodToCall=route")
46 public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
47 HttpServletRequest request, HttpServletResponse response) {
48 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
49 MaintenanceDocument document = (MaintenanceDocument) maintenanceForm.getDocument();
50 OleFixedDueDate oleNewFixedDueDate = (OleFixedDueDate) document.getNewMaintainableObject().getDataObject();
51
52 if (oleNewFixedDueDate.getOleFixedDateTimeSpanList() != null && oleNewFixedDueDate.getOleFixedDateTimeSpanList().size() > 0) {
53 for (int i = 0; i < oleNewFixedDueDate.getOleFixedDateTimeSpanList().size(); i++) {
54 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
55 Date from = oleNewFixedDueDate.getOleFixedDateTimeSpanList().get(i).getFromDueDate();
56 String fromDate = null;
57 String toDate = null;
58 if (from == null) {
59 GlobalVariables.getMessageMap().putError(OLEConstants.FROM_DUE_DATE_FIELD, OLEConstants.FROM_DUE_DATE_MANDATORY);
60 } else {
61 fromDate = sdf.format(from);
62 }
63 Date to = oleNewFixedDueDate.getOleFixedDateTimeSpanList().get(i).getToDueDate();
64 if (to == null) {
65 GlobalVariables.getMessageMap().putError(OLEConstants.TO_DUE_DATE_FIELD, OLEConstants.TO_DUE_DATE_MANDATORY);
66 } else {
67 toDate = sdf.format(to);
68 }
69
70 Date fixedDate = oleNewFixedDueDate.getOleFixedDateTimeSpanList().get(i).getFixedDueDate();
71 if (fixedDate == null) {
72 GlobalVariables.getMessageMap().putError(OLEConstants.FIXED_DUE_DATE_FIELD, OLEConstants.FIXED_DUE_DATE_MANDATORY);
73 }
74
75 if (from == null || to == null || fixedDate == null) {
76 return getUIFModelAndView(form);
77 }
78 String timeSpan = fromDate + "-" + toDate;
79 (oleNewFixedDueDate.getOleFixedDateTimeSpanList().get(i)).setTimeSpan(timeSpan);
80
81 }
82 } else {
83 GlobalVariables.getMessageMap().putError(OLEConstants.FIXED_DUE_DATE_FIELD, OLEConstants.TIME_SPAN_MANDATORY);
84 return getUIFModelAndView(form);
85 }
86 List<OleFixedDateTimeSpan> oleDeleteFixedDateTimeSpanList = oleNewFixedDueDate.getOleDeleteFixedDateTimeSpanList();
87 for (OleFixedDateTimeSpan oleFixedDateTimeSpan : oleDeleteFixedDateTimeSpanList) {
88 if (oleFixedDateTimeSpan.getOleFixedDateTimeSpanId() != null) {
89 Map<String, String> map = new HashMap<String, String>();
90 map.put("OleFixedDateTimeSpanId", oleFixedDateTimeSpan.getOleFixedDateTimeSpanId());
91 KRADServiceLocator.getBusinessObjectService().deleteMatching(OleFixedDateTimeSpan.class, map);
92 }
93 }
94 return super.route(form, result, request, response);
95 }
96
97 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=validateAddLine")
98 public ModelAndView validateAddLine(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
99 HttpServletRequest request, HttpServletResponse response) {
100 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
101 MaintenanceDocument maintenanceDocument = (MaintenanceDocument) maintenanceForm.getDocument();
102 String selectedCollectionPath = maintenanceForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
103 CollectionGroup collectionGroup = maintenanceForm.getPostedView().getViewIndex().getCollectionGroupByPath(
104 selectedCollectionPath);
105 String addLinePath = collectionGroup.getAddLineBindingInfo().getBindingPath();
106 Object eventObject = ObjectPropertyUtils.getPropertyValue(maintenanceForm, addLinePath);
107 OleFixedDateTimeSpan oleFixedDateTimeSpan = (OleFixedDateTimeSpan) eventObject;
108 if(oleFixedDateTimeSpan.getFromDueDate()!=null && oleFixedDateTimeSpan.getToDueDate()!=null && oleFixedDateTimeSpan.getFixedDueDate()!=null) {
109 GlobalVariables.getMessageMap().removeAllErrorMessagesForProperty(OLEConstants.FROM_DUE_DATE_FIELD);
110 ModelAndView modelAndView = super.addLine(form, result, request, response);
111 return modelAndView;
112 }else {
113 if(oleFixedDateTimeSpan.getFromDueDate() == null)
114 GlobalVariables.getMessageMap().putErrorForSectionId("create_timeSpan", OLEConstants.FROM_DUE_DATE_MANDATORY);
115 if(oleFixedDateTimeSpan.getToDueDate() == null)
116 GlobalVariables.getMessageMap().putErrorForSectionId("create_timeSpan", OLEConstants.TO_DUE_DATE_MANDATORY);
117 if(oleFixedDateTimeSpan.getFixedDueDate() == null)
118 GlobalVariables.getMessageMap().putErrorForSectionId("create_timeSpan", OLEConstants.FIXED_DUE_DATE_MANDATORY);
119 return getUIFModelAndView(form);
120 }
121 }
122
123
124 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=timeSpanDelete")
125 public ModelAndView timeSpanDelete(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
126 HttpServletRequest request, HttpServletResponse response) {
127 LOG.debug("Initialized deleteLine method");
128 MaintenanceDocumentForm form = (MaintenanceDocumentForm) uifForm;
129 String selectedLineIndex = form.getActionParamaterValue("selectedLineIndex");
130 MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
131 OleFixedDueDate oleFixedDueDate = (OleFixedDueDate) document.getNewMaintainableObject().getDataObject();
132 oleFixedDueDate.getOleDeleteFixedDateTimeSpanList().add(oleFixedDueDate.getOleFixedDateTimeSpanList().get(Integer.parseInt(selectedLineIndex)));
133 return deleteLine(uifForm, result, request, response);
134 }
135
136 @Override
137 @RequestMapping(params = "methodToCall=maintenanceCopy")
138 public ModelAndView maintenanceCopy(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result, HttpServletRequest request, HttpServletResponse response) throws Exception {
139 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
140 setupMaintenance(form, request, KRADConstants.MAINTENANCE_COPY_ACTION);
141 super.maintenanceCopy(form, result, request, response);
142 MaintenanceDocument document = (MaintenanceDocument) maintenanceForm.getDocument();
143 OleFixedDueDate oleNewFixedDueDate = (OleFixedDueDate) document.getNewMaintainableObject().getDataObject();
144 List<OleFixedDateTimeSpan> oleDeleteFixedDateTimeSpanList = oleNewFixedDueDate.getOleFixedDateTimeSpanList();
145 oleNewFixedDueDate.setFixedDueDateId(null);
146 oleNewFixedDueDate.setObjectId(null);
147 oleNewFixedDueDate.setVersionNumber(null);
148 for (OleFixedDateTimeSpan oleFixedDateTimeSpan : oleDeleteFixedDateTimeSpanList) {
149 oleFixedDateTimeSpan.setOleFixedDateTimeSpanId(null);
150 oleFixedDateTimeSpan.setObjectId(null);
151 oleFixedDateTimeSpan.setVersionNumber(null);
152 oleFixedDateTimeSpan.setOleFixedDueDate(oleNewFixedDueDate);
153 }
154 return getUIFModelAndView(form);
155 }
156 }