Revision 5cd18037 web_client/src/gr/grnet/pithos/web/client/foldertree/Folder.java

b/web_client/src/gr/grnet/pithos/web/client/foldertree/Folder.java
4 4

  
5 5
package gr.grnet.pithos.web.client.foldertree;
6 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;
7 14
import java.util.ArrayList;
15
import java.util.Date;
16
import java.util.LinkedHashSet;
8 17
import java.util.List;
18
import java.util.Set;
19
import org.w3c.css.sac.ElementSelector;
9 20

  
10
public class Folder {
11
    private String uri;
21
public class Folder extends Resource {
22
    private String name = null;
12 23

  
13
    private String name;
24
    private Date lastModified = null;
14 25

  
15
    private List<Folder> subfolders = new ArrayList<Folder>();
26
    private long bytesUsed = 0;
27

  
28
    private Set<Folder> subfolders = new LinkedHashSet<Folder>();
29
    private String container = null;
30

  
31
    private String prefix = "";
32

  
33
    public Folder() {};
16 34

  
17 35
    public Folder(String name) {
18 36
        this.name = name;
19 37
    }
38
    
39
    public String getName() {
40
        return name;
41
    }
20 42

  
21
    public String getUri() {
22
        return uri;
43
    public Date getLastModified() {
44
        return lastModified;
23 45
    }
24 46

  
25
    public String getName() {
26
        return name;
47
    public long getBytesUsed() {
48
        return bytesUsed;
27 49
    }
28 50

  
29
    public List<Folder> getSubfolders() {
51
    public void setLastModified(Date lastModified) {
52
        this.lastModified = lastModified;
53
    }
54

  
55
    public Set<Folder> getSubfolders() {
30 56
        return subfolders;
31 57
    }
32 58

  
33
    public void setSubfolders(List<Folder> subfolders) {
59
    public void setSubfolders(Set<Folder> subfolders) {
34 60
        this.subfolders = subfolders;
35 61
    }
62

  
63
    public String getContainer() {
64
        return container;
65
    }
66

  
67
    public void setContainer(String container) {
68
        this.container = container;
69
    }
70

  
71
    public String getPrefix() {
72
        return prefix;
73
    }
74

  
75
    public void setPrefix(String prefix) {
76
        this.prefix = prefix;
77
    }
78

  
79
    public void populate(Response response) {
80
        String header = response.getHeader("Last-Modified");
81
        if (header != null)
82
            lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
83

  
84
        header = response.getHeader("X-Container-Bytes-Used");
85
        if (header != null)
86
            bytesUsed = Long.valueOf(header);
87

  
88
        JSONValue json = JSONParser.parseStrict(response.getText());
89
        JSONArray array = json.isArray();
90
        if (array != null) {
91
            for (int i=0; i<array.size(); i++) {
92
                JSONObject o = array.get(i).isObject();
93
                if (o != null) {
94
                    if (o.containsKey("subdir")) {
95
                        Folder f = new Folder();
96
                        f.populate(o);
97
                        f.setContainer(container == null ? name : container);
98
                        f.setPrefix(container == null ? f.getName() : prefix + "/" + f.getName());
99
                        subfolders.add(f);
100
                    }
101
                    else {
102
                        String contentType = unmarshallString(o, "content_type");
103
                        if (contentType != null && contentType.startsWith("application/directory")) {
104
                            Folder f = new Folder();
105
                            f.populate(o);
106
                            f.setContainer(container == null ? name : container);
107
                            f.setPrefix(container == null ? f.getName() : prefix + "/" + f.getName());
108
                            subfolders.add(f);
109
                        }
110
                        else {
111
                            // add file
112
                        }
113
                    }
114
                }
115
            }
116
        }
117
    }
118

  
119
    public void populate(JSONObject o) {
120
        if (o.containsKey("subdir")) {
121
            name = unmarshallString(o, "subdir");
122
            if (name.endsWith("/"))
123
                name = name.substring(0, name.length() - 1);
124
        }
125
        else {
126
            name = unmarshallString(o, "name");
127
            lastModified = unmarshallDate(o, "last_modified");
128
        }
129
    }
130

  
131
    @Override
132
    public String getLastModifiedSince() {
133
        return null;  //To change body of implemented methods use File | Settings | File Templates.
134
    }
135

  
136
    public static Folder createFromResponse(Response response, Folder result) {
137
        Folder f = null;
138
        if (result == null)
139
            f = new Folder();
140
        else
141
            f = result;
142

  
143
        f.populate(response);
144
        return f;
145
    }
146

  
147
    @Override
148
    public boolean equals(Object other) {
149
        if (other instanceof Folder) {
150
            Folder o = (Folder) other;
151
            return name.equals(o.getName()) && prefix.equals(o.getPrefix());
152
        }
153
        return false;
154
    }
155

  
156
    @Override
157
    public int hashCode() {
158
        return prefix.hashCode() + name.hashCode();
159
    }
36 160
}

Also available in: Unified diff