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