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.kuali.kfs.module.endow.EndowKeyConstants;
022    import org.kuali.kfs.module.endow.businessobject.EndowmentSourceTransactionLine;
023    import org.kuali.kfs.module.endow.businessobject.EndowmentTargetTransactionLine;
024    import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine;
025    import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionTaxLotLine;
026    import org.kuali.kfs.module.endow.document.EndowmentSecurityDetailsDocument;
027    import org.kuali.kfs.module.endow.document.EndowmentTaxLotLinesDocument;
028    import org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocument;
029    import org.kuali.kfs.module.endow.document.SecurityTransferDocument;
030    import org.kuali.kfs.module.endow.document.validation.DeleteTaxLotLineRule;
031    import org.kuali.rice.kns.document.Document;
032    import org.kuali.rice.kns.util.GlobalVariables;
033    
034    public class SecurityTransferDocumentRules extends EndowmentTransactionLinesDocumentBaseRules implements DeleteTaxLotLineRule<EndowmentTaxLotLinesDocument, EndowmentTransactionTaxLotLine, EndowmentTransactionLine, Number, Number> {
035    
036    
037        /**
038         * @see org.kuali.kfs.module.endow.document.validation.AddTransactionLineRule#processAddTransactionLineRules(org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocument,
039         *      org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine)
040         */
041        @Override
042        public boolean processAddTransactionLineRules(EndowmentTransactionLinesDocument transLineDocument, EndowmentTransactionLine line) {
043            boolean isValid = true;
044    
045            SecurityTransferDocument securityTransferDocument = (SecurityTransferDocument) transLineDocument;
046    
047            isValid &= validateSecurity(isValid, securityTransferDocument, true);
048    
049            isValid &= validateRegistration(isValid, securityTransferDocument, true);
050    
051            // if adding a target transaction line check that there is at least one source transaction line entered
052            if (line instanceof EndowmentTargetTransactionLine) {
053                if (transLineDocument.getSourceTransactionLines() == null || transLineDocument.getSourceTransactionLines().size() == 0) {
054                    putFieldError(getErrorPrefix(line, -1), EndowKeyConstants.EndowmentTransactionDocumentConstants.ERROR_SECURITY_TRANSFER_ENTER_SOURCE_TRANS_LINE);
055                    return false;
056                }
057            }
058    
059            // there can be only one source transaction line
060            isValid &= validateOnlyOneSourceTransactionLine(true, securityTransferDocument, line, -1);
061    
062            if (isValid) {
063                isValid &= super.processAddTransactionLineRules(securityTransferDocument, line);
064            }
065    
066            if (isValid) {
067                isValid &= validateSecurityTransferTransactionLine(true, securityTransferDocument, line, -1, -1);
068            }
069    
070            return GlobalVariables.getMessageMap().getErrorCount() == 0;
071        }
072    
073        /**
074         * Validates that one and only one source transaction line can be added.
075         * 
076         * @param validateForAdd tells whether the validation is for add or not
077         * @param endowmentTransactionLinesDocument
078         * @return true if valid, false otherwise
079         */
080        protected boolean validateOnlyOneSourceTransactionLine(boolean validateForAdd, EndowmentTransactionLinesDocument endowmentTransactionLinesDocument, EndowmentTransactionLine line, int index) {
081            boolean isValid = true;
082    
083            if (line instanceof EndowmentSourceTransactionLine) {
084    
085                // if we do validation upon adding a new transaction line make sure we don't allow more than one line to be added; if
086                // there
087                // is already one source transaction line than validation will fail as no more source transaction line can be added
088                if (validateForAdd) {
089                    if (endowmentTransactionLinesDocument.getSourceTransactionLines() != null && endowmentTransactionLinesDocument.getSourceTransactionLines().size() >= 1) {
090                        isValid = false;
091                        putFieldError(getErrorPrefix(line, index), EndowKeyConstants.EndowmentTransactionDocumentConstants.ERROR_SECURITY_TRANSFER_ONE_AND_ONLY_ONE_SOURCE_TRANS_LINE);
092                    }
093                }
094                // if we do validation on save or submit we have to make sure that there is one and only one source transaction line
095                else {
096                    if (endowmentTransactionLinesDocument.getSourceTransactionLines() != null && endowmentTransactionLinesDocument.getSourceTransactionLines().size() != 1) {
097                        isValid = false;
098                        putFieldError(getErrorPrefix(line, index), EndowKeyConstants.EndowmentTransactionDocumentConstants.ERROR_SECURITY_TRANSFER_ONE_AND_ONLY_ONE_SOURCE_TRANS_LINE);
099                    }
100                }
101            }
102    
103            return isValid;
104    
105        }
106    
107        /**
108         * Validate Security Transfer Transaction Line.
109         * 
110         * @param endowmentTransactionLinesDocumentBase
111         * @param line
112         * @param index
113         * @return
114         */
115        protected boolean validateSecurityTransferTransactionLine(boolean isAdd, EndowmentTransactionLinesDocument endowmentTransactionLinesDocument, EndowmentTransactionLine line, int transLineIndex, int taxLotIndex) {
116            boolean isValid = super.validateTransactionLine(endowmentTransactionLinesDocument, line, transLineIndex);
117    
118            if (isValid) {
119                // Obtain Prefix for Error fields in UI.
120                String ERROR_PREFIX = getErrorPrefix(line, transLineIndex);
121    
122                // Validate Units is Greater then Zero(thus positive) value
123                isValid &= validateTransactionUnitsGreaterThanZero(line, ERROR_PREFIX);
124                if (isValid) {
125    
126                    if (line instanceof EndowmentSourceTransactionLine) {
127                        // Validate if Sufficient Units are Avaiable
128                        isValid &= validateSufficientUnits(isAdd, endowmentTransactionLinesDocument, line, transLineIndex, taxLotIndex);
129                    }
130    
131                    // Check if value of Endowment is being reduced.
132                    checkWhetherReducePermanentlyRestrictedFund(line, ERROR_PREFIX);
133                }
134            }
135    
136            return GlobalVariables.getMessageMap().getErrorCount() == 0;
137        }
138    
139        /**
140         * @see org.kuali.rice.kns.rules.DocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.Document)
141         */
142        @Override
143        protected boolean processCustomRouteDocumentBusinessRules(Document document) {
144            boolean isValid = super.processCustomRouteDocumentBusinessRules(document);
145    
146            if (isValid) {
147                SecurityTransferDocument securityTransferDocument = (SecurityTransferDocument) document;
148    
149                // Validate Security
150                isValid &= validateSecurity(isValid, securityTransferDocument, true);
151    
152                // Validate Registration code.
153                isValid &= validateRegistration(isValid, securityTransferDocument, true);
154    
155                // Validate atleast one Source Tx was entered.
156                if (!transactionLineSizeGreaterThanZero(securityTransferDocument, true))
157                    return false;
158    
159                // Validate atleast one Target Tx was entered.
160                if (!transactionLineSizeGreaterThanZero(securityTransferDocument, false))
161                    return false;
162    
163                // Obtaining all the transaction lines for validations
164                List<EndowmentTransactionLine> txLines = new ArrayList<EndowmentTransactionLine>();
165                txLines.addAll(securityTransferDocument.getSourceTransactionLines());
166                txLines.addAll(securityTransferDocument.getTargetTransactionLines());
167    
168                // Validate All the Transaction Lines.
169                for (int i = 0; i < txLines.size(); i++) {
170                    EndowmentTransactionLine txLine = txLines.get(i);
171    
172                    if (!validateSecurityTransferTransactionLine(false, securityTransferDocument, txLine, i, -1)) {
173                        return false;
174                    }
175    
176                    isValid &= validateTaxLots(securityTransferDocument, txLine, i);
177                    isValid &= validateTotalUnits(securityTransferDocument, txLine, i);
178                }
179    
180                // Validate the source & target units are equal.
181                isValid &= validateSourceTargetUnitsEqual(securityTransferDocument);
182    
183                // Validate the source & target amounts are equal.
184                isValid &= validateSourceTargetAmountEqual(securityTransferDocument);
185    
186            }
187    
188            return GlobalVariables.getMessageMap().getErrorCount() == 0;
189        }
190    
191        /**
192         * @see org.kuali.kfs.module.endow.document.validation.impl.EndowmentTransactionalDocumentBaseRule#validateSecurityClassTypeCode(org.kuali.kfs.module.endow.document.EndowmentSecurityDetailsDocument,
193         *      boolean, java.lang.String)
194         */
195        @Override
196        protected boolean validateSecurityClassTypeCode(EndowmentSecurityDetailsDocument document, boolean isSource, String classCodeType) {
197            return true;
198        }
199    
200        /**
201         * @see org.kuali.kfs.module.endow.document.validation.impl.EndowmentTransactionLinesDocumentBaseRules#processRefreshTransactionLineRules(org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocument,
202         *      org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine, java.lang.Number)
203         */
204        @Override
205        public boolean processRefreshTransactionLineRules(EndowmentTransactionLinesDocument endowmentTransactionLinesDocument, EndowmentTransactionLine endowmentTransactionLine, Number index) {
206            boolean isValid = super.processRefreshTransactionLineRules(endowmentTransactionLinesDocument, endowmentTransactionLine, index);
207            if (isValid) {
208                isValid &= validateSecurityTransferTransactionLine(false, endowmentTransactionLinesDocument, endowmentTransactionLine, (Integer) index, -1);
209            }
210            return isValid;
211        }
212    
213        /**
214         * @see org.kuali.kfs.module.endow.document.validation.DeleteTaxLotLineRule#processDeleteTaxLotLineRules(org.kuali.kfs.module.endow.document.EndowmentTaxLotLinesDocument,
215         *      org.kuali.kfs.module.endow.businessobject.EndowmentTransactionTaxLotLine,
216         *      org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine, java.lang.Number, java.lang.Number)
217         */
218        public boolean processDeleteTaxLotLineRules(EndowmentTaxLotLinesDocument endowmentTaxLotLinesDocument, EndowmentTransactionTaxLotLine endowmentTransactionTaxLotLine, EndowmentTransactionLine endowmentTransactionLine, Number index, Number numberX) {
219    
220            boolean isValid = true;
221            isValid &= validateTransactionLine(endowmentTaxLotLinesDocument, endowmentTransactionLine, (Integer) index);
222            if (isValid) {
223                isValid &= validateSecurityTransferTransactionLine(false, endowmentTaxLotLinesDocument, endowmentTransactionLine, (Integer) index, (Integer) numberX);
224            }
225            return isValid;
226        }
227    }