root / web_client / src / gr / grnet / pithos / web / client / foldertree / Folder.java @ 1ac430a1
History | View | Annotate | Download (4.9 kB)
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.Date; |
15 |
import java.util.LinkedHashSet; |
16 |
import java.util.Set; |
17 |
|
18 |
public class Folder extends Resource { |
19 |
/*
|
20 |
* The name of the folder. If the folder is a container this is its name. If it is a virtual folder this is the
|
21 |
* last part of its path
|
22 |
*/
|
23 |
private String name = null; |
24 |
|
25 |
private Date lastModified = null; |
26 |
|
27 |
private long bytesUsed = 0; |
28 |
|
29 |
private Set<Folder> subfolders = new LinkedHashSet<Folder>(); |
30 |
/*
|
31 |
* The name of the container that this folder belongs to. If this folder is container, this field equals name
|
32 |
*/
|
33 |
private String container = null; |
34 |
|
35 |
/*
|
36 |
* This is the full path of the folder (prefix is a misnomer but it was named so because this is used as a prefix=
|
37 |
* parameter in the request that fetches its children). If the folder is a cointainer this is empty string
|
38 |
*/
|
39 |
private String prefix = ""; |
40 |
|
41 |
public Folder() {};
|
42 |
|
43 |
public Folder(String name) { |
44 |
this.name = name;
|
45 |
} |
46 |
|
47 |
public String getName() { |
48 |
return name;
|
49 |
} |
50 |
|
51 |
public Date getLastModified() { |
52 |
return lastModified;
|
53 |
} |
54 |
|
55 |
public long getBytesUsed() { |
56 |
return bytesUsed;
|
57 |
} |
58 |
|
59 |
public void setLastModified(Date lastModified) { |
60 |
this.lastModified = lastModified;
|
61 |
} |
62 |
|
63 |
public Set<Folder> getSubfolders() { |
64 |
return subfolders;
|
65 |
} |
66 |
|
67 |
public void setSubfolders(Set<Folder> subfolders) { |
68 |
this.subfolders = subfolders;
|
69 |
} |
70 |
|
71 |
public String getContainer() { |
72 |
return container;
|
73 |
} |
74 |
|
75 |
public String getPrefix() { |
76 |
return prefix;
|
77 |
} |
78 |
|
79 |
public void setPrefix(String prefix) { |
80 |
this.prefix = prefix;
|
81 |
} |
82 |
|
83 |
public void populate(Response response) { |
84 |
String header = response.getHeader("Last-Modified"); |
85 |
if (header != null) |
86 |
lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header); |
87 |
|
88 |
header = response.getHeader("X-Container-Bytes-Used");
|
89 |
if (header != null) |
90 |
bytesUsed = Long.valueOf(header);
|
91 |
|
92 |
JSONValue json = JSONParser.parseStrict(response.getText()); |
93 |
JSONArray array = json.isArray(); |
94 |
if (array != null) { |
95 |
for (int i=0; i<array.size(); i++) { |
96 |
JSONObject o = array.get(i).isObject(); |
97 |
if (o != null) { |
98 |
String contentType = unmarshallString(o, "content_type"); |
99 |
if (o.containsKey("subdir") || (contentType != null && contentType.startsWith("application/directory"))) { |
100 |
Folder f = new Folder();
|
101 |
f.populate(o, container); |
102 |
subfolders.add(f); |
103 |
} |
104 |
else {
|
105 |
// add file
|
106 |
} |
107 |
} |
108 |
} |
109 |
} |
110 |
} |
111 |
|
112 |
public void populate(JSONObject o, String aContainer) { |
113 |
String path = null; |
114 |
if (o.containsKey("subdir")) { |
115 |
path = unmarshallString(o, "subdir");
|
116 |
} |
117 |
else {
|
118 |
path = unmarshallString(o, "name");
|
119 |
lastModified = unmarshallDate(o, "last_modified");
|
120 |
} |
121 |
if (path.endsWith("/")) |
122 |
path = path.substring(0, path.length() - 1); |
123 |
if (path.contains("/")) |
124 |
name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix |
125 |
else
|
126 |
name = path; |
127 |
if (aContainer != null) { |
128 |
container = aContainer; |
129 |
prefix = path; |
130 |
} |
131 |
else {
|
132 |
container = name; |
133 |
prefix = "";
|
134 |
} |
135 |
} |
136 |
|
137 |
@Override
|
138 |
public String getLastModifiedSince() { |
139 |
return null; //To change body of implemented methods use File | Settings | File Templates. |
140 |
} |
141 |
|
142 |
public static Folder createFromResponse(Response response, Folder result) { |
143 |
Folder f = null;
|
144 |
if (result == null) |
145 |
f = new Folder();
|
146 |
else
|
147 |
f = result; |
148 |
|
149 |
f.populate(response); |
150 |
return f;
|
151 |
} |
152 |
|
153 |
@Override
|
154 |
public boolean equals(Object other) { |
155 |
if (other instanceof Folder) { |
156 |
Folder o = (Folder) other; |
157 |
if (container != null) |
158 |
return prefix.equals(o.getPrefix()) && container.equals(o.getContainer());
|
159 |
else
|
160 |
return o.getContainer() == null && name.equals(o.getName()); |
161 |
} |
162 |
return false; |
163 |
} |
164 |
|
165 |
@Override
|
166 |
public int hashCode() { |
167 |
return prefix.hashCode() + name.hashCode();
|
168 |
} |
169 |
} |