001/*
002 * Copyright 2007 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 */
016package org.kuali.ole.pdp.businessobject;
017
018import java.util.LinkedHashMap;
019import java.util.List;
020
021import org.apache.commons.lang.StringUtils;
022import org.kuali.ole.pdp.PdpPropertyConstants;
023import org.kuali.ole.pdp.PdpConstants.PayeeIdTypeCodes;
024import org.kuali.ole.sys.context.SpringContext;
025import org.kuali.ole.vnd.businessobject.VendorDetail;
026import org.kuali.ole.vnd.document.service.VendorService;
027import org.kuali.rice.core.api.mo.common.active.MutableInactivatable;
028import org.kuali.rice.core.api.util.type.KualiInteger;
029import org.kuali.rice.kim.api.identity.Person;
030import org.kuali.rice.kim.api.identity.PersonService;
031import org.kuali.rice.kim.api.identity.entity.EntityDefault;
032import org.kuali.rice.kim.api.identity.principal.Principal;
033import org.kuali.rice.kim.api.services.KimApiServiceLocator;
034import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
035import org.kuali.rice.krad.util.ObjectUtils;
036
037public class PayeeACHAccount extends PersistableBusinessObjectBase implements MutableInactivatable {
038
039    private KualiInteger achAccountGeneratedIdentifier;
040    private String bankRoutingNumber;
041    private String bankAccountNumber;
042    private String payeeIdNumber;
043    private String payeeName;
044    private String payeeEmailAddress;
045    private String payeeIdentifierTypeCode;
046    private String achTransactionType;
047    private String bankAccountTypeCode;
048    private boolean active;
049
050    private ACHBank bankRouting;
051    private ACHTransactionType transactionType;
052    private ACHPayee achPayee;
053
054    /**
055     * Default constructor.
056     */
057    public PayeeACHAccount() {
058
059    }
060
061    /**
062     * Gets the achAccountGeneratedIdentifier attribute.
063     * 
064     * @return Returns the achAccountGeneratedIdentifier
065     */
066    public KualiInteger getAchAccountGeneratedIdentifier() {
067        return achAccountGeneratedIdentifier;
068    }
069
070    /**
071     * Sets the achAccountGeneratedIdentifier attribute.
072     * 
073     * @param achAccountGeneratedIdentifier The achAccountGeneratedIdentifier to set.
074     */
075    public void setAchAccountGeneratedIdentifier(KualiInteger achAccountGeneratedIdentifier) {
076        this.achAccountGeneratedIdentifier = achAccountGeneratedIdentifier;
077    }
078
079
080    /**
081     * Gets the bankRoutingNumber attribute.
082     * 
083     * @return Returns the bankRoutingNumber
084     */
085    public String getBankRoutingNumber() {
086        return bankRoutingNumber;
087    }
088
089    /**
090     * Sets the bankRoutingNumber attribute.
091     * 
092     * @param bankRoutingNumber The bankRoutingNumber to set.
093     */
094    public void setBankRoutingNumber(String bankRoutingNumber) {
095        this.bankRoutingNumber = bankRoutingNumber;
096    }
097
098
099    /**
100     * Gets the bankAccountNumber attribute.
101     * 
102     * @return Returns the bankAccountNumber
103     */
104    public String getBankAccountNumber() {
105        return bankAccountNumber;
106    }
107
108    /**
109     * Sets the bankAccountNumber attribute.
110     * 
111     * @param bankAccountNumber The bankAccountNumber to set.
112     */
113    public void setBankAccountNumber(String bankAccountNumber) {
114        this.bankAccountNumber = bankAccountNumber;
115    }
116
117    /**
118     * Gets the payee's name from KIM or Vendor data, if the payee type is Employee, Entity or Vendor;
119     * otherwise returns the stored field value.
120     * 
121     * @return Returns the payee name
122     */
123    public String getPayeeName() {
124        // for Employee, retrieves from Person table by employee ID
125        if (StringUtils.equalsIgnoreCase(payeeIdentifierTypeCode, PayeeIdTypeCodes.EMPLOYEE)) {
126            Person person = SpringContext.getBean(PersonService.class).getPersonByEmployeeId(payeeIdNumber);
127            if (ObjectUtils.isNotNull(person)) {
128                return person.getName();
129            }
130        }
131        // for Entity, retrieve from Entity table by entity ID
132        else if (StringUtils.equalsIgnoreCase(payeeIdentifierTypeCode, PayeeIdTypeCodes.ENTITY)) {
133            if(ObjectUtils.isNotNull(payeeIdNumber)) {
134                EntityDefault entity = KimApiServiceLocator.getIdentityService().getEntityDefault(payeeIdNumber);
135                if (ObjectUtils.isNotNull(entity) && ObjectUtils.isNotNull(entity.getName())) {
136                    return entity.getName().getCompositeName();
137                }
138            }
139        }
140        // for Vendor, retrieves from Vendor table by vendor number
141        else if (StringUtils.equalsIgnoreCase(payeeIdentifierTypeCode, PayeeIdTypeCodes.VENDOR_ID)) {
142            VendorDetail vendor = SpringContext.getBean(VendorService.class).getVendorDetail(payeeIdNumber);
143            if (ObjectUtils.isNotNull(vendor)) {
144                return vendor.getVendorName();
145            }
146        }
147
148        // otherwise return field value
149        return payeeName;
150    }
151
152    /**
153     * Sets the payeeName attribute.
154     * 
155     * @param payeeName The payeeName to set.
156     */
157    public void setPayeeName(String payeeName) {
158        this.payeeName = payeeName;
159    }
160
161    /**
162     * Gets the payee's email address from KIM data if the payee type is Employee or Entity;
163     * otherwise, returns the stored field value.
164     * 
165     * @return Returns the payeeEmailAddress
166     */
167    public String getPayeeEmailAddress() {
168        // for Employee, retrieve from Person table by employee ID
169        if (StringUtils.equalsIgnoreCase(payeeIdentifierTypeCode, PayeeIdTypeCodes.EMPLOYEE)) {
170            Person person = SpringContext.getBean(PersonService.class).getPersonByEmployeeId(payeeIdNumber);
171            if (ObjectUtils.isNotNull(person)) {
172                return person.getEmailAddress();
173            }
174        }
175        // for Entity, retrieve from Entity table by entity ID then from Person table
176        else if (StringUtils.equalsIgnoreCase(payeeIdentifierTypeCode, PayeeIdTypeCodes.ENTITY)) {
177            if(ObjectUtils.isNotNull(payeeIdNumber)) {
178                EntityDefault entity = KimApiServiceLocator.getIdentityService().getEntityDefault(payeeIdNumber);
179                if (ObjectUtils.isNotNull(entity)) {
180                    List<Principal> principals = entity.getPrincipals();
181                    if (principals.size() > 0 && ObjectUtils.isNotNull(principals.get(0))) {
182                        String principalId = principals.get(0).getPrincipalId();
183                        Person person = SpringContext.getBean(PersonService.class).getPerson(principalId);
184                        if (ObjectUtils.isNotNull(person)) {
185                            return person.getEmailAddress();
186                        }
187                    }
188                }
189            }
190        }
191
192        // otherwise returns the field value
193        return payeeEmailAddress;
194    }
195
196    /**
197     * Sets the payeeEmailAddress attribute if the payee is not Employee or Entity.
198     * 
199     * @param payeeEmailAddress The payeeEmailAddress to set.
200     */
201    public void setPayeeEmailAddress(String payeeEmailAddress) {
202        this.payeeEmailAddress = payeeEmailAddress;
203    }
204
205    /**
206     * Gets the payeeIdentifierTypeCode attribute.
207     * 
208     * @return Returns the payeeIdentifierTypeCode
209     */
210    public String getPayeeIdentifierTypeCode() {
211        return payeeIdentifierTypeCode;
212    }
213
214    /**
215     * Sets the payeeIdentifierTypeCode attribute.
216     * 
217     * @param payeeIdentifierTypeCode The payeeIdentifierTypeCode to set.
218     */
219    public void setPayeeIdentifierTypeCode(String payeeIdentifierTypeCode) {
220        this.payeeIdentifierTypeCode = payeeIdentifierTypeCode;
221    }
222
223    /**
224     * Gets the achTransactionType attribute.
225     * 
226     * @return Returns the achTransactionType.
227     */
228    public String getAchTransactionType() {
229        return achTransactionType;
230    }
231
232    /**
233     * Sets the achTransactionType attribute value.
234     * 
235     * @param achTransactionType The achTransactionType to set.
236     */
237    public void setAchTransactionType(String achTransactionType) {
238        this.achTransactionType = achTransactionType;
239    }
240
241    /**
242     * Gets the transactionType attribute.
243     * 
244     * @return Returns the transactionType.
245     */
246    public ACHTransactionType getTransactionType() {
247        return transactionType;
248    }
249
250    /**
251     * Sets the transactionType attribute value.
252     * 
253     * @param transactionType The transactionType to set.
254     */
255    public void setTransactionType(ACHTransactionType transactionType) {
256        this.transactionType = transactionType;
257    }
258
259    /**
260     * Gets the active attribute.
261     * 
262     * @return Returns the active
263     */
264    @Override
265    public boolean isActive() {
266        return active;
267    }
268
269    /**
270     * Sets the active attribute.
271     * 
272     * @param active The active to set.
273     */
274    @Override
275    public void setActive(boolean active) {
276        this.active = active;
277    }
278
279    /**
280     * Gets the bankAccountTypeCode attribute.
281     * 
282     * @return Returns the bankAccountTypeCode.
283     */
284    public String getBankAccountTypeCode() {
285        return bankAccountTypeCode;
286    }
287
288    /**
289     * Sets the bankAccountTypeCode attribute value.
290     * 
291     * @param bankAccountTypeCode The bankAccountTypeCode to set.
292     */
293    public void setBankAccountTypeCode(String bankAccountTypeCode) {
294        this.bankAccountTypeCode = bankAccountTypeCode;
295    }
296
297    /**
298     * Gets the bankRouting attribute.
299     * 
300     * @return Returns the bankRouting.
301     */
302    public ACHBank getBankRouting() {
303        return bankRouting;
304    }
305
306    /**
307     * Sets the bankRouting attribute value.
308     * 
309     * @param bankRouting The bankRouting to set.
310     * @deprecated
311     */
312    @Deprecated
313    public void setBankRouting(ACHBank bankRouting) {
314        this.bankRouting = bankRouting;
315    }
316
317
318    /**
319     * Gets the payeeIdNumber attribute.
320     * 
321     * @return Returns the payeeIdNumber.
322     */
323    public String getPayeeIdNumber() {
324        return payeeIdNumber;
325    }
326
327    /**
328     * Sets the payeeIdNumber attribute value.
329     * 
330     * @param payeeIdNumber The payeeIdNumber to set.
331     */
332    public void setPayeeIdNumber(String payeeIdNumber) {
333        this.payeeIdNumber = payeeIdNumber;
334    }
335
336
337    /**
338     * Gets the achPayee attribute.
339     * 
340     * @return Returns the achPayee.
341     */
342    public ACHPayee getAchPayee() {
343        return achPayee;
344    }
345
346    /**
347     * Sets the achPayee attribute value.
348     * 
349     * @param achPayee The achPayee to set.
350     */
351    public void setAchPayee(ACHPayee achPayee) {
352        this.achPayee = achPayee;
353    }
354
355    /**
356     * @see org.kuali.rice.krad.bo.BusinessObjectBase#toStringMapper()
357     */
358    protected LinkedHashMap toStringMapper_RICE20_REFACTORME() {
359        LinkedHashMap m = new LinkedHashMap();
360        if (this.achAccountGeneratedIdentifier != null) {
361            m.put(PdpPropertyConstants.ACH_ACCOUNT_GENERATED_IDENTIFIER, this.achAccountGeneratedIdentifier.toString());
362        }
363        return m;
364    }
365}