Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / othersharedtree / OtherSharedTreeViewModel.java @ 9539e23d

History | View | Annotate | Download (17.3 kB)

1 6d8e0f58 Christos Stathis
/*
2 6d8e0f58 Christos Stathis
 * Copyright 2011 GRNET S.A. All rights reserved.
3 6d8e0f58 Christos Stathis
 *
4 6d8e0f58 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 6d8e0f58 Christos Stathis
 * without modification, are permitted provided that the following
6 6d8e0f58 Christos Stathis
 * conditions are met:
7 6d8e0f58 Christos Stathis
 *
8 6d8e0f58 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 6d8e0f58 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 6d8e0f58 Christos Stathis
 *      disclaimer.
11 6d8e0f58 Christos Stathis
 *
12 6d8e0f58 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 6d8e0f58 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 6d8e0f58 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 6d8e0f58 Christos Stathis
 *      provided with the distribution.
16 6d8e0f58 Christos Stathis
 *
17 6d8e0f58 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 6d8e0f58 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 6d8e0f58 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 6d8e0f58 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 6d8e0f58 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 6d8e0f58 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 6d8e0f58 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 6d8e0f58 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 6d8e0f58 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 6d8e0f58 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 6d8e0f58 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 6d8e0f58 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 6d8e0f58 Christos Stathis
 *
30 6d8e0f58 Christos Stathis
 * The views and conclusions contained in the software and
31 6d8e0f58 Christos Stathis
 * documentation are those of the authors and should not be
32 6d8e0f58 Christos Stathis
 * interpreted as representing official policies, either expressed
33 6d8e0f58 Christos Stathis
 * or implied, of GRNET S.A.
34 6d8e0f58 Christos Stathis
 */
