Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.2 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.ebs.gss.client.rest.resource;
20

    
21
import gr.ebs.gss.client.GSS;
22
import gr.ebs.gss.client.rest.MultipleGetCommand;
23
import gr.ebs.gss.client.rest.MultipleGetCommand.Cached;
24

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

    
30
import com.google.gwt.core.client.GWT;
31
import com.google.gwt.http.client.URL;
32
import com.google.gwt.json.client.JSONArray;
33
import com.google.gwt.json.client.JSONObject;
34
import com.google.gwt.json.client.JSONParser;
35
import com.google.gwt.user.client.ui.TreeItem;
36

    
37

    
38
/**
39
 * @author kman
40
 *
41
 */
42
public class SharedResource extends RestResource{
43

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

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

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

    
54
        private boolean filesExpanded=false;
55

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

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

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

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

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

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

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

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

    
128
        @Override
129
        public void createFromJSON(String text) {
130
                JSONObject json = (JSONObject) JSONParser.parse(text);
131
                if (json.get("folders") != null) {
132
                        JSONArray subs = json.get("folders").isArray();
133
                        if (subs != null)
134
                                for (int i = 0; i < subs.size(); i++) {
135
                                        JSONObject so = subs.get(i).isObject();
136
                                        if (so != null) {
137
                                                String subUri = unmarshallString(so, "uri");
138
                                                String subName = unmarshallString(so, "name");
139
                                                if (subUri != null && subName != null) {
140
                                                        if (!subUri.endsWith("/"))
141
                                                                subUri = subUri + "/";
142
                                                        FolderResource sub = new FolderResource(subUri);
143
                                                        sub.setName(subName);
144
                                                        sub.setNeedsExpanding(true);
145
                                                        folders.add(sub);
146
                                                        subfolderPaths.add(subUri);
147
                                                }
148
                                        }
149
                                }
150
                }
151
                if (json.get("files") != null) {
152
                        JSONArray subs = json.get("files").isArray();
153
                        if (subs != null)
154
                                for (int i = 0; i < subs.size(); i++) {
155
                                        JSONObject fo = subs.get(i).isObject();
156
                                        if (fo != null) {
157
                                                String fname = unmarshallString(fo, "name");
158
                                                String fowner = unmarshallString(fo, "owner");
159
                                                String fcontent = unmarshallString(fo, "content");
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.setContentLength(fsize);
183
                                                fs.setDeleted(fdeleted);
184
                                                fs.setCreationDate(fcreationDate);
185
                                                fs.setModificationDate(fmodificationDate);
186
                                                fs.setContentType(fcontent);
187
                                                files.add(fs);
188
                                        }
189
                                }
190
                }
191
        }
192

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

    
206
        @Override
207
        public String getLastModifiedSince() {
208
                return null;
209
        }
210

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

    
229
        public void setFilesExpanded(boolean newFilesExpanded) {
230
                filesExpanded = newFilesExpanded;
231
        }
232
        /**
233
         * history support for folder navigation
234
         */
235
        @Override
236
        public void updateHistory(TreeItem item, String path){
237
                GSS.get().updateHistory("Files/"+ getUri().substring(path.lastIndexOf("/")+1), item);
238
        }
239
}