2 * Copyright 2011 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and
31 * documentation are those of the authors and should not be
32 * interpreted as representing official policies, either expressed
33 * or implied, of GRNET S.A.
36 package gr.grnet.pithos.web.client.rest.resource;
38 import gr.grnet.pithos.web.client.DisplayHelper;
39 import gr.grnet.pithos.web.client.Pithos;
40 import gr.grnet.pithos.web.client.rest.MultipleGetCommand;
41 import gr.grnet.pithos.web.client.rest.MultipleGetCommand.Cached;
43 import java.util.ArrayList;
44 import java.util.Date;
45 import java.util.HashSet;
46 import java.util.LinkedList;
47 import java.util.List;
50 import com.google.gwt.core.client.GWT;
51 import com.google.gwt.http.client.URL;
52 import com.google.gwt.json.client.JSONArray;
53 import com.google.gwt.json.client.JSONObject;
54 import com.google.gwt.json.client.JSONParser;
55 import com.google.gwt.user.client.ui.TreeItem;
57 public class FolderResource extends RestResource {
59 public FolderResource(String aUri) {
73 Date modificationDate;
75 List<String> filePaths = new LinkedList<String>();
77 List<String> subfolderPaths = new LinkedList<String>();
79 Set<PermissionHolder> permissions = new HashSet<PermissionHolder>();
81 List<FolderResource> folders = new ArrayList<FolderResource>();
83 List<FileResource> files = new ArrayList<FileResource>();
87 boolean deleted = false;
89 boolean needsExpanding = false;
93 private boolean filesExpanded=false;
106 * @param shared the shared to set
108 public void setShared(Boolean shared) {
109 this.shared = shared;
113 * Modify the parentName.
115 * @param aParentName the parentName to set
117 public void setParentName(String aParentName) {
118 parentName = aParentName;
127 public String getName() {
134 * @param aName the name to set
136 public void setName(String aName) {
141 * Retrieve the owner.
145 public String getOwner() {
152 * @param anOwner the owner to set
154 public void setOwner(String anOwner) {
159 * Retrieve the createdBy.
161 * @return the createdBy
163 public String getCreatedBy() {
168 * Modify the createdBy.
170 * @param aCreatedBy the createdBy to set
172 public void setCreatedBy(String aCreatedBy) {
173 createdBy = aCreatedBy;
177 * Retrieve the modifiedBy.
179 * @return the modifiedBy
181 public String getModifiedBy() {
186 * Modify the modifiedBy.
188 * @param aModifiedBy the modifiedBy to set
190 public void setModifiedBy(String aModifiedBy) {
191 modifiedBy = aModifiedBy;
195 * Retrieve the creationDate.
197 * @return the creationDate
199 public Date getCreationDate() {
204 * Modify the creationDate.
206 * @param aCreationDate the creationDate to set
208 public void setCreationDate(Date aCreationDate) {
209 creationDate = aCreationDate;
213 * Retrieve the modificationDate.
215 * @return the modificationDate
217 public Date getModificationDate() {
218 return modificationDate;
222 * Modify the modificationDate.
224 * @param aModificationDate the modificationDate to set
226 public void setModificationDate(Date aModificationDate) {
227 modificationDate = aModificationDate;
231 * Retrieve the filePaths.
233 * @return the filePaths
235 public List<String> getFilePaths() {
240 * Modify the filePaths.
242 * @param newFilePaths the filePaths to set
244 public void setFilePaths(List<String> newFilePaths) {
245 filePaths = newFilePaths;
249 * Retrieve the subfolderPaths.
251 * @return the subfolderPaths
253 public List<String> getSubfolderPaths() {
254 return subfolderPaths;
258 * Modify the subfolderPaths.
260 * @param newSubfolderPaths the subfolderPaths to set
262 public void setSubfolderPaths(List<String> newSubfolderPaths) {
263 subfolderPaths = newSubfolderPaths;
267 * Retrieve the permissions.
269 * @return the permissions
271 public Set<PermissionHolder> getPermissions() {
276 * Modify the permissions.
278 * @param newPermissions the permissions to set
280 public void setPermissions(Set<PermissionHolder> newPermissions) {
281 permissions = newPermissions;
285 * Retrieve the deleted.
287 * @return the deleted
289 public boolean isDeleted() {
294 * Modify the deleted.
296 * @param newDeleted the deleted to set
298 public void setDeleted(boolean newDeleted) {
299 deleted = newDeleted;
302 public void removeSubfolderPath(String spath) {
303 if (subfolderPaths.remove(spath))
305 else if (subfolderPaths.remove(spath + "/"))
308 subfolderPaths.remove(spath.substring(0, spath.length() - 1));
312 * Retrieve the folders.
314 * @return the folders
316 public List<FolderResource> getFolders() {
321 * Modify the folders.
323 * @param newFolders the folders to set
325 public void setFolders(List<FolderResource> newFolders) {
326 folders = newFolders;
330 * Retrieve the files.
334 public List<FileResource> getFiles() {
341 * @param newFiles the files to set
343 public void setFiles(List<FileResource> newFiles) {
348 * Retrieve the parentURI.
350 * @return the parentURI
352 public String getParentURI() {
357 * Modify the parentURI.
359 * @param aParentURI the parentURI to set
361 public void setParentURI(String aParentURI) {
362 parentURI = aParentURI;
366 public void createFromJSON(String text) {
367 JSONObject json = (JSONObject) JSONParser.parse(text);
368 name = unmarshallString(json, "name");
369 owner = unmarshallString(json, "owner");
370 createdBy = unmarshallString(json, "createdBy");
371 modifiedBy = unmarshallString(json, "modifiedBy");
372 deleted = unmarshallBoolean(json, "deleted");
373 shared = unmarshallBoolean(json, "shared");
374 readForAll = unmarshallBoolean(json, "readForAll");
376 GWT.log("FOUND A DELETED FOLDER:" + name, null);
378 if (json.get("parent") != null) {
379 JSONObject parent = json.get("parent").isObject();
380 parentURI = unmarshallString(parent, "uri");
381 parentName = unmarshallString(parent, "name");
384 if (json.get("permissions") != null) {
385 JSONArray perm = json.get("permissions").isArray();
387 for (int i = 0; i < perm.size(); i++) {
388 JSONObject obj = perm.get(i).isObject();
390 PermissionHolder permission = new PermissionHolder();
391 if (obj.get("user") != null)
392 permission.setUser(unmarshallString(obj, "user"));
393 if (obj.get("group") != null)
394 permission.setGroup(URL.decodeComponent(unmarshallString(obj, "group")));
395 permission.setRead(unmarshallBoolean(obj, "read"));
396 permission.setWrite(unmarshallBoolean(obj, "write"));
397 permission.setModifyACL(unmarshallBoolean(obj, "modifyACL"));
398 permissions.add(permission);
402 if (json.get("folders") != null) {
403 JSONArray subs = json.get("folders").isArray();
405 for (int i = 0; i < subs.size(); i++) {
406 JSONObject so = subs.get(i).isObject();
408 String subUri = unmarshallString(so, "uri");
409 String subName = unmarshallString(so, "name");
410 if (subUri != null && subName != null) {
411 if (!subUri.endsWith("/"))
412 subUri = subUri + "/";
413 FolderResource sub = new FolderResource(subUri);
414 sub.setName(subName);
415 sub.setParentURI(uri);
416 sub.setParentName(name);
417 sub.setNeedsExpanding(true);
419 subfolderPaths.add(subUri);
424 if (json.get("files") != null) {
425 JSONArray subs = json.get("files").isArray();
427 for (int i = 0; i < subs.size(); i++) {
428 JSONObject fo = subs.get(i).isObject();
430 String fname = unmarshallString(fo, "name");
431 String fowner = unmarshallString(fo, "owner");
432 String fcontent = unmarshallString(fo, "content");
433 String fpath = unmarshallString(fo, "path");
434 Boolean fshared = unmarshallBoolean(fo, "shared");
435 fpath = URL.decodeComponent(fpath);
436 Integer fversion = null;
437 if (fo.get("version") != null)
438 fversion = new Integer(fo.get("version").toString());
439 boolean fdeleted = unmarshallBoolean(fo, "deleted");
440 Date fcreationDate = null;
441 if (fo.get("creationDate") != null)
442 fcreationDate = new Date(new Long(fo.get("creationDate").toString()));
443 Date fmodificationDate = null;
444 if (fo.get("modificationDate") != null)
445 fmodificationDate = new Date(new Long(fo.get("modificationDate").toString()));
446 String furi = unmarshallString(fo, "uri");
448 if (fo.get("size") != null)
449 fsize = new Long(fo.get("size").toString());
451 FileResource fs = new FileResource(furi);
455 fs.setVersioned(unmarshallBoolean(fo, "versioned"));
456 fs.setVersion(fversion);
457 fs.setContentLength(fsize);
458 fs.setDeleted(fdeleted);
459 fs.setShared(fshared);
460 fs.setCreationDate(fcreationDate);
461 fs.setModificationDate(fmodificationDate);
462 fs.setContentType(fcontent);
467 if (json.get("creationDate") != null)
468 creationDate = new Date(new Long(json.get("creationDate").toString()));
469 if (json.get("modificationDate") != null)
470 modificationDate = new Date(new Long(json.get("modificationDate").toString()));
473 public String getParentName(){
478 * Retrieve the needsExpanding.
480 * @return the needsExpanding
482 public boolean isNeedsExpanding() {
483 return needsExpanding;
487 * Modify the needsExpanding.
489 * @param newNeedsExpanding the needsExpanding to set
491 public void setNeedsExpanding(boolean newNeedsExpanding) {
492 needsExpanding = newNeedsExpanding;
495 public boolean isShared(){
500 public String getLastModifiedSince() {
501 if(modificationDate != null)
502 return getDate(modificationDate.getTime());
506 public MultipleGetCommand.Cached[] getCache(){
507 if(getSubfolderPaths().size() != getFolders().size()){
508 GWT.log("MISMATCH IN PATH AND FOLDERS SIZE", null);
511 MultipleGetCommand.Cached[] result = new MultipleGetCommand.Cached[getSubfolderPaths().size()];
512 for(int i=0; i<getFolders().size();i++){
513 FolderResource r = getFolders().get(i);
514 Cached c = new Cached();
522 public MultipleGetCommand.Cached[] getFileCache(){
523 if(getFilePaths().size() != getFiles().size()){
524 GWT.log("MISMATCH IN PATH AND FILES SIZE", null);
529 MultipleGetCommand.Cached[] result = new MultipleGetCommand.Cached[getFilePaths().size()];
530 for(int i=0; i<getFiles().size();i++){
531 FileResource r = getFiles().get(i);
532 Cached c = new Cached();
540 public void setFilesExpanded(boolean newFilesExpanded) {
541 filesExpanded = newFilesExpanded;
544 * this method constructs the partial path of a given TreeItem using it's text
546 * @param selectedItem the selectedItem to check
548 private String constructPartialPath(TreeItem selectedItem){
549 String result = DisplayHelper.trim(selectedItem.getText());
550 TreeItem parent = selectedItem.getParentItem();
551 while (!(DisplayHelper.trim(parent.getText()).equals("My Shared") || DisplayHelper.trim(parent.getText()).equals("Other's Shared")||DisplayHelper.trim(parent.getText()).equals("Trash"))){
552 result = DisplayHelper.trim(parent.getText()) + "/" + result;
553 if(result.equals("My Shared")||result.equals("Other's Shared")) return result;
554 parent = parent.getParentItem();
560 * examine whether a folder name like "Trash", "My Shared", "Other's Shared" is inside path
562 * @param selectedItem the selectedTreeItem to check
565 private boolean containsFolder(TreeItem selectedItem, String folderName){
566 TreeItem parent = selectedItem.getParentItem();
567 while (parent != null){
568 String parentItemText = parent.getText();
569 String parentItemTextTr = DisplayHelper.trim(parentItemText);
570 if(parentItemTextTr.equals(folderName)) return true;
571 parent = parent.getParentItem();
576 public String constructUri(TreeItem treeItem, String path){
577 String constructedUri = "";
578 if(containsFolder(treeItem, "My Shared")){
579 //case: selected folders below My Shared folder
580 String partialUri = constructPartialPath(treeItem);
581 constructedUri = constructedUri + "Files/shared/" + partialUri;
582 return constructedUri;
583 }else if(containsFolder(treeItem, "Other's Shared")){
584 //case: selected folders below Other's Shared folder
585 String partialPath = constructPartialPath(treeItem);
586 constructedUri = constructedUri + "Files/others/"+ partialPath;
587 return constructedUri;
589 else if(getParentURI()==null){
590 if(containsFolder(treeItem, "Trash")){
591 //case: selected folders below Trash folder
592 String partialUri = constructPartialPath(treeItem);
593 constructedUri = constructedUri + "Files/trash/" + partialUri;
594 return constructedUri;
596 //case: home folder is selected
597 constructedUri = constructedUri + "Files/files/" + getName();
598 return constructedUri;
600 else if(treeItem.getParentItem() == null){
601 //this is the case when the user uses the browser's forward arrow to navigate through other's
602 //shared folders and item.getParentItem is null only inside other's shared folder
603 String apiPath = Pithos.get().getApiPath();
604 String newPath = getParentURI().substring(apiPath.lastIndexOf("/"));
605 constructedUri = constructedUri + "Files"+ newPath + getName();
606 return constructedUri;
609 String finalUri = getParentURI().substring(path.lastIndexOf("/")) + getName();
610 constructedUri = constructedUri + "Files"+ finalUri;
611 return constructedUri;
617 * Retrieve the readForAll.
619 * @return the readForAll
621 public boolean isReadForAll() {
625 * Modify the readForAll.
627 * @param newReadForAll the readForAll to set
629 public void setReadForAll(boolean newReadForAll) {
630 readForAll = newReadForAll;
634 public int countNotDeletedSubfolders(){
636 for(FolderResource r : folders){