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;
20 public class Folder extends Resource {
22 * The name of the folder. If the folder is a container this is its name. If it is a virtual folder this is the
23 * last part of its path
25 private String name = null;
27 private Date lastModified = null;
29 private long bytesUsed = 0;
31 private Set<Folder> subfolders = new LinkedHashSet<Folder>();
33 * The name of the container that this folder belongs to. If this folder is container, this field equals name
35 private String container = null;
38 * This is the full path of the folder (prefix is a misnomer but it was named so because this is used as a prefix=
39 * parameter in the request that fetches its children). If the folder is a cointainer this is empty string
41 private String prefix = "";
43 private Set<File> files = new LinkedHashSet<File>();
47 public Folder(String name) {
51 public String getName() {
55 public Date getLastModified() {
59 public long getBytesUsed() {
63 public void setLastModified(Date lastModified) {
64 this.lastModified = lastModified;
67 public Set<Folder> getSubfolders() {
71 public void setSubfolders(Set<Folder> subfolders) {
72 this.subfolders = subfolders;
75 public String getContainer() {
79 public String getPrefix() {
83 public void setPrefix(String prefix) {
87 public void populate(Response response) {
88 String header = response.getHeader("Last-Modified");
90 lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
92 header = response.getHeader("X-Container-Bytes-Used");
94 bytesUsed = Long.valueOf(header);
96 JSONValue json = JSONParser.parseStrict(response.getText());
97 JSONArray array = json.isArray();
99 for (int i=0; i<array.size(); i++) {
100 JSONObject o = array.get(i).isObject();
102 String contentType = unmarshallString(o, "content_type");
103 if (o.containsKey("subdir") || (contentType != null && contentType.startsWith("application/directory"))) {
104 Folder f = new Folder();
105 f.populate(o, container);
109 File file = new File();
118 public void populate(JSONObject o, String aContainer) {
120 if (o.containsKey("subdir")) {
121 path = unmarshallString(o, "subdir");
124 path = unmarshallString(o, "name");
125 lastModified = unmarshallDate(o, "last_modified");
127 if (path.endsWith("/"))
128 path = path.substring(0, path.length() - 1);
129 if (path.contains("/"))
130 name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
133 if (aContainer != null) {
134 container = aContainer;
144 public String getLastModifiedSince() {
145 return null; //To change body of implemented methods use File | Settings | File Templates.
148 public static Folder createFromResponse(Response response, Folder result) {
155 f.populate(response);
160 public boolean equals(Object other) {
161 if (other instanceof Folder) {
162 Folder o = (Folder) other;
163 if (container != null)
164 return prefix.equals(o.getPrefix()) && container.equals(o.getContainer());
166 return o.getContainer() == null && name.equals(o.getName());
172 public int hashCode() {
173 return prefix.hashCode() + name.hashCode();
176 public Set<File> getFiles() {