Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / rest / resource / SharedResource.java @ b98ec534

History | View | Annotate | Download (6.2 kB)

1 a52ea5e4 pastith
/*
2 b98ec534 Natasa Kapravelou
 * Copyright 2009, 2010 Electronic Business Systems Ltd.
3 a52ea5e4 pastith
 *
4 a52ea5e4 pastith
 * This file is part of GSS.
5 a52ea5e4 pastith
 *
6 a52ea5e4 pastith
 * GSS is free software: you can redistribute it and/or modify
7 a52ea5e4 pastith
 * it under the terms of the GNU General Public License as published by
8 a52ea5e4 pastith
 * the Free Software Foundation, either version 3 of the License, or
9 a52ea5e4 pastith
 * (at your option) any later version.
10 a52ea5e4 pastith
 *
11 a52ea5e4 pastith
 * GSS is distributed in the hope that it will be useful,
12 a52ea5e4 pastith
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 a52ea5e4 pastith
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 a52ea5e4 pastith
 * GNU General Public License for more details.
15 a52ea5e4 pastith
 *
16 a52ea5e4 pastith
 * You should have received a copy of the GNU General Public License
17 a52ea5e4 pastith
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18 a52ea5e4 pastith
 */
19 a52ea5e4 pastith
package gr.ebs.gss.client.rest.resource;
20 a52ea5e4 pastith
21 c5070ebe Natasa Kapravelou
import gr.ebs.gss.client.GSS;
22 757813cf Giannis Koutsoubos
import gr.ebs.gss.client.rest.MultipleGetCommand;
23 757813cf Giannis Koutsoubos
import gr.ebs.gss.client.rest.MultipleGetCommand.Cached;
24 757813cf Giannis Koutsoubos
25 a52ea5e4 pastith
import java.util.ArrayList;
26 a52ea5e4 pastith
import java.util.Date;
27 a52ea5e4 pastith
import java.util.LinkedList;
28 a52ea5e4 pastith
import java.util.List;
29 a52ea5e4 pastith
30 757813cf Giannis Koutsoubos
import com.google.gwt.core.client.GWT;
31 d6ba3052 koutsoub
import com.google.gwt.http.client.URL;
32 a52ea5e4 pastith
import com.google.gwt.json.client.JSONArray;
33 a52ea5e4 pastith
import com.google.gwt.json.client.JSONObject;
34 a52ea5e4 pastith
import com.google.gwt.json.client.JSONParser;
35 c5070ebe Natasa Kapravelou
import com.google.gwt.user.client.ui.TreeItem;
36 a52ea5e4 pastith
37 a52ea5e4 pastith
38 a52ea5e4 pastith
/**
39 a52ea5e4 pastith
 * @author kman
40 a52ea5e4 pastith
 *
41 a52ea5e4 pastith
 */
