001    /*
002     * Copyright 2011 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     */
016    package org.kuali.kfs.coa.service.impl;
017    
018    import java.util.Iterator;
019    import java.util.List;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.kuali.kfs.coa.businessobject.AccountDelegate;
023    import org.kuali.kfs.coa.dataaccess.AccountDelegateDao;
024    import org.kuali.kfs.coa.dataaccess.AccountDelegateGlobalDao;
025    import org.kuali.kfs.coa.document.AccountDelegateGlobalMaintainableImpl;
026    import org.kuali.kfs.coa.document.AccountDelegateMaintainableImpl;
027    import org.kuali.kfs.coa.service.AccountDelegateService;
028    import org.kuali.kfs.sys.KFSConstants;
029    import org.kuali.kfs.sys.context.SpringContext;
030    import org.kuali.kfs.sys.document.FinancialSystemMaintainable;
031    import org.kuali.kfs.sys.service.NonTransactional;
032    import org.kuali.rice.kim.service.RoleManagementService;
033    import org.kuali.rice.kns.bo.PersistableBusinessObject;
034    import org.kuali.rice.kns.document.MaintenanceLock;
035    import org.kuali.rice.kns.maintenance.Maintainable;
036    import org.kuali.rice.kns.service.BusinessObjectService;
037    import org.kuali.rice.kns.service.DataDictionaryService;
038    import org.springframework.transaction.annotation.Propagation;
039    import org.springframework.transaction.annotation.Transactional;
040    
041    /**
042     * The default implementation of AccountDelegateService.
043     */
044    public class AccountDelegateServiceImpl implements AccountDelegateService {
045    
046        private AccountDelegateDao accountDelegateDao;
047        private AccountDelegateGlobalDao accountDelegateGlobalDao;
048        private DataDictionaryService dataDictionaryService;
049        private BusinessObjectService businessObjectService;
050    
051        /**
052         * 
053         * @see org.kuali.kfs.coa.service.AccountDelegateService#getLockingDocumentId(org.kuali.kfs.coa.document.AccountDelegateGlobalMaintainableImpl, java.lang.String)
054         */
055        @NonTransactional
056        public String getLockingDocumentId(AccountDelegateGlobalMaintainableImpl global, String docNumber) {
057            String lockingDocId = null;
058            List<MaintenanceLock> maintenanceLocks = global.generateMaintenanceLocks();
059            for (MaintenanceLock maintenanceLock : maintenanceLocks) {
060                lockingDocId = accountDelegateGlobalDao.getLockingDocumentNumber(maintenanceLock.getLockingRepresentation(),docNumber);
061                if (StringUtils.isNotBlank(lockingDocId)) {
062                    break;
063                }
064            }
065            return lockingDocId;
066        }
067    
068        /**
069         * 
070         * @see org.kuali.kfs.coa.service.AccountDelegateService#getLockingDocumentId(org.kuali.kfs.coa.document.AccountDelegateMaintainableImpl, java.lang.String)
071         */
072        @NonTransactional
073        public String getLockingDocumentId(AccountDelegateMaintainableImpl delegate, String docNumber) {
074            String lockingDocId = null;
075            List<MaintenanceLock> maintenanceLocks = delegate.generateMaintenanceLocks();
076            maintenanceLocks.add(delegate.createGlobalAccountLock());
077            
078            for (MaintenanceLock maintenanceLock : maintenanceLocks) {
079                lockingDocId = accountDelegateDao.getLockingDocumentNumber(maintenanceLock.getLockingRepresentation(),docNumber);
080                if (StringUtils.isNotBlank(lockingDocId)) {
081                    break;
082                }
083            }
084            return lockingDocId;
085        }
086        
087        
088        /**
089         * @see org.kuali.kfs.coa.service.AccountDelegateService#buildMaintainableForAccountDelegate(org.kuali.kfs.coa.businessobject.AccountDelegate)
090         */
091        @NonTransactional
092        public FinancialSystemMaintainable buildMaintainableForAccountDelegate(AccountDelegate delegate) {
093            FinancialSystemMaintainable maintainable = getAccountDelegateMaintainable();
094            maintainable.setBoClass(delegate.getClass());
095            maintainable.setBusinessObject(delegate);
096            return maintainable;
097        }
098        
099        /**
100         * @return the proper class for the Maintainable associated with AccountDelegate maintenance documents
101         */
102        protected Class<? extends Maintainable> getAccountDelegateMaintainableClass() {
103            return getDataDictionaryService().getDataDictionary().getMaintenanceDocumentEntryForBusinessObjectClass(AccountDelegate.class).getMaintainableClass();
104        }
105        
106        /**
107         * @return a new instance of the proper maintainable for AccountDelegate maintenance documents
108         */
109        protected FinancialSystemMaintainable getAccountDelegateMaintainable() {
110            final Class<? extends Maintainable> maintainableClazz = getAccountDelegateMaintainableClass();
111            final FinancialSystemMaintainable maintainable;
112            try {
113                maintainable = (FinancialSystemMaintainable)maintainableClazz.newInstance();
114            }
115            catch (InstantiationException ie) {
116                throw new RuntimeException("Could not instantiate maintainable for AccountDelegate maintenance document", ie);
117            }
118            catch (IllegalAccessException iae) {
119                throw new RuntimeException("Could not instantiate maintainable for AccountDelegate maintenance document", iae);
120            }
121            return maintainable;
122        }
123    
124        /**
125         * @see org.kuali.kfs.coa.service.AccountDelegateService#retrieveAllActiveDelegationsForPerson(java.lang.String)
126         */
127        @NonTransactional
128        public Iterator<AccountDelegate> retrieveAllActiveDelegationsForPerson(String principalId, boolean primary) {
129            return (Iterator<AccountDelegate>)getAccountDelegateDao().getAccountDelegationsForPerson(principalId, primary);
130        }
131    
132        /**
133         * @see org.kuali.kfs.coa.service.AccountDelegateService#isPrincipalInAnyWayShapeOrFormPrimaryAccountDelegate(java.lang.String)
134         */
135        @NonTransactional
136        public boolean isPrincipalInAnyWayShapeOrFormPrimaryAccountDelegate(String principalId) {
137            return accountDelegateDao.isPrincipalInAnyWayShapeOrFormPrimaryAccountDelegate(principalId);
138        }
139    
140        /**
141         * @see org.kuali.kfs.coa.service.AccountDelegateService#isPrincipalInAnyWayShapeOrFormSecondaryAccountDelegate(java.lang.String)
142         */
143        @NonTransactional
144        public boolean isPrincipalInAnyWayShapeOrFormSecondaryAccountDelegate(String principalId) {
145            return accountDelegateDao.isPrincipalInAnyWayShapeOrFormSecondaryAccountDelegate(principalId);
146        }
147        
148        /**
149         * Saves the given account delegate to the persistence store
150         * @param accountDelegate the account delegate to save
151         */
152        @Transactional(propagation = Propagation.REQUIRES_NEW)
153        public void saveForMaintenanceDocument(AccountDelegate accountDelegate) {
154            getBusinessObjectService().linkAndSave(accountDelegate);
155        }
156        
157        /**
158         * Persists the given account delegate global maintenance document inactivations
159         * @param delegatesToInactivate the List of delegates to inactivate
160         */
161        @Transactional(propagation = Propagation.REQUIRES_NEW)
162        public void saveInactivationsForGlobalMaintenanceDocument(List<PersistableBusinessObject> delegatesToInactivate) {
163            if (delegatesToInactivate != null && !delegatesToInactivate.isEmpty()) {
164                getBusinessObjectService().save(delegatesToInactivate);
165            }
166        }
167        
168        /**
169         * Persists the given account delegate global maintenance document changes
170         * @param delegatesToChange the List of delegates to change
171         */
172        @Transactional(propagation = Propagation.REQUIRES_NEW)
173        public void saveChangesForGlobalMaintenanceDocument(List<PersistableBusinessObject> delegatesToChange) {
174            if (delegatesToChange != null && !delegatesToChange.isEmpty()) {
175                getBusinessObjectService().save(delegatesToChange);
176            }
177        }
178        
179        /**
180         * Updates the role that this delegate is part of, to account for the changes in this delegate
181         */
182        @Transactional
183        public void updateDelegationRole() {
184            final RoleManagementService roleManagementService = SpringContext.getBean(RoleManagementService.class);
185            final String roleId = roleManagementService.getRoleIdByName(KFSConstants.ParameterNamespaces.KFS, KFSConstants.SysKimConstants.FISCAL_OFFICER_KIM_ROLE_NAME);
186            if (!StringUtils.isBlank(roleId)) {
187                roleManagementService.applicationRoleMembershipChanged(roleId);
188            }
189        }
190        
191        /**
192         * Gets the accountDelegateDao attribute. 
193         * @return Returns the accountDelegateDao.
194         */
195        @NonTransactional
196        public AccountDelegateDao getAccountDelegateDao() {
197            return accountDelegateDao;
198        }
199    
200        /**
201         * Sets the accountDelegateDao attribute value.
202         * @param accountDelegateDao The accountDelegateDao to set.
203         */
204        @NonTransactional
205        public void setAccountDelegateDao(AccountDelegateDao accountDelegateDao) {
206            this.accountDelegateDao = accountDelegateDao;
207        }
208    
209        /**
210         * Gets the accountDelegateGlobalDao attribute. 
211         * @return Returns the accountDelegateGlobalDao.
212         */
213        @NonTransactional
214        public AccountDelegateGlobalDao getAccountDelegateGlobalDao() {
215            return accountDelegateGlobalDao;
216        }
217    
218        /**
219         * Sets the accountDelegateGlobalDao attribute value.
220         * @param accountDelegateGlobalDao The accountDelegateGlobalDao to set.
221         */
222        @NonTransactional
223        public void setAccountDelegateGlobalDao(AccountDelegateGlobalDao accountDelegateGlobalDao) {
224            this.accountDelegateGlobalDao = accountDelegateGlobalDao;
225        }
226    
227        /**
228         * Gets the dataDictionaryService attribute. 
229         * @return Returns the dataDictionaryService.
230         */
231        @NonTransactional
232        public DataDictionaryService getDataDictionaryService() {
233            return dataDictionaryService;
234        }
235    
236        /**
237         * Sets the dataDictionaryService attribute value.
238         * @param dataDictionaryService The dataDictionaryService to set.
239         */
240        @NonTransactional
241        public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
242            this.dataDictionaryService = dataDictionaryService;
243        }
244    
245        /**
246         * Gets the businessObjectService attribute. 
247         * @return Returns the businessObjectService.
248         */
249        @NonTransactional
250        public BusinessObjectService getBusinessObjectService() {
251            return businessObjectService;
252        }
253    
254        /**
255         * Sets the businessObjectService attribute value.
256         * @param businessObjectService The businessObjectService to set.
257         */
258        @NonTransactional
259        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
260            this.businessObjectService = businessObjectService;
261        }
262        
263    }