View Javadoc
1   /*
2    * Copyright 2006 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.coa.document.validation.impl;
17  
18  import org.kuali.ole.coa.businessobject.Account;
19  import org.kuali.ole.coa.businessobject.SubObjectCode;
20  import org.kuali.ole.sys.OLEKeyConstants;
21  import org.kuali.rice.kns.document.MaintenanceDocument;
22  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
23  import org.kuali.rice.krad.util.ObjectUtils;
24  
25  public class SubObjCdRule extends MaintenanceDocumentRuleBase {
26  
27      protected static final String ACCOUNT_ORG_RULE_KEY = "SubObjectCode.AccountOrgsAllowingClosedAccounts";
28  
29      protected SubObjectCode oldSubObjectCode;
30      protected SubObjectCode newSubObjectCode;
31  
32      public SubObjCdRule() {
33          super();
34      }
35  
36      /**
37       * This performs rules checks on document approve
38       * <ul>
39       * <li>{@link SubObjCdRule#checkExistenceAndActive()}</li>
40       * </ul>
41       * This rule fails on business rule failures
42       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomApproveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
43       */
44      protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
45  
46          LOG.debug("Entering processCustomApproveDocumentBusinessRules()");
47  
48          // check that all sub-objects whose keys are specified have matching objects in the db
49          checkExistenceAndActive();
50  
51          return true;
52      }
53  
54      /**
55       * This performs rules checks on document route
56       * <ul>
57       * <li>{@link SubObjCdRule#checkExistenceAndActive()}</li>
58       * </ul>
59       * This rule fails on business rule failures
60       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
61       */
62      protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
63  
64          boolean success = true;
65  
66          LOG.debug("Entering processCustomRouteDocumentBusinessRules()");
67  
68          // check that all sub-objects whose keys are specified have matching objects in the db
69          success &= checkExistenceAndActive();
70  
71          return success;
72      }
73  
74      /**
75       * This performs rules checks on document save
76       * <ul>
77       * <li>{@link SubObjCdRule#checkExistenceAndActive()}</li>
78       * </ul>
79       * This rule does not fail on business rule failures
80       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
81       */
82      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
83  
84          boolean success = true;
85  
86          LOG.debug("Entering processCustomSaveDocumentBusinessRules()");
87  
88          // check that all sub-objects whose keys are specified have matching objects in the db
89          success &= checkExistenceAndActive();
90  
91          return success;
92      }
93  
94      /**
95       * This method sets the convenience objects like newSubObjectCode and oldSubObjectCode, so you have short and easy handles to the new and
96       * old objects contained in the maintenance document. It also calls the BusinessObjectBase.refresh(), which will attempt to load
97       * all sub-objects from the DB by their primary keys, if available.
98       * 
99       * @param document - the maintenanceDocument being evaluated
100      */
101     public void setupConvenienceObjects() {
102 
103         // setup oldAccount convenience objects, make sure all possible sub-objects are populated
104         oldSubObjectCode = (SubObjectCode) super.getOldBo();
105 
106         // setup newAccount convenience objects, make sure all possible sub-objects are populated
107         newSubObjectCode = (SubObjectCode) super.getNewBo();
108     }
109 
110     /**
111      * 
112      * This checks that the account on the sub object code is not closed
113      * @return false if the account is closed
114      */
115     protected boolean checkExistenceAndActive() {
116 
117         LOG.debug("Entering checkExistenceAndActive()");
118         boolean success = true;
119 
120         // disallow closed accounts unless in certain orgs
121         if (ObjectUtils.isNotNull(newSubObjectCode.getAccount())) {
122             Account account = newSubObjectCode.getAccount();
123 
124             // if the account is closed
125             if (!account.isActive()) {
126                 putFieldError("accountNumber", OLEKeyConstants.ERROR_DOCUMENT_SUBOBJECTMAINT_ACCOUNT_MAY_NOT_BE_CLOSED);
127                 success &= false;
128             }
129         }
130         return success;
131     }
132 
133 }