Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.7 kB)

1
/*
2
 *  Copyright (c) 2011 Greek Research and Technology Network
3
 */
4
package gr.grnet.pithos.web.client.rest.resource;
5

    
6
import gr.grnet.pithos.web.client.rest.MultipleGetCommand;
7
import gr.grnet.pithos.web.client.rest.MultipleGetCommand.Cached;
8

    
9
import java.util.ArrayList;
10
import java.util.Date;
11
import java.util.LinkedList;
12
import java.util.List;
13

    
14
import com.google.gwt.core.client.GWT;
15
import com.google.gwt.http.client.URL;
16
import com.google.gwt.json.client.JSONArray;
17
import com.google.gwt.json.client.JSONObject;
18
import com.google.gwt.json.client.JSONParser;
19
import com.google.gwt.user.client.ui.TreeItem;
20

    
21

    
22
public class SharedResource extends RestResource{
23

    
24
        public SharedResource(String aUri) {
25
                super(aUri);
26
        }
27

    
28
        List<String> filePaths = new LinkedList<String>();
29
        List<String> subfolderPaths = new LinkedList<String>();
30
        List<FolderResource> folders = new ArrayList<FolderResource>();
31

    
32
        List<FileResource> files = new ArrayList<FileResource>();
33

    
34
        private boolean filesExpanded=false;
35

    
36
        /**
37
         * Retrieve the files.
38
         *
39
         * @return the files
40
         */
41
        public List<String> getFilePaths() {
42
                return filePaths;
43
        }
44

    
45
        /**
46
         * Modify the files.
47
         *
48
         * @param newFilePaths the files to set
49
         */
50
        public void setFilePaths(List<String> newFilePaths) {
51
                filePaths = newFilePaths;
52
        }
53

    
54
        /**
55
         * Retrieve the subfolders.
56
         *
57
         * @return the subfolders
58
         */
59
        public List<String> getSubfolderPaths() {
60
                return subfolderPaths;
61
        }
62

    
63
        /**
64
         * Modify the subfolder paths.
65
         *
66
         * @param newSubfolderPaths the subfolder paths to set
67
         */
68
        public void setSubfolderPaths(List<String> newSubfolderPaths) {
69
                subfolderPaths = newSubfolderPaths;
70
        }
71

    
72
        /**
73
         * Retrieve the folders.
74
         *
75
         * @return the folders
76
         */
77
        public List<FolderResource> getFolders() {
78
                return folders;
79
        }
80

    
81
        /**
82
         * Modify the folders.
83
         *
84
         * @param newFolders the folders to set
85
         */
86
        public void setFolders(List<FolderResource> newFolders) {
87
                folders = newFolders;
88
        }
89

    
90
        /**
91
         * Retrieve the files.
92
         *
93
         * @return the files
94
         */
95
        public List<FileResource> getFiles() {
96
                return files;
97
        }
98

    
99
        /**
100
         * Modify the files.
101
         *
102
         * @param newFiles the files to set
103
         */
104
        public void setFiles(List<FileResource> newFiles) {
105
                files = newFiles;
106
        }
107

    
108
        @Override
109
        public void createFromJSON(String text) {
110
                JSONObject json = (JSONObject) JSONParser.parse(text);
111
                if (json.get("folders") != null) {
112
                        JSONArray subs = json.get("folders").isArray();
113
                        if (subs != null)
114
                                for (int i = 0; i < subs.size(); i++) {
115
                                        JSONObject so = subs.get(i).isObject();
116
                                        if (so != null) {
117
                                                String subUri = unmarshallString(so, "uri");
118
                                                String subName = unmarshallString(so, "name");
119
                                                if (subUri != null && subName != null) {
120
                                                        if (!subUri.endsWith("/"))
121
                                                                subUri = subUri + "/";
122
                                                        FolderResource sub = new FolderResource(subUri);
123
                                                        sub.setName(subName);
124
                                                        sub.setNeedsExpanding(true);
125
                                                        folders.add(sub);
126
                                                        subfolderPaths.add(subUri);
127
                                                }
128
                                        }
129
                                }
130
                }
131
                if (json.get("files") != null) {
132
                        JSONArray subs = json.get("files").isArray();
133
                        if (subs != null)
134
                                for (int i = 0; i < subs.size(); i++) {
135
                                        JSONObject fo = subs.get(i).isObject();
136
                                        if (fo != null) {
137
                                                String fname = unmarshallString(fo, "name");
138
                                                String fowner = unmarshallString(fo, "owner");
139
                                                String fcontent = unmarshallString(fo, "content");
140
                                                boolean fversioned = unmarshallBoolean(fo, "versioned");
141
                                                Integer fversion = null;
142
                                                if (fo.get("version") != null)
143
                                                        fversion = new Integer(fo.get("version").toString());
144
                                                boolean fdeleted = unmarshallBoolean(fo, "deleted");
145
                                                Date fcreationDate = null;
146
                                                if (fo.get("creationDate") != null)
147
                                                        fcreationDate = new Date(new Long(fo.get("creationDate").toString()));
148
                                                Date fmodificationDate = null;
149
                                                if (fo.get("modificationDate") != null)
150
                                                        fmodificationDate = new Date(new Long(fo.get("modificationDate").toString()));
151
                                                String furi = unmarshallString(fo,"uri");
152
                                                Long fsize = 0L;
153
                                                if(fo.get("size") != null)
154
                                                        fsize = new Long(fo.get("size").toString());
155
                                                filePaths.add(furi);
156
                                                String fpath = unmarshallString(fo, "path");
157
                                                fpath = URL.decodeComponent(fpath);
158
                                                FileResource fs = new FileResource(furi);
159
                                                fs.setPath(fpath);
160
                                                fs.setName(fname);
161
                                                fs.setOwner(fowner);
162
                                                fs.setVersion(fversion);
163
                                                fs.setVersioned(unmarshallBoolean(fo, "versioned"));
164
                                                fs.setContentLength(fsize);
165
                                                fs.setDeleted(fdeleted);
166
                                                fs.setShared(unmarshallBoolean(fo,"shared"));
167
                                                fs.setVersioned(fversioned);
168
                                                fs.setCreationDate(fcreationDate);
169
                                                fs.setModificationDate(fmodificationDate);
170
                                                fs.setContentType(fcontent);
171
                                                files.add(fs);
172
                                        }
173
                                }
174
                }
175
        }
176

    
177
        public List<String> getRootSharedFiles(){
178
                List<String> res = new ArrayList<String>();
179
                for(String f : getFilePaths()){
180
                        boolean contained = false;
181
                        for(String fo : getSubfolderPaths())
182
                                if(f.startsWith(fo))
183
                                        contained = true;
184
                        if(!contained)
185
                                res.add(f);
186
                }
187
                return res;
188
        }
189

    
190
        @Override
191
        public String getLastModifiedSince() {
192
                return null;
193
        }
194

    
195
        public MultipleGetCommand.Cached[] getFileCache(){
196
                if(getFilePaths().size() != getFiles().size()){
197
                        GWT.log("MISMATCH IN PATH AND FILES SIZE", null);
198
                        return null;
199
                }
200
                if(!filesExpanded)
201
                        return null;
202
                MultipleGetCommand.Cached[] result = new MultipleGetCommand.Cached[getFilePaths().size()];
203
                for(int i=0; i<getFiles().size();i++){
204
                        FileResource r = getFiles().get(i);
205
                        Cached c = new Cached();
206
                        c.cache=r;
207
                        c.uri=r.uri;
208
                        result[i] = c;
209
                }
210
                return result;
211
        }
212

    
213
        public void setFilesExpanded(boolean newFilesExpanded) {
214
                filesExpanded = newFilesExpanded;
215
        }
216

    
217
        @Override
218
        public String constructUri(TreeItem treeItem, String path){
219
                String constructedUri = "Files/"+ getUri().substring(path.lastIndexOf("/")+1);
220
                return constructedUri;
221
        }
222

    
223
}