View Javadoc
1   /*
2    * Copyright 2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.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/ecl2.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.coa.document;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.HashSet;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.kuali.ole.coa.businessobject.AccountDelegate;
27  import org.kuali.ole.coa.businessobject.AccountDelegateGlobal;
28  import org.kuali.ole.coa.businessobject.AccountDelegateGlobalDetail;
29  import org.kuali.ole.coa.businessobject.AccountDelegateModel;
30  import org.kuali.ole.coa.businessobject.AccountDelegateModelDetail;
31  import org.kuali.ole.coa.businessobject.AccountGlobalDetail;
32  import org.kuali.ole.coa.service.AccountDelegateService;
33  import org.kuali.ole.sys.OLEConstants;
34  import org.kuali.ole.sys.context.SpringContext;
35  import org.kuali.ole.sys.document.FinancialSystemGlobalMaintainable;
36  import org.kuali.rice.kns.document.MaintenanceDocument;
37  import org.kuali.rice.krad.bo.PersistableBusinessObject;
38  import org.kuali.rice.krad.maintenance.MaintenanceLock;
39  import org.kuali.rice.krad.service.BusinessObjectService;
40  import org.kuali.rice.krad.util.ObjectUtils;
41  
42  /**
43   * This class overrides the base {@link FinancialSystemGlobalMaintainable} to generate the specific maintenance locks for Global delegates
44   * and to help with using delegate models
45   * 
46   * @see OrganizationRoutingModelName
47   */
48  public class AccountDelegateGlobalMaintainableImpl extends FinancialSystemGlobalMaintainable {
49      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AccountDelegateGlobalMaintainableImpl.class);
50  
51      /**
52       * This method is used for the creation of a delegate from a {@link OrganizationRoutingModelName}
53       * 
54       * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#setupNewFromExisting()
55       */
56      @Override
57      public void setupNewFromExisting( MaintenanceDocument document, Map<String,String[]> parameters ) {
58          super.setupNewFromExisting( document, parameters );
59  
60          AccountDelegateGlobal globalDelegate = (AccountDelegateGlobal) this.getBusinessObject();
61          globalDelegate.setVersionNumber(1L);
62          this.setBusinessObject(globalDelegate);
63          // 1. if model name, chart of accounts, and org code are all present
64          // then let's see if we've actually got a model record
65          if (!StringUtils.isBlank(globalDelegate.getModelName()) && !StringUtils.isBlank(globalDelegate.getModelChartOfAccountsCode()) && !StringUtils.isBlank(globalDelegate.getModelOrganizationCode())) {
66              Map pkMap = new HashMap();
67              pkMap.put("accountDelegateModelName", globalDelegate.getModelName());
68              pkMap.put("chartOfAccountsCode", globalDelegate.getModelChartOfAccountsCode());
69              pkMap.put("organizationCode", globalDelegate.getModelOrganizationCode());
70  
71              AccountDelegateModel globalDelegateTemplate = (AccountDelegateModel) SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(AccountDelegateModel.class, pkMap);
72              if (globalDelegateTemplate != null) {
73                  // 2. if there is a model record, then let's populate the global delegate
74                  // based on that
75                  for (AccountDelegateModelDetail model : globalDelegateTemplate.getAccountDelegateModelDetails()) {
76                      if (model.isActive()) { // only populate with active models
77                          AccountDelegateGlobalDetail newDelegate = new AccountDelegateGlobalDetail(model);
78                          // allow deletion of the new delegate from the global delegate
79                          newDelegate.setNewCollectionRecord(true);
80                          globalDelegate.getDelegateGlobals().add(newDelegate);
81                      }
82                  }
83              }
84          }
85      }
86  
87      @Override
88      public String getLockingDocumentId() {
89         String lock = super.getLockingDocumentId();
90         if (StringUtils.isNotBlank(lock))
91             return lock;
92         else {
93             AccountDelegateService accountDelegateService = SpringContext.getBean(AccountDelegateService.class);
94             lock = accountDelegateService.getLockingDocumentId(this, getDocumentNumber());
95             return lock;
96         }
97      }
98      
99      
100     /**
101      * This creates the particular locking representation for this global document.
102      * 
103      * @see org.kuali.rice.kns.maintenance.Maintainable#generateMaintenanceLocks()
104      */
105     @Override
106     public List<MaintenanceLock> generateMaintenanceLocks() {
107         // create locking rep for each combination of account and object code
108         List<MaintenanceLock> maintenanceLocks = new ArrayList();
109         AccountDelegateGlobal delegateGlobal = (AccountDelegateGlobal) getBusinessObject();
110 
111         // hold all the locking representations in a set to make sure we don't get any duplicates
112         Set<String> lockingRepresentations = new HashSet<String>();
113 
114         MaintenanceLock maintenanceLock;
115         if (ObjectUtils.isNotNull(delegateGlobal)) {
116             for (AccountGlobalDetail accountGlobalDetail : delegateGlobal.getAccountGlobalDetails()) {
117                 for (AccountDelegateGlobalDetail delegateGlobalDetail : delegateGlobal.getDelegateGlobals()) {
118                     StringBuilder lockRep = new StringBuilder();
119                     lockRep.append(AccountDelegate.class.getName());
120                     lockRep.append(OLEConstants.Maintenance.AFTER_CLASS_DELIM);
121                     lockRep.append("chartOfAccountsCode");
122                     lockRep.append(OLEConstants.Maintenance.AFTER_FIELDNAME_DELIM);
123                     lockRep.append(accountGlobalDetail.getChartOfAccountsCode());
124                     lockRep.append(OLEConstants.Maintenance.AFTER_VALUE_DELIM);
125                     lockRep.append("accountNumber");
126                     lockRep.append(OLEConstants.Maintenance.AFTER_FIELDNAME_DELIM);
127                     lockRep.append(accountGlobalDetail.getAccountNumber());
128                     lockRep.append(OLEConstants.Maintenance.AFTER_VALUE_DELIM);
129                     lockRep.append("financialDocumentTypeCode");
130                     lockRep.append(OLEConstants.Maintenance.AFTER_FIELDNAME_DELIM);
131                     lockRep.append(delegateGlobalDetail.getFinancialDocumentTypeCode());
132                     lockRep.append(OLEConstants.Maintenance.AFTER_VALUE_DELIM);
133                     lockRep.append("accountDelegateSystemId");
134                     lockRep.append(OLEConstants.Maintenance.AFTER_FIELDNAME_DELIM);
135                     lockRep.append(delegateGlobalDetail.getAccountDelegateUniversalId());
136                     // FIXME above is a bit dangerous b/c it hard codes the attribute names, which could change (particularly
137                     // accountDelegateSystemId) - guess they should either be constants or obtained by looping through Delegate keys;
138                     // however, I copied this from elsewhere which had them hard-coded, so I'm leaving it for now
139 
140                     if (!lockingRepresentations.contains(lockRep.toString())) {
141                         maintenanceLock = new MaintenanceLock();
142                         maintenanceLock.setDocumentNumber(delegateGlobal.getDocumentNumber());
143                         maintenanceLock.setLockingRepresentation(lockRep.toString());
144                         maintenanceLocks.add(maintenanceLock);
145                         lockingRepresentations.add(lockRep.toString());
146                     }
147 
148                     lockRep = new StringBuilder();
149                     lockRep.append(AccountDelegate.class.getName());
150                     lockRep.append(OLEConstants.Maintenance.AFTER_CLASS_DELIM);
151                     lockRep.append("chartOfAccountsCode");
152                     lockRep.append(OLEConstants.Maintenance.AFTER_FIELDNAME_DELIM);
153                     lockRep.append(accountGlobalDetail.getChartOfAccountsCode());
154                     lockRep.append(OLEConstants.Maintenance.AFTER_VALUE_DELIM);
155                     lockRep.append("accountNumber");
156                     lockRep.append(OLEConstants.Maintenance.AFTER_FIELDNAME_DELIM);
157                     lockRep.append(accountGlobalDetail.getAccountNumber());
158                     lockRep.append(OLEConstants.Maintenance.AFTER_VALUE_DELIM);
159                     lockRep.append("financialDocumentTypeCode");
160                     lockRep.append(OLEConstants.Maintenance.AFTER_FIELDNAME_DELIM);
161                     lockRep.append(delegateGlobalDetail.getFinancialDocumentTypeCode());
162                     lockRep.append(OLEConstants.Maintenance.AFTER_VALUE_DELIM);
163                     lockRep.append("accountsDelegatePrmrtIndicator");
164                     lockRep.append(OLEConstants.Maintenance.AFTER_FIELDNAME_DELIM);
165                     lockRep.append("true");
166 
167                     if (!lockingRepresentations.contains(lockRep.toString())) {
168                         maintenanceLock = new MaintenanceLock();
169                         maintenanceLock.setDocumentNumber(delegateGlobal.getDocumentNumber());
170                         maintenanceLock.setLockingRepresentation(lockRep.toString());
171                         maintenanceLocks.add(maintenanceLock);
172                         lockingRepresentations.add(lockRep.toString());
173                     }
174 
175                     lockRep = new StringBuilder();
176                     lockRep.append(AccountDelegateGlobal.class.getName());
177                     lockRep.append(OLEConstants.Maintenance.AFTER_CLASS_DELIM);
178                     lockRep.append("chartOfAccountsCode");
179                     lockRep.append(OLEConstants.Maintenance.AFTER_FIELDNAME_DELIM);
180                     lockRep.append(accountGlobalDetail.getChartOfAccountsCode());
181                     lockRep.append(OLEConstants.Maintenance.AFTER_VALUE_DELIM);
182                     lockRep.append("accountNumber");
183                     lockRep.append(OLEConstants.Maintenance.AFTER_FIELDNAME_DELIM);
184                     lockRep.append(accountGlobalDetail.getAccountNumber());
185 
186                     if (!lockingRepresentations.contains(lockRep.toString())) {
187                         maintenanceLock = new MaintenanceLock();
188                         maintenanceLock.setDocumentNumber(delegateGlobal.getDocumentNumber());
189                         maintenanceLock.setLockingRepresentation(lockRep.toString());
190                         maintenanceLocks.add(maintenanceLock);
191                         lockingRepresentations.add(lockRep.toString());
192                     }
193                 }
194             }
195         }
196         return maintenanceLocks;
197     }
198 
199     @Override
200     public Class<? extends PersistableBusinessObject> getPrimaryEditedBusinessObjectClass() {
201         return AccountDelegate.class;
202     }
203 
204     /**
205      * Overridden to update the delegations for currently routing documents; this also guarantees that the business
206      * objects to change will be saved in a separate transaction
207      * @see org.kuali.rice.kns.maintenance.KualiGlobalMaintainableImpl#saveBusinessObject()
208      */
209     @Override
210     public void saveBusinessObject() {
211         final AccountDelegateGlobal accountDelegateGlobal = (AccountDelegateGlobal)this.getBusinessObject();
212         final AccountDelegateService accountDelegateService = SpringContext.getBean(AccountDelegateService.class);
213         
214         accountDelegateService.saveInactivationsForGlobalMaintenanceDocument(accountDelegateGlobal.generateDeactivationsToPersist());
215         accountDelegateService.saveChangesForGlobalMaintenanceDocument(accountDelegateGlobal.generateGlobalChangesToPersist());
216         
217         accountDelegateService.updateDelegationRole();
218     }
219 }