Statistics
| Branch: | Tag: | Revision:

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

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 gr.grnet.pithos.web.client.rest.resource;
20

    
21
import gr.grnet.pithos.web.client.rest.MultipleGetCommand;
22
import gr.grnet.pithos.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 SharedResource extends RestResource{
42

    
43
        public SharedResource(String aUri) {
44
                super(aUri);
45
        }
46

    
47
        List<String> filePaths = new LinkedList<String>();
48
        List<String> subfolderPaths = new LinkedList<String>();
49
        List<FolderResource> folders = new ArrayList<FolderResource>();
50

    
51
        List<FileResource> files = new ArrayList<FileResource>();
52

    
53
        private boolean filesExpanded=false;
54

    
55
        /**
56
         * Retrieve the files.
57
         *
58
         * @return the files
59
         */
60
        public List<String> getFilePaths() {
61
                return filePaths;
62
        }
63

    
64
        /**
65
         * Modify the files.
66
         *
67
         * @param newFilePaths the files to set
68
         */
69
        public void setFilePaths(List<String> newFilePaths) {
70
                filePaths = newFilePaths;
71
        }
72

    
73
        /**
74
         * Retrieve the subfolders.
75
         *
76
         * @return the subfolders
77
         */
78
        public List<String> getSubfolderPaths() {
79
                return subfolderPaths;
80
        }
81

    
82
        /**
83
         * Modify the subfolder paths.
84
         *
85
         * @param newSubfolderPaths the subfolder paths to set
86
         */
87
        public void setSubfolderPaths(List<String> newSubfolderPaths) {
88
                subfolderPaths = newSubfolderPaths;
89
        }
90

    
91
        /**
92
         * Retrieve the folders.
93
         *
94
         * @return the folders
95
         */
96
        public List<FolderResource> getFolders() {
97
                return folders;
98
        }
99

    
100
        /**
101
         * Modify the folders.
102
         *
103
         * @param newFolders the folders to set
104
         */
105
        public void setFolders(List<FolderResource> newFolders) {
106
                folders = newFolders;
107
        }
108

    
109
        /**
110
         * Retrieve the files.
111
         *
112
         * @return the files
113
         */
114
        public List<FileResource> getFiles() {
115
                return files;
116
        }
117

    
118
        /**
119
         * Modify the files.
120
         *
121
         * @param newFiles the files to set
122
         */
123
        public void setFiles(List<FileResource> newFiles) {
124
                files = newFiles;
125
        }
126

    
127
        @Override
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();
132
                        if (subs != null)
133
                                for (int i = 0; i < subs.size(); i++) {
134
                                        JSONObject so = subs.get(i).isObject();
135
                                        if (so != null) {
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);
144
                                                        folders.add(sub);
145
                                                        subfolderPaths.add(subUri);
146
                                                }
147
                                        }
148
                                }
149
                }
150
                if (json.get("files") != null) {
151
                        JSONArray subs = json.get("files").isArray();
152
                        if (subs != null)
153
                                for (int i = 0; i < subs.size(); i++) {
154
                                        JSONObject fo = subs.get(i).isObject();
155
                                        if (fo != null) {
156
                                                String fname = unmarshallString(fo, "name");
157
                                                String fowner = unmarshallString(fo, "owner");
158
                                                String fcontent = unmarshallString(fo, "content");
159
                                                boolean fversioned = unmarshallBoolean(fo, "versioned");
160
                                                Integer fversion = null;
161
                                                if (fo.get("version") != null)
162
                                                        fversion = new Integer(fo.get("version").toString());
163
                                                boolean fdeleted = unmarshallBoolean(fo, "deleted");
164
                                                Date fcreationDate = null;
165
                                                if (fo.get("creationDate") != null)
166
                                                        fcreationDate = new Date(new Long(fo.get("creationDate").toString()));
167
                                                Date fmodificationDate = null;
168
                                                if (fo.get("modificationDate") != null)
169
                                                        fmodificationDate = new Date(new Long(fo.get("modificationDate").toString()));
170
                                                String furi = unmarshallString(fo,"uri");
171
                                                Long fsize = 0L;
172
                                                if(fo.get("size") != null)
173
                                                        fsize = new Long(fo.get("size").toString());
174
                                                filePaths.add(furi);
175
                                                String fpath = unmarshallString(fo, "path");
176
                                                fpath = URL.decodeComponent(fpath);
177
                                                FileResource fs = new FileResource(furi);
178
                                                fs.setPath(fpath);
179
                                                fs.setName(fname);
180
                                                fs.setOwner(fowner);
181
                                                fs.setVersion(fversion);
182
                                                fs.setVersioned(unmarshallBoolean(fo, "versioned"));
183
                                                fs.setContentLength(fsize);
184
                                                fs.setDeleted(fdeleted);
185
                                                fs.setShared(unmarshallBoolean(fo,"shared"));
186
                                                fs.setVersioned(fversioned);
187
                                                fs.setCreationDate(fcreationDate);
188
                                                fs.setModificationDate(fmodificationDate);
189
                                                fs.setContentType(fcontent);
190
                                                files.add(fs);
191
                                        }
192
                                }
193
                }
194
        }
195

    
196
        public List<String> getRootSharedFiles(){
197
                List<String> res = new ArrayList<String>();
198
                for(String f : getFilePaths()){
199
                        boolean contained = false;
200
                        for(String fo : getSubfolderPaths())
201
                                if(f.startsWith(fo))
202
                                        contained = true;
203
                        if(!contained)
204
                                res.add(f);
205
                }
206
                return res;
207
        }
208

    
209
        @Override
210
        public String getLastModifiedSince() {
211
                return null;
212
        }
213

    
214
        public MultipleGetCommand.Cached[] getFileCache(){
215
                if(getFilePaths().size() != getFiles().size()){
216
                        GWT.log("MISMATCH IN PATH AND FILES SIZE", null);
217
                        return null;
218
                }
219
                if(!filesExpanded)
220
                        return null;
221
                MultipleGetCommand.Cached[] result = new MultipleGetCommand.Cached[getFilePaths().size()];
222
                for(int i=0; i<getFiles().size();i++){
223
                        FileResource r = getFiles().get(i);
224
                        Cached c = new Cached();
225
                        c.cache=r;
226
                        c.uri=r.uri;
227
                        result[i] = c;
228
                }
229
                return result;
230
        }
231

    
232
        public void setFilesExpanded(boolean newFilesExpanded) {
233
                filesExpanded = newFilesExpanded;
234
        }
235

    
236
        @Override
237
        public String constructUri(TreeItem treeItem, String path){
238
                String constructedUri = "Files/"+ getUri().substring(path.lastIndexOf("/")+1);
239
                return constructedUri;
240
        }
241

    
242
}