Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / rest / resource / SharedResource.java @ 58777026

History | View | Annotate | Download (7.2 kB)

1
/*
2
 * Copyright 2011 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
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.
16
 *
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.
29
 *
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.
34
 */
35
package gr.grnet.pithos.web.client.rest.resource;
36

    
37
import gr.grnet.pithos.web.client.rest.MultipleGetCommand;
38
import gr.grnet.pithos.web.client.rest.MultipleGetCommand.Cached;
39

    
40
import java.util.ArrayList;
41
import java.util.Date;
42
import java.util.LinkedList;
43
import java.util.List;
44

    
45
import com.google.gwt.core.client.GWT;
46
import com.google.gwt.http.client.URL;
47
import com.google.gwt.json.client.JSONArray;
48
import com.google.gwt.json.client.JSONObject;
49
import com.google.gwt.json.client.JSONParser;
50
import com.google.gwt.user.client.ui.TreeItem;
51

    
52

    
53
public class SharedResource extends RestResource{
54

    
55
        public SharedResource(String aUri) {
56
                super(aUri);
57
        }
58

    
59
        List<String> filePaths = new LinkedList<String>();
60
        List<String> subfolderPaths = new LinkedList<String>();
61
        List<FolderResource> folders = new ArrayList<FolderResource>();
62

    
63
        List<FileResource> files = new ArrayList<FileResource>();
64

    
65
        private boolean filesExpanded=false;
66

    
67
        /**
68
         * Retrieve the files.
69
         *
70
         * @return the files
71
         */
72
        public List<String> getFilePaths() {
73
                return filePaths;
74
        }
75

    
76
        /**
77
         * Modify the files.
78
         *
79
         * @param newFilePaths the files to set
80
         */
81
        public void setFilePaths(List<String> newFilePaths) {
82
                filePaths = newFilePaths;
83
        }
84

    
85
        /**
86
         * Retrieve the subfolders.
87
         *
88
         * @return the subfolders
89
         */
90
        public List<String> getSubfolderPaths() {
91
                return subfolderPaths;
92
        }
93

    
94
        /**
95
         * Modify the subfolder paths.
96
         *
97
         * @param newSubfolderPaths the subfolder paths to set
98
         */
99
        public void setSubfolderPaths(List<String> newSubfolderPaths) {
100
                subfolderPaths = newSubfolderPaths;
101
        }
102

    
103
        /**
104
         * Retrieve the folders.
105
         *
106
         * @return the folders
107
         */
108
        public List<FolderResource> getFolders() {
109
                return folders;
110
        }
111

    
112
        /**
113
         * Modify the folders.
114
         *
115
         * @param newFolders the folders to set
116
         */
117
        public void setFolders(List<FolderResource> newFolders) {
118
                folders = newFolders;
119
        }
120

    
121
        /**
122
         * Retrieve the files.
123
         *
124
         * @return the files
125
         */
126
        public List<FileResource> getFiles() {
127
                return files;
128
        }
129

    
130
        /**
131
         * Modify the files.
132
         *
133
         * @param newFiles the files to set
134
         */
135
        public void setFiles(List<FileResource> newFiles) {
136
                files = newFiles;
137
        }
138

    
139
        @Override
140
        public void createFromJSON(String text) {
141
                JSONObject json = (JSONObject) JSONParser.parse(text);
142
                if (json.get("folders") != null) {
143
                        JSONArray subs = json.get("folders").isArray();
144
                        if (subs != null)
145
                                for (int i = 0; i < subs.size(); i++) {
146
                                        JSONObject so = subs.get(i).isObject();
147
                                        if (so != null) {
148
                                                String subUri = unmarshallString(so, "uri");
149
                                                String subName = unmarshallString(so, "name");
150
                                                if (subUri != null && subName != null) {
151
                                                        if (!subUri.endsWith("/"))
152
                                                                subUri = subUri + "/";
153
                                                        FolderResource sub = new FolderResource(subUri);
154
                                                        sub.setName(subName);
155
                                                        sub.setNeedsExpanding(true);
156
                                                        folders.add(sub);
157
                                                        subfolderPaths.add(subUri);
158
                                                }
159
                                        }
160
                                }
161
                }
162
                if (json.get("files") != null) {
163
                        JSONArray subs = json.get("files").isArray();
164
                        if (subs != null)
165
                                for (int i = 0; i < subs.size(); i++) {
166
                                        JSONObject fo = subs.get(i).isObject();
167
                                        if (fo != null) {
168
                                                String fname = unmarshallString(fo, "name");
169
                                                String fowner = unmarshallString(fo, "owner");
170
                                                String fcontent = unmarshallString(fo, "content");
171
                                                boolean fversioned = unmarshallBoolean(fo, "versioned");
172
                                                Integer fversion = null;
173
                                                if (fo.get("version") != null)
174
                                                        fversion = new Integer(fo.get("version").toString());
175
                                                boolean fdeleted = unmarshallBoolean(fo, "deleted");
176
                                                Date fcreationDate = null;
177
                                                if (fo.get("creationDate") != null)
178
                                                        fcreationDate = new Date(new Long(fo.get("creationDate").toString()));
179
                                                Date fmodificationDate = null;
180
                                                if (fo.get("modificationDate") != null)
181
                                                        fmodificationDate = new Date(new Long(fo.get("modificationDate").toString()));
182
                                                String furi = unmarshallString(fo,"uri");
183
                                                Long fsize = 0L;
184
                                                if(fo.get("size") != null)
185
                                                        fsize = new Long(fo.get("size").toString());
186
                                                filePaths.add(furi);
187
                                                String fpath = unmarshallString(fo, "path");
188
                                                fpath = URL.decodeComponent(fpath);
189
                                                FileResource fs = new FileResource(furi);
190
                                                fs.setPath(fpath);
191
                                                fs.setName(fname);
192
                                                fs.setOwner(fowner);
193
                                                fs.setVersion(fversion);
194
                                                fs.setVersioned(unmarshallBoolean(fo, "versioned"));
195
                                                fs.setContentLength(fsize);
196
                                                fs.setDeleted(fdeleted);
197
                                                fs.setShared(unmarshallBoolean(fo,"shared"));
198
                                                fs.setVersioned(fversioned);
199
                                                fs.setCreationDate(fcreationDate);
200
                                                fs.setModificationDate(fmodificationDate);
201
                                                fs.setContentType(fcontent);
202
                                                files.add(fs);
203
                                        }
204
                                }
205
                }
206
        }
207

    
208
        public List<String> getRootSharedFiles(){
209
                List<String> res = new ArrayList<String>();
210
                for(String f : getFilePaths()){
211
                        boolean contained = false;
212
                        for(String fo : getSubfolderPaths())
213
                                if(f.startsWith(fo))
214
                                        contained = true;
215
                        if(!contained)
216
                                res.add(f);
217
                }
218
                return res;
219
        }
220

    
221
        @Override
222
        public String getLastModifiedSince() {
223
                return null;
224
        }
225

    
226
        public MultipleGetCommand.Cached[] getFileCache(){
227
                if(getFilePaths().size() != getFiles().size()){
228
                        GWT.log("MISMATCH IN PATH AND FILES SIZE", null);
229
                        return null;
230
                }
231
                if(!filesExpanded)
232
                        return null;
233
                MultipleGetCommand.Cached[] result = new MultipleGetCommand.Cached[getFilePaths().size()];
234
                for(int i=0; i<getFiles().size();i++){
235
                        FileResource r = getFiles().get(i);
236
                        Cached c = new Cached();
237
                        c.cache=r;
238
                        c.uri=r.uri;
239
                        result[i] = c;
240
                }
241
                return result;
242
        }
243

    
244
        public void setFilesExpanded(boolean newFilesExpanded) {
245
                filesExpanded = newFilesExpanded;
246
        }
247

    
248
        @Override
249
        public String constructUri(TreeItem treeItem, String path){
250
                String constructedUri = "Files/"+ getUri().substring(path.lastIndexOf("/")+1);
251
                return constructedUri;
252
        }
253

    
254
}