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