Finally displayed virtual folder hierarchy up to second level
[pithos] / web_client / src / gr / grnet / pithos / web / client / foldertree / Folder.java
1 /*
2  * Copyright (c) 2011 Greek Research and Technology Network
3  */
4
5 package gr.grnet.pithos.web.client.foldertree;
6
7 import com.google.gwt.http.client.Response;
8 import com.google.gwt.i18n.client.DateTimeFormat;
9 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
10 import com.google.gwt.json.client.JSONArray;
11 import com.google.gwt.json.client.JSONObject;
12 import com.google.gwt.json.client.JSONParser;
13 import com.google.gwt.json.client.JSONValue;
14 import java.util.ArrayList;
15 import java.util.Date;
16 import java.util.LinkedHashSet;
17 import java.util.List;
18 import java.util.Set;
19 import org.w3c.css.sac.ElementSelector;
20
21 public class Folder extends Resource {
22     private String name = null;
23
24     private Date lastModified = null;
25
26     private long bytesUsed = 0;
27
28     private Set<Folder> subfolders = new LinkedHashSet<Folder>();
29     private String container = null;
30
31     private String prefix = "";
32
33     public Folder() {};
34
35     public Folder(String name) {
36         this.name = name;
37     }
38     
39     public String getName() {
40         return name;
41     }
42
43     public Date getLastModified() {
44         return lastModified;
45     }
46
47     public long getBytesUsed() {
48         return bytesUsed;
49     }
50
51     public void setLastModified(Date lastModified) {
52         this.lastModified = lastModified;
53     }
54
55     public Set<Folder> getSubfolders() {
56         return subfolders;
57     }
58
59     public void setSubfolders(Set<Folder> subfolders) {
60         this.subfolders = subfolders;
61     }
62
63     public String getContainer() {
64         return container;
65     }
66
67     public void setContainer(String container) {
68         this.container = container;
69     }
70
71     public String getPrefix() {
72         return prefix;
73     }
74
75     public void setPrefix(String prefix) {
76         this.prefix = prefix;
77     }
78
79     public void populate(Response response) {
80         String header = response.getHeader("Last-Modified");
81         if (header != null)
82             lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
83
84         header = response.getHeader("X-Container-Bytes-Used");
85         if (header != null)
86             bytesUsed = Long.valueOf(header);
87
88         JSONValue json = JSONParser.parseStrict(response.getText());
89         JSONArray array = json.isArray();
90         if (array != null) {
91             for (int i=0; i<array.size(); i++) {
92                 JSONObject o = array.get(i).isObject();
93                 if (o != null) {
94                     if (o.containsKey("subdir")) {
95                         Folder f = new Folder();
96                         f.populate(o);
97                         f.setContainer(container == null ? name : container);
98                         f.setPrefix(container == null ? f.getName() : prefix + "/" + f.getName());
99                         subfolders.add(f);
100                     }
101                     else {
102                         String contentType = unmarshallString(o, "content_type");
103                         if (contentType != null && contentType.startsWith("application/directory")) {
104                             Folder f = new Folder();
105                             f.populate(o);
106                             f.setContainer(container == null ? name : container);
107                             f.setPrefix(container == null ? f.getName() : prefix + "/" + f.getName());
108                             subfolders.add(f);
109                         }
110                         else {
111                             // add file
112                         }
113                     }
114                 }
115             }
116         }
117     }
118
119     public void populate(JSONObject o) {
120         if (o.containsKey("subdir")) {
121             name = unmarshallString(o, "subdir");
122             if (name.endsWith("/"))
123                 name = name.substring(0, name.length() - 1);
124         }
125         else {
126             name = unmarshallString(o, "name");
127             lastModified = unmarshallDate(o, "last_modified");
128         }
129     }
130
131     @Override
132     public String getLastModifiedSince() {
133         return null;  //To change body of implemented methods use File | Settings | File Templates.
134     }
135
136     public static Folder createFromResponse(Response response, Folder result) {
137         Folder f = null;
138         if (result == null)
139             f = new Folder();
140         else
141             f = result;
142
143         f.populate(response);
144         return f;
145     }
146
147     @Override
148     public boolean equals(Object other) {
149         if (other instanceof Folder) {
150             Folder o = (Folder) other;
151             return name.equals(o.getName()) && prefix.equals(o.getPrefix());
152         }
153         return false;
154     }
155
156     @Override
157     public int hashCode() {
158         return prefix.hashCode() + name.hashCode();
159     }
160 }