Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (10 kB)

1
/*
2
 * Copyright 2011-2012 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.Const;
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.core.client.GWT;
47
import com.google.gwt.http.client.Response;
48
import com.google.gwt.http.client.URL;
49
import com.google.gwt.i18n.client.DateTimeFormat;
50
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
51
import com.google.gwt.json.client.JSONArray;
52
import com.google.gwt.json.client.JSONObject;
53
import com.google.gwt.json.client.JSONParser;
54
import com.google.gwt.json.client.JSONValue;
55
import gr.grnet.pithos.web.client.Resource;
56

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

    
64
    private Date lastModified = null;
65

    
66
    private long bytesUsed = 0;
67

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

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

    
82
    private Set<File> files = new LinkedHashSet<File>();
83

    
84
    private String ownerID;
85

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

    
88
    private String inheritedPermissionsFrom;
89

    
90
    public Folder() {};
91

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

    
100
    @Override
101
        public Date getLastModified() {
102
        return lastModified;
103
    }
104

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

    
109
    public Set<Folder> getSubfolders() {
110
        return subfolders;
111
    }
112

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

    
117
    public String getContainer() {
118
        return container;
119
    }
120

    
121
    public String getPrefix() {
122
        return prefix;
123
    }
124

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

    
148
    public void populate(String _owner, Response response) {
149
        this.ownerID = _owner;
150
        String header = response.getHeader("Last-Modified");
151
        if (header != null)
152
                        try {
153
                                lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
154
                        } catch (IllegalArgumentException e) {
155
                                GWT.log("Last-Modified will be set to null", e);
156
                                lastModified = null;
157
                        }
158

    
159
        header = response.getHeader("X-Container-Bytes-Used");
160
        if (header != null && header.length() > 0)
161
            bytesUsed = Long.valueOf(header);
162

    
163
        String rawPermissions = response.getHeader("X-Object-Sharing");
164
        if (rawPermissions != null && rawPermissions.length() > 0) {
165
            parsePermissions(URL.decodePathSegment(rawPermissions));
166
        }
167
        
168
        if (response.getText() == null || response.getText().isEmpty())
169
                return;
170
        JSONValue json = JSONParser.parseStrict(response.getText());
171
        JSONArray array = json.isArray();
172
        if (array != null) {
173
            subfolders.clear(); //This is necessary in case we update a pre-existing Folder so that stale subfolders won't show up
174
            files.clear();
175
            for (int i=0; i<array.size(); i++) {
176
                JSONObject o = array.get(i).isObject();
177
                if (o != null) {
178
                    String contentType = unmarshallString(o, "content_type");
179
                    if (o.containsKey("subdir") || (contentType != null && (contentType.startsWith("application/directory") || contentType.startsWith("application/folder")))) {
180
                        Folder f = new Folder();
181
                        f.populate(this, o, _owner, container);
182
                        if (f.getName().length() > 0)
183
                                subfolders.add(f);
184
                    }
185
                    else {
186
                        File file = new File();
187
                        file.populate(this, o, _owner, container);
188
                        if (file.getName().length() > 0)
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
            if (path.endsWith("/")) { //Always true for "subdir"
202
                path = path.substring(0, path.length() - 1);
203
            }
204
            if (parent != null && parent.getPrefix().length() > 0)
205
                    name = path.substring(parent.getPrefix().length() + 1);
206
            else
207
                    name = path;
208
            if (name.equals("/"))
209
                    name = "";
210
        }
211
        else {
212
            path = unmarshallString(o, "name");
213
            lastModified = unmarshallDate(o, "last_modified");
214
            if (parent != null && parent.getPrefix().length() > 0)
215
                    name = path.substring(parent.getPrefix().length() + 1);
216
            else
217
                    name = path;
218
        }
219
        if (aContainer != null) {
220
            container = aContainer;
221
            prefix = path;
222
        }
223
        else {
224
            container = name;
225
            prefix = "";
226
        }
227
        this.ownerID = _owner;
228

    
229
        inheritedPermissionsFrom = unmarshallString(o, "x_object_shared_by");
230
        String rawPermissions = unmarshallString(o, "x_object_sharing");
231
        if (rawPermissions != null)
232
            parsePermissions(rawPermissions);
233
    }
234

    
235
    public static Folder createFromResponse(String owner, Response response, Folder result) {
236
        Folder f = null;
237
        if (result == null)
238
            f = new Folder();
239
        else
240
            f = result;
241

    
242
        f.populate(owner, response);
243
        return f;
244
    }
245

    
246
    @Override
247
    public boolean equals(Object other) {
248
        if (other instanceof Folder) {
249
            Folder o = (Folder) other;
250
            return (ownerID == null ? true : ownerID.equals(o.getOwnerID()))
251
                            && (getUri().equals(o.getUri()));
252
        }
253
        return false;
254
    }
255

    
256
    @Override
257
    public int hashCode() {
258
        return getUri().hashCode();
259
    }
260

    
261
    public Set<File> getFiles() {
262
        return files;
263
    }
264

    
265
    public Folder getParent() {
266
        return parent;
267
    }
268

    
269
    public String getUri() {
270
        return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix);
271
    }
272

    
273
    public boolean isContainer() {
274
        return parent == null;
275
    }
276

    
277
    public void setContainer(String container) {
278
        this.container = container;
279
    }
280

    
281
    public String getInheritedPermissionsFrom() {
282
        return inheritedPermissionsFrom;
283
    }
284

    
285
    public Map<String, Boolean[]> getPermissions() {
286
        return permissions;
287
    }
288

    
289
    public String getOwnerID() {
290
        return ownerID;
291
    }
292

    
293
        public boolean isShared() {
294
                return !permissions.isEmpty();
295
        }
296

    
297
        /**
298
         * I am THE trash
299
         * 
300
         * @return
301
         */
302
        public boolean isTrash() {
303
                return isContainer() && name.equals(Const.TRASH_CONTAINER);
304
        }
305
        
306
        /**
307
         * I am IN THE trash
308
         * 
309
         * @return
310
         */
311
        public boolean isInTrash() {
312
                return container.equals(Const.TRASH_CONTAINER);
313
        }
314

    
315
        public boolean isHome() {
316
                return isContainer() && name.equals(Const.HOME_CONTAINER);
317
        }
318

    
319
        public boolean contains(Folder folder) {
320
                for (Folder f : subfolders)
321
                        if (f.equals(folder) || f.contains(folder))
322
                                return true;
323
                return false;
324
        }
325
}