Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.6 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 Set<Folder> subfolders = new LinkedHashSet<Folder>();
63
    /*
64
     * The name of the container that this folder belongs to. If this folder is container, this field equals name
65
     */
66
    private String container = null;
67

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

    
74
    private Set<File> files = new LinkedHashSet<File>();
75

    
76
    public Folder() {};
77

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

    
86
    public Date getLastModified() {
87
        return lastModified;
88
    }
89

    
90
    public long getBytesUsed() {
91
        return bytesUsed;
92
    }
93

    
94
    public void setLastModified(Date lastModified) {
95
        this.lastModified = lastModified;
96
    }
97

    
98
    public Set<Folder> getSubfolders() {
99
        return subfolders;
100
    }
101

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

    
106
    public String getContainer() {
107
        return container;
108
    }
109

    
110
    public String getPrefix() {
111
        return prefix;
112
    }
113

    
114
    public void setPrefix(String prefix) {
115
        this.prefix = prefix;
116
    }
117

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

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

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

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

    
174
    @Override
175
    public String getLastModifiedSince() {
176
        return null;  //To change body of implemented methods use File | Settings | File Templates.
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
            if (container != null)
195
                return prefix.equals(o.getPrefix()) && container.equals(o.getContainer());
196
            else
197
                return o.getContainer() == null && name.equals(o.getName());
198
        }
199
        return false;
200
    }
201

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

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