35 6d8e0f58 Christos Stathis
36 6d8e0f58 Christos Stathis
package gr.grnet.pithos.web.client.othersharedtree;
37 6d8e0f58 Christos Stathis
38 6d8e0f58 Christos Stathis
import gr.grnet.pithos.web.client.FolderContextMenu;
39 6d8e0f58 Christos Stathis
import gr.grnet.pithos.web.client.Pithos;
40 6d8e0f58 Christos Stathis
import gr.grnet.pithos.web.client.SharingUsers;
41 220831bd Christos Stathis
import gr.grnet.pithos.web.client.foldertree.AccountResource;
42 220831bd Christos Stathis
import gr.grnet.pithos.web.client.foldertree.File;
43 6d8e0f58 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Folder;
44 6d8e0f58 Christos Stathis
import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeView.Templates;
45 6d8e0f58 Christos Stathis
import gr.grnet.pithos.web.client.rest.GetRequest;
46 6d8e0f58 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
47 6d8e0f58 Christos Stathis
48 6d8e0f58 Christos Stathis
import java.util.HashMap;
49 220831bd Christos Stathis
import java.util.HashSet;
50 6d8e0f58 Christos Stathis
import java.util.Iterator;
51 6d8e0f58 Christos Stathis
import java.util.Map;
52 220831bd Christos Stathis
import java.util.Set;
53 6d8e0f58 Christos Stathis
54 6d8e0f58 Christos Stathis
import com.google.gwt.cell.client.AbstractCell;
55 6d8e0f58 Christos Stathis
import com.google.gwt.cell.client.Cell;
56 6d8e0f58 Christos Stathis
import com.google.gwt.cell.client.TextCell;
57 6d8e0f58 Christos Stathis
import com.google.gwt.cell.client.ValueUpdater;
58 6d8e0f58 Christos Stathis
import com.google.gwt.core.client.GWT;
59 6d8e0f58 Christos Stathis
import com.google.gwt.core.client.Scheduler;
60 6d8e0f58 Christos Stathis
import com.google.gwt.event.dom.client.ContextMenuEvent;
61 9539e23d Christos Stathis
import com.google.gwt.http.client.Response;
62 6d8e0f58 Christos Stathis
import com.google.gwt.safehtml.shared.SafeHtml;
63 6d8e0f58 Christos Stathis
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
64 6d8e0f58 Christos Stathis
import com.google.gwt.text.shared.SafeHtmlRenderer;
65 6d8e0f58 Christos Stathis
import com.google.gwt.user.client.Command;
66 6d8e0f58 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
67 6d8e0f58 Christos Stathis
import com.google.gwt.view.client.ListDataProvider;
68 6d8e0f58 Christos Stathis
import com.google.gwt.view.client.SelectionChangeEvent;
69 6d8e0f58 Christos Stathis
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
70 6d8e0f58 Christos Stathis
import com.google.gwt.view.client.SingleSelectionModel;
71 6d8e0f58 Christos Stathis
import com.google.gwt.view.client.TreeViewModel;
72 6d8e0f58 Christos Stathis
73 6d8e0f58 Christos Stathis
public class OtherSharedTreeViewModel implements TreeViewModel {
74 6d8e0f58 Christos Stathis
75 6d8e0f58 Christos Stathis
    protected Pithos app;
76 6d8e0f58 Christos Stathis
77 6d8e0f58 Christos Stathis
    private Cell<Folder> folderCell = new AbstractCell<Folder>(ContextMenuEvent.getType().getName()) {
78 6d8e0f58 Christos Stathis
79 6d8e0f58 Christos Stathis
       @Override
80 7811b9d1 Christos Stathis
        public void render(@SuppressWarnings("unused") Context context, Folder folder, SafeHtmlBuilder safeHtmlBuilder) {
81 6d8e0f58 Christos Stathis
            String html = AbstractImagePrototype.create(OtherSharedTreeView.images.folderYellow()).getHTML();
82 6d8e0f58 Christos Stathis
            safeHtmlBuilder.appendHtmlConstant(html);
83 6d8e0f58 Christos Stathis
            safeHtmlBuilder.append(Templates.INSTANCE.nameSpan(folder.getName()));
84 6d8e0f58 Christos Stathis
        }
85 6d8e0f58 Christos Stathis
86 6d8e0f58 Christos Stathis
        @Override
87 7811b9d1 Christos Stathis
        public void onBrowserEvent(@SuppressWarnings("unused") Context context, @SuppressWarnings("unused") com.google.gwt.dom.client.Element parent, Folder folder, com.google.gwt.dom.client.NativeEvent event, @SuppressWarnings("unused") ValueUpdater<Folder> valueUpdater) {
88 6d8e0f58 Christos Stathis
            if (event.getType().equals(ContextMenuEvent.getType().getName())) {
89 6d8e0f58 Christos Stathis
                OtherSharedTreeViewModel.this.selectionModel.setSelected(folder, true);
90 02d3a335 Christos Stathis
                FolderContextMenu menu = new FolderContextMenu(app, OtherSharedTreeView.images, app.getSelectedTree(), folder);
91 6d8e0f58 Christos Stathis
                menu.setPopupPosition(event.getClientX(), event.getClientY());
92 6d8e0f58 Christos Stathis
                menu.show();
93 6d8e0f58 Christos Stathis
            }
94 6d8e0f58 Christos Stathis
        }
95 6d8e0f58 Christos Stathis
    };
96 6d8e0f58 Christos Stathis
97 6d8e0f58 Christos Stathis
    private ListDataProvider<String> rootDataProvider = new ListDataProvider<String>();
98 220831bd Christos Stathis
    protected ListDataProvider<String> userLevelDataProvider = new ListDataProvider<String>();
99 6d8e0f58 Christos Stathis
100 220831bd Christos Stathis
    protected Map<String, ListDataProvider<Folder>> userDataProviderMap = new HashMap<String, ListDataProvider<Folder>>();
101 6d8e0f58 Christos Stathis
    private Map<Folder, ListDataProvider<Folder>> dataProviderMap = new HashMap<Folder, ListDataProvider<Folder>>();
102 220831bd Christos Stathis
    
103 220831bd Christos Stathis
    protected Map<String, Set<File>> sharedFiles = new HashMap<String, Set<File>>();
104 6d8e0f58 Christos Stathis
105 220831bd Christos Stathis
    protected SingleSelectionModel<Folder> selectionModel;
106 6d8e0f58 Christos Stathis
107 6d8e0f58 Christos Stathis
    public OtherSharedTreeViewModel(Pithos _app, SingleSelectionModel<Folder> selectionModel) {
108 6d8e0f58 Christos Stathis
        app = _app;
109 6d8e0f58 Christos Stathis
        this.selectionModel = selectionModel;
110 6d8e0f58 Christos Stathis
    }
111 6d8e0f58 Christos Stathis
112 6d8e0f58 Christos Stathis
    @Override
113 6d8e0f58 Christos Stathis
    public <T> NodeInfo<?> getNodeInfo(T value) {
114 6d8e0f58 Christos Stathis
        if (value == null) {
115 6d8e0f58 Christos Stathis
            rootDataProvider.getList().add("Other 's Shared");
116 6d8e0f58 Christos Stathis
            final SingleSelectionModel<String> selectionModel2 = new SingleSelectionModel<String>();
117 6d8e0f58 Christos Stathis
            selectionModel2.addSelectionChangeHandler(new Handler() {
118 6d8e0f58 Christos Stathis
119 6d8e0f58 Christos Stathis
                @Override
120 7811b9d1 Christos Stathis
                public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
121 6d8e0f58 Christos Stathis
                    if (selectionModel2.getSelectedObject() != null) {
122 f1b24b82 Christos Stathis
                            app.deselectOthers(app.getOtherSharedTreeView(), selectionModel2);
123 3ebb88f4 Christos Stathis
                            app.applyPermissions(null);
124 220831bd Christos Stathis
                            app.showFiles(new HashSet<File>());
125 6d8e0f58 Christos Stathis
                    }
126 6d8e0f58 Christos Stathis
                }
127 6d8e0f58 Christos Stathis
            });
128 6d8e0f58 Christos Stathis
            app.addSelectionModel(selectionModel2);
129 6d8e0f58 Christos Stathis
            return new DefaultNodeInfo<String>(rootDataProvider, new TextCell(new SafeHtmlRenderer<String>() {
130 6d8e0f58 Christos Stathis
                @Override
131 6d8e0f58 Christos Stathis
                public SafeHtml render(String object) {
132 6d8e0f58 Christos Stathis
                    SafeHtmlBuilder builder = new SafeHtmlBuilder();
133 6d8e0f58 Christos Stathis
                    render(object, builder);
134 6d8e0f58 Christos Stathis
                    return builder.toSafeHtml();
135 6d8e0f58 Christos Stathis
                }
136 6d8e0f58 Christos Stathis
137 6d8e0f58 Christos Stathis
                @Override
138 6d8e0f58 Christos Stathis
                public void render(String object, SafeHtmlBuilder builder) {
139 6d8e0f58 Christos Stathis
                    String html = AbstractImagePrototype.create(OtherSharedTreeView.images.othersShared()).getHTML();
140 6d8e0f58 Christos Stathis
                    builder.appendHtmlConstant(html);
141 6d8e0f58 Christos Stathis
                    builder.append(OtherSharedTreeView.Templates.INSTANCE.nameSpan(object));
142 6d8e0f58 Christos Stathis
                }
143 6d8e0f58 Christos Stathis
            }),  selectionModel2, null);
144 6d8e0f58 Christos Stathis
        }
145 6d8e0f58 Christos Stathis
        else if (value instanceof String) {
146 6d8e0f58 Christos Stathis
                if (value.equals("Other 's Shared")) {
147 220831bd Christos Stathis
                        fetchSharingUsers();
148 6d8e0f58 Christos Stathis
                    final SingleSelectionModel<String> selectionModel3 = new SingleSelectionModel<String>();
149 6d8e0f58 Christos Stathis
                    selectionModel3.addSelectionChangeHandler(new Handler() {
150 6d8e0f58 Christos Stathis
151 6d8e0f58 Christos Stathis
                        @Override
152 7811b9d1 Christos Stathis
                        public void onSelectionChange(@SuppressWarnings("unused") SelectionChangeEvent event) {
153 6d8e0f58 Christos Stathis
                            if (selectionModel3.getSelectedObject() != null) {
154 f1b24b82 Christos Stathis
                                    app.deselectOthers(app.getOtherSharedTreeView(), selectionModel3);
155 3ebb88f4 Christos Stathis
                                    app.applyPermissions(null);
156 220831bd Christos Stathis
                                    String username = selectionModel3.getSelectedObject();
157 63ee965c Christos Stathis
                                            if (userDataProviderMap.get(username) == null) {
158 63ee965c Christos Stathis
                                                    userDataProviderMap.put(username, new ListDataProvider<Folder>());
159 63ee965c Christos Stathis
                                            }
160 220831bd Christos Stathis
                                    fetchSharedFiles(username, userDataProviderMap.get(username));
161 6d8e0f58 Christos Stathis
                            }
162 6d8e0f58 Christos Stathis
                        }
163 6d8e0f58 Christos Stathis
                    });
164 6d8e0f58 Christos Stathis
                    app.addSelectionModel(selectionModel3);
165 220831bd Christos Stathis
                    return new DefaultNodeInfo<String>(userLevelDataProvider, new TextCell(new SafeHtmlRenderer<String>() {
166 6d8e0f58 Christos Stathis
167 6d8e0f58 Christos Stathis
                                        @Override
168 6d8e0f58 Christos Stathis
                                        public SafeHtml render(String object) {
169 6d8e0f58 Christos Stathis
                            SafeHtmlBuilder builder = new SafeHtmlBuilder();
170 6d8e0f58 Christos Stathis
                            render(object, builder);
171 6d8e0f58 Christos Stathis
                            return builder.toSafeHtml();
172 6d8e0f58 Christos Stathis
                                        }
173 6d8e0f58 Christos Stathis
174 6d8e0f58 Christos Stathis
                                        @Override
175 6d8e0f58 Christos Stathis
                                        public void render(String object, SafeHtmlBuilder builder) {
176 220831bd Christos Stathis
                            String html = AbstractImagePrototype.create(OtherSharedTreeView.images.user()).getHTML();
177 6d8e0f58 Christos Stathis
                            builder.appendHtmlConstant(html);
178 6d8e0f58 Christos Stathis
                            builder.append(OtherSharedTreeView.Templates.INSTANCE.nameSpan(object));
179 6d8e0f58 Christos Stathis
                                        }
180 6d8e0f58 Christos Stathis
                                }), selectionModel3, null);
181 6d8e0f58 Christos Stathis
                }
182 220831bd Christos Stathis
                        String username = (String) value;
183 220831bd Christos Stathis
                        if (userDataProviderMap.get(username) == null) {
184 220831bd Christos Stathis
                                userDataProviderMap.put(username, new ListDataProvider<Folder>());
185 220831bd Christos Stathis
                        }
186 220831bd Christos Stathis
                        final ListDataProvider<Folder> dataProvider = userDataProviderMap.get(username);
187 220831bd Christos Stathis
                        fetchSharedContainers(username, dataProvider);
188 220831bd Christos Stathis
                        return new DefaultNodeInfo<Folder>(dataProvider, folderCell, selectionModel, null);
189 6d8e0f58 Christos Stathis
        }
190 6d8e0f58 Christos Stathis
        else {
191 6d8e0f58 Christos Stathis
            final Folder f = (Folder) value;
192 6d8e0f58 Christos Stathis
            if (dataProviderMap.get(f) == null) {
193 6d8e0f58 Christos Stathis
                dataProviderMap.put(f, new ListDataProvider<Folder>());
194 6d8e0f58 Christos Stathis
            }
195 6d8e0f58 Christos Stathis
            final ListDataProvider<Folder> dataProvider = dataProviderMap.get(f);
196 6d8e0f58 Christos Stathis
            fetchFolder(f, dataProvider, false);
197 6d8e0f58 Christos Stathis
            return new DefaultNodeInfo<Folder>(dataProvider, folderCell, selectionModel, null);
198 6d8e0f58 Christos Stathis
        }
199 6d8e0f58 Christos Stathis
    }
