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.module.cg.service.impl;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
020    import java.util.HashMap;
021    import java.util.List;
022    import java.util.Map;
023    
024    import org.apache.commons.lang.StringUtils;
025    import org.kuali.kfs.coa.businessobject.Account;
026    import org.kuali.kfs.integration.cg.ContractsAndGrantsModuleService;
027    import org.kuali.kfs.module.cg.CGConstants;
028    import org.kuali.kfs.module.cg.businessobject.AwardAccount;
029    import org.kuali.kfs.module.cg.service.AgencyService;
030    import org.kuali.kfs.module.cg.service.CfdaService;
031    import org.kuali.kfs.sys.KFSPropertyConstants;
032    import org.kuali.kfs.sys.context.SpringContext;
033    import org.kuali.kfs.sys.service.NonTransactional;
034    import org.kuali.kfs.sys.service.impl.KfsParameterConstants;
035    import org.kuali.rice.kim.bo.Person;
036    import org.kuali.rice.kns.service.BusinessObjectService;
037    import org.kuali.rice.kns.service.ParameterService;
038    
039    @NonTransactional
040    public class ContractsAndGrantsModuleServiceImpl implements ContractsAndGrantsModuleService {
041        private org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ContractsAndGrantsModuleServiceImpl.class);
042    
043        /**
044         * @see org.kuali.kfs.integration.cg.ContractsAndGrantsModuleService#getProjectDirectorForAccount(java.lang.String,
045         *      java.lang.String)
046         */
047        public Person getProjectDirectorForAccount(String chartOfAccountsCode, String accountNumber) {
048            Map<String, Object> awardAccountMap = new HashMap<String, Object>();
049            awardAccountMap.put(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
050            awardAccountMap.put(KFSPropertyConstants.ACCOUNT_NUMBER, accountNumber);
051    
052            Collection<AwardAccount> proposals = getBusinessObjectService().findMatchingOrderBy(AwardAccount.class, awardAccountMap, KFSPropertyConstants.PROPOSAL_NUMBER, false);
053            if (proposals != null && !proposals.isEmpty()) {
054                AwardAccount proposalWithMaxProposalNumber = proposals.iterator().next();
055    
056                return proposalWithMaxProposalNumber.getProjectDirector();
057            }
058    
059            return null;
060        }
061    
062        /**
063         * @see org.kuali.kfs.integration.service.ContractsAndGrantsModuleService#getProjectDirectorForAccount(org.kuali.kfs.coa.businessobject.Account)
064         */
065        public Person getProjectDirectorForAccount(Account account) {
066            if (account != null) {
067                String chartOfAccountsCode = account.getChartOfAccountsCode();
068                String accountNumber = account.getAccountNumber();
069                return this.getProjectDirectorForAccount(chartOfAccountsCode, accountNumber);
070            }
071            return null;
072        }
073    
074        /**
075         * @see org.kuali.kfs.integration.service.ContractsAndGrantsModuleService#isAwardedByFederalAgency(java.lang.String,
076         *      java.lang.String, java.util.List)
077         */
078        public boolean isAwardedByFederalAgency(String chartOfAccountsCode, String accountNumber, List<String> federalAgencyTypeCodes) {
079            AwardAccount primaryAward = getPrimaryAwardAccount(chartOfAccountsCode, accountNumber);
080            if (primaryAward == null) {
081                return false;
082            }
083    
084            String agencyTypeCode = primaryAward.getAward().getAgency().getAgencyTypeCode();
085            if (federalAgencyTypeCodes.contains(agencyTypeCode) || primaryAward.getAward().getFederalPassThroughIndicator()) {
086                return true;
087            }
088    
089            return false;
090        }
091    
092        /**
093         * get the primary award account for the given account
094         * 
095         * @param account the given account
096         * @return the primary award account for the given account
097         */
098        protected AwardAccount getPrimaryAwardAccount(String chartOfAccountsCode, String accountNumber) {
099            AwardAccount primaryAwardAccount = null;
100            long highestProposalNumber = 0;
101    
102            Map<String, Object> accountKeyValues = new HashMap<String, Object>();
103            accountKeyValues.put(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
104            accountKeyValues.put(KFSPropertyConstants.ACCOUNT_NUMBER, accountNumber);
105    
106            for (Object awardAccountAsObject : getBusinessObjectService().findMatching(AwardAccount.class, accountKeyValues)) {
107                AwardAccount awardAccount = (AwardAccount) awardAccountAsObject;
108                Long proposalNumber = awardAccount.getProposalNumber();
109    
110                if (proposalNumber >= highestProposalNumber) {
111                    highestProposalNumber = proposalNumber;
112                    primaryAwardAccount = awardAccount;
113                }
114            }
115    
116            return primaryAwardAccount;
117        }
118    
119        /**
120         * @see org.kuali.kfs.integration.cg.ContractsAndGrantsModuleService#getAllAccountReponsiblityIds()
121         */
122        public List<Integer> getAllAccountReponsiblityIds() {
123            int maxResponsibilityId = this.getMaxiumAccountResponsibilityId();
124    
125            List<Integer> contractsAndGrantsReponsiblityIds = new ArrayList<Integer>();
126            for (int id = 1; id <= maxResponsibilityId; id++) {
127                contractsAndGrantsReponsiblityIds.add(id);
128            }
129    
130            return contractsAndGrantsReponsiblityIds;
131        }
132    
133        /**
134         * @see org.kuali.kfs.integration.cg.ContractsAndGrantsModuleService#hasValidAccountReponsiblityIdIfExists(org.kuali.kfs.coa.businessobject.Account)
135         */
136        public boolean hasValidAccountReponsiblityIdIfNotNull(Account account) {
137            Integer accountResponsiblityId = account.getContractsAndGrantsAccountResponsibilityId();
138    
139            if (accountResponsiblityId == null) {
140                return true;
141            }
142    
143            return accountResponsiblityId >= 1 && accountResponsiblityId <= this.getMaxiumAccountResponsibilityId();
144        }
145    
146        /**
147         * retieve the maxium account responsiblity id from system parameter
148         * 
149         * @return the maxium account responsiblity id from system parameter
150         */
151        protected int getMaxiumAccountResponsibilityId() {
152            String maxResponsibilityId = getParameterService().getParameterValue(KfsParameterConstants.CONTRACTS_AND_GRANTS_ALL.class, CGConstants.MAXIMUM_ACCOUNT_RESPONSIBILITY_ID);
153    
154            return Integer.valueOf(maxResponsibilityId);
155        }
156    
157        /**
158         * Returns an implementation of the parameterService
159         * 
160         * @return an implementation of the parameterService
161         */
162        public ParameterService getParameterService() {
163            return SpringContext.getBean(ParameterService.class);
164        }
165    
166        /**
167         * Returns the default implementation of the C&G AgencyService
168         * 
169         * @return an implementation of AgencyService
170         */
171        public AgencyService getAgencyService() {
172            return SpringContext.getBean(AgencyService.class);
173        }
174    
175        /**
176         * Returns an implementation of the CfdaService
177         * 
178         * @return an implementation of the CfdaService
179         */
180        public CfdaService getCfdaService() {
181            return SpringContext.getBean(CfdaService.class);
182        }
183    
184        /**
185         * Returns an implementation of the BusinessObjectService
186         * 
187         * @return an implementation of the BusinessObjectService
188         */
189        public BusinessObjectService getBusinessObjectService() {
190            return SpringContext.getBean(BusinessObjectService.class);
191        }
192        
193        public List<String> getParentUnits(String unitNumber) {
194            return null;
195        }
196    
197        public String getProposalNumberForAccountAndProjectDirector(String chartOfAccountsCode, String accountNumber, String projectDirectorId) {
198            String proposalNumber = null;
199            
200            Map<String, Object> awardAccountMap = new HashMap<String, Object>();
201            awardAccountMap.put(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
202            awardAccountMap.put(KFSPropertyConstants.ACCOUNT_NUMBER, accountNumber);
203    
204            Collection<AwardAccount> proposals = getBusinessObjectService().findMatchingOrderBy(AwardAccount.class, awardAccountMap, KFSPropertyConstants.PROPOSAL_NUMBER, false);
205            if (proposals != null && !proposals.isEmpty()) {
206                AwardAccount proposalWithMaxProposalNumber = proposals.iterator().next();
207    
208                if( StringUtils.equalsIgnoreCase(proposalWithMaxProposalNumber.getProjectDirector().getPrincipalId(), projectDirectorId) ){
209                    proposalNumber = proposalWithMaxProposalNumber.getProposalNumber().toString();
210                }
211            }
212            
213            return proposalNumber;
214        }
215    }
216