Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / rest / resource / SharedResource.java @ 58777026

History | View | Annotate | Download (7.2 kB)

1 ab1eb3f8 Christos Stathis
/*
2 58777026 Christos Stathis
 * Copyright 2011 GRNET S.A. All rights reserved.
3 58777026 Christos Stathis
 *
4 58777026 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 58777026 Christos Stathis
 * without modification, are permitted provided that the following
6 58777026 Christos Stathis
 * conditions are met:
7 58777026 Christos Stathis
 *
8 58777026 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 58777026 Christos Stathis
 *      disclaimer.
11 58777026 Christos Stathis
 *
12 58777026 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 58777026 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 58777026 Christos Stathis
 *      provided with the distribution.
16 58777026 Christos Stathis
 *
17 58777026 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 58777026 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 58777026 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 58777026 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 58777026 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 58777026 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 58777026 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 58777026 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 58777026 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 58777026 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 58777026 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 58777026 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 58777026 Christos Stathis
 *
30 58777026 Christos Stathis
 * The views and conclusions contained in the software and
31 58777026 Christos Stathis
 * documentation are those of the authors and should not be
32 58777026 Christos Stathis
 * interpreted as representing official policies, either expressed
33 58777026 Christos Stathis
 * or implied, of GRNET S.A.
34 ab1eb3f8 Christos Stathis
 */
