001 /*
002 * Copyright 2011 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 */
016 package org.kuali.kfs.module.purap.document.validation.impl;
017
018 import java.util.HashMap;
019 import java.util.Iterator;
020 import java.util.List;
021 import java.util.Map;
022
023 import org.kuali.kfs.module.purap.PurapConstants;
024 import org.kuali.kfs.module.purap.PurapKeyConstants;
025 import org.kuali.kfs.module.purap.PurapPropertyConstants;
026 import org.kuali.kfs.module.purap.businessobject.ContractManagerAssignmentDetail;
027 import org.kuali.kfs.module.purap.document.ContractManagerAssignmentDocument;
028 import org.kuali.kfs.sys.context.SpringContext;
029 import org.kuali.kfs.vnd.businessobject.ContractManager;
030 import org.kuali.rice.kns.document.Document;
031 import org.kuali.rice.kns.rule.event.ApproveDocumentEvent;
032 import org.kuali.rice.kns.rules.TransactionalDocumentRuleBase;
033 import org.kuali.rice.kns.service.BusinessObjectService;
034 import org.kuali.rice.kns.util.GlobalVariables;
035 import org.kuali.rice.kns.util.KNSPropertyConstants;
036 import org.kuali.rice.kns.util.ObjectUtils;
037
038 /**
039 * Business rule(s) applicable to Contract Manager Assignment document.
040 */
041 public 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.kns.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.kns.rules.DocumentRuleBase#processCustomApproveDocumentBusinessRules(org.kuali.rice.kns.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 A list containing the code to be validated.
081 * @return Boolean indicating if validation succeeded
082 */
083 public boolean validateContractManagerCodes(List contractManagerAssignmentDetails) {
084 LOG.debug("validateContractManagerCodes(): entered method.");
085 boolean isValid = true;
086 int count = 0;
087
088 for (Iterator iter = contractManagerAssignmentDetails.iterator(); iter.hasNext();) {
089 ContractManagerAssignmentDetail detail = (ContractManagerAssignmentDetail) iter.next();
090
091 // Look for the contractManagerCode in the table. If not there the code is invalid.
092 if (ObjectUtils.isNotNull(detail.getContractManagerCode())) {
093 Map fieldValues = new HashMap();
094 fieldValues.put(PurapPropertyConstants.CONTRACT_MANAGER_CODE, detail.getContractManagerCode());
095 fieldValues.put(KNSPropertyConstants.ACTIVE, true);
096 if (SpringContext.getBean(BusinessObjectService.class).countMatching(ContractManager.class, fieldValues) != 1) {
097 GlobalVariables.getMessageMap().putError(PurapConstants.ASSIGN_CONTRACT_MANAGER_TAB_ERRORS, PurapKeyConstants.INVALID_CONTRACT_MANAGER_CODE, detail.getContractManagerCode().toString());
098 isValid = false;
099 }
100 else count++;
101 if (detail.getContractManagerCode().equals(PurapConstants.APO_CONTRACT_MANAGER)) {
102 GlobalVariables.getMessageMap().putError(PurapConstants.ASSIGN_CONTRACT_MANAGER_TAB_ERRORS, PurapKeyConstants.ERROR_APO_CONTRACT_MANAGER_CODE_CHOSEN, detail.getContractManagerCode().toString());
103 isValid = false;
104 }
105 }
106 }
107
108 // check if at least one row has a valid CM code assigned
109 if (count < 1) {
110 GlobalVariables.getMessageMap().putError(PurapConstants.ASSIGN_CONTRACT_MANAGER_TAB_ERRORS, PurapKeyConstants.NO_CONTRACT_MANAGER_ASSIGNED);
111 isValid = false;
112 }
113
114 LOG.debug("validateContractManagerCodes(): leaving method.");
115 return isValid;
116 }
117 }