View Javadoc

1   /*
2    * Copyright 2007-2008 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.module.cg.service.impl;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.kuali.ole.coa.businessobject.Account;
26  import org.kuali.ole.integration.cg.ContractsAndGrantsModuleService;
27  import org.kuali.ole.module.cg.CGConstants;
28  import org.kuali.ole.module.cg.businessobject.AwardAccount;
29  import org.kuali.ole.module.cg.service.AgencyService;
30  import org.kuali.ole.coa.service.CfdaService;
31  import org.kuali.ole.sys.OLEPropertyConstants;
32  import org.kuali.ole.sys.context.SpringContext;
33  import org.kuali.ole.sys.service.NonTransactional;
34  import org.kuali.ole.sys.service.impl.OleParameterConstants;
35  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
36  import org.kuali.rice.kim.api.identity.Person;
37  import org.kuali.rice.krad.service.BusinessObjectService;
38  
39  @NonTransactional
40  public class ContractsAndGrantsModuleServiceImpl implements ContractsAndGrantsModuleService {
41      private org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ContractsAndGrantsModuleServiceImpl.class);
42  
43      /**
44       * @see org.kuali.ole.integration.cg.ContractsAndGrantsModuleService#getProjectDirectorForAccount(java.lang.String,
45       *      java.lang.String)
46       */
47      public Person getProjectDirectorForAccount(String chartOfAccountsCode, String accountNumber) {
48          Map<String, Object> awardAccountMap = new HashMap<String, Object>();
49          awardAccountMap.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
50          awardAccountMap.put(OLEPropertyConstants.ACCOUNT_NUMBER, accountNumber);
51  
52          Collection<AwardAccount> proposals = getBusinessObjectService().findMatchingOrderBy(AwardAccount.class, awardAccountMap, OLEPropertyConstants.PROPOSAL_NUMBER, false);
53          if (proposals != null && !proposals.isEmpty()) {
54              AwardAccount proposalWithMaxProposalNumber = proposals.iterator().next();
55  
56              return proposalWithMaxProposalNumber.getProjectDirector();
57          }
58  
59          return null;
60      }
61  
62      /**
63       * @see org.kuali.ole.integration.service.ContractsAndGrantsModuleService#getProjectDirectorForAccount(org.kuali.ole.coa.businessobject.Account)
64       */
65      public Person getProjectDirectorForAccount(Account account) {
66          if (account != null) {
67              String chartOfAccountsCode = account.getChartOfAccountsCode();
68              String accountNumber = account.getAccountNumber();
69              return this.getProjectDirectorForAccount(chartOfAccountsCode, accountNumber);
70          }
71          return null;
72      }
73  
74      /**
75       * @see org.kuali.ole.integration.service.ContractsAndGrantsModuleService#isAwardedByFederalAgency(java.lang.String,
76       *      java.lang.String, java.util.List)
77       */
78      public boolean isAwardedByFederalAgency(String chartOfAccountsCode, String accountNumber, Collection<String> federalAgencyTypeCodes) {
79          AwardAccount primaryAward = getPrimaryAwardAccount(chartOfAccountsCode, accountNumber);
80          if (primaryAward == null) {
81              return false;
82          }
83  
84          String agencyTypeCode = primaryAward.getAward().getAgency().getAgencyTypeCode();
85          if (federalAgencyTypeCodes.contains(agencyTypeCode) || primaryAward.getAward().getFederalPassThroughIndicator()) {
86              return true;
87          }
88  
89          return false;
90      }
91  
92      /**
93       * get the primary award account for the given account
94       * 
95       * @param account the given account
96       * @return the primary award account for the given account
97       */
98      protected AwardAccount getPrimaryAwardAccount(String chartOfAccountsCode, String accountNumber) {
99          AwardAccount primaryAwardAccount = null;
100         long highestProposalNumber = 0;
101 
102         Map<String, Object> accountKeyValues = new HashMap<String, Object>();
103         accountKeyValues.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
104         accountKeyValues.put(OLEPropertyConstants.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.ole.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.ole.integration.cg.ContractsAndGrantsModuleService#hasValidAccountReponsiblityIdIfExists(org.kuali.ole.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().getParameterValueAsString(OleParameterConstants.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(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
202         awardAccountMap.put(OLEPropertyConstants.ACCOUNT_NUMBER, accountNumber);
203 
204         Collection<AwardAccount> proposals = getBusinessObjectService().findMatchingOrderBy(AwardAccount.class, awardAccountMap, OLEPropertyConstants.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