View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.r1.common.assembly.util;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.log4j.Logger;
22  import org.kuali.student.r1.common.assembly.data.AssemblyException;
23  import org.kuali.student.r1.common.assembly.data.LookupMetadata;
24  import org.kuali.student.r1.common.assembly.data.LookupParamMetadata;
25  import org.kuali.student.r1.common.assembly.data.Metadata.WriteAccess;
26  import org.kuali.student.r2.common.dto.ContextInfo;
27  import org.kuali.student.r2.common.exceptions.MissingParameterException;
28  import org.kuali.student.r2.common.exceptions.OperationFailedException;
29  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
30  import org.kuali.student.r2.core.search.dto.*;
31  import org.kuali.student.r2.core.search.service.SearchService;
32  import org.kuali.student.r2.common.util.ContextUtils;
33  
34  /**
35   * Builds and issues SearchRequests parameterized with the information from LookupMetadata and the provided ID
36   * 
37   * @author Kuali Student Team
38   *
39   */
40  public class IdTranslator {
41      
42      private static final String ENUMERATION = "enumeration";
43  	private List<SearchParamInfo> additionalParams = new ArrayList<SearchParamInfo>();
44      private SearchService searchDispatcher;
45      
46      final Logger LOG = Logger.getLogger(IdTranslator.class);
47  
48      public IdTranslator(SearchService searchDispatcher) throws AssemblyException {
49      	this.searchDispatcher = searchDispatcher;
50      }
51      
52  	private SearchRequestInfo buildSearchRequestById(LookupMetadata lookupMetadata, String searchId) {
53      	SearchRequestInfo sr = new SearchRequestInfo();
54      	sr.setNeededTotalResults(false);
55      	
56      	sr.setSearchKey(lookupMetadata.getSearchTypeId());
57      	sr.setSortColumn(lookupMetadata.getResultSortKey());
58  
59  
60      	List<SearchParamInfo> searchParams = new ArrayList<SearchParamInfo>();
61      	if (lookupMetadata.getSearchParamIdKey() != null) {
62      		//FIXME: workaround until orch dict can handle multi part keys on initiallookup defs
63      		if (lookupMetadata.getSearchParamIdKey().contains(ENUMERATION)) {
64      			for (LookupParamMetadata p : lookupMetadata.getParams()) {
65      				if (p.getWriteAccess() != null && p.getWriteAccess().equals(WriteAccess.NEVER) && p.getDefaultValueString() != null) {
66      					SearchParamInfo param = createParam(p.getKey(), p.getDefaultValueString());
67      					searchParams.add(param);   							    
68      				}
69      				else if (p.getWriteAccess() == null || !p.getWriteAccess().equals(WriteAccess.NEVER)){
70      					SearchParamInfo param = createParam(p.getKey(), searchId);
71      					searchParams.add(param);   								
72      				}
73      			}
74      		}
75      		else {
76      			SearchParamInfo param = createParam(lookupMetadata.getSearchParamIdKey(), searchId);
77      			searchParams.add(param);	
78      		}
79  
80      		sr.setParams(searchParams); 		
81      	}	
82      	else {
83      		LOG.warn("Unable to build search request for " + lookupMetadata.getSearchTypeId() + " translation for id " + searchId);
84      		
85      	}
86  
87      	
88      	sr.getParams().addAll(additionalParams);
89  
90          return sr;
91      }
92      
93      private SearchParamInfo createParam(String key, String value) {
94      	SearchParamInfo param = new SearchParamInfo();
95      	
96      	if(key == null) {
97  			param.setKey("");
98  		} else {
99  			param.setKey(key);
100 		}
101 
102     	if(value == null) {
103 			param.getValues().add("");
104 		} else {
105 			param.getValues().add(value);
106 		}
107     	
108     	return param;
109     }
110     
111     public IdTranslation getTranslation(LookupMetadata lookupMetadata, String id) throws AssemblyException {
112         SearchRequestInfo searchRequest = buildSearchRequestById(lookupMetadata, id);
113         SearchResultInfo searchResults = null;
114 
115         try {
116             searchResults = searchDispatcher.search(searchRequest, ContextUtils.getContextInfo());
117         } catch (Exception e) {
118             throw new RuntimeException(e);
119         }
120 
121         IdTranslation result = null; 
122         if(searchResults != null && searchResults.getRows().size() > 0){
123         	result = new IdTranslation();
124         	result.setId(id);
125         	
126         	SearchResultRowInfo r = searchResults.getRows().get(0);
127             for(SearchResultCellInfo c: r.getCells()){
128                 if(c.getKey().equals(lookupMetadata.getResultDisplayKey())){
129                     result.addAttribute(c.getKey(), c.getValue());
130                     result.setDisplay(c.getValue());
131                 } else {
132                     result.addAttribute(c.getKey(), c.getValue());
133                 }
134             }
135         }
136         return result;
137         
138     }
139 
140     public String getTranslationForAtp(String value, ContextInfo contextInfo) {
141         SearchRequestInfo searchRequest = new SearchRequestInfo();
142         searchRequest.setSearchKey("atp.search.advancedAtpSearch");
143         ArrayList<SearchParamInfo> searchParams = new ArrayList<SearchParamInfo>();
144         SearchParamInfo searchParam = new SearchParamInfo();
145         searchParam.setKey("atp.advancedAtpSearchParam.atpId");
146         searchParam.getValues().add(value);
147         searchParams.add(searchParam);
148         searchRequest.setParams(searchParams);
149         SearchResultInfo searchResult = null;
150         try {
151             searchResult = searchDispatcher.search(searchRequest, contextInfo);
152         } catch (Exception e) {
153             throw new RuntimeException(e);
154         }
155         if(searchResult!= null){
156             for (SearchResultRowInfo resultRow : searchResult.getRows()) {
157                 for (SearchResultCellInfo searchResultCell : resultRow.getCells()) {
158                     if(searchResultCell.getKey().equals("atp.resultColumn.atpDescrPlain")){
159                         return searchResultCell.getValue();
160                     }
161                 }
162             }
163         }
164         return null;
165     }
166 }