Implemented move folder to trash
[pithos-web-client] / src / gr / grnet / pithos / web / client / foldertree / Folder.java
1 /*
2  * Copyright 2011 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials
15  *      provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and
31  * documentation are those of the authors and should not be
32  * interpreted as representing official policies, either expressed
33  * or implied, of GRNET S.A.
34  */
35
36 package gr.grnet.pithos.web.client.foldertree;
37
38 import com.google.gwt.http.client.Response;
39 import com.google.gwt.i18n.client.DateTimeFormat;
40 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
41 import com.google.gwt.json.client.JSONArray;
42 import com.google.gwt.json.client.JSONObject;
43 import com.google.gwt.json.client.JSONParser;
44 import com.google.gwt.json.client.JSONValue;
45 import java.util.ArrayList;
46 import java.util.Date;
47 import java.util.Iterator;
48 import java.util.LinkedHashSet;
49 import java.util.List;
50 import java.util.Set;
51
52 public class Folder extends Resource {
53     /*
54      * The name of the folder. If the folder is a container this is its name. If it is a virtual folder this is the
55      * last part of its path
56      */
57     private String name = null;
58
59     private Date lastModified = null;
60
61     private long bytesUsed = 0;
62
63     private Folder parent = null;
64     
65     private Set<Folder> subfolders = new LinkedHashSet<Folder>();
66     /*
67      * The name of the container that this folder belongs to. If this folder is container, this field equals name
68      */
69     private String container = null;
70
71     /*
72      * This is the full path of the folder (prefix is a misnomer but it was named so because this is used as a prefix=
73      * parameter in the request that fetches its children). If the folder is a cointainer this is empty string
74      */
75     private String prefix = "";
76
77     private Set<File> files = new LinkedHashSet<File>();
78
79     private boolean inTrash = false;
80
81     public Folder() {};
82
83     public Folder(String name) {
84         this.name = name;
85     }
86     
87     public String getName() {
88         return name;
89     }
90
91     public Date getLastModified() {
92         return lastModified;
93     }
94
95     public long getBytesUsed() {
96         return bytesUsed;
97     }
98
99     public void setLastModified(Date lastModified) {
100         this.lastModified = lastModified;
101     }
102
103     public Set<Folder> getSubfolders() {
104         return subfolders;
105     }
106
107     public void setSubfolders(Set<Folder> subfolders) {
108         this.subfolders = subfolders;
109     }
110
111     public String getContainer() {
112         return container;
113     }
114
115     public String getPrefix() {
116         return prefix;
117     }
118
119     public void setPrefix(String prefix) {
120         this.prefix = prefix;
121     }
122
123     public void populate(Response response) {
124         String header = response.getHeader("Last-Modified");
125         if (header != null)
126             lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
127
128         header = response.getHeader("X-Container-Bytes-Used");
129         if (header != null)
130             bytesUsed = Long.valueOf(header);
131
132         header = response.getHeader("X-Object-Meta-Trash");
133         if (header != null && header.equals("true"))
134             inTrash = true;
135
136         subfolders.clear(); //This is necessary in case we update a pre-existing Folder so that stale subfolders won't show up
137         files.clear();
138         JSONValue json = JSONParser.parseStrict(response.getText());
139         JSONArray array = json.isArray();
140         if (array != null) {
141             for (int i=0; i<array.size(); i++) {
142                 JSONObject o = array.get(i).isObject();
143                 if (o != null) {
144                     String contentType = unmarshallString(o, "content_type");
145                     if (o.containsKey("subdir") || (contentType != null && (contentType.startsWith("application/directory") || contentType.startsWith("application/folder")))) {
146                         Folder f = new Folder();
147                         f.populate(this, o, container);
148                         subfolders.add(f);
149                     }
150                     else {
151                         File file = new File();
152                         file.populate(this, o, container);
153                         files.add(file);
154                     }
155                 }
156             }
157             Iterator<Folder> iter = subfolders.iterator();
158             while (iter.hasNext()) {
159                 Folder f = iter.next();
160                 if (f.isInTrash())
161                     iter.remove();
162             }
163             Iterator<File> it = files.iterator();
164             while (it.hasNext()) {
165                 File f = it.next();
166                 if (f.isInTrash())
167                     it.remove();
168             }
169         }
170     }
171
172     public void populate(Folder parent, JSONObject o, String aContainer) {
173         this.parent = parent;
174         String path = null;
175         if (o.containsKey("subdir")) {
176             path = unmarshallString(o, "subdir");
177         }
178         else {
179             path = unmarshallString(o, "name");
180             lastModified = unmarshallDate(o, "last_modified");
181         }
182         if (path.endsWith("/"))
183             path = path.substring(0, path.length() - 1);
184         if (path.contains("/"))
185             name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
186         else
187             name = path;
188         if (aContainer != null) {
189             container = aContainer;
190             prefix = path;
191         }
192         else {
193             container = name;
194             prefix = "";
195         }
196         if (o.containsKey("x_object_meta_trash") && o.get("x_object_meta_trash").isString().stringValue().equals("true"))
197             inTrash = true;
198     }
199
200     public static Folder createFromResponse(Response response, Folder result) {
201         Folder f = null;
202         if (result == null)
203             f = new Folder();
204         else
205             f = result;
206
207         f.populate(response);
208         return f;
209     }
210
211     @Override
212     public boolean equals(Object other) {
213         if (other instanceof Folder) {
214             Folder o = (Folder) other;
215             return (container + prefix).equals(o.getContainer() + o.getPrefix());
216         }
217         return false;
218     }
219
220     @Override
221     public int hashCode() {
222         return (container + prefix).hashCode();
223     }
224
225     public Set<File> getFiles() {
226         return files;
227     }
228
229     public Folder getParent() {
230         return parent;
231     }
232
233     public String getUri() {
234         return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix);
235     }
236
237     public boolean isInTrash() {
238         return inTrash;
239     }
240 }