20abca32f654b22be86e6a1f0a2441b2435a0dc2
[pithos-web-client] / src / gr / grnet / pithos / web / client / rest / resource / SearchResource.java
1 /*
2  * Copyright 2009 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 java.util.ArrayList;
22 import java.util.Date;
23 import java.util.LinkedList;
24 import java.util.List;
25
26 import com.google.gwt.http.client.URL;
27 import com.google.gwt.json.client.JSONArray;
28 import com.google.gwt.json.client.JSONObject;
29 import com.google.gwt.json.client.JSONParser;
30
31 /**
32  * @author kman
33  */
34 public class SearchResource extends RestResource {
35         int size;
36         public SearchResource(String aUri) {
37                 super(aUri);
38         }
39
40         List<FileResource> files = new ArrayList<FileResource>();
41         List<String> filePaths = new LinkedList<String>();
42
43         /**
44          * Retrieve the files.
45          *
46          * @return the files
47          */
48         public List<String> getFilePaths() {
49                 return filePaths;
50         }
51
52         /**
53          * Modify the files.
54          *
55          * @param newFilePaths the files to set
56          */
57         public void setFilePaths(List<String> newFilePaths) {
58                 filePaths = newFilePaths;
59         }
60
61         /**
62          * Retrieve the files.
63          *
64          * @return the files
65          */
66         public List<FileResource> getFiles() {
67                 return files;
68         }
69
70         /**
71          * Modify the files.
72          *
73          * @param newFiles the files to set
74          */
75         public void setFiles(List<FileResource> newFiles) {
76                 files = newFiles;
77         }
78
79         @Override
80         public void createFromJSON(String text) {
81                 JSONArray subs = JSONParser.parse(text).isArray();
82                 if (subs != null)
83                         for (int i = 0; i < subs.size(); i++) {
84                                 JSONObject fo = subs.get(i).isObject();
85                                 if (fo != null) {
86                                         if(i==0&&unmarshallInt(fo, "length")!=-1){
87                                                 setSize(unmarshallInt(fo, "length"));
88                                         }
89                                         else{
90                                         String fname = unmarshallString(fo, "name");
91                                         String fowner = unmarshallString(fo, "owner");
92                                         String fcontent = unmarshallString(fo, "content");
93                                         String fpath = unmarshallString(fo, "path");
94                                         Boolean fshared = unmarshallBoolean(fo,"shared");
95                                         boolean fversioned = unmarshallBoolean(fo,"versioned");
96                                         fpath = URL.decodeComponent(fpath);
97                                         Integer fversion = null;
98                                         if (fo.get("version") != null)
99                                                 fversion = new Integer(fo.get("version").toString());
100                                         boolean fdeleted = unmarshallBoolean(fo, "deleted");
101                                         Date fcreationDate = null;
102                                         if (fo.get("creationDate") != null)
103                                                 fcreationDate = new Date(new Long(fo.get("creationDate").toString()));
104                                         Date fmodificationDate = null;
105                                         if (fo.get("modificationDate") != null)
106                                                 fmodificationDate = new Date(new Long(fo.get("modificationDate").toString()));
107                                         String furi = unmarshallString(fo,"uri");
108                                         Long fsize = 0L;
109                                         if(fo.get("size") != null)
110                                                 fsize = new Long(fo.get("size").toString());
111                                         filePaths.add(furi);
112                                         FileResource fs = new FileResource(furi);
113                                         fs.setName(fname);
114                                         fs.setOwner(fowner);
115                                         fs.setVersion(fversion);
116                                         fs.setContentLength(fsize);
117                                         fs.setDeleted(fdeleted);
118                                         fs.setCreationDate(fcreationDate);
119                                         fs.setModificationDate(fmodificationDate);
120                                         fs.setContentType(fcontent);
121                                         fs.setPath(fpath);
122                                         fs.setShared(fshared);
123                                         fs.setVersioned(fversioned);
124                                         files.add(fs);
125                                         }
126                                 }
127                         }
128         }
129
130         @Override
131         public String getLastModifiedSince() {
132                 return null;
133         }
134         
135         
136         /**
137          * Retrieve the size.
138          *
139          * @return the size
140          */
141         public int getSize() {
142                 return size;
143         }
144         
145         
146         /**
147          * Modify the size.
148          *
149          * @param size the size to set
150          */
151         public void setSize(int size) {
152                 this.size = size;
153         }
154 }