Coverage Report - org.kuali.student.core.subjectcode.service.impl.SubjectCodeServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
SubjectCodeServiceImpl
0%
0/119
0%
0/66
2.842
 
 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
                         if("subjectCode.queryParam.code".equals(param.getKey()) && param.getValue() != null){
 125  
                                 // Note: this is a hack because of a bug in rice: KSLAB-2050
 126  0
                                 paramMap.put(param.getKey(), ((String)param.getValue()).toUpperCase());
 127  
                         }else{
 128  0
                                 paramMap.put(param.getKey(), param.getValue());
 129  
                         }
 130  
                 }
 131  
                 
 132  0
                 SearchResult searchResult = null;
 133  
                 
 134  0
             if(cachingEnabled){
 135  
                     //Get From Cache
 136  0
                     MaxAgeSoftReference<SearchResult> ref = searchCache.get(searchRequest.toString());
 137  0
                     if ( ref != null ) {
 138  0
                             searchResult = ref.get();
 139  0
                             if(searchResult != null){
 140  0
                                     return searchResult;
 141  
                             }
 142  
                     }
 143  
                 }
 144  
                 
 145  
                 //Do searches
 146  0
             if("subjectCode.search.subjectCodeGeneric".equals(searchKey)){
 147  0
                         searchResult = doSubjectCodeGenericSearch(paramMap);
 148  0
                 }else if("subjectCode.search.orgsForSubjectCode".equals(searchKey)){
 149  0
                         searchResult = doOrgsForSubjectCodeSearch(paramMap);
 150  
                 }
 151  
 
 152  
                 //Store to Cache
 153  0
             if(cachingEnabled){
 154  
                     //Store to cache
 155  0
                     searchCache.put(searchRequest.toString(), new MaxAgeSoftReference<SearchResult>( searchCacheMaxAgeSeconds, searchResult) );
 156  
             }
 157  
             
 158  0
                 return searchResult;
 159  
         }
 160  
         
 161  
     private SearchResult doOrgsForSubjectCodeSearch(Map<String, Object> paramMap) throws MissingParameterException {
 162  0
                 SearchResult searchResult = new SearchResult();
 163  0
                 Map<String,Object> queryMap = new HashMap<String,Object>();
 164  0
                 String codeParam = (String) paramMap.get("subjectCode.queryParam.code");
 165  0
                 if(codeParam!=null){ 
 166  0
                         queryMap.put("subjectCode.code", codeParam);
 167  
                 }
 168  0
                 String orgIdParam = (String) paramMap.get("subjectCode.queryParam.optionalOrgId");
 169  0
                 if(orgIdParam!=null){ 
 170  0
                         queryMap.put("orgId", orgIdParam);
 171  
                 }
 172  
                 
 173  
                 //Use the BO service to perform the query
 174  0
                 List<SubjectCodeJoinOrg> subjectCodeJoinOrgs = (List<SubjectCodeJoinOrg>) getBusinessObjectService().findMatching(SubjectCodeJoinOrg.class, queryMap);
 175  0
         Map<String,List<SearchResultRow>> orgIdToRowMapping = new HashMap<String,List<SearchResultRow>> ();
 176  
         
 177  
         //Create search results from the results of the BO search
 178  0
                 for(SubjectCodeJoinOrg subjectCodeJoinOrg:subjectCodeJoinOrgs){
 179  
                         //Only include active orgs if the search is not looking by org id
 180  0
                         if(orgIdParam!=null || subjectCodeJoinOrg.isActive()){
 181  0
                         SearchResultRow row = new SearchResultRow();
 182  0
                         row.addCell("subjectCode.resultColumn.code", subjectCodeJoinOrg.getSubjectCode().getCode());
 183  0
                         row.addCell("subjectCode.resultColumn.type", subjectCodeJoinOrg.getSubjectCode().getTypeId());
 184  0
                         row.addCell("subjectCode.resultColumn.activeFrom", subjectCodeJoinOrg.getActiveFromDate()==null?null:format.format(new Date(subjectCodeJoinOrg.getActiveFromDate().getTime())));
 185  0
                         row.addCell("subjectCode.resultColumn.activeTo", subjectCodeJoinOrg.getActiveToDate()==null?null:format.format(new Date(subjectCodeJoinOrg.getActiveToDate().getTime())));
 186  0
                         row.addCell("subjectCode.resultColumn.orgId", subjectCodeJoinOrg.getOrgId());
 187  
                         //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
 188  0
                         List<SearchResultRow> rowList = orgIdToRowMapping.get(subjectCodeJoinOrg.getOrgId());
 189  0
                         if(rowList==null){
 190  0
                                 rowList = new ArrayList<SearchResultRow>();
 191  0
                                 orgIdToRowMapping.put(subjectCodeJoinOrg.getOrgId(), rowList);
 192  
                         }
 193  0
                         rowList.add(row);
 194  
         
 195  0
                         searchResult.getRows().add(row);
 196  0
                         }
 197  
         }
 198  
                 
 199  
                 //Translate Org Id to name.  Make a searchRequest for mapping org ids to org longname/shortname
 200  
                 //Use the org search to Translate the orgIds into Org names and update the holder cells
 201  0
                 if(!orgIdToRowMapping.isEmpty()){
 202  
                         //Perform the Org search
 203  0
                         SearchRequest orgIdTranslationSearchRequest = new SearchRequest("org.search.generic");
 204  0
                         orgIdTranslationSearchRequest.addParam("org.queryParam.orgOptionalIds", new ArrayList<String>(orgIdToRowMapping.keySet()));
 205  0
                         SearchResult orgIdTranslationSearchResult = getOrganizationService().search(orgIdTranslationSearchRequest);
 206  
                         
 207  
                         //For each translation, update the result cell with the translated org name
 208  0
                         for(SearchResultRow row:orgIdTranslationSearchResult.getRows()){
 209  
                                 //Get Params
 210  0
                                 String orgId = null;
 211  0
                                 String orgShortName = null;
 212  0
                                 String orgLongName = null;
 213  0
                                 String orgType = null; 
 214  0
                                 for(SearchResultCell cell:row.getCells()){
 215  0
                                         if("org.resultColumn.orgId".equals(cell.getKey())){
 216  0
                                                 orgId = cell.getValue();
 217  0
                                         }else if("org.resultColumn.orgShortName".equals(cell.getKey())){
 218  0
                                                 orgShortName = cell.getValue();
 219  0
                                         }else if("org.resultColumn.orgOptionalLongName".equals(cell.getKey())){
 220  0
                                                 orgLongName = cell.getValue();
 221  0
                                         }else if("org.resultColumn.orgType".equals(cell.getKey())){
 222  0
                                                 orgType = cell.getValue();
 223  
                                         }
 224  
                                 }
 225  
                                 //Update the rows with the org translation
 226  0
                                 for(SearchResultRow rowToUpdate:orgIdToRowMapping.get(orgId)){
 227  0
                                         rowToUpdate.addCell("subjectCode.resultColumn.orgShortName", orgShortName);
 228  0
                                         rowToUpdate.addCell("subjectCode.resultColumn.orgLongName", orgLongName);
 229  0
                                         rowToUpdate.addCell("subjectCode.resultColumn.orgType", orgType);
 230  
                                 }
 231  0
                         }
 232  
                 }
 233  0
                 return searchResult;
 234  
         }
 235  
 
 236  
         private SearchResult doSubjectCodeGenericSearch(Map<String, Object> paramMap) {
 237  0
                 SearchResult searchResult = new SearchResult();
 238  0
                 Map<String,Object> queryMap = new HashMap<String,Object>();
 239  0
                 String code = (String) paramMap.get("subjectCode.queryParam.code");
 240  0
                 if(code!=null){ 
 241  0
                         queryMap.put("code", "*" + paramMap.get("subjectCode.queryParam.code") + "*");
 242  
                 }
 243  
 
 244  0
                 List<SubjectCode> subjectCodes = (List<SubjectCode>) getLookupService().findCollectionBySearchHelper(SubjectCode.class, queryMap, true);
 245  
         
 246  0
         for(SubjectCode subjectCode:subjectCodes){
 247  0
                 SearchResultRow row = new SearchResultRow();
 248  0
                 row.addCell("subjectCode.resultColumn.id", subjectCode.getId());
 249  0
                 row.addCell("subjectCode.resultColumn.code", subjectCode.getCode());
 250  0
                 row.addCell("subjectCode.resultColumn.name", subjectCode.getName());
 251  0
                 row.addCell("subjectCode.resultColumn.type", subjectCode.getTypeId());
 252  0
                 searchResult.getRows().add(row);
 253  0
         }
 254  0
         return searchResult;
 255  
         }
 256  
 
 257  
         protected BusinessObjectService getBusinessObjectService() {
 258  0
         if (businessObjectService == null) {
 259  0
             businessObjectService = KNSServiceLocator.getBusinessObjectService();
 260  
         }
 261  0
         return  businessObjectService;
 262  
     }
 263  
         protected LookupService getLookupService() {
 264  0
         if (lookupService == null) {
 265  0
                 lookupService = KNSServiceLocator.getLookupService();
 266  
         }
 267  0
         return lookupService;
 268  
     }
 269  
     
 270  
         protected static OrganizationService getOrganizationService() {
 271  0
                 if (organizationService == null) {
 272  0
                         organizationService = (OrganizationService) GlobalResourceLoader
 273  
                                         .getService(new QName(
 274  
                                                         "http://student.kuali.org/wsdl/organization",
 275  
                                                         "OrganizationService"));
 276  
                 }
 277  0
                 return organizationService;
 278  
         }
 279  
         
 280  
         @Override
 281  
         public void afterPropertiesSet() throws Exception {
 282  0
                 if(cachingEnabled){
 283  0
                         searchCache = Collections.synchronizedMap( new MaxSizeMap<String,MaxAgeSoftReference<SearchResult>>( searchCacheMaxSize ) );
 284  
                 }
 285  0
         }
 286  
         
 287  
         public void setCachingEnabled(boolean cachingEnabled) {
 288  0
                 this.cachingEnabled = cachingEnabled;
 289  0
         }
 290  
 
 291  
         public void setSearchCacheMaxSize(int searchCacheMaxSize) {
 292  0
                 this.searchCacheMaxSize = searchCacheMaxSize;
 293  0
         }
 294  
 
 295  
         public void setSearchCacheMaxAgeSeconds(int searchCacheMaxAgeSeconds) {
 296  0
                 this.searchCacheMaxAgeSeconds = searchCacheMaxAgeSeconds;
 297  0
         }
 298  
 
 299  
         public void setSearchManager(SearchManager searchManager) {
 300  0
                 this.searchManager = searchManager;
 301  0
         }
 302  
 }