fix dnd bug, and use filelist.getSelectedFiles() on file drop
[pithos] / src / gr / ebs / gss / admin / client / ui / UserPanel.java
1 package gr.ebs.gss.admin.client.ui;
2
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;
7
8 import java.util.ArrayList;
9 import java.util.Date;
10 import java.util.List;
11
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;
37
38 public class UserPanel extends Composite {
39
40         private static UserPanelUiBinder uiBinder = GWT
41                         .create(UserPanelUiBinder.class);
42
43         interface UserPanelUiBinder extends UiBinder<Widget, UserPanel> {
44         }
45
46
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;
55
56         public UserPanel() {
57                 DateTimeFormat dateFormat = DateTimeFormat.getLongDateFormat();
58             dateBox.setFormat(new DateBox.DefaultFormat(dateFormat));
59                 g.setCellPadding(5);
60                 g.setCellSpacing(5);
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() {
89
90                         @Override
91                         public void onKeyPress(KeyPressEvent event) {
92                                 char keyCode = event.getCharCode();
93                                 if (keyCode == '\r')
94                                         handleClick(null);
95                         }
96                 });
97                 usersTable.addRowSelectionHandler(new RowSelectionHandler() {
98
99                         @Override
100                         public void onRowSelection(RowSelectionEvent event) {
101                                 final UserDTO user = usersTable.getSelectedRowObject(event);
102                                 clearProfile();
103                                 if(user!=null)
104                                         DeferredCommand.addCommand(new Command() {
105
106                                                 @Override
107                                                 public void execute() {
108                                                         TwoAdmin.get().showLoadingBox();
109                                                         TwoAdmin.get().getAdminService().getUserStatistics(user.getId(), new AsyncCallback<StatsDTO>() {
110
111                                                                 @Override
112                                                                 public void onSuccess(StatsDTO result) {
113                                                                         displayProfile(user,result);
114                                                                         TwoAdmin.get().hideLoadingBox();
115
116                                                                 }
117
118                                                                 @Override
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");
123
124                                                                 }
125                                                         });
126
127                                                 }
128                                         });
129
130                         }
131                 });
132
133
134                 clearProfile();
135
136         }
137
138         @UiHandler("showInactiveButton")
139         void handleWaitingClick(@SuppressWarnings("unused") ClickEvent e){
140                 lastQuery = true;
141                 populateTable();
142         }
143
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");
149                         return;
150                 }
151                 lastQuery = toSearch;
152                 populateTable(toSearch);
153         }
154
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");
160                         return;
161                 }
162                 lastQuery = toSearch;
163                 populateTable(toSearch);
164         }
165
166
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>() {
172
173                                                         @Override
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");
178
179                                                         }
180
181                                                         @Override
182                                                         public void onSuccess(final UserDTO user) {
183                                                                 List<UserDTO> res = new ArrayList<UserDTO>();
184                                                                 res.add(user);
185                                                                 usersTable.showUsers(res);
186                                                                 TwoAdmin.get().getAdminService().getUserStatistics(user.getId(), new AsyncCallback<StatsDTO>() {
187
188                                                                         @Override
189                                                                         public void onSuccess(StatsDTO result) {
190                                                                                 displayProfile(user,result);
191                                                                                 TwoAdmin.get().hideLoadingBox();
192
193                                                                         }
194
195                                                                         @Override
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");
200
201                                                                         }
202                                                                 });
203                                                         }
204                                                         });
205                 } else
206                         TwoAdmin.get().getAdminService().searchUsers(query,new AsyncCallback<List<UserDTO>>() {
207
208                                 @Override
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");
213                                 }
214
215                                 @Override
216                                 public void onSuccess(List<UserDTO> result) {
217                                         usersTable.showUsers(result);
218                                         TwoAdmin.get().hideLoadingBox();
219
220                                 }
221
222                         });
223         }
224
225         private void populateTable(){
226                 TwoAdmin.get().showLoadingBox();
227                 TwoAdmin.get().getAdminService().getUsersWaitingActivation(new AsyncCallback<List<UserDTO>>() {
228
229                         @Override
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();
234                         }
235
236                         @Override
237                         public void onSuccess(List<UserDTO> result) {
238                                 usersTable.showUsers(result);
239                                 TwoAdmin.get().hideLoadingBox();
240                         }
241
242                 });
243         }
244
245         private void populateTable(Date query){
246                 TwoAdmin.get().showLoadingBox();
247                 TwoAdmin.get().getAdminService().getLastLoggedInUsers(query,new AsyncCallback<List<UserDTO>>() {
248
249                         @Override
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();
254                         }
255
256                         @Override
257                         public void onSuccess(List<UserDTO> result) {
258                                 usersTable.showUsers(result);
259                                 TwoAdmin.get().hideLoadingBox();
260                         }
261
262                 });
263         }
264
265
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("") );
272
273         CheckBox ck = new CheckBox();
274         ck.setValue(false);
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("") );
287
288         }
289
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()) );
293
294         g.setWidget( 2, 1, new HTML(user.getEmail()) );
295         if(user.getUserClass()!=null){
296                 HTML userClass;
297                 g.setWidget( 3, 1,userClass=new HTML("<a href='#'>"+user.getUserClass().getName()+"</a>") );
298                 userClass.addClickHandler(new ClickHandler() {
299
300                                 @Override
301                                 public void onClick(ClickEvent event) {
302                                         TwoAdmin.get().getAdminService().getUserClasses(new AsyncCallback<List<UserClassDTO>>() {
303
304                                                 @Override
305                                                 public void onFailure(Throwable caught) {
306                                                         // TODO Auto-generated method stub
307
308                                                 }
309
310                                                 @Override
311                                                 public void onSuccess(List<UserClassDTO> result) {
312                                                         DialogBox cbox =createDialogBox(user, result);
313                                                         cbox.center();
314                                                         cbox.show();
315
316                                                 }
317
318                                         });
319
320                                 }
321                         });
322         }
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() {
329
330                         @Override
331                         public void onClick(ClickEvent event) {
332
333                                 ConfirmationDialog confirm = new ConfirmationDialog("Are you sure you want to toggle user State?","Toggle") {
334
335                                         @Override
336                                         public void confirm() {
337                                                 TwoAdmin.get().showLoadingBox();
338                                                 TwoAdmin.get().getAdminService().toggleActiveUser(user.getId(), new AsyncCallback<Void>() {
339
340                                                         @Override
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());
347                                                                 else
348                                                                         populateTable();
349                                                                 TwoAdmin.get().hideLoadingBox();
350
351                                                         }
352
353                                                         @Override
354                                                         public void onFailure(Throwable caught) {
355                                                                 TwoAdmin.get().hideLoadingBox();
356
357                                                         }
358                                                 });
359
360                                         }
361
362                                         @Override
363                                         public void cancel() {
364                                                 // TODO Auto-generated method stub
365
366                                         }
367                                 };
368                                 confirm.center();
369                                 confirm.show();
370
371                         }
372                 });
373         g.setWidget(5,1,status);
374
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
379         }
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() {
387
388                         @Override
389                         public void onClick(ClickEvent event) {
390
391                                 ConfirmationDialog confirm = new ConfirmationDialog("Are you" +
392                                                 " sure you want to <strong>permanently</strong> " +
393                                                 "remove user " + user.getUsername() + "?", "Remove") {
394
395                                         @Override
396                                         public void confirm() {
397                                                 TwoAdmin.get().showLoadingBox();
398                                                 TwoAdmin.get().getAdminService().removeUser(user.getId(), new AsyncCallback<Void>() {
399
400                                                         @Override
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());
406                                                                 else
407                                                                         populateTable();
408                                                                 TwoAdmin.get().hideLoadingBox();
409
410                                                         }
411
412                                                         @Override
413                                                         public void onFailure(Throwable caught) {
414                                                                 GWT.log("",caught);
415                                                                 TwoAdmin.get().hideLoadingBox();
416
417                                                         }
418                                                 });
419
420                                         }
421
422                                         @Override
423                                         public void cancel() {
424                                                 // TODO Auto-generated method stub
425
426                                         }
427                                 };
428                                 confirm.center();
429                                 confirm.show();
430
431                         }
432                 });
433         g.setWidget(2,5,remove);
434
435
436
437         }
438
439
440         /**
441          * Retrieve the lastQuery.
442          *
443          * @return the lastQuery
444          */
445         public Object getLastQuery() {
446                 return lastQuery;
447         }
448
449         public DialogBox createDialogBox(final UserDTO m, List<UserClassDTO> classes) {
450
451                 // Create a dialog box and set the caption text
452                 final DialogBox dialogBox = new DialogBox();
453                 dialogBox.setHTML("User Class: "+m.getUserClass().getName());
454
455                 // Create a table to layout the content
456                 VerticalPanel dialogVPanel = new VerticalPanel();
457                 dialogBox.setSize("50%", "50%");
458
459                 ClickHandler cancelHandler = new ClickHandler() {
460                         @Override
461                         public void onClick(ClickEvent event) {
462                                 dialogBox.hide();
463                         }
464                 };
465
466                 dialogBox.setWidget(dialogVPanel);
467                 final ListBox listbox = new ListBox();
468                 int i=0;
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);
473                         i++;
474                 }
475                 dialogVPanel.add(listbox);
476                 Button change = new Button("Change");
477                 change.addClickHandler(new ClickHandler() {
478
479                         @Override
480                         public void onClick(ClickEvent event) {
481                                 TwoAdmin.get().getAdminService().changeUserClass(m.getId(), Long.parseLong(listbox.getValue(listbox.getSelectedIndex())), new AsyncCallback<Void>() {
482
483                                         @Override
484                                         public void onFailure(Throwable caught) {
485
486
487                                         }
488
489                                         @Override
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());
495                                                 else
496                                                         populateTable();
497                                                 TwoAdmin.get().hideLoadingBox();
498
499                                         }
500
501                                 });
502                                 dialogBox.hide();
503
504                         }
505                 });
506
507                 Button close = new Button("Close");
508                 close.addClickHandler(cancelHandler);
509                 dialogVPanel.add(change);
510                 dialogVPanel.add(close);
511
512                 // Return the dialog box
513                 return dialogBox;
514         }
515 }