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 public class SearchUtils {
31
32
33
34
35
36
37 public static class SearchRequestWrapper{
38 SearchRequest searchRequest;
39 HashSet<String> crossConstraints = new HashSet<String>();
40 boolean deferSearch = false;
41
42 public SearchRequest getSearchRequest() {
43 return searchRequest;
44 }
45
46 public void setSearchRequest(SearchRequest searchRequest) {
47 this.searchRequest = searchRequest;
48 }
49
50 public HashSet<String> getCrossConstraints() {
51 return crossConstraints;
52 }
53
54 public void setCrossConstraints(HashSet<String> constraints) {
55 this.crossConstraints = constraints;
56 }
57
58 public boolean isDeferSearch() {
59 return deferSearch;
60 }
61
62 public void setDeferSearch(boolean deferSearch) {
63 this.deferSearch = deferSearch;
64 }
65 }
66
67
68
69
70
71
72
73
74
75 public static SearchRequest initializeSearchRequest(LookupMetadata lookup) {
76
77
78 SearchRequestWrapper searchRequestWrapper = new SearchRequestWrapper();
79 initializeSearchRequest(lookup, searchRequestWrapper);
80 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 HashSet<String> crossConstraints = searchRequestWrapper.getCrossConstraints();
97
98 SearchRequest sr = new SearchRequest();
99 List<SearchParam> params = new ArrayList<SearchParam>();
100
101 sr.setSearchKey(lookup.getSearchTypeId());
102
103 if (lookup.getResultSortKey() != null){
104 sr.setSortColumn(lookup.getResultSortKey());
105 }
106 if(SortDirection.DESC.equals(lookup.getSortDirection())){
107 sr.setSortDirection(SortDirection.DESC);
108 }
109
110
111 for(final LookupParamMetadata metaParam: lookup.getParams()){
112 if(metaParam.getWriteAccess() == WriteAccess.NEVER){
113 if ((metaParam.getDefaultValueString() == null || metaParam.getDefaultValueString().isEmpty())&&
114 (metaParam.getDefaultValueList() == null || metaParam.getDefaultValueList().isEmpty())&&
115 (metaParam.getFieldPath() == null || metaParam.getFieldPath().isEmpty())) {
116
117 GWT.log("Key = " + metaParam.getKey() + " has write access NEVER but has no default value!", null);
118 continue;
119 }
120 final SearchParam param = new SearchParam();
121 param.setKey(metaParam.getKey());
122 if(metaParam.getFieldPath()!=null){
123 FieldDescriptor fd = null;
124 String finalPath = resolvePath(metaParam.getFieldPath());
125 crossConstraints.add(finalPath);
126 fd = Application.getApplicationContext().getPathToFieldMapping(null, finalPath);
127 if(fd!=null){
128 Value value = null;
129 if(fd.getFieldElement().getFieldWidget() instanceof KSTextBox){
130 value = new StringValue(((KSTextBox)fd.getFieldElement().getFieldWidget()).getValue());
131 }
132 if(fd.getFieldElement().getFieldWidget() instanceof HasDataValue){
133 value = ((HasDataValue)fd.getFieldElement().getFieldWidget()).getValue();
134 }
135 if(value!=null&&value.get()!=null){
136 if(value.get() instanceof Data){
137 ArrayList<String> listValue = new ArrayList<String>();
138 for(Iterator<Property> iter =((Data)value.get()).realPropertyIterator();iter.hasNext();){
139 listValue.add(iter.next().getValue().toString());
140 }
141 if(listValue.isEmpty()){
142 listValue.add("");
143 }
144 param.setValue(listValue);
145 }else{
146 param.setValue(value.get().toString());
147 }
148 }else{
149 param.setValue((String)null);
150 }
151 }
152 searchRequestWrapper.setDeferSearch(true);
153 }else if(metaParam.getDefaultValueList()==null){
154 param.setValue(metaParam.getDefaultValueString());
155 }else{
156 param.setValue(metaParam.getDefaultValueList());
157 }
158 params.add(param);
159 }
160 else if(metaParam.getWriteAccess() == WriteAccess.WHEN_NULL){
161 if((metaParam.getDefaultValueString() != null && !metaParam.getDefaultValueString().isEmpty())||
162 (metaParam.getDefaultValueList() != null && !metaParam.getDefaultValueList().isEmpty())||
163 (metaParam.getFieldPath() != null && !metaParam.getFieldPath().isEmpty())){
164 final SearchParam param = new SearchParam();
165 param.setKey(metaParam.getKey());
166 if(metaParam.getFieldPath()!=null){
167 FieldDescriptor fd = null;
168 String finalPath;
169 if(metaParam.getFieldPath().startsWith("/")){
170 finalPath=metaParam.getFieldPath().substring(1);
171 }else{
172 finalPath=Application.getApplicationContext().getParentPath()+metaParam.getFieldPath();
173 }
174 crossConstraints.add(finalPath);
175 fd = Application.getApplicationContext().getPathToFieldMapping(null, finalPath);
176 if(fd!=null){
177 if(fd.getFieldElement().getFieldWidget() instanceof HasDataValue){
178 Value value = ((HasDataValue)fd.getFieldElement().getFieldWidget()).getValue();
179 param.setValue(value==null?null:value.get()==null?null:value.get().toString());
180 }
181 }
182 searchRequestWrapper.setDeferSearch(true);
183 }else if(metaParam.getDefaultValueList()==null){
184 param.setValue(metaParam.getDefaultValueString());
185 }else{
186 param.setValue(metaParam.getDefaultValueList());
187 }
188 params.add(param);
189 }
190 }
191 }
192 sr.setParams(params);
193 searchRequestWrapper.setSearchRequest(sr);
194 }
195
196 public static String resolvePath(String path) {
197 String finalPath;
198 if(path.startsWith("/")){
199 finalPath=path.substring(1);
200 }else{
201 finalPath=Application.getApplicationContext().getParentPath()+path;
202 }
203 return finalPath;
204 }
205
206 }