Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / foldertree / Folder.java @ fbff60ff

History | View | Annotate | Download (5.1 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.json.client.JSONArray;
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.ArrayList;
15
import java.util.Date;
16
import java.util.LinkedHashSet;
17
import java.util.List;
18
import java.util.Set;
19

    
20
public class Folder extends Resource {
21
    /*
22
     * The name of the folder. If the folder is a container this is its name. If it is a virtual folder this is the
23
     * last part of its path
24
     */
25
    private String name = null;
26

    
27
    private Date lastModified = null;
28

    
29
    private long bytesUsed = 0;
30

    
31
    private Set<Folder> subfolders = new LinkedHashSet<Folder>();
32
    /*
33
     * The name of the container that this folder belongs to. If this folder is container, this field equals name
34
     */
35
    private String container = null;
36

    
37
    /*
38
     * This is the full path of the folder (prefix is a misnomer but it was named so because this is used as a prefix=
39
     * parameter in the request that fetches its children). If the folder is a cointainer this is empty string
40
     */
41
    private String prefix = "";
42

    
43
    private Set<File> files = new LinkedHashSet<File>();
44

    
45
    public Folder() {};
46

    
47
    public Folder(String name) {
48
        this.name = name;
49
    }
50
    
51
    public String getName() {
52
        return name;
53
    }
54

    
55
    public Date getLastModified() {
56
        return lastModified;
57
    }
58

    
59
    public long getBytesUsed() {
60
        return bytesUsed;
61
    }
62

    
63
    public void setLastModified(Date lastModified) {
64
        this.lastModified = lastModified;
65
    }
66

    
67
    public Set<Folder> getSubfolders() {
68
        return subfolders;
69
    }
70

    
71
    public void setSubfolders(Set<Folder> subfolders) {
72
        this.subfolders = subfolders;
73
    }
74

    
75
    public String getContainer() {
76
        return container;
77
    }
78

    
79
    public String getPrefix() {
80
        return prefix;
81
    }
82

    
83
    public void setPrefix(String prefix) {
84
        this.prefix = prefix;
85
    }
86

    
87
    public void populate(Response response) {
88
        String header = response.getHeader("Last-Modified");
89
        if (header != null)
90
            lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
91

    
92
        header = response.getHeader("X-Container-Bytes-Used");
93
        if (header != null)
94
            bytesUsed = Long.valueOf(header);
95

    
96
        JSONValue json = JSONParser.parseStrict(response.getText());
97
        JSONArray array = json.isArray();
98
        if (array != null) {
99
            for (int i=0; i<array.size(); i++) {
100
                JSONObject o = array.get(i).isObject();
101
                if (o != null) {
102
                    String contentType = unmarshallString(o, "content_type");
103
                    if (o.containsKey("subdir") || (contentType != null && contentType.startsWith("application/directory"))) {
104
                        Folder f = new Folder();
105
                        f.populate(o, container);
106
                        subfolders.add(f);
107
                    }
108
                    else {
109
                        File file = new File();
110
                        file.populate(o);
111
                        files.add(file);
112
                    }
113
                }
114
            }
115
        }
116
    }
117

    
118
    public void populate(JSONObject o, String aContainer) {
119
        String path = null;
120
        if (o.containsKey("subdir")) {
121
            path = unmarshallString(o, "subdir");
122
        }
123
        else {
124
            path = unmarshallString(o, "name");
125
            lastModified = unmarshallDate(o, "last_modified");
126
        }
127
        if (path.endsWith("/"))
128
            path = path.substring(0, path.length() - 1);
129
        if (path.contains("/"))
130
            name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
131
        else
132
            name = path;
133
        if (aContainer != null) {
134
            container = aContainer;
135
            prefix = path;
136
        }
137
        else {
138
            container = name;
139
            prefix = "";
140
        }
141
    }
142

    
143
    @Override
144
    public String getLastModifiedSince() {
145
        return null;  //To change body of implemented methods use File | Settings | File Templates.
146
    }
147

    
148
    public static Folder createFromResponse(Response response, Folder result) {
149
        Folder f = null;
150
        if (result == null)
151
            f = new Folder();
152
        else
153
            f = result;
154

    
155
        f.populate(response);
156
        return f;
157
    }
158

    
159
    @Override
160
    public boolean equals(Object other) {
161
        if (other instanceof Folder) {
162
            Folder o = (Folder) other;
163
            if (container != null)
164
                return prefix.equals(o.getPrefix()) && container.equals(o.getContainer());
165
            else
166
                return o.getContainer() == null && name.equals(o.getName());
167
        }
168
        return false;
169
    }
170

    
171
    @Override
172
    public int hashCode() {
173
        return prefix.hashCode() + name.hashCode();
174
    }
175

    
176
    public Set<File> getFiles() {
177
        return files;
178
    }
179
}