Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (9.8 kB)

1 6084aa02 Christos Stathis
/*
2 cae2a8db Christos Stathis
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
3 63366925 Christos Stathis
 *
4 63366925 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 63366925 Christos Stathis
 * without modification, are permitted provided that the following
6 63366925 Christos Stathis
 * conditions are met:
7 63366925 Christos Stathis
 *
8 63366925 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 63366925 Christos Stathis
 *      disclaimer.
11 63366925 Christos Stathis
 *
12 63366925 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 63366925 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 63366925 Christos Stathis
 *      provided with the distribution.
16 63366925 Christos Stathis
 *
17 63366925 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 63366925 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 63366925 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 63366925 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 63366925 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 63366925 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 63366925 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 63366925 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 63366925 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 63366925 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 63366925 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 63366925 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 63366925 Christos Stathis
 *
30 63366925 Christos Stathis
 * The views and conclusions contained in the software and
31 63366925 Christos Stathis
 * documentation are those of the authors and should not be
32 63366925 Christos Stathis
 * interpreted as representing official policies, either expressed
33 63366925 Christos Stathis
 * or implied, of GRNET S.A.
34 6084aa02 Christos Stathis
 */
35 6084aa02 Christos Stathis
36 6084aa02 Christos Stathis
package gr.grnet.pithos.web.client.foldertree;
37 6084aa02 Christos Stathis
38 7b28ae07 Christos Stathis
import gr.grnet.pithos.web.client.Pithos;
39 7b28ae07 Christos Stathis
40 7811b9d1 Christos Stathis
import java.util.Date;
41 7811b9d1 Christos Stathis
import java.util.HashMap;
42 7811b9d1 Christos Stathis
import java.util.LinkedHashSet;
43 7811b9d1 Christos Stathis
import java.util.Map;
44 7811b9d1 Christos Stathis
import java.util.Set;
45 7811b9d1 Christos Stathis
46 f3c8bd5b Christos Stathis
import com.google.gwt.core.client.GWT;
47 8e61880b Christos Stathis
import com.google.gwt.http.client.Response;
48 ea94470a Christos Stathis
import com.google.gwt.http.client.URL;
49 8e61880b Christos Stathis
import com.google.gwt.i18n.client.DateTimeFormat;
50 8e61880b Christos Stathis
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
51 8e61880b Christos Stathis
import com.google.gwt.json.client.JSONArray;
52 8e61880b Christos Stathis
import com.google.gwt.json.client.JSONObject;
53 8e61880b Christos Stathis
import com.google.gwt.json.client.JSONParser;
54 8e61880b Christos Stathis
import com.google.gwt.json.client.JSONValue;
55 6084aa02 Christos Stathis
56 8e61880b Christos Stathis
public class Folder extends Resource {
57 7c818c14 Christos Stathis
    /*
58 7c818c14 Christos Stathis
     * The name of the folder. If the folder is a container this is its name. If it is a virtual folder this is the
59 7c818c14 Christos Stathis
     * last part of its path
60 7c818c14 Christos Stathis
     */
61 8e61880b Christos Stathis
    private String name = null;
62 6084aa02 Christos Stathis
63 8e61880b Christos Stathis
    private Date lastModified = null;
64 6084aa02 Christos Stathis
65 8e61880b Christos Stathis
    private long bytesUsed = 0;
66 8e61880b Christos Stathis
67 74ebb3a3 Christos Stathis
    private Folder parent = null;
68 74ebb3a3 Christos Stathis
    
69 8e61880b Christos Stathis
    private Set<Folder> subfolders = new LinkedHashSet<Folder>();
70 7c818c14 Christos Stathis
    /*
71 7c818c14 Christos Stathis
     * The name of the container that this folder belongs to. If this folder is container, this field equals name
72 7c818c14 Christos Stathis
     */
73 8e61880b Christos Stathis
    private String container = null;
74 8e61880b Christos Stathis
75 7c818c14 Christos Stathis
    /*
76 7c818c14 Christos Stathis
     * This is the full path of the folder (prefix is a misnomer but it was named so because this is used as a prefix=
77 7c818c14 Christos Stathis
     * parameter in the request that fetches its children). If the folder is a cointainer this is empty string
78 7c818c14 Christos Stathis
     */
79 8e61880b Christos Stathis
    private String prefix = "";
80 8e61880b Christos Stathis
81 875a0179 Christos Stathis
    private Set<File> files = new LinkedHashSet<File>();
82 875a0179 Christos Stathis
83 b51c628b Christos Stathis
    private String owner;
84 b51c628b Christos Stathis
85 d5c6298f Christos Stathis
    private Map<String, Boolean[]> permissions = new HashMap<String, Boolean[]>();
86 d5c6298f Christos Stathis
87 d5c6298f Christos Stathis
    private String inheritedPermissionsFrom;
88 d5c6298f Christos Stathis
89 8e61880b Christos Stathis
    public Folder() {};
90 6084aa02 Christos Stathis
91 6084aa02 Christos Stathis
    public Folder(String name) {
92 6084aa02 Christos Stathis
        this.name = name;
93 6084aa02 Christos Stathis
    }
94 8e61880b Christos Stathis
    
95 8e61880b Christos Stathis
    public String getName() {
96 8e61880b Christos Stathis
        return name;
97 8e61880b Christos Stathis
    }
98 6084aa02 Christos Stathis
99 f5023f13 Christos Stathis
    @Override
100 f5023f13 Christos Stathis
        public Date getLastModified() {
101 8e61880b Christos Stathis
        return lastModified;
102 6084aa02 Christos Stathis
    }
103 6084aa02 Christos Stathis
104 8e61880b Christos Stathis
    public long getBytesUsed() {
105 8e61880b Christos Stathis
        return bytesUsed;
106 6084aa02 Christos Stathis
    }
107 6084aa02 Christos Stathis
108 8e61880b Christos Stathis
    public Set<Folder> getSubfolders() {
109 6084aa02 Christos Stathis
        return subfolders;
110 6084aa02 Christos Stathis
    }
111 6084aa02 Christos Stathis
112 8e61880b Christos Stathis
    public void setSubfolders(Set<Folder> subfolders) {
113 6084aa02 Christos Stathis
        this.subfolders = subfolders;
114 6084aa02 Christos Stathis
    }
115 8e61880b Christos Stathis
116 8e61880b Christos Stathis
    public String getContainer() {
117 8e61880b Christos Stathis
        return container;
118 8e61880b Christos Stathis
    }
119 8e61880b Christos Stathis
120 8e61880b Christos Stathis
    public String getPrefix() {
121 8e61880b Christos Stathis
        return prefix;
122 8e61880b Christos Stathis
    }
123 8e61880b Christos Stathis
124 d5c6298f Christos Stathis
    private void parsePermissions(String rawPermissions) {
125 d5c6298f Christos Stathis
        String[] readwrite = rawPermissions.split(";");
126 d5c6298f Christos Stathis
        for (String s : readwrite) {
127 d5c6298f Christos Stathis
            String[] part = s.split("=");
128 d5c6298f Christos Stathis
            String perm = part[0].trim();
129 d5c6298f Christos Stathis
            String[] users = part[1].split(",");
130 d5c6298f Christos Stathis
            for (String u : users) {
131 d5c6298f Christos Stathis
                String user = u.trim();
132 d5c6298f Christos Stathis
                Boolean[] userPerm = permissions.get(u);
133 d5c6298f Christos Stathis
                if (userPerm == null) {
134 d5c6298f Christos Stathis
                    userPerm = new Boolean[2];
135 d5c6298f Christos Stathis
                    permissions.put(user, userPerm);
136 d5c6298f Christos Stathis
                }
137 d5c6298f Christos Stathis
                if (perm.equals("read")) {
138 d5c6298f Christos Stathis
                    userPerm[0] = Boolean.TRUE;
139 d5c6298f Christos Stathis
                }
140 d5c6298f Christos Stathis
                else if (perm.equals("write")) {
141 d5c6298f Christos Stathis
                    userPerm[1] = Boolean.TRUE;
142 d5c6298f Christos Stathis
                }
143 d5c6298f Christos Stathis
            }
144 d5c6298f Christos Stathis
        }
145 d5c6298f Christos Stathis
    }
146 d5c6298f Christos Stathis
147 7811b9d1 Christos Stathis
    public void populate(String _owner, Response response) {
148 7811b9d1 Christos Stathis
        this.owner = _owner;
149 8e61880b Christos Stathis
        String header = response.getHeader("Last-Modified");
150 8e61880b Christos Stathis
        if (header != null)
151 f3c8bd5b Christos Stathis
                        try {
152 f3c8bd5b Christos Stathis
                                lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
153 f3c8bd5b Christos Stathis
                        } catch (IllegalArgumentException e) {
154 f3c8bd5b Christos Stathis
                                GWT.log("Last-Modified will be set to null", e);
155 f3c8bd5b Christos Stathis
                                lastModified = null;
156 f3c8bd5b Christos Stathis
                        }
157 8e61880b Christos Stathis
158 8e61880b Christos Stathis
        header = response.getHeader("X-Container-Bytes-Used");
159 e2403069 Christos Stathis
        if (header != null && header.length() > 0)
160 8e61880b Christos Stathis
            bytesUsed = Long.valueOf(header);
161 8e61880b Christos Stathis
162 b3832e45 Christos Stathis
        String rawPermissions = response.getHeader("X-Object-Sharing");
163 b3832e45 Christos Stathis
        if (rawPermissions != null && rawPermissions.length() > 0) {
164 b3832e45 Christos Stathis
            parsePermissions(URL.decodePathSegment(rawPermissions));
165 b3832e45 Christos Stathis
        }
166 b3832e45 Christos Stathis
        
167 b3832e45 Christos Stathis
        if (response.getText() == null || response.getText().isEmpty())
168 b3832e45 Christos Stathis
                return;
169 8e61880b Christos Stathis
        JSONValue json = JSONParser.parseStrict(response.getText());
170 8e61880b Christos Stathis
        JSONArray array = json.isArray();
171 8e61880b Christos Stathis
        if (array != null) {
172 b3832e45 Christos Stathis
            subfolders.clear(); //This is necessary in case we update a pre-existing Folder so that stale subfolders won't show up
173 b3832e45 Christos Stathis
            files.clear();
174 8e61880b Christos Stathis
            for (int i=0; i<array.size(); i++) {
175 8e61880b Christos Stathis
                JSONObject o = array.get(i).isObject();
176 8e61880b Christos Stathis
                if (o != null) {
177 7c818c14 Christos Stathis
                    String contentType = unmarshallString(o, "content_type");
178 07a1b5fe Christos Stathis
                    if (o.containsKey("subdir") || (contentType != null && (contentType.startsWith("application/directory") || contentType.startsWith("application/folder")))) {
179 8e61880b Christos Stathis
                        Folder f = new Folder();
180 7811b9d1 Christos Stathis
                        f.populate(this, o, _owner, container);
181 5aaf7821 Christos Stathis
                        if (f.getName().length() > 0)
182 5aaf7821 Christos Stathis
                                subfolders.add(f);
183 8e61880b Christos Stathis
                    }
184 7b28ae07 Christos Stathis
                    else {
185 875a0179 Christos Stathis
                        File file = new File();
186 7811b9d1 Christos Stathis
                        file.populate(this, o, _owner, container);
187 5aaf7821 Christos Stathis
                        if (file.getName().length() > 0)
188 5aaf7821 Christos Stathis
                                files.add(file);
189 8e61880b Christos Stathis
                    }
190 8e61880b Christos Stathis
                }
191 8e61880b Christos Stathis
            }
192 8e61880b Christos Stathis
        }
193 8e61880b Christos Stathis
    }
194 8e61880b Christos Stathis
195 7811b9d1 Christos Stathis
    public void populate(Folder _parent, JSONObject o, String _owner, String aContainer) {
196 7811b9d1 Christos Stathis
        this.parent = _parent;
197 7c818c14 Christos Stathis
        String path = null;
198 8e61880b Christos Stathis
        if (o.containsKey("subdir")) {
199 7c818c14 Christos Stathis
            path = unmarshallString(o, "subdir");
200 5aaf7821 Christos Stathis
            if (path.endsWith("/")) { //Always true for "subdir"
201 5aaf7821 Christos Stathis
                path = path.substring(0, path.length() - 1);
202 5aaf7821 Christos Stathis
            }
203 5aaf7821 Christos Stathis
            if (parent != null && parent.getPrefix().length() > 0)
204 5aaf7821 Christos Stathis
                    name = path.substring(parent.getPrefix().length() + 1);
205 5aaf7821 Christos Stathis
            else
206 5aaf7821 Christos Stathis
                    name = path;
207 5aaf7821 Christos Stathis
            if (name.equals("/"))
208 5aaf7821 Christos Stathis
                    name = "";
209 8e61880b Christos Stathis
        }
210 8e61880b Christos Stathis
        else {
211 7c818c14 Christos Stathis
            path = unmarshallString(o, "name");
212 8e61880b Christos Stathis
            lastModified = unmarshallDate(o, "last_modified");
213 5aaf7821 Christos Stathis
            if (parent != null && parent.getPrefix().length() > 0)
214 5aaf7821 Christos Stathis
                    name = path.substring(parent.getPrefix().length() + 1);
215 5aaf7821 Christos Stathis
            else
216 5aaf7821 Christos Stathis
                    name = path;
217 8e61880b Christos Stathis
        }
218 7c818c14 Christos Stathis
        if (aContainer != null) {
219 7c818c14 Christos Stathis
            container = aContainer;
220 7c818c14 Christos Stathis
            prefix = path;
221 7c818c14 Christos Stathis
        }
222 7c818c14 Christos Stathis
        else {
223 7c818c14 Christos Stathis
            container = name;
224 7c818c14 Christos Stathis
            prefix = "";
225 7c818c14 Christos Stathis
        }
226 7811b9d1 Christos Stathis
        this.owner = _owner;
227 d5c6298f Christos Stathis
228 d5c6298f Christos Stathis
        inheritedPermissionsFrom = unmarshallString(o, "x_object_shared_by");
229 d5c6298f Christos Stathis
        String rawPermissions = unmarshallString(o, "x_object_sharing");
230 d5c6298f Christos Stathis
        if (rawPermissions != null)
231 d5c6298f Christos Stathis
            parsePermissions(rawPermissions);
232 8e61880b Christos Stathis
    }
233 8e61880b Christos Stathis
234 b51c628b Christos Stathis
    public static Folder createFromResponse(String owner, Response response, Folder result) {
235 8e61880b Christos Stathis
        Folder f = null;
236 8e61880b Christos Stathis
        if (result == null)
237 8e61880b Christos Stathis
            f = new Folder();
238 8e61880b Christos Stathis
        else
239 8e61880b Christos Stathis
            f = result;
240 8e61880b Christos Stathis
241 b51c628b Christos Stathis
        f.populate(owner, response);
242 8e61880b Christos Stathis
        return f;
243 8e61880b Christos Stathis
    }
244 8e61880b Christos Stathis
245 8e61880b Christos Stathis
    @Override
246 8e61880b Christos Stathis
    public boolean equals(Object other) {
247 8e61880b Christos Stathis
        if (other instanceof Folder) {
248 8e61880b Christos Stathis
            Folder o = (Folder) other;
249 5ebc9312 Christos Stathis
            return getUri().equals(o.getUri());
250 8e61880b Christos Stathis
        }
251 8e61880b Christos Stathis
        return false;
252 8e61880b Christos Stathis
    }
253 8e61880b Christos Stathis
254 8e61880b Christos Stathis
    @Override
255 8e61880b Christos Stathis
    public int hashCode() {
256 5ebc9312 Christos Stathis
        return getUri().hashCode();
257 8e61880b Christos Stathis
    }
258 875a0179 Christos Stathis
259 875a0179 Christos Stathis
    public Set<File> getFiles() {
260 875a0179 Christos Stathis
        return files;
261 875a0179 Christos Stathis
    }
262 74ebb3a3 Christos Stathis
263 74ebb3a3 Christos Stathis
    public Folder getParent() {
264 74ebb3a3 Christos Stathis
        return parent;
265 74ebb3a3 Christos Stathis
    }
266 bdda6b2f Christos Stathis
267 bdda6b2f Christos Stathis
    public String getUri() {
268 bdda6b2f Christos Stathis
        return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix);
269 bdda6b2f Christos Stathis
    }
