Use separate progress bars next to each file, in order to better track the progress...
[pithos] / src / gr / ebs / gss / client / rest / resource / TrashResource.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 TrashResource extends RestResource{
37
38         public TrashResource(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         List<FileResource> files = new ArrayList<FileResource>();
46
47         /**
48          * Retrieve the file paths.
49          *
50          * @return the file paths
51          */
52         public List<String> getFilePaths() {
53                 return filePaths;
54         }
55
56         /**
57          * Modify the file paths.
58          *
59          * @param newFilePaths the file paths to set
60          */
61         public void setFilePaths(List<String> newFilePaths) {
62                 filePaths = newFilePaths;
63         }
64
65         /**
66          * Retrieve the subfolder paths.
67          *
68          * @return the subfolder paths
69          */
70         public List<String> getSubfolderPaths() {
71                 return subfolderPaths;
72         }
73
74         /**
75          * Modify the subfolder paths.
76          *
77          * @param newSubfolderPaths the subfolders to set
78          */
79         public void setSubfolderPaths(List<String> newSubfolderPaths) {
80                 subfolderPaths = newSubfolderPaths;
81         }
82
83         /**
84          * Retrieve the folders.
85          *
86          * @return the folders
87          */
88         public List<FolderResource> getFolders() {
89                 return folders;
90         }
91
92         /**
93          * Modify the folders.
94          *
95          * @param newFolders the folders to set
96          */
97         public void setFolders(List<FolderResource> newFolders) {
98                 folders = newFolders;
99         }
100
101         /**
102          * Retrieve the files.
103          *
104          * @return the files
105          */
106         public List<FileResource> getFiles() {
107                 return files;
108         }
109
110         /**
111          * Modify the files.
112          *
113          * @param newFiles the files to set
114          */
115         public void setFiles(List<FileResource> newFiles) {
116                 files = newFiles;
117         }
118
119         @Override
120         public void createFromJSON(String text) {
121                 JSONObject json = (JSONObject) JSONParser.parse(text);
122                 if (json.get("folders") != null) {
123                         JSONArray subs = json.get("folders").isArray();
124                         if (subs != null)
125                                 for (int i = 0; i < subs.size(); i++) {
126                                         JSONObject so = subs.get(i).isObject();
127                                         if (so != null) {
128                                                 String subUri = unmarshallString(so, "uri");
129                                                 String subName = unmarshallString(so, "name");
130                                                 subName = URL.decodeComponent(subName);
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                                                 String fpath = unmarshallString(fo, "path");
153                                                 fpath = URL.decodeComponent(fpath);
154                                                 Integer fversion = null;
155                                                 if (fo.get("version") != null)
156                                                         fversion = new Integer(fo.get("version").toString());
157                                                 boolean fdeleted = unmarshallBoolean(fo, "deleted");
158                                                 Date fcreationDate = null;
159                                                 if (fo.get("creationDate") != null)
160                                                         fcreationDate = new Date(new Long(fo.get("creationDate").toString()));
161                                                 String furi = unmarshallString(fo,"uri");
162                                                 Long fsize = 0L;
163                                                 if(fo.get("size") != null)
164                                                         fsize = new Long(fo.get("size").toString());
165                                                 filePaths.add(furi);
166                                                 FileResource fs = new FileResource(furi);
167                                                 fs.setName(fname);
168                                                 fs.setOwner(fowner);
169                                                 fs.setVersion(fversion);
170                                                 fs.setContentLength(fsize);
171                                                 fs.setDeleted(fdeleted);
172                                                 fs.setPath(fpath);
173                                                 fs.setCreationDate(fcreationDate);
174                                                 fs.setContentType(fcontent);
175                                                 files.add(fs);
176                                         }
177                                 }
178                 }
179         }
180
181         public List<FolderResource> getTrashedFolders(){
182                 List<FolderResource> res = new ArrayList<FolderResource>();
183                 for(String s : subfolderPaths){
184                         String[] pathElements =  s.split("/");
185                         FolderResource tr = new FolderResource(s);
186                         tr.setName(URL.decodeComponent(pathElements[pathElements.length-1]));
187                         res.add(tr);
188                 }
189                 return res;
190         }
191 }