1
2
3
4
5
6
7
8
9
10
11
12
13
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
35
36
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
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 }