42 a52ea5e4 pastith
public class SharedResource extends RestResource{
43 a52ea5e4 pastith
44 555e8e59 pastith
        public SharedResource(String aUri) {
45 555e8e59 pastith
                super(aUri);
46 a52ea5e4 pastith
        }
47 a52ea5e4 pastith
48 a52ea5e4 pastith
        List<String> filePaths = new LinkedList<String>();
49 a52ea5e4 pastith
        List<String> subfolderPaths = new LinkedList<String>();
50 a52ea5e4 pastith
        List<FolderResource> folders = new ArrayList<FolderResource>();
51 a52ea5e4 pastith
52 a52ea5e4 pastith
        List<FileResource> files = new ArrayList<FileResource>();
53 a52ea5e4 pastith
54 757813cf Giannis Koutsoubos
        private boolean filesExpanded=false;
55 757813cf Giannis Koutsoubos
56 a52ea5e4 pastith
        /**
57 a52ea5e4 pastith
         * Retrieve the files.
58 a52ea5e4 pastith
         *
59 a52ea5e4 pastith
         * @return the files
60 a52ea5e4 pastith
         */
61 a52ea5e4 pastith
        public List<String> getFilePaths() {
62 a52ea5e4 pastith
                return filePaths;
63 a52ea5e4 pastith
        }
64 a52ea5e4 pastith
65 a52ea5e4 pastith
        /**
66 a52ea5e4 pastith
         * Modify the files.
67 a52ea5e4 pastith
         *
68 0e4865ee pastith
         * @param newFilePaths the files to set
69 a52ea5e4 pastith
         */
70 0e4865ee pastith
        public void setFilePaths(List<String> newFilePaths) {
71 0e4865ee pastith
                filePaths = newFilePaths;
72 a52ea5e4 pastith
        }
73 a52ea5e4 pastith
74 a52ea5e4 pastith
        /**
75 a52ea5e4 pastith
         * Retrieve the subfolders.
76 a52ea5e4 pastith
         *
77 a52ea5e4 pastith
         * @return the subfolders
78 a52ea5e4 pastith
         */
79 a52ea5e4 pastith
        public List<String> getSubfolderPaths() {
80 a52ea5e4 pastith
                return subfolderPaths;
81 a52ea5e4 pastith
        }
82 a52ea5e4 pastith
83 a52ea5e4 pastith
        /**
84 0e4865ee pastith
         * Modify the subfolder paths.
85 a52ea5e4 pastith
         *
86 0e4865ee pastith
         * @param newSubfolderPaths the subfolder paths to set
87 a52ea5e4 pastith
         */
88 0e4865ee pastith
        public void setSubfolderPaths(List<String> newSubfolderPaths) {
89 0e4865ee pastith
                subfolderPaths = newSubfolderPaths;
90 a52ea5e4 pastith
        }
91 a52ea5e4 pastith
92 a52ea5e4 pastith
        /**
93 a52ea5e4 pastith
         * Retrieve the folders.
94 a52ea5e4 pastith
         *
95 a52ea5e4 pastith
         * @return the folders
96 a52ea5e4 pastith
         */
97 a52ea5e4 pastith
        public List<FolderResource> getFolders() {
98 a52ea5e4 pastith
                return folders;
99 a52ea5e4 pastith
        }
100 a52ea5e4 pastith
101 a52ea5e4 pastith
        /**
102 a52ea5e4 pastith
         * Modify the folders.
103 a52ea5e4 pastith
         *
104 0e4865ee pastith
         * @param newFolders the folders to set
105 a52ea5e4 pastith
         */
106 0e4865ee pastith
        public void setFolders(List<FolderResource> newFolders) {
107 0e4865ee pastith
                folders = newFolders;
108 a52ea5e4 pastith
        }
109 a52ea5e4 pastith
110 a52ea5e4 pastith
        /**
111 a52ea5e4 pastith
         * Retrieve the files.
112 a52ea5e4 pastith
         *
113 a52ea5e4 pastith
         * @return the files
114 a52ea5e4 pastith
         */
115 a52ea5e4 pastith
        public List<FileResource> getFiles() {
116 a52ea5e4 pastith
                return files;
117 a52ea5e4 pastith
        }
118 a52ea5e4 pastith
119 a52ea5e4 pastith
        /**
120 a52ea5e4 pastith
         * Modify the files.
121 a52ea5e4 pastith
         *
122 0e4865ee pastith
         * @param newFiles the files to set
123 a52ea5e4 pastith
         */
124 0e4865ee pastith
        public void setFiles(List<FileResource> newFiles) {
125 0e4865ee pastith
                files = newFiles;
126 a52ea5e4 pastith
        }
127 a52ea5e4 pastith
128 0e4865ee pastith
        @Override
129 a52ea5e4 pastith
        public void createFromJSON(String text) {
130 a52ea5e4 pastith
                JSONObject json = (JSONObject) JSONParser.parse(text);
131 a52ea5e4 pastith
                if (json.get("folders") != null) {
132 a52ea5e4 pastith
                        JSONArray subs = json.get("folders").isArray();
133 a52ea5e4 pastith
                        if (subs != null)
134 a52ea5e4 pastith
                                for (int i = 0; i < subs.size(); i++) {
135 a52ea5e4 pastith
                                        JSONObject so = subs.get(i).isObject();
136 a52ea5e4 pastith
                                        if (so != null) {
137 a52ea5e4 pastith
                                                String subUri = unmarshallString(so, "uri");
138 a52ea5e4 pastith
                                                String subName = unmarshallString(so, "name");
139 a52ea5e4 pastith
                                                if (subUri != null && subName != null) {
140 a52ea5e4 pastith
                                                        if (!subUri.endsWith("/"))
141 a52ea5e4 pastith
                                                                subUri = subUri + "/";
142 a52ea5e4 pastith
                                                        FolderResource sub = new FolderResource(subUri);
143 a52ea5e4 pastith
                                                        sub.setName(subName);
144 a52ea5e4 pastith
                                                        sub.setNeedsExpanding(true);
145 a52ea5e4 pastith
                                                        folders.add(sub);
146 a52ea5e4 pastith
                                                        subfolderPaths.add(subUri);
147 a52ea5e4 pastith
                                                }
148 a52ea5e4 pastith
                                        }
149 a52ea5e4 pastith
                                }
150 a52ea5e4 pastith
                }
151 a52ea5e4 pastith
                if (json.get("files") != null) {
152 a52ea5e4 pastith
                        JSONArray subs = json.get("files").isArray();
153 a52ea5e4 pastith
                        if (subs != null)
154 a52ea5e4 pastith
                                for (int i = 0; i < subs.size(); i++) {
155 a52ea5e4 pastith
                                        JSONObject fo = subs.get(i).isObject();
156 a52ea5e4 pastith
                                        if (fo != null) {
157 a52ea5e4 pastith
                                                String fname = unmarshallString(fo, "name");
158 a52ea5e4 pastith
                                                String fowner = unmarshallString(fo, "owner");
159 0e4865ee pastith
                                                String fcontent = unmarshallString(fo, "content");
160 a52ea5e4 pastith
                                                Integer fversion = null;
161 a52ea5e4 pastith
                                                if (fo.get("version") != null)
162 a52ea5e4 pastith
                                                        fversion = new Integer(fo.get("version").toString());
163 a52ea5e4 pastith
                                                boolean fdeleted = unmarshallBoolean(fo, "deleted");
164 a52ea5e4 pastith
                                                Date fcreationDate = null;
165 a52ea5e4 pastith
                                                if (fo.get("creationDate") != null)
166 a52ea5e4 pastith
                                                        fcreationDate = new Date(new Long(fo.get("creationDate").toString()));
167 d0d4d0f5 Panagiotis Astithas
                                                Date fmodificationDate = null;
168 d0d4d0f5 Panagiotis Astithas
                                                if (fo.get("modificationDate") != null)
169 d0d4d0f5 Panagiotis Astithas
                                                        fmodificationDate = new Date(new Long(fo.get("modificationDate").toString()));
170 a52ea5e4 pastith
                                                String furi = unmarshallString(fo,"uri");
171 a52ea5e4 pastith
                                                Long fsize = 0L;
172 a52ea5e4 pastith
                                                if(fo.get("size") != null)
173 a52ea5e4 pastith
                                                        fsize = new Long(fo.get("size").toString());
174 a52ea5e4 pastith
                                                filePaths.add(furi);
175 d6ba3052 koutsoub
                                                String fpath = unmarshallString(fo, "path");
176 d6ba3052 koutsoub
                                                fpath = URL.decodeComponent(fpath);
177 a52ea5e4 pastith
                                                FileResource fs = new FileResource(furi);
178 555e8e59 pastith
                                                fs.setPath(fpath);
179 a52ea5e4 pastith
                                                fs.setName(fname);
180 a52ea5e4 pastith
                                                fs.setOwner(fowner);
181 a52ea5e4 pastith
                                                fs.setVersion(fversion);
182 a52ea5e4 pastith
                                                fs.setContentLength(fsize);
183 a52ea5e4 pastith
                                                fs.setDeleted(fdeleted);
184 a52ea5e4 pastith
                                                fs.setCreationDate(fcreationDate);
185 d0d4d0f5 Panagiotis Astithas
                                                fs.setModificationDate(fmodificationDate);
186 0e4865ee pastith
                                                fs.setContentType(fcontent);
187 a52ea5e4 pastith
                                                files.add(fs);
188 a52ea5e4 pastith
                                        }
189 a52ea5e4 pastith
                                }
190 a52ea5e4 pastith
                }
191 a52ea5e4 pastith
        }
192 a52ea5e4 pastith
193 a52ea5e4 pastith
        public List<String> getRootSharedFiles(){
194 a52ea5e4 pastith
                List<String> res = new ArrayList<String>();
195 a52ea5e4 pastith
                for(String f : getFilePaths()){
196 a52ea5e4 pastith
                        boolean contained = false;
197 a52ea5e4 pastith
                        for(String fo : getSubfolderPaths())
198 a52ea5e4 pastith
                                if(f.startsWith(fo))
199 a52ea5e4 pastith
                                        contained = true;
200 a52ea5e4 pastith
                        if(!contained)
201 a52ea5e4 pastith
                                res.add(f);
202 a52ea5e4 pastith
                }
203 a52ea5e4 pastith
                return res;
204 a52ea5e4 pastith
        }
205 a52ea5e4 pastith
206 62f168b2 Giannis Koutsoubos
        @Override
207 62f168b2 Giannis Koutsoubos
        public String getLastModifiedSince() {
208 62f168b2 Giannis Koutsoubos
                return null;
209 62f168b2 Giannis Koutsoubos
        }
210 757813cf Giannis Koutsoubos
211 757813cf Giannis Koutsoubos
        public MultipleGetCommand.Cached[] getFileCache(){
212 757813cf Giannis Koutsoubos
                if(getFilePaths().size() != getFiles().size()){
213 757813cf Giannis Koutsoubos
                        GWT.log("MISMATCH IN PATH AND FILES SIZE", null);
214 757813cf Giannis Koutsoubos
                        return null;
215 757813cf Giannis Koutsoubos
                }
216 757813cf Giannis Koutsoubos
                if(!filesExpanded)
217 757813cf Giannis Koutsoubos
                        return null;
218 757813cf Giannis Koutsoubos
                MultipleGetCommand.Cached[] result = new MultipleGetCommand.Cached[getFilePaths().size()];
219 757813cf Giannis Koutsoubos
                for(int i=0; i<getFiles().size();i++){
220 757813cf Giannis Koutsoubos
                        FileResource r = getFiles().get(i);
221 757813cf Giannis Koutsoubos
                        Cached c = new Cached();
222 757813cf Giannis Koutsoubos
                        c.cache=r;
223 757813cf Giannis Koutsoubos
                        c.uri=r.uri;
224 757813cf Giannis Koutsoubos
                        result[i] = c;
225 757813cf Giannis Koutsoubos
                }
226 757813cf Giannis Koutsoubos
                return result;
227 757813cf Giannis Koutsoubos
        }
228 757813cf Giannis Koutsoubos
229 86c951b2 Panagiotis Astithas
        public void setFilesExpanded(boolean newFilesExpanded) {
230 86c951b2 Panagiotis Astithas
                filesExpanded = newFilesExpanded;
231 757813cf Giannis Koutsoubos
        }
232 c5070ebe Natasa Kapravelou
        /**
233 b98ec534 Natasa Kapravelou
         * history support for folder navigation
234 c5070ebe Natasa Kapravelou
         */
235 c5070ebe Natasa Kapravelou
        @Override
236 b98ec534 Natasa Kapravelou
        public void updateHistory(TreeItem item, String path){
237 b98ec534 Natasa Kapravelou
                GSS.get().updateHistory("Files/"+ getUri().substring(path.lastIndexOf("/")+1), item);
238 c5070ebe Natasa Kapravelou
        }
239 a52ea5e4 pastith
}