View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 1.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.pdp.batch.service.impl;
17  
18  import java.util.Date;
19  import java.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.ole.pdp.PdpConstants.PayeeIdTypeCodes;
23  import org.kuali.ole.pdp.batch.service.InactivatePayeeAchAccountsService;
24  import org.kuali.ole.pdp.businessobject.PayeeACHAccount;
25  import org.kuali.ole.pdp.service.AchService;
26  import org.kuali.ole.sys.service.ReportWriterService;
27  import org.kuali.ole.vnd.businessobject.VendorDetail;
28  import org.kuali.ole.vnd.document.service.VendorService;
29  import org.kuali.rice.core.api.datetime.DateTimeService;
30  import org.kuali.rice.kim.api.identity.Person;
31  import org.kuali.rice.kim.api.identity.PersonService;
32  import org.kuali.rice.kim.api.identity.entity.EntityDefault;
33  import org.kuali.rice.kim.api.services.IdentityManagementService;
34  import org.kuali.rice.krad.service.BusinessObjectService;
35  import org.kuali.rice.krad.util.ObjectUtils;
36  
37  /**
38   * Implementation for InactivatePayeeAchAccountsService interface.
39   */
40  public class InactivatePayeeAchAccountsServiceImpl implements InactivatePayeeAchAccountsService {
41      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(InactivatePayeeAchAccountsServiceImpl.class);
42  
43      private BusinessObjectService businessObjectService;
44      private DateTimeService dateTimeService;
45      private IdentityManagementService identityManagementService;
46      private PersonService personService;
47      private VendorService vendorService;
48      private AchService achService;
49      private ReportWriterService reportWriterService;
50  
51      /**
52       * @see rg.kuali.kfs.pdp.batch.service.#inactivatePayeeAchAccounts()
53       */
54      @Override
55      public boolean inactivatePayeeAchAccounts() {
56          LOG.info("Retrieving currently active Payee ACH Accounts ...");
57          List<PayeeACHAccount> accounts = achService.getActiveAchAccounts();
58  
59          LOG.info("Inactivating ACH Accounts for inactive Payees and writing to the report ...");
60          reportWriterService.writeTableHeader(PayeeACHAccount.class);
61          int countEmployee = 0;
62          int countEntity = 0;
63          int countVendor = 0;
64          Date currentDate = dateTimeService.getCurrentDate();
65  
66          for (PayeeACHAccount account : accounts) {
67              String idType = account.getPayeeIdentifierTypeCode();
68              String idNumber = account.getPayeeIdNumber();
69  
70              // for Employee, retrieve from Person table by employee ID
71              if (StringUtils.equalsIgnoreCase(idType, PayeeIdTypeCodes.EMPLOYEE)) {
72                  Person person = personService.getPersonByEmployeeId(idNumber);
73                  // inactivate the account if the person doesn't exist anymore or is inactive
74                  if (ObjectUtils.isNull(person) || !person.isActive()) {
75                      LOG.info("Inactivating Payee ACH account for employee with ID # " + idNumber);
76                      account.setActive(false);
77                      businessObjectService.save(account);
78                      countEmployee++;
79                      reportWriterService.writeTableRow(account);
80                  }
81              }
82              // for Entity, retrieve from Entity table by entity ID
83              else if (StringUtils.equalsIgnoreCase(idType, PayeeIdTypeCodes.ENTITY)) {
84                  EntityDefault entity = identityManagementService.getEntityDefaultInfo(idNumber);
85                  // inactivate the account if the entity doesn't exist anymore or is inactive
86                  if (ObjectUtils.isNull(entity) || !entity.isActive()) {
87                      LOG.info("Inactivating Payee ACH account for entity with ID # " + idNumber);
88                      account.setActive(false);
89                      businessObjectService.save(account);
90                      countEntity++;
91                      reportWriterService.writeTableRow(account);
92                  }
93              }
94              // for Vendor, retrieve from Vendor table by vendor number
95              else if (StringUtils.equalsIgnoreCase(idType, PayeeIdTypeCodes.VENDOR_ID)) {
96                  /*
97                  // in PayeeACHAccount table, in case the vendor number only refers to the headerId,
98                  // with default detailId being 0, we need to add the default detailId to the vendor number
99                  String vendorNumber = idNumber;
100                 if (!StringUtils.contains(idNumber, VendorConstants.DASH)) {
101                     vendorNumber += VendorConstants.DASH + VendorConstants.DEFAULT_VENDOR_DETAIL_ID;
102                 }
103                 VendorDetail vendor = vendorService.getVendorDetail(vendorNumber);
104                 */
105                 VendorDetail vendor = vendorService.getVendorDetail(idNumber);
106                 // inactivate the account if the vendor doesn't exist anymore or is inactive
107                 if (ObjectUtils.isNull(vendor) || !vendor.isActiveIndicator()) {
108                     LOG.info("Inactivating Payee ACH account for vendor with vendor # " + idNumber);
109                     account.setActive(false);
110                     businessObjectService.save(account);
111                     countVendor++;
112                     reportWriterService.writeTableRow(account);
113                 }
114             }
115         }
116 
117         LOG.info("Total number of Employee Payee ACH accounts inacticated:" + countEmployee);
118         LOG.info("Total number of Entity Payee ACH accounts inacticated:" + countEntity);
119         LOG.info("Total number of Vendor Payee ACH accounts inacticated:" + countVendor);
120 
121         reportWriterService.writeStatisticLine("%s %s", "TOTAL ACTIVE ACCOUNTS BEFORE RUNNING THE JOB:", accounts.size());
122         reportWriterService.writeStatisticLine("%s %s", "TOTAL ACCOUNTS INACTIVATED FOR EMPLOYEES:    ", countEmployee);
123         reportWriterService.writeStatisticLine("%s %s", "TOTAL ACCOUNTS INACTIVATED FOR ENTITIES:     ", countEntity);
124         reportWriterService.writeStatisticLine("%s %s", "TOTAL ACCOUNTS INACTIVATED FOR VENDORS:      ", countVendor);
125         reportWriterService.writeStatisticLine("%s %s", "ACCOUNTS INACTIVATION DATE:                  ", currentDate);
126 
127         return true;
128     }
129 
130     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
131         this.businessObjectService = businessObjectService;
132     }
133 
134     public void setDateTimeService(DateTimeService dateTimeService) {
135         this.dateTimeService = dateTimeService;
136     }
137 
138     public void setIdentityManagementService(IdentityManagementService identityManagementService) {
139         this.identityManagementService = identityManagementService;
140     }
141 
142     public void setPersonService(PersonService personService) {
143         this.personService = personService;
144     }
145 
146     public void setVendorService(VendorService vendorService) {
147         this.vendorService = vendorService;
148     }
149 
150     public void setAchService(AchService achService) {
151         this.achService = achService;
152     }
153 
154     public void setReportWriterService(ReportWriterService reportWriterService) {
155         this.reportWriterService = reportWriterService;
156     }
157 
158 }