1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
45  
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  
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  
76  
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  
94  
95  
96  
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 
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 
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 
148 
149 
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 
159 
160 
161 
162     public ParameterService getParameterService() {
163         return SpringContext.getBean(ParameterService.class);
164     }
165 
166     
167 
168 
169 
170 
171     public AgencyService getAgencyService() {
172         return SpringContext.getBean(AgencyService.class);
173     }
174 
175     
176 
177 
178 
179 
180     public CfdaService getCfdaService() {
181         return SpringContext.getBean(CfdaService.class);
182     }
183 
184     
185 
186 
187 
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