1 package org.kuali.ole.deliver.controller;
2
3 import org.apache.log4j.Logger;
4 import org.kuali.ole.OLEConstants;
5 import org.kuali.ole.alert.bo.AlertConditionAndReceiverInformation;
6 import org.kuali.ole.alert.bo.AlertDocument;
7 import org.kuali.ole.alert.bo.AlertDocumentType;
8 import org.kuali.ole.alert.bo.AlertFieldValueMapping;
9 import org.kuali.ole.deliver.service.AlertDocumentService;
10 import org.kuali.ole.deliver.service.impl.AlertDocumentServiceImpl;
11 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
12 import org.kuali.rice.krad.service.KRADServiceLocator;
13 import org.kuali.rice.krad.uif.UifParameters;
14 import org.kuali.rice.krad.uif.container.CollectionGroup;
15 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
16 import org.kuali.rice.krad.util.GlobalVariables;
17 import org.kuali.rice.krad.web.controller.MaintenanceDocumentController;
18 import org.kuali.rice.krad.web.form.DocumentFormBase;
19 import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
20 import org.kuali.rice.krad.web.form.UifFormBase;
21 import org.springframework.stereotype.Controller;
22 import org.springframework.util.StringUtils;
23 import org.springframework.validation.BindingResult;
24 import org.springframework.web.bind.annotation.ModelAttribute;
25 import org.springframework.web.bind.annotation.RequestMapping;
26 import org.springframework.web.bind.annotation.RequestMethod;
27 import org.springframework.web.servlet.ModelAndView;
28
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
36
37
38
39
40
41 @Controller
42 @RequestMapping(value = "/alertMaintenance")
43 public class AlertDocumentController extends MaintenanceDocumentController {
44
45 private static final Logger LOG = Logger.getLogger(AlertDocumentController.class);
46
47
48
49
50
51
52
53 @Override
54 public ModelAndView save(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) throws Exception {
55
56 return super.save(form, result, request, response);
57 }
58
59
60
61
62
63
64
65 @Override
66 @RequestMapping(params = "methodToCall=route")
67 public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
68 HttpServletRequest request, HttpServletResponse response) {
69
70 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
71 MaintenanceDocument maintenanceDocument = (MaintenanceDocument) form.getDocument();
72 AlertDocument alertDocument = (AlertDocument)maintenanceDocument.getDocumentDataObject();
73 if(alertDocument.getDocumentTypeName() == null || alertDocument.getDocumentTypeName().equalsIgnoreCase("")){
74 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ALRT_DOC_TYPE_NAME_FIELD,OLEConstants.EMPTY_DOCTYPENAME);
75 getUIFModelAndView(form);
76 }else if(validateDocTypeName(alertDocument)){
77 getUIFModelAndView(form);
78 }
79 if(alertDocument.getAlertConditionAndReceiverInformations()==null || alertDocument.getAlertConditionAndReceiverInformations().size()==0){
80 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ALRT_DOC_TYPE_NAME_FIELD,OLEConstants.ACTION_EVENT);
81 getUIFModelAndView(form);
82 }
83
84
85
86 return super.route(form, result, request, response);
87 }
88
89 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=mapAddLine")
90 public ModelAndView searchAddLine(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
91 HttpServletRequest request, HttpServletResponse response) {
92 boolean name = false;
93 boolean type = false;
94 boolean value = false;
95
96 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
97 String selectedCollectionPath = form.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
98 CollectionGroup collectionGroup = form.getPostedView().getViewIndex().getCollectionGroupByPath(
99 selectedCollectionPath);
100 String addLinePath = collectionGroup.getAddLineBindingInfo().getBindingPath();
101 Object alertDocumentObject = ObjectPropertyUtils.getPropertyValue(maintenanceForm, addLinePath);
102 AlertFieldValueMapping alertDocumentFieldValueMapping = (AlertFieldValueMapping) alertDocumentObject;
103 if(alertDocumentFieldValueMapping.getFieldName() == null || alertDocumentFieldValueMapping.getFieldName().equalsIgnoreCase("")){
104 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ALRT_DOC_ALRT_FIELD_VAL,OLEConstants.EMPTY_FIELDNAME);
105 name=true;
106 }
107 if(alertDocumentFieldValueMapping.getFieldType() == null || alertDocumentFieldValueMapping.getFieldType().equalsIgnoreCase("")){
108 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ALRT_DOC_ALRT_FIELD_VAL,OLEConstants.EMPTY_FIELDTYPE);
109 type=true;
110 }
111 if(alertDocumentFieldValueMapping.getFieldValue() == null || alertDocumentFieldValueMapping.getFieldValue().equalsIgnoreCase("") && !alertDocumentFieldValueMapping.getFieldType().equalsIgnoreCase("boolean")){
112 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ALRT_DOC_ALRT_FIELD_VAL,OLEConstants.EMPTY_FIELDVALUE);
113 value=true;
114 }
115 if(name || type || value){
116 return getUIFModelAndView(form);
117 }
118 if(alertDocumentFieldValueMapping.getFieldType().equalsIgnoreCase("Integer")){
119 Pattern p = Pattern.compile("^[0-9]*$");
120 Matcher m = p.matcher(alertDocumentFieldValueMapping.getFieldValue());
121 while (!m.find()) {
122 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ALRT_DOC_ALRT_FIELD_VAL, OLEConstants.INVALID_INTEGER);
123 return getUIFModelAndView(form);
124 }
125 }
126
127 if(alertDocumentFieldValueMapping.getFieldType().equalsIgnoreCase("boolean")){
128 if(!alertDocumentFieldValueMapping.getFieldValue().equalsIgnoreCase("Y") && !alertDocumentFieldValueMapping.getFieldValue().equalsIgnoreCase("N")){
129 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ALRT_DOC_ALRT_FIELD_VAL, OLEConstants.INVALID_BOOLEAN);
130 return getUIFModelAndView(form);
131 }
132 }
133
134
135 return super.addLine(form, result, request, response);
136 }
137
138 public boolean validateDocTypeName(AlertDocument alertDocument){
139 boolean result=false;
140 Map<String, String> criteria = new HashMap<String, String>();
141 List<AlertDocumentType> alertDocumentTypeList;
142 if(alertDocument.getDocumentTypeName()!=null && alertDocument.getDocumentClassName()!=null){
143 criteria.put("alertDocumentTypeName",alertDocument.getDocumentTypeName());
144 criteria.put("alertDocumentClass",alertDocument.getDocumentClassName());
145 alertDocumentTypeList = (List<AlertDocumentType>) KRADServiceLocator.getBusinessObjectService().findMatching(AlertDocumentType.class, criteria);
146 if(alertDocumentTypeList!=null && alertDocumentTypeList.size()>0){
147 GlobalVariables.getMessageMap().putError(OLEConstants.ALRT_DOC_TYPE_NAME_FIELD,"doctype.already.exist");
148 result= true;
149 }else{
150 GlobalVariables.getMessageMap().putError(OLEConstants.ALRT_DOC_TYPE_NAME_FIELD,OLEConstants.INVALID_DOCTYPENAME );
151 result= true;
152 }
153 }else if(alertDocument.getDocumentTypeName()==null && alertDocument.getDocumentClassName()!=null){
154 criteria = new HashMap<String,String>();
155 criteria.put("alertDocumentClass",alertDocument.getDocumentClassName());
156 alertDocumentTypeList = (List<AlertDocumentType>) KRADServiceLocator.getBusinessObjectService()
157 .findMatching(AlertDocumentType.class, criteria);
158 if(alertDocumentTypeList!=null && alertDocumentTypeList.size()>0){
159 GlobalVariables.getMessageMap().putError(OLEConstants.ALRT_DOC_TYPE_NAME_FIELD,"doctype.already.exist");
160 result= true;
161 }else{
162 GlobalVariables.getMessageMap().putError(OLEConstants.ALRT_DOC_TYPE_NAME_FIELD,OLEConstants.INVALID_DOCTYPENAME );
163 result= true;
164 }
165 }else if(alertDocument.getDocumentTypeName()!=null && alertDocument.getDocumentClassName()==null){
166 criteria = new HashMap<String,String>();
167 criteria.put("alertDocumentTypeName",alertDocument.getDocumentTypeName());
168 alertDocumentTypeList = (List<AlertDocumentType>) KRADServiceLocator.getBusinessObjectService()
169 .findMatching(AlertDocumentType.class, criteria);
170 if(alertDocumentTypeList!=null && alertDocumentTypeList.size()>0){
171
172 GlobalVariables.getMessageMap().putError(OLEConstants.ALRT_DOC_TYPE_NAME_FIELD,"doctype.already.exist");
173
174 result= true;
175 }else{
176 GlobalVariables.getMessageMap().putError(OLEConstants.ALRT_DOC_TYPE_NAME_FIELD,OLEConstants.INVALID_DOCTYPENAME);
177 result= true;
178 }
179 }
180
181 return result;
182 }
183
184
185 @RequestMapping(params = "methodToCall=alertCondition")
186 public ModelAndView alertCondition(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
187 HttpServletRequest request, HttpServletResponse response) {
188 boolean validateRole = false;
189 boolean validateGroup = false;
190 boolean validatePerson = false;
191
192
193 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
194 MaintenanceDocument document = (MaintenanceDocument) maintenanceForm.getDocument();
195 AlertDocument alertDocument = (AlertDocument) document.getNewMaintainableObject().getDataObject();
196
197 String selectedCollectionPath = maintenanceForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
198 CollectionGroup collectionGroup = maintenanceForm.getPostedView().getViewIndex().getCollectionGroupByPath(selectedCollectionPath);
199 String addLinePath = collectionGroup.getAddLineBindingInfo().getBindingPath();
200 Object eventObject = ObjectPropertyUtils.getPropertyValue(maintenanceForm, addLinePath);
201
202 AlertConditionAndReceiverInformation alertConditionAndReceiverInformation = (AlertConditionAndReceiverInformation) eventObject;
203 if(StringUtils.isEmpty(alertConditionAndReceiverInformation.getRoleName()) && StringUtils.isEmpty(alertConditionAndReceiverInformation.getRoleId()) && StringUtils.isEmpty(alertConditionAndReceiverInformation.getGroupName()) && StringUtils.isEmpty(alertConditionAndReceiverInformation.getGroupId()) && StringUtils.isEmpty(alertConditionAndReceiverInformation.getPrincipalName()) && StringUtils.isEmpty(alertConditionAndReceiverInformation.getPrincipalId())){
204 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ALERT_DOC_CONDITION, OLEConstants.ERROR_EMPTY_ROLE_PERSON_GROUP);
205 return getUIFModelAndView(form);
206 }
207 AlertDocumentService alertDocumentService=new AlertDocumentServiceImpl();
208 validateRole=alertDocumentService.validateRole(alertConditionAndReceiverInformation);
209 if(validateRole){
210 return getUIFModelAndView(form);
211 }
212 validateGroup=alertDocumentService.validateGroup(alertConditionAndReceiverInformation);
213 if(validateGroup){
214 return getUIFModelAndView(form);
215 }
216 validatePerson=alertDocumentService.validatePerson(alertConditionAndReceiverInformation);
217 if(validatePerson){
218 return getUIFModelAndView(form);
219 }
220
221
222
223 return super.addLine(form, result, request, response);
224 }
225
226
227 }