1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.ole.coa.document;
17  
18  import java.sql.Date;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.log4j.Logger;
25  import org.kuali.ole.coa.businessobject.Account;
26  import org.kuali.ole.coa.service.AccountPersistenceStructureService;
27  import org.kuali.ole.coa.service.AccountService;
28  import org.kuali.ole.coa.service.SubAccountTrickleDownInactivationService;
29  import org.kuali.ole.coa.service.SubObjectTrickleDownInactivationService;
30  import org.kuali.ole.sys.OLEPropertyConstants;
31  import org.kuali.ole.sys.context.SpringContext;
32  import org.kuali.ole.sys.document.FinancialSystemMaintainable;
33  import org.kuali.rice.core.api.datetime.DateTimeService;
34  import org.kuali.rice.kns.document.MaintenanceDocument;
35  import org.kuali.rice.krad.bo.PersistableBusinessObject;
36  import org.kuali.rice.krad.maintenance.MaintenanceLock;
37  import org.kuali.rice.krad.util.KRADConstants;
38  import org.kuali.rice.krad.util.ObjectUtils;
39  
40  
41  
42  
43  
44  
45  public class KualiAccountMaintainableImpl extends FinancialSystemMaintainable {
46      private static final Logger LOG = Logger.getLogger(KualiAccountMaintainableImpl.class);
47      private static final String ACCOUNT_GUIDE_LINE_PROPERTY = "accountGuideline";
48      
49      
50  
51  
52  
53  
54      @Override
55      public void saveBusinessObject() {
56          boolean isClosingAccount = isClosingAccount();
57  
58          
59          super.saveBusinessObject();
60  
61          
62          
63          if (isClosingAccount) {
64              SpringContext.getBean(SubAccountTrickleDownInactivationService.class).trickleDownInactivateSubAccounts((Account) getBusinessObject(), getDocumentNumber());
65              SpringContext.getBean(SubObjectTrickleDownInactivationService.class).trickleDownInactivateSubObjects((Account) getBusinessObject(), getDocumentNumber());
66          }
67      }
68  
69      
70  
71  
72  
73  
74      @Override
75      public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> parameters) {
76          Account account = (Account) this.getBusinessObject();
77          account.setAccountCreateDate(null); 
78          account.setAccountEffectiveDate(new Date(SpringContext.getBean(DateTimeService.class).getCurrentDate().getTime()));
79          account.setActive(true);
80          super.processAfterCopy(document, parameters);
81      }
82      
83   
84      @Override
85      public List<MaintenanceLock> generateMaintenanceLocks() {
86          List<MaintenanceLock> maintenanceLocks = super.generateMaintenanceLocks();
87          boolean isClosingAccount = false;
88  
89          if (isClosingAccount()) {
90              maintenanceLocks.addAll(SpringContext.getBean(SubAccountTrickleDownInactivationService.class).generateTrickleDownMaintenanceLocks((Account) getBusinessObject(), getDocumentNumber()));
91              maintenanceLocks.addAll(SpringContext.getBean(SubObjectTrickleDownInactivationService.class).generateTrickleDownMaintenanceLocks((Account) getBusinessObject(), getDocumentNumber()));
92          }
93          return maintenanceLocks;
94      }
95  
96      protected Account retrieveExistingAccountFromDB() {
97          Account newAccount = (Account) getBusinessObject();
98           Account oldAccount = SpringContext.getBean(AccountService.class).getByPrimaryId(newAccount.getChartOfAccountsCode(), newAccount.getAccountNumber());
99          return oldAccount;
100     }
101 
102     protected boolean isClosingAccount() {
103         
104         if (KRADConstants.MAINTENANCE_EDIT_ACTION.equals(getMaintenanceAction()) && !((Account) getBusinessObject()).isActive()) {
105             Account existingAccountFromDB = retrieveExistingAccountFromDB();
106             if (ObjectUtils.isNotNull(existingAccountFromDB)) {
107                 
108                 if (existingAccountFromDB.isActive()) {
109                     return true;
110                 }
111             }
112         }
113         return false;
114     }
115 
116     
117 
118 
119 
120 
121 
122 
123     public String getRoutingAccountsSupervisorySystemsIdentifier() {
124         final Account existingAccountFromDB = retrieveExistingAccountFromDB();
125         if (ObjectUtils.isNull(existingAccountFromDB)) {
126             return ((Account) getBusinessObject()).getAccountsSupervisorySystemsIdentifier();
127         }
128         return existingAccountFromDB.getAccountsSupervisorySystemsIdentifier();
129     }
130 
131     
132 
133 
134 
135 
136 
137     @Override
138     public void refresh(String refreshCaller, Map fieldValues, MaintenanceDocument document) {
139         super.refresh(refreshCaller, fieldValues, document);
140         Account newAcct = (Account) document.getNewMaintainableObject().getBusinessObject();
141         Account oldAcct = (Account) document.getOldMaintainableObject().getBusinessObject();
142         if (KRADConstants.MAINTENANCE_COPY_ACTION.equals(document.getNewMaintainableObject().getMaintenanceAction())) {
143             if (ObjectUtils.isNull(newAcct.getAccountGuideline())) {
144                 newAcct.setAccountGuideline(oldAcct.getAccountGuideline());
145             }
146         }
147 
148     }
149 
150     @Override
151     protected void refreshReferences(String referencesToRefresh) {        
152         
153         super.refreshReferences( removeReferenceFromString(referencesToRefresh, ACCOUNT_GUIDE_LINE_PROPERTY) );
154     }
155     
156     
157 
158 
159     protected String removeReferenceFromString(String referencesToRefresh, String referenceToRemove){
160         String newReference = referencesToRefresh;
161         
162         if(ObjectUtils.isNotNull(newReference)){
163             int index = newReference.indexOf(referenceToRemove);        
164             if(index != -1){  
165                 
166                 if(index == 0){
167                     
168                     String suffix = "";
169                     
170                     if(newReference.length() != referenceToRemove.length()){
171                         suffix = ",";
172                     }                    
173                     newReference = referencesToRefresh.replaceAll(ACCOUNT_GUIDE_LINE_PROPERTY + suffix, "");
174                     
175                 }else{
176                     
177                     newReference = referencesToRefresh.replaceAll("," + ACCOUNT_GUIDE_LINE_PROPERTY, "");
178                 }
179             }
180         }
181         
182         return newReference;
183     }
184     
185     
186 
187 
188 
189 
190 
191 
192     @Override
193     protected void populateChartOfAccountsCodeFields() {
194         
195         AccountService acctService = SpringContext.getBean(AccountService.class);        
196         AccountPersistenceStructureService apsService = SpringContext.getBean(AccountPersistenceStructureService.class);
197         PersistableBusinessObject bo = getBusinessObject();        
198         Iterator<Map.Entry<String, String>> chartAccountPairs = apsService.listChartCodeAccountNumberPairs(bo).entrySet().iterator();        
199  
200         
201         while (chartAccountPairs.hasNext()) {
202             Map.Entry<String, String> entry = (Map.Entry<String, String>)chartAccountPairs.next();
203             String coaCodeName = entry.getKey();            
204             String acctNumName = entry.getValue(); 
205             String accountNumber = (String)ObjectUtils.getPropertyValue(bo, acctNumName);
206             String coaCode = null;
207             String coaCodePK = (String)ObjectUtils.getPropertyValue(bo, OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE);
208             String accountNumberPK = (String)ObjectUtils.getPropertyValue(bo, OLEPropertyConstants.ACCOUNT_NUMBER);
209             
210             
211             if (StringUtils.equalsIgnoreCase(accountNumber, accountNumberPK)) {
212                 coaCode = coaCodePK;
213             }
214             
215             else {
216                 Account account = acctService.getUniqueAccountForAccountNumber(accountNumber);            
217                 if (ObjectUtils.isNotNull(account)) {
218                     coaCode = account.getChartOfAccountsCode();
219                 }
220             }
221             
222             try {
223                 ObjectUtils.setObjectProperty(bo, coaCodeName, coaCode); 
224             }
225             catch (Exception e) {
226                 LOG.error("Error in setting property value for " + coaCodeName);
227             }
228         }
229     }    
230 
231 }