Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.8 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.LinkedHashSet;
48
import java.util.List;
49
import java.util.Set;
50

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

    
58
    private Date lastModified = null;
59

    
60
    private long bytesUsed = 0;
61

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

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

    
76
    private Set<File> files = new LinkedHashSet<File>();
77

    
78
    public Folder() {};
79

    
80
    public Folder(String name) {
81
        this.name = name;
82
    }
83
    
84
    public String getName() {
85
        return name;
86
    }
87

    
88
    public Date getLastModified() {
89
        return lastModified;
90
    }
91

    
92
    public long getBytesUsed() {
93
        return bytesUsed;
94
    }
95

    
96
    public void setLastModified(Date lastModified) {
97
        this.lastModified = lastModified;
98
    }
99

    
100
    public Set<Folder> getSubfolders() {
101
        return subfolders;
102
    }
103

    
104
    public void setSubfolders(Set<Folder> subfolders) {
105
        this.subfolders = subfolders;
106
    }
107

    
108
    public String getContainer() {
109
        return container;
110
    }
111

    
112
    public String getPrefix() {
113
        return prefix;
114
    }
115

    
116
    public void setPrefix(String prefix) {
117
        this.prefix = prefix;
118
    }
119

    
120
    public void populate(Response response) {
121
        String header = response.getHeader("Last-Modified");
122
        if (header != null)
123
            lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
124

    
125
        header = response.getHeader("X-Container-Bytes-Used");
126
        if (header != null)
127
            bytesUsed = Long.valueOf(header);
128

    
129
        subfolders.clear(); //This is necessary in case we update a pre-existing Folder so that stale subfolders won't show up
130
        files.clear();
131
        JSONValue json = JSONParser.parseStrict(response.getText());
132
        JSONArray array = json.isArray();
133
        if (array != null) {
134
            for (int i=0; i<array.size(); i++) {
135
                JSONObject o = array.get(i).isObject();
136
                if (o != null) {
137
                    String contentType = unmarshallString(o, "content_type");
138
                    if (o.containsKey("subdir") || (contentType != null && (contentType.startsWith("application/directory") || contentType.startsWith("application/folder")))) {
139
                        Folder f = new Folder();
140
                        f.populate(this, o, container);
141
                        subfolders.add(f);
142
                    }
143
                    else {
144
                        File file = new File();
145
                        file.populate(this, o, container);
146
                        files.add(file);
147
                    }
148
                }
149
            }
150
        }
151
    }
152

    
153
    public void populate(Folder parent, JSONObject o, String aContainer) {
154
        this.parent = parent;
155
        String path = null;
156
        if (o.containsKey("subdir")) {
157
            path = unmarshallString(o, "subdir");
158
        }
159
        else {
160
            path = unmarshallString(o, "name");
161
            lastModified = unmarshallDate(o, "last_modified");
162
        }
163
        if (path.endsWith("/"))
164
            path = path.substring(0, path.length() - 1);
165
        if (path.contains("/"))
166
            name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
167
        else
168
            name = path;
169
        if (aContainer != null) {
170
            container = aContainer;
171
            prefix = path;
172
        }
173
        else {
174
            container = name;
175
            prefix = "";
176
        }
177
    }
178

    
179
    public static Folder createFromResponse(Response response, Folder result) {
180
        Folder f = null;
181
        if (result == null)
182
            f = new Folder();
183
        else
184
            f = result;
185

    
186
        f.populate(response);
187
        return f;
188
    }
189

    
190
    @Override
191
    public boolean equals(Object other) {
192
        if (other instanceof Folder) {
193
            Folder o = (Folder) other;
194
            return (container + prefix).equals(o.getContainer() + o.getPrefix());
195
        }
196
        return false;
197
    }
198

    
199
    @Override
200
    public int hashCode() {
201
        return (container + prefix).hashCode();
202
    }
203

    
204
    public Set<File> getFiles() {
205
        return files;
206
    }
207

    
208
    public Folder getParent() {
209
        return parent;
210
    }
211

    
212
    public String getUri() {
213
        return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix);
214
    }
215
}