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.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.common.assembly.data.AssemblyException;
23  import org.kuali.student.common.assembly.data.LookupMetadata;
24  import org.kuali.student.common.assembly.data.LookupParamMetadata;
25  import org.kuali.student.common.assembly.data.Metadata.WriteAccess;
26  import org.kuali.student.common.search.dto.SearchParam;
27  import org.kuali.student.common.search.dto.SearchRequest;
28  import org.kuali.student.common.search.dto.SearchResult;
29  import org.kuali.student.common.search.dto.SearchResultCell;
30  import org.kuali.student.common.search.dto.SearchResultRow;
31  import org.kuali.student.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  public class IdTranslator {
40      
41      private static final String ENUMERATION = "enumeration";
42  	private List<SearchParam> additionalParams = new ArrayList<SearchParam>();
43      private SearchDispatcher searchDispatcher;
44      
45      final Logger LOG = Logger.getLogger(IdTranslator.class);
46  
47      public IdTranslator(SearchDispatcher searchDispatcher) throws AssemblyException {
48      	this.searchDispatcher = searchDispatcher;
49      }
50      
51  	private SearchRequest buildSearchRequestById(LookupMetadata lookupMetadata, String searchId) {
52      	SearchRequest sr = new SearchRequest();
53      	sr.setNeededTotalResults(false);
54      	
55      	sr.setSearchKey(lookupMetadata.getSearchTypeId());
56      	sr.setSortColumn(lookupMetadata.getResultSortKey());
57  
58  
59      	List<SearchParam> searchParams = new ArrayList<SearchParam>();
60      	if (lookupMetadata.getSearchParamIdKey() != null) {
61      		//FIXME: workaround until orch dict can handle multi part keys on initiallookup defs
62      		if (lookupMetadata.getSearchParamIdKey().contains(ENUMERATION)) {
63      			for (LookupParamMetadata p : lookupMetadata.getParams()) {
64      				if (p.getWriteAccess() != null && p.getWriteAccess().equals(WriteAccess.NEVER) && p.getDefaultValueString() != null) {
65      					SearchParam param = createParam(p.getKey(), p.getDefaultValueString());
66      					searchParams.add(param);   							    
67      				}
68      				else if (p.getWriteAccess() == null || !p.getWriteAccess().equals(WriteAccess.NEVER)){
69      					SearchParam param = createParam(p.getKey(), searchId);
70      					searchParams.add(param);   								
71      				}
72      			}
73      		}
74      		else {
75      			SearchParam param = createParam(lookupMetadata.getSearchParamIdKey(), searchId);
76      			searchParams.add(param);	
77      		}
78  
79      		sr.setParams(searchParams); 		
80      	}	
81      	else {
82      		LOG.warn("Unable to build search request for " + lookupMetadata.getSearchTypeId() + " translation for id " + searchId);
83      		
84      	}
85  
86      	
87      	sr.getParams().addAll(additionalParams);
88  
89          return sr;
90      }
91      
92      private SearchParam createParam(String key, String value) {
93      	SearchParam param = new SearchParam();
94      	
95      	if(key == null) {
96  			param.setKey("");
97  		} else {
98  			param.setKey(key);
99  		}
100 
101     	if(value == null) {
102 			param.setValue("");
103 		} else {
104 			param.setValue(value);
105 		}
106     	
107     	return param;
108     }
109     
110     public IdTranslation getTranslation(LookupMetadata lookupMetadata, String id) throws AssemblyException {
111         SearchRequest searchRequest = buildSearchRequestById(lookupMetadata, id);
112         SearchResult searchResults = null;
113 		
114 		searchResults = searchDispatcher.dispatchSearch(searchRequest);
115 			
116 		
117         IdTranslation result = null; 
118         if(searchResults != null && searchResults.getRows().size() > 0){
119         	result = new IdTranslation();
120         	result.setId(id);
121         	
122         	SearchResultRow r = searchResults.getRows().get(0);
123             for(SearchResultCell c: r.getCells()){
124                 if(c.getKey().equals(lookupMetadata.getResultDisplayKey())){
125                     result.addAttribute(c.getKey(), c.getValue());
126                     result.setDisplay(c.getValue());
127                 } else {
128                     result.addAttribute(c.getKey(), c.getValue());
129                 }
130             }
131         }
132         return result;
133         
134     }
135 
136     public String getTranslationForAtp(String value) {
137         SearchRequest searchRequest = new SearchRequest();
138         searchRequest.setSearchKey("atp.search.advancedAtpSearch");
139         ArrayList<SearchParam> searchParams = new ArrayList<SearchParam>();
140         SearchParam searchParam = new SearchParam();
141         searchParam.setKey("atp.advancedAtpSearchParam.atpId");
142         searchParam.setValue(value);
143         searchParams.add(searchParam);
144         searchRequest.setParams(searchParams);
145         SearchResult searchResult = searchDispatcher.dispatchSearch(searchRequest);
146         if(searchResult!= null){
147             for (SearchResultRow resultRow : searchResult.getRows()) {
148                 for (SearchResultCell searchResultCell : resultRow.getCells()) {
149                     if(searchResultCell.getKey().equals("atp.resultColumn.atpDescrPlain")){
150                         return searchResultCell.getValue();
151                     }
152                 }
153             }
154         }
155         return null;
156     }
157 }