200 6d8e0f58 Christos Stathis
201 220831bd Christos Stathis
    private void fetchSharingUsers() {
202 6d8e0f58 Christos Stathis
        GetRequest<SharingUsers> getSharingUsers = new GetRequest<SharingUsers>(SharingUsers.class, app.getApiPath(), "", "?format=json") {
203 6d8e0f58 Christos Stathis
            @Override
204 7811b9d1 Christos Stathis
            public void onSuccess(final SharingUsers _result) {
205 220831bd Christos Stathis
                userLevelDataProvider.getList().clear();
206 7811b9d1 Christos Stathis
                userLevelDataProvider.getList().addAll(_result.getUsers());
207 7811b9d1 Christos Stathis
                for (String name : _result.getUsers()) {
208 220831bd Christos Stathis
                        sharedFiles.put(name, new HashSet<File>());
209 220831bd Christos Stathis
                }
210 6d8e0f58 Christos Stathis
            }
211 6d8e0f58 Christos Stathis
212 6d8e0f58 Christos Stathis
            @Override
213 6d8e0f58 Christos Stathis
            public void onError(Throwable t) {
214 6d8e0f58 Christos Stathis
                GWT.log("Error getting folder", t);
215 6d8e0f58 Christos Stathis
                if (t instanceof RestException)
216 6d8e0f58 Christos Stathis
                    app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
217 6d8e0f58 Christos Stathis
                else
218 6d8e0f58 Christos Stathis
                    app.displayError("System error fetching folder: " + t.getMessage());
219 6d8e0f58 Christos Stathis
            }
220 9539e23d Christos Stathis
221 9539e23d Christos Stathis
                        @Override
222 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
223 9539e23d Christos Stathis
                                app.sessionExpired();
224 9539e23d Christos Stathis
                        }
225 6d8e0f58 Christos Stathis
        };
