Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (10.8 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.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.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.NumberFormat;
51
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
52
import com.google.gwt.json.client.JSONArray;
53
import com.google.gwt.json.client.JSONObject;
54
import com.google.gwt.json.client.JSONParser;
55
import com.google.gwt.json.client.JSONValue;
56

    
57
public class Folder extends FileFolderResource {
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 owner;
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
    @Override
97
        public String getName() {
98
        return name;
99
    }
100

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

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

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

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

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

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

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

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

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

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

    
197
    public void populate(Folder _parent, JSONObject o, String _owner, String aContainer) {
198
        this.parent = _parent;
199
        String path = null;
200
        if (o.containsKey("subdir")) {
201
            path = unmarshallString(o, "subdir");
202
            if (path.endsWith("/")) { //Always true for "subdir"
203
                path = path.substring(0, path.length() - 1);
204
            }
205
            if (parent != null && parent.getPrefix().length() > 0)
206
                    name = path.substring(parent.getPrefix().length() + 1);
207
            else
208
                    name = path;
209
            if (name.equals("/"))
210
                    name = "";
211
        }
212
        else {
213
            path = unmarshallString(o, "name");
214
            lastModified = unmarshallDate(o, "last_modified");
215
            if (parent != null && parent.getPrefix().length() > 0)
216
                    name = path.substring(parent.getPrefix().length() + 1);
217
            else
218
                    name = path;
219
        }
220
        if (aContainer != null) {
221
            container = aContainer;
222
            prefix = path;
223
        }
224
        else {
225
            container = name;
226
            prefix = "";
227
        }
228
        this.owner = _owner;
229

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

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

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

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

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

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

    
266
    @Override
267
        public Folder getParent() {
268
        return parent;
269
    }
270

    
271
    @Override
272
        public String getUri() {
273
        return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix);
274
    }
275

    
276
    public boolean isContainer() {
277
        return parent == null;
278
    }
279

    
280
    public void setContainer(String container) {
281
        this.container = container;
282
    }
283

    
284
    public String getInheritedPermissionsFrom() {
285
        return inheritedPermissionsFrom;
286
    }
287

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

    
292
    @Override
293
        public String getOwner() {
294
        return owner;
295
    }
296

    
297
        public boolean isShared() {
298
                return !permissions.isEmpty();
299
        }
300

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

    
319
        public boolean isHome() {
320
                return isContainer() && name.equals(Pithos.HOME_CONTAINER);
321
        }
322

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

    
330
        @Override
331
        public String getContentType() {
332
                return "application/folder";
333
        }
334

    
335
        @Override
336
        public String getPath() {
337
                return "";
338
        }
339

    
340
        @Override
341
        public String getSizeAsString() {
342
                long bytes = calculateContentsSize();
343
        NumberFormat nf = NumberFormat.getFormat("######.#");
344
        if (bytes < 1024)
345
            return String.valueOf(bytes) + " B";
346
        else if (bytes < 1024 * 1024)
347
            return nf.format(Double.valueOf(bytes)/(1024)) + " KB";
348
        else if (bytes < 1024 * 1024 * 1024)
349
            return nf.format(Double.valueOf(bytes)/(1024 * 1024)) + " MB";
350
        return nf.format(Double.valueOf(bytes)/(1024 * 1024 * 1024)) + " GB";
351
        }
352

    
353
        private long calculateContentsSize() {
354
                long total = 0;
355
                for (File f : files)
356
                        total += f.getBytes();
357
                return total;
358
        }
359

    
360
        @Override
361
        public long getBytes() {
362
                return 0;
363
        }
364
}