|
| 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.page; |
| 21 | + |
| 22 | +import org.unitime.localization.messages.CourseMessages; |
| 23 | +import org.unitime.timetable.gwt.client.ToolBox; |
| 24 | +import org.unitime.timetable.gwt.client.tables.TableInterface; |
| 25 | +import org.unitime.timetable.gwt.client.tables.TableWidget; |
| 26 | +import org.unitime.timetable.gwt.client.widgets.SimpleForm; |
| 27 | +import org.unitime.timetable.gwt.client.widgets.UniTimeHeaderPanel; |
| 28 | +import org.unitime.timetable.gwt.command.client.GwtRpcRequest; |
| 29 | +import org.unitime.timetable.gwt.command.client.GwtRpcResponse; |
| 30 | +import org.unitime.timetable.gwt.command.client.GwtRpcService; |
| 31 | +import org.unitime.timetable.gwt.command.client.GwtRpcServiceAsync; |
| 32 | +import org.unitime.timetable.gwt.resources.GwtMessages; |
| 33 | + |
| 34 | +import com.google.gwt.core.client.GWT; |
| 35 | +import com.google.gwt.event.logical.shared.ValueChangeEvent; |
| 36 | +import com.google.gwt.event.logical.shared.ValueChangeHandler; |
| 37 | +import com.google.gwt.user.client.History; |
| 38 | +import com.google.gwt.user.client.Window; |
| 39 | +import com.google.gwt.user.client.rpc.AsyncCallback; |
| 40 | +import com.google.gwt.user.client.ui.CheckBox; |
| 41 | +import com.google.gwt.user.client.ui.Composite; |
| 42 | +import com.google.gwt.user.client.ui.Label; |
| 43 | + |
| 44 | +public class SelectUserRolePage extends Composite { |
| 45 | + private static final CourseMessages COURSE = GWT.create(CourseMessages.class); |
| 46 | + private static final GwtMessages MESSAGES = GWT.create(GwtMessages.class); |
| 47 | + protected static GwtRpcServiceAsync RPC = GWT.create(GwtRpcService.class); |
| 48 | + |
| 49 | + SimpleForm iPanel = new SimpleForm(); |
| 50 | + CheckBox iInactive; |
| 51 | + UniTimeHeaderPanel iHeader, iFooter; |
| 52 | + Label iMessage = null; |
| 53 | + |
| 54 | + public SelectUserRolePage() { |
| 55 | + iPanel.addStyleName("unitime-SelectUserRolePage"); |
| 56 | + initWidget(iPanel); |
| 57 | + iInactive = new CheckBox(COURSE.checkIncludeInactiveSessions()); |
| 58 | + iInactive.setValue("1".equals(ToolBox.getCookie("SelectUserRole.Inactive"))); |
| 59 | + iInactive.addValueChangeHandler(new ValueChangeHandler<Boolean>() { |
| 60 | + @Override |
| 61 | + public void onValueChange(ValueChangeEvent<Boolean> event) { |
| 62 | + ToolBox.setCookie("SelectUserRole.Inactive", event.getValue() ? "1" : "0"); |
| 63 | + populate(null); |
| 64 | + } |
| 65 | + }); |
| 66 | + History.addValueChangeHandler(new ValueChangeHandler<String>() { |
| 67 | + @Override |
| 68 | + public void onValueChange(ValueChangeEvent<String> event) { |
| 69 | + if (event.getValue() != null) |
| 70 | + populate(event.getValue()); |
| 71 | + } |
| 72 | + }); |
| 73 | + iInactive.setVisible(false); |
| 74 | + iMessage = new Label(); |
| 75 | + iMessage.addStyleName("unitime-Message"); |
| 76 | + |
| 77 | + iHeader = new UniTimeHeaderPanel(COURSE.sectSelectAcademicSession()); |
| 78 | + iPanel.addHeaderRow(iHeader); |
| 79 | + iFooter = new UniTimeHeaderPanel(); |
| 80 | + iFooter.insertLeft(iInactive, false); |
| 81 | + |
| 82 | + populate(null); |
| 83 | + } |
| 84 | + |
| 85 | + protected void populate(String auth) { |
| 86 | + SelectUserRoleRequest request = new SelectUserRoleRequest(); |
| 87 | + request.setTarget(Window.Location.getParameter("target")); |
| 88 | + request.setList("Y".equalsIgnoreCase(Window.Location.getParameter("list"))); |
| 89 | + request.setIncludeInactive(iInactive.getValue()); |
| 90 | + request.setAuthority(auth); |
| 91 | + iHeader.showLoading(); |
| 92 | + RPC.execute(request, new AsyncCallback<SelectUserRoleReponse>() { |
| 93 | + @Override |
| 94 | + public void onFailure(Throwable caught) { |
| 95 | + iHeader.setErrorMessage(MESSAGES.failedToInitialize(caught.getMessage())); |
| 96 | + UniTimeNotifications.error(MESSAGES.failedToInitialize(caught.getMessage()), caught); |
| 97 | + ToolBox.checkAccess(caught); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void onSuccess(SelectUserRoleReponse response) { |
| 102 | + if (response.hasUrl()) { |
| 103 | + if (response.getUrl().startsWith(GWT.getHostPageBaseURL())) |
| 104 | + ToolBox.open(response.getUrl()); |
| 105 | + else |
| 106 | + ToolBox.open(GWT.getHostPageBaseURL() + response.getUrl()); |
| 107 | + } |
| 108 | + iHeader.clear(); |
| 109 | + iPanel.clear(); |
| 110 | + if (response.hasMessage()) { |
| 111 | + iMessage.setText(response.getMessage()); |
| 112 | + iPanel.addRow(iMessage); |
| 113 | + } |
| 114 | + iPanel.addHeaderRow(iHeader); |
| 115 | + if (response.hasTable()) { |
| 116 | + iHeader.setHTML(response.getTable().getName()); |
| 117 | + iPanel.addRow(new TableWidget(response.getTable())); |
| 118 | + iPanel.addBottomRow(iFooter); |
| 119 | + } |
| 120 | + if (response.hasPageName()) |
| 121 | + UniTimePageLabel.getInstance().setPageName(response.getPageName()); |
| 122 | + iInactive.setVisible(response.hasInactive()); |
| 123 | + } |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + public static class SelectUserRoleRequest implements GwtRpcRequest<SelectUserRoleReponse>{ |
| 129 | + private String iTarget; |
| 130 | + private boolean iList = false; |
| 131 | + private boolean iInactive = false; |
| 132 | + private String iAuthority; |
| 133 | + |
| 134 | + public boolean isList() { return iList; } |
| 135 | + public void setList(boolean list) { iList = list; } |
| 136 | + public String getTarget() { return iTarget; } |
| 137 | + public void setTarget(String target) { iTarget = target; } |
| 138 | + public boolean hasTarget() { return iTarget != null && !iTarget.isEmpty(); } |
| 139 | + public void setIncludeInactive(boolean includeInactive) { iInactive = includeInactive; } |
| 140 | + public boolean isIncludeInactive() { return iInactive; } |
| 141 | + public void setAuthority(String authority) { iAuthority = authority; } |
| 142 | + public String getAuthority() { return iAuthority; } |
| 143 | + public boolean hasAuthority() { return iAuthority != null && !iAuthority.isEmpty(); } |
| 144 | + } |
| 145 | + |
| 146 | + public static class SelectUserRoleReponse implements GwtRpcResponse { |
| 147 | + public String iMessage; |
| 148 | + public String iPageName; |
| 149 | + public TableInterface iTable; |
| 150 | + public String iUrl; |
| 151 | + private boolean iHasInactive = false; |
| 152 | + |
| 153 | + public String getMessage() { return iMessage; } |
| 154 | + public boolean hasMessage() { return iMessage != null && !iMessage.isEmpty(); } |
| 155 | + public void setMessage(String message) { iMessage = message; } |
| 156 | + public TableInterface getTable() { return iTable; } |
| 157 | + public boolean hasTable() { return iTable != null; } |
| 158 | + public void setTable(TableInterface table) { iTable = table; } |
| 159 | + public String getPageName() { return iPageName; } |
| 160 | + public void setPageName(String pageName) { iPageName = pageName; } |
| 161 | + public boolean hasPageName() { return iPageName != null && !iPageName.isEmpty(); } |
| 162 | + public void setUrl(String url) { iUrl = url; } |
| 163 | + public String getUrl() { return iUrl; } |
| 164 | + public boolean hasUrl() { return iUrl != null && !iUrl.isEmpty(); } |
| 165 | + public void setHasInactive(boolean hasInactive) { iHasInactive = hasInactive; } |
| 166 | + public boolean hasInactive() { return iHasInactive; } |
| 167 | + } |
| 168 | +} |
0 commit comments