1 | |
package org.kuali.student.core.subjectcode.service.impl; |
2 | |
|
3 | |
import java.text.DateFormat; |
4 | |
import java.text.SimpleDateFormat; |
5 | |
import java.util.ArrayList; |
6 | |
import java.util.Collections; |
7 | |
import java.util.Comparator; |
8 | |
import java.util.Date; |
9 | |
import java.util.HashMap; |
10 | |
import java.util.List; |
11 | |
import java.util.Map; |
12 | |
|
13 | |
import javax.jws.WebService; |
14 | |
import javax.jws.soap.SOAPBinding; |
15 | |
import javax.xml.namespace.QName; |
16 | |
|
17 | |
import org.kuali.rice.core.resourceloader.GlobalResourceLoader; |
18 | |
import org.kuali.rice.core.util.MaxAgeSoftReference; |
19 | |
import org.kuali.rice.core.util.MaxSizeMap; |
20 | |
import org.kuali.rice.kns.service.BusinessObjectService; |
21 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
22 | |
import org.kuali.rice.kns.service.LookupService; |
23 | |
import org.kuali.student.common.exceptions.DoesNotExistException; |
24 | |
import org.kuali.student.common.exceptions.InvalidParameterException; |
25 | |
import org.kuali.student.common.exceptions.MissingParameterException; |
26 | |
import org.kuali.student.common.exceptions.OperationFailedException; |
27 | |
import org.kuali.student.common.search.dto.SearchCriteriaTypeInfo; |
28 | |
import org.kuali.student.common.search.dto.SearchParam; |
29 | |
import org.kuali.student.common.search.dto.SearchRequest; |
30 | |
import org.kuali.student.common.search.dto.SearchResult; |
31 | |
import org.kuali.student.common.search.dto.SearchResultCell; |
32 | |
import org.kuali.student.common.search.dto.SearchResultRow; |
33 | |
import org.kuali.student.common.search.dto.SearchResultTypeInfo; |
34 | |
import org.kuali.student.common.search.dto.SearchTypeInfo; |
35 | |
import org.kuali.student.common.search.service.SearchManager; |
36 | |
import org.kuali.student.core.organization.service.OrganizationService; |
37 | |
import org.kuali.student.core.subjectcode.bo.SubjectCode; |
38 | |
import org.kuali.student.core.subjectcode.bo.SubjectCodeJoinOrg; |
39 | |
import org.kuali.student.core.subjectcode.service.SubjectCodeService; |
40 | |
import org.springframework.beans.factory.InitializingBean; |
41 | |
|
42 | |
@WebService(endpointInterface = "org.kuali.student.core.subjectcode.service.SubjectCodeService", serviceName = "SubjectCodeService", portName = "SubjectCodeService", targetNamespace = "http://student.kuali.org/wsdl/subjectCode") |
43 | |
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED) |
44 | 0 | public class SubjectCodeServiceImpl implements SubjectCodeService, InitializingBean{ |
45 | |
|
46 | |
private static OrganizationService organizationService; |
47 | |
private static LookupService lookupService; |
48 | |
private BusinessObjectService businessObjectService; |
49 | |
private SearchManager searchManager; |
50 | 0 | private static DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); |
51 | |
|
52 | 0 | protected boolean cachingEnabled = false; |
53 | 0 | protected int searchCacheMaxSize = 20; |
54 | 0 | protected int searchCacheMaxAgeSeconds = 90; |
55 | |
protected Map<String,MaxAgeSoftReference<SearchResult>> searchCache; |
56 | |
|
57 | |
@Override |
58 | |
public List<SearchTypeInfo> getSearchTypes() |
59 | |
throws OperationFailedException { |
60 | 0 | return searchManager.getSearchTypes(); |
61 | |
} |
62 | |
|
63 | |
@Override |
64 | |
public SearchTypeInfo getSearchType(String searchTypeKey) |
65 | |
throws DoesNotExistException, InvalidParameterException, |
66 | |
MissingParameterException, OperationFailedException { |
67 | 0 | return searchManager.getSearchType(searchTypeKey); |
68 | |
} |
69 | |
|
70 | |
@Override |
71 | |
public List<SearchTypeInfo> getSearchTypesByResult( |
72 | |
String searchResultTypeKey) throws DoesNotExistException, |
73 | |
InvalidParameterException, MissingParameterException, |
74 | |
OperationFailedException { |
75 | 0 | return searchManager.getSearchTypesByResult(searchResultTypeKey); |
76 | |
} |
77 | |
|
78 | |
@Override |
79 | |
public List<SearchTypeInfo> getSearchTypesByCriteria( |
80 | |
String searchCriteriaTypeKey) throws DoesNotExistException, |
81 | |
InvalidParameterException, MissingParameterException, |
82 | |
OperationFailedException { |
83 | 0 | return searchManager.getSearchTypesByCriteria(searchCriteriaTypeKey); |
84 | |
} |
85 | |
|
86 | |
@Override |
87 | |
public List<SearchResultTypeInfo> getSearchResultTypes() |
88 | |
throws OperationFailedException { |
89 | 0 | return searchManager.getSearchResultTypes(); |
90 | |
} |
91 | |
|
92 | |
@Override |
93 | |
public SearchResultTypeInfo getSearchResultType(String searchResultTypeKey) |
94 | |
throws DoesNotExistException, InvalidParameterException, |
95 | |
MissingParameterException, OperationFailedException { |
96 | 0 | return searchManager.getSearchResultType(searchResultTypeKey); |
97 | |
} |
98 | |
|
99 | |
@Override |
100 | |
public List<SearchCriteriaTypeInfo> getSearchCriteriaTypes() |
101 | |
throws OperationFailedException { |
102 | 0 | return searchManager.getSearchCriteriaTypes(); |
103 | |
} |
104 | |
|
105 | |
@Override |
106 | |
public SearchCriteriaTypeInfo getSearchCriteriaType( |
107 | |
String searchCriteriaTypeKey) throws DoesNotExistException, |
108 | |
InvalidParameterException, MissingParameterException, |
109 | |
OperationFailedException { |
110 | 0 | return searchManager.getSearchCriteriaType(searchCriteriaTypeKey); |
111 | |
} |
112 | |
|
113 | |
@Override |
114 | |
public SearchResult search(SearchRequest searchRequest) |
115 | |
throws MissingParameterException { |
116 | 0 | String searchKey = searchRequest.getSearchKey(); |
117 | |
|
118 | 0 | if(searchKey==null||searchKey.isEmpty()){ |
119 | 0 | throw new MissingParameterException("searchRequest parameter is required."); |
120 | |
} |
121 | |
|
122 | |
|
123 | 0 | Map<String,Object> paramMap = new HashMap<String,Object>(); |
124 | 0 | for(SearchParam param:searchRequest.getParams()){ |
125 | 0 | if("subjectCode.queryParam.code".equals(param.getKey()) && param.getValue() != null){ |
126 | |
|
127 | 0 | paramMap.put(param.getKey(), ((String)param.getValue()).toUpperCase()); |
128 | |
}else{ |
129 | 0 | paramMap.put(param.getKey(), param.getValue()); |
130 | |
} |
131 | |
} |
132 | |
|
133 | 0 | SearchResult searchResult = null; |
134 | |
|
135 | 0 | if(cachingEnabled){ |
136 | |
|
137 | 0 | MaxAgeSoftReference<SearchResult> ref = searchCache.get(searchRequest.toString()); |
138 | 0 | if ( ref != null ) { |
139 | 0 | searchResult = ref.get(); |
140 | 0 | if(searchResult != null){ |
141 | 0 | return searchResult; |
142 | |
} |
143 | |
} |
144 | |
} |
145 | |
|
146 | |
|
147 | 0 | if("subjectCode.search.subjectCodeGeneric".equals(searchKey)){ |
148 | 0 | searchResult = doSubjectCodeGenericSearch(paramMap); |
149 | 0 | }else if("subjectCode.search.orgsForSubjectCode".equals(searchKey)){ |
150 | 0 | searchResult = doOrgsForSubjectCodeSearch(paramMap); |
151 | |
} |
152 | |
|
153 | |
|
154 | 0 | if(cachingEnabled){ |
155 | |
|
156 | 0 | searchCache.put(searchRequest.toString(), new MaxAgeSoftReference<SearchResult>( searchCacheMaxAgeSeconds, searchResult) ); |
157 | |
} |
158 | |
|
159 | 0 | return searchResult; |
160 | |
} |
161 | |
|
162 | |
private SearchResult doOrgsForSubjectCodeSearch(Map<String, Object> paramMap) throws MissingParameterException { |
163 | 0 | SearchResult searchResult = new SearchResult(); |
164 | 0 | Map<String,Object> queryMap = new HashMap<String,Object>(); |
165 | 0 | String codeParam = (String) paramMap.get("subjectCode.queryParam.code"); |
166 | 0 | if(codeParam!=null){ |
167 | 0 | queryMap.put("subjectCode.code", codeParam); |
168 | |
} |
169 | 0 | String orgIdParam = (String) paramMap.get("subjectCode.queryParam.optionalOrgId"); |
170 | 0 | if(orgIdParam!=null){ |
171 | 0 | queryMap.put("orgId", orgIdParam); |
172 | |
} |
173 | |
|
174 | |
|
175 | 0 | List<SubjectCodeJoinOrg> subjectCodeJoinOrgs = (List<SubjectCodeJoinOrg>) getBusinessObjectService().findMatching(SubjectCodeJoinOrg.class, queryMap); |
176 | 0 | Map<String,List<SearchResultRow>> orgIdToRowMapping = new HashMap<String,List<SearchResultRow>> (); |
177 | |
|
178 | |
|
179 | 0 | for(SubjectCodeJoinOrg subjectCodeJoinOrg:subjectCodeJoinOrgs){ |
180 | |
|
181 | 0 | if(orgIdParam!=null || subjectCodeJoinOrg.isActive()){ |
182 | 0 | SearchResultRow row = new SearchResultRow(); |
183 | 0 | row.addCell("subjectCode.resultColumn.code", subjectCodeJoinOrg.getSubjectCode().getCode()); |
184 | 0 | row.addCell("subjectCode.resultColumn.type", subjectCodeJoinOrg.getSubjectCode().getTypeId()); |
185 | 0 | row.addCell("subjectCode.resultColumn.activeFrom", subjectCodeJoinOrg.getActiveFromDate()==null?null:format.format(new Date(subjectCodeJoinOrg.getActiveFromDate().getTime()))); |
186 | 0 | row.addCell("subjectCode.resultColumn.activeTo", subjectCodeJoinOrg.getActiveToDate()==null?null:format.format(new Date(subjectCodeJoinOrg.getActiveToDate().getTime()))); |
187 | 0 | row.addCell("subjectCode.resultColumn.orgId", subjectCodeJoinOrg.getOrgId()); |
188 | |
|
189 | 0 | List<SearchResultRow> rowList = orgIdToRowMapping.get(subjectCodeJoinOrg.getOrgId()); |
190 | 0 | if(rowList==null){ |
191 | 0 | rowList = new ArrayList<SearchResultRow>(); |
192 | 0 | orgIdToRowMapping.put(subjectCodeJoinOrg.getOrgId(), rowList); |
193 | |
} |
194 | 0 | rowList.add(row); |
195 | |
|
196 | 0 | searchResult.getRows().add(row); |
197 | 0 | } |
198 | |
} |
199 | |
|
200 | |
|
201 | |
|
202 | 0 | if(!orgIdToRowMapping.isEmpty()){ |
203 | |
|
204 | 0 | SearchRequest orgIdTranslationSearchRequest = new SearchRequest("org.search.generic"); |
205 | 0 | orgIdTranslationSearchRequest.addParam("org.queryParam.orgOptionalIds", new ArrayList<String>(orgIdToRowMapping.keySet())); |
206 | 0 | SearchResult orgIdTranslationSearchResult = getOrganizationService().search(orgIdTranslationSearchRequest); |
207 | |
|
208 | |
|
209 | 0 | for(SearchResultRow row:orgIdTranslationSearchResult.getRows()){ |
210 | |
|
211 | 0 | String orgId = null; |
212 | 0 | String orgShortName = null; |
213 | 0 | String orgLongName = null; |
214 | 0 | String orgType = null; |
215 | 0 | for(SearchResultCell cell:row.getCells()){ |
216 | 0 | if("org.resultColumn.orgId".equals(cell.getKey())){ |
217 | 0 | orgId = cell.getValue(); |
218 | 0 | }else if("org.resultColumn.orgShortName".equals(cell.getKey())){ |
219 | 0 | orgShortName = cell.getValue(); |
220 | 0 | }else if("org.resultColumn.orgOptionalLongName".equals(cell.getKey())){ |
221 | 0 | orgLongName = cell.getValue(); |
222 | 0 | }else if("org.resultColumn.orgType".equals(cell.getKey())){ |
223 | 0 | orgType = cell.getValue(); |
224 | |
} |
225 | |
} |
226 | |
|
227 | 0 | for(SearchResultRow rowToUpdate:orgIdToRowMapping.get(orgId)){ |
228 | 0 | rowToUpdate.addCell("subjectCode.resultColumn.orgShortName", orgShortName); |
229 | 0 | rowToUpdate.addCell("subjectCode.resultColumn.orgLongName", orgLongName); |
230 | 0 | rowToUpdate.addCell("subjectCode.resultColumn.orgType", orgType); |
231 | |
} |
232 | 0 | } |
233 | |
} |
234 | 0 | return searchResult; |
235 | |
} |
236 | |
|
237 | |
private SearchResult doSubjectCodeGenericSearch(Map<String, Object> paramMap) { |
238 | 0 | SearchResult searchResult = new SearchResult(); |
239 | 0 | Map<String,Object> queryMap = new HashMap<String,Object>(); |
240 | 0 | String code = (String) paramMap.get("subjectCode.queryParam.code"); |
241 | 0 | if(code!=null){ |
242 | 0 | queryMap.put("code", "*" + paramMap.get("subjectCode.queryParam.code") + "*"); |
243 | |
} |
244 | |
|
245 | |
|
246 | 0 | List<SubjectCode> subjectCodes = (List<SubjectCode>) getLookupService().findCollectionBySearchHelper(SubjectCode.class, queryMap, true); |
247 | |
|
248 | |
|
249 | 0 | Collections.sort(subjectCodes,new Comparator<SubjectCode>(){ |
250 | |
@Override |
251 | |
public int compare(SubjectCode o1, SubjectCode o2) { |
252 | 0 | if(o1==null && o2==null){ |
253 | 0 | return 0; |
254 | |
} |
255 | 0 | return o1.getCode().compareTo(o2.getCode()); |
256 | |
}}); |
257 | |
|
258 | |
|
259 | 0 | for(SubjectCode subjectCode:subjectCodes){ |
260 | 0 | SearchResultRow row = new SearchResultRow(); |
261 | 0 | row.addCell("subjectCode.resultColumn.id", subjectCode.getId()); |
262 | 0 | row.addCell("subjectCode.resultColumn.code", subjectCode.getCode()); |
263 | 0 | row.addCell("subjectCode.resultColumn.name", subjectCode.getName()); |
264 | 0 | row.addCell("subjectCode.resultColumn.type", subjectCode.getTypeId()); |
265 | 0 | searchResult.getRows().add(row); |
266 | 0 | } |
267 | 0 | return searchResult; |
268 | |
} |
269 | |
|
270 | |
protected BusinessObjectService getBusinessObjectService() { |
271 | 0 | if (businessObjectService == null) { |
272 | 0 | businessObjectService = KNSServiceLocator.getBusinessObjectService(); |
273 | |
} |
274 | 0 | return businessObjectService; |
275 | |
} |
276 | |
protected LookupService getLookupService() { |
277 | 0 | if (lookupService == null) { |
278 | 0 | lookupService = KNSServiceLocator.getLookupService(); |
279 | |
} |
280 | 0 | return lookupService; |
281 | |
} |
282 | |
|
283 | |
protected static OrganizationService getOrganizationService() { |
284 | 0 | if (organizationService == null) { |
285 | 0 | organizationService = (OrganizationService) GlobalResourceLoader |
286 | |
.getService(new QName( |
287 | |
"http://student.kuali.org/wsdl/organization", |
288 | |
"OrganizationService")); |
289 | |
} |
290 | 0 | return organizationService; |
291 | |
} |
292 | |
|
293 | |
@Override |
294 | |
public void afterPropertiesSet() throws Exception { |
295 | 0 | if(cachingEnabled){ |
296 | 0 | searchCache = Collections.synchronizedMap( new MaxSizeMap<String,MaxAgeSoftReference<SearchResult>>( searchCacheMaxSize ) ); |
297 | |
} |
298 | 0 | } |
299 | |
|
300 | |
public void setCachingEnabled(boolean cachingEnabled) { |
301 | 0 | this.cachingEnabled = cachingEnabled; |
302 | 0 | } |
303 | |
|
304 | |
public void setSearchCacheMaxSize(int searchCacheMaxSize) { |
305 | 0 | this.searchCacheMaxSize = searchCacheMaxSize; |
306 | 0 | } |
307 | |
|
308 | |
public void setSearchCacheMaxAgeSeconds(int searchCacheMaxAgeSeconds) { |
309 | 0 | this.searchCacheMaxAgeSeconds = searchCacheMaxAgeSeconds; |
310 | 0 | } |
311 | |
|
312 | |
public void setSearchManager(SearchManager searchManager) { |
313 | 0 | this.searchManager = searchManager; |
314 | 0 | } |
315 | |
} |