Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / foldertree / File.java @ 3da18014

History | View | Annotate | Download (5.2 kB)

1 9e8e14e4 Christos Stathis
/*
2 58777026 Christos Stathis
 * Copyright 2011 GRNET S.A. All rights reserved.
3 58777026 Christos Stathis
 *
4 58777026 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 58777026 Christos Stathis
 * without modification, are permitted provided that the following
6 58777026 Christos Stathis
 * conditions are met:
7 58777026 Christos Stathis
 *
8 58777026 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 58777026 Christos Stathis
 *      disclaimer.
11 58777026 Christos Stathis
 *
12 58777026 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 58777026 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 58777026 Christos Stathis
 *      provided with the distribution.
16 58777026 Christos Stathis
 *
17 58777026 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 58777026 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 58777026 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 58777026 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 58777026 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 58777026 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 58777026 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 58777026 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 58777026 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 58777026 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 58777026 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 58777026 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 58777026 Christos Stathis
 *
30 58777026 Christos Stathis
 * The views and conclusions contained in the software and
31 58777026 Christos Stathis
 * documentation are those of the authors and should not be
32 58777026 Christos Stathis
 * interpreted as representing official policies, either expressed
33 58777026 Christos Stathis
 * or implied, of GRNET S.A.
34 9e8e14e4 Christos Stathis
 */
