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.ar.document.validation.impl;
017    
018    import org.apache.log4j.Logger;
019    import org.kuali.kfs.coa.businessobject.Account;
020    import org.kuali.kfs.coa.service.AccountService;
021    import org.kuali.kfs.coa.service.ObjectTypeService;
022    import org.kuali.kfs.module.ar.ArKeyConstants;
023    import org.kuali.kfs.module.ar.ArPropertyConstants;
024    import org.kuali.kfs.module.ar.businessobject.SystemInformation;
025    import org.kuali.kfs.module.ar.document.service.SystemInformationService;
026    import org.kuali.kfs.sys.context.SpringContext;
027    import org.kuali.rice.kns.document.MaintenanceDocument;
028    import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
029    import org.kuali.rice.kns.util.ObjectUtils;
030    
031    public class SystemInformationRule extends MaintenanceDocumentRuleBase {
032        protected static Logger LOG = org.apache.log4j.Logger.getLogger(SystemInformationRule.class);
033        
034        protected ObjectTypeService objectTypeService;
035        protected AccountService accountService;
036        protected SystemInformation newSystemInformation;
037        protected SystemInformation oldSystemInformation;
038    
039        public SystemInformationRule() {
040            super();
041            // insert object type service
042            this.setObjectTypeService(SpringContext.getBean(ObjectTypeService.class));
043        }
044    
045        @Override
046        public void setupConvenienceObjects() {
047            // setup oldAccount convenience objects, make sure all possible sub-objects are populated
048            oldSystemInformation = (SystemInformation) super.getOldBo();
049    
050            // setup newAccount convenience objects, make sure all possible sub-objects are populated
051            newSystemInformation = (SystemInformation) super.getNewBo();
052        }
053    
054        /**
055         * This performs the following checks on document approve:
056         * <ul>
057         * <li>{@link SystemInformationRule#checkClearingAccountIsActive()}</li>
058         * </ul>
059         * This rule fails on rule failure
060         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomApproveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
061         */
062        @Override
063        protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
064    
065            boolean success = true;
066    
067            LOG.info("Entering processCustomApproveDocumentBusinessRules()");
068    
069            success &= checkClearingAccountIsActive();
070    //        success &= checkWireAccountIsActive();
071            success &= checkLockboxNumberIsUnique();
072    
073            return success;
074        }
075    
076        /**
077         * This performs the following checks on document route:
078         * <ul>
079         * <ul>
080         * <li>{@link SystemInformationRule#checkClearingAccountIsActive()}</li>
081         * </ul>
082         * </ul>
083         * This rule fails on rule failure
084         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
085         */
086        @Override
087        protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
088    
089            boolean success = true;
090    
091            LOG.info("Entering processCustomRouteDocumentBusinessRules()");
092    
093            success &= checkClearingAccountIsActive();
094    //        success &= checkWireAccountIsActive();
095            success &= checkLockboxNumberIsUnique();
096    
097            return success;
098        }
099    
100        /**
101         * This performs the following checks on document save:
102         * <ul>
103         * <ul>
104         * <li>{@link SystemInformationRule#checkClearingAccountIsActive()}</li>
105         * </ul>
106         * </ul>
107         * This rule does not fail on rule failure
108         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
109         */
110        @Override
111        protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
112    
113            boolean success = true;
114    
115            LOG.info("Entering processCustomSaveDocumentBusinessRules()");
116    
117            success &= checkClearingAccountIsActive();
118    //        success &= checkWireAccountIsActive();
119            success &= checkLockboxNumberIsUnique();
120    
121            //return success;
122            return true;
123            // TODO method never shows any errors even if returning false; just says 'Document was successfully saved'
124        }
125    
126         /**
127         *
128         * This checks to see if the account is active
129         * @return true if the account is active or false otherwise
130         */
131        protected boolean checkClearingAccountIsActive() {
132    
133            LOG.info("Entering checkClearingAccountIsActive()");
134            boolean success = true;
135    
136           AccountService accountService = SpringContext.getBean(AccountService.class);
137           Account clearingAccount = accountService.getByPrimaryId(newSystemInformation.getUniversityClearingChartOfAccountsCode(), newSystemInformation.getUniversityClearingAccountNumber());
138    
139            // check clearing account is not in-active
140             if (ObjectUtils.isNull(clearingAccount)) {         
141             return false;
142             }
143             if (!clearingAccount.isActive()) {
144                success &= false;
145                putGlobalError(ArKeyConstants.SystemInformation.ERROR_CLEARING_ACCOUNT_INACTIVE);
146            }
147    
148            return success;
149        }
150    
151    //    /**
152    //     * This checks to see if the account is active
153    //     *
154    //     * @return true if the account is active or false otherwise
155    //     */
156    //    protected boolean checkWireAccountIsActive() {
157    //
158    //        LOG.info("Entering checkWireAccountIsActive()");
159    //        boolean success = true;
160    //
161    //        AccountService accountService = SpringContext.getBean(AccountService.class);
162    //        Account wireAccount = accountService.getByPrimaryId(newSystemInformation.getWireChartOfAccountsCode(), newSystemInformation.getWireAccountNumber());
163    //
164    //        // check wire account is not in-active
165    //        if (ObjectUtils.isNull(wireAccount)) {
166    //            return false;
167    //        }
168    //        
169    //        if (!wireAccount.isActive()) {
170    //            success &= false;
171    //            putGlobalError(ArKeyConstants.SystemInformation.ERROR_WIRE_ACCOUNT_INACTIVE);
172    //        }
173    //
174    //        return success;
175    //    }
176        
177        /**
178        *
179        * This checks to see if the lockbox number is unique
180        * @return true if the lockbox number is active or false otherwise
181        */
182       protected boolean checkLockboxNumberIsUnique() {
183    
184           LOG.info("Entering checkLockboxNumberIsUnique()");
185           boolean success = true;
186    
187          SystemInformationService systemInformationService = SpringContext.getBean(SystemInformationService.class);
188          int recordNumber = systemInformationService.getCountByChartOrgAndLockboxNumber(newSystemInformation.getProcessingChartOfAccountCode(),
189                                                                                         newSystemInformation.getProcessingOrganizationCode(),
190                                                                                         newSystemInformation.getLockboxNumber());
191          // if not unique
192          if (recordNumber > 0) {
193              success = false;
194              putFieldError(ArPropertyConstants.SystemInformationFields.LOCKBOX_NUMBER, ArKeyConstants.SystemInformation.ERROR_LOCKBOX_NUMBER_NOT_UNIQUE);
195          }
196    
197          return success;
198       }
199    
200    /*
201        @Override
202        protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
203    
204            boolean success;
205            success = checkSalesTaxObjectValidCode(newSystemInformation);
206    
207            return success;
208        }
209    */
210    
211     /*
212        @Override
213        protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
214            // always return true even if there are business rule failures.
215            //processCustomRouteDocumentBusinessRules(document);
216            return true;
217        }
218    */
219        
220        /**
221         * 
222         * This method checks that the Sales Tax Objcet Code is of type Income
223         * Using the ParameterService to find this valid value?
224         * <ul>
225         * <li>IC</li>
226         * <li>IH</li>
227         * <li>CN</li>
228         * </ul>
229         * @return true if it is an income object
230         */
231    /*
232        protected boolean checkSalesTaxObjectValidCode(SystemInformation document) {
233            boolean success = true;
234            Integer universityFiscalYear = document.getUniversityFiscalYear();
235            ObjectCode salesTaxFinancialObject = document.getSalesTaxFinancialObject();
236            
237            if (ObjectUtils.isNotNull(universityFiscalYear) && ObjectUtils.isNotNull(salesTaxFinancialObject)) {
238                success = objectTypeService.getBasicIncomeObjectTypes(universityFiscalYear).contains(salesTaxFinancialObject.getFinancialObjectTypeCode());
239    
240                if (!success) {
241                    putFieldError("salesTaxFinancialObjectCode",ArKeyConstants.SystemInformation.SALES_TAX_OBJECT_CODE_INVALID,salesTaxFinancialObject.getCode());
242                }
243            }
244            return success;
245        }
246    */
247    
248    
249        public ObjectTypeService getObjectTypeService() {
250            return objectTypeService;
251        }
252    
253        public void setObjectTypeService(ObjectTypeService objectTypeService) {
254            this.objectTypeService = objectTypeService;
255        }
256    }