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.r1.common.search.dto.SearchParam;
27  import org.kuali.student.r1.common.search.dto.SearchRequest;
28  import org.kuali.student.r1.common.search.dto.SearchResult;
29  import org.kuali.student.r1.common.search.dto.SearchResultCell;
30  import org.kuali.student.r1.common.search.dto.SearchResultRow;
31  import org.kuali.student.r1.common.search.service.SearchDispatcher;
32  
33  /**
34   * Builds and issues SearchRequests parameterized with the information from LookupMetadata and the provided ID
35   * 
36   * @author Kuali Student Team
37   *
38   */
39  @Deprecated
40  public class IdTranslator {
41      
42      private static final String ENUMERATION = "enumeration";
43  	private List<SearchParam> additionalParams = new ArrayList<SearchParam>();
44      private SearchDispatcher searchDispatcher;
45      
46      final Logger LOG = Logger.getLogger(IdTranslator.class);
47  
48      public IdTranslator(SearchDispatcher searchDispatcher) throws AssemblyException {
49      	this.searchDispatcher = searchDispatcher;
50      }
51      
52  	private SearchRequest buildSearchRequestById(LookupMetadata lookupMetadata, String searchId) {
53      	SearchRequest sr = new SearchRequest();
54      	sr.setNeededTotalResults(false);
55      	
56      	sr.setSearchKey(lookupMetadata.getSearchTypeId());
57      	sr.setSortColumn(lookupMetadata.getResultSortKey());
58  
59  
60      	List<SearchParam> searchParams = new ArrayList<SearchParam>();
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      					SearchParam param = createParam(p.getKey(), p.getDefaultValueString());
67      					searchParams.add(param);   							    
68      				}
69      				else if (p.getWriteAccess() == null || !p.getWriteAccess().equals(WriteAccess.NEVER)){
70      					SearchParam param = createParam(p.getKey(), searchId);
71      					searchParams.add(param);   								
72      				}
73      			}
74      		}
75      		else {
76      			SearchParam 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 SearchParam createParam(String key, String value) {
94      	SearchParam param = new SearchParam();
95      	
96      	if(key == null) {
97  			param.setKey("");
98  		} else {
99  			param.setKey(key);
100 		}
101 
102     	if(value == null) {
103 			param.setValue("");
104 		} else {
105 			param.setValue(value);
106 		}
107     	
108     	return param;
109     }
110     
111     public IdTranslation getTranslation(LookupMetadata lookupMetadata, String id) throws AssemblyException {
112         SearchRequest searchRequest = buildSearchRequestById(lookupMetadata, id);
113         SearchResult searchResults = null;
114 		
115 		searchResults = searchDispatcher.dispatchSearch(searchRequest);
116 			
117 		
118         IdTranslation result = null; 
119         if(searchResults != null && searchResults.getRows().size() > 0){
120         	result = new IdTranslation();
121         	result.setId(id);
122         	
123         	SearchResultRow r = searchResults.getRows().get(0);
124             for(SearchResultCell c: r.getCells()){
125                 if(c.getKey().equals(lookupMetadata.getResultDisplayKey())){
126                     result.addAttribute(c.getKey(), c.getValue());
127                     result.setDisplay(c.getValue());
128                 } else {
129                     result.addAttribute(c.getKey(), c.getValue());
130                 }
131             }
132         }
133         return result;
134         
135     }
136 
137     public String getTranslationForAtp(String value) {
138         SearchRequest searchRequest = new SearchRequest();
139         searchRequest.setSearchKey("atp.search.advancedAtpSearch");
140         ArrayList<SearchParam> searchParams = new ArrayList<SearchParam>();
141         SearchParam searchParam = new SearchParam();
142         searchParam.setKey("atp.advancedAtpSearchParam.atpId");
143         searchParam.setValue(value);
144         searchParams.add(searchParam);
145         searchRequest.setParams(searchParams);
146         SearchResult searchResult = searchDispatcher.dispatchSearch(searchRequest);
147         if(searchResult!= null){
148             for (SearchResultRow resultRow : searchResult.getRows()) {
149                 for (SearchResultCell searchResultCell : resultRow.getCells()) {
150                     if(searchResultCell.getKey().equals("atp.resultColumn.atpDescrPlain")){
151                         return searchResultCell.getValue();
152                     }
153                 }
154             }
155         }
156         return null;
157     }
158 }