bfebfabd49c1cc55a9b7f1839974f04a6cc2cf93
[pithos-web-client] / src / gr / grnet / pithos / web / client / catalog / UpdateUserCatalogs.java
1 package gr.grnet.pithos.web.client.catalog;
2
3 import com.google.gwt.core.client.Scheduler;
4 import com.google.gwt.http.client.Request;
5 import com.google.gwt.http.client.Response;
6 import com.google.gwt.json.client.JSONObject;
7 import gr.grnet.pithos.web.client.Helpers;
8 import gr.grnet.pithos.web.client.Pithos;
9
10 import java.util.List;
11
12 /**
13  * This is a wrapper around GetUserCatalogs that takes care of updating
14  * the application-wide user catalogs.
15  *
16  * @author Christos KK Loverdos <loverdos@gmail.com>
17  */
18 public class UpdateUserCatalogs implements Scheduler.ScheduledCommand {
19     private final GetUserCatalogs getUserCatalogs;
20     private final Pithos app;
21
22     public UpdateUserCatalogs(Pithos app) {
23         this(app, null, null);
24     }
25
26     public UpdateUserCatalogs(Pithos app, String userID) {
27         this(app, Helpers.toList(userID), null);
28     }
29
30     public UpdateUserCatalogs(Pithos app, List<String> ids) {
31         this(app, ids, null);
32     }
33
34     public void onSuccess(UserCatalogs requestedUserCatalogs, UserCatalogs updatedUserCatalogs) {
35     }
36
37     public void onError(Request request, Throwable t) {
38         getUserCatalogs.onError(request, t);
39     }
40
41     public UpdateUserCatalogs(Pithos app, List<String> ids, List<String> names) {
42         this.app = app;
43         this.getUserCatalogs = new GetUserCatalogs(app, ids, names) {
44             @Override
45             public void onSuccess(Request request, Response response, JSONObject result, UserCatalogs userCatalogs) {
46                 UpdateUserCatalogs.this.app.getUserCatalogs().updateFrom(userCatalogs);
47                 UpdateUserCatalogs.this.onSuccess(userCatalogs, UpdateUserCatalogs.this.app.getUserCatalogs());
48             }
49
50             @Override
51             public void onError(Request request, Throwable t) {
52                 UpdateUserCatalogs.this.onError(request, t);
53             }
54         };
55     }
56
57     @Override
58     public void execute() {
59         this.getUserCatalogs.execute();
60     }
61
62     public void scheduleDeferred() {
63         Scheduler.get().scheduleDeferred(this);
64     }
65 }