Restore check for atob, since it works fine in every combination tried: Windows/Linux...
[pithos] / src / gr / ebs / gss / client / rest / resource / SharedResource.java
1 /*
2  * Copyright 2009 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 java.util.ArrayList;
22 import java.util.Date;
23 import java.util.LinkedList;
24 import java.util.List;
25
26 import com.google.gwt.http.client.URL;
27 import com.google.gwt.json.client.JSONArray;
28 import com.google.gwt.json.client.JSONObject;
29 import com.google.gwt.json.client.JSONParser;
30
31
32 /**
33  * @author kman
34  *
35  */
36 public class SharedResource extends RestResource{
37
38         public SharedResource(String aUri) {
39                 super(aUri);
40         }
41
42         List<String> filePaths = new LinkedList<String>();
43         List<String> subfolderPaths = new LinkedList<String>();
44         List<FolderResource> folders = new ArrayList<FolderResource>();
45
46         List<FileResource> files = new ArrayList<FileResource>();
47
48         /**
49          * Retrieve the files.
50          *
51          * @return the files
52          */
53         public List<String> getFilePaths() {
54                 return filePaths;
55         }
56
57         /**
58          * Modify the files.
59          *
60          * @param newFilePaths the files to set
61          */
62         public void setFilePaths(List<String> newFilePaths) {
63                 filePaths = newFilePaths;
64         }
65
66         /**
67          * Retrieve the subfolders.
68          *
69          * @return the subfolders
70          */
71         public List<String> getSubfolderPaths() {
72                 return subfolderPaths;
73         }
74
75         /**
76          * Modify the subfolder paths.
77          *
78          * @param newSubfolderPaths the subfolder paths to set
79          */
80         public void setSubfolderPaths(List<String> newSubfolderPaths) {
81                 subfolderPaths = newSubfolderPaths;
82         }
83
84         /**
85          * Retrieve the folders.
86          *
87          * @return the folders
88          */
89         public List<FolderResource> getFolders() {
90                 return folders;
91         }
92
93         /**
94          * Modify the folders.
95          *
96          * @param newFolders the folders to set
97          */
98         public void setFolders(List<FolderResource> newFolders) {
99                 folders = newFolders;
100         }
101
102         /**
103          * Retrieve the files.
104          *
105          * @return the files
106          */
107         public List<FileResource> getFiles() {
108                 return files;
109         }
110
111         /**
112          * Modify the files.
113          *
114          * @param newFiles the files to set
115          */
116         public void setFiles(List<FileResource> newFiles) {
117                 files = newFiles;
118         }
119
120         @Override
121         public void createFromJSON(String text) {
122                 JSONObject json = (JSONObject) JSONParser.parse(text);
123                 if (json.get("folders") != null) {
124                         JSONArray subs = json.get("folders").isArray();
125                         if (subs != null)
126                                 for (int i = 0; i < subs.size(); i++) {
127                                         JSONObject so = subs.get(i).isObject();
128                                         if (so != null) {
129                                                 String subUri = unmarshallString(so, "uri");
130                                                 String subName = unmarshallString(so, "name");
131                                                 if (subUri != null && subName != null) {
132                                                         if (!subUri.endsWith("/"))
133                                                                 subUri = subUri + "/";
134                                                         FolderResource sub = new FolderResource(subUri);
135                                                         sub.setName(subName);
136                                                         sub.setNeedsExpanding(true);
137                                                         folders.add(sub);
138                                                         subfolderPaths.add(subUri);
139                                                 }
140                                         }
141                                 }
142                 }
143                 if (json.get("files") != null) {
144                         JSONArray subs = json.get("files").isArray();
145                         if (subs != null)
146                                 for (int i = 0; i < subs.size(); i++) {
147                                         JSONObject fo = subs.get(i).isObject();
148                                         if (fo != null) {
149                                                 String fname = unmarshallString(fo, "name");
150                                                 String fowner = unmarshallString(fo, "owner");
151                                                 String fcontent = unmarshallString(fo, "content");
152                                                 Integer fversion = null;
153                                                 if (fo.get("version") != null)
154                                                         fversion = new Integer(fo.get("version").toString());
155                                                 boolean fdeleted = unmarshallBoolean(fo, "deleted");
156                                                 Date fcreationDate = null;
157                                                 if (fo.get("creationDate") != null)
158                                                         fcreationDate = new Date(new Long(fo.get("creationDate").toString()));
159                                                 Date fmodificationDate = null;
160                                                 if (fo.get("modificationDate") != null)
161                                                         fmodificationDate = new Date(new Long(fo.get("modificationDate").toString()));
162                                                 String furi = unmarshallString(fo,"uri");
163                                                 Long fsize = 0L;
164                                                 if(fo.get("size") != null)
165                                                         fsize = new Long(fo.get("size").toString());
166                                                 filePaths.add(furi);
167                                                 String fpath = unmarshallString(fo, "path");
168                                                 fpath = URL.decodeComponent(fpath);
169                                                 FileResource fs = new FileResource(furi);
170                                                 fs.setPath(fpath);
171                                                 fs.setName(fname);
172                                                 fs.setOwner(fowner);
173                                                 fs.setVersion(fversion);
174                                                 fs.setContentLength(fsize);
175                                                 fs.setDeleted(fdeleted);
176                                                 fs.setCreationDate(fcreationDate);
177                                                 fs.setModificationDate(fmodificationDate);
178                                                 fs.setContentType(fcontent);
179                                                 files.add(fs);
180                                         }
181                                 }
182                 }
183         }
184
185         public List<String> getRootSharedFiles(){
186                 List<String> res = new ArrayList<String>();
187                 for(String f : getFilePaths()){
188                         boolean contained = false;
189                         for(String fo : getSubfolderPaths())
190                                 if(f.startsWith(fo))
191                                         contained = true;
192                         if(!contained)
193                                 res.add(f);
194                 }
195                 return res;
196         }
197
198 }