001/*
002 * Copyright 2006-2008 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.module.purap.document.validation.impl;
017
018import org.kuali.ole.module.purap.PurapConstants;
019import org.kuali.ole.module.purap.PurapKeyConstants;
020import org.kuali.ole.module.purap.PurapPropertyConstants;
021import org.kuali.ole.module.purap.businessobject.ContractManagerAssignmentDetail;
022import org.kuali.ole.module.purap.document.ContractManagerAssignmentDocument;
023import org.kuali.ole.sys.context.SpringContext;
024import org.kuali.ole.vnd.businessobject.ContractManager;
025import org.kuali.rice.kns.rules.TransactionalDocumentRuleBase;
026import org.kuali.rice.krad.document.Document;
027import org.kuali.rice.krad.rules.rule.event.ApproveDocumentEvent;
028import org.kuali.rice.krad.service.BusinessObjectService;
029import org.kuali.rice.krad.util.GlobalVariables;
030import org.kuali.rice.krad.util.KRADPropertyConstants;
031import org.kuali.rice.krad.util.ObjectUtils;
032
033import java.util.HashMap;
034import java.util.Iterator;
035import java.util.List;
036import java.util.Map;
037
038/**
039 * Business rule(s) applicable to Contract Manager Assignment document.
040 */
041public class ContractManagerAssignmentDocumentRule extends TransactionalDocumentRuleBase {
042    protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ContractManagerAssignmentDocumentRule.class);
043
044    /**
045     * @see org.kuali.rice.krad.rules.TransactionalDocumentRuleBase#processCustomRouteDocumentBusinessRules(Document)
046     */
047    @Override
048    protected boolean processCustomRouteDocumentBusinessRules(Document document) {
049        boolean isValid = true;
050        ContractManagerAssignmentDocument acmDocument = (ContractManagerAssignmentDocument) document;
051        return isValid &= processValidation(acmDocument);
052    }
053
054    /**
055     * @see org.kuali.rice.krad.rules.DocumentRuleBase#processCustomApproveDocumentBusinessRules(org.kuali.rice.krad.rule.event.ApproveDocumentEvent)
056     */
057    @Override
058    protected boolean processCustomApproveDocumentBusinessRules(ApproveDocumentEvent approveEvent) {
059        boolean isValid = true;
060        ContractManagerAssignmentDocument acmDocument = (ContractManagerAssignmentDocument) approveEvent.getDocument();
061        // isValid &= processValidation(acmDocument);
062        return isValid;
063    }
064
065    /**
066     * Perform validation for Contract Manager Assignment document such as validating contract manager codes.
067     *
068     * @param document Contract Manager Assignment document
069     * @return Boolean indicating if validation succeeded
070     */
071    protected boolean processValidation(ContractManagerAssignmentDocument document) {
072        return validateContractManagerCodes(document.getContractManagerAssignmentDetails());
073    }
074
075    /**
076     * Review the list of ContractManagerAssignmentDetails where the user has entered ContractManagerCodes,
077     * validates that each entered code is valid;
078     * on the other hand, validate that at least one row has a valid CM code assigned.
079     *
080     * @param contractManagerAssignmentDetails
081     *         A list containing the code to be validated.
082     * @return Boolean indicating if validation succeeded
083     */
084    public boolean validateContractManagerCodes(List contractManagerAssignmentDetails) {
085        LOG.debug("validateContractManagerCodes(): entered method.");
086        boolean isValid = true;
087        int count = 0;
088
089        for (Iterator iter = contractManagerAssignmentDetails.iterator(); iter.hasNext(); ) {
090            ContractManagerAssignmentDetail detail = (ContractManagerAssignmentDetail) iter.next();
091
092            // Look for the contractManagerCode in the table. If not there the code is invalid.
093            if (ObjectUtils.isNotNull(detail.getContractManagerCode())) {
094                Map fieldValues = new HashMap();
095                fieldValues.put(PurapPropertyConstants.CONTRACT_MANAGER_CODE, detail.getContractManagerCode());
096                fieldValues.put(KRADPropertyConstants.ACTIVE, true);
097                if (SpringContext.getBean(BusinessObjectService.class).countMatching(ContractManager.class, fieldValues) != 1) {
098                    GlobalVariables.getMessageMap().putError(PurapConstants.ASSIGN_CONTRACT_MANAGER_TAB_ERRORS, PurapKeyConstants.INVALID_CONTRACT_MANAGER_CODE, detail.getContractManagerCode().toString());
099                    isValid = false;
100                } else {
101                    count++;
102                }
103                if (detail.getContractManagerCode().equals(PurapConstants.APO_CONTRACT_MANAGER)) {
104                    GlobalVariables.getMessageMap().putError(PurapConstants.ASSIGN_CONTRACT_MANAGER_TAB_ERRORS, PurapKeyConstants.ERROR_APO_CONTRACT_MANAGER_CODE_CHOSEN, detail.getContractManagerCode().toString());
105                    isValid = false;
106                }
107            }
108        }
109
110        // check if at least one row has a valid CM code assigned
111        if (count < 1) {
112            GlobalVariables.getMessageMap().putError(PurapConstants.ASSIGN_CONTRACT_MANAGER_TAB_ERRORS, PurapKeyConstants.NO_CONTRACT_MANAGER_ASSIGNED);
113            isValid = false;
114        }
115
116        LOG.debug("validateContractManagerCodes(): leaving method.");
117        return isValid;
118    }
119}