Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.8 kB)

1
/*
2
 * Copyright (c) 2011 Greek Research and Technology Network
3
 */
4

    
5
package gr.grnet.pithos.web.client.foldertree;
6

    
7
import com.google.gwt.http.client.Response;
8
import com.google.gwt.i18n.client.DateTimeFormat;
9
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
10
import com.google.gwt.i18n.client.NumberFormat;
11
import com.google.gwt.json.client.JSONObject;
12
import com.google.gwt.json.client.JSONParser;
13
import com.google.gwt.json.client.JSONValue;
14
import java.util.Date;
15

    
16
public class File extends Resource {
17
    private String name;
18

    
19
    private String hash;
20

    
21
    private int version;
22

    
23
    private long bytes;
24

    
25
    private String contentType;
26

    
27
    private Date lastModified;
28

    
29
    private String modifiedBy;
30

    
31
    private Date versionTimestamp;
32

    
33
    private String path;
34

    
35
    private String owner;
36

    
37
    private boolean inTrash;
38

    
39
    private String container;
40

    
41
    public String getContentType() {
42
        return contentType;
43
    }
44

    
45
    public String getHash() {
46
        return hash;
47
    }
48

    
49
    public Date getLastModified() {
50
        return lastModified;
51
    }
52

    
53
    public String getModifiedBy() {
54
        return modifiedBy;
55
    }
56

    
57
    public String getName() {
58
        return name;
59
    }
60

    
61
    public int getVersion() {
62
        return version;
63
    }
64

    
65
    public Date getVersionTimestamp() {
66
        return versionTimestamp;
67
    }
68

    
69
    @Override
70
    public String getLastModifiedSince() {
71
        return null;  //To change body of implemented methods use File | Settings | File Templates.
72
    }
73

    
74
    public String getUri() {
75
        return path + "/" + name;
76
    }
77

    
78
    public String getOwner() {
79
        return owner;
80
    }
81

    
82
    public String getPath() {
83
        return path;
84
    }
85

    
86
    public long getBytes() {
87
        return bytes;
88
    }
89

    
90
    public String getSizeAsString() {
91
        NumberFormat nf = NumberFormat.getFormat("######.#");
92
        if (bytes < 1024)
93
            return String.valueOf(bytes) + " B";
94
        else if (bytes < 1024 * 1024)
95
            return nf.format(Double.valueOf(bytes)/(1024)) + " KB";
96
        else if (bytes < 1024 * 1024 * 1024)
97
            return nf.format(Double.valueOf(bytes)/(1024 * 1024)) + " MB";
98
        return nf.format(Double.valueOf(bytes)/(1024 * 1024 * 1024)) + " GB";
99
    }
100

    
101
    public boolean isShared() {
102
        return false;
103
    }
104

    
105
    public boolean isInTrash() {
106
        return inTrash;
107
    }
108

    
109
    public void populate(JSONObject o, String container) {
110
        path = unmarshallString(o, "name");
111
        if (path.contains("/"))
112
            name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
113
        else
114
            name = path;
115
        hash = unmarshallString(o, "hash");
116
        bytes = unmarshallLong(o, "bytes");
117
        version = unmarshallInt(o, "version");
118
        contentType = unmarshallString(o, "content_type");
119
        lastModified = unmarshallDate(o, "last_modified");
120
        modifiedBy = unmarshallString(o, "modified_by");
121
        versionTimestamp = unmarshallDate(o, "version_timestamp");
122
        this.container = container;
123
    }
124

    
125
    public boolean equals(Object other) {
126
        if (other instanceof File) {
127
            File o = (File) other;
128
            return name.equals(o.getName());
129
        }
130
        return false;
131
    }
132

    
133
    public int hashCode() {
134
        return name.hashCode();
135
    }
136

    
137
    public String getContainer() {
138
        return container;
139
    }
140

    
141
    public static File createFromResponse(Response response, File result) {
142
        result.populate(response);
143
        return result;
144
    }
145

    
146
    private void populate(Response response) {
147
        String header = response.getHeader("X-Object-Meta-Trash");
148
        if (header != null)
149
            inTrash = Boolean.valueOf(header);
150
        else
151
            inTrash = false;
152

    
153
        JSONValue json = JSONParser.parseStrict(response.getText());
154
        JSONObject o = json.isObject();
155
    }
156
}