Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / foldertree / FolderTreeViewModel.java @ 58777026

History | View | Annotate | Download (6.5 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.core.client.GWT;
39
import com.google.gwt.core.client.Scheduler;
40
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
41
import com.google.gwt.view.client.ListDataProvider;
42
import com.google.gwt.view.client.SelectionChangeEvent;
43
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
44
import com.google.gwt.view.client.SingleSelectionModel;
45
import com.google.gwt.view.client.TreeViewModel;
46
import gr.grnet.pithos.web.client.GSS;
47
import gr.grnet.pithos.web.client.foldertree.FolderTreeView.FolderCell;
48
import gr.grnet.pithos.web.client.rest.GetRequest;
49
import gr.grnet.pithos.web.client.rest.RestException;
50
import java.util.Iterator;
51
import java.util.Set;
52

    
53
public class FolderTreeViewModel implements TreeViewModel {
54

    
55
    private ListDataProvider<Folder> rootDataProvider = new ListDataProvider<Folder>();
56

    
57
    private SingleSelectionModel<Folder> selectionModel;
58

    
59
    public FolderTreeViewModel(SingleSelectionModel<Folder> selectionModel) {
60
        this.selectionModel = selectionModel;
61
    }
62

    
63
    @Override
64
    public <T> NodeInfo<?> getNodeInfo(T value) {
65
        if (value == null) {
66
            Folder f = new Folder("Loading ...");
67
            rootDataProvider.getList().add(f);
68
            return new DefaultNodeInfo<Folder>(rootDataProvider, new FolderCell(), selectionModel, null);
69
        }
70
        else {
71
            final Folder f = (Folder) value;
72
            final ListDataProvider<Folder> dataProvider = new ListDataProvider<Folder>();
73
            dataProvider.flush();
74
            Scheduler.get().scheduleDeferred(new ScheduledCommand() {
75
                @Override
76
                public void execute() {
77
                    final GSS app = GSS.get();
78
                    String path = app.getApiPath() + app.getUsername() + "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + f.getPrefix();
79
                    GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, path, f) {
80
                        @Override
81
                        public void onSuccess(Folder result) {
82
                            Iterator<Folder> iter = result.getSubfolders().iterator();
83
                            fetchFolder(iter, dataProvider, result.getSubfolders());
84
                        }
85

    
86
                        @Override
87
                        public void onError(Throwable t) {
88
                            GWT.log("Error getting folder", t);
89
                            if (t instanceof RestException)
90
                                GSS.get().displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
91
                            else
92
                                GSS.get().displayError("System error fetching folder: " + t.getMessage());
93
                        }
94
                    };
95
                    getFolder.setHeader("X-Auth-Token", app.getToken());
96
                    Scheduler.get().scheduleDeferred(getFolder);
97
                }
98
            });
99
            return new DefaultNodeInfo<Folder>(dataProvider, new FolderCell(), selectionModel, null);
100
        }
101
    }
102

    
103
    @Override
104
    public boolean isLeaf(Object o) {
105
        if (o instanceof Folder) {
106
            Folder f = (Folder) o;
107
            return f.getSubfolders().isEmpty();
108
        }
109
        return false;
110
    }
111

    
112
    private void fetchFolder(final Iterator<Folder> iter, final ListDataProvider<Folder> dataProvider, final Set<Folder> folders) {
113
        if (iter.hasNext()) {
114
            final Folder f = iter.next();
115

    
116
            GSS app = GSS.get();
117
            String path = app.getApiPath() + app.getUsername() + "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + f.getPrefix();
118
            GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, path, f) {
119
                @Override
120
                public void onSuccess(Folder result) {
121
                    fetchFolder(iter, dataProvider, folders);
122
                }
123

    
124
                @Override
125
                public void onError(Throwable t) {
126
                    GWT.log("Error getting folder", t);
127
                    if (t instanceof RestException)
128
                        GSS.get().displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
129
                    else
130
                        GSS.get().displayError("System error fetching folder: " + t.getMessage());
131
                }
132
            };
133
            getFolder.setHeader("X-Auth-Token", app.getToken());
134
            Scheduler.get().scheduleDeferred(getFolder);
135
        }
136
        else {
137
            dataProvider.getList().clear();
138
            dataProvider.getList().addAll(folders);
139
            if (dataProvider.equals(rootDataProvider))
140
                selectionModel.setSelected(dataProvider.getList().get(0), true);
141
        }
142
    }
143

    
144
    public void initialize(AccountResource account) {
145
        Iterator<Folder> iter = account.getContainers().iterator();
146
        fetchFolder(iter, rootDataProvider, account.getContainers());
147
    }
148

    
149
    public Folder getSelection() {
150
        return selectionModel.getSelectedObject();
151
    }
152
}