001package org.kuali.ole.describe.controller;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.describe.bo.OleCountryCodes;
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/24/13
030 * Time: 5:40 PM
031 * To change this template use File | Settings | File Templates.
032 */
033@Controller
034@RequestMapping(value = "/countryCodesMaintenance")
035public class OleCountryCodesController 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        MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
040        MaintenanceDocument maintenanceDocument = (MaintenanceDocument) form.getDocument();
041        OleCountryCodes oleCountryCodes = (OleCountryCodes) maintenanceDocument.getDocumentDataObject();
042        OleCountryCodes oleCountryCodesOld = (OleCountryCodes) maintenanceDocument.getOldMaintainableObject().getDataObject();
043        String oleCountryCodeOldCode = oleCountryCodesOld.getCountryCode();
044        Map<String, String> countryCodesMap = new HashMap<String, String>();
045        countryCodesMap.put(OLEConstants.OleCountryCodes.COUNTRY_CD, oleCountryCodes.getCountryCode());
046        List<OleCountryCodes> countryCodesInDatabase = (List<OleCountryCodes>) KRADServiceLocator.getBusinessObjectService().findMatching(OleCountryCodes.class, countryCodesMap);
047        if ("Edit".equals(maintenanceDocument.getNewMaintainableObject().getMaintenanceAction())) {
048            Document document = form.getDocument();
049            String successMessageKey = null;
050            if (oleCountryCodes.getCountryCode().equals(oleCountryCodeOldCode) || countryCodesInDatabase.size() == 0) {
051                getDocumentService().blanketApproveDocument(document, form.getAnnotation(), combineAdHocRecipients(
052                        form));
053                successMessageKey = RiceKeyConstants.MESSAGE_ROUTE_APPROVED;
054                GlobalVariables.getMessageMap().putInfo(OLEConstants.OleCountryCodes.COUNTRY_CODE, "message.route.approved");
055                return getUIFModelAndView(form);
056            } else {
057                if (countryCodesInDatabase.size() > 0) {
058                    for (OleCountryCodes countryCodesObj : countryCodesInDatabase) {
059                        String countryCodeTypeId = countryCodesObj.getCountryCodeId().toString();
060                        String countryCodeCode = countryCodesObj.getCountryCode();
061                        if (null == oleCountryCodes.getCountryCodeId() ||
062                                !(oleCountryCodes.getCountryCodeId().equals(countryCodeTypeId))) {
063                            GlobalVariables.getMessageMap().putError(OLEConstants.OleCountryCodes.COUNTRY_CODE, "error.duplicate.code.country");
064                            return getUIFModelAndView(form);
065                        }
066                        getDocumentService().blanketApproveDocument(document, form.getAnnotation(), combineAdHocRecipients(
067                                form));
068                        successMessageKey = RiceKeyConstants.MESSAGE_ROUTE_APPROVED;
069                        GlobalVariables.getMessageMap().putInfo(OLEConstants.OleCountryCodes.COUNTRY_CODE, "message.route.approved");
070                        return getUIFModelAndView(form);
071
072
073                    }
074                }
075            }
076        } else if ("Copy".equals(maintenanceDocument.getNewMaintainableObject().getMaintenanceAction())) {
077            String successMessageKey = null;
078            Document document = form.getDocument();
079            if (countryCodesInDatabase.size() == 0) {
080                getDocumentService().blanketApproveDocument(document, form.getAnnotation(), combineAdHocRecipients(
081                        form));
082                successMessageKey = RiceKeyConstants.MESSAGE_ROUTE_APPROVED;
083                GlobalVariables.getMessageMap().putInfo(OLEConstants.OleCountryCodes.COUNTRY_CODE, "message.route.approved");
084                return getUIFModelAndView(form);
085            } else if ((countryCodesInDatabase.size() > 0)) {
086                for (OleCountryCodes countryCodesObj : countryCodesInDatabase) {
087                    String instanceItemTypeId = countryCodesObj.getCountryCodeId().toString();
088                    if (null == oleCountryCodes.getCountryCodeId() ||
089                            !(oleCountryCodes.getCountryCodeId().equals(instanceItemTypeId))) {
090                        GlobalVariables.getMessageMap().putError(OLEConstants.OleCountryCodes.COUNTRY_CODE, "error.duplicate.code.country");
091                        return getUIFModelAndView(form);
092                    }
093                }
094            }
095
096        } else if ("New".equals(maintenanceDocument.getNewMaintainableObject().getMaintenanceAction())) {
097            String successMessageKey = null;
098            Document document = form.getDocument();
099            if ((countryCodesInDatabase.size() == 0)) {
100                getDocumentService().blanketApproveDocument(document, form.getAnnotation(), combineAdHocRecipients(
101                        form));
102                successMessageKey = RiceKeyConstants.MESSAGE_ROUTE_APPROVED;
103                GlobalVariables.getMessageMap().putInfo(OLEConstants.OleCountryCodes.COUNTRY_CODE, "message.route.approved");
104                return getUIFModelAndView(form);
105            } else if ((countryCodesInDatabase.size() > 0)) {
106                for (OleCountryCodes countryCodesObj : countryCodesInDatabase) {
107                    String instanceItemTypeId = countryCodesObj.getCountryCodeId().toString();
108                    if (null == oleCountryCodes.getCountryCodeId() ||
109                            !(oleCountryCodes.getCountryCodeId().equals(instanceItemTypeId))) {
110                        GlobalVariables.getMessageMap().putError(OLEConstants.OleCountryCodes.COUNTRY_CODE, "error.duplicate.code.country");
111                        return getUIFModelAndView(form);
112                    }
113                }
114            }
115        }
116
117        /*else if((countryCodesInDatabase.size() > 0)) {
118
119            for(OleCountryCodes countryCodesObj : countryCodesInDatabase){
120                String instanceItemTypeId =  countryCodesObj.getCountryCodeId().toString();
121                if (null == oleCountryCodes.getCountryCodeId()  ||
122                        !(oleCountryCodes.getCountryCodeId().equals(instanceItemTypeId))) {
123                    GlobalVariables.getMessageMap().putError(OLEConstants.OleCountryCodes.COUNTRY_CODE, "error.duplicate.code");
124                    return getUIFModelAndView(form);
125                }
126            }
127
128        }*/
129        performWorkflowAction(form, UifConstants.WorkflowAction.BLANKETAPPROVE, true);
130        return returnToPrevious(form);
131    }
132}
133