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;
017    
018    import java.util.List;
019    
020    import org.kuali.kfs.module.endow.businessobject.EndowmentAccountingLine;
021    import org.kuali.kfs.module.endow.businessobject.EndowmentAccountingLineParser;
022    import org.kuali.kfs.module.endow.businessobject.SourceEndowmentAccountingLine;
023    import org.kuali.kfs.module.endow.businessobject.TargetEndowmentAccountingLine;
024    import org.kuali.kfs.sys.context.SpringContext;
025    import org.kuali.kfs.sys.document.datadictionary.FinancialSystemTransactionalDocumentEntry;
026    import org.kuali.rice.kns.service.DataDictionaryService;
027    import org.kuali.rice.kns.util.KualiDecimal;
028    import org.kuali.rice.kns.util.TypedArrayList;
029    
030    /**
031     * Provides a base class for the Endowment Transfer of Funds documents.
032     */
033    public abstract class EndowmentAccountingLinesDocumentBase extends EndowmentSecurityDetailsDocumentBase implements EndowmentAccountingLinesDocument {
034        protected Integer nextSourceAccountingLineNumber;
035        protected Integer nextTargetAccountingLineNumber;
036        protected List<SourceEndowmentAccountingLine> sourceAccountingLines;
037        protected List<TargetEndowmentAccountingLine> targetAccountingLines;
038    
039        protected transient FinancialSystemTransactionalDocumentEntry dataDictionaryEntry;
040    
041        protected transient Class sourceAccountingLineClass;
042        protected transient Class targetAccountingLineClass;
043    
044        /**
045         * Constructs a EndowmentTransferOfFundsDocument.
046         */
047        public EndowmentAccountingLinesDocumentBase() {
048            super();
049            this.nextSourceAccountingLineNumber = new Integer(1);
050            this.nextTargetAccountingLineNumber = new Integer(1);
051            sourceAccountingLines = new TypedArrayList(SourceEndowmentAccountingLine.class);
052            targetAccountingLines = new TypedArrayList(TargetEndowmentAccountingLine.class);
053        }
054    
055    
056        /**
057         * Gets the nextSourceAccountingLineNumber.
058         * 
059         * @return nextSourceAccountingLineNumber
060         */
061        public Integer getNextSourceAccountingLineNumber() {
062            return nextSourceAccountingLineNumber;
063        }
064    
065        /**
066         * Sets the nextSourceAccountingLineNumber.
067         * 
068         * @param nextSourceAccountingLineNumber
069         */
070        public void setNextSourceAccountingLineNumber(Integer nextSourceAccountingLineNumber) {
071            this.nextSourceAccountingLineNumber = nextSourceAccountingLineNumber;
072        }
073    
074        /**
075         * Gets the nextTargetAccountingLineNumber.
076         * 
077         * @return nextTargetAccountingLineNumber
078         */
079        public Integer getNextTargetAccountingLineNumber() {
080            return nextTargetAccountingLineNumber;
081        }
082    
083        /**
084         * Sets the nextTargetAccountingLineNumber.
085         * 
086         * @param nextTargetAccountingLineNumber
087         */
088        public void setNextTargetAccountingLineNumber(Integer nextTargetAccountingLineNumber) {
089            this.nextTargetAccountingLineNumber = nextTargetAccountingLineNumber;
090        }
091    
092        /**
093         * Gets the sourceAccountingLines.
094         * 
095         * @return sourceAccountingLines
096         */
097        public List<SourceEndowmentAccountingLine> getSourceAccountingLines() {
098            return sourceAccountingLines;
099        }
100    
101        /**
102         * Sets the sourceAccountingLines.
103         * 
104         * @param sourceAccountingLines
105         */
106        public void setSourceAccountingLines(List<SourceEndowmentAccountingLine> sourceAccountingLines) {
107            this.sourceAccountingLines = sourceAccountingLines;
108        }
109    
110        /**
111         * Gets the targetAccountingLines.
112         * 
113         * @return targetAccountingLines
114         */
115        public List<TargetEndowmentAccountingLine> getTargetAccountingLines() {
116            return targetAccountingLines;
117        }
118    
119        /**
120         * Sets the targetAccountingLines.
121         * 
122         * @param targetAccountingLines
123         */
124        public void setTargetAccountingLines(List<TargetEndowmentAccountingLine> targetAccountingLines) {
125            this.targetAccountingLines = targetAccountingLines;
126        }
127    
128        /**
129         * @see org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocument#addSourceTransactionLine(org.kuali.kfs.module.endow.businessobject.EndowmentSourceTransactionLine)
130         */
131        public void addSourceAccountingLine(SourceEndowmentAccountingLine line) {
132            line.setAccountingLineNumber(this.getNextSourceAccountingLineNumber());
133            this.sourceAccountingLines.add(line);
134            this.nextSourceAccountingLineNumber = new Integer(this.getNextSourceAccountingLineNumber().intValue() + 1);
135    
136        }
137    
138    
139        /**
140         * @see org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocument#addTargetTransactionLine(org.kuali.kfs.module.endow.businessobject.EndowmentTargetTransactionLine)
141         */
142        public void addTargetAccountingLine(TargetEndowmentAccountingLine line) {
143            line.setAccountingLineNumber(this.getNextTargetAccountingLineNumber());
144            this.targetAccountingLines.add(line);
145            this.nextTargetAccountingLineNumber = new Integer(this.getNextTargetAccountingLineNumber().intValue() + 1);
146        }
147    
148    
149        /**
150         * @see org.kuali.kfs.module.endow.document.EndowmentAccountingLinesDocument#getSourceAccountingLine(int)
151         */
152        public SourceEndowmentAccountingLine getSourceAccountingLine(int index) {
153    
154            while (getSourceAccountingLines().size() <= index) {
155                getSourceAccountingLines().add(new SourceEndowmentAccountingLine());
156            }
157            return (SourceEndowmentAccountingLine) getSourceAccountingLines().get(index);
158        }
159    
160    
161        /**
162         * @see org.kuali.kfs.module.endow.document.EndowmentAccountingLinesDocument#getTargetAccountingLine(int)
163         */
164        public TargetEndowmentAccountingLine getTargetAccountingLine(int index) {
165            while (getTargetAccountingLines().size() <= index) {
166                getTargetAccountingLines().add(new TargetEndowmentAccountingLine());
167            }
168            return (TargetEndowmentAccountingLine) getTargetAccountingLines().get(index);
169        }
170    
171        /**
172         * Gets the total amount of the accounting lines on this document.
173         * 
174         * @return total amount of the accounting lines
175         */
176        public KualiDecimal getTotalAccountingLinesAmount() {
177    
178            KualiDecimal totalAmount = KualiDecimal.ZERO;
179    
180            if (sourceAccountingLines.size() > 0) {
181                for (EndowmentAccountingLine accountingLine : sourceAccountingLines) {
182                    if (accountingLine.getAmount() != null) {
183                        totalAmount = totalAmount.add(accountingLine.getAmount());
184                    }
185                }
186            }
187            else if (targetAccountingLines.size() > 0) {
188                for (EndowmentAccountingLine accountingLine : targetAccountingLines) {
189                    if (accountingLine.getAmount() != null) {
190                        totalAmount = totalAmount.add(accountingLine.getAmount());
191                    }
192                }
193            }
194    
195            return totalAmount;
196        }
197    
198    
199        /**
200         * @see org.kuali.rice.kns.bo.PersistableBusinessObjectBase#buildListOfDeletionAwareLists()
201         */
202        public List buildListOfDeletionAwareLists() {
203            List managedList = super.buildListOfDeletionAwareLists();
204    
205            managedList.add(getTargetAccountingLines());
206            managedList.add(getSourceAccountingLines());
207    
208            return managedList;
209        }
210    
211        /**
212         * Used to get the appropriate <code>{@link EndowmentAccountingLineParser}</code> for the <code>Document</code>
213         * 
214         * @return EndowmentAccountingLineParser
215         */
216        public EndowmentAccountingLineParser getEndowmentAccountingLineParser() {
217            // TODO: check if this is needed
218            // try {
219            // if (getDataDictionaryEntry().getImportedLineParserClass() != null) {
220            // return getDataDictionaryEntry().getImportedLineParserClass().newInstance();
221            // }
222            // }
223            // catch (InstantiationException ie) {
224            // throw new IllegalStateException("Accounting Line Parser class " +
225            // getDataDictionaryEntry().getImportedLineParserClass().getName() + " cannot be instantiated", ie);
226            // }
227            // catch (IllegalAccessException iae) {
228            // throw new IllegalStateException("Illegal Access Exception while attempting to instantiate Accounting Line Parser class "
229            // + getDataDictionaryEntry().getImportedLineParserClass().getName(), iae);
230            // }
231            return new EndowmentAccountingLineParserBase();
232        }
233    
234        /**
235         * @return the data dictionary entry for this document
236         */
237        public FinancialSystemTransactionalDocumentEntry getDataDictionaryEntry() {
238            if (dataDictionaryEntry == null) {
239                dataDictionaryEntry = (FinancialSystemTransactionalDocumentEntry) SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getDocumentEntry(SpringContext.getBean(DataDictionaryService.class).getValidDocumentTypeNameByClass(getClass()));
240            }
241            return dataDictionaryEntry;
242        }
243    
244    
245        /**
246         * @see org.kuali.kfs.module.endow.document.EndowmentAccountingLinesDocument#getSourceAccountingLineClass()
247         */
248        public Class getSourceAccountingLineClass() {
249            if (sourceAccountingLineClass == null) {
250                sourceAccountingLineClass = (getDataDictionaryEntry().getAccountingLineGroups() != null && getDataDictionaryEntry().getAccountingLineGroups().containsKey("source") && getDataDictionaryEntry().getAccountingLineGroups().get("source").getAccountingLineClass() != null) ? getDataDictionaryEntry().getAccountingLineGroups().get("source").getAccountingLineClass() : SourceEndowmentAccountingLine.class;
251            }
252            return sourceAccountingLineClass;
253        }
254    
255    
256        /**
257         * @see org.kuali.kfs.module.endow.document.EndowmentAccountingLinesDocument#getTargetAccountingLineClass()
258         */
259        public Class getTargetAccountingLineClass() {
260            if (targetAccountingLineClass == null) {
261                targetAccountingLineClass = (getDataDictionaryEntry().getAccountingLineGroups() != null && getDataDictionaryEntry().getAccountingLineGroups().containsKey("target") && getDataDictionaryEntry().getAccountingLineGroups().get("target").getAccountingLineClass() != null) ? getDataDictionaryEntry().getAccountingLineGroups().get("target").getAccountingLineClass() : TargetEndowmentAccountingLine.class;
262            }
263            return targetAccountingLineClass;
264        }
265    }