Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.2 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
    private boolean published;
82

    
83
    public String getContentType() {
84
        return contentType;
85
    }
86

    
87
    public String getHash() {
88
        return hash;
89
    }
90

    
91
    public Date getLastModified() {
92
        return lastModified;
93
    }
94

    
95
    public String getModifiedBy() {
96
        return modifiedBy;
97
    }
98

    
99
    public String getName() {
100
        return name;
101
    }
102

    
103
    public int getVersion() {
104
        return version;
105
    }
106

    
107
    public Date getVersionTimestamp() {
108
        return versionTimestamp;
109
    }
110

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

    
115
    public String getOwner() {
116
        return owner;
117
    }
118

    
119
    public String getPath() {
120
        return path;
121
    }
122

    
123
    public long getBytes() {
124
        return bytes;
125
    }
126

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

    
138
    public boolean isShared() {
139
        return false;
140
    }
141

    
142
    public boolean isInTrash() {
143
        return inTrash;
144
    }
145

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

    
164
        for (String key : o.keySet())
165
            if (key.startsWith("x_object_meta_") && !key.equals("x_object_meta_trash"))
166
                tags.add(key.substring("x_object_meta_".length()).trim().toLowerCase());
167

    
168
        
169
    }
170

    
171
    public boolean equals(Object other) {
172
        if (other instanceof File) {
173
            File o = (File) other;
174
            return name.equals(o.getName());
175
        }
176
        return false;
177
    }
178

    
179
    public int hashCode() {
180
        return name.hashCode();
181
    }
182

    
183
    public String getContainer() {
184
        return container;
185
    }
186

    
187
    public static File createFromResponse(String owner, Response response, File result) {
188
        result.populate(owner, response);
189
        return result;
190
    }
191

    
192
    private void populate(String owner, Response response) {
193
        this.owner = owner;
194
        for (Header h : response.getHeaders()) {
195
            String header = h.getName();
196
            if (header.startsWith("X-Object-Meta-") && !header.equals("X-Object-Meta-Trash"))
197
                tags.add(header.substring("X-Object-Meta-".length()).trim().toLowerCase());
198

    
199
        }
200
        String header = response.getHeader("X-Object-Meta-Trash");
201
        if (header != null)
202
            inTrash = Boolean.valueOf(header);
203
        else
204
            inTrash = false;
205
    }
206

    
207
    public Folder getParent() {
208
        return parent;
209
    }
210

    
211
    public Set<String> getTags() {
212
        return tags;
213
    }
214

    
215
    public boolean isPublished() {
216
        return published;
217
    }
218
}