226 6d8e0f58 Christos Stathis
        getSharingUsers.setHeader("X-Auth-Token", app.getToken());
227 6d8e0f58 Christos Stathis
        Scheduler.get().scheduleDeferred(getSharingUsers);
228 6d8e0f58 Christos Stathis
        }
229 6d8e0f58 Christos Stathis
230 6d8e0f58 Christos Stathis
        @Override
231 6d8e0f58 Christos Stathis
    public boolean isLeaf(Object o) {
232 6d8e0f58 Christos Stathis
        if (o instanceof Folder) {
233 6d8e0f58 Christos Stathis
            Folder f = (Folder) o;
234 6d8e0f58 Christos Stathis
            return f.getSubfolders().isEmpty();
235 6d8e0f58 Christos Stathis
        }
236 6d8e0f58 Christos Stathis
        return false;
237 6d8e0f58 Christos Stathis
    }
238 6d8e0f58 Christos Stathis
239 220831bd Christos Stathis
        private void fetchSharedContainers(final String username, final ListDataProvider<Folder> dataProvider) {
240 220831bd Christos Stathis
                GetRequest<AccountResource> getUserSharedContainers = new GetRequest<AccountResource>(AccountResource.class, app.getApiPath(), username, "?format=json") {
241 220831bd Christos Stathis
242 220831bd Christos Stathis
                        @Override
243 7811b9d1 Christos Stathis
                        public void onSuccess(AccountResource _result) {
244 220831bd Christos Stathis
                            final ListDataProvider<Folder> tempProvider = new ListDataProvider<Folder>();
245 7811b9d1 Christos Stathis
                                Iterator<Folder> iter = _result.getContainers().iterator();
246 220831bd Christos Stathis
                                fetchFolder(username, iter, tempProvider, new Command() {
247 220831bd Christos Stathis
                                        
248 220831bd Christos Stathis
                                        @Override
249 220831bd Christos Stathis
                                        public void execute() {
250 220831bd Christos Stathis
                                                dataProvider.getList().clear();
251 220831bd Christos Stathis
                                                dataProvider.getList().addAll(tempProvider.getList());
252 220831bd Christos Stathis
                                        }
253 220831bd Christos Stathis
                                });
254 220831bd Christos Stathis
                        }
255 220831bd Christos Stathis
256 220831bd Christos Stathis
                        @Override
257 220831bd Christos Stathis
                        public void onError(Throwable t) {
258 220831bd Christos Stathis
                GWT.log("Error getting account", t);
259 220831bd Christos Stathis
                if (t instanceof RestException)
260 220831bd Christos Stathis
                    app.displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
261 220831bd Christos Stathis
                else
262 220831bd Christos Stathis
                    app.displayError("System error fetching user data: " + t.getMessage());
263 220831bd Christos Stathis
                        }
264 9539e23d Christos Stathis
265 9539e23d Christos Stathis
                        @Override
266 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
267 9539e23d Christos Stathis
                                app.sessionExpired();
268 9539e23d Christos Stathis
                        }
269 220831bd Christos Stathis
                };
