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.HorizontalBlockFlowPanel; |
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 HorizontalBlockFlowPanel layout = new HorizontalBlockFlowPanel(); |
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 | 0 | layout.add(browsePanel); |
96 | |
|
97 | 0 | container.add(layout); |
98 | |
|
99 | 0 | browsePanel.executeSearch(new Callback<Boolean>(){ |
100 | |
@Override |
101 | |
public void exec(Boolean result) { |
102 | 0 | } |
103 | |
}); |
104 | |
|
105 | 0 | } |
106 | |
|
107 | |
private void handleSelections(Map<String, List<String>> selections) { |
108 | 0 | if(selections == null || selections.isEmpty()){ |
109 | 0 | browsePanel.showAllRows(); |
110 | |
}else{ |
111 | 0 | HashSet<String> rowKeys = new HashSet<String>(); |
112 | 0 | for(ResultRow resultRow:browsePanel.getAllResultRows()){ |
113 | 0 | boolean matches = true; |
114 | 0 | for(Entry<String, List<String>> entry:selections.entrySet()){ |
115 | 0 | String entryKey = entry.getKey(); |
116 | 0 | String rowValue = resultRow.getValue(entryKey); |
117 | 0 | List<String> entryValue = entry.getValue(); |
118 | 0 | if(entryValue!=null){ |
119 | 0 | if("lu.resultColumn.resultComponentId".equals(entryKey)|| |
120 | |
"lu.resultColumn.luOptionalCampusLocation".equals(entryKey)){ |
121 | 0 | String[] rowValues = rowValue.split("<br/>"); |
122 | 0 | ArrayList<String> intersection = new ArrayList<String>(entryValue); |
123 | 0 | intersection.retainAll(Arrays.asList(rowValues)); |
124 | 0 | if(intersection.isEmpty()){ |
125 | 0 | matches = false; |
126 | 0 | break; |
127 | |
} |
128 | 0 | }else if(!entryValue.contains(rowValue)){ |
129 | 0 | matches = false; |
130 | 0 | break; |
131 | |
} |
132 | |
} |
133 | 0 | } |
134 | 0 | if(matches){ |
135 | 0 | rowKeys.add(resultRow.getId()); |
136 | |
} |
137 | 0 | } |
138 | |
|
139 | 0 | browsePanel.showOnlyRows(rowKeys); |
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | 0 | } |
145 | |
|
146 | |
@Override |
147 | |
public void beforeShow(final Callback<Boolean> onReadyCallback) { |
148 | 0 | if (!initialized) { |
149 | 0 | KSBlockingProgressIndicator.addTask(initializingTask); |
150 | |
|
151 | |
|
152 | 0 | metadataServiceAsync.getMetadata("browseProgram", null, null, new KSAsyncCallback<Metadata>(){ |
153 | |
|
154 | |
@Override |
155 | |
public void handleFailure(Throwable caught) { |
156 | 0 | KSBlockingProgressIndicator.removeTask(initializingTask); |
157 | 0 | throw new RuntimeException("Failed to load search definitions.", caught); |
158 | |
} |
159 | |
|
160 | |
@Override |
161 | |
public void onSuccess(Metadata result) { |
162 | 0 | searchDefinition = new DataModelDefinition(result); |
163 | 0 | init(); |
164 | 0 | onReadyCallback.exec(true); |
165 | 0 | initialized = true; |
166 | 0 | KSBlockingProgressIndicator.removeTask(initializingTask); |
167 | 0 | } |
168 | |
|
169 | |
}); |
170 | |
} else { |
171 | 0 | onReadyCallback.exec(true); |
172 | |
} |
173 | 0 | } |
174 | |
|
175 | 0 | private class ViewCourseCallback implements BrowsePanel.OnSelectedCallback { |
176 | |
|
177 | |
@Override |
178 | |
public void selected (List<String> selectedIds) { |
179 | 0 | if (selectedIds.size () == 0) { |
180 | 0 | Window.alert ("Please select a row before clicking"); |
181 | 0 | return; |
182 | |
} |
183 | 0 | ViewContext viewContext = new ViewContext (); |
184 | 0 | viewContext.setId (selectedIds.get (0)); |
185 | 0 | viewContext.setIdType (IdType.OBJECT_ID); |
186 | 0 | Application.navigate("/HOME/CURRICULUM_HOME/PROGRAM_VIEW", viewContext); |
187 | 0 | } |
188 | |
} |
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
} |