Automated merge with https://gss.googlecode.com/hg/
[pithos] / src / gr / ebs / gss / client / rest / resource / SharedResource.java
1 /*
2  * Copyright 2009, 2010 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 package gr.ebs.gss.client.rest.resource;
20
21 import gr.ebs.gss.client.rest.MultipleGetCommand;
22 import gr.ebs.gss.client.rest.MultipleGetCommand.Cached;
23
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.LinkedList;
27 import java.util.List;
28
29 import com.google.gwt.core.client.GWT;
30 import com.google.gwt.http.client.URL;
31 import com.google.gwt.json.client.JSONArray;
32 import com.google.gwt.json.client.JSONObject;
33 import com.google.gwt.json.client.JSONParser;
34 import com.google.gwt.user.client.ui.TreeItem;
35
36
37 /**
38  * @author kman
39  *
40  */
41 public class SharedResource extends RestResource{
42
43         public SharedResource(String aUri) {
44                 super(aUri);
45         }
46
47         List<String> filePaths = new LinkedList<String>();
48         List<String> subfolderPaths = new LinkedList<String>();
49         List<FolderResource> folders = new ArrayList<FolderResource>();
50
51         List<FileResource> files = new ArrayList<FileResource>();
52
53         private boolean filesExpanded=false;
54
55         /**
56          * Retrieve the files.
57          *
58          * @return the files
59          */
60         public List<String> getFilePaths() {
61                 return filePaths;
62         }
63
64         /**
65          * Modify the files.
66          *
67          * @param newFilePaths the files to set
68          */
69         public void setFilePaths(List<String> newFilePaths) {
70                 filePaths = newFilePaths;
71         }
72
73         /**
74          * Retrieve the subfolders.
75          *
76          * @return the subfolders
77          */
78         public List<String> getSubfolderPaths() {
79                 return subfolderPaths;
80         }
81
82         /**
83          * Modify the subfolder paths.
84          *
85          * @param newSubfolderPaths the subfolder paths to set
86          */
87         public void setSubfolderPaths(List<String> newSubfolderPaths) {
88                 subfolderPaths = newSubfolderPaths;
89         }
90
91         /**
92          * Retrieve the folders.
93          *
94          * @return the folders
95          */
96         public List<FolderResource> getFolders() {
97                 return folders;
98         }
99
100         /**
101          * Modify the folders.
102          *
103          * @param newFolders the folders to set
104          */
105         public void setFolders(List<FolderResource> newFolders) {
106                 folders = newFolders;
107         }
108
109         /**
110          * Retrieve the files.
111          *
112          * @return the files
113          */
114         public List<FileResource> getFiles() {
115                 return files;
116         }
117
118         /**
119          * Modify the files.
120          *
121          * @param newFiles the files to set
122          */
123         public void setFiles(List<FileResource> newFiles) {
124                 files = newFiles;
125         }
126
127         @Override
128         public void createFromJSON(String text) {
129                 JSONObject json = (JSONObject) JSONParser.parse(text);
130                 if (json.get("folders") != null) {
131                         JSONArray subs = json.get("folders").isArray();
132                         if (subs != null)
133                                 for (int i = 0; i < subs.size(); i++) {
134                                         JSONObject so = subs.get(i).isObject();
135                                         if (so != null) {
136                                                 String subUri = unmarshallString(so, "uri");
137                                                 String subName = unmarshallString(so, "name");
138                                                 if (subUri != null && subName != null) {
139                                                         if (!subUri.endsWith("/"))
140                                                                 subUri = subUri + "/";
141                                                         FolderResource sub = new FolderResource(subUri);
142                                                         sub.setName(subName);
143                                                         sub.setNeedsExpanding(true);
144                                                         folders.add(sub);
145                                                         subfolderPaths.add(subUri);
146                                                 }
147                                         }
148                                 }
149                 }
150                 if (json.get("files") != null) {
151                         JSONArray subs = json.get("files").isArray();
152                         if (subs != null)
153                                 for (int i = 0; i < subs.size(); i++) {
154                                         JSONObject fo = subs.get(i).isObject();
155                                         if (fo != null) {
156                                                 String fname = unmarshallString(fo, "name");
157                                                 String fowner = unmarshallString(fo, "owner");
158                                                 String fcontent = unmarshallString(fo, "content");
159                                                 Integer fversion = null;
160                                                 if (fo.get("version") != null)
161                                                         fversion = new Integer(fo.get("version").toString());
162                                                 boolean fdeleted = unmarshallBoolean(fo, "deleted");
163                                                 Date fcreationDate = null;
164                                                 if (fo.get("creationDate") != null)
165                                                         fcreationDate = new Date(new Long(fo.get("creationDate").toString()));
166                                                 Date fmodificationDate = null;
167                                                 if (fo.get("modificationDate") != null)
168                                                         fmodificationDate = new Date(new Long(fo.get("modificationDate").toString()));
169                                                 String furi = unmarshallString(fo,"uri");
170                                                 Long fsize = 0L;
171                                                 if(fo.get("size") != null)
172                                                         fsize = new Long(fo.get("size").toString());
173                                                 filePaths.add(furi);
174                                                 String fpath = unmarshallString(fo, "path");
175                                                 fpath = URL.decodeComponent(fpath);
176                                                 FileResource fs = new FileResource(furi);
177                                                 fs.setPath(fpath);
178                                                 fs.setName(fname);
179                                                 fs.setOwner(fowner);
180                                                 fs.setVersion(fversion);
181                                                 fs.setContentLength(fsize);
182                                                 fs.setDeleted(fdeleted);
183                                                 fs.setShared(unmarshallBoolean(fo,"shared"));
184                                                 fs.setCreationDate(fcreationDate);
185                                                 fs.setModificationDate(fmodificationDate);
186                                                 fs.setContentType(fcontent);
187                                                 files.add(fs);
188                                         }
189                                 }
190                 }
191         }
192
193         public List<String> getRootSharedFiles(){
194                 List<String> res = new ArrayList<String>();
195                 for(String f : getFilePaths()){
196                         boolean contained = false;
197                         for(String fo : getSubfolderPaths())
198                                 if(f.startsWith(fo))
199                                         contained = true;
200                         if(!contained)
201                                 res.add(f);
202                 }
203                 return res;
204         }
205
206         @Override
207         public String getLastModifiedSince() {
208                 return null;
209         }
210
211         public MultipleGetCommand.Cached[] getFileCache(){
212                 if(getFilePaths().size() != getFiles().size()){
213                         GWT.log("MISMATCH IN PATH AND FILES SIZE", null);
214                         return null;
215                 }
216                 if(!filesExpanded)
217                         return null;
218                 MultipleGetCommand.Cached[] result = new MultipleGetCommand.Cached[getFilePaths().size()];
219                 for(int i=0; i<getFiles().size();i++){
220                         FileResource r = getFiles().get(i);
221                         Cached c = new Cached();
222                         c.cache=r;
223                         c.uri=r.uri;
224                         result[i] = c;
225                 }
226                 return result;
227         }
228
229         public void setFilesExpanded(boolean newFilesExpanded) {
230                 filesExpanded = newFilesExpanded;
231         }
232
233         @Override
234         public String constructUri(TreeItem treeItem, String path){
235                 String constructedUri = "Files/"+ getUri().substring(path.lastIndexOf("/")+1);
236                 return constructedUri;
237         }
238
239 }