001/*
002 * The Kuali Financial System, a comprehensive financial management system for higher education.
003 * 
004 * Copyright 2005-2014 The Kuali Foundation
005 * 
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Affero General Public License as
008 * published by the Free Software Foundation, either version 3 of the
009 * License, or (at your option) any later version.
010 * 
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU Affero General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Affero General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.kuali.kfs.pdp.businessobject;
020
021import java.lang.reflect.Field;
022import java.util.LinkedHashMap;
023import java.util.List;
024
025import org.apache.commons.lang.StringUtils;
026import org.apache.commons.lang.builder.ReflectionToStringBuilder;
027import org.kuali.kfs.pdp.PdpConstants.PayeeIdTypeCodes;
028import org.kuali.kfs.pdp.PdpPropertyConstants;
029import org.kuali.kfs.sys.businessobject.TimestampedBusinessObjectBase;
030import org.kuali.kfs.sys.context.SpringContext;
031import org.kuali.kfs.sys.service.FinancialSystemUserService;
032import org.kuali.kfs.vnd.businessobject.VendorDetail;
033import org.kuali.kfs.vnd.document.service.VendorService;
034import org.kuali.rice.core.api.mo.common.active.MutableInactivatable;
035import org.kuali.rice.core.api.util.type.KualiInteger;
036import org.kuali.rice.kim.api.identity.Person;
037import org.kuali.rice.kim.api.identity.PersonService;
038import org.kuali.rice.kim.api.identity.entity.EntityDefault;
039import org.kuali.rice.kim.api.identity.principal.Principal;
040import org.kuali.rice.kim.api.services.KimApiServiceLocator;
041import org.kuali.rice.krad.bo.BusinessObject;
042import org.kuali.rice.krad.datadictionary.AttributeSecurity;
043import org.kuali.rice.krad.service.DataDictionaryService;
044import org.kuali.rice.krad.util.ObjectUtils;
045
046public class PayeeACHAccount extends TimestampedBusinessObjectBase implements MutableInactivatable {
047
048    private KualiInteger achAccountGeneratedIdentifier;
049    private String bankRoutingNumber;
050    private String bankAccountNumber;
051    private String payeeIdNumber;
052    private String payeeName;
053    private String payeeEmailAddress;
054    private String payeeIdentifierTypeCode;
055    private String achTransactionType;
056    private String bankAccountTypeCode;
057    private boolean active;
058    private boolean autoInactivationIndicator;
059
060
061    private ACHBank bankRouting;
062    private ACHTransactionType transactionType;
063    private ACHPayee achPayee;
064
065    /**
066     * Default constructor.
067     */
068    public PayeeACHAccount() {
069
070    }
071
072    /**
073     * Gets the achAccountGeneratedIdentifier attribute.
074     *
075     * @return Returns the achAccountGeneratedIdentifier
076     */
077    public KualiInteger getAchAccountGeneratedIdentifier() {
078        return achAccountGeneratedIdentifier;
079    }
080
081    /**
082     * Sets the achAccountGeneratedIdentifier attribute.
083     *
084     * @param achAccountGeneratedIdentifier The achAccountGeneratedIdentifier to set.
085     */
086    public void setAchAccountGeneratedIdentifier(KualiInteger achAccountGeneratedIdentifier) {
087        this.achAccountGeneratedIdentifier = achAccountGeneratedIdentifier;
088    }
089
090
091    /**
092     * Gets the bankRoutingNumber attribute.
093     *
094     * @return Returns the bankRoutingNumber
095     */
096    public String getBankRoutingNumber() {
097        return bankRoutingNumber;
098    }
099
100    /**
101     * Sets the bankRoutingNumber attribute.
102     *
103     * @param bankRoutingNumber The bankRoutingNumber to set.
104     */
105    public void setBankRoutingNumber(String bankRoutingNumber) {
106        this.bankRoutingNumber = bankRoutingNumber;
107    }
108
109
110    /**
111     * Gets the bankAccountNumber attribute.
112     *
113     * @return Returns the bankAccountNumber
114     */
115    public String getBankAccountNumber() {
116        return bankAccountNumber;
117    }
118
119    /**
120     * Sets the bankAccountNumber attribute.
121     *
122     * @param bankAccountNumber The bankAccountNumber to set.
123     */
124    public void setBankAccountNumber(String bankAccountNumber) {
125        this.bankAccountNumber = bankAccountNumber;
126    }
127
128    /**
129     * Gets the payee's name from KIM or Vendor data, if the payee type is Employee, Entity or Vendor; otherwise returns the stored
130     * field value.
131     *
132     * @return Returns the payee name
133     */
134    public String getPayeeName() {
135        // for Employee, retrieves from Person table by employee ID
136        if (StringUtils.equalsIgnoreCase(payeeIdentifierTypeCode, PayeeIdTypeCodes.EMPLOYEE)) {
137            if (ObjectUtils.isNotNull(payeeIdNumber)) {
138                String name = SpringContext.getBean(FinancialSystemUserService.class).getPersonNameByEmployeeId(payeeIdNumber);
139
140                // Person person = SpringContext.getBean(PersonService.class).getPersonByEmployeeId(payeeIdNumber);
141                if (ObjectUtils.isNotNull(name)) {
142                    return name;
143                }
144            }
145        }
146        // for Entity, retrieve from Entity table by entity ID
147        else if (StringUtils.equalsIgnoreCase(payeeIdentifierTypeCode, PayeeIdTypeCodes.ENTITY)) {
148            if (ObjectUtils.isNotNull(payeeIdNumber)) {
149                EntityDefault entity = KimApiServiceLocator.getIdentityService().getEntityDefault(payeeIdNumber);
150                if (ObjectUtils.isNotNull(entity) && ObjectUtils.isNotNull(entity.getName())) {
151                    return entity.getName().getCompositeName();
152                }
153            }
154        }
155        // for Vendor, retrieves from Vendor table by vendor number
156        else if (StringUtils.equalsIgnoreCase(payeeIdentifierTypeCode, PayeeIdTypeCodes.VENDOR_ID)) {
157            VendorDetail vendor = SpringContext.getBean(VendorService.class).getVendorDetail(payeeIdNumber);
158            if (ObjectUtils.isNotNull(vendor)) {
159                return vendor.getVendorName();
160            }
161        }
162
163        // otherwise return field value
164        return payeeName;
165    }
166
167    /**
168     * Sets the payeeName attribute.
169     *
170     * @param payeeName The payeeName to set.
171     */
172    public void setPayeeName(String payeeName) {
173        this.payeeName = payeeName;
174    }
175
176    /**
177     * Gets the payee's email address from KIM data if the payee type is Employee or Entity; otherwise, returns the stored field
178     * value.
179     *
180     * @return Returns the payeeEmailAddress
181     */
182    public String getPayeeEmailAddress() {
183        // for Employee, retrieve from Person table by employee ID
184        if (StringUtils.equalsIgnoreCase(payeeIdentifierTypeCode, PayeeIdTypeCodes.EMPLOYEE)) {
185            Person person = SpringContext.getBean(PersonService.class).getPersonByEmployeeId(payeeIdNumber);
186            if (ObjectUtils.isNotNull(person)) {
187                return person.getEmailAddress();
188            }
189        }
190        // for Entity, retrieve from Entity table by entity ID then from Person table
191        else if (StringUtils.equalsIgnoreCase(payeeIdentifierTypeCode, PayeeIdTypeCodes.ENTITY)) {
192            if (ObjectUtils.isNotNull(payeeIdNumber)) {
193                EntityDefault entity = KimApiServiceLocator.getIdentityService().getEntityDefault(payeeIdNumber);
194                if (ObjectUtils.isNotNull(entity)) {
195                    List<Principal> principals = entity.getPrincipals();
196                    if (principals.size() > 0 && ObjectUtils.isNotNull(principals.get(0))) {
197                        String principalId = principals.get(0).getPrincipalId();
198                        Person person = SpringContext.getBean(PersonService.class).getPerson(principalId);
199                        if (ObjectUtils.isNotNull(person)) {
200                            return person.getEmailAddress();
201                        }
202                    }
203                }
204            }
205        }
206
207        // otherwise returns the field value
208        return payeeEmailAddress;
209    }
210
211    /**
212     * Sets the payeeEmailAddress attribute if the payee is not Employee or Entity.
213     *
214     * @param payeeEmailAddress The payeeEmailAddress to set.
215     */
216    public void setPayeeEmailAddress(String payeeEmailAddress) {
217        this.payeeEmailAddress = payeeEmailAddress;
218    }
219
220    /**
221     * Gets the payeeIdentifierTypeCode attribute.
222     *
223     * @return Returns the payeeIdentifierTypeCode
224     */
225    public String getPayeeIdentifierTypeCode() {
226        return payeeIdentifierTypeCode;
227    }
228
229    /**
230     * Sets the payeeIdentifierTypeCode attribute.
231     *
232     * @param payeeIdentifierTypeCode The payeeIdentifierTypeCode to set.
233     */
234    public void setPayeeIdentifierTypeCode(String payeeIdentifierTypeCode) {
235        this.payeeIdentifierTypeCode = payeeIdentifierTypeCode;
236    }
237
238    /**
239     * Gets the achTransactionType attribute.
240     *
241     * @return Returns the achTransactionType.
242     */
243    public String getAchTransactionType() {
244        return achTransactionType;
245    }
246
247    /**
248     * Sets the achTransactionType attribute value.
249     *
250     * @param achTransactionType The achTransactionType to set.
251     */
252    public void setAchTransactionType(String achTransactionType) {
253        this.achTransactionType = achTransactionType;
254    }
255
256    /**
257     * Gets the transactionType attribute.
258     *
259     * @return Returns the transactionType.
260     */
261    public ACHTransactionType getTransactionType() {
262        return transactionType;
263    }
264
265    /**
266     * Sets the transactionType attribute value.
267     *
268     * @param transactionType The transactionType to set.
269     */
270    public void setTransactionType(ACHTransactionType transactionType) {
271        this.transactionType = transactionType;
272    }
273
274    /**
275     * Gets the active attribute.
276     *
277     * @return Returns the active
278     */
279    @Override
280    public boolean isActive() {
281        return active;
282    }
283
284    /**
285     * Sets the active attribute.
286     *
287     * @param active The active to set.
288     */
289    @Override
290    public void setActive(boolean active) {
291        this.active = active;
292    }
293
294    public boolean isAutoInactivationIndicator() {
295        return autoInactivationIndicator;
296    }
297
298    public void setAutoInactivationIndicator(boolean autoInactivationIndicator) {
299        this.autoInactivationIndicator = autoInactivationIndicator;
300    }
301
302    /**
303     * Gets the bankAccountTypeCode attribute.
304     *
305     * @return Returns the bankAccountTypeCode.
306     */
307    public String getBankAccountTypeCode() {
308        return bankAccountTypeCode;
309    }
310
311    /**
312     * Sets the bankAccountTypeCode attribute value.
313     *
314     * @param bankAccountTypeCode The bankAccountTypeCode to set.
315     */
316    public void setBankAccountTypeCode(String bankAccountTypeCode) {
317        this.bankAccountTypeCode = bankAccountTypeCode;
318    }
319
320    /**
321     * Gets the bankRouting attribute.
322     *
323     * @return Returns the bankRouting.
324     */
325    public ACHBank getBankRouting() {
326        return bankRouting;
327    }
328
329    /**
330     * Sets the bankRouting attribute value.
331     *
332     * @param bankRouting The bankRouting to set.
333     * @deprecated
334     */
335    @Deprecated
336    public void setBankRouting(ACHBank bankRouting) {
337        this.bankRouting = bankRouting;
338    }
339
340
341    /**
342     * Gets the payeeIdNumber attribute.
343     *
344     * @return Returns the payeeIdNumber.
345     */
346    public String getPayeeIdNumber() {
347        return payeeIdNumber;
348    }
349
350    /**
351     * Sets the payeeIdNumber attribute value.
352     *
353     * @param payeeIdNumber The payeeIdNumber to set.
354     */
355    public void setPayeeIdNumber(String payeeIdNumber) {
356        this.payeeIdNumber = payeeIdNumber;
357    }
358
359
360    /**
361     * Gets the achPayee attribute.
362     *
363     * @return Returns the achPayee.
364     */
365    public ACHPayee getAchPayee() {
366        return achPayee;
367    }
368
369    /**
370     * Sets the achPayee attribute value.
371     *
372     * @param achPayee The achPayee to set.
373     */
374    public void setAchPayee(ACHPayee achPayee) {
375        this.achPayee = achPayee;
376    }
377
378    /**
379     * @see org.kuali.rice.krad.bo.BusinessObjectBase#toStringMapper()
380     */
381    protected LinkedHashMap toStringMapper_RICE20_REFACTORME() {
382        LinkedHashMap m = new LinkedHashMap();
383        if (this.achAccountGeneratedIdentifier != null) {
384            m.put(PdpPropertyConstants.ACH_ACCOUNT_GENERATED_IDENTIFIER, this.achAccountGeneratedIdentifier.toString());
385        }
386        return m;
387    }
388
389    /**
390     * KFSCNTRB-1682: Some of the fields contain confidential information
391     * @see org.kuali.rice.krad.bo.BusinessObjectBase#toString()
392     */
393    @Override
394    public String toString() {
395        class PayeeACHAccountToStringBuilder extends ReflectionToStringBuilder {
396            private PayeeACHAccountToStringBuilder(Object object) {
397                super(object);
398            }
399
400            @Override
401            public boolean accept(Field field) {
402                if (BusinessObject.class.isAssignableFrom(field.getType())) {
403                    return false;
404                }
405
406                DataDictionaryService dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
407                AttributeSecurity attributeSecurity = dataDictionaryService.getAttributeSecurity(PayeeACHAccount.class.getName(), field.getName());
408                if ((ObjectUtils.isNotNull(attributeSecurity)
409                        && (attributeSecurity.isHide() || attributeSecurity.isMask() || attributeSecurity.isPartialMask()))) {
410                    return false;
411                }
412
413                return super.accept(field);
414            }
415        };
416        ReflectionToStringBuilder toStringBuilder = new PayeeACHAccountToStringBuilder(this);
417        return toStringBuilder.toString();
418    }
419}