35 9e8e14e4 Christos Stathis
36 9e8e14e4 Christos Stathis
package gr.grnet.pithos.web.client.foldertree;
37 9e8e14e4 Christos Stathis
38 0bc032bf Christos Stathis
import com.google.gwt.http.client.Response;
39 0bc032bf Christos Stathis
import com.google.gwt.i18n.client.DateTimeFormat;
40 0bc032bf Christos Stathis
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
41 fbff60ff Christos Stathis
import com.google.gwt.i18n.client.NumberFormat;
42 fbff60ff Christos Stathis
import com.google.gwt.json.client.JSONObject;
43 0bc032bf Christos Stathis
import com.google.gwt.json.client.JSONParser;
44 0bc032bf Christos Stathis
import com.google.gwt.json.client.JSONValue;
45 fbff60ff Christos Stathis
import java.util.Date;
46 fbff60ff Christos Stathis
47 fbff60ff Christos Stathis
public class File extends Resource {
48 fbff60ff Christos Stathis
    private String name;
49 fbff60ff Christos Stathis
50 fbff60ff Christos Stathis
    private String hash;
51 fbff60ff Christos Stathis
52 fbff60ff Christos Stathis
    private int version;
53 fbff60ff Christos Stathis
54 fbff60ff Christos Stathis
    private long bytes;
55 fbff60ff Christos Stathis
56 fbff60ff Christos Stathis
    private String contentType;
57 fbff60ff Christos Stathis
58 fbff60ff Christos Stathis
    private Date lastModified;
59 fbff60ff Christos Stathis
60 fbff60ff Christos Stathis
    private String modifiedBy;
61 fbff60ff Christos Stathis
62 fbff60ff Christos Stathis
    private Date versionTimestamp;
63 fbff60ff Christos Stathis
64 fbff60ff Christos Stathis
    private String path;
65 fbff60ff Christos Stathis
66 fbff60ff Christos Stathis
    private String owner;
67 fbff60ff Christos Stathis
68 fbff60ff Christos Stathis
    private boolean inTrash;
69 fbff60ff Christos Stathis
70 0bc032bf Christos Stathis
    private String container;
71 0bc032bf Christos Stathis
72 3ca8cd89 Christos Stathis
    private Folder parent;
73 3ca8cd89 Christos Stathis
74 fbff60ff Christos Stathis
    public String getContentType() {
75 fbff60ff Christos Stathis
        return contentType;
76 fbff60ff Christos Stathis
    }
77 fbff60ff Christos Stathis
78 fbff60ff Christos Stathis
    public String getHash() {
79 fbff60ff Christos Stathis
        return hash;
80 fbff60ff Christos Stathis
    }
81 fbff60ff Christos Stathis
82 fbff60ff Christos Stathis
    public Date getLastModified() {
83 fbff60ff Christos Stathis
        return lastModified;
84 fbff60ff Christos Stathis
    }
85 fbff60ff Christos Stathis
86 fbff60ff Christos Stathis
    public String getModifiedBy() {
87 fbff60ff Christos Stathis
        return modifiedBy;
88 fbff60ff Christos Stathis
    }
89 fbff60ff Christos Stathis
90 fbff60ff Christos Stathis
    public String getName() {
91 fbff60ff Christos Stathis
        return name;
92 fbff60ff Christos Stathis
    }
93 fbff60ff Christos Stathis
94 fbff60ff Christos Stathis
    public int getVersion() {
95 fbff60ff Christos Stathis
        return version;
96 fbff60ff Christos Stathis
    }
97 fbff60ff Christos Stathis
98 fbff60ff Christos Stathis
    public Date getVersionTimestamp() {
99 fbff60ff Christos Stathis
        return versionTimestamp;
100 fbff60ff Christos Stathis
    }
101 fbff60ff Christos Stathis
102 fbff60ff Christos Stathis
    public String getUri() {
103 816de19f Christos Stathis
        return "/" + container + "/" + path;
104 fbff60ff Christos Stathis
    }
105 fbff60ff Christos Stathis
106 fbff60ff Christos Stathis
    public String getOwner() {
107 fbff60ff Christos Stathis
        return owner;
108 fbff60ff Christos Stathis
    }
109 fbff60ff Christos Stathis
110 fbff60ff Christos Stathis
    public String getPath() {
111 fbff60ff Christos Stathis
        return path;
112 fbff60ff Christos Stathis
    }
113 fbff60ff Christos Stathis
114 fbff60ff Christos Stathis
    public long getBytes() {
115 fbff60ff Christos Stathis
        return bytes;
116 fbff60ff Christos Stathis
    }
117 fbff60ff Christos Stathis
118 fbff60ff Christos Stathis
    public String getSizeAsString() {
119 fbff60ff Christos Stathis
        NumberFormat nf = NumberFormat.getFormat("######.#");
120 fbff60ff Christos Stathis
        if (bytes < 1024)
121 fbff60ff Christos Stathis
            return String.valueOf(bytes) + " B";
122 fbff60ff Christos Stathis
        else if (bytes < 1024 * 1024)
123 fbff60ff Christos Stathis
            return nf.format(Double.valueOf(bytes)/(1024)) + " KB";
124 fbff60ff Christos Stathis
        else if (bytes < 1024 * 1024 * 1024)
125 fbff60ff Christos Stathis
            return nf.format(Double.valueOf(bytes)/(1024 * 1024)) + " MB";
126 fbff60ff Christos Stathis
        return nf.format(Double.valueOf(bytes)/(1024 * 1024 * 1024)) + " GB";
127 fbff60ff Christos Stathis
    }
128 fbff60ff Christos Stathis
129 fbff60ff Christos Stathis
    public boolean isShared() {
130 fbff60ff Christos Stathis
        return false;
131 fbff60ff Christos Stathis
    }
132 fbff60ff Christos Stathis
133 fbff60ff Christos Stathis
    public boolean isInTrash() {
134 fbff60ff Christos Stathis
        return inTrash;
135 fbff60ff Christos Stathis
    }
136 fbff60ff Christos Stathis
137 3ca8cd89 Christos Stathis
    public void populate(Folder parent, JSONObject o, String container) {
138 3ca8cd89 Christos Stathis
        this.parent = parent;
139 0bc032bf Christos Stathis
        path = unmarshallString(o, "name");
140 fbff60ff Christos Stathis
        if (path.contains("/"))
141 fbff60ff Christos Stathis
            name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
142 fbff60ff Christos Stathis
        else
143 fbff60ff Christos Stathis
            name = path;
144 fbff60ff Christos Stathis
        hash = unmarshallString(o, "hash");
145 fbff60ff Christos Stathis
        bytes = unmarshallLong(o, "bytes");
146 fbff60ff Christos Stathis
        version = unmarshallInt(o, "version");
147 fbff60ff Christos Stathis
        contentType = unmarshallString(o, "content_type");
148 fbff60ff Christos Stathis
        lastModified = unmarshallDate(o, "last_modified");
149 fbff60ff Christos Stathis
        modifiedBy = unmarshallString(o, "modified_by");
150 fbff60ff Christos Stathis
        versionTimestamp = unmarshallDate(o, "version_timestamp");
151 0bc032bf Christos Stathis
        this.container = container;
152 fbff60ff Christos Stathis
    }
153 fbff60ff Christos Stathis
154 fbff60ff Christos Stathis
    public boolean equals(Object other) {
155 fbff60ff Christos Stathis
        if (other instanceof File) {
156 fbff60ff Christos Stathis
            File o = (File) other;
157 fbff60ff Christos Stathis
            return name.equals(o.getName());
158 fbff60ff Christos Stathis
        }
159 fbff60ff Christos Stathis
        return false;
160 fbff60ff Christos Stathis
    }
161 fbff60ff Christos Stathis
162 fbff60ff Christos Stathis
    public int hashCode() {
163 fbff60ff Christos Stathis
        return name.hashCode();
164 fbff60ff Christos Stathis
    }
165 0bc032bf Christos Stathis
166 0bc032bf Christos Stathis
    public String getContainer() {
167 0bc032bf Christos Stathis
        return container;
168 0bc032bf Christos Stathis
    }
169 0bc032bf Christos Stathis
170 0bc032bf Christos Stathis
    public static File createFromResponse(Response response, File result) {
171 0bc032bf Christos Stathis
        result.populate(response);
172 0bc032bf Christos Stathis
        return result;
173 0bc032bf Christos Stathis
    }
174 0bc032bf Christos Stathis
175 0bc032bf Christos Stathis
    private void populate(Response response) {
176 0bc032bf Christos Stathis
        String header = response.getHeader("X-Object-Meta-Trash");
177 0bc032bf Christos Stathis
        if (header != null)
178 0bc032bf Christos Stathis
            inTrash = Boolean.valueOf(header);
179 0bc032bf Christos Stathis
        else
180 0bc032bf Christos Stathis
            inTrash = false;
181 0bc032bf Christos Stathis
182 0bc032bf Christos Stathis
        JSONValue json = JSONParser.parseStrict(response.getText());
183 0bc032bf Christos Stathis
        JSONObject o = json.isObject();
184 0bc032bf Christos Stathis
    }
185 816de19f Christos Stathis
186 816de19f Christos Stathis
    public Folder getParent() {
187 816de19f Christos Stathis
        return parent;
188 816de19f Christos Stathis
    }
189 9e8e14e4 Christos Stathis
}