270 220831bd Christos Stathis
                getUserSharedContainers.setHeader("X-Auth-Token", app.getToken());
271 220831bd Christos Stathis
                Scheduler.get().scheduleDeferred(getUserSharedContainers);
272 220831bd Christos Stathis
        }
273 220831bd Christos Stathis
274 220831bd Christos Stathis
        protected void fetchSharedFiles(final String username, final ListDataProvider<Folder> dataProvider) {
275 220831bd Christos Stathis
                GetRequest<AccountResource> getUserSharedContainers = new GetRequest<AccountResource>(AccountResource.class, app.getApiPath(), username, "?format=json") {
276 220831bd Christos Stathis
277 220831bd Christos Stathis
                        @Override
278 7811b9d1 Christos Stathis
                        public void onSuccess(AccountResource _result) {
279 220831bd Christos Stathis
                            final ListDataProvider<Folder> tempProvider = new ListDataProvider<Folder>();
280 7811b9d1 Christos Stathis
                                Iterator<Folder> iter = _result.getContainers().iterator();
281 220831bd Christos Stathis
                                fetchFolder(username, iter, tempProvider, new Command() {
282 220831bd Christos Stathis
                                        
283 220831bd Christos Stathis
                                        @Override
284 220831bd Christos Stathis
                                        public void execute() {
285 220831bd Christos Stathis
                                                dataProvider.getList().clear();
286 220831bd Christos Stathis
                                                dataProvider.getList().addAll(tempProvider.getList());
287 220831bd Christos Stathis
                                                app.showFiles(sharedFiles.get(username));
288 220831bd Christos Stathis
                                        }
289 220831bd Christos Stathis
                                });
290 220831bd Christos Stathis
                        }
291 220831bd Christos Stathis
292 220831bd Christos Stathis
                        @Override
293 220831bd Christos Stathis
                        public void onError(Throwable t) {
294 220831bd Christos Stathis
                GWT.log("Error getting account", t);
295 220831bd Christos Stathis
                if (t instanceof RestException)
296 220831bd Christos Stathis
                    app.displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
297 220831bd Christos Stathis
                else
298 220831bd Christos Stathis
                    app.displayError("System error fetching user data: " + t.getMessage());
299 220831bd Christos Stathis
                        }
300 9539e23d Christos Stathis
301 9539e23d Christos Stathis
                        @Override
302 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
303 9539e23d Christos Stathis
                                app.sessionExpired();
304 9539e23d Christos Stathis
                        }
305 220831bd Christos Stathis
                };
