2 * Copyright 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
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.
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.
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/>.
20 package gr.ebs.gss.client.rest.resource;
22 import java.util.ArrayList;
23 import java.util.Date;
24 import java.util.HashSet;
25 import java.util.LinkedList;
26 import java.util.List;
29 import com.google.gwt.core.client.GWT;
30 import com.google.gwt.http.client.URL;
31 import com.google.gwt.json.client.JSONArray;
32 import com.google.gwt.json.client.JSONObject;
33 import com.google.gwt.json.client.JSONParser;
38 public class FolderResource extends RestResource {
40 public FolderResource(String aUri) {
54 Date modificationDate;
56 List<String> filePaths = new LinkedList<String>();
58 List<String> subfolderPaths = new LinkedList<String>();
60 Set<PermissionHolder> permissions = new HashSet<PermissionHolder>();
62 List<FolderResource> folders = new ArrayList<FolderResource>();
64 List<FileResource> files = new ArrayList<FileResource>();
68 boolean deleted = false;
70 boolean needsExpanding = false;
75 * Modify the parentName.
77 * @param aParentName the parentName to set
79 public void setParentName(String aParentName) {
80 parentName = aParentName;
88 public String getName() {
95 * @param aName the name to set
97 public void setName(String aName) {
102 * Retrieve the owner.
106 public String getOwner() {
113 * @param anOwner the owner to set
115 public void setOwner(String anOwner) {
120 * Retrieve the createdBy.
122 * @return the createdBy
124 public String getCreatedBy() {
129 * Modify the createdBy.
131 * @param aCreatedBy the createdBy to set
133 public void setCreatedBy(String aCreatedBy) {
134 createdBy = aCreatedBy;
138 * Retrieve the modifiedBy.
140 * @return the modifiedBy
142 public String getModifiedBy() {
147 * Modify the modifiedBy.
149 * @param aModifiedBy the modifiedBy to set
151 public void setModifiedBy(String aModifiedBy) {
152 modifiedBy = aModifiedBy;
156 * Retrieve the creationDate.
158 * @return the creationDate
160 public Date getCreationDate() {
165 * Modify the creationDate.
167 * @param aCreationDate the creationDate to set
169 public void setCreationDate(Date aCreationDate) {
170 creationDate = aCreationDate;
174 * Retrieve the modificationDate.
176 * @return the modificationDate
178 public Date getModificationDate() {
179 return modificationDate;
183 * Modify the modificationDate.
185 * @param aModificationDate the modificationDate to set
187 public void setModificationDate(Date aModificationDate) {
188 modificationDate = aModificationDate;
192 * Retrieve the filePaths.
194 * @return the filePaths
196 public List<String> getFilePaths() {
201 * Modify the filePaths.
203 * @param newFilePaths the filePaths to set
205 public void setFilePaths(List<String> newFilePaths) {
206 filePaths = newFilePaths;
210 * Retrieve the subfolderPaths.
212 * @return the subfolderPaths
214 public List<String> getSubfolderPaths() {
215 return subfolderPaths;
219 * Modify the subfolderPaths.
221 * @param newSubfolderPaths the subfolderPaths to set
223 public void setSubfolderPaths(List<String> newSubfolderPaths) {
224 subfolderPaths = newSubfolderPaths;
228 * Retrieve the permissions.
230 * @return the permissions
232 public Set<PermissionHolder> getPermissions() {
237 * Modify the permissions.
239 * @param newPermissions the permissions to set
241 public void setPermissions(Set<PermissionHolder> newPermissions) {
242 permissions = newPermissions;
246 * Retrieve the deleted.
248 * @return the deleted
250 public boolean isDeleted() {
255 * Modify the deleted.
257 * @param newDeleted the deleted to set
259 public void setDeleted(boolean newDeleted) {
260 deleted = newDeleted;
263 public void removeSubfolderPath(String spath) {
264 if (subfolderPaths.remove(spath))
266 else if (subfolderPaths.remove(spath + "/"))
269 subfolderPaths.remove(spath.substring(0, spath.length() - 1));
273 * Retrieve the folders.
275 * @return the folders
277 public List<FolderResource> getFolders() {
282 * Modify the folders.
284 * @param newFolders the folders to set
286 public void setFolders(List<FolderResource> newFolders) {
287 folders = newFolders;
291 * Retrieve the files.
295 public List<FileResource> getFiles() {
302 * @param newFiles the files to set
304 public void setFiles(List<FileResource> newFiles) {
309 * Retrieve the parentURI.
311 * @return the parentURI
313 public String getParentURI() {
318 * Modify the parentURI.
320 * @param aParentURI the parentURI to set
322 public void setParentURI(String aParentURI) {
323 parentURI = aParentURI;
327 public void createFromJSON(String text) {
328 JSONObject json = (JSONObject) JSONParser.parse(text);
329 name = unmarshallString(json, "name");
330 owner = unmarshallString(json, "owner");
331 deleted = unmarshallBoolean(json, "deleted");
333 GWT.log("FOUND A DELETED FOLDER:" + name, null);
335 if (json.get("parent") != null) {
336 JSONObject parent = json.get("parent").isObject();
337 parentURI = unmarshallString(parent, "uri");
338 parentName = unmarshallString(parent, "name");
339 if(parentName != null)
340 parentName = URL.decodeComponent(parentName);
343 if (json.get("permissions") != null) {
344 JSONArray perm = json.get("permissions").isArray();
346 for (int i = 0; i < perm.size(); i++) {
347 JSONObject obj = perm.get(i).isObject();
349 PermissionHolder permission = new PermissionHolder();
350 if (obj.get("user") != null)
351 permission.setUser(unmarshallString(obj, "user"));
352 if (obj.get("group") != null)
353 permission.setGroup(URL.decodeComponent(unmarshallString(obj, "group")));
354 permission.setRead(unmarshallBoolean(obj, "read"));
355 permission.setWrite(unmarshallBoolean(obj, "write"));
356 permission.setModifyACL(unmarshallBoolean(obj, "modifyACL"));
357 permissions.add(permission);
361 if (json.get("folders") != null) {
362 JSONArray subs = json.get("folders").isArray();
364 for (int i = 0; i < subs.size(); i++) {
365 JSONObject so = subs.get(i).isObject();
367 String subUri = unmarshallString(so, "uri");
368 String subName = unmarshallString(so, "name");
369 if (subUri != null && subName != null) {
370 if (!subUri.endsWith("/"))
371 subUri = subUri + "/";
372 FolderResource sub = new FolderResource(subUri);
373 sub.setName(subName);
374 sub.setNeedsExpanding(true);
376 subfolderPaths.add(subUri);
381 if (json.get("files") != null) {
382 JSONArray subs = json.get("files").isArray();
384 for (int i = 0; i < subs.size(); i++) {
385 JSONObject fo = subs.get(i).isObject();
387 String fname = unmarshallString(fo, "name");
388 String fowner = unmarshallString(fo, "owner");
389 String fcontent = unmarshallString(fo, "content");
390 String fpath = unmarshallString(fo, "path");
391 fpath = URL.decodeComponent(fpath);
392 Integer fversion = null;
393 if (fo.get("version") != null)
394 fversion = new Integer(fo.get("version").toString());
395 boolean fdeleted = unmarshallBoolean(fo, "deleted");
396 Date fcreationDate = null;
397 if (fo.get("creationDate") != null)
398 fcreationDate = new Date(new Long(fo.get("creationDate").toString()));
399 String furi = unmarshallString(fo, "uri");
401 if (fo.get("size") != null)
402 fsize = new Long(fo.get("size").toString());
404 FileResource fs = new FileResource(furi);
408 fs.setVersion(fversion);
409 fs.setContentLength(fsize);
410 fs.setDeleted(fdeleted);
411 fs.setCreationDate(fcreationDate);
412 fs.setContentType(fcontent);
417 if (json.get("creationDate") != null)
418 creationDate = new Date(new Long(json.get("creationDate").toString()));
419 if (json.get("modificationDate") != null)
420 modificationDate = new Date(new Long(json.get("modificationDate").toString()));
423 public String getParentName(){
428 * Retrieve the needsExpanding.
430 * @return the needsExpanding
432 public boolean isNeedsExpanding() {
433 return needsExpanding;
437 * Modify the needsExpanding.
439 * @param newNeedsExpanding the needsExpanding to set
441 public void setNeedsExpanding(boolean newNeedsExpanding) {
442 needsExpanding = newNeedsExpanding;
445 public boolean isShared(){
447 for(PermissionHolder perm : permissions){
448 if(perm.getUser() != null && !owner.equals(perm.getUser()))
450 if(perm.getGroup() != null)