View Javadoc
1   /*
2    * Copyright 2006-2008 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.purap.document.validation.impl;
17  
18  import org.kuali.ole.module.purap.PurapConstants;
19  import org.kuali.ole.module.purap.PurapKeyConstants;
20  import org.kuali.ole.module.purap.PurapPropertyConstants;
21  import org.kuali.ole.module.purap.businessobject.ContractManagerAssignmentDetail;
22  import org.kuali.ole.module.purap.document.ContractManagerAssignmentDocument;
23  import org.kuali.ole.sys.context.SpringContext;
24  import org.kuali.ole.vnd.businessobject.ContractManager;
25  import org.kuali.rice.kns.rules.TransactionalDocumentRuleBase;
26  import org.kuali.rice.krad.document.Document;
27  import org.kuali.rice.krad.rules.rule.event.ApproveDocumentEvent;
28  import org.kuali.rice.krad.service.BusinessObjectService;
29  import org.kuali.rice.krad.util.GlobalVariables;
30  import org.kuali.rice.krad.util.KRADPropertyConstants;
31  import org.kuali.rice.krad.util.ObjectUtils;
32  
33  import java.util.HashMap;
34  import java.util.Iterator;
35  import java.util.List;
36  import java.util.Map;
37  
38  /**
39   * Business rule(s) applicable to Contract Manager Assignment document.
40   */
41  public class ContractManagerAssignmentDocumentRule extends TransactionalDocumentRuleBase {
42      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ContractManagerAssignmentDocumentRule.class);
43  
44      /**
45       * @see org.kuali.rice.krad.rules.TransactionalDocumentRuleBase#processCustomRouteDocumentBusinessRules(Document)
46       */
47      @Override
48      protected boolean processCustomRouteDocumentBusinessRules(Document document) {
49          boolean isValid = true;
50          ContractManagerAssignmentDocument acmDocument = (ContractManagerAssignmentDocument) document;
51          return isValid &= processValidation(acmDocument);
52      }
53  
54      /**
55       * @see org.kuali.rice.krad.rules.DocumentRuleBase#processCustomApproveDocumentBusinessRules(org.kuali.rice.krad.rule.event.ApproveDocumentEvent)
56       */
57      @Override
58      protected boolean processCustomApproveDocumentBusinessRules(ApproveDocumentEvent approveEvent) {
59          boolean isValid = true;
60          ContractManagerAssignmentDocument acmDocument = (ContractManagerAssignmentDocument) approveEvent.getDocument();
61          // isValid &= processValidation(acmDocument);
62          return isValid;
63      }
64  
65      /**
66       * Perform validation for Contract Manager Assignment document such as validating contract manager codes.
67       *
68       * @param document Contract Manager Assignment document
69       * @return Boolean indicating if validation succeeded
70       */
71      protected boolean processValidation(ContractManagerAssignmentDocument document) {
72          return validateContractManagerCodes(document.getContractManagerAssignmentDetails());
73      }
74  
75      /**
76       * Review the list of ContractManagerAssignmentDetails where the user has entered ContractManagerCodes,
77       * validates that each entered code is valid;
78       * on the other hand, validate that at least one row has a valid CM code assigned.
79       *
80       * @param contractManagerAssignmentDetails
81       *         A list containing the code to be validated.
82       * @return Boolean indicating if validation succeeded
83       */
84      public boolean validateContractManagerCodes(List contractManagerAssignmentDetails) {
85          LOG.debug("validateContractManagerCodes(): entered method.");
86          boolean isValid = true;
87          int count = 0;
88  
89          for (Iterator iter = contractManagerAssignmentDetails.iterator(); iter.hasNext(); ) {
90              ContractManagerAssignmentDetail detail = (ContractManagerAssignmentDetail) iter.next();
91  
92              // Look for the contractManagerCode in the table. If not there the code is invalid.
93              if (ObjectUtils.isNotNull(detail.getContractManagerCode())) {
94                  Map fieldValues = new HashMap();
95                  fieldValues.put(PurapPropertyConstants.CONTRACT_MANAGER_CODE, detail.getContractManagerCode());
96                  fieldValues.put(KRADPropertyConstants.ACTIVE, true);
97                  if (SpringContext.getBean(BusinessObjectService.class).countMatching(ContractManager.class, fieldValues) != 1) {
98                      GlobalVariables.getMessageMap().putError(PurapConstants.ASSIGN_CONTRACT_MANAGER_TAB_ERRORS, PurapKeyConstants.INVALID_CONTRACT_MANAGER_CODE, detail.getContractManagerCode().toString());
99                      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 }