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.purap.businessobject;
017    
018    import java.math.BigDecimal;
019    import java.util.LinkedHashMap;
020    import java.util.Map;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.apache.commons.lang.builder.EqualsBuilder;
024    import org.kuali.kfs.sys.KFSPropertyConstants;
025    import org.kuali.kfs.sys.businessobject.SourceAccountingLine;
026    import org.kuali.rice.kns.util.KualiDecimal;
027    import org.kuali.rice.kns.util.ObjectUtils;
028    
029    /**
030     * Purap Accounting Line Base Business Object.
031     */
032    public abstract class PurApAccountingLineBase extends SourceAccountingLine implements PurApAccountingLine, Comparable {
033        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PurApAccountingLineBase.class);
034        protected Integer accountIdentifier;
035        private Integer itemIdentifier;
036        private BigDecimal accountLinePercent;
037        private String postingPeriodCode;  //stored in DB only for PREQ and CM Account History
038        private KualiDecimal alternateAmountForGLEntryCreation; // not stored in DB; needed for disencumbrances and such
039        
040        private PurApItem purapItem;
041    
042        public Integer getAccountIdentifier() {
043            return accountIdentifier;
044        }
045    
046        public void setAccountIdentifier(Integer requisitionAccountIdentifier) {
047            this.accountIdentifier = requisitionAccountIdentifier;
048        }
049    
050        public Integer getItemIdentifier() {
051            return itemIdentifier;
052        }
053    
054        public void setItemIdentifier(Integer requisitionItemIdentifier) {
055            this.itemIdentifier = requisitionItemIdentifier;
056        }
057    
058        public BigDecimal getAccountLinePercent() {
059            return accountLinePercent;
060        }
061    
062        public void setAccountLinePercent(BigDecimal accountLinePercent) {
063            this.accountLinePercent = accountLinePercent;
064        }
065    
066        /**
067         * @see org.kuali.kfs.module.purap.businessobject.PurApAccountingLine#isEmpty()
068         */
069        public boolean isEmpty() {
070            return !(StringUtils.isNotEmpty(getAccountNumber()) || StringUtils.isNotEmpty(getChartOfAccountsCode()) || StringUtils.isNotEmpty(getFinancialObjectCode()) || StringUtils.isNotEmpty(getFinancialSubObjectCode()) || StringUtils.isNotEmpty(getOrganizationReferenceId()) || StringUtils.isNotEmpty(getProjectCode()) || StringUtils.isNotEmpty(getSubAccountNumber()) || ObjectUtils.isNotNull(getAccountLinePercent()));
071        }
072    
073        /**
074         * @see org.kuali.kfs.module.purap.businessobject.PurApAccountingLine#createBlankAmountsCopy()
075         */
076        public PurApAccountingLine createBlankAmountsCopy() {
077            PurApAccountingLine newAccount = (PurApAccountingLine) ObjectUtils.deepCopy(this);
078            newAccount.setAccountLinePercent(BigDecimal.ZERO);
079            newAccount.setAmount(KualiDecimal.ZERO);
080            return newAccount;
081        }
082    
083    
084        /**
085         * @see org.kuali.kfs.module.purap.businessobject.PurApAccountingLine#accountStringsAreEqual(org.kuali.kfs.sys.businessobject.SourceAccountingLine)
086         */
087        public boolean accountStringsAreEqual(SourceAccountingLine accountingLine) {
088            if (accountingLine == null) {
089                return false;
090            }
091            return new EqualsBuilder().append(getChartOfAccountsCode(), accountingLine.getChartOfAccountsCode()).append(getAccountNumber(), accountingLine.getAccountNumber()).append(getSubAccountNumber(), accountingLine.getSubAccountNumber()).append(getFinancialObjectCode(), accountingLine.getFinancialObjectCode()).append(getFinancialSubObjectCode(), accountingLine.getFinancialSubObjectCode()).append(getProjectCode(), accountingLine.getProjectCode()).append(getOrganizationReferenceId(), accountingLine.getOrganizationReferenceId())
092                    .isEquals();
093        }
094    
095        public boolean accountStringsAreEqual(PurApAccountingLine accountingLine) {
096            return accountStringsAreEqual((SourceAccountingLine) accountingLine);
097    
098        }
099    
100        /**
101         * @see org.kuali.kfs.module.purap.businessobject.PurApAccountingLine#generateSourceAccountingLine()
102         */
103        public SourceAccountingLine generateSourceAccountingLine() {
104            // the fields here should probably match method 'accountStringsAreEqual' above
105            SourceAccountingLine sourceLine = new SourceAccountingLine();
106            sourceLine.setChartOfAccountsCode(getChartOfAccountsCode());
107            sourceLine.setAccountNumber(getAccountNumber());
108            sourceLine.setSubAccountNumber(getSubAccountNumber());
109            sourceLine.setFinancialObjectCode(getFinancialObjectCode());
110            sourceLine.setFinancialSubObjectCode(getFinancialSubObjectCode());
111            sourceLine.setProjectCode(getProjectCode());
112            sourceLine.setOrganizationReferenceId(getOrganizationReferenceId());
113            sourceLine.setAmount(getAmount());
114            return sourceLine;
115        }
116    
117        /**
118         * @see org.kuali.kfs.sys.businessobject.AccountingLineBase#toStringMapper()
119         */
120        @Override
121        protected LinkedHashMap toStringMapper() {
122            LinkedHashMap m = new LinkedHashMap();
123    
124            m.put("chart", getChartOfAccountsCode());
125            m.put("account", getAccountNumber());
126            m.put("objectCode", getFinancialObjectCode());
127            m.put("subAccount", getSubAccountNumber());
128            m.put("subObjectCode", getFinancialSubObjectCode());
129            m.put("projectCode", getProjectCode());
130            m.put("orgRefId", getOrganizationReferenceId());
131    
132            return m;
133        }
134    
135        public int compareTo(Object arg0) {
136            if (arg0 instanceof PurApAccountingLine) {
137                PurApAccountingLine account = (PurApAccountingLine) arg0;
138                return this.getString().compareTo(account.getString());
139            }
140            return -1;
141        }
142    
143        public String getString() {
144            return getChartOfAccountsCode() + "~" + getAccountNumber() + "~" + getSubAccountNumber() + "~" + getFinancialObjectCode() + "~" + getFinancialSubObjectCode() + "~" + getProjectCode() + "~" + getOrganizationReferenceId();
145        }
146    
147        public KualiDecimal getAlternateAmountForGLEntryCreation() {
148            return alternateAmountForGLEntryCreation;
149        }
150    
151        public void setAlternateAmountForGLEntryCreation(KualiDecimal alternateAmount) {
152            this.alternateAmountForGLEntryCreation = alternateAmount;
153        }
154    
155        /**
156         * @see org.kuali.kfs.sys.businessobject.AccountingLineBase#getSequenceNumber()
157         */
158        @Override
159        public Integer getSequenceNumber() {
160            return this.getAccountIdentifier();
161        }
162        
163        protected void copyFrom(PurApAccountingLine other) {
164            
165            super.copyFrom(other);
166    
167            setAccountLinePercent(other.getAccountLinePercent());      
168            setAlternateAmountForGLEntryCreation(other.getAlternateAmountForGLEntryCreation());            
169     
170        }
171     
172        @Override
173        public void refreshNonUpdateableReferences() {
174            //hold onto item reference if there without itemId
175            PurApItem item = null;
176            PurApItem tempItem = getPurapItem();
177            if(tempItem != null &&
178               tempItem.getItemIdentifier() != null) {
179                item = tempItem;
180            }
181            super.refreshNonUpdateableReferences();
182            if(ObjectUtils.isNotNull(item)) {
183                this.setPurapItem(item);
184            }
185        }
186    
187        public <T extends PurApItem> T getPurapItem() {
188            return (T) purapItem;
189        }
190        
191        /**
192         * Sets the requisitionItem attribute.
193         * @deprecated
194         * @param item
195         */
196        public void setPurapItem(PurApItem item) {
197            purapItem = item;
198        }
199    
200        public String getPostingPeriodCode() {
201            return postingPeriodCode;
202        }
203    
204        public void setPostingPeriodCode(String postingPeriodCode) {
205            this.postingPeriodCode = postingPeriodCode;
206        }
207    
208        /**
209         * Overridden to use purap doc identifier, rather than document number
210         * @see org.kuali.kfs.sys.businessobject.AccountingLineBase#getValuesMap()
211         */
212        @Override
213        public Map getValuesMap() {
214            Map valuesMap = super.getValuesMap();
215            // remove document number
216            valuesMap.remove(KFSPropertyConstants.DOCUMENT_NUMBER);
217            return valuesMap;
218        }
219    
220    }