1 package gr.ebs.gss.admin.client.ui;
3 import gr.ebs.gss.admin.client.TwoAdmin;
4 import gr.ebs.gss.server.domain.dto.StatsDTO;
5 import gr.ebs.gss.server.domain.dto.UserClassDTO;
6 import gr.ebs.gss.server.domain.dto.UserDTO;
8 import java.util.ArrayList;
10 import java.util.List;
12 import com.google.gwt.core.client.GWT;
13 import com.google.gwt.event.dom.client.ClickEvent;
14 import com.google.gwt.event.dom.client.ClickHandler;
15 import com.google.gwt.event.dom.client.KeyPressEvent;
16 import com.google.gwt.event.dom.client.KeyPressHandler;
17 import com.google.gwt.gen2.table.event.client.RowSelectionEvent;
18 import com.google.gwt.gen2.table.event.client.RowSelectionHandler;
19 import com.google.gwt.i18n.client.DateTimeFormat;
20 import com.google.gwt.uibinder.client.UiBinder;
21 import com.google.gwt.uibinder.client.UiField;
22 import com.google.gwt.uibinder.client.UiHandler;
23 import com.google.gwt.user.client.Command;
24 import com.google.gwt.user.client.DeferredCommand;
25 import com.google.gwt.user.client.rpc.AsyncCallback;
26 import com.google.gwt.user.client.ui.Button;
27 import com.google.gwt.user.client.ui.CheckBox;
28 import com.google.gwt.user.client.ui.Composite;
29 import com.google.gwt.user.client.ui.DialogBox;
30 import com.google.gwt.user.client.ui.Grid;
31 import com.google.gwt.user.client.ui.HTML;
32 import com.google.gwt.user.client.ui.ListBox;
33 import com.google.gwt.user.client.ui.TextBox;
34 import com.google.gwt.user.client.ui.VerticalPanel;
35 import com.google.gwt.user.client.ui.Widget;
36 import com.google.gwt.user.datepicker.client.DateBox;
38 public class UserPanel extends Composite {
40 private static UserPanelUiBinder uiBinder = GWT
41 .create(UserPanelUiBinder.class);
43 interface UserPanelUiBinder extends UiBinder<Widget, UserPanel> {
47 @UiField(provided=true) DateBox dateBox = new DateBox();
48 @UiField(provided=true) Button showLastLoginButton = new Button();
49 @UiField TextBox searchBox;
50 @UiField Button searchButton;
51 @UiField Button showInactiveButton;
52 @UiField(provided=true) final UsersTable usersTable = new UsersTable();
53 @UiField(provided=true) Grid g =new Grid(8,6);
54 private Object lastQuery;
57 DateTimeFormat dateFormat = DateTimeFormat.getLongDateFormat();
58 dateBox.setFormat(new DateBox.DefaultFormat(dateFormat));
61 g.setText( 0, 0, "Username:" );
62 g.getCellFormatter().setStyleName(0, 0, "props-toplabels");
63 g.setText( 1, 0, "Name:" );
64 g.getCellFormatter().setStyleName(1, 0, "props-toplabels");
65 g.setText( 2, 0, "Email:" );
66 g.getCellFormatter().setStyleName(2, 0, "props-toplabels");
67 g.setText( 3, 0, "User Class:" );
68 g.getCellFormatter().setStyleName(3, 0, "props-toplabels");
69 g.setText( 4, 0, "Active:" );
70 g.getCellFormatter().setStyleName(4, 0, "props-toplabels");
71 g.setText( 0, 2, "Quota:" );
72 g.getCellFormatter().setStyleName(0, 2, "props-toplabels");
73 g.setText( 1, 2, "Bandwith Quota:" );
74 g.getCellFormatter().setStyleName(1, 2, "props-toplabels");
75 g.setText( 2, 2, "File Count:" );
76 g.getCellFormatter().setStyleName(2, 2, "props-toplabels");
77 g.setText( 3, 2, "Total File Size:" );
78 g.getCellFormatter().setStyleName(3, 2, "props-toplabels");
79 g.setText( 4, 2, "Quota Left:" );
80 g.getCellFormatter().setStyleName(4, 2, "props-toplabels");
81 g.setText( 5, 2, "Bandwith Quota Used:" );
82 g.getCellFormatter().setStyleName(5, 2, "props-toplabels");
83 g.setText( 0, 4, "Quota Used%:" );
84 g.getCellFormatter().setStyleName(0, 4, "props-toplabels");
85 g.setText( 1, 4, "Bandwith Quota Used%:" );
86 g.getCellFormatter().setStyleName(1, 4, "props-toplabels");
87 initWidget(uiBinder.createAndBindUi(this));
88 searchBox.addKeyPressHandler(new KeyPressHandler() {
91 public void onKeyPress(KeyPressEvent event) {
92 char keyCode = event.getCharCode();
97 usersTable.addRowSelectionHandler(new RowSelectionHandler() {
100 public void onRowSelection(RowSelectionEvent event) {
101 final UserDTO user = usersTable.getSelectedRowObject(event);
104 DeferredCommand.addCommand(new Command() {
107 public void execute() {
108 TwoAdmin.get().showLoadingBox();
109 TwoAdmin.get().getAdminService().getUserStatistics(user.getId(), new AsyncCallback<StatsDTO>() {
112 public void onSuccess(StatsDTO result) {
113 displayProfile(user,result);
114 TwoAdmin.get().hideLoadingBox();
119 public void onFailure(Throwable caught) {
120 GWT.log("Error requesting user statistics file", caught);
121 TwoAdmin.get().hideLoadingBox();
122 TwoAdmin.get().showErrorBox("Error requesting user statistics");
138 @UiHandler("showInactiveButton")
139 void handleWaitingClick(@SuppressWarnings("unused") ClickEvent e){
144 @UiHandler("searchButton")
145 void handleClick(@SuppressWarnings("unused") ClickEvent e){
146 final String toSearch = searchBox.getText();
147 if(toSearch == null || "".equals(toSearch.trim())){
148 TwoAdmin.get().showErrorBox("You must enter a query");
151 lastQuery = toSearch;
152 populateTable(toSearch);
155 @UiHandler("showLastLoginButton")
156 void handleDateClick(@SuppressWarnings("unused") ClickEvent e){
157 final Date toSearch = dateBox.getValue();
158 if(toSearch == null){
159 TwoAdmin.get().showErrorBox("You must enter a query");
162 lastQuery = toSearch;
163 populateTable(toSearch);
167 public void populateTable(String query){
168 TwoAdmin.get().showLoadingBox();
169 if(query.startsWith("username:")){
170 String username = query.replaceAll("username:","");
171 TwoAdmin.get().getAdminService().getUser(username, new AsyncCallback<UserDTO>() {
174 public void onFailure(Throwable caught) {
175 TwoAdmin.get().hideLoadingBox();
176 GWT.log("Error fetching users", caught);
177 TwoAdmin.get().showErrorBox("Unable to Find any Users");
182 public void onSuccess(final UserDTO user) {
183 List<UserDTO> res = new ArrayList<UserDTO>();
185 usersTable.showUsers(res);
186 TwoAdmin.get().getAdminService().getUserStatistics(user.getId(), new AsyncCallback<StatsDTO>() {
189 public void onSuccess(StatsDTO result) {
190 displayProfile(user,result);
191 TwoAdmin.get().hideLoadingBox();
196 public void onFailure(Throwable caught) {
197 GWT.log("Error requesting user statistics file", caught);
198 TwoAdmin.get().hideLoadingBox();
199 TwoAdmin.get().showErrorBox("Error requesting user statistics");
206 TwoAdmin.get().getAdminService().searchUsers(query,new AsyncCallback<List<UserDTO>>() {
209 public void onFailure(Throwable caught) {
210 TwoAdmin.get().hideLoadingBox();
211 GWT.log("Error fetching users", caught);
212 TwoAdmin.get().showErrorBox("Unable to Find any Users");
216 public void onSuccess(List<UserDTO> result) {
217 usersTable.showUsers(result);
218 TwoAdmin.get().hideLoadingBox();
225 private void populateTable(){
226 TwoAdmin.get().showLoadingBox();
227 TwoAdmin.get().getAdminService().getUsersWaitingActivation(new AsyncCallback<List<UserDTO>>() {
230 public void onFailure(Throwable caught) {
231 GWT.log("Error fetching users", caught);
232 TwoAdmin.get().showErrorBox("Unable to Find any Users");
233 TwoAdmin.get().hideLoadingBox();
237 public void onSuccess(List<UserDTO> result) {
238 usersTable.showUsers(result);
239 TwoAdmin.get().hideLoadingBox();
245 private void populateTable(Date query){
246 TwoAdmin.get().showLoadingBox();
247 TwoAdmin.get().getAdminService().getLastLoggedInUsers(query,new AsyncCallback<List<UserDTO>>() {
250 public void onFailure(Throwable caught) {
251 GWT.log("Error fetching users", caught);
252 TwoAdmin.get().showErrorBox("Unable to Find any Users");
253 TwoAdmin.get().hideLoadingBox();
257 public void onSuccess(List<UserDTO> result) {
258 usersTable.showUsers(result);
259 TwoAdmin.get().hideLoadingBox();
266 public void clearProfile(){
267 g.getColumnFormatter().setWidth(1, "200px");
268 g.setWidget( 0, 1, new HTML("") );
269 g.setWidget( 1, 1, new HTML("") );
270 g.setWidget( 2, 1, new HTML("") );
271 g.setWidget( 3, 1, new HTML("") );
273 CheckBox ck = new CheckBox();
275 ck.setEnabled(false);
276 g.setWidget( 4, 1, ck );
277 g.getColumnFormatter().setWidth(3, "200px");
278 g.setWidget( 0, 3, new HTML("") );
279 g.setWidget( 1, 3, new HTML("") );
280 g.setWidget( 2, 3, new HTML("") );
281 g.setWidget( 3, 3, new HTML("") );
282 g.setWidget( 4, 3, new HTML("") );
283 g.setWidget( 5, 3, new HTML("") );
284 g.setWidget( 0, 5, new HTML("") );
285 g.setWidget( 1, 5, new HTML("") );
286 g.setWidget( 2, 5, new HTML("") );
290 public void displayProfile(final UserDTO user, StatsDTO stats){
291 g.setWidget( 0, 1, new HTML(user.getUsername()) );
292 g.setWidget( 1, 1, new HTML(user.getName()) );
294 g.setWidget( 2, 1, new HTML(user.getEmail()) );
295 if(user.getUserClass()!=null){
297 g.setWidget( 3, 1,userClass=new HTML("<a href='#'>"+user.getUserClass().getName()+"</a>") );
298 userClass.addClickHandler(new ClickHandler() {
301 public void onClick(ClickEvent event) {
302 TwoAdmin.get().getAdminService().getUserClasses(new AsyncCallback<List<UserClassDTO>>() {
305 public void onFailure(Throwable caught) {
306 // TODO Auto-generated method stub
311 public void onSuccess(List<UserClassDTO> result) {
312 DialogBox cbox =createDialogBox(user, result);
323 final CheckBox ck = new CheckBox();
324 ck.setValue(user.isActive());
325 ck.setEnabled(false);
326 g.setWidget( 4, 1, ck );
327 Button status = new Button("Toggle Active");
328 status.addClickHandler(new ClickHandler() {
331 public void onClick(ClickEvent event) {
333 ConfirmationDialog confirm = new ConfirmationDialog("Are you sure you want to toggle user State?","Toggle") {
336 public void confirm() {
337 TwoAdmin.get().showLoadingBox();
338 TwoAdmin.get().getAdminService().toggleActiveUser(user.getId(), new AsyncCallback<Void>() {
341 public void onSuccess(Void result) {
342 ck.setValue(!user.isActive());
343 if(getLastQuery() instanceof Date)
344 populateTable((Date)getLastQuery());
345 else if(getLastQuery() instanceof String)
346 populateTable((String)getLastQuery());
349 TwoAdmin.get().hideLoadingBox();
354 public void onFailure(Throwable caught) {
355 TwoAdmin.get().hideLoadingBox();
363 public void cancel() {
364 // TODO Auto-generated method stub
373 g.setWidget(5,1,status);
375 if(user.getUserClass()!=null){
376 g.setWidget( 0, 3, new HTML(user.getUserClass().getQuotaAsString()) );
377 g.setWidget( 1, 3, new HTML("") ); // XXX
378 g.setWidget( 1, 5, new HTML("") ); // XXX
380 g.setWidget( 2, 3, new HTML(""+stats.getFileCount()) );
381 g.setWidget( 3, 3, new HTML(stats.getFileSizeAsString()) );
382 g.setWidget( 4, 3, new HTML(stats.getQuotaLeftAsString()) );
383 g.setWidget( 5, 3, new HTML("") ); // XXX
384 g.setWidget( 0, 5, new HTML(100-stats.percentOfFreeSpace()+"%"));
385 Button remove = new Button("Remove User");
386 remove.addClickHandler(new ClickHandler() {
389 public void onClick(ClickEvent event) {
391 ConfirmationDialog confirm = new ConfirmationDialog("Are you" +
392 " sure you want to <strong>permanently</strong> " +
393 "remove user " + user.getUsername() + "?", "Remove") {
396 public void confirm() {
397 TwoAdmin.get().showLoadingBox();
398 TwoAdmin.get().getAdminService().removeUser(user.getId(), new AsyncCallback<Void>() {
401 public void onSuccess(Void result) {
402 if(getLastQuery() instanceof Date)
403 populateTable((Date)getLastQuery());
404 else if(getLastQuery() instanceof String)
405 populateTable((String)getLastQuery());
408 TwoAdmin.get().hideLoadingBox();
413 public void onFailure(Throwable caught) {
415 TwoAdmin.get().hideLoadingBox();
423 public void cancel() {
424 // TODO Auto-generated method stub
433 g.setWidget(2,5,remove);
441 * Retrieve the lastQuery.
443 * @return the lastQuery
445 public Object getLastQuery() {
449 public DialogBox createDialogBox(final UserDTO m, List<UserClassDTO> classes) {
451 // Create a dialog box and set the caption text
452 final DialogBox dialogBox = new DialogBox();
453 dialogBox.setHTML("User Class: "+m.getUserClass().getName());
455 // Create a table to layout the content
456 VerticalPanel dialogVPanel = new VerticalPanel();
457 dialogBox.setSize("50%", "50%");
459 ClickHandler cancelHandler = new ClickHandler() {
461 public void onClick(ClickEvent event) {
466 dialogBox.setWidget(dialogVPanel);
467 final ListBox listbox = new ListBox();
469 for(UserClassDTO u : classes){
470 listbox.addItem(u.getName(), u.getId().toString());
471 if(u.getName().equals(m.getUserClass().getName()))
472 listbox.setSelectedIndex(i);
475 dialogVPanel.add(listbox);
476 Button change = new Button("Change");
477 change.addClickHandler(new ClickHandler() {
480 public void onClick(ClickEvent event) {
481 TwoAdmin.get().getAdminService().changeUserClass(m.getId(), Long.parseLong(listbox.getValue(listbox.getSelectedIndex())), new AsyncCallback<Void>() {
484 public void onFailure(Throwable caught) {
490 public void onSuccess(Void result) {
491 if(getLastQuery() instanceof Date)
492 populateTable((Date)getLastQuery());
493 else if(getLastQuery() instanceof String)
494 populateTable((String)getLastQuery());
497 TwoAdmin.get().hideLoadingBox();
507 Button close = new Button("Close");
508 close.addClickHandler(cancelHandler);
509 dialogVPanel.add(change);
510 dialogVPanel.add(close);
512 // Return the dialog box