2 * Copyright (c) 2011 Greek Research and Technology Network
5 package gr.grnet.pithos.web.client.foldertree;
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;
19 import org.w3c.css.sac.ElementSelector;
21 public class Folder extends Resource {
22 private String name = null;
24 private Date lastModified = null;
26 private long bytesUsed = 0;
28 private Set<Folder> subfolders = new LinkedHashSet<Folder>();
29 private String container = null;
31 private String prefix = "";
35 public Folder(String name) {
39 public String getName() {
43 public Date getLastModified() {
47 public long getBytesUsed() {
51 public void setLastModified(Date lastModified) {
52 this.lastModified = lastModified;
55 public Set<Folder> getSubfolders() {
59 public void setSubfolders(Set<Folder> subfolders) {
60 this.subfolders = subfolders;
63 public String getContainer() {
67 public void setContainer(String container) {
68 this.container = container;
71 public String getPrefix() {
75 public void setPrefix(String prefix) {
79 public void populate(Response response) {
80 String header = response.getHeader("Last-Modified");
82 lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
84 header = response.getHeader("X-Container-Bytes-Used");
86 bytesUsed = Long.valueOf(header);
88 JSONValue json = JSONParser.parseStrict(response.getText());
89 JSONArray array = json.isArray();
91 for (int i=0; i<array.size(); i++) {
92 JSONObject o = array.get(i).isObject();
94 if (o.containsKey("subdir")) {
95 Folder f = new Folder();
97 f.setContainer(container == null ? name : container);
98 f.setPrefix(container == null ? f.getName() : prefix + "/" + f.getName());
102 String contentType = unmarshallString(o, "content_type");
103 if (contentType != null && contentType.startsWith("application/directory")) {
104 Folder f = new Folder();
106 f.setContainer(container == null ? name : container);
107 f.setPrefix(container == null ? f.getName() : prefix + "/" + f.getName());
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);
126 name = unmarshallString(o, "name");
127 lastModified = unmarshallDate(o, "last_modified");
132 public String getLastModifiedSince() {
133 return null; //To change body of implemented methods use File | Settings | File Templates.
136 public static Folder createFromResponse(Response response, Folder result) {
143 f.populate(response);
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());
157 public int hashCode() {
158 return prefix.hashCode() + name.hashCode();