Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (9.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 gr.grnet.pithos.web.client.Pithos;
39

    
40
import java.util.Date;
41
import java.util.HashMap;
42
import java.util.LinkedHashSet;
43
import java.util.Map;
44
import java.util.Set;
45

    
46
import com.google.gwt.http.client.Response;
47
import com.google.gwt.i18n.client.DateTimeFormat;
48
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
49
import com.google.gwt.json.client.JSONArray;
50
import com.google.gwt.json.client.JSONObject;
51
import com.google.gwt.json.client.JSONParser;
52
import com.google.gwt.json.client.JSONValue;
53

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

    
61
    private Date lastModified = null;
62

    
63
    private long bytesUsed = 0;
64

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

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

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

    
81
    private Set<String> tags = new LinkedHashSet<String>();
82

    
83
    private String owner;
84

    
85
    private Map<String, Boolean[]> permissions = new HashMap<String, Boolean[]>();
86

    
87
    private String inheritedPermissionsFrom;
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
    @Override
100
        public Date getLastModified() {
101
        return lastModified;
102
    }
103

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

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

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

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

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

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

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

    
132
    private void parsePermissions(String rawPermissions) {
133
        String[] readwrite = rawPermissions.split(";");
134
        for (String s : readwrite) {
135
            String[] part = s.split("=");
136
            String perm = part[0].trim();
137
            String[] users = part[1].split(",");
138
            for (String u : users) {
139
                String user = u.trim();
140
                Boolean[] userPerm = permissions.get(u);
141
                if (userPerm == null) {
142
                    userPerm = new Boolean[2];
143
                    permissions.put(user, userPerm);
144
                }
145
                if (perm.equals("read")) {
146
                    userPerm[0] = Boolean.TRUE;
147
                }
148
                else if (perm.equals("write")) {
149
                    userPerm[1] = Boolean.TRUE;
150
                }
151
            }
152
        }
153
    }
154

    
155
    public void populate(String _owner, Response response) {
156
        this.owner = _owner;
157
        String header = response.getHeader("Last-Modified");
158
        if (header != null)
159
            lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
160

    
161
        header = response.getHeader("X-Container-Bytes-Used");
162
        if (header != null)
163
            bytesUsed = Long.valueOf(header);
164

    
165
        header = response.getHeader("X-Container-Object-Meta");
166
        if (header != null && header.length() > 0) {
167
            for (String t : header.split(",")) {
168
                tags.add(t.toLowerCase().trim());
169
            }
170
        }
171

    
172
        subfolders.clear(); //This is necessary in case we update a pre-existing Folder so that stale subfolders won't show up
173
        files.clear();
174
        JSONValue json = JSONParser.parseStrict(response.getText());
175
        JSONArray array = json.isArray();
176
        if (array != null) {
177
            for (int i=0; i<array.size(); i++) {
178
                JSONObject o = array.get(i).isObject();
179
                if (o != null) {
180
                    String contentType = unmarshallString(o, "content_type");
181
                    if (o.containsKey("subdir") || (contentType != null && (contentType.startsWith("application/directory") || contentType.startsWith("application/folder")))) {
182
                        Folder f = new Folder();
183
                        f.populate(this, o, _owner, container);
184
                        subfolders.add(f);
185
                    }
186
                    else {
187
                        File file = new File();
188
                        file.populate(this, o, _owner, container);
189
                        files.add(file);
190
                    }
191
                }
192
            }
193
        }
194
    }
195

    
196
    public void populate(Folder _parent, JSONObject o, String _owner, String aContainer) {
197
        this.parent = _parent;
198
        String path = null;
199
        if (o.containsKey("subdir")) {
200
            path = unmarshallString(o, "subdir");
201
        }
202
        else {
203
            path = unmarshallString(o, "name");
204
            lastModified = unmarshallDate(o, "last_modified");
205
        }
206
        if (path.endsWith("/"))
207
            path = path.substring(0, path.length() - 1);
208
        if (path.contains("/"))
209
            name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
210
        else
211
            name = path;
212
        if (aContainer != null) {
213
            container = aContainer;
214
            prefix = path;
215
        }
216
        else {
217
            container = name;
218
            prefix = "";
219
        }
220
        this.owner = _owner;
221

    
222
        inheritedPermissionsFrom = unmarshallString(o, "x_object_shared_by");
223
        String rawPermissions = unmarshallString(o, "x_object_sharing");
224
        if (rawPermissions != null)
225
            parsePermissions(rawPermissions);
226
    }
227

    
228
    public static Folder createFromResponse(String owner, Response response, Folder result) {
229
        Folder f = null;
230
        if (result == null)
231
            f = new Folder();
232
        else
233
            f = result;
234

    
235
        f.populate(owner, response);
236
        return f;
237
    }
238

    
239
    @Override
240
    public boolean equals(Object other) {
241
        if (other instanceof Folder) {
242
            Folder o = (Folder) other;
243
            return getUri().equals(o.getUri());
244
        }
245
        return false;
246
    }
247

    
248
    @Override
249
    public int hashCode() {
250
        return getUri().hashCode();
251
    }
252

    
253
    public Set<File> getFiles() {
254
        return files;
255
    }
256

    
257
    public Folder getParent() {
258
        return parent;
259
    }
260

    
261
    public String getUri() {
262
        return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix);
263
    }
264

    
265
    public boolean isContainer() {
266
        return parent == null;
267
    }
268

    
269
    public void setContainer(String container) {
270
        this.container = container;
271
    }
272

    
273
    public Set<String> getTags() {
274
        return tags;
275
    }
276

    
277
    public String getInheritedPermissionsFrom() {
278
        return inheritedPermissionsFrom;
279
    }
280

    
281
    public Map<String, Boolean[]> getPermissions() {
282
        return permissions;
283
    }
284

    
285
    public String getOwner() {
286
        return owner;
287
    }
288

    
289
    public boolean existChildrenPermissions() {
290
        for (File f : files)
291
            if (!f.getPermissions().isEmpty() && f.getInheritedPermissionsFrom() == null)
292
                return true;
293

    
294
        for (Folder fo : subfolders)
295
            if ((!fo.getPermissions().isEmpty() && fo.getInheritedPermissionsFrom() == null) || fo.existChildrenPermissions())
296
                return true;
297
        return false;
298
    }
299

    
300
        public boolean isShared() {
301
                return !permissions.isEmpty();
302
        }
303

    
304
        /**
305
         * I am THE trash
306
         * 
307
         * @return
308
         */
309
        public boolean isTrash() {
310
                return isContainer() && name.equals(Pithos.TRASH_CONTAINER);
311
        }
312
        
313
        /**
314
         * I am IN THE trash
315
         * 
316
         * @return
317
         */
318
        public boolean isInTrash() {
319
                return container.equals(Pithos.TRASH_CONTAINER);
320
        }
321

    
322
        public boolean isHome() {
323
                return isContainer() && name.equals(Pithos.HOME_CONTAINER);
324
        }
325

    
326
        public boolean contains(Folder folder) {
327
                for (Folder f : subfolders)
328
                        if (f.equals(folder) || f.contains(folder))
329
                                return true;
330
                return false;
331
        }
332
}