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