View Javadoc
1   /*
2    * Copyright 2007-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.PurapKeyConstants;
19  import org.kuali.ole.module.purap.businessobject.VendorStipulation;
20  import org.kuali.rice.kns.document.MaintenanceDocument;
21  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
22  import org.kuali.rice.krad.service.BusinessObjectService;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  /**
28   * Business rule(s) applicable to Purchase Order Contract Language maintenance document.
29   */
30  public class VendorStipulationRule extends MaintenanceDocumentRuleBase {
31  
32      private VendorStipulation newStipulation;
33      private VendorStipulation oldStipulation;
34      private BusinessObjectService boService;
35  
36      /**
37       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#setupConvenienceObjects()
38       */
39      @Override
40      public void setupConvenienceObjects() {
41          // setup newDelegateChange convenience objects, make sure all possible sub-objects are populated
42          newStipulation = (VendorStipulation) super.getNewBo();
43          oldStipulation = (VendorStipulation) super.getOldBo();
44          boService = (BusinessObjectService) super.getBoService();
45          super.setupConvenienceObjects();
46      }
47  
48      /**
49       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomApproveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
50       */
51      protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
52          LOG.info("processCustomApproveDocumentBusinessRules called");
53          this.setupConvenienceObjects();
54          boolean success = this.checkForDuplicate();
55          return success && super.processCustomApproveDocumentBusinessRules(document);
56      }
57  
58      /**
59       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
60       */
61      protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
62          LOG.info("processCustomRouteDocumentBusinessRules called");
63          this.setupConvenienceObjects();
64          boolean success = this.checkForDuplicate();
65          return success && super.processCustomRouteDocumentBusinessRules(document);
66      }
67  
68      /**
69       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
70       */
71      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
72          LOG.info("processCustomSaveDocumentBusinessRules called");
73          this.setupConvenienceObjects();
74          boolean success = this.checkForDuplicate();
75          return success && super.processCustomSaveDocumentBusinessRules(document);
76      }
77  
78      /**
79       * Check to see if data duplicates existing data.
80       *
81       * @return boolean indicating if validation succeeded.
82       */
83      protected boolean checkForDuplicate() {
84          LOG.info("checkForDuplicate called");
85          boolean success = true;
86          Map fieldValues = new HashMap();
87  
88          if (oldStipulation.getVendorStipulationIdentifier() != null && newStipulation.getVendorStipulationIdentifier() != null &&
89                  oldStipulation.getVendorStipulationIdentifier().equals(newStipulation.getVendorStipulationIdentifier()) &&
90                  oldStipulation.getVendorStipulationName().equals(newStipulation.getVendorStipulationName())) {
91              return true;
92          } else {
93              fieldValues.put("vendorStipulationName", newStipulation.getVendorStipulationName());
94              if (boService.countMatching(newStipulation.getClass(), fieldValues) != 0) {
95                  success &= false;
96                  putGlobalError(PurapKeyConstants.PURAP_GENERAL_POTENTIAL_DUPLICATE);
97              }
98          }
99  
100         return success;
101     }
102 }