1 | |
package org.kuali.student.lum.lu.ui.browseprogram.client.views; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.Arrays; |
5 | |
import java.util.HashSet; |
6 | |
import java.util.List; |
7 | |
import java.util.Map; |
8 | |
import java.util.Map.Entry; |
9 | |
|
10 | |
import org.kuali.student.common.assembly.data.LookupMetadata; |
11 | |
import org.kuali.student.common.assembly.data.Metadata; |
12 | |
import org.kuali.student.common.ui.client.application.Application; |
13 | |
import org.kuali.student.common.ui.client.application.KSAsyncCallback; |
14 | |
import org.kuali.student.common.ui.client.application.ViewContext; |
15 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
16 | |
import org.kuali.student.common.ui.client.mvc.Controller; |
17 | |
import org.kuali.student.common.ui.client.mvc.DataModelDefinition; |
18 | |
import org.kuali.student.common.ui.client.mvc.ViewComposite; |
19 | |
import org.kuali.student.common.ui.client.service.MetadataRpcService; |
20 | |
import org.kuali.student.common.ui.client.service.MetadataRpcServiceAsync; |
21 | |
import org.kuali.student.common.ui.client.widgets.field.layout.layouts.VerticalFieldLayout; |
22 | |
import org.kuali.student.common.ui.client.widgets.filter.FilterEvent; |
23 | |
import org.kuali.student.common.ui.client.widgets.filter.FilterEventHandler; |
24 | |
import org.kuali.student.common.ui.client.widgets.filter.FilterResetEventHandler; |
25 | |
import org.kuali.student.common.ui.client.widgets.filter.KSFilterOptions; |
26 | |
import org.kuali.student.common.ui.client.widgets.headers.KSDocumentHeader; |
27 | |
import org.kuali.student.common.ui.client.widgets.layout.HorizontalTableCellPanel; |
28 | |
import org.kuali.student.common.ui.client.widgets.progress.BlockingTask; |
29 | |
import org.kuali.student.common.ui.client.widgets.progress.KSBlockingProgressIndicator; |
30 | |
import org.kuali.student.common.ui.client.widgets.searchtable.ResultRow; |
31 | |
import org.kuali.student.common.ui.shared.IdAttributes.IdType; |
32 | |
import org.kuali.student.lum.lu.ui.tools.client.widgets.BrowsePanel; |
33 | |
|
34 | |
import com.google.gwt.core.client.GWT; |
35 | |
import com.google.gwt.user.client.Window; |
36 | |
|
37 | |
|
38 | 0 | public class BrowseProgramView extends ViewComposite { |
39 | |
|
40 | 0 | private final BlockingTask initializingTask = new BlockingTask("Loading"); |
41 | 0 | protected MetadataRpcServiceAsync metadataServiceAsync = GWT.create(MetadataRpcService.class); |
42 | |
|
43 | 0 | private VerticalFieldLayout container = new VerticalFieldLayout(); |
44 | 0 | private HorizontalTableCellPanel layout = new HorizontalTableCellPanel(); |
45 | |
protected KSFilterOptions dependencyFilter; |
46 | |
protected DataModelDefinition searchDefinition; |
47 | 0 | protected boolean initialized = false; |
48 | |
BrowsePanel browsePanel; |
49 | |
|
50 | |
public BrowseProgramView(Controller controller, String name, |
51 | |
Enum<?> viewType) { |
52 | 0 | super(controller, name, viewType); |
53 | 0 | } |
54 | |
|
55 | |
private void init() { |
56 | 0 | initWidget(container); |
57 | |
Metadata metaData; |
58 | |
|
59 | 0 | KSDocumentHeader header = new KSDocumentHeader(); |
60 | 0 | header.setTitle("Browse Majors and Specializations"); |
61 | |
|
62 | 0 | container.setTitleWidget(header); |
63 | |
|
64 | 0 | List<LookupMetadata> lookups = new ArrayList<LookupMetadata>(); |
65 | 0 | metaData = searchDefinition.getMetadata("filter"); |
66 | 0 | lookups.add(metaData.getInitialLookup()); |
67 | |
|
68 | 0 | dependencyFilter = new KSFilterOptions(metaData.getAdditionalLookups()); |
69 | 0 | dependencyFilter.addFilterEventHandler(new FilterEventHandler(){ |
70 | |
@Override |
71 | |
public void onDeselect(FilterEvent e) { |
72 | 0 | handleSelections(e.getSelections()); |
73 | 0 | } |
74 | |
@Override |
75 | |
public void onSelect(FilterEvent e) { |
76 | 0 | handleSelections(e.getSelections()); |
77 | 0 | } |
78 | |
|
79 | |
}); |
80 | |
|
81 | 0 | dependencyFilter.addFilterResetEventHandler(new FilterResetEventHandler(){ |
82 | |
@Override |
83 | |
public void onReset() { |
84 | 0 | handleSelections(null); |
85 | 0 | } |
86 | |
}); |
87 | |
|
88 | 0 | layout.add(dependencyFilter); |
89 | |
|
90 | 0 | metaData = searchDefinition.getMetadata("search"); |
91 | |
|
92 | 0 | browsePanel = new BrowsePanel(metaData.getInitialLookup(),400); |
93 | |
|
94 | 0 | browsePanel.setOnSelectectedCallback(new ViewCourseCallback()); |
95 | |
|
96 | 0 | layout.add(browsePanel); |
97 | |
|
98 | 0 | container.add(layout); |
99 | |
|
100 | 0 | browsePanel.executeSearch(new Callback<Boolean>(){ |
101 | |
@Override |
102 | |
public void exec(Boolean result) { |
103 | 0 | } |
104 | |
}); |
105 | |
|
106 | 0 | } |
107 | |
|
108 | |
private void handleSelections(Map<String, List<String>> selections) { |
109 | 0 | if(selections == null || selections.isEmpty()){ |
110 | 0 | browsePanel.showAllRows(); |
111 | |
}else{ |
112 | 0 | HashSet<String> rowKeys = new HashSet<String>(); |
113 | 0 | for(ResultRow resultRow:browsePanel.getAllResultRows()){ |
114 | 0 | boolean matches = true; |
115 | 0 | for(Entry<String, List<String>> entry:selections.entrySet()){ |
116 | 0 | String entryKey = entry.getKey(); |
117 | 0 | String rowValue = resultRow.getValue(entryKey); |
118 | 0 | List<String> entryValue = entry.getValue(); |
119 | 0 | if(entryValue!=null){ |
120 | 0 | if("lu.resultColumn.resultComponentId".equals(entryKey)|| |
121 | |
"lu.resultColumn.luOptionalCampusLocation".equals(entryKey)){ |
122 | 0 | String[] rowValues = rowValue.split("<br/>"); |
123 | 0 | ArrayList<String> intersection = new ArrayList<String>(entryValue); |
124 | 0 | intersection.retainAll(Arrays.asList(rowValues)); |
125 | 0 | if(intersection.isEmpty()){ |
126 | 0 | matches = false; |
127 | 0 | break; |
128 | |
} |
129 | 0 | }else if(!entryValue.contains(rowValue)){ |
130 | 0 | matches = false; |
131 | 0 | break; |
132 | |
} |
133 | |
} |
134 | 0 | } |
135 | 0 | if(matches){ |
136 | 0 | rowKeys.add(resultRow.getId()); |
137 | |
} |
138 | 0 | } |
139 | |
|
140 | 0 | browsePanel.showOnlyRows(rowKeys); |
141 | |
} |
142 | |
|
143 | |
|
144 | |
|
145 | 0 | } |
146 | |
|
147 | |
@Override |
148 | |
public void beforeShow(final Callback<Boolean> onReadyCallback) { |
149 | 0 | if (!initialized) { |
150 | 0 | KSBlockingProgressIndicator.addTask(initializingTask); |
151 | |
|
152 | |
|
153 | 0 | metadataServiceAsync.getMetadata("browseProgram", null, null, new KSAsyncCallback<Metadata>(){ |
154 | |
|
155 | |
@Override |
156 | |
public void handleFailure(Throwable caught) { |
157 | 0 | KSBlockingProgressIndicator.removeTask(initializingTask); |
158 | 0 | throw new RuntimeException("Failed to load search definitions.", caught); |
159 | |
} |
160 | |
|
161 | |
@Override |
162 | |
public void onSuccess(Metadata result) { |
163 | 0 | searchDefinition = new DataModelDefinition(result); |
164 | 0 | init(); |
165 | 0 | onReadyCallback.exec(true); |
166 | 0 | initialized = true; |
167 | 0 | KSBlockingProgressIndicator.removeTask(initializingTask); |
168 | 0 | } |
169 | |
|
170 | |
}); |
171 | |
} else { |
172 | 0 | onReadyCallback.exec(true); |
173 | |
} |
174 | 0 | } |
175 | |
|
176 | 0 | private class ViewCourseCallback implements BrowsePanel.OnSelectedCallback { |
177 | |
|
178 | |
@Override |
179 | |
public void selected (List<String> selectedIds) { |
180 | 0 | if (selectedIds.size () == 0) { |
181 | 0 | Window.alert ("Please select a row before clicking"); |
182 | 0 | return; |
183 | |
} |
184 | 0 | ViewContext viewContext = new ViewContext (); |
185 | 0 | viewContext.setId (selectedIds.get (0)); |
186 | 0 | viewContext.setIdType (IdType.OBJECT_ID); |
187 | 0 | Application.navigate("/HOME/CURRICULUM_HOME/PROGRAM_VIEW", viewContext); |
188 | 0 | } |
189 | |
} |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
} |