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.endow.document.validation.impl;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.kuali.kfs.coa.businessobject.ObjectCode;
023    import org.kuali.kfs.module.endow.EndowConstants;
024    import org.kuali.kfs.module.endow.EndowKeyConstants;
025    import org.kuali.kfs.module.endow.EndowParameterKeyConstants;
026    import org.kuali.kfs.module.endow.EndowPropertyConstants;
027    import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionCode;
028    import org.kuali.kfs.module.endow.businessobject.GLLink;
029    import org.kuali.kfs.sys.KFSConstants;
030    import org.kuali.kfs.sys.context.SpringContext;
031    import org.kuali.rice.kns.bo.PersistableBusinessObject;
032    import org.kuali.rice.kns.document.MaintenanceDocument;
033    import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
034    import org.kuali.rice.kns.service.ParameterService;
035    import org.kuali.rice.kns.util.GlobalVariables;
036    import org.kuali.rice.kns.util.MessageMap;
037    import org.kuali.rice.kns.util.ObjectUtils;
038    
039    public class EndowmentTransactionCodeRule extends MaintenanceDocumentRuleBase {
040    
041    
042        /**
043         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
044         */
045        @Override
046        protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
047            boolean isValid = true;
048            isValid &= super.processCustomRouteDocumentBusinessRules(document);
049            MessageMap errorMap = GlobalVariables.getMessageMap();
050            isValid &= errorMap.hasNoErrors();
051    
052            if (isValid) {
053                isValid &= checkGLLinks(document);
054            }
055    
056            return isValid;
057        }
058    
059        /**
060         * Checks that glLinks is not empty. Each Etran Code must have at least one associated record in END_ETRAN_GL_LNK_T.
061         * 
062         * @param document maintenance document
063         * @return true if valid, false otherwise
064         */
065        private boolean checkGLLinks(MaintenanceDocument document) {
066            boolean isValid = true;
067            EndowmentTransactionCode endowmentTransactionCode = (EndowmentTransactionCode) document.getNewMaintainableObject().getBusinessObject();
068            List<GLLink> activeGLLinks = new ArrayList<GLLink>();
069    
070            int i = 0;
071            for (GLLink link : endowmentTransactionCode.getGlLinks()) {
072    
073                // check that ObjectCode.ObjectTypeCode.BasicAccountingCategory == EndowmentTransactionCode.TransactionTypeCode
074                link.refreshReferenceObject(EndowPropertyConstants.GL_LINK_FINANCIAL_OBJECT_CODE);
075    
076                if (!validateGLLinkObjectCode(endowmentTransactionCode, link)) {
077                    isValid = false;
078                    putFieldError(EndowPropertyConstants.ENDOWMENT_TRANSACTION_GL_LINKS + "[" + i + "]" + "." + EndowPropertyConstants.GL_LINK_OBJECT_CD, EndowKeyConstants.EndowmentTransactionConstants.ERROR_GL_LINK_OBJ_CD_ACC_CATEGORY_MUST_EQUAL_ETRAN_TYPE);
079                    break;
080                }
081    
082                // add it to the active glLinks list
083                if (link.isActive()) {
084                    activeGLLinks.add(link);
085                }
086                i++;
087            }
088    
089            // check if the endowment transaction code has at least one active GL Link
090            if (isValid) {
091                if (activeGLLinks.size() == 0) {
092                    putFieldError(EndowPropertyConstants.ENDOWMENT_TRANSACTION_GL_LINKS, EndowKeyConstants.EndowmentTransactionConstants.ERROR_ENDOWMENT_TRANSACTION_MUST_HAVE_AT_LEAST_ONE_GLLINK);
093                    isValid = false;
094                }
095            }
096    
097            return isValid;
098        }
099    
100        /**
101         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomAddCollectionLineBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument,
102         *      java.lang.String, org.kuali.rice.kns.bo.PersistableBusinessObject)
103         */
104        @Override
105        public boolean processCustomAddCollectionLineBusinessRules(MaintenanceDocument document, String collectionName, PersistableBusinessObject bo) {
106            boolean isValid = true;
107            isValid &= super.processCustomAddCollectionLineBusinessRules(document, collectionName, bo);
108            MessageMap errorMap = GlobalVariables.getMessageMap();
109            isValid &= errorMap.hasNoErrors();
110            EndowmentTransactionCode endowmentTransactionCode = (EndowmentTransactionCode) document.getNewMaintainableObject().getBusinessObject();
111            GLLink glLink = (GLLink) bo;
112    
113            if (isValid) {
114                bo.refreshReferenceObject(EndowPropertyConstants.GL_LINK_FINANCIAL_OBJECT_CODE);
115    
116                // check that ObjectCode.ObjectTypeCode.BasicAccountingCategory == EndowmentTransactionCode.TransactionTypeCode
117                if (!validateGLLinkObjectCode(endowmentTransactionCode, glLink)) {
118                    isValid = false;
119                    putFieldError(KFSConstants.ADD_PREFIX + "." + EndowPropertyConstants.ENDOWMENT_TRANSACTION_GL_LINKS + "." + EndowPropertyConstants.GL_LINK_OBJECT_CD, EndowKeyConstants.EndowmentTransactionConstants.ERROR_GL_LINK_OBJ_CD_ACC_CATEGORY_MUST_EQUAL_ETRAN_TYPE);
120                }
121    
122                // check that the selected chart and the object code chart are the same
123                if (ObjectUtils.isNotNull(glLink.getFinancialObjectCode()) && !StringUtils.equalsIgnoreCase(glLink.getFinancialObjectCode().getChartOfAccountsCode(), glLink.getChartCode())) {
124                    isValid = false;
125                    putFieldError(KFSConstants.ADD_PREFIX + "." + EndowPropertyConstants.ENDOWMENT_TRANSACTION_GL_LINKS + "." + EndowPropertyConstants.GL_LINK_CHART_CD, EndowKeyConstants.EndowmentTransactionConstants.ERROR_GL_LINK_CHART_CD_MUST_EQUAL_OBJECT_CHART_CD);
126                }
127    
128                // check that there is no other GL Link with the same chart already added
129                if (collectionName.equals(EndowPropertyConstants.ENDOWMENT_TRANSACTION_GL_LINKS)) {
130                    if (glLink.getChartCode() != null) {
131                        for (GLLink tmpGlLink : endowmentTransactionCode.getGlLinks()) {
132                            if (glLink.getChartCode().equals(tmpGlLink.getChartCode())) {
133                                isValid = false;
134                                putFieldError(KFSConstants.ADD_PREFIX + "." + EndowPropertyConstants.ENDOWMENT_TRANSACTION_GL_LINKS + "." + EndowPropertyConstants.GL_LINK_CHART_CD, EndowKeyConstants.EndowmentTransactionConstants.ERROR_GL_LINK_WITH_SAME_CHART_ALREADY_EXISTS);
135                            }
136                        }
137                    }
138                }
139            }
140    
141            return isValid;
142        }
143    
144        /**
145         * Checks that the selected object code for the glLink is valid.
146         * 
147         * @param endowmentTransactionCode the endowment transaction
148         * @param glLink the gl link
149         * @return true if the object code is valid, false otherwise
150         */
151        private boolean validateGLLinkObjectCode(EndowmentTransactionCode endowmentTransactionCode, GLLink glLink) {
152            boolean isValid = true;
153    
154            ParameterService parameterService = SpringContext.getBean(ParameterService.class);
155            ObjectCode objectCode = glLink.getFinancialObjectCode();
156    
157            if (ObjectUtils.isNotNull(objectCode)) {
158    
159                // check that ObjectCode.ObjectTypeCode.BasicAccountingCategory == EndowmentTransactionCode.TransactionTypeCode
160                if (EndowConstants.EndowmentTransactionTypeCodes.ASSET_TYPE_CODE.equalsIgnoreCase(endowmentTransactionCode.getEndowmentTransactionTypeCode())) {
161                    String basicAccountingCategoryAsset = parameterService.getParameterValue(EndowmentTransactionCode.class, EndowParameterKeyConstants.ASSETS_ENTRAN_TYPE);
162    
163                    if (!objectCode.getFinancialObjectType().getBasicAccountingCategoryCode().equalsIgnoreCase(basicAccountingCategoryAsset)) {
164                        isValid = false;
165                    }
166                }
167    
168                if (EndowConstants.EndowmentTransactionTypeCodes.EXPENSE_TYPE_CODE.equalsIgnoreCase(endowmentTransactionCode.getEndowmentTransactionTypeCode())) {
169                    String basicAccountingCategoryExpense = parameterService.getParameterValue(EndowmentTransactionCode.class, EndowParameterKeyConstants.EXPENSES_ENTRAN_TYPE);
170    
171                    if (!objectCode.getFinancialObjectType().getBasicAccountingCategoryCode().equalsIgnoreCase(basicAccountingCategoryExpense)) {
172                        isValid = false;
173                    }
174                }
175    
176                if (EndowConstants.EndowmentTransactionTypeCodes.INCOME_TYPE_CODE.equalsIgnoreCase(endowmentTransactionCode.getEndowmentTransactionTypeCode())) {
177                    String basicAccountingCategoryIncome = parameterService.getParameterValue(EndowmentTransactionCode.class, EndowParameterKeyConstants.INCOME_ENTRAN_TYPE);
178    
179                    if (!objectCode.getFinancialObjectType().getBasicAccountingCategoryCode().equalsIgnoreCase(basicAccountingCategoryIncome)) {
180                        isValid = false;
181                    }
182                }
183    
184                if (EndowConstants.EndowmentTransactionTypeCodes.LIABILITY_TYPE_CODE.equalsIgnoreCase(endowmentTransactionCode.getEndowmentTransactionTypeCode())) {
185                    String basicAccountingCategoryLiabilities = parameterService.getParameterValue(EndowmentTransactionCode.class, EndowParameterKeyConstants.LIABILITIES_ENTRAN_TYPE);
186    
187                    if (!objectCode.getFinancialObjectType().getBasicAccountingCategoryCode().equalsIgnoreCase(basicAccountingCategoryLiabilities)) {
188                        isValid = false;
189                    }
190                }
191            }
192            return isValid;
193        }
194    
195    }