View Javadoc
1   /*
2    * Copyright 2006-2007 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.businessobject.PurchaseOrderSensitiveData;
19  import org.kuali.ole.module.purap.businessobject.SensitiveData;
20  import org.kuali.ole.sys.OLEKeyConstants;
21  import org.kuali.ole.sys.context.SpringContext;
22  import org.kuali.ole.vnd.businessobject.CommodityCode;
23  import org.kuali.rice.kns.document.MaintenanceDocument;
24  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
25  import org.kuali.rice.krad.service.BusinessObjectService;
26  
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  /**
31   * Validates the SensitiveData maintenance document.
32   */
33  public class SensitiveDataRule extends MaintenanceDocumentRuleBase {
34  
35      /**
36       * This method performs custom route business rule checks on the document being routed.  The rules include confirming the
37       * validity of the work group.
38       *
39       * @param document The document being routed.
40       * @return True if all the business rules pass, false otherwise.
41       * @see MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
42       */
43      @Override
44      protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
45          boolean valid = true;
46          valid &= validateInactivationBlocking();
47          return valid;
48      }
49  
50      protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
51          boolean valid = true;
52          valid &= validateInactivationBlocking();
53          return valid;
54      }
55  
56      protected boolean validateInactivationBlocking() {
57          SensitiveData oldSensitiveData = (SensitiveData) getOldBo();
58          SensitiveData newSensitiveData = (SensitiveData) getNewBo();
59          if (oldSensitiveData.isActive() && !newSensitiveData.isActive()) {
60              if (hasABlockingRecord(newSensitiveData.getSensitiveDataCode())) {
61                  String documentLabel = "SensitiveData"; //SpringContext.getBean(DataDictionaryService.class).getDocumentLabelByClass(newSensitiveData.getClass());
62                  putGlobalError(OLEKeyConstants.ERROR_CANNOT_INACTIVATE_USED_BY_ACTIVE_RECORDS, documentLabel);
63                  return false;
64              }
65          }
66          return true;
67      }
68  
69      protected boolean hasABlockingRecord(String sensitiveDataCode) {
70          Map<String, Object> queryMap = new HashMap<String, Object>();
71          queryMap.put("sensitiveDataCode", sensitiveDataCode);
72  
73          //Check whether there are any PurchaseOrderSensitiveData whose sensitiveDataCode match with this SensitiveData's code
74          boolean hasPurchaseOrderSensitiveDataBlockingRecord = SpringContext.getBean(BusinessObjectService.class).countMatching(PurchaseOrderSensitiveData.class, queryMap) > 0;
75  
76          queryMap.put("active", true);
77          //Check whether there are any active CommodityCode whose sensitiveDataCode match with this SensitiveData's code
78          boolean hasCommodityCodeBlockingRecord = SpringContext.getBean(BusinessObjectService.class).countMatching(CommodityCode.class, queryMap) > 0;
79  
80          return hasPurchaseOrderSensitiveDataBlockingRecord || hasCommodityCodeBlockingRecord;
81      }
82  
83  }