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