Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6 kB)

1
/*
2
 * Copyright 2011 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35

    
36
package gr.grnet.pithos.web.client.foldertree;
37

    
38
import com.google.gwt.http.client.Header;
39
import com.google.gwt.http.client.Response;
40
import com.google.gwt.i18n.client.DateTimeFormat;
41
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
42
import com.google.gwt.i18n.client.NumberFormat;
43
import com.google.gwt.json.client.JSONObject;
44
import com.google.gwt.json.client.JSONParser;
45
import com.google.gwt.json.client.JSONValue;
46
import java.io.StringWriter;
47
import java.security.Key;
48
import java.util.Date;
49
import java.util.HashSet;
50
import java.util.Set;
51

    
52
public class File extends Resource {
53
    private String name;
54

    
55
    private String hash;
56

    
57
    private int version;
58

    
59
    private long bytes;
60

    
61
    private String contentType;
62

    
63
    private Date lastModified;
64

    
65
    private String modifiedBy;
66

    
67
    private Date versionTimestamp;
68

    
69
    private String path;
70

    
71
    private String owner;
72

    
73
    private boolean inTrash;
74

    
75
    private String container;
76

    
77
    private Folder parent;
78

    
79
    private Set<String> tags = new HashSet<String>();
80

    
81
    public String getContentType() {
82
        return contentType;
83
    }
84

    
85
    public String getHash() {
86
        return hash;
87
    }
88

    
89
    public Date getLastModified() {
90
        return lastModified;
91
    }
92

    
93
    public String getModifiedBy() {
94
        return modifiedBy;
95
    }
96

    
97
    public String getName() {
98
        return name;
99
    }
100

    
101
    public int getVersion() {
102
        return version;
103
    }
104

    
105
    public Date getVersionTimestamp() {
106
        return versionTimestamp;
107
    }
108

    
109
    public String getUri() {
110
        return "/" + container + "/" + path;
111
    }
112

    
113
    public String getOwner() {
114
        return owner;
115
    }
116

    
117
    public String getPath() {
118
        return path;
119
    }
120

    
121
    public long getBytes() {
122
        return bytes;
123
    }
124

    
125
    public String getSizeAsString() {
126
        NumberFormat nf = NumberFormat.getFormat("######.#");
127
        if (bytes < 1024)
128
            return String.valueOf(bytes) + " B";
129
        else if (bytes < 1024 * 1024)
130
            return nf.format(Double.valueOf(bytes)/(1024)) + " KB";
131
        else if (bytes < 1024 * 1024 * 1024)
132
            return nf.format(Double.valueOf(bytes)/(1024 * 1024)) + " MB";
133
        return nf.format(Double.valueOf(bytes)/(1024 * 1024 * 1024)) + " GB";
134
    }
135

    
136
    public boolean isShared() {
137
        return false;
138
    }
139

    
140
    public boolean isInTrash() {
141
        return inTrash;
142
    }
143

    
144
    public void populate(Folder parent, JSONObject o, String container) {
145
        this.parent = parent;
146
        path = unmarshallString(o, "name");
147
        if (path.contains("/"))
148
            name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
149
        else
150
            name = path;
151
        hash = unmarshallString(o, "hash");
152
        bytes = unmarshallLong(o, "bytes");
153
        version = unmarshallInt(o, "version");
154
        contentType = unmarshallString(o, "content_type");
155
        lastModified = unmarshallDate(o, "last_modified");
156
        modifiedBy = unmarshallString(o, "modified_by");
157
        versionTimestamp = unmarshallDate(o, "version_timestamp");
158
        this.container = container;
159

    
160
        for (String key : o.keySet())
161
            if (key.startsWith("x_object_meta_") && !key.equals("x_object_meta_trash"))
162
                tags.add(key.substring("x_object_meta_".length()).trim().toLowerCase());
163
    }
164

    
165
    public boolean equals(Object other) {
166
        if (other instanceof File) {
167
            File o = (File) other;
168
            return name.equals(o.getName());
169
        }
170
        return false;
171
    }
172

    
173
    public int hashCode() {
174
        return name.hashCode();
175
    }
176

    
177
    public String getContainer() {
178
        return container;
179
    }
180

    
181
    public static File createFromResponse(Response response, File result) {
182
        result.populate(response);
183
        return result;
184
    }
185

    
186
    private void populate(Response response) {
187
        for (Header h : response.getHeaders()) {
188
            String header = h.getName();
189
            if (header.startsWith("X-Object-Meta-") && !header.equals("X-Object-Meta-Trash"))
190
                tags.add(header.substring("X-Object-Meta-".length()).trim().toLowerCase());
191

    
192
        }
193
        String header = response.getHeader("X-Object-Meta-Trash");
194
        if (header != null)
195
            inTrash = Boolean.valueOf(header);
196
        else
197
            inTrash = false;
198

    
199
        JSONValue json = JSONParser.parseStrict(response.getText());
200
        JSONObject o = json.isObject();
201
    }
202

    
203
    public Folder getParent() {
204
        return parent;
205
    }
206

    
207
    public Set<String> getTags() {
208
        return tags;
209
    }
210
}