270 2637c9cd Christos Stathis
271 390f5c04 Christos Stathis
    public boolean isContainer() {
272 390f5c04 Christos Stathis
        return parent == null;
273 390f5c04 Christos Stathis
    }
274 05cf9e5b Christos Stathis
275 05cf9e5b Christos Stathis
    public void setContainer(String container) {
276 05cf9e5b Christos Stathis
        this.container = container;
277 05cf9e5b Christos Stathis
    }
278 0d1e8310 Christos Stathis
279 ecf95c9e Christos Stathis
    public String getInheritedPermissionsFrom() {
280 ecf95c9e Christos Stathis
        return inheritedPermissionsFrom;
281 ecf95c9e Christos Stathis
    }
282 ecf95c9e Christos Stathis
283 ecf95c9e Christos Stathis
    public Map<String, Boolean[]> getPermissions() {
284 ecf95c9e Christos Stathis
        return permissions;
285 ecf95c9e Christos Stathis
    }
286 ecf95c9e Christos Stathis
287 ecf95c9e Christos Stathis
    public String getOwner() {
288 ecf95c9e Christos Stathis
        return owner;
289 ecf95c9e Christos Stathis
    }
290 ecf95c9e Christos Stathis
291 ff06bcd5 Christos Stathis
        public boolean isShared() {
292 ff06bcd5 Christos Stathis
                return !permissions.isEmpty();
293 ff06bcd5 Christos Stathis
        }
