Moved all web client files to their folder. Updated ignored files
[pithos-web-client] / src / org / gss_project / gss / web / client / rest / resource / TrashResource.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 org.gss_project.gss.web.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 import com.google.gwt.user.client.ui.TreeItem;
31
32
33 /**
34  * @author kman
35  *
36  */
37 public class TrashResource extends RestResource{
38
39         public TrashResource(String aUri) {
40                 super(aUri);
41         }
42
43         List<String> filePaths = new LinkedList<String>();
44         List<String> subfolderPaths = new LinkedList<String>();
45         List<FolderResource> folders = new ArrayList<FolderResource>();
46         List<FileResource> files = new ArrayList<FileResource>();
47
48         /**
49          * Retrieve the file paths.
50          *
51          * @return the file paths
52          */
53         public List<String> getFilePaths() {
54                 return filePaths;
55         }
56
57         /**
58          * Modify the file paths.
59          *
60          * @param newFilePaths the file paths to set
61          */
62         public void setFilePaths(List<String> newFilePaths) {
63                 filePaths = newFilePaths;
64         }
65
66         /**
67          * Retrieve the subfolder paths.
68          *
69          * @return the subfolder paths
70          */
71         public List<String> getSubfolderPaths() {
72                 return subfolderPaths;
73         }
74
75         /**
76          * Modify the subfolder paths.
77          *
78          * @param newSubfolderPaths the subfolders 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                                                 subName = URL.decodeComponent(subName);
132                                                 if (subUri != null && subName != null) {
133                                                         if (!subUri.endsWith("/"))
134                                                                 subUri = subUri + "/";
135                                                         FolderResource sub = new FolderResource(subUri);
136                                                         sub.setName(subName);
137                                                         sub.setNeedsExpanding(true);
138                                                         folders.add(sub);
139                                                         subfolderPaths.add(subUri);
140                                                 }
141                                         }
142                                 }
143                 }
144                 if (json.get("files") != null) {
145                         JSONArray subs = json.get("files").isArray();
146                         if (subs != null)
147                                 for (int i = 0; i < subs.size(); i++) {
148                                         JSONObject fo = subs.get(i).isObject();
149                                         if (fo != null) {
150                                                 String fname = unmarshallString(fo, "name");
151                                                 String fowner = unmarshallString(fo, "owner");
152                                                 String fcontent = unmarshallString(fo, "content");
153                                                 Boolean fshared = unmarshallBoolean(fo, "shared");                                              
154                                                 Boolean fversioned = unmarshallBoolean(fo, "versioned");        
155                                                 String fpath = unmarshallString(fo, "path");                                            
156                                                 fpath = URL.decodeComponent(fpath);
157                                                 Integer fversion = null;
158                                                 if (fo.get("version") != null)
159                                                         fversion = new Integer(fo.get("version").toString());
160                                                 boolean fdeleted = unmarshallBoolean(fo, "deleted");
161                                                 Date fcreationDate = null;
162                                                 if (fo.get("creationDate") != null)
163                                                         fcreationDate = new Date(new Long(fo.get("creationDate").toString()));
164                                                 Date fmodificationDate = null;
165                                                 if (fo.get("modificationDate") != null)
166                                                         fmodificationDate = new Date(new Long(fo.get("modificationDate").toString()));
167                                                 String furi = unmarshallString(fo,"uri");
168                                                 Long fsize = 0L;
169                                                 if(fo.get("size") != null)
170                                                         fsize = new Long(fo.get("size").toString());
171                                                 filePaths.add(furi);
172                                                 FileResource fs = new FileResource(furi);
173                                                 fs.setName(fname);
174                                                 fs.setOwner(fowner);
175                                                 fs.setVersion(fversion);
176                                                 fs.setContentLength(fsize);
177                                                 fs.setDeleted(fdeleted);
178                                                 fs.setPath(fpath);
179                                                 fs.setCreationDate(fcreationDate);
180                                                 fs.setModificationDate(fmodificationDate);
181                                                 fs.setContentType(fcontent);
182                                                 fs.setShared(fshared);
183                                                 fs.setVersioned(fversioned);
184                                                 files.add(fs);
185                                         }
186                                 }
187                 }
188         }
189
190         public List<FolderResource> getTrashedFolders(){
191                 List<FolderResource> res = new ArrayList<FolderResource>();
192                 for(String s : subfolderPaths){
193                         String[] pathElements =  s.split("/");
194                         FolderResource tr = new FolderResource(s);
195                         tr.setName(URL.decodeComponent(pathElements[pathElements.length-1]));
196                         res.add(tr);
197                 }
198                 return res;
199         }
200
201         @Override
202         public String getLastModifiedSince() {
203                 return null;
204         }
205         @Override
206         public String constructUri(TreeItem treeItem, String path){
207                 String constructedUri = "Files/"+ getUri().substring(path.lastIndexOf("/")+1);
208                 if (!constructedUri.endsWith("/"))
209                         constructedUri += "/";
210                 return constructedUri;
211         }
212 }