Statistics
| Branch: | Tag: | Revision:

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

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
        JSONValue json = JSONParser.parseStrict(response.getText());
131
        JSONArray array = json.isArray();
132
        if (array != null) {
133
            for (int i=0; i<array.size(); i++) {
134
                JSONObject o = array.get(i).isObject();
135
                if (o != null) {
136
                    String contentType = unmarshallString(o, "content_type");
137
                    if (o.containsKey("subdir") || (contentType != null && (contentType.startsWith("application/directory") || contentType.startsWith("application/folder")))) {
138
                        Folder f = new Folder();
139
                        f.populate(this, o, container);
140
                        subfolders.add(f);
141
                    }
142
                    else {
143
                        File file = new File();
144
                        file.populate(this, o, container);
145
                        files.add(file);
146
                    }
147
                }
148
            }
149
        }
150
    }
151

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

    
178
    @Override
179
    public String getLastModifiedSince() {
180
        return null;  //To change body of implemented methods use File | Settings | File Templates.
181
    }
182

    
183
    public static Folder createFromResponse(Response response, Folder result) {
184
        Folder f = null;
185
        if (result == null)
186
            f = new Folder();
187
        else
188
            f = result;
189

    
190
        f.populate(response);
191
        return f;
192
    }
193

    
194
    @Override
195
    public boolean equals(Object other) {
196
        if (other instanceof Folder) {
197
            Folder o = (Folder) other;
198
            return (container + prefix).equals(o.getContainer() + o.getPrefix());
199
        }
200
        return false;
201
    }
202

    
203
    @Override
204
    public int hashCode() {
205
        return (container + prefix).hashCode();
206
    }
207

    
208
    public Set<File> getFiles() {
209
        return files;
210
    }
211

    
212
    public Folder getParent() {
213
        return parent;
214
    }
215
}