294 7b28ae07 Christos Stathis
295 31477486 Christos Stathis
        /**
296 31477486 Christos Stathis
         * I am THE trash
297 31477486 Christos Stathis
         * 
298 31477486 Christos Stathis
         * @return
299 31477486 Christos Stathis
         */
300 7b28ae07 Christos Stathis
        public boolean isTrash() {
301 7b28ae07 Christos Stathis
                return isContainer() && name.equals(Pithos.TRASH_CONTAINER);
302 7b28ae07 Christos Stathis
        }
303 31477486 Christos Stathis
        
304 31477486 Christos Stathis
        /**
305 31477486 Christos Stathis
         * I am IN THE trash
306 31477486 Christos Stathis
         * 
307 31477486 Christos Stathis
         * @return
308 31477486 Christos Stathis
         */
309 31477486 Christos Stathis
        public boolean isInTrash() {
310 31477486 Christos Stathis
                return container.equals(Pithos.TRASH_CONTAINER);
311 31477486 Christos Stathis
        }
312 7b28ae07 Christos Stathis
313 7b28ae07 Christos Stathis
        public boolean isHome() {
314 7b28ae07 Christos Stathis
                return isContainer() && name.equals(Pithos.HOME_CONTAINER);
315 7b28ae07 Christos Stathis
        }
316 8b2b67ba Christos Stathis
317 8b2b67ba Christos Stathis
        public boolean contains(Folder folder) {
318 8b2b67ba Christos Stathis
                for (Folder f : subfolders)
319 8b2b67ba Christos Stathis
                        if (f.equals(folder) || f.contains(folder))
320 8b2b67ba Christos Stathis
                                return true;
321 8b2b67ba Christos Stathis
                return false;
322 8b2b67ba Christos Stathis
        }
323 6084aa02 Christos Stathis
}