1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.ole.coa.businessobject;
17  
18  import java.sql.Date;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.LinkedHashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.kuali.ole.sys.OLEPropertyConstants;
29  import org.kuali.ole.sys.context.SpringContext;
30  import org.kuali.rice.core.api.util.type.KualiDecimal;
31  import org.kuali.rice.krad.bo.GlobalBusinessObject;
32  import org.kuali.rice.krad.bo.GlobalBusinessObjectDetail;
33  import org.kuali.rice.krad.bo.PersistableBusinessObject;
34  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
35  import org.kuali.rice.krad.service.BusinessObjectService;
36  import org.kuali.rice.krad.service.PersistenceStructureService;
37  
38  
39  
40  
41  
42  public class AccountDelegateGlobal extends PersistableBusinessObjectBase implements GlobalBusinessObject {
43  
44      protected String documentNumber;
45  
46      protected String modelName;
47      protected String modelChartOfAccountsCode;
48      protected String modelOrganizationCode;
49  
50      protected AccountDelegateModel model;
51  
52      protected List<AccountGlobalDetail> accountGlobalDetails;
53      protected List<AccountDelegateGlobalDetail> delegateGlobals;
54  
55      
56  
57  
58      public AccountDelegateGlobal() {
59          super();
60          accountGlobalDetails = new ArrayList<AccountGlobalDetail>();
61          delegateGlobals = new ArrayList<AccountDelegateGlobalDetail>();
62      }
63  
64      
65  
66  
67  
68  
69  
70      public void addAccount(AccountGlobalDetail accountGlobalDetail) {
71  
72          
73          if (accountGlobalDetail == null) {
74              throw new IllegalArgumentException("The accountGlobalDetail instanced passed in was null.");
75          }
76          else if (StringUtils.isBlank(accountGlobalDetail.getChartOfAccountsCode())) {
77              throw new IllegalArgumentException("The chartOfAccountsCode member of the accountGlobalDetail object was not populated.");
78          }
79          else if (StringUtils.isBlank(accountGlobalDetail.getAccountNumber())) {
80              throw new IllegalArgumentException("The accountNumber member of the accountGlobalDetail object was not populated.");
81          }
82  
83          
84          AccountGlobalDetail testObject = getAccount(accountGlobalDetail.getChartOfAccountsCode(), accountGlobalDetail.getAccountNumber());
85          if (testObject == null) {
86              this.accountGlobalDetails.add(accountGlobalDetail);
87          }
88      }
89  
90      
91  
92  
93  
94  
95  
96  
97  
98      public AccountGlobalDetail getAccount(String chartCode, String accountNumber) {
99  
100         
101         if (StringUtils.isBlank(chartCode)) {
102             throw new IllegalArgumentException("The chartCode argument was null or empty.");
103         }
104         else if (StringUtils.isBlank(accountNumber)) {
105             throw new IllegalArgumentException("The accountNumber argument was null or empty.");
106         }
107 
108         
109         for (Iterator iter = this.accountGlobalDetails.iterator(); iter.hasNext();) {
110             AccountGlobalDetail accountGlobalDetail = (AccountGlobalDetail) iter.next();
111 
112             
113             if (chartCode.equalsIgnoreCase(accountGlobalDetail.getChartOfAccountsCode()) && accountNumber.equalsIgnoreCase(accountGlobalDetail.getAccountNumber())) {
114                 return accountGlobalDetail;
115             }
116         }
117 
118         
119         return null;
120     }
121 
122     
123 
124 
125     public List<PersistableBusinessObject> generateDeactivationsToPersist() {
126         BusinessObjectService boService = SpringContext.getBean(BusinessObjectService.class);
127 
128         
129         List<AccountDelegate> bosToDeactivate = new ArrayList();
130         Map<String, Object> fieldValues;
131         Collection existingDelegates;
132         for (AccountGlobalDetail accountDetail : getAccountGlobalDetails()) {
133             fieldValues = new HashMap();
134             fieldValues.put("chartOfAccountsCode", accountDetail.getChartOfAccountsCode());
135             fieldValues.put("accountNumber", accountDetail.getAccountNumber());
136             fieldValues.put("active", true);
137             existingDelegates = boService.findMatching(AccountDelegate.class, fieldValues);
138             bosToDeactivate.addAll(existingDelegates);
139         }
140 
141         
142         for (AccountDelegate accountDelegate : bosToDeactivate) {
143             accountDelegate.setActive(false);
144         }
145         return new ArrayList<PersistableBusinessObject>(bosToDeactivate);
146     }
147 
148     
149 
150 
151     @SuppressWarnings("deprecation")
152     public List<PersistableBusinessObject> generateGlobalChangesToPersist() {
153 
154         BusinessObjectService boService = SpringContext.getBean(BusinessObjectService.class);
155         List<AccountDelegate> persistables = new ArrayList();
156 
157         List<AccountDelegateGlobalDetail> changeDocuments = this.getDelegateGlobals();
158         List<AccountGlobalDetail> accountDetails = this.getAccountGlobalDetails();
159 
160         for (AccountDelegateGlobalDetail changeDocument : changeDocuments) {
161             for (AccountGlobalDetail accountDetail : accountDetails) {
162 
163                 Account account = (Account) boService.findByPrimaryKey(Account.class, accountDetail.getPrimaryKeys());
164 
165                 
166                 
167                 if (account == null) {
168                     throw new RuntimeException("Account [" + accountDetail.getChartOfAccountsCode() + "-" + accountDetail.getAccountNumber() + "] was not present in the database. " + "This should never happen under normal circumstances, as an invalid account should have " + "been caught by the Business Rules infrastructure.");
169                 }
170 
171                 
172                 
173                 Map pkMap = new HashMap();
174                 pkMap.putAll(accountDetail.getPrimaryKeys()); 
175                 pkMap.put("financialDocumentTypeCode", changeDocument.getFinancialDocumentTypeCode());
176                 pkMap.put("accountDelegateSystemId", changeDocument.getAccountDelegateUniversalId());
177                 AccountDelegate delegate = (AccountDelegate) boService.findByPrimaryKey(AccountDelegate.class, pkMap);
178 
179                 
180                 
181                 if (delegate == null) {
182                     delegate = new AccountDelegate();
183                     delegate.setChartOfAccountsCode(accountDetail.getChartOfAccountsCode());
184                     delegate.setAccountNumber(accountDetail.getAccountNumber());
185                     delegate.setAccountDelegateSystemId(changeDocument.getAccountDelegateUniversalId());
186                     delegate.setFinancialDocumentTypeCode(changeDocument.getFinancialDocumentTypeCode());
187                     delegate.setActive(true);
188                 }
189                 else {
190                     delegate.setActive(true);
191                 }
192 
193                 
194                 if (changeDocument.getApprovalFromThisAmount() != null) {
195                     if (!changeDocument.getApprovalFromThisAmount().equals(KualiDecimal.ZERO)) {
196                         delegate.setFinDocApprovalFromThisAmt(changeDocument.getApprovalFromThisAmount());
197                     }
198                 }
199 
200                 
201                 if (changeDocument.getApprovalToThisAmount() != null) {
202                     if (!changeDocument.getApprovalToThisAmount().equals(KualiDecimal.ZERO)) {
203                         delegate.setFinDocApprovalToThisAmount(changeDocument.getApprovalToThisAmount());
204                     }
205                 }
206 
207                 
208                 delegate.setAccountsDelegatePrmrtIndicator(changeDocument.getAccountDelegatePrimaryRoutingIndicator());
209 
210                 
211                 if (changeDocument.getAccountDelegateStartDate() != null) {
212                     delegate.setAccountDelegateStartDate(new Date(changeDocument.getAccountDelegateStartDate().getTime()));
213                 }
214 
215                 persistables.add(delegate);
216 
217             }
218         }
219 
220         return new ArrayList<PersistableBusinessObject>(persistables);
221     }
222 
223     
224 
225 
226     
227     protected LinkedHashMap toStringMapper_RICE20_REFACTORME() {
228 
229         LinkedHashMap m = new LinkedHashMap();
230 
231         m.put(OLEPropertyConstants.DOCUMENT_NUMBER, this.documentNumber);
232         return m;
233     }
234 
235     
236 
237 
238     public String getDocumentNumber() {
239         return documentNumber;
240     }
241 
242     
243 
244 
245     public void setDocumentNumber(String documentNumber) {
246         this.documentNumber = documentNumber;
247 
248     }
249 
250     
251 
252 
253 
254 
255     public final List<AccountGlobalDetail> getAccountGlobalDetails() {
256         return accountGlobalDetails;
257     }
258 
259     
260 
261 
262 
263 
264     public final void setAccountGlobalDetails(List<AccountGlobalDetail> accountGlobalDetails) {
265         this.accountGlobalDetails = accountGlobalDetails;
266     }
267 
268     
269 
270 
271 
272 
273     public final List<AccountDelegateGlobalDetail> getDelegateGlobals() {
274         return delegateGlobals;
275     }
276 
277     
278 
279 
280 
281 
282     public final void setDelegateGlobals(List<AccountDelegateGlobalDetail> delegateGlobals) {
283         this.delegateGlobals = delegateGlobals;
284     }
285 
286     
287 
288 
289     public boolean isPersistable() {
290         PersistenceStructureService persistenceStructureService = SpringContext.getBean(PersistenceStructureService.class);
291 
292         
293         if (StringUtils.isBlank(documentNumber)) {
294             return false;
295         }
296 
297         
298         for (AccountDelegateGlobalDetail delegateGlobals : getDelegateGlobals()) {
299             if (!persistenceStructureService.hasPrimaryKeyFieldValues(delegateGlobals)) {
300                 return false;
301             }
302         }
303         for (AccountGlobalDetail account : getAccountGlobalDetails()) {
304             if (!persistenceStructureService.hasPrimaryKeyFieldValues(account)) {
305                 return false;
306             }
307         }
308 
309         
310         return true;
311     }
312 
313     public String getModelName() {
314         return modelName;
315     }
316 
317     public void setModelName(String loadModelName) {
318         this.modelName = loadModelName;
319     }
320 
321     public String getModelChartOfAccountsCode() {
322         return modelChartOfAccountsCode;
323     }
324 
325     public void setModelChartOfAccountsCode(String loadModelChartOfAccountsCode) {
326         this.modelChartOfAccountsCode = loadModelChartOfAccountsCode;
327     }
328 
329     public String getModelOrganizationCode() {
330         return modelOrganizationCode;
331     }
332 
333     public void setModelOrganizationCode(String loadModelOrganizationCode) {
334         this.modelOrganizationCode = loadModelOrganizationCode;
335     }
336 
337     public AccountDelegateModel getModel() {
338         return model;
339     }
340 
341     public void setModel(AccountDelegateModel loadModel) {
342         this.model = loadModel;
343     }
344 
345     public List<? extends GlobalBusinessObjectDetail> getAllDetailObjects() {
346         ArrayList<GlobalBusinessObjectDetail> details = new ArrayList<GlobalBusinessObjectDetail>(accountGlobalDetails.size() + delegateGlobals.size());
347         details.addAll(accountGlobalDetails);
348         details.addAll(delegateGlobals);
349         return details;
350     }
351 
352     @Override
353     public void linkEditableUserFields() {
354         super.linkEditableUserFields();
355         if (this == null) {
356             throw new IllegalArgumentException("globalDelegate parameter passed in was null");
357         }
358         List<PersistableBusinessObject> bos = new ArrayList<PersistableBusinessObject>();
359         bos.addAll(getDelegateGlobals());
360         SpringContext.getBean(BusinessObjectService.class).linkUserFields(bos);
361     }
362 
363     
364 
365 
366     @Override
367     public List<Collection<PersistableBusinessObject>> buildListOfDeletionAwareLists() {
368         List<Collection<PersistableBusinessObject>> managedLists = super.buildListOfDeletionAwareLists();
369 
370         managedLists.add( new ArrayList<PersistableBusinessObject>( getAccountGlobalDetails() ) );
371         managedLists.add( new ArrayList<PersistableBusinessObject>( getDelegateGlobals() ) );
372 
373         return managedLists;
374     }
375 }