View Javadoc
1   /*
2    * Copyright 2009 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.service.impl;
17  
18  import java.util.HashSet;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Set;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.ole.coa.businessobject.AccountDelegate;
25  import org.kuali.ole.coa.dataaccess.AccountDelegateDao;
26  import org.kuali.ole.coa.dataaccess.AccountDelegateGlobalDao;
27  import org.kuali.ole.coa.document.AccountDelegateGlobalMaintainableImpl;
28  import org.kuali.ole.coa.document.AccountDelegateMaintainableImpl;
29  import org.kuali.ole.coa.service.AccountDelegateService;
30  import org.kuali.ole.sys.OLEConstants;
31  import org.kuali.ole.sys.document.FinancialSystemMaintainable;
32  import org.kuali.ole.sys.service.NonTransactional;
33  import org.kuali.rice.core.api.datetime.DateTimeService;
34  import org.kuali.rice.kew.service.KEWServiceLocator;
35  import org.kuali.rice.kim.api.role.RoleResponsibility;
36  import org.kuali.rice.kim.api.role.RoleService;
37  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
38  import org.kuali.rice.kns.service.DataDictionaryService;
39  import org.kuali.rice.krad.bo.PersistableBusinessObject;
40  import org.kuali.rice.krad.maintenance.MaintenanceLock;
41  import org.kuali.rice.krad.service.BusinessObjectService;
42  import org.kuali.rice.krad.util.ObjectUtils;
43  import org.springframework.transaction.annotation.Propagation;
44  import org.springframework.transaction.annotation.Transactional;
45  
46  /**
47   * The default implementation of AccountDelegateService.
48   */
49  public class AccountDelegateServiceImpl implements AccountDelegateService {
50  
51      private AccountDelegateDao accountDelegateDao;
52      private AccountDelegateGlobalDao accountDelegateGlobalDao;
53      private DataDictionaryService dataDictionaryService;
54      private BusinessObjectService businessObjectService;
55      protected DateTimeService dateTimeService;
56  
57      /**
58       * @see org.kuali.ole.coa.service.AccountDelegateService#getLockingDocumentId(org.kuali.ole.coa.document.AccountDelegateGlobalMaintainableImpl,
59       *      java.lang.String)
60       */
61      @Override
62      @NonTransactional
63      public String getLockingDocumentId(AccountDelegateGlobalMaintainableImpl global, String docNumber) {
64          String lockingDocId = null;
65          List<MaintenanceLock> maintenanceLocks = global.generateMaintenanceLocks();
66          for (MaintenanceLock maintenanceLock : maintenanceLocks) {
67              lockingDocId = accountDelegateGlobalDao.getLockingDocumentNumber(maintenanceLock.getLockingRepresentation(), docNumber);
68              if (StringUtils.isNotBlank(lockingDocId)) {
69                  break;
70              }
71          }
72          return lockingDocId;
73      }
74  
75      /**
76       * @see org.kuali.ole.coa.service.AccountDelegateService#getLockingDocumentId(org.kuali.ole.coa.document.AccountDelegateMaintainableImpl,
77       *      java.lang.String)
78       */
79      @Override
80      @NonTransactional
81      public String getLockingDocumentId(AccountDelegateMaintainableImpl delegate, String docNumber) {
82          String lockingDocId = null;
83          List<MaintenanceLock> maintenanceLocks = delegate.generateMaintenanceLocks();
84          maintenanceLocks.add(delegate.createGlobalAccountLock());
85  
86          for (MaintenanceLock maintenanceLock : maintenanceLocks) {
87              lockingDocId = accountDelegateDao.getLockingDocumentNumber(maintenanceLock.getLockingRepresentation(), docNumber);
88              if (StringUtils.isNotBlank(lockingDocId)) {
89                  break;
90              }
91          }
92          return lockingDocId;
93      }
94  
95  
96      /**
97       * @see org.kuali.ole.coa.service.AccountDelegateService#buildMaintainableForAccountDelegate(org.kuali.ole.coa.businessobject.AccountDelegate)
98       */
99      @Override
100     @NonTransactional
101     public FinancialSystemMaintainable buildMaintainableForAccountDelegate(AccountDelegate delegate) {
102         FinancialSystemMaintainable maintainable = getAccountDelegateMaintainable();
103         maintainable.setBoClass(delegate.getClass());
104         maintainable.setBusinessObject(delegate);
105         return maintainable;
106     }
107 
108     /**
109      * @return the proper class for the Maintainable associated with AccountDelegate maintenance documents
110      */
111     protected Class getAccountDelegateMaintainableClass() {
112         return dataDictionaryService.getDataDictionary().getMaintenanceDocumentEntryForBusinessObjectClass(AccountDelegate.class).getMaintainableClass();
113     }
114 
115     /**
116      * @return a new instance of the proper maintainable for AccountDelegate maintenance documents
117      */
118     protected FinancialSystemMaintainable getAccountDelegateMaintainable() {
119         final Class maintainableClazz = getAccountDelegateMaintainableClass();
120         final FinancialSystemMaintainable maintainable;
121         try {
122             maintainable = (FinancialSystemMaintainable) maintainableClazz.newInstance();
123         }
124         catch (Exception ie) {
125             throw new RuntimeException("Could not instantiate maintainable for AccountDelegate maintenance document", ie);
126         }
127         return maintainable;
128     }
129 
130     /**
131      * @see org.kuali.ole.coa.service.AccountDelegateService#retrieveAllActiveDelegationsForPerson(java.lang.String)
132      */
133     @Override
134     @NonTransactional
135     public Iterator<AccountDelegate> retrieveAllActiveDelegationsForPerson(String principalId, boolean primary) {
136         return accountDelegateDao.getAccountDelegationsForPerson(principalId, primary);
137     }
138 
139     /**
140      * @see org.kuali.ole.coa.service.AccountDelegateService#isPrincipalInAnyWayShapeOrFormPrimaryAccountDelegate(java.lang.String)
141      */
142     @Override
143     @NonTransactional
144     public boolean isPrincipalInAnyWayShapeOrFormPrimaryAccountDelegate(String principalId) {
145         return accountDelegateDao.isPrincipalInAnyWayShapeOrFormPrimaryAccountDelegate(principalId, dateTimeService.getCurrentSqlDate());
146     }
147 
148     /**
149      * @see org.kuali.ole.coa.service.AccountDelegateService#isPrincipalInAnyWayShapeOrFormSecondaryAccountDelegate(java.lang.String)
150      */
151     @Override
152     @NonTransactional
153     public boolean isPrincipalInAnyWayShapeOrFormSecondaryAccountDelegate(String principalId) {
154         return accountDelegateDao.isPrincipalInAnyWayShapeOrFormSecondaryAccountDelegate(principalId, dateTimeService.getCurrentSqlDate());
155     }
156 
157     /**
158      * Saves the given account delegate to the persistence store
159      *
160      * @param accountDelegate the account delegate to save
161      */
162     @Override
163     @Transactional(propagation = Propagation.REQUIRES_NEW)
164     public void saveForMaintenanceDocument(AccountDelegate accountDelegate) {
165         businessObjectService.linkAndSave(accountDelegate);
166     }
167 
168     /**
169      * Persists the given account delegate global maintenance document inactivations
170      *
171      * @param delegatesToInactivate the List of delegates to inactivate
172      */
173     @Override
174     @Transactional(propagation = Propagation.REQUIRES_NEW)
175     public void saveInactivationsForGlobalMaintenanceDocument(List<PersistableBusinessObject> delegatesToInactivate) {
176         if (delegatesToInactivate != null && !delegatesToInactivate.isEmpty()) {
177             businessObjectService.save(delegatesToInactivate);
178         }
179     }
180 
181     /**
182      * Persists the given account delegate global maintenance document changes
183      *
184      * @param delegatesToChange the List of delegates to change
185      */
186     @Override
187     @Transactional(propagation = Propagation.REQUIRES_NEW)
188     public void saveChangesForGlobalMaintenanceDocument(List<PersistableBusinessObject> delegatesToChange) {
189         if (delegatesToChange != null && !delegatesToChange.isEmpty()) {
190             businessObjectService.save(delegatesToChange);
191         }
192     }
193 
194     /**
195      * Updates the role that this delegate is part of, to account for the changes in this delegate
196      */
197     @Override
198     @Transactional
199     public void updateDelegationRole() {
200         final RoleService roleManagementService = KimApiServiceLocator.getRoleService();
201         final String roleId = roleManagementService.getRoleIdByNamespaceCodeAndName(OLEConstants.CoreModuleNamespaces.OLE, OLEConstants.SysKimApiConstants.FISCAL_OFFICER_KIM_ROLE_NAME);
202         if (!StringUtils.isBlank(roleId)) {
203             List<RoleResponsibility> newRoleResp = roleManagementService.getRoleResponsibilities(roleId);
204             KEWServiceLocator.getActionRequestService().updateActionRequestsForResponsibilityChange(getChangedRoleResponsibilityIds(newRoleResp));
205         }
206     }
207     protected Set<String> getChangedRoleResponsibilityIds( List<RoleResponsibility> newRoleResp){
208         Set<String> lRet = new HashSet<String>();
209           if(ObjectUtils.isNotNull(newRoleResp)){
210             for(RoleResponsibility roleResp: newRoleResp){
211                 lRet.add(roleResp.getResponsibilityId());
212             }
213         }
214         return lRet;
215     }
216 
217     @NonTransactional
218     public void setAccountDelegateDao(AccountDelegateDao accountDelegateDao) {
219         this.accountDelegateDao = accountDelegateDao;
220     }
221 
222     @NonTransactional
223     public void setAccountDelegateGlobalDao(AccountDelegateGlobalDao accountDelegateGlobalDao) {
224         this.accountDelegateGlobalDao = accountDelegateGlobalDao;
225     }
226 
227     @NonTransactional
228     public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
229         this.dataDictionaryService = dataDictionaryService;
230     }
231 
232     @NonTransactional
233     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
234         this.businessObjectService = businessObjectService;
235     }
236 
237     @NonTransactional
238     public void setDateTimeService(DateTimeService dateTimeService) {
239         this.dateTimeService = dateTimeService;
240     }
241 
242 }