35 ab1eb3f8 Christos Stathis
package gr.grnet.pithos.web.client.rest.resource;
36 ab1eb3f8 Christos Stathis
37 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.MultipleGetCommand;
38 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.MultipleGetCommand.Cached;
39 ab1eb3f8 Christos Stathis
40 ab1eb3f8 Christos Stathis
import java.util.ArrayList;
41 ab1eb3f8 Christos Stathis
import java.util.Date;
42 ab1eb3f8 Christos Stathis
import java.util.LinkedList;
43 ab1eb3f8 Christos Stathis
import java.util.List;
44 ab1eb3f8 Christos Stathis
45 ab1eb3f8 Christos Stathis
import com.google.gwt.core.client.GWT;
46 ab1eb3f8 Christos Stathis
import com.google.gwt.http.client.URL;
47 ab1eb3f8 Christos Stathis
import com.google.gwt.json.client.JSONArray;
48 ab1eb3f8 Christos Stathis
import com.google.gwt.json.client.JSONObject;
49 ab1eb3f8 Christos Stathis
import com.google.gwt.json.client.JSONParser;
50 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.TreeItem;
51 ab1eb3f8 Christos Stathis
52 ab1eb3f8 Christos Stathis
53 ab1eb3f8 Christos Stathis
public class SharedResource extends RestResource{
54 ab1eb3f8 Christos Stathis
55 ab1eb3f8 Christos Stathis
        public SharedResource(String aUri) {
56 ab1eb3f8 Christos Stathis
                super(aUri);
57 ab1eb3f8 Christos Stathis
        }
58 ab1eb3f8 Christos Stathis
59 ab1eb3f8 Christos Stathis
        List<String> filePaths = new LinkedList<String>();
60 ab1eb3f8 Christos Stathis
        List<String> subfolderPaths = new LinkedList<String>();
61 ab1eb3f8 Christos Stathis
        List<FolderResource> folders = new ArrayList<FolderResource>();
62 ab1eb3f8 Christos Stathis
63 ab1eb3f8 Christos Stathis
        List<FileResource> files = new ArrayList<FileResource>();
64 ab1eb3f8 Christos Stathis
65 ab1eb3f8 Christos Stathis
        private boolean filesExpanded=false;
66 ab1eb3f8 Christos Stathis
67 ab1eb3f8 Christos Stathis
        /**
68 ab1eb3f8 Christos Stathis
         * Retrieve the files.
69 ab1eb3f8 Christos Stathis
         *
70 ab1eb3f8 Christos Stathis
         * @return the files
71 ab1eb3f8 Christos Stathis
         */
72 ab1eb3f8 Christos Stathis
        public List<String> getFilePaths() {
73 ab1eb3f8 Christos Stathis
                return filePaths;
74 ab1eb3f8 Christos Stathis
        }
75 ab1eb3f8 Christos Stathis
76 ab1eb3f8 Christos Stathis
        /**
77 ab1eb3f8 Christos Stathis
         * Modify the files.
78 ab1eb3f8 Christos Stathis
         *
79 ab1eb3f8 Christos Stathis
         * @param newFilePaths the files to set
80 ab1eb3f8 Christos Stathis
         */
81 ab1eb3f8 Christos Stathis
        public void setFilePaths(List<String> newFilePaths) {
82 ab1eb3f8 Christos Stathis
                filePaths = newFilePaths;
83 ab1eb3f8 Christos Stathis
        }
84 ab1eb3f8 Christos Stathis
85 ab1eb3f8 Christos Stathis
        /**
86 ab1eb3f8 Christos Stathis
         * Retrieve the subfolders.
87 ab1eb3f8 Christos Stathis
         *
88 ab1eb3f8 Christos Stathis
         * @return the subfolders
89 ab1eb3f8 Christos Stathis
         */
90 ab1eb3f8 Christos Stathis
        public List<String> getSubfolderPaths() {
91 ab1eb3f8 Christos Stathis
                return subfolderPaths;
92 ab1eb3f8 Christos Stathis
        }
93 ab1eb3f8 Christos Stathis
94 ab1eb3f8 Christos Stathis
        /**
95 ab1eb3f8 Christos Stathis
         * Modify the subfolder paths.
96 ab1eb3f8 Christos Stathis
         *
97 ab1eb3f8 Christos Stathis
         * @param newSubfolderPaths the subfolder paths to set
98 ab1eb3f8 Christos Stathis
         */
99 ab1eb3f8 Christos Stathis
        public void setSubfolderPaths(List<String> newSubfolderPaths) {
100 ab1eb3f8 Christos Stathis
                subfolderPaths = newSubfolderPaths;
101 ab1eb3f8 Christos Stathis
        }
102 ab1eb3f8 Christos Stathis
103 ab1eb3f8 Christos Stathis
        /**
104 ab1eb3f8 Christos Stathis
         * Retrieve the folders.
105 ab1eb3f8 Christos Stathis
         *
106 ab1eb3f8 Christos Stathis
         * @return the folders
107 ab1eb3f8 Christos Stathis
         */
108 ab1eb3f8 Christos Stathis
        public List<FolderResource> getFolders() {
109 ab1eb3f8 Christos Stathis
                return folders;
110 ab1eb3f8 Christos Stathis
        }
111 ab1eb3f8 Christos Stathis
112 ab1eb3f8 Christos Stathis
        /**
113 ab1eb3f8 Christos Stathis
         * Modify the folders.
114 ab1eb3f8 Christos Stathis
         *
115 ab1eb3f8 Christos Stathis
         * @param newFolders the folders to set
116 ab1eb3f8 Christos Stathis
         */
117 ab1eb3f8 Christos Stathis
        public void setFolders(List<FolderResource> newFolders) {
118 ab1eb3f8 Christos Stathis
                folders = newFolders;
119 ab1eb3f8 Christos Stathis
        }
120 ab1eb3f8 Christos Stathis
121 ab1eb3f8 Christos Stathis
        /**
122 ab1eb3f8 Christos Stathis
         * Retrieve the files.
123 ab1eb3f8 Christos Stathis
         *
124 ab1eb3f8 Christos Stathis
         * @return the files
125 ab1eb3f8 Christos Stathis
         */
126 ab1eb3f8 Christos Stathis
        public List<FileResource> getFiles() {
127 ab1eb3f8 Christos Stathis
                return files;
128 ab1eb3f8 Christos Stathis
        }
129 ab1eb3f8 Christos Stathis
130 ab1eb3f8 Christos Stathis
        /**
131 ab1eb3f8 Christos Stathis
         * Modify the files.
132 ab1eb3f8 Christos Stathis
         *
133 ab1eb3f8 Christos Stathis
         * @param newFiles the files to set
134 ab1eb3f8 Christos Stathis
         */
135 ab1eb3f8 Christos Stathis
        public void setFiles(List<FileResource> newFiles) {
136 ab1eb3f8 Christos Stathis
                files = newFiles;
137 ab1eb3f8 Christos Stathis
        }
138 ab1eb3f8 Christos Stathis
139 ab1eb3f8 Christos Stathis
        @Override
140 ab1eb3f8 Christos Stathis
        public void createFromJSON(String text) {
141 ab1eb3f8 Christos Stathis
                JSONObject json = (JSONObject) JSONParser.parse(text);
142 ab1eb3f8 Christos Stathis
                if (json.get("folders") != null) {
143 ab1eb3f8 Christos Stathis
                        JSONArray subs = json.get("folders").isArray();
144 ab1eb3f8 Christos Stathis
                        if (subs != null)
145 ab1eb3f8 Christos Stathis
                                for (int i = 0; i < subs.size(); i++) {
146 ab1eb3f8 Christos Stathis
                                        JSONObject so = subs.get(i).isObject();
147 ab1eb3f8 Christos Stathis
                                        if (so != null) {
148 ab1eb3f8 Christos Stathis
                                                String subUri = unmarshallString(so, "uri");
149 ab1eb3f8 Christos Stathis
                                                String subName = unmarshallString(so, "name");
150 ab1eb3f8 Christos Stathis
                                                if (subUri != null && subName != null) {
151 ab1eb3f8 Christos Stathis
                                                        if (!subUri.endsWith("/"))
152 ab1eb3f8 Christos Stathis
                                                                subUri = subUri + "/";
153 ab1eb3f8 Christos Stathis
                                                        FolderResource sub = new FolderResource(subUri);
154 ab1eb3f8 Christos Stathis
                                                        sub.setName(subName);
155 ab1eb3f8 Christos Stathis
                                                        sub.setNeedsExpanding(true);
156 ab1eb3f8 Christos Stathis
                                                        folders.add(sub);
157 ab1eb3f8 Christos Stathis
                                                        subfolderPaths.add(subUri);
158 ab1eb3f8 Christos Stathis
                                                }
159 ab1eb3f8 Christos Stathis
                                        }
160 ab1eb3f8 Christos Stathis
                                }
161 ab1eb3f8 Christos Stathis
                }
162 ab1eb3f8 Christos Stathis
                if (json.get("files") != null) {
163 ab1eb3f8 Christos Stathis
                        JSONArray subs = json.get("files").isArray();
164 ab1eb3f8 Christos Stathis
                        if (subs != null)
165 ab1eb3f8 Christos Stathis
                                for (int i = 0; i < subs.size(); i++) {
166 ab1eb3f8 Christos Stathis
                                        JSONObject fo = subs.get(i).isObject();
167 ab1eb3f8 Christos Stathis
                                        if (fo != null) {
168 ab1eb3f8 Christos Stathis
                                                String fname = unmarshallString(fo, "name");
169 ab1eb3f8 Christos Stathis
                                                String fowner = unmarshallString(fo, "owner");
170 ab1eb3f8 Christos Stathis
                                                String fcontent = unmarshallString(fo, "content");
171 ab1eb3f8 Christos Stathis
                                                boolean fversioned = unmarshallBoolean(fo, "versioned");
172 ab1eb3f8 Christos Stathis
                                                Integer fversion = null;
173 ab1eb3f8 Christos Stathis
                                                if (fo.get("version") != null)
174 ab1eb3f8 Christos Stathis
                                                        fversion = new Integer(fo.get("version").toString());
175 ab1eb3f8 Christos Stathis
                                                boolean fdeleted = unmarshallBoolean(fo, "deleted");
176 ab1eb3f8 Christos Stathis
                                                Date fcreationDate = null;
177 ab1eb3f8 Christos Stathis
                                                if (fo.get("creationDate") != null)
178 ab1eb3f8 Christos Stathis
                                                        fcreationDate = new Date(new Long(fo.get("creationDate").toString()));
179 ab1eb3f8 Christos Stathis
                                                Date fmodificationDate = null;
180 ab1eb3f8 Christos Stathis
                                                if (fo.get("modificationDate") != null)
181 ab1eb3f8 Christos Stathis
                                                        fmodificationDate = new Date(new Long(fo.get("modificationDate").toString()));
182 ab1eb3f8 Christos Stathis
                                                String furi = unmarshallString(fo,"uri");
183 ab1eb3f8 Christos Stathis
                                                Long fsize = 0L;
184 ab1eb3f8 Christos Stathis
                                                if(fo.get("size") != null)
185 ab1eb3f8 Christos Stathis
                                                        fsize = new Long(fo.get("size").toString());
186 ab1eb3f8 Christos Stathis
                                                filePaths.add(furi);
187 ab1eb3f8 Christos Stathis
                                                String fpath = unmarshallString(fo, "path");
188 ab1eb3f8 Christos Stathis
                                                fpath = URL.decodeComponent(fpath);
189 ab1eb3f8 Christos Stathis
                                                FileResource fs = new FileResource(furi);
190 ab1eb3f8 Christos Stathis
                                                fs.setPath(fpath);
191 ab1eb3f8 Christos Stathis
                                                fs.setName(fname);
192 ab1eb3f8 Christos Stathis
                                                fs.setOwner(fowner);
193 ab1eb3f8 Christos Stathis
                                                fs.setVersion(fversion);
194 ab1eb3f8 Christos Stathis
                                                fs.setVersioned(unmarshallBoolean(fo, "versioned"));
195 ab1eb3f8 Christos Stathis
                                                fs.setContentLength(fsize);
196 ab1eb3f8 Christos Stathis
                                                fs.setDeleted(fdeleted);
197 ab1eb3f8 Christos Stathis
                                                fs.setShared(unmarshallBoolean(fo,"shared"));
198 ab1eb3f8 Christos Stathis
                                                fs.setVersioned(fversioned);
199 ab1eb3f8 Christos Stathis
                                                fs.setCreationDate(fcreationDate);
200 ab1eb3f8 Christos Stathis
                                                fs.setModificationDate(fmodificationDate);
201 ab1eb3f8 Christos Stathis
                                                fs.setContentType(fcontent);
202 ab1eb3f8 Christos Stathis
                                                files.add(fs);
203 ab1eb3f8 Christos Stathis
                                        }
204 ab1eb3f8 Christos Stathis
                                }
205 ab1eb3f8 Christos Stathis
                }
206 ab1eb3f8 Christos Stathis
        }
207 ab1eb3f8 Christos Stathis
208 ab1eb3f8 Christos Stathis
        public List<String> getRootSharedFiles(){
209 ab1eb3f8 Christos Stathis
                List<String> res = new ArrayList<String>();
210 ab1eb3f8 Christos Stathis
                for(String f : getFilePaths()){
211 ab1eb3f8 Christos Stathis
                        boolean contained = false;
212 ab1eb3f8 Christos Stathis
                        for(String fo : getSubfolderPaths())
213 ab1eb3f8 Christos Stathis
                                if(f.startsWith(fo))
214 ab1eb3f8 Christos Stathis
                                        contained = true;
215 ab1eb3f8 Christos Stathis
                        if(!contained)
216 ab1eb3f8 Christos Stathis
                                res.add(f);
217 ab1eb3f8 Christos Stathis
                }
218 ab1eb3f8 Christos Stathis
                return res;
219 ab1eb3f8 Christos Stathis
        }
220 ab1eb3f8 Christos Stathis
221 ab1eb3f8 Christos Stathis
        @Override
222 ab1eb3f8 Christos Stathis
        public String getLastModifiedSince() {
223 ab1eb3f8 Christos Stathis
                return null;
224 ab1eb3f8 Christos Stathis
        }
225 ab1eb3f8 Christos Stathis
226 ab1eb3f8 Christos Stathis
        public MultipleGetCommand.Cached[] getFileCache(){
227 ab1eb3f8 Christos Stathis
                if(getFilePaths().size() != getFiles().size()){
228 ab1eb3f8 Christos Stathis
                        GWT.log("MISMATCH IN PATH AND FILES SIZE", null);
229 ab1eb3f8 Christos Stathis
                        return null;
230 ab1eb3f8 Christos Stathis
                }
231 ab1eb3f8 Christos Stathis
                if(!filesExpanded)
232 ab1eb3f8 Christos Stathis
                        return null;
233 ab1eb3f8 Christos Stathis
                MultipleGetCommand.Cached[] result = new MultipleGetCommand.Cached[getFilePaths().size()];
234 ab1eb3f8 Christos Stathis
                for(int i=0; i<getFiles().size();i++){
235 ab1eb3f8 Christos Stathis
                        FileResource r = getFiles().get(i);
236 ab1eb3f8 Christos Stathis
                        Cached c = new Cached();
237 ab1eb3f8 Christos Stathis
                        c.cache=r;
238 ab1eb3f8 Christos Stathis
                        c.uri=r.uri;
239 ab1eb3f8 Christos Stathis
                        result[i] = c;
240 ab1eb3f8 Christos Stathis
                }
241 ab1eb3f8 Christos Stathis
                return result;
242 ab1eb3f8 Christos Stathis
        }
243 ab1eb3f8 Christos Stathis
244 ab1eb3f8 Christos Stathis
        public void setFilesExpanded(boolean newFilesExpanded) {
245 ab1eb3f8 Christos Stathis
                filesExpanded = newFilesExpanded;
246 ab1eb3f8 Christos Stathis
        }
247 ab1eb3f8 Christos Stathis
248 ab1eb3f8 Christos Stathis
        @Override
249 ab1eb3f8 Christos Stathis
        public String constructUri(TreeItem treeItem, String path){
250 ab1eb3f8 Christos Stathis
                String constructedUri = "Files/"+ getUri().substring(path.lastIndexOf("/")+1);
251 ab1eb3f8 Christos Stathis
                return constructedUri;
252 ab1eb3f8 Christos Stathis
        }
253 ab1eb3f8 Christos Stathis
254 ab1eb3f8 Christos Stathis
}