root / web_client / src / gr / grnet / pithos / web / client / rest / resource / TrashResource.java @ 9e8e14e4
History | View | Annotate | Download (5.1 kB)
1 | ab1eb3f8 | Christos Stathis | /*
|
---|---|---|---|
2 | 6dd67d1c | Christos Stathis | * Copyright (c) 2011 Greek Research and Technology Network
|
3 | ab1eb3f8 | Christos Stathis | */
|
4 | ab1eb3f8 | Christos Stathis | package gr.grnet.pithos.web.client.rest.resource; |
5 | ab1eb3f8 | Christos Stathis | |
6 | ab1eb3f8 | Christos Stathis | import java.util.ArrayList; |
7 | ab1eb3f8 | Christos Stathis | import java.util.Date; |
8 | ab1eb3f8 | Christos Stathis | import java.util.LinkedList; |
9 | ab1eb3f8 | Christos Stathis | import java.util.List; |
10 | ab1eb3f8 | Christos Stathis | |
11 | ab1eb3f8 | Christos Stathis | import com.google.gwt.http.client.URL; |
12 | ab1eb3f8 | Christos Stathis | import com.google.gwt.json.client.JSONArray; |
13 | ab1eb3f8 | Christos Stathis | import com.google.gwt.json.client.JSONObject; |
14 | ab1eb3f8 | Christos Stathis | import com.google.gwt.json.client.JSONParser; |
15 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.TreeItem; |
16 | ab1eb3f8 | Christos Stathis | |
17 | ab1eb3f8 | Christos Stathis | |
18 | ab1eb3f8 | Christos Stathis | public class TrashResource extends RestResource{ |
19 | ab1eb3f8 | Christos Stathis | |
20 | ab1eb3f8 | Christos Stathis | public TrashResource(String aUri) { |
21 | ab1eb3f8 | Christos Stathis | super(aUri);
|
22 | ab1eb3f8 | Christos Stathis | } |
23 | ab1eb3f8 | Christos Stathis | |
24 | ab1eb3f8 | Christos Stathis | List<String> filePaths = new LinkedList<String>(); |
25 | ab1eb3f8 | Christos Stathis | List<String> subfolderPaths = new LinkedList<String>(); |
26 | ab1eb3f8 | Christos Stathis | List<FolderResource> folders = new ArrayList<FolderResource>(); |
27 | ab1eb3f8 | Christos Stathis | List<FileResource> files = new ArrayList<FileResource>(); |
28 | ab1eb3f8 | Christos Stathis | |
29 | ab1eb3f8 | Christos Stathis | /**
|
30 | ab1eb3f8 | Christos Stathis | * Retrieve the file paths.
|
31 | ab1eb3f8 | Christos Stathis | *
|
32 | ab1eb3f8 | Christos Stathis | * @return the file paths
|
33 | ab1eb3f8 | Christos Stathis | */
|
34 | ab1eb3f8 | Christos Stathis | public List<String> getFilePaths() { |
35 | ab1eb3f8 | Christos Stathis | return filePaths;
|
36 | ab1eb3f8 | Christos Stathis | } |
37 | ab1eb3f8 | Christos Stathis | |
38 | ab1eb3f8 | Christos Stathis | /**
|
39 | ab1eb3f8 | Christos Stathis | * Modify the file paths.
|
40 | ab1eb3f8 | Christos Stathis | *
|
41 | ab1eb3f8 | Christos Stathis | * @param newFilePaths the file paths to set
|
42 | ab1eb3f8 | Christos Stathis | */
|
43 | ab1eb3f8 | Christos Stathis | public void setFilePaths(List<String> newFilePaths) { |
44 | ab1eb3f8 | Christos Stathis | filePaths = newFilePaths; |
45 | ab1eb3f8 | Christos Stathis | } |
46 | ab1eb3f8 | Christos Stathis | |
47 | ab1eb3f8 | Christos Stathis | /**
|
48 | ab1eb3f8 | Christos Stathis | * Retrieve the subfolder paths.
|
49 | ab1eb3f8 | Christos Stathis | *
|
50 | ab1eb3f8 | Christos Stathis | * @return the subfolder paths
|
51 | ab1eb3f8 | Christos Stathis | */
|
52 | ab1eb3f8 | Christos Stathis | public List<String> getSubfolderPaths() { |
53 | ab1eb3f8 | Christos Stathis | return subfolderPaths;
|
54 | ab1eb3f8 | Christos Stathis | } |
55 | ab1eb3f8 | Christos Stathis | |
56 | ab1eb3f8 | Christos Stathis | /**
|
57 | ab1eb3f8 | Christos Stathis | * Modify the subfolder paths.
|
58 | ab1eb3f8 | Christos Stathis | *
|
59 | ab1eb3f8 | Christos Stathis | * @param newSubfolderPaths the subfolders to set
|
60 | ab1eb3f8 | Christos Stathis | */
|
61 | ab1eb3f8 | Christos Stathis | public void setSubfolderPaths(List<String> newSubfolderPaths) { |
62 | ab1eb3f8 | Christos Stathis | subfolderPaths = newSubfolderPaths; |
63 | ab1eb3f8 | Christos Stathis | } |
64 | ab1eb3f8 | Christos Stathis | |
65 | ab1eb3f8 | Christos Stathis | /**
|
66 | ab1eb3f8 | Christos Stathis | * Retrieve the folders.
|
67 | ab1eb3f8 | Christos Stathis | *
|
68 | ab1eb3f8 | Christos Stathis | * @return the folders
|
69 | ab1eb3f8 | Christos Stathis | */
|
70 | ab1eb3f8 | Christos Stathis | public List<FolderResource> getFolders() { |
71 | ab1eb3f8 | Christos Stathis | return folders;
|
72 | ab1eb3f8 | Christos Stathis | } |
73 | ab1eb3f8 | Christos Stathis | |
74 | ab1eb3f8 | Christos Stathis | /**
|
75 | ab1eb3f8 | Christos Stathis | * Modify the folders.
|
76 | ab1eb3f8 | Christos Stathis | *
|
77 | ab1eb3f8 | Christos Stathis | * @param newFolders the folders to set
|
78 | ab1eb3f8 | Christos Stathis | */
|
79 | ab1eb3f8 | Christos Stathis | public void setFolders(List<FolderResource> newFolders) { |
80 | ab1eb3f8 | Christos Stathis | folders = newFolders; |
81 | ab1eb3f8 | Christos Stathis | } |
82 | ab1eb3f8 | Christos Stathis | |
83 | ab1eb3f8 | Christos Stathis | /**
|
84 | ab1eb3f8 | Christos Stathis | * Retrieve the files.
|
85 | ab1eb3f8 | Christos Stathis | *
|
86 | ab1eb3f8 | Christos Stathis | * @return the files
|
87 | ab1eb3f8 | Christos Stathis | */
|
88 | ab1eb3f8 | Christos Stathis | public List<FileResource> getFiles() { |
89 | ab1eb3f8 | Christos Stathis | return files;
|
90 | ab1eb3f8 | Christos Stathis | } |
91 | ab1eb3f8 | Christos Stathis | |
92 | ab1eb3f8 | Christos Stathis | /**
|
93 | ab1eb3f8 | Christos Stathis | * Modify the files.
|
94 | ab1eb3f8 | Christos Stathis | *
|
95 | ab1eb3f8 | Christos Stathis | * @param newFiles the files to set
|
96 | ab1eb3f8 | Christos Stathis | */
|
97 | ab1eb3f8 | Christos Stathis | public void setFiles(List<FileResource> newFiles) { |
98 | ab1eb3f8 | Christos Stathis | files = newFiles; |
99 | ab1eb3f8 | Christos Stathis | } |
100 | ab1eb3f8 | Christos Stathis | |
101 | ab1eb3f8 | Christos Stathis | @Override
|
102 | ab1eb3f8 | Christos Stathis | public void createFromJSON(String text) { |
103 | ab1eb3f8 | Christos Stathis | JSONObject json = (JSONObject) JSONParser.parse(text); |
104 | ab1eb3f8 | Christos Stathis | if (json.get("folders") != null) { |
105 | ab1eb3f8 | Christos Stathis | JSONArray subs = json.get("folders").isArray();
|
106 | ab1eb3f8 | Christos Stathis | if (subs != null) |
107 | ab1eb3f8 | Christos Stathis | for (int i = 0; i < subs.size(); i++) { |
108 | ab1eb3f8 | Christos Stathis | JSONObject so = subs.get(i).isObject(); |
109 | ab1eb3f8 | Christos Stathis | if (so != null) { |
110 | ab1eb3f8 | Christos Stathis | String subUri = unmarshallString(so, "uri"); |
111 | ab1eb3f8 | Christos Stathis | String subName = unmarshallString(so, "name"); |
112 | ab1eb3f8 | Christos Stathis | subName = URL.decodeComponent(subName);
|
113 | ab1eb3f8 | Christos Stathis | if (subUri != null && subName != null) { |
114 | ab1eb3f8 | Christos Stathis | if (!subUri.endsWith("/")) |
115 | ab1eb3f8 | Christos Stathis | subUri = subUri + "/";
|
116 | ab1eb3f8 | Christos Stathis | FolderResource sub = new FolderResource(subUri);
|
117 | ab1eb3f8 | Christos Stathis | sub.setName(subName); |
118 | ab1eb3f8 | Christos Stathis | sub.setNeedsExpanding(true);
|
119 | ab1eb3f8 | Christos Stathis | folders.add(sub); |
120 | ab1eb3f8 | Christos Stathis | subfolderPaths.add(subUri); |
121 | ab1eb3f8 | Christos Stathis | } |
122 | ab1eb3f8 | Christos Stathis | } |
123 | ab1eb3f8 | Christos Stathis | } |
124 | ab1eb3f8 | Christos Stathis | } |
125 | ab1eb3f8 | Christos Stathis | if (json.get("files") != null) { |
126 | ab1eb3f8 | Christos Stathis | JSONArray subs = json.get("files").isArray();
|
127 | ab1eb3f8 | Christos Stathis | if (subs != null) |
128 | ab1eb3f8 | Christos Stathis | for (int i = 0; i < subs.size(); i++) { |
129 | ab1eb3f8 | Christos Stathis | JSONObject fo = subs.get(i).isObject(); |
130 | ab1eb3f8 | Christos Stathis | if (fo != null) { |
131 | ab1eb3f8 | Christos Stathis | String fname = unmarshallString(fo, "name"); |
132 | ab1eb3f8 | Christos Stathis | String fowner = unmarshallString(fo, "owner"); |
133 | ab1eb3f8 | Christos Stathis | String fcontent = unmarshallString(fo, "content"); |
134 | ab1eb3f8 | Christos Stathis | Boolean fshared = unmarshallBoolean(fo, "shared"); |
135 | ab1eb3f8 | Christos Stathis | Boolean fversioned = unmarshallBoolean(fo, "versioned"); |
136 | ab1eb3f8 | Christos Stathis | String fpath = unmarshallString(fo, "path"); |
137 | ab1eb3f8 | Christos Stathis | fpath = URL.decodeComponent(fpath);
|
138 | ab1eb3f8 | Christos Stathis | Integer fversion = null; |
139 | ab1eb3f8 | Christos Stathis | if (fo.get("version") != null) |
140 | ab1eb3f8 | Christos Stathis | fversion = new Integer(fo.get("version").toString()); |
141 | ab1eb3f8 | Christos Stathis | boolean fdeleted = unmarshallBoolean(fo, "deleted"); |
142 | ab1eb3f8 | Christos Stathis | Date fcreationDate = null; |
143 | ab1eb3f8 | Christos Stathis | if (fo.get("creationDate") != null) |
144 | ab1eb3f8 | Christos Stathis | fcreationDate = new Date(new Long(fo.get("creationDate").toString())); |
145 | ab1eb3f8 | Christos Stathis | Date fmodificationDate = null; |
146 | ab1eb3f8 | Christos Stathis | if (fo.get("modificationDate") != null) |
147 | ab1eb3f8 | Christos Stathis | fmodificationDate = new Date(new Long(fo.get("modificationDate").toString())); |
148 | ab1eb3f8 | Christos Stathis | String furi = unmarshallString(fo,"uri"); |
149 | ab1eb3f8 | Christos Stathis | Long fsize = 0L; |
150 | ab1eb3f8 | Christos Stathis | if(fo.get("size") != null) |
151 | ab1eb3f8 | Christos Stathis | fsize = new Long(fo.get("size").toString()); |
152 | ab1eb3f8 | Christos Stathis | filePaths.add(furi); |
153 | ab1eb3f8 | Christos Stathis | FileResource fs = new FileResource(furi);
|
154 | ab1eb3f8 | Christos Stathis | fs.setName(fname); |
155 | ab1eb3f8 | Christos Stathis | fs.setOwner(fowner); |
156 | ab1eb3f8 | Christos Stathis | fs.setVersion(fversion); |
157 | ab1eb3f8 | Christos Stathis | fs.setContentLength(fsize); |
158 | ab1eb3f8 | Christos Stathis | fs.setDeleted(fdeleted); |
159 | ab1eb3f8 | Christos Stathis | fs.setPath(fpath); |
160 | ab1eb3f8 | Christos Stathis | fs.setCreationDate(fcreationDate); |
161 | ab1eb3f8 | Christos Stathis | fs.setModificationDate(fmodificationDate); |
162 | ab1eb3f8 | Christos Stathis | fs.setContentType(fcontent); |
163 | ab1eb3f8 | Christos Stathis | fs.setShared(fshared); |
164 | ab1eb3f8 | Christos Stathis | fs.setVersioned(fversioned); |
165 | ab1eb3f8 | Christos Stathis | files.add(fs); |
166 | ab1eb3f8 | Christos Stathis | } |
167 | ab1eb3f8 | Christos Stathis | } |
168 | ab1eb3f8 | Christos Stathis | } |
169 | ab1eb3f8 | Christos Stathis | } |
170 | ab1eb3f8 | Christos Stathis | |
171 | ab1eb3f8 | Christos Stathis | public List<FolderResource> getTrashedFolders(){ |
172 | ab1eb3f8 | Christos Stathis | List<FolderResource> res = new ArrayList<FolderResource>(); |
173 | ab1eb3f8 | Christos Stathis | for(String s : subfolderPaths){ |
174 | ab1eb3f8 | Christos Stathis | String[] pathElements = s.split("/"); |
175 | ab1eb3f8 | Christos Stathis | FolderResource tr = new FolderResource(s);
|
176 | ab1eb3f8 | Christos Stathis | tr.setName(URL.decodeComponent(pathElements[pathElements.length-1])); |
177 | ab1eb3f8 | Christos Stathis | res.add(tr); |
178 | ab1eb3f8 | Christos Stathis | } |
179 | ab1eb3f8 | Christos Stathis | return res;
|
180 | ab1eb3f8 | Christos Stathis | } |
181 | ab1eb3f8 | Christos Stathis | |
182 | ab1eb3f8 | Christos Stathis | @Override
|
183 | ab1eb3f8 | Christos Stathis | public String getLastModifiedSince() { |
184 | ab1eb3f8 | Christos Stathis | return null; |
185 | ab1eb3f8 | Christos Stathis | } |
186 | ab1eb3f8 | Christos Stathis | @Override
|
187 | ab1eb3f8 | Christos Stathis | public String constructUri(TreeItem treeItem, String path){ |
188 | ab1eb3f8 | Christos Stathis | String constructedUri = "Files/"+ getUri().substring(path.lastIndexOf("/")+1); |
189 | ab1eb3f8 | Christos Stathis | if (!constructedUri.endsWith("/")) |
190 | ab1eb3f8 | Christos Stathis | constructedUri += "/";
|
191 | ab1eb3f8 | Christos Stathis | return constructedUri;
|
192 | ab1eb3f8 | Christos Stathis | } |
193 | ab1eb3f8 | Christos Stathis | } |