|
| 1 | +/* |
| 2 | + * Licensed to The Apereo Foundation under one or more contributor license |
| 3 | + * agreements. See the NOTICE file distributed with this work for |
| 4 | + * additional information regarding copyright ownership. |
| 5 | + * |
| 6 | + * The Apereo Foundation licenses this file to you under the Apache License, |
| 7 | + * Version 2.0 (the "License"); you may not use this file except in |
| 8 | + * compliance with the License. You may obtain a copy of the License at: |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * |
| 19 | +*/ |
| 20 | +package org.unitime.timetable.gwt.client.exams; |
| 21 | + |
| 22 | +import org.unitime.localization.messages.ExaminationMessages; |
| 23 | +import org.unitime.timetable.gwt.client.ToolBox; |
| 24 | +import org.unitime.timetable.gwt.client.exams.ExamsInterface.ExamConflictBasedStatisticsRequest; |
| 25 | +import org.unitime.timetable.gwt.client.exams.ExamsInterface.ExamConflictStatisticsFilterRequest; |
| 26 | +import org.unitime.timetable.gwt.client.page.UniTimeNotifications; |
| 27 | +import org.unitime.timetable.gwt.client.solver.PageFilter; |
| 28 | +import org.unitime.timetable.gwt.client.solver.PreferenceLegend; |
| 29 | +import org.unitime.timetable.gwt.client.solver.suggestions.ConflictBasedStatisticsTree; |
| 30 | +import org.unitime.timetable.gwt.client.widgets.LoadingWidget; |
| 31 | +import org.unitime.timetable.gwt.client.widgets.P; |
| 32 | +import org.unitime.timetable.gwt.client.widgets.SimpleForm; |
| 33 | +import org.unitime.timetable.gwt.client.widgets.UniTimeFrameDialog; |
| 34 | +import org.unitime.timetable.gwt.command.client.GwtRpcResponseList; |
| 35 | +import org.unitime.timetable.gwt.command.client.GwtRpcService; |
| 36 | +import org.unitime.timetable.gwt.command.client.GwtRpcServiceAsync; |
| 37 | +import org.unitime.timetable.gwt.resources.GwtMessages; |
| 38 | +import org.unitime.timetable.gwt.shared.FilterInterface; |
| 39 | +import org.unitime.timetable.gwt.shared.CourseTimetablingSolverInterface.ConflictStatisticsFilterResponse; |
| 40 | +import org.unitime.timetable.gwt.shared.SolverInterface.PageMessage; |
| 41 | +import org.unitime.timetable.gwt.shared.SolverInterface.PageMessageType; |
| 42 | +import org.unitime.timetable.gwt.shared.SuggestionsInterface.CBSNode; |
| 43 | +import org.unitime.timetable.gwt.shared.SuggestionsInterface.SelectedAssignment; |
| 44 | + |
| 45 | +import com.google.gwt.core.client.GWT; |
| 46 | +import com.google.gwt.event.dom.client.ClickEvent; |
| 47 | +import com.google.gwt.event.dom.client.ClickHandler; |
| 48 | +import com.google.gwt.event.logical.shared.ValueChangeEvent; |
| 49 | +import com.google.gwt.event.logical.shared.ValueChangeHandler; |
| 50 | +import com.google.gwt.user.client.History; |
| 51 | +import com.google.gwt.user.client.rpc.AsyncCallback; |
| 52 | +import com.google.gwt.user.client.ui.Composite; |
| 53 | +import com.google.gwt.user.client.ui.RootPanel; |
| 54 | +import com.google.gwt.user.client.ui.SimplePanel; |
| 55 | + |
| 56 | +/** |
| 57 | + * @author Tomas Muller |
| 58 | + */ |
| 59 | +public class ExamConflictBasedStatisticsPage extends Composite { |
| 60 | + private static final GwtMessages MESSAGES = GWT.create(GwtMessages.class); |
| 61 | + private static final ExaminationMessages XMSG = GWT.create(ExaminationMessages.class); |
| 62 | + protected static GwtRpcServiceAsync RPC = GWT.create(GwtRpcService.class); |
| 63 | + private PageFilter iFilter; |
| 64 | + private SimplePanel iRootPanel; |
| 65 | + private SimpleForm iPanel; |
| 66 | + private FilterInterface iLastFilter; |
| 67 | + private GwtRpcResponseList<CBSNode> iLastResponse; |
| 68 | + private ConflictStatisticsFilterResponse iFilterResponse; |
| 69 | + private ConflictBasedStatisticsTree iTree; |
| 70 | + private PreferenceLegend iLegend; |
| 71 | + |
| 72 | + public ExamConflictBasedStatisticsPage() { |
| 73 | + iFilter = new PageFilter(); |
| 74 | + iFilter.getHeader().setCollapsible("1".equals(ToolBox.getSessionCookie("Exams.Filter"))); |
| 75 | + iFilter.getHeader().addCollapsibleHandler(new ValueChangeHandler<Boolean>() { |
| 76 | + @Override |
| 77 | + public void onValueChange(ValueChangeEvent<Boolean> event) { |
| 78 | + ToolBox.setSessionCookie("Exams.Filter", event.getValue() ? "1" : "0"); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + iPanel = new SimpleForm(2); |
| 83 | + iPanel.removeStyleName("unitime-NotPrintableBottomLine"); |
| 84 | + iPanel.addRow(iFilter); |
| 85 | + |
| 86 | + iFilter.getFooter().addButton("search", MESSAGES.buttonSearch(), new ClickHandler() { |
| 87 | + @Override |
| 88 | + public void onClick(ClickEvent event) { |
| 89 | + String token = iFilter.getQuery(); |
| 90 | + if (!History.getToken().equals(token)) |
| 91 | + History.newItem(token, false); |
| 92 | + search(null); |
| 93 | + } |
| 94 | + }); |
| 95 | + iFilter.getFooter().setEnabled("search", false); |
| 96 | + iRootPanel = new SimplePanel(iPanel); |
| 97 | + iRootPanel.addStyleName("unitime-ConflictBasedStatisticsPage"); |
| 98 | + initWidget(iRootPanel); |
| 99 | + init(); |
| 100 | + |
| 101 | + History.addValueChangeHandler(new ValueChangeHandler<String>() { |
| 102 | + @Override |
| 103 | + public void onValueChange(ValueChangeEvent<String> event) { |
| 104 | + iFilter.setQuery(event.getValue(), true); |
| 105 | + if (iPanel.getRowCount() > 1) |
| 106 | + search(null); |
| 107 | + } |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + protected void init() { |
| 112 | + RPC.execute(new ExamConflictStatisticsFilterRequest(), new AsyncCallback<ConflictStatisticsFilterResponse>() { |
| 113 | + @Override |
| 114 | + public void onFailure(Throwable caught) { |
| 115 | + iFilter.getFooter().setErrorMessage(MESSAGES.failedToInitialize(caught.getMessage())); |
| 116 | + UniTimeNotifications.error(MESSAGES.failedToInitialize(caught.getMessage()), caught); |
| 117 | + ToolBox.checkAccess(caught); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void onSuccess(ConflictStatisticsFilterResponse result) { |
| 122 | + iLegend = new PreferenceLegend(result.getSuggestionProperties().getPreferences()); |
| 123 | + iFilterResponse = result; |
| 124 | + iFilter.getFooter().clearMessage(); |
| 125 | + iFilter.setValue(result); |
| 126 | + iFilter.getFooter().setEnabled("search", true); |
| 127 | + createTriggers(); |
| 128 | + if (iFilter.getHeader().isCollapsible() != null && !iFilter.getHeader().isCollapsible()) |
| 129 | + search(null); |
| 130 | + } |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + public static native void createTriggers()/*-{ |
| 135 | + $wnd.refreshPage = function() { |
| 136 | + }; |
| 137 | + }-*/; |
| 138 | + |
| 139 | + protected void search(final AsyncCallback<Boolean> callback) { |
| 140 | + final ExamConflictBasedStatisticsRequest request = new ExamConflictBasedStatisticsRequest(); |
| 141 | + final FilterInterface filter = iFilter.getValue(); |
| 142 | + try { |
| 143 | + request.setLimit(Double.valueOf(filter.getParameterValue("limit", "25.0"))); |
| 144 | + } catch (NumberFormatException e) {} |
| 145 | + request.setVariableOriented("0".equals(filter.getParameterValue("mode", "0"))); |
| 146 | + iFilter.getFooter().clearMessage(); |
| 147 | + for (int row = iPanel.getRowCount() - 1; row > 0; row--) |
| 148 | + iPanel.removeRow(row); |
| 149 | + iFilter.getFooter().showLoading(); |
| 150 | + iFilter.getFooter().setEnabled("search", false); |
| 151 | + LoadingWidget.showLoading(MESSAGES.waitLoadingData()); |
| 152 | + RPC.execute(request, new AsyncCallback<GwtRpcResponseList<CBSNode>>() { |
| 153 | + @Override |
| 154 | + public void onFailure(Throwable caught) { |
| 155 | + LoadingWidget.hideLoading(); |
| 156 | + iFilter.getFooter().setErrorMessage(MESSAGES.failedToLoadConflictStatistics(caught.getMessage())); |
| 157 | + UniTimeNotifications.error(MESSAGES.failedToLoadConflictStatistics(caught.getMessage()), caught); |
| 158 | + iFilter.getFooter().setEnabled("search", true); |
| 159 | + if (callback != null) |
| 160 | + callback.onFailure(caught); |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public void onSuccess(GwtRpcResponseList<CBSNode> result) { |
| 165 | + LoadingWidget.hideLoading(); |
| 166 | + iFilter.getFooter().clearMessage(); |
| 167 | + populate(filter, result); |
| 168 | + iFilter.getFooter().setEnabled("search", true); |
| 169 | + if (callback != null) |
| 170 | + callback.onSuccess(result != null && !result.isEmpty()); |
| 171 | + } |
| 172 | + }); |
| 173 | + } |
| 174 | + |
| 175 | + protected void populate(FilterInterface filter, GwtRpcResponseList<CBSNode> response) { |
| 176 | + iLastFilter = filter; |
| 177 | + iLastResponse = response; |
| 178 | + for (int row = iPanel.getRowCount() - 1; row > 0; row--) |
| 179 | + iPanel.removeRow(row); |
| 180 | + |
| 181 | + RootPanel cpm = RootPanel.get("UniTimeGWT:CustomPageMessages"); |
| 182 | + if (cpm != null && iFilterResponse != null) { |
| 183 | + cpm.clear(); |
| 184 | + if (iFilterResponse.hasPageMessages()) { |
| 185 | + for (final PageMessage pm: iFilterResponse.getPageMessages()) { |
| 186 | + P p = new P(pm.getType() == PageMessageType.ERROR ? "unitime-PageError" : pm.getType() == PageMessageType.WARNING ? "unitime-PageWarn" : "unitime-PageMessage"); |
| 187 | + p.setHTML(pm.getMessage()); |
| 188 | + if (pm.hasUrl()) { |
| 189 | + p.addStyleName("unitime-ClickablePageMessage"); |
| 190 | + p.addClickHandler(new ClickHandler() { |
| 191 | + @Override |
| 192 | + public void onClick(ClickEvent event) { |
| 193 | + if (pm.hasUrl()) ToolBox.open(GWT.getHostPageBaseURL() + pm.getUrl()); |
| 194 | + } |
| 195 | + }); |
| 196 | + } |
| 197 | + cpm.add(p); |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + if (response == null || response.isEmpty()) { |
| 203 | + iFilter.getFooter().setMessage(MESSAGES.errorConflictStatisticsNoDataReturned()); |
| 204 | + return; |
| 205 | + } |
| 206 | + |
| 207 | + if (iTree == null) { |
| 208 | + iTree = new ConflictBasedStatisticsTree(iFilterResponse.getSuggestionProperties()) { |
| 209 | + protected void onClick(ClickEvent event, CBSNode node) { |
| 210 | + if (node.hasLink()) { |
| 211 | + ToolBox.open(GWT.getHostPageBaseURL() + node.getLink()); |
| 212 | + } else if (node.hasClassId()) { |
| 213 | + UniTimeFrameDialog.openDialog(XMSG.dialogExamAssign(), "examInfo.action?menu=hide&examId=" + node.getClassId(), "900", "85%"); |
| 214 | + } else if (node.hasSelection()) { |
| 215 | + SelectedAssignment selection = node.getSelection(); |
| 216 | + UniTimeFrameDialog.openDialog(XMSG.dialogExamAssign(), "examInfo.action?menu=hide&examId=" + selection.getClassId() |
| 217 | + + "&period=" + selection.getPatternId() + "&room=" + selection.getRoomIds(":") + "&op=Try&reset=1", "900", "85%"); |
| 218 | + } |
| 219 | + } |
| 220 | + }; |
| 221 | + } |
| 222 | + iTree.setValue(response); |
| 223 | + iPanel.addRow(iTree); |
| 224 | + iPanel.addRow(iLegend); |
| 225 | + } |
| 226 | +} |
0 commit comments