Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / rest / resource / OtherUserResource.java @ 63366925

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 OtherUserResource extends RestResource{
54
        public OtherUserResource(String aUri) {
55
                super(aUri);
56
        }
57

    
58
        String username;
59
        List<String> filePaths = new LinkedList<String>();
60
        List<String> subfolderPaths = new LinkedList<String>();
61
        List<FolderResource> folders = new ArrayList<FolderResource>();
62
        List<FileResource> files = new ArrayList<FileResource>();
63

    
64
        private boolean filesExpanded=false;
65
        /**
66
         * Retrieve the username.
67
         *
68
         * @return the username
69
         */
70
        public String getUsername() {
71
                return username;
72
        }
73

    
74
        /**
75
         * Modify the username.
76
         *
77
         * @param aUsername the username to set
78
         */
79
        public void setUsername(String aUsername) {
80
                username = aUsername;
81
        }
82

    
83
        /**
84
         * Retrieve the files.
85
         *
86
         * @return the files
87
         */
88
        public List<String> getFilePaths() {
89
                return filePaths;
90
        }
91

    
92
        /**
93
         * Modify the files.
94
         *
95
         * @param newFiles the files to set
96
         */
97
        public void setFilePaths(List<String> newFiles) {
98
                filePaths = newFiles;
99
        }
100

    
101
        /**
102
         * Retrieve the subfolders.
103
         *
104
         * @return the subfolders
105
         */
106
        public List<String> getSubfolderPaths() {
107
                return subfolderPaths;
108
        }
109

    
110
        /**
111
         * Modify the subfolders.
112
         *
113
         * @param subfolders the subfolders to set
114
         */
115
        public void setSubfolderPaths(List<String> subfolders) {
116
                subfolderPaths = subfolders;
117
        }
118

    
119
        /**
120
         * Retrieve the folders.
121
         *
122
         * @return the folders
123
         */
124
        public List<FolderResource> getFolders() {
125
                return folders;
126
        }
127

    
128
        /**
129
         * Modify the folders.
130
         *
131
         * @param newFolders the folders to set
132
         */
133
        public void setFolders(List<FolderResource> newFolders) {
134
                folders = newFolders;
135
        }
136

    
137
        /**
138
         * Retrieve the files.
139
         *
140
         * @return the files
141
         */
142
        public List<FileResource> getFiles() {
143
                return files;
144
        }
145

    
146
        /**
147
         * Modify the files.
148
         *
149
         * @param newFiles the files to set
150
         */
151
        public void setFiles(List<FileResource> newFiles) {
152
                files = newFiles;
153
        }
154

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

    
224
        @Override
225
        public String getName(){
226
                String[] names = uri.split("/");
227
                return names[names.length -1];
228
        }
229

    
230
        @Override
231
        public String getLastModifiedSince() {
232
                return null;
233
        }
234

    
235
        public MultipleGetCommand.Cached[] getFileCache(){
236
                if(getFilePaths().size() != getFiles().size()){
237
                        GWT.log("MISMATCH IN PATH AND FILES SIZE", null);
238
                        return null;
239
                }
240
                if(!filesExpanded)
241
                        return null;
242
                MultipleGetCommand.Cached[] result = new MultipleGetCommand.Cached[getFilePaths().size()];
243
                for(int i=0; i<getFiles().size();i++){
244
                        FileResource r = getFiles().get(i);
245
                        Cached c = new Cached();
246
                        c.cache=r;
247
                        c.uri=r.uri;
248
                        result[i] = c;
249
                }
250
                return result;
251
        }
252

    
253
        public void setFilesExpanded(boolean newFilesExpanded) {
254
                filesExpanded = newFilesExpanded;
255
        }
256

    
257
        @Override
258
        public String constructUri(TreeItem treeItem, String path){
259
                String constructedUri = "Files/others/"+ getName();
260
                return constructedUri;
261
        }
262
}