Sent invitations along with status are shown in the invitation dialog
[pithos-web-client] / src / gr / grnet / pithos / web / client / Invitations.java
1 package gr.grnet.pithos.web.client;
2
3 import java.util.ArrayList;
4 import java.util.Date;
5 import java.util.List;
6
7 import com.google.gwt.http.client.Response;
8 import com.google.gwt.json.client.JSONArray;
9 import com.google.gwt.json.client.JSONObject;
10 import com.google.gwt.json.client.JSONParser;
11 import com.google.gwt.json.client.JSONValue;
12
13 import gr.grnet.pithos.web.client.foldertree.Resource;
14
15 public class Invitations extends Resource {
16
17         private int invitationsLeft = 0;
18         
19         private List<Invitation> sentInvitations = new ArrayList<Invitation>();
20         
21         @Override
22         public Date getLastModified() {
23                 return null;
24         }
25
26         public int getInvitationsLeft() {
27                 return invitationsLeft;
28         }
29
30         public static Invitations createFromResponse(Response response) {
31                 Invitations result = new Invitations();
32                 result.populate(response);
33                 return result;
34         }
35
36         private void populate(Response response) {
37         JSONValue json = JSONParser.parseStrict(response.getText());
38         JSONObject o = json.isObject();
39         if (o != null) {
40                 invitationsLeft = unmarshallInt(o, "invitations");
41                 JSONArray sent = o.get("sent").isArray();
42                 if (sent != null) {
43                         for (int i=0; i<sent.size(); i++) {
44                                 JSONObject ob = sent.get(i).isObject();
45                                 if (ob != null) {
46                                         boolean accepted = unmarshallBoolean(ob, "is_accepted");
47                                         String email = unmarshallString(ob, "email");
48                                         String realname = unmarshallString(ob, "realname");
49                                         Invitation inv = new Invitation(accepted, realname, email);
50                                         sentInvitations.add(inv);
51                                 }
52                         }
53                 }
54         }
55         }
56
57         public List<Invitation> getSentInvitations() {
58                 return sentInvitations;
59         }
60
61 }