001/*
002 * Copyright 2009 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.coa.service.impl;
017
018import java.util.HashSet;
019import java.util.Iterator;
020import java.util.List;
021import java.util.Set;
022
023import org.apache.commons.lang.StringUtils;
024import org.kuali.ole.coa.businessobject.AccountDelegate;
025import org.kuali.ole.coa.dataaccess.AccountDelegateDao;
026import org.kuali.ole.coa.dataaccess.AccountDelegateGlobalDao;
027import org.kuali.ole.coa.document.AccountDelegateGlobalMaintainableImpl;
028import org.kuali.ole.coa.document.AccountDelegateMaintainableImpl;
029import org.kuali.ole.coa.service.AccountDelegateService;
030import org.kuali.ole.sys.OLEConstants;
031import org.kuali.ole.sys.document.FinancialSystemMaintainable;
032import org.kuali.ole.sys.service.NonTransactional;
033import org.kuali.rice.core.api.datetime.DateTimeService;
034import org.kuali.rice.kew.service.KEWServiceLocator;
035import org.kuali.rice.kim.api.role.RoleResponsibility;
036import org.kuali.rice.kim.api.role.RoleService;
037import org.kuali.rice.kim.api.services.KimApiServiceLocator;
038import org.kuali.rice.kns.service.DataDictionaryService;
039import org.kuali.rice.krad.bo.PersistableBusinessObject;
040import org.kuali.rice.krad.maintenance.MaintenanceLock;
041import org.kuali.rice.krad.service.BusinessObjectService;
042import org.kuali.rice.krad.util.ObjectUtils;
043import org.springframework.transaction.annotation.Propagation;
044import org.springframework.transaction.annotation.Transactional;
045
046/**
047 * The default implementation of AccountDelegateService.
048 */
049public class AccountDelegateServiceImpl implements AccountDelegateService {
050
051    private AccountDelegateDao accountDelegateDao;
052    private AccountDelegateGlobalDao accountDelegateGlobalDao;
053    private DataDictionaryService dataDictionaryService;
054    private BusinessObjectService businessObjectService;
055    protected DateTimeService dateTimeService;
056
057    /**
058     * @see org.kuali.ole.coa.service.AccountDelegateService#getLockingDocumentId(org.kuali.ole.coa.document.AccountDelegateGlobalMaintainableImpl,
059     *      java.lang.String)
060     */
061    @Override
062    @NonTransactional
063    public String getLockingDocumentId(AccountDelegateGlobalMaintainableImpl global, String docNumber) {
064        String lockingDocId = null;
065        List<MaintenanceLock> maintenanceLocks = global.generateMaintenanceLocks();
066        for (MaintenanceLock maintenanceLock : maintenanceLocks) {
067            lockingDocId = accountDelegateGlobalDao.getLockingDocumentNumber(maintenanceLock.getLockingRepresentation(), docNumber);
068            if (StringUtils.isNotBlank(lockingDocId)) {
069                break;
070            }
071        }
072        return lockingDocId;
073    }
074
075    /**
076     * @see org.kuali.ole.coa.service.AccountDelegateService#getLockingDocumentId(org.kuali.ole.coa.document.AccountDelegateMaintainableImpl,
077     *      java.lang.String)
078     */
079    @Override
080    @NonTransactional
081    public String getLockingDocumentId(AccountDelegateMaintainableImpl delegate, String docNumber) {
082        String lockingDocId = null;
083        List<MaintenanceLock> maintenanceLocks = delegate.generateMaintenanceLocks();
084        maintenanceLocks.add(delegate.createGlobalAccountLock());
085
086        for (MaintenanceLock maintenanceLock : maintenanceLocks) {
087            lockingDocId = accountDelegateDao.getLockingDocumentNumber(maintenanceLock.getLockingRepresentation(), docNumber);
088            if (StringUtils.isNotBlank(lockingDocId)) {
089                break;
090            }
091        }
092        return lockingDocId;
093    }
094
095
096    /**
097     * @see org.kuali.ole.coa.service.AccountDelegateService#buildMaintainableForAccountDelegate(org.kuali.ole.coa.businessobject.AccountDelegate)
098     */
099    @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}