Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / foldertree / Folder.java @ a39e5b47

History | View | Annotate | Download (8.5 kB)

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
import java.util.StringTokenizer;
52

    
53
public class Folder extends Resource {
54
    /*
55
     * The name of the folder. If the folder is a container this is its name. If it is a virtual folder this is the
56
     * last part of its path
57
     */
58
    private String name = null;
59

    
60
    private Date lastModified = null;
61

    
62
    private long bytesUsed = 0;
63

    
64
    private Folder parent = null;
65
    
66
    private Set<Folder> subfolders = new LinkedHashSet<Folder>();
67
    /*
68
     * The name of the container that this folder belongs to. If this folder is container, this field equals name
69
     */
70
    private String container = null;
71

    
72
    /*
73
     * This is the full path of the folder (prefix is a misnomer but it was named so because this is used as a prefix=
74
     * parameter in the request that fetches its children). If the folder is a cointainer this is empty string
75
     */
76
    private String prefix = "";
77

    
78
    private Set<File> files = new LinkedHashSet<File>();
79

    
80
    private boolean inTrash = false;
81

    
82
    /*
83
     * Flag that indicates that this folder is the Trash
84
     */
85
    private boolean trash = false;
86

    
87
    private Set<String> tags = new LinkedHashSet<String>();
88

    
89
    public Folder() {};
90

    
91
    public Folder(String name) {
92
        this.name = name;
93
    }
94
    
95
    public String getName() {
96
        return name;
97
    }
98

    
99
    public Date getLastModified() {
100
        return lastModified;
101
    }
102

    
103
    public long getBytesUsed() {
104
        return bytesUsed;
105
    }
106

    
107
    public void setLastModified(Date lastModified) {
108
        this.lastModified = lastModified;
109
    }
110

    
111
    public Set<Folder> getSubfolders() {
112
        return subfolders;
113
    }
114

    
115
    public void setSubfolders(Set<Folder> subfolders) {
116
        this.subfolders = subfolders;
117
    }
118

    
119
    public String getContainer() {
120
        return container;
121
    }
122

    
123
    public String getPrefix() {
124
        return prefix;
125
    }
126

    
127
    public void setPrefix(String prefix) {
128
        this.prefix = prefix;
129
    }
130

    
131
    public void populate(Response response) {
132
        String header = response.getHeader("Last-Modified");
133
        if (header != null)
134
            lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
135

    
136
        header = response.getHeader("X-Container-Bytes-Used");
137
        if (header != null)
138
            bytesUsed = Long.valueOf(header);
139

    
140
        header = response.getHeader("X-Object-Meta-Trash");
141
        if (header != null && header.equals("true"))
142
            inTrash = true;
143

    
144
        header = response.getHeader("X-Container-Object-Meta");
145
        if (header != null && header.length() > 0) {
146
            for (String t : header.split(",")) {
147
                tags.add(t.toLowerCase().trim());
148
            }
149
        }
150

    
151
        subfolders.clear(); //This is necessary in case we update a pre-existing Folder so that stale subfolders won't show up
152
        files.clear();
153
        JSONValue json = JSONParser.parseStrict(response.getText());
154
        JSONArray array = json.isArray();
155
        if (array != null) {
156
            for (int i=0; i<array.size(); i++) {
157
                JSONObject o = array.get(i).isObject();
158
                if (o != null) {
159
                    String contentType = unmarshallString(o, "content_type");
160
                    if (o.containsKey("subdir") || (contentType != null && (contentType.startsWith("application/directory") || contentType.startsWith("application/folder")))) {
161
                        Folder f = new Folder();
162
                        f.populate(this, o, container);
163
                        subfolders.add(f);
164
                    }
165
                    else if (!(o.containsKey("x_object_meta_trash") && o.get("x_object_meta_trash").isString().stringValue().equals("true"))) {
166
                        File file = new File();
167
                        file.populate(this, o, container);
168
                        files.add(file);
169
                    }
170
                }
171
            }
172
            //This step is necessary to remove the trashed folders. Trashed folders are added initially because we need to
173
            //avoid having in the list the virtual folders of the form {"subdir":"folder1"} which have no indication of thrash
174
            Iterator<Folder> iter = subfolders.iterator();
175
            while (iter.hasNext()) {
176
                Folder f = iter.next();
177
                if (f.isInTrash())
178
                    iter.remove();
179
            }
180
        }
181
    }
182

    
183
    public void populate(Folder parent, JSONObject o, String aContainer) {
184
        this.parent = parent;
185
        String path = null;
186
        if (o.containsKey("subdir")) {
187
            path = unmarshallString(o, "subdir");
188
        }
189
        else {
190
            path = unmarshallString(o, "name");
191
            lastModified = unmarshallDate(o, "last_modified");
192
        }
193
        if (path.endsWith("/"))
194
            path = path.substring(0, path.length() - 1);
195
        if (path.contains("/"))
196
            name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
197
        else
198
            name = path;
199
        if (aContainer != null) {
200
            container = aContainer;
201
            prefix = path;
202
        }
203
        else {
204
            container = name;
205
            prefix = "";
206
        }
207
        if (o.containsKey("x_object_meta_trash") && o.get("x_object_meta_trash").isString().stringValue().equals("true"))
208
            inTrash = true;
209
    }
210

    
211
    public static Folder createFromResponse(Response response, Folder result) {
212
        Folder f = null;
213
        if (result == null)
214
            f = new Folder();
215
        else
216
            f = result;
217

    
218
        f.populate(response);
219
        return f;
220
    }
221

    
222
    @Override
223
    public boolean equals(Object other) {
224
        if (other instanceof Folder) {
225
            Folder o = (Folder) other;
226
            return (container + prefix).equals(o.getContainer() + o.getPrefix());
227
        }
228
        return false;
229
    }
230

    
231
    @Override
232
    public int hashCode() {
233
        return (container + prefix).hashCode();
234
    }
235

    
236
    public Set<File> getFiles() {
237
        return files;
238
    }
239

    
240
    public Folder getParent() {
241
        return parent;
242
    }
243

    
244
    public String getUri() {
245
        return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix);
246
    }
247

    
248
    public boolean isInTrash() {
249
        return inTrash;
250
    }
251

    
252
    public boolean isContainer() {
253
        return parent == null;
254
    }
255

    
256
    public boolean isTrash() {
257
        return trash;
258
    }
259

    
260
    public void setTrash(boolean trash) {
261
        this.trash = trash;
262
    }
263

    
264
    public void setContainer(String container) {
265
        this.container = container;
266
    }
267

    
268
    public Set<String> getTags() {
269
        return tags;
270
    }
271
}