Use separate progress bars next to each file, in order to better track the progress...
[pithos] / src / gr / ebs / gss / client / rest / resource / OtherUserResource.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 OtherUserResource extends RestResource{
37         public OtherUserResource(String aUri) {
38                 super(aUri);
39         }
40
41         String username;
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 username.
49          *
50          * @return the username
51          */
52         public String getUsername() {
53                 return username;
54         }
55
56         /**
57          * Modify the username.
58          *
59          * @param aUsername the username to set
60          */
61         public void setUsername(String aUsername) {
62                 username = aUsername;
63         }
64
65         /**
66          * Retrieve the files.
67          *
68          * @return the files
69          */
70         public List<String> getFilePaths() {
71                 return filePaths;
72         }
73
74         /**
75          * Modify the files.
76          *
77          * @param newFiles the files to set
78          */
79         public void setFilePaths(List<String> newFiles) {
80                 filePaths = newFiles;
81         }
82
83         /**
84          * Retrieve the subfolders.
85          *
86          * @return the subfolders
87          */
88         public List<String> getSubfolderPaths() {
89                 return subfolderPaths;
90         }
91
92         /**
93          * Modify the subfolders.
94          *
95          * @param subfolders the subfolders to set
96          */
97         public void setSubfolderPaths(List<String> subfolders) {
98                 subfolderPaths = subfolders;
99         }
100
101         /**
102          * Retrieve the folders.
103          *
104          * @return the folders
105          */
106         public List<FolderResource> getFolders() {
107                 return folders;
108         }
109
110         /**
111          * Modify the folders.
112          *
113          * @param newFolders the folders to set
114          */
115         public void setFolders(List<FolderResource> newFolders) {
116                 folders = newFolders;
117         }
118
119         /**
120          * Retrieve the files.
121          *
122          * @return the files
123          */
124         public List<FileResource> getFiles() {
125                 return files;
126         }
127
128         /**
129          * Modify the files.
130          *
131          * @param newFiles the files to set
132          */
133         public void setFiles(List<FileResource> newFiles) {
134                 files = newFiles;
135         }
136
137         @Override
138         public void createFromJSON(String text) {
139                 JSONObject json = (JSONObject) JSONParser.parse(text);
140                 if (json.get("folders") != null) {
141                         JSONArray subs = json.get("folders").isArray();
142                         if (subs != null)
143                                 for (int i = 0; i < subs.size(); i++) {
144                                         JSONObject so = subs.get(i).isObject();
145                                         if (so != null) {
146                                                 String subUri = unmarshallString(so, "uri");
147                                                 String subName = unmarshallString(so, "name");
148                                                 if (subUri != null && subName != null) {
149                                                         if (!subUri.endsWith("/"))
150                                                                 subUri = subUri + "/";
151                                                         FolderResource sub = new FolderResource(subUri);
152                                                         sub.setName(subName);
153                                                         sub.setNeedsExpanding(true);
154                                                         folders.add(sub);
155                                                         subfolderPaths.add(subUri);
156                                                 }
157                                         }
158                                 }
159                 }
160                 if (json.get("files") != null) {
161                         JSONArray subs = json.get("files").isArray();
162                         if (subs != null)
163                                 for (int i = 0; i < subs.size(); i++) {
164                                         JSONObject fo = subs.get(i).isObject();
165                                         if (fo != null) {
166                                                 String fname = unmarshallString(fo, "name");
167                                                 String fowner = unmarshallString(fo, "owner");
168                                                 String fcontent = unmarshallString(fo, "content");
169                                                 Integer fversion = null;
170                                                 if (fo.get("version") != null)
171                                                         fversion = new Integer(fo.get("version").toString());
172                                                 boolean fdeleted = unmarshallBoolean(fo, "deleted");
173                                                 Date fcreationDate = null;
174                                                 if (fo.get("creationDate") != null)
175                                                         fcreationDate = new Date(new Long(fo.get("creationDate").toString()));
176                                                 String furi = unmarshallString(fo,"uri");
177                                                 Long fsize = 0L;
178                                                 if(fo.get("size") != null)
179                                                         fsize = new Long(fo.get("size").toString());
180                                                 filePaths.add(furi);
181                                                 String fpath = unmarshallString(fo, "path");
182                                                 fpath = URL.decodeComponent(fpath);
183                                                 FileResource fs = new FileResource(furi);
184                                                 fs.setName(fname);
185                                                 fs.setPath(fpath);
186                                                 fs.setOwner(fowner);
187                                                 fs.setVersion(fversion);
188                                                 fs.setContentLength(fsize);
189                                                 fs.setDeleted(fdeleted);
190                                                 fs.setCreationDate(fcreationDate);
191                                                 fs.setContentType(fcontent);
192                                                 files.add(fs);
193                                         }
194                                 }
195                 }
196         }
197
198         public String getName(){
199                 String[] names = uri.split("/");
200                 return names[names.length -1];
201         }
202 }