Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (10.1 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
        inheritedPermissionsFrom = response.getHeader("X-Object-Shared-By");
173
        String rawPermissions = response.getHeader("X-Object-Sharing");
174
        if (rawPermissions != null)
175
            parsePermissions(rawPermissions);
176

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

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

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

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

    
240
        f.populate(owner, response);
241
        return f;
242
    }
243

    
244
    @Override
245
    public boolean equals(Object other) {
246
        if (other instanceof Folder) {
247
            Folder o = (Folder) other;
248
            return getUri().equals(o.getUri());
249
        }
250
        return false;
251
    }
252

    
253
    @Override
254
    public int hashCode() {
255
        return getUri().hashCode();
256
    }
257

    
258
    public Set<File> getFiles() {
259
        return files;
260
    }
261

    
262
    public Folder getParent() {
263
        return parent;
264
    }
265

    
266
    public String getUri() {
267
        return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix);
268
    }
269

    
270
    public boolean isContainer() {
271
        return parent == null;
272
    }
273

    
274
    public void setContainer(String container) {
275
        this.container = container;
276
    }
277

    
278
    public Set<String> getTags() {
279
        return tags;
280
    }
281

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

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

    
290
    public String getOwner() {
291
        return owner;
292
    }
293

    
294
    public boolean existChildrenPermissions() {
295
        for (File f : files)
296
            if (!f.getPermissions().isEmpty() && f.getInheritedPermissionsFrom() == null)
297
                return true;
298

    
299
        for (Folder fo : subfolders)
300
            if ((!fo.getPermissions().isEmpty() && fo.getInheritedPermissionsFrom() == null) || fo.existChildrenPermissions())
301
                return true;
302
        return false;
303
    }
304

    
305
        public boolean isShared() {
306
                return !permissions.isEmpty();
307
        }
308

    
309
        /**
310
         * I am THE trash
311
         * 
312
         * @return
313
         */
314
        public boolean isTrash() {
315
                return isContainer() && name.equals(Pithos.TRASH_CONTAINER);
316
        }
317
        
318
        /**
319
         * I am IN THE trash
320
         * 
321
         * @return
322
         */
323
        public boolean isInTrash() {
324
                return container.equals(Pithos.TRASH_CONTAINER);
325
        }
326

    
327
        public boolean isHome() {
328
                return isContainer() && name.equals(Pithos.HOME_CONTAINER);
329
        }
330

    
331
        public boolean contains(Folder folder) {
332
                for (Folder f : subfolders)
333
                        if (f.equals(folder) || f.contains(folder))
334
                                return true;
335
                return false;
336
        }
337
}