306 220831bd Christos Stathis
                getUserSharedContainers.setHeader("X-Auth-Token", app.getToken());
307 220831bd Christos Stathis
                Scheduler.get().scheduleDeferred(getUserSharedContainers);
308 220831bd Christos Stathis
        }
309 220831bd Christos Stathis
310 220831bd Christos Stathis
        protected void fetchFolder(final String username, final Iterator<Folder> iter, final ListDataProvider<Folder> dataProvider, final Command callback) {
311 6d8e0f58 Christos Stathis
        if (iter.hasNext()) {
312 6d8e0f58 Christos Stathis
            final Folder f = iter.next();
313 6d8e0f58 Christos Stathis
314 6d8e0f58 Christos Stathis
            String path = "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + f.getPrefix();
315 220831bd Christos Stathis
            GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), username, path, f) {
316 6d8e0f58 Christos Stathis
                @Override
317 7811b9d1 Christos Stathis
                public void onSuccess(Folder _result) {
318 7811b9d1 Christos Stathis
                        if (!_result.isShared()) {
319 7811b9d1 Christos Stathis
                                for (File file : _result.getFiles()) {
320 220831bd Christos Stathis
                                        if (file.isShared())
321 220831bd Christos Stathis
                                                sharedFiles.get(username).add(file);
322 220831bd Christos Stathis
                                }
323 7811b9d1 Christos Stathis
                                Iterator<Folder> iter2 = _result.getSubfolders().iterator();
324 220831bd Christos Stathis
                                fetchFolder(username, iter2, dataProvider, new Command() {
325 220831bd Christos Stathis
                                                        
326 220831bd Christos Stathis
                                                        @Override
327 220831bd Christos Stathis
                                                        public void execute() {
328 220831bd Christos Stathis
                                            fetchFolder(username, iter, dataProvider, callback);
329 220831bd Christos Stathis
                                                        }
330 220831bd Christos Stathis
                                                });
331 220831bd Christos Stathis
                        }
332 220831bd Christos Stathis
                        else {
333 7811b9d1 Christos Stathis
                                dataProvider.getList().add(_result);
334 220831bd Christos Stathis
                            fetchFolder(username, iter, dataProvider, callback);
335 220831bd Christos Stathis
                        }
336 6d8e0f58 Christos Stathis
                }
337 6d8e0f58 Christos Stathis
338 6d8e0f58 Christos Stathis
                @Override
339 6d8e0f58 Christos Stathis
                public void onError(Throwable t) {
340 6d8e0f58 Christos Stathis
                    GWT.log("Error getting folder", t);
341 6d8e0f58 Christos Stathis
                    if (t instanceof RestException)
342 6d8e0f58 Christos Stathis
                        app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
343 6d8e0f58 Christos Stathis
                    else
344 6d8e0f58 Christos Stathis
                        app.displayError("System error fetching folder: " + t.getMessage());
345 6d8e0f58 Christos Stathis
                }
346 9539e23d Christos Stathis
347 9539e23d Christos Stathis
                                @Override
348 9539e23d Christos Stathis
                                protected void onUnauthorized(Response response) {
349 9539e23d Christos Stathis
                                        app.sessionExpired();
350 9539e23d Christos Stathis
                                }
351 6d8e0f58 Christos Stathis
            };
