1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.ui.client.widgets.list; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.Collections; |
20 | |
import java.util.List; |
21 | |
|
22 | |
import org.kuali.student.common.assembly.data.LookupMetadata; |
23 | |
import org.kuali.student.common.search.dto.ResultColumnInfo; |
24 | |
import org.kuali.student.common.search.dto.SearchResultRow; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class SearchResultListItems implements ListItems{ |
34 | |
|
35 | |
private ArrayList<String> attrKeys; |
36 | 0 | private List<SearchResultRow> resultDataMap = new ArrayList<SearchResultRow>(); |
37 | 0 | private int attrOffset = 0; |
38 | |
|
39 | |
|
40 | |
|
41 | 0 | private int sortAttrNdx = 1; |
42 | 0 | private int itemTextAttrNdx = 1; |
43 | 0 | private int keyAttrNdx = 0; |
44 | |
|
45 | |
public int getSortAttrNdx() { |
46 | 0 | return sortAttrNdx; |
47 | |
} |
48 | |
|
49 | |
public void setSortAttrNdx(int sortAttrNdx) { |
50 | 0 | this.sortAttrNdx = sortAttrNdx; |
51 | 0 | } |
52 | |
|
53 | |
public void setSortAttrNdxFromAttrKey(List<SearchResultRow> results, String sortAttrKey) { |
54 | 0 | this.sortAttrNdx = getAttrKeyNdx(results, sortAttrKey); |
55 | 0 | } |
56 | |
|
57 | |
public int getKeyAttrNdx() { |
58 | 0 | return keyAttrNdx; |
59 | |
} |
60 | |
|
61 | |
public void setKeyAttrNdx(int keyAttrNdx) { |
62 | 0 | this.keyAttrNdx = keyAttrNdx; |
63 | 0 | } |
64 | |
|
65 | |
public void setKeyAttrNdxFromAttrKey(List<SearchResultRow> results, String keyAttrKey) { |
66 | 0 | this.keyAttrNdx = getAttrKeyNdx(results, keyAttrKey); |
67 | 0 | } |
68 | |
|
69 | |
public int getItemTextAttrNdx() { |
70 | 0 | return itemTextAttrNdx; |
71 | |
} |
72 | |
|
73 | |
public void setItemTextAttrNdx(int itemTextAttrNdx) { |
74 | 0 | this.itemTextAttrNdx = itemTextAttrNdx; |
75 | 0 | } |
76 | |
|
77 | |
public void setItemTextAttrNdxFromAttrKey(List<SearchResultRow> results, String itemTextAttrKey) { |
78 | 0 | this.itemTextAttrNdx = getAttrKeyNdx(results, itemTextAttrKey); |
79 | 0 | } |
80 | |
|
81 | 0 | public SearchResultListItems(){ |
82 | 0 | } |
83 | |
|
84 | |
private void setAttrNdxs(List<SearchResultRow> results, LookupMetadata lookupMetadata) { |
85 | |
|
86 | 0 | setItemTextAttrNdxFromAttrKey(results, lookupMetadata.getResultDisplayKey()); |
87 | 0 | setKeyAttrNdxFromAttrKey(results, lookupMetadata.getResultReturnKey()); |
88 | 0 | setSortAttrNdxFromAttrKey(results, lookupMetadata.getResultSortKey()); |
89 | 0 | } |
90 | |
|
91 | 0 | public SearchResultListItems(List<ResultColumnInfo> resultColumns, List<SearchResultRow> results, LookupMetadata lookupMetadata){ |
92 | |
|
93 | 0 | setAttrNdxs(results, lookupMetadata); |
94 | 0 | setResultColumns(resultColumns); |
95 | 0 | setResults(results); |
96 | 0 | } |
97 | |
|
98 | 0 | public SearchResultListItems(List<SearchResultRow> results, LookupMetadata lookupMetadata){ |
99 | |
|
100 | 0 | setAttrNdxs(results, lookupMetadata); |
101 | 0 | setResults(results); |
102 | 0 | } |
103 | |
|
104 | 0 | public SearchResultListItems(List<SearchResultRow> results){ |
105 | |
|
106 | 0 | setResults(results); |
107 | 0 | } |
108 | |
|
109 | |
public void setResultColumns(List<ResultColumnInfo> resultColumns){ |
110 | 0 | attrKeys = new ArrayList<String>(); |
111 | |
|
112 | 0 | for (ResultColumnInfo r:resultColumns){ |
113 | 0 | attrKeys.add(r.getName()); |
114 | |
} |
115 | |
|
116 | |
|
117 | 0 | attrKeys.remove(0); |
118 | 0 | attrOffset = 1; |
119 | 0 | } |
120 | |
|
121 | |
public void setResults(List<SearchResultRow> results) { |
122 | 0 | resultDataMap.clear(); |
123 | |
|
124 | 0 | if (results != null){ |
125 | 0 | resultDataMap = new ArrayList<SearchResultRow>(results); |
126 | |
|
127 | |
|
128 | 0 | if (results.size() > 0){ |
129 | 0 | SearchResultRow r = results.get(0); |
130 | 0 | if (attrKeys == null){ |
131 | 0 | attrKeys = new ArrayList<String>(); |
132 | 0 | for (int i=0; i < r.getCells().size(); i ++){ |
133 | 0 | attrKeys.add("attr" + String.valueOf(i)); |
134 | |
} |
135 | |
} |
136 | |
} |
137 | |
} |
138 | 0 | } |
139 | |
|
140 | |
private int getAttrKeyNdx(List<SearchResultRow> results, String keyAttrKey) { |
141 | |
|
142 | 0 | if (results.size() > 0){ |
143 | 0 | for (int i=0; i < results.get(0).getCells().size(); i++){ |
144 | 0 | if (results.get(0).getCells().get(i).getKey().equals(keyAttrKey)) { |
145 | 0 | return i; |
146 | |
} |
147 | |
} |
148 | |
} |
149 | |
|
150 | 0 | return 0; |
151 | |
} |
152 | |
|
153 | |
@Override |
154 | |
public List<String> getAttrKeys() { |
155 | 0 | return attrKeys; |
156 | |
} |
157 | |
|
158 | |
@Override |
159 | |
public String getItemAttribute(String id, String attrKey) { |
160 | 0 | SearchResultRow r = getListItem(id); |
161 | |
|
162 | 0 | int attrIndex = attrKeys.indexOf(attrKey); |
163 | 0 | if (attrIndex >= 0 && r != null){ |
164 | 0 | return r.getCells().get(attrIndex + attrOffset).getValue(); |
165 | |
} |
166 | |
|
167 | 0 | return null; |
168 | |
} |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
@Override |
174 | |
public int getItemCount() { |
175 | 0 | return resultDataMap.size(); |
176 | |
} |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
@Override |
182 | |
public List<String> getItemIds() { |
183 | 0 | List<String> ids = new ArrayList<String>(); |
184 | |
|
185 | 0 | for (SearchResultRow s:resultDataMap){ |
186 | 0 | ids.add(s.getCells().get(keyAttrNdx).getValue()); |
187 | |
} |
188 | |
|
189 | 0 | return ids; |
190 | |
} |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
@Override |
196 | |
public String getItemText(String id) { |
197 | 0 | if(getListItem(id)!=null){ |
198 | 0 | return getListItem(id).getCells().get(itemTextAttrNdx).getValue(); |
199 | |
} |
200 | 0 | return ""; |
201 | |
} |
202 | |
|
203 | |
private SearchResultRow getListItem(String id) { |
204 | 0 | for (SearchResultRow s : resultDataMap) { |
205 | 0 | if (s.getCells().get(keyAttrNdx).getValue().equals(id)) { |
206 | 0 | return s; |
207 | |
} |
208 | |
} |
209 | 0 | return null; |
210 | |
} |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
public List<SearchResultRow> getReadOnlyResults() { |
219 | 0 | return Collections.unmodifiableList(resultDataMap); |
220 | |
} |
221 | |
} |