001package org.kuali.ole.describe.controller;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.describe.bo.OleAccessMethod;
005import org.kuali.rice.core.api.util.RiceKeyConstants;
006import org.kuali.rice.krad.document.Document;
007import org.kuali.rice.krad.maintenance.MaintenanceDocument;
008import org.kuali.rice.krad.service.KRADServiceLocator;
009import org.kuali.rice.krad.uif.UifConstants;
010import org.kuali.rice.krad.util.GlobalVariables;
011import org.kuali.rice.krad.web.controller.MaintenanceDocumentController;
012import org.kuali.rice.krad.web.form.DocumentFormBase;
013import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
014import org.springframework.stereotype.Controller;
015import org.springframework.validation.BindingResult;
016import org.springframework.web.bind.annotation.ModelAttribute;
017import org.springframework.web.bind.annotation.RequestMapping;
018import org.springframework.web.servlet.ModelAndView;
019
020import javax.servlet.http.HttpServletRequest;
021import javax.servlet.http.HttpServletResponse;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026/**
027 * Created with IntelliJ IDEA.
028 * User: ?
029 * Date: 1/23/13
030 * Time: 12:36 PM
031 * To change this template use File | Settings | File Templates.
032 */
033@Controller
034@RequestMapping(value = "/accessMethodMaintenance")
035public class OleAccessMethodController extends MaintenanceDocumentController {
036    @RequestMapping(params = "methodToCall=blanketApprove")
037    public ModelAndView blanketApprove(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
038                                       HttpServletRequest request, HttpServletResponse response) throws Exception {
039
040        MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
041        MaintenanceDocument maintenanceDocument = (MaintenanceDocument) form.getDocument();
042        OleAccessMethod oleAccessMethod = (OleAccessMethod) maintenanceDocument.getDocumentDataObject();
043        String accessMethodName = oleAccessMethod.getAccessMethodName();
044        OleAccessMethod oleAccessMethodOld = (OleAccessMethod) maintenanceDocument.getOldMaintainableObject().getDataObject();
045        String newAccessMethodCode = oleAccessMethodOld.getAccessMethodCode();
046        Map<String, String> accessMethodMap = new HashMap<String, String>();
047        accessMethodMap.put(OLEConstants.OleAccessMethod.ACCESS_METHOD_CD, oleAccessMethod.getAccessMethodCode());
048        List<OleAccessMethod> accessMethodInDatabase = (List<OleAccessMethod>) KRADServiceLocator.getBusinessObjectService().findMatching(OleAccessMethod.class, accessMethodMap);
049        if ("Edit".equals(maintenanceDocument.getNewMaintainableObject().getMaintenanceAction())) {
050            Document document = form.getDocument();
051            String successMessageKey = null;
052            if (oleAccessMethod.getAccessMethodCode().equals(newAccessMethodCode) || accessMethodInDatabase.size() == 0) {
053                getDocumentService().blanketApproveDocument(document, form.getAnnotation(), combineAdHocRecipients(
054                        form));
055                successMessageKey = RiceKeyConstants.MESSAGE_ROUTE_APPROVED;
056                GlobalVariables.getMessageMap().putInfo(OLEConstants.OleAccessMethod.ACCESS_METHOD_CODE, "message.route.approved");
057                return getUIFModelAndView(form);
058
059            } else {
060                if (accessMethodInDatabase.size() > 0) {
061                    for (OleAccessMethod accessMehtodObj : accessMethodInDatabase) {
062                        String accessMethodId = accessMehtodObj.getAccessMethodId().toString();
063                        //String accessMethodCode=accessMehtodObj.getAccessMethodCode();
064                        if (null == oleAccessMethod.getAccessMethodId() ||
065                                !(oleAccessMethod.getAccessMethodId().equals(accessMethodId))) {
066                            GlobalVariables.getMessageMap().putError(OLEConstants.OleAccessMethod.ACCESS_METHOD_CODE, "error.duplicate.code.access");
067                            return getUIFModelAndView(form);
068                        }
069                        getDocumentService().blanketApproveDocument(document, form.getAnnotation(), combineAdHocRecipients(
070                                form));
071                        successMessageKey = RiceKeyConstants.MESSAGE_ROUTE_APPROVED;
072                        GlobalVariables.getMessageMap().putInfo(OLEConstants.OleAccessMethod.ACCESS_METHOD_CODE, "message.route.approved");
073                        return getUIFModelAndView(form);
074
075                    }
076                }
077
078            }
079
080        } else if ("Copy".equals(maintenanceDocument.getNewMaintainableObject().getMaintenanceAction())) {
081            String successMessageKey = null;
082            Document document = form.getDocument();
083            if (accessMethodInDatabase.size() == 0) {
084                getDocumentService().blanketApproveDocument(document, form.getAnnotation(), combineAdHocRecipients(
085                        form));
086                successMessageKey = RiceKeyConstants.MESSAGE_ROUTE_APPROVED;
087                GlobalVariables.getMessageMap().putInfo(OLEConstants.OleAccessMethod.ACCESS_METHOD_CODE, "message.route.approved");
088                return getUIFModelAndView(form);
089            } else if ((accessMethodInDatabase.size() > 0)) {
090
091                for (OleAccessMethod accessMehtodObj : accessMethodInDatabase) {
092                    String accessMethodId = accessMehtodObj.getAccessMethodId().toString();
093                    if (null == oleAccessMethod.getAccessMethodId() ||
094                            !(oleAccessMethod.getAccessMethodId().equals(accessMethodId))) {
095                        GlobalVariables.getMessageMap().putError(OLEConstants.OleAccessMethod.ACCESS_METHOD_CODE, "error.duplicate.code.access");
096                        return getUIFModelAndView(form);
097                    }
098                }
099
100            }
101        } else if ("New".equals(maintenanceDocument.getNewMaintainableObject().getMaintenanceAction())) {
102            String successMessageKey = null;
103            Document document = form.getDocument();
104            if (accessMethodInDatabase.size() == 0) {
105                getDocumentService().blanketApproveDocument(document, form.getAnnotation(), combineAdHocRecipients(
106                        form));
107                successMessageKey = RiceKeyConstants.MESSAGE_ROUTE_APPROVED;
108                GlobalVariables.getMessageMap().putInfo(OLEConstants.OleAccessMethod.ACCESS_METHOD_CODE, "message.route.approved");
109                return getUIFModelAndView(form);
110            } else if ((accessMethodInDatabase.size() > 0)) {
111
112                for (OleAccessMethod accessMehtodObj : accessMethodInDatabase) {
113                    String accessMethodId = accessMehtodObj.getAccessMethodId().toString();
114                    if (null == oleAccessMethod.getAccessMethodId() ||
115                            !(oleAccessMethod.getAccessMethodId().equals(accessMethodId))) {
116                        GlobalVariables.getMessageMap().putError(OLEConstants.OleAccessMethod.ACCESS_METHOD_CODE, "error.duplicate.code.access");
117                        return getUIFModelAndView(form);
118                    }
119                }
120
121            }
122        }
123
124        /*else if((accessMethodInDatabase.size() > 0)) {
125
126            for(OleAccessMethod accessMehtodObj : accessMethodInDatabase){
127                String accessMethodId =  accessMehtodObj.getAccessMethodId().toString();
128                if (null == oleAccessMethod.getAccessMethodId()  ||
129                        !(oleAccessMethod.getAccessMethodId().equals(accessMethodId))) {
130                    GlobalVariables.getMessageMap().putError(OLEConstants.OleAccessMethod.ACCESS_METHOD_CODE, "error.duplicate.code");
131                    return getUIFModelAndView(form);
132                }
133            }
134
135        }*/
136        performWorkflowAction(form, UifConstants.WorkflowAction.BLANKETAPPROVE, true);
137        return returnToPrevious(form);
138    }
139}