352 6d8e0f58 Christos Stathis
            getFolder.setHeader("X-Auth-Token", app.getToken());
353 6d8e0f58 Christos Stathis
            Scheduler.get().scheduleDeferred(getFolder);
354 6d8e0f58 Christos Stathis
        }
355 6d8e0f58 Christos Stathis
        else if (callback != null)
356 6d8e0f58 Christos Stathis
            callback.execute();
357 6d8e0f58 Christos Stathis
    }
358 6d8e0f58 Christos Stathis
359 6d8e0f58 Christos Stathis
    public Folder getSelection() {
360 6d8e0f58 Christos Stathis
        return selectionModel.getSelectedObject();
361 6d8e0f58 Christos Stathis
    }
362 6d8e0f58 Christos Stathis
363 6d8e0f58 Christos Stathis
    public void updateFolder(Folder folder, boolean showfiles) {
364 6d8e0f58 Christos Stathis
        if (dataProviderMap.get(folder) == null) {
365 6d8e0f58 Christos Stathis
            dataProviderMap.put(folder, new ListDataProvider<Folder>());
366 6d8e0f58 Christos Stathis
        }
367 6d8e0f58 Christos Stathis
        final ListDataProvider<Folder> dataProvider = dataProviderMap.get(folder);
368 6d8e0f58 Christos Stathis
        fetchFolder(folder, dataProvider, showfiles);
369 6d8e0f58 Christos Stathis
    }
