2 * Copyright 2009, 2010 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/>.
19 package gr.ebs.gss.client.rest.resource;
21 import gr.ebs.gss.client.rest.MultipleGetCommand;
22 import gr.ebs.gss.client.rest.MultipleGetCommand.Cached;
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.LinkedList;
27 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;
34 import com.google.gwt.user.client.ui.TreeItem;
41 public class SharedResource extends RestResource{
43 public SharedResource(String aUri) {
47 List<String> filePaths = new LinkedList<String>();
48 List<String> subfolderPaths = new LinkedList<String>();
49 List<FolderResource> folders = new ArrayList<FolderResource>();
51 List<FileResource> files = new ArrayList<FileResource>();
53 private boolean filesExpanded=false;
60 public List<String> getFilePaths() {
67 * @param newFilePaths the files to set
69 public void setFilePaths(List<String> newFilePaths) {
70 filePaths = newFilePaths;
74 * Retrieve the subfolders.
76 * @return the subfolders
78 public List<String> getSubfolderPaths() {
79 return subfolderPaths;
83 * Modify the subfolder paths.
85 * @param newSubfolderPaths the subfolder paths to set
87 public void setSubfolderPaths(List<String> newSubfolderPaths) {
88 subfolderPaths = newSubfolderPaths;
92 * Retrieve the folders.
96 public List<FolderResource> getFolders() {
101 * Modify the folders.
103 * @param newFolders the folders to set
105 public void setFolders(List<FolderResource> newFolders) {
106 folders = newFolders;
110 * Retrieve the files.
114 public List<FileResource> getFiles() {
121 * @param newFiles the files to set
123 public void setFiles(List<FileResource> newFiles) {
128 public void createFromJSON(String text) {
129 JSONObject json = (JSONObject) JSONParser.parse(text);
130 if (json.get("folders") != null) {
131 JSONArray subs = json.get("folders").isArray();
133 for (int i = 0; i < subs.size(); i++) {
134 JSONObject so = subs.get(i).isObject();
136 String subUri = unmarshallString(so, "uri");
137 String subName = unmarshallString(so, "name");
138 if (subUri != null && subName != null) {
139 if (!subUri.endsWith("/"))
140 subUri = subUri + "/";
141 FolderResource sub = new FolderResource(subUri);
142 sub.setName(subName);
143 sub.setNeedsExpanding(true);
145 subfolderPaths.add(subUri);
150 if (json.get("files") != null) {
151 JSONArray subs = json.get("files").isArray();
153 for (int i = 0; i < subs.size(); i++) {
154 JSONObject fo = subs.get(i).isObject();
156 String fname = unmarshallString(fo, "name");
157 String fowner = unmarshallString(fo, "owner");
158 String fcontent = unmarshallString(fo, "content");
159 Integer fversion = null;
160 if (fo.get("version") != null)
161 fversion = new Integer(fo.get("version").toString());
162 boolean fdeleted = unmarshallBoolean(fo, "deleted");
163 Date fcreationDate = null;
164 if (fo.get("creationDate") != null)
165 fcreationDate = new Date(new Long(fo.get("creationDate").toString()));
166 Date fmodificationDate = null;
167 if (fo.get("modificationDate") != null)
168 fmodificationDate = new Date(new Long(fo.get("modificationDate").toString()));
169 String furi = unmarshallString(fo,"uri");
171 if(fo.get("size") != null)
172 fsize = new Long(fo.get("size").toString());
174 String fpath = unmarshallString(fo, "path");
175 fpath = URL.decodeComponent(fpath);
176 FileResource fs = new FileResource(furi);
180 fs.setVersion(fversion);
181 fs.setContentLength(fsize);
182 fs.setDeleted(fdeleted);
183 fs.setCreationDate(fcreationDate);
184 fs.setModificationDate(fmodificationDate);
185 fs.setContentType(fcontent);
192 public List<String> getRootSharedFiles(){
193 List<String> res = new ArrayList<String>();
194 for(String f : getFilePaths()){
195 boolean contained = false;
196 for(String fo : getSubfolderPaths())
206 public String getLastModifiedSince() {
210 public MultipleGetCommand.Cached[] getFileCache(){
211 if(getFilePaths().size() != getFiles().size()){
212 GWT.log("MISMATCH IN PATH AND FILES SIZE", null);
217 MultipleGetCommand.Cached[] result = new MultipleGetCommand.Cached[getFilePaths().size()];
218 for(int i=0; i<getFiles().size();i++){
219 FileResource r = getFiles().get(i);
220 Cached c = new Cached();
228 public void setFilesExpanded(boolean newFilesExpanded) {
229 filesExpanded = newFilesExpanded;
233 public String constructUri(TreeItem treeItem, String path){
234 String constructedUri = "Files/"+ getUri().substring(path.lastIndexOf("/")+1);
235 return constructedUri;