1 | |
package org.kuali.student.common.ui.client.util; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.HashSet; |
5 | |
import java.util.Iterator; |
6 | |
import java.util.List; |
7 | |
|
8 | |
import org.kuali.student.common.assembly.data.Data; |
9 | |
import org.kuali.student.common.assembly.data.Data.Property; |
10 | |
import org.kuali.student.common.assembly.data.Data.Value; |
11 | |
import org.kuali.student.common.assembly.data.Data.StringValue; |
12 | |
import org.kuali.student.common.assembly.data.LookupMetadata; |
13 | |
import org.kuali.student.common.assembly.data.LookupParamMetadata; |
14 | |
import org.kuali.student.common.assembly.data.Metadata.WriteAccess; |
15 | |
import org.kuali.student.common.search.dto.SearchParam; |
16 | |
import org.kuali.student.common.search.dto.SearchRequest; |
17 | |
import org.kuali.student.common.search.dto.SortDirection; |
18 | |
import org.kuali.student.common.ui.client.application.Application; |
19 | |
import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor; |
20 | |
import org.kuali.student.common.ui.client.mvc.HasDataValue; |
21 | |
import org.kuali.student.common.ui.client.widgets.KSTextBox; |
22 | |
|
23 | |
import com.google.gwt.core.client.GWT; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | 0 | public class SearchUtils { |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 0 | public static class SearchRequestWrapper{ |
38 | |
SearchRequest searchRequest; |
39 | 0 | HashSet<String> crossConstraints = new HashSet<String>(); |
40 | 0 | boolean deferSearch = false; |
41 | |
|
42 | |
public SearchRequest getSearchRequest() { |
43 | 0 | return searchRequest; |
44 | |
} |
45 | |
|
46 | |
public void setSearchRequest(SearchRequest searchRequest) { |
47 | 0 | this.searchRequest = searchRequest; |
48 | 0 | } |
49 | |
|
50 | |
public HashSet<String> getCrossConstraints() { |
51 | 0 | return crossConstraints; |
52 | |
} |
53 | |
|
54 | |
public void setCrossConstraints(HashSet<String> constraints) { |
55 | 0 | this.crossConstraints = constraints; |
56 | 0 | } |
57 | |
|
58 | |
public boolean isDeferSearch() { |
59 | 0 | return deferSearch; |
60 | |
} |
61 | |
|
62 | |
public void setDeferSearch(boolean deferSearch) { |
63 | 0 | this.deferSearch = deferSearch; |
64 | 0 | } |
65 | |
} |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public static SearchRequest initializeSearchRequest(LookupMetadata lookup) { |
76 | |
|
77 | |
|
78 | 0 | SearchRequestWrapper searchRequestWrapper = new SearchRequestWrapper(); |
79 | 0 | initializeSearchRequest(lookup, searchRequestWrapper); |
80 | 0 | return searchRequestWrapper.getSearchRequest(); |
81 | |
} |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public static void initializeSearchRequest(LookupMetadata lookup, SearchRequestWrapper searchRequestWrapper) { |
95 | |
|
96 | 0 | HashSet<String> crossConstraints = searchRequestWrapper.getCrossConstraints(); |
97 | |
|
98 | 0 | SearchRequest sr = new SearchRequest(); |
99 | 0 | List<SearchParam> params = new ArrayList<SearchParam>(); |
100 | |
|
101 | 0 | sr.setSearchKey(lookup.getSearchTypeId()); |
102 | |
|
103 | 0 | if (lookup.getResultSortKey() != null){ |
104 | 0 | sr.setSortColumn(lookup.getResultSortKey()); |
105 | |
} |
106 | 0 | if(SortDirection.DESC.equals(lookup.getSortDirection())){ |
107 | 0 | sr.setSortDirection(SortDirection.DESC); |
108 | |
} |
109 | |
|
110 | |
|
111 | 0 | for(final LookupParamMetadata metaParam: lookup.getParams()){ |
112 | 0 | if(metaParam.getWriteAccess() == WriteAccess.NEVER){ |
113 | 0 | if ((metaParam.getDefaultValueString() == null || metaParam.getDefaultValueString().isEmpty())&& |
114 | |
(metaParam.getDefaultValueList() == null || metaParam.getDefaultValueList().isEmpty())&& |
115 | |
(metaParam.getFieldPath() == null || metaParam.getFieldPath().isEmpty())) { |
116 | |
|
117 | 0 | GWT.log("Key = " + metaParam.getKey() + " has write access NEVER but has no default value!", null); |
118 | 0 | continue; |
119 | |
} |
120 | 0 | final SearchParam param = new SearchParam(); |
121 | 0 | param.setKey(metaParam.getKey()); |
122 | 0 | if(metaParam.getFieldPath()!=null){ |
123 | 0 | FieldDescriptor fd = null; |
124 | 0 | String finalPath = resolvePath(metaParam.getFieldPath()); |
125 | 0 | crossConstraints.add(finalPath); |
126 | 0 | fd = Application.getApplicationContext().getPathToFieldMapping(null, finalPath); |
127 | 0 | if(fd!=null){ |
128 | 0 | Value value = null; |
129 | 0 | if(fd.getFieldElement().getFieldWidget() instanceof KSTextBox){ |
130 | 0 | value = new StringValue(((KSTextBox)fd.getFieldElement().getFieldWidget()).getValue()); |
131 | |
} |
132 | 0 | if(fd.getFieldElement().getFieldWidget() instanceof HasDataValue){ |
133 | 0 | value = ((HasDataValue)fd.getFieldElement().getFieldWidget()).getValue(); |
134 | |
} |
135 | 0 | if(value!=null&&value.get()!=null){ |
136 | 0 | if(value.get() instanceof Data){ |
137 | 0 | ArrayList<String> listValue = new ArrayList<String>(); |
138 | 0 | for(Iterator<Property> iter =((Data)value.get()).realPropertyIterator();iter.hasNext();){ |
139 | 0 | listValue.add(iter.next().getValue().toString()); |
140 | |
} |
141 | 0 | if(listValue.isEmpty()){ |
142 | 0 | listValue.add(""); |
143 | |
} |
144 | 0 | param.setValue(listValue); |
145 | 0 | }else{ |
146 | 0 | param.setValue(value.get().toString()); |
147 | |
} |
148 | |
}else{ |
149 | 0 | param.setValue((String)null); |
150 | |
} |
151 | |
} |
152 | 0 | searchRequestWrapper.setDeferSearch(true); |
153 | 0 | }else if(metaParam.getDefaultValueList()==null){ |
154 | 0 | param.setValue(metaParam.getDefaultValueString()); |
155 | |
}else{ |
156 | 0 | param.setValue(metaParam.getDefaultValueList()); |
157 | |
} |
158 | 0 | params.add(param); |
159 | 0 | } |
160 | 0 | else if(metaParam.getWriteAccess() == WriteAccess.WHEN_NULL){ |
161 | 0 | if((metaParam.getDefaultValueString() != null && !metaParam.getDefaultValueString().isEmpty())|| |
162 | |
(metaParam.getDefaultValueList() != null && !metaParam.getDefaultValueList().isEmpty())|| |
163 | |
(metaParam.getFieldPath() != null && !metaParam.getFieldPath().isEmpty())){ |
164 | 0 | final SearchParam param = new SearchParam(); |
165 | 0 | param.setKey(metaParam.getKey()); |
166 | 0 | if(metaParam.getFieldPath()!=null){ |
167 | 0 | FieldDescriptor fd = null; |
168 | |
String finalPath; |
169 | 0 | if(metaParam.getFieldPath().startsWith("/")){ |
170 | 0 | finalPath=metaParam.getFieldPath().substring(1); |
171 | |
}else{ |
172 | 0 | finalPath=Application.getApplicationContext().getParentPath()+metaParam.getFieldPath(); |
173 | |
} |
174 | 0 | crossConstraints.add(finalPath); |
175 | 0 | fd = Application.getApplicationContext().getPathToFieldMapping(null, finalPath); |
176 | 0 | if(fd!=null){ |
177 | 0 | if(fd.getFieldElement().getFieldWidget() instanceof HasDataValue){ |
178 | 0 | Value value = ((HasDataValue)fd.getFieldElement().getFieldWidget()).getValue(); |
179 | 0 | param.setValue(value==null?null:value.get()==null?null:value.get().toString()); |
180 | |
} |
181 | |
} |
182 | 0 | searchRequestWrapper.setDeferSearch(true); |
183 | 0 | }else if(metaParam.getDefaultValueList()==null){ |
184 | 0 | param.setValue(metaParam.getDefaultValueString()); |
185 | |
}else{ |
186 | 0 | param.setValue(metaParam.getDefaultValueList()); |
187 | |
} |
188 | 0 | params.add(param); |
189 | 0 | } |
190 | |
} |
191 | |
} |
192 | 0 | sr.setParams(params); |
193 | 0 | searchRequestWrapper.setSearchRequest(sr); |
194 | 0 | } |
195 | |
|
196 | |
public static String resolvePath(String path) { |
197 | |
String finalPath; |
198 | 0 | if(path.startsWith("/")){ |
199 | 0 | finalPath=path.substring(1); |
200 | |
}else{ |
201 | 0 | finalPath=Application.getApplicationContext().getParentPath()+path; |
202 | |
} |
203 | 0 | return finalPath; |
204 | |
} |
205 | |
|
206 | |
} |