001/*
002 * Copyright 2006 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
017package org.kuali.ole.coa.businessobject;
018
019import java.io.Serializable;
020import java.text.DateFormat;
021import java.text.ParseException;
022import java.text.SimpleDateFormat;
023import java.util.Calendar;
024import java.util.Date;
025import java.util.LinkedHashMap;
026import javax.persistence.*;
027
028import org.apache.commons.lang.StringUtils;
029import org.apache.commons.lang.builder.CompareToBuilder;
030import org.apache.commons.lang.builder.EqualsBuilder;
031import org.apache.commons.lang.builder.HashCodeBuilder;
032import org.apache.commons.lang.builder.ToStringBuilder;
033import org.apache.commons.lang.time.DateFormatUtils;
034import org.apache.commons.lang.time.DateUtils;
035import org.kuali.ole.coa.businessobject.Chart;
036import org.kuali.ole.sys.context.SpringContext;
037import org.kuali.rice.core.api.datetime.DateTimeService;
038import org.kuali.rice.core.api.mo.common.active.MutableInactivatable;
039import org.kuali.rice.core.api.util.type.KualiDecimal;
040import org.kuali.rice.kew.api.doctype.DocumentTypeService;
041import org.kuali.rice.kew.doctype.bo.DocumentType;
042import org.kuali.rice.kew.doctype.bo.DocumentTypeEBO;
043import org.kuali.rice.kim.api.identity.Person;
044import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
045import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
046
047/**
048 * 
049 */
050@Entity
051@Table(name = "CA_ORG_RTNG_MDL_T")
052@IdClass(AccountDelegateModelDetail.AccountDelegateModelDetailId.class)
053public class AccountDelegateModelDetail extends PersistableBusinessObjectBase implements MutableInactivatable {
054
055    @Id
056    @Column(name = "FIN_COA_CD")
057    private String chartOfAccountsCode;
058
059    @Id
060    @Column(name = "ORG_CD")
061    private String organizationCode;
062
063    @Id
064    @Column(name = "ORG_RTNG_MDL_NM")
065    private String accountDelegateModelName;
066
067    @Id
068    @Column(name = "ACCT_DLGT_UNVL_ID")
069    private String accountDelegateUniversalId;
070
071    @Id
072    @Column(name = "FDOC_TYP_CD")
073    private String financialDocumentTypeCode;
074
075    @Column(name = "FDOC_APRV_FROM_AMT")
076    private KualiDecimal approvalFromThisAmount;
077
078    @Column(name = "FDOC_APRV_TO_AMT")
079    private KualiDecimal approvalToThisAmount;
080
081    @Column(name = "ACCT_DLGT_PRMRT_CD")
082    @Convert(converter = BooleanYNConverter.class)
083    private boolean accountDelegatePrimaryRoutingIndicator;
084
085    @Column(name = "ACCT_DLGT_START_DT")
086    @Temporal(TemporalType.DATE)
087    private Date accountDelegateStartDate;
088
089    @Column(name = "ROW_ACTV_IND")
090    @Convert(converter = BooleanYNConverter.class)
091    private boolean active;
092
093    @ManyToOne(targetEntity = Chart.class, fetch = FetchType.LAZY, cascade = { CascadeType.REFRESH })
094    @JoinColumn(name = "FIN_COA_CD", referencedColumnName = "FIN_COA_CD", insertable = false, updatable = false)
095    private Chart chartOfAccounts;
096
097    @Transient
098    private DocumentTypeEBO financialSystemDocumentTypeCode;
099
100    @Transient
101    private Person accountDelegate;
102
103    /**
104     * Default constructor.
105     */
106    public AccountDelegateModelDetail() {
107    }
108
109    public AccountDelegateModelDetail(AccountDelegateGlobalDetail delegateGlobalDetail) {
110        accountDelegateUniversalId = delegateGlobalDetail.getAccountDelegateUniversalId();
111        accountDelegatePrimaryRoutingIndicator = delegateGlobalDetail.getAccountDelegatePrimaryRoutingIndicator();
112        approvalFromThisAmount = delegateGlobalDetail.getApprovalFromThisAmount();
113        approvalToThisAmount = delegateGlobalDetail.getApprovalToThisAmount();
114        accountDelegateStartDate = delegateGlobalDetail.getAccountDelegateStartDate();
115        financialDocumentTypeCode = delegateGlobalDetail.getFinancialDocumentTypeCode();
116    }
117
118    /**
119     * Gets the chartOfAccountsCode attribute.
120     * 
121     * @return Returns the chartOfAccountsCode
122     */
123    public String getChartOfAccountsCode() {
124        return chartOfAccountsCode;
125    }
126
127    /**
128     * Sets the chartOfAccountsCode attribute.
129     * 
130     * @param chartOfAccountsCode The chartOfAccountsCode to set.
131     */
132    public void setChartOfAccountsCode(String chartOfAccountsCode) {
133        this.chartOfAccountsCode = chartOfAccountsCode;
134    }
135
136
137    /**
138     * Gets the organizationCode attribute.
139     * 
140     * @return Returns the organizationCode
141     */
142    public String getOrganizationCode() {
143        return organizationCode;
144    }
145
146    /**
147     * Sets the organizationCode attribute.
148     * 
149     * @param organizationCode The organizationCode to set.
150     */
151    public void setOrganizationCode(String organizationCode) {
152        this.organizationCode = organizationCode;
153    }
154
155
156    /**
157     * Gets the accountDelegateModelName attribute.
158     * 
159     * @return Returns the accountDelegateModelName
160     */
161    public String getAccountDelegateModelName() {
162        return accountDelegateModelName;
163    }
164
165    /**
166     * Sets the accountDelegateModelName attribute.
167     * 
168     * @param accountDelegateModelName The accountDelegateModelName to set.
169     */
170    public void setAccountDelegateModelName(String organizationRoutingModelName) {
171        this.accountDelegateModelName = organizationRoutingModelName;
172    }
173
174
175    /**
176     * Gets the accountDelegateUniversalId attribute.
177     * 
178     * @return Returns the accountDelegateUniversalId
179     */
180    public String getAccountDelegateUniversalId() {
181        return accountDelegateUniversalId;
182    }
183
184    /**
185     * Sets the accountDelegateUniversalId attribute.
186     * 
187     * @param accountDelegateUniversalId The accountDelegateUniversalId to set.
188     */
189    public void setAccountDelegateUniversalId(String accountDelegateUniversalId) {
190        this.accountDelegateUniversalId = accountDelegateUniversalId;
191    }
192
193
194    /**
195     * Gets the accountDelegate attribute.
196     * 
197     * @return Returns the accountDelegate.
198     */
199    public Person getAccountDelegate() {
200        accountDelegate = SpringContext.getBean(org.kuali.rice.kim.api.identity.PersonService.class).updatePersonIfNecessary(accountDelegateUniversalId, accountDelegate);
201        return accountDelegate;
202    }
203
204    /**
205     * Sets the accountDelegate attribute value.
206     * 
207     * @param accountDelegate The accountDelegate to set.
208     */
209    public void setAccountDelegate(Person accountDelegate) {
210        this.accountDelegate = accountDelegate;
211    }
212
213    /**
214     * Gets the financialDocumentTypeCode attribute.
215     * 
216     * @return Returns the financialDocumentTypeCode
217     */
218    public String getFinancialDocumentTypeCode() {
219        return financialDocumentTypeCode;
220    }
221
222    /**
223     * Sets the financialDocumentTypeCode attribute.
224     * 
225     * @param financialDocumentTypeCode The financialDocumentTypeCode to set.
226     */
227    public void setFinancialDocumentTypeCode(String financialDocumentTypeCode) {
228        this.financialDocumentTypeCode = financialDocumentTypeCode;
229    }
230
231    /**
232     * Gets the financialSystemDocumentTypeCode attribute. 
233     * @return Returns the financialSystemDocumentTypeCode.
234     */
235    public DocumentTypeEBO getFinancialSystemDocumentTypeCode() {
236        if ( StringUtils.isBlank( financialDocumentTypeCode ) ) {
237            financialSystemDocumentTypeCode = null;
238        } else {
239            if ( financialSystemDocumentTypeCode == null || !StringUtils.equals(financialDocumentTypeCode, financialSystemDocumentTypeCode.getName() ) ) {
240                org.kuali.rice.kew.api.doctype.DocumentType temp = SpringContext.getBean(DocumentTypeService.class).getDocumentTypeByName(financialDocumentTypeCode);
241                if ( temp != null ) {
242                    financialSystemDocumentTypeCode = DocumentType.from( temp );
243                } else {
244                    financialSystemDocumentTypeCode = null;
245                }
246            }
247        }
248        return financialSystemDocumentTypeCode;
249    }
250
251    /**
252     * Gets the approvalFromThisAmount attribute.
253     * 
254     * @return Returns the approvalFromThisAmount
255     */
256    public KualiDecimal getApprovalFromThisAmount() {
257        return approvalFromThisAmount;
258    }
259
260    /**
261     * Sets the approvalFromThisAmount attribute.
262     * 
263     * @param approvalFromThisAmount The approvalFromThisAmount to set.
264     */
265    public void setApprovalFromThisAmount(KualiDecimal approvalFromThisAmount) {
266        this.approvalFromThisAmount = approvalFromThisAmount;
267    }
268
269
270    /**
271     * Gets the approvalToThisAmount attribute.
272     * 
273     * @return Returns the approvalToThisAmount
274     */
275    public KualiDecimal getApprovalToThisAmount() {
276        return approvalToThisAmount;
277    }
278
279    /**
280     * Sets the approvalToThisAmount attribute.
281     * 
282     * @param approvalToThisAmount The approvalToThisAmount to set.
283     */
284    public void setApprovalToThisAmount(KualiDecimal approvalToThisAmount) {
285        this.approvalToThisAmount = approvalToThisAmount;
286    }
287
288
289    /**
290     * Gets the accountDelegatePrimaryRoutingIndicator attribute.
291     * 
292     * @return Returns the accountDelegatePrimaryRoutingIndicator
293     */
294    public boolean getAccountDelegatePrimaryRoutingIndicator() {
295        return accountDelegatePrimaryRoutingIndicator;
296    }
297
298    /**
299     * Sets the accountDelegatePrimaryRoutingIndicator attribute.
300     * 
301     * @param accountDelegatePrimaryRoutingCode The accountDelegatePrimaryRoutingIndicator to set.
302     */
303    public void setAccountDelegatePrimaryRoutingIndicator(boolean accountDelegatePrimaryRoutingCode) {
304        this.accountDelegatePrimaryRoutingIndicator = accountDelegatePrimaryRoutingCode;
305    }
306
307
308    /**
309     * Gets the accountDelegateStartDate attribute.
310     * 
311     * @return Returns the accountDelegateStartDate
312     */
313    public Date getAccountDelegateStartDate() {
314        if(this.accountDelegateStartDate == null){
315            String dateString = null;
316            accountDelegateStartDate =  SpringContext.getBean(DateTimeService.class).getCurrentDate();
317            accountDelegateStartDate =  DateUtils.truncate(this.accountDelegateStartDate, Calendar.DAY_OF_MONTH);
318            dateString = DateFormatUtils.format(accountDelegateStartDate, "MM/dd/yyyy");
319            DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
320            try {
321                this.accountDelegateStartDate = df.parse(dateString);
322            } catch (ParseException ex) {
323                ex.printStackTrace();
324            }
325        }
326        return this.accountDelegateStartDate;
327    }
328
329    /**
330     * Sets the accountDelegateStartDate attribute.
331     * 
332     * @param accountDelegateStartDate The accountDelegateStartDate to set.
333     */
334    public void setAccountDelegateStartDate(Date accountDelegateStartDate) {
335        this.accountDelegateStartDate = accountDelegateStartDate;
336    }
337
338    /**
339     * Gets the active attribute.
340     * 
341     * @return Returns the active.
342     */
343    @Override
344    public boolean isActive() {
345        return active;
346    }
347
348    /**
349     * Sets the active attribute value.
350     * 
351     * @param active The active to set.
352     */
353    public void setActive(boolean active) {
354        this.active = active;
355    }
356
357    /**
358     * Gets the chartOfAccounts attribute.
359     * 
360     * @return Returns the chartOfAccounts
361     */
362    public Chart getChartOfAccounts() {
363        return chartOfAccounts;
364    }
365
366    /**
367     * Sets the chartOfAccounts attribute.
368     * 
369     * @param chartOfAccounts The chartOfAccounts to set.
370     * @deprecated
371     */
372    public void setChartOfAccounts(Chart chartOfAccounts) {
373        this.chartOfAccounts = chartOfAccounts;
374    }
375
376    /**
377     * @see org.kuali.rice.krad.bo.BusinessObjectBase#toStringMapper()
378     */
379    protected LinkedHashMap toStringMapper() {
380        LinkedHashMap m = new LinkedHashMap();
381        m.put("chartOfAccountsCode", this.chartOfAccountsCode);
382        m.put("organizationCode", this.organizationCode);
383        m.put("accountDelegateModelName", this.accountDelegateModelName);
384        m.put("accountDelegateUniversalId", this.accountDelegateUniversalId);
385        m.put("financialDocumentTypeCode", this.financialDocumentTypeCode);
386        return m;
387    }
388
389    public static final class AccountDelegateModelDetailId implements Serializable, Comparable<AccountDelegateModelDetailId> {
390
391        private String chartOfAccountsCode;
392
393        private String organizationCode;
394
395        private String accountDelegateModelName;
396
397        private String accountDelegateUniversalId;
398
399        private String financialDocumentTypeCode;
400
401        public String getChartOfAccountsCode() {
402            return this.chartOfAccountsCode;
403        }
404
405        public void setChartOfAccountsCode(String chartOfAccountsCode) {
406            this.chartOfAccountsCode = chartOfAccountsCode;
407        }
408
409        public String getOrganizationCode() {
410            return this.organizationCode;
411        }
412
413        public void setOrganizationCode(String organizationCode) {
414            this.organizationCode = organizationCode;
415        }
416
417        public String getAccountDelegateModelName() {
418            return this.accountDelegateModelName;
419        }
420
421        public void setAccountDelegateModelName(String accountDelegateModelName) {
422            this.accountDelegateModelName = accountDelegateModelName;
423        }
424
425        public String getAccountDelegateUniversalId() {
426            return this.accountDelegateUniversalId;
427        }
428
429        public void setAccountDelegateUniversalId(String accountDelegateUniversalId) {
430            this.accountDelegateUniversalId = accountDelegateUniversalId;
431        }
432
433        public String getFinancialDocumentTypeCode() {
434            return this.financialDocumentTypeCode;
435        }
436
437        public void setFinancialDocumentTypeCode(String financialDocumentTypeCode) {
438            this.financialDocumentTypeCode = financialDocumentTypeCode;
439        }
440
441        @Override
442        public String toString() {
443            return new ToStringBuilder(this).append("chartOfAccountsCode", this.chartOfAccountsCode).append("organizationCode", this.organizationCode).append("accountDelegateModelName", this.accountDelegateModelName).append("accountDelegateUniversalId", this.accountDelegateUniversalId).append("financialDocumentTypeCode", this.financialDocumentTypeCode).toString();
444        }
445
446        @Override
447        public boolean equals(Object other) {
448            if (other == null)
449                return false;
450            if (other == this)
451                return true;
452            if (other.getClass() != this.getClass())
453                return false;
454            final AccountDelegateModelDetailId rhs = (AccountDelegateModelDetailId) other;
455            return new EqualsBuilder().append(this.chartOfAccountsCode, rhs.chartOfAccountsCode).append(this.organizationCode, rhs.organizationCode).append(this.accountDelegateModelName, rhs.accountDelegateModelName).append(this.accountDelegateUniversalId, rhs.accountDelegateUniversalId).append(this.financialDocumentTypeCode, rhs.financialDocumentTypeCode).isEquals();
456        }
457
458        @Override
459        public int hashCode() {
460            return new HashCodeBuilder(17, 37).append(this.chartOfAccountsCode).append(this.organizationCode).append(this.accountDelegateModelName).append(this.accountDelegateUniversalId).append(this.financialDocumentTypeCode).toHashCode();
461        }
462
463        @Override
464        public int compareTo(AccountDelegateModelDetailId other) {
465            return new CompareToBuilder().append(this.chartOfAccountsCode, other.chartOfAccountsCode).append(this.organizationCode, other.organizationCode).append(this.accountDelegateModelName, other.accountDelegateModelName).append(this.accountDelegateUniversalId, other.accountDelegateUniversalId).append(this.financialDocumentTypeCode, other.financialDocumentTypeCode).toComparison();
466        }
467    }
468}