370 6d8e0f58 Christos Stathis
371 6d8e0f58 Christos Stathis
    public void fetchFolder(final Folder f, final ListDataProvider<Folder> dataProvider, final boolean showfiles) {
372 6d8e0f58 Christos Stathis
        String path = "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + f.getPrefix();
373 220831bd Christos Stathis
        GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), path, f) {
374 6d8e0f58 Christos Stathis
            @Override
375 7811b9d1 Christos Stathis
            public void onSuccess(final Folder _result) {
376 6d8e0f58 Christos Stathis
                if (showfiles)
377 7811b9d1 Christos Stathis
                    app.showFiles(_result);
378 7811b9d1 Christos Stathis
                Iterator<Folder> iter = _result.getSubfolders().iterator();
379 7811b9d1 Christos Stathis
                fetchFolder(_result.getOwner(), iter, dataProvider, new Command() {
380 6d8e0f58 Christos Stathis
                    @Override
381 6d8e0f58 Christos Stathis
                    public void execute() {
382 6d8e0f58 Christos Stathis
                        dataProvider.getList().clear();
383 7811b9d1 Christos Stathis
                        dataProvider.getList().addAll(_result.getSubfolders());
384 6d8e0f58 Christos Stathis
                        app.getOtherSharedTreeView().updateChildren(f);
385 6d8e0f58 Christos Stathis
                    }
386 6d8e0f58 Christos Stathis
                });
387 6d8e0f58 Christos Stathis
            }
388 6d8e0f58 Christos Stathis
389 6d8e0f58 Christos Stathis
            @Override
390 6d8e0f58 Christos Stathis
            public void onError(Throwable t) {
391 6d8e0f58 Christos Stathis
                GWT.log("Error getting folder", t);
392 6d8e0f58 Christos Stathis
                if (t instanceof RestException)
393 6d8e0f58 Christos Stathis
                    app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
394 6d8e0f58 Christos Stathis
                else
395 6d8e0f58 Christos Stathis
                    app.displayError("System error fetching folder: " + t.getMessage());
396 6d8e0f58 Christos Stathis
            }
397 9539e23d Christos Stathis
398 9539e23d Christos Stathis
                        @Override
399 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
400 9539e23d Christos Stathis
                                app.sessionExpired();
401 9539e23d Christos Stathis
                        }
402 6d8e0f58 Christos Stathis
        };
403 6d8e0f58 Christos Stathis
        getFolder.setHeader("X-Auth-Token", app.getToken());
404 6d8e0f58 Christos Stathis
        Scheduler.get().scheduleDeferred(getFolder);
405 6d8e0f58 Christos Stathis
    }
406 6d8e0f58 Christos Stathis
}