Statistics
| Branch: | Tag: | Revision:

root / src / org / gss_project / gss / web / client / rest / resource / OtherUserResource.java @ 1206:292dec4eae08

History | View | Annotate | Download (6.4 kB)

1
/*
2
 * Copyright 2009, 2010 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
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.
10
 *
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.
15
 *
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/>.
18
 */
19
package org.gss_project.gss.web.client.rest.resource;
20

    
21
import org.gss_project.gss.web.client.rest.MultipleGetCommand;
22
import org.gss_project.gss.web.client.rest.MultipleGetCommand.Cached;
23

    
24
import java.util.ArrayList;
25
import java.util.Date;
26
import java.util.LinkedList;
27
import java.util.List;
28

    
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;
35

    
36

    
37
/**
38
 * @author kman
39
 *
40
 */
41
public class OtherUserResource extends RestResource{
42
        public OtherUserResource(String aUri) {
43
                super(aUri);
44
        }
45

    
46
        String username;
47
        List<String> filePaths = new LinkedList<String>();
48
        List<String> subfolderPaths = new LinkedList<String>();
49
        List<FolderResource> folders = new ArrayList<FolderResource>();
50
        List<FileResource> files = new ArrayList<FileResource>();
51

    
52
        private boolean filesExpanded=false;
53
        /**
54
         * Retrieve the username.
55
         *
56
         * @return the username
57
         */
58
        public String getUsername() {
59
                return username;
60
        }
61

    
62
        /**
63
         * Modify the username.
64
         *
65
         * @param aUsername the username to set
66
         */
67
        public void setUsername(String aUsername) {
68
                username = aUsername;
69
        }
70

    
71
        /**
72
         * Retrieve the files.
73
         *
74
         * @return the files
75
         */
76
        public List<String> getFilePaths() {
77
                return filePaths;
78
        }
79

    
80
        /**
81
         * Modify the files.
82
         *
83
         * @param newFiles the files to set
84
         */
85
        public void setFilePaths(List<String> newFiles) {
86
                filePaths = newFiles;
87
        }
88

    
89
        /**
90
         * Retrieve the subfolders.
91
         *
92
         * @return the subfolders
93
         */
94
        public List<String> getSubfolderPaths() {
95
                return subfolderPaths;
96
        }
97

    
98
        /**
99
         * Modify the subfolders.
100
         *
101
         * @param subfolders the subfolders to set
102
         */
103
        public void setSubfolderPaths(List<String> subfolders) {
104
                subfolderPaths = subfolders;
105
        }
106

    
107
        /**
108
         * Retrieve the folders.
109
         *
110
         * @return the folders
111
         */
112
        public List<FolderResource> getFolders() {
113
                return folders;
114
        }
115

    
116
        /**
117
         * Modify the folders.
118
         *
119
         * @param newFolders the folders to set
120
         */
121
        public void setFolders(List<FolderResource> newFolders) {
122
                folders = newFolders;
123
        }
124

    
125
        /**
126
         * Retrieve the files.
127
         *
128
         * @return the files
129
         */
130
        public List<FileResource> getFiles() {
131
                return files;
132
        }
133

    
134
        /**
135
         * Modify the files.
136
         *
137
         * @param newFiles the files to set
138
         */
139
        public void setFiles(List<FileResource> newFiles) {
140
                files = newFiles;
141
        }
142

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

    
212
        @Override
213
        public String getName(){
214
                String[] names = uri.split("/");
215
                return names[names.length -1];
216
        }
217

    
218
        @Override
219
        public String getLastModifiedSince() {
220
                return null;
221
        }
222

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

    
241
        public void setFilesExpanded(boolean newFilesExpanded) {
242
                filesExpanded = newFilesExpanded;
243
        }
244

    
245
        @Override
246
        public String constructUri(TreeItem treeItem, String path){
247
                String constructedUri = "Files/others/"+ getName();
248
                return constructedUri;
249
        }
250
}