Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / mysharedtree / MysharedTreeViewModel.java @ 28e270f9

History | View | Annotate | Download (10.2 kB)

1 105b4e49 Christos Stathis
/*
2 e6e9f6e6 Christos KK Loverdos
 * Copyright 2011-2013 GRNET S.A. All rights reserved.
3 105b4e49 Christos Stathis
 *
4 105b4e49 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 105b4e49 Christos Stathis
 * without modification, are permitted provided that the following
6 105b4e49 Christos Stathis
 * conditions are met:
7 105b4e49 Christos Stathis
 *
8 105b4e49 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 105b4e49 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 105b4e49 Christos Stathis
 *      disclaimer.
11 105b4e49 Christos Stathis
 *
12 105b4e49 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 105b4e49 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 105b4e49 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 105b4e49 Christos Stathis
 *      provided with the distribution.
16 105b4e49 Christos Stathis
 *
17 105b4e49 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 105b4e49 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 105b4e49 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 105b4e49 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 105b4e49 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 105b4e49 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 105b4e49 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 105b4e49 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 105b4e49 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 105b4e49 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 105b4e49 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 105b4e49 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 105b4e49 Christos Stathis
 *
30 105b4e49 Christos Stathis
 * The views and conclusions contained in the software and
31 105b4e49 Christos Stathis
 * documentation are those of the authors and should not be
32 105b4e49 Christos Stathis
 * interpreted as representing official policies, either expressed
33 105b4e49 Christos Stathis
 * or implied, of GRNET S.A.
34 105b4e49 Christos Stathis
 */
35 105b4e49 Christos Stathis
36 105b4e49 Christos Stathis
package gr.grnet.pithos.web.client.mysharedtree;
37 105b4e49 Christos Stathis
38 86432004 Christos KK Loverdos
import gr.grnet.pithos.web.client.Const;
39 105b4e49 Christos Stathis
import gr.grnet.pithos.web.client.FolderContextMenu;
40 105b4e49 Christos Stathis
import gr.grnet.pithos.web.client.Pithos;
41 56f9134b Christos Stathis
import gr.grnet.pithos.web.client.foldertree.AccountResource;
42 ff06bcd5 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.File;
43 105b4e49 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Folder;
44 105b4e49 Christos Stathis
import gr.grnet.pithos.web.client.mysharedtree.MysharedTreeView.Templates;
45 105b4e49 Christos Stathis
import gr.grnet.pithos.web.client.rest.GetRequest;
46 105b4e49 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
47 105b4e49 Christos Stathis
48 105b4e49 Christos Stathis
import java.util.Iterator;
49 105b4e49 Christos Stathis
50 105b4e49 Christos Stathis
import com.google.gwt.cell.client.AbstractCell;
51 105b4e49 Christos Stathis
import com.google.gwt.cell.client.Cell;
52 105b4e49 Christos Stathis
import com.google.gwt.cell.client.ValueUpdater;
53 105b4e49 Christos Stathis
import com.google.gwt.core.client.GWT;
54 105b4e49 Christos Stathis
import com.google.gwt.core.client.Scheduler;
55 105b4e49 Christos Stathis
import com.google.gwt.event.dom.client.ContextMenuEvent;
56 9539e23d Christos Stathis
import com.google.gwt.http.client.Response;
57 f5023f13 Christos Stathis
import com.google.gwt.http.client.URL;
58 105b4e49 Christos Stathis
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
59 105b4e49 Christos Stathis
import com.google.gwt.user.client.Command;
60 105b4e49 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
61 105b4e49 Christos Stathis
import com.google.gwt.view.client.ListDataProvider;
62 105b4e49 Christos Stathis
import com.google.gwt.view.client.SingleSelectionModel;
63 105b4e49 Christos Stathis
import com.google.gwt.view.client.TreeViewModel;
64 105b4e49 Christos Stathis
65 105b4e49 Christos Stathis
public class MysharedTreeViewModel implements TreeViewModel {
66 105b4e49 Christos Stathis
67 105b4e49 Christos Stathis
    protected Pithos app;
68 105b4e49 Christos Stathis
69 3f62b626 Christos Stathis
    Folder dummy = new Folder("No files shared by me");
70 3f62b626 Christos Stathis
    
71 5d18aa82 Christos Stathis
    private Cell<Folder> folderCell = new AbstractCell<Folder>(ContextMenuEvent.getType().getName()) {
72 105b4e49 Christos Stathis
73 105b4e49 Christos Stathis
       @Override
74 ebead1b5 Christos Stathis
        public void render(Context context, Folder folder, SafeHtmlBuilder safeHtmlBuilder) {
75 3f62b626 Christos Stathis
           if (!folder.equals(dummy)) {
76 3f62b626 Christos Stathis
                   String html = AbstractImagePrototype.create(MysharedTreeView.images.folderYellow()).getHTML();
77 3f62b626 Christos Stathis
                    safeHtmlBuilder.appendHtmlConstant(html).appendHtmlConstant("&nbsp;");
78 3f62b626 Christos Stathis
           }
79 105b4e49 Christos Stathis
            safeHtmlBuilder.append(Templates.INSTANCE.nameSpan(folder.getName()));
80 105b4e49 Christos Stathis
        }
81 5d18aa82 Christos Stathis
82 5d18aa82 Christos Stathis
       @Override
83 5d18aa82 Christos Stathis
       public void onBrowserEvent(Context context, com.google.gwt.dom.client.Element parent, final Folder folder, com.google.gwt.dom.client.NativeEvent event, ValueUpdater<Folder> valueUpdater) {
84 5d18aa82 Christos Stathis
           if (event.getType().equals(ContextMenuEvent.getType().getName())) {
85 5d18aa82 Christos Stathis
                   final int x = event.getClientX();
86 5d18aa82 Christos Stathis
                   final int y = event.getClientY();
87 5d18aa82 Christos Stathis
               MysharedTreeViewModel.this.selectionModel.setSelected(folder, true);
88 5d18aa82 Christos Stathis
               app.scheduleFolderHeadCommand(folder, new Command() {
89 5d18aa82 Christos Stathis
                                        
90 5d18aa82 Christos Stathis
                                        @Override
91 5d18aa82 Christos Stathis
                                        public void execute() {
92 5d18aa82 Christos Stathis
                                FolderContextMenu menu = new FolderContextMenu(app, MysharedTreeView.images, app.getSelectedTree(), folder);
93 5d18aa82 Christos Stathis
                                menu.setPopupPosition(x, y);
94 5d18aa82 Christos Stathis
                                menu.show();
95 5d18aa82 Christos Stathis
                                        }
96 5d18aa82 Christos Stathis
                                });
97 5d18aa82 Christos Stathis
           }
98 5d18aa82 Christos Stathis
       }
99 105b4e49 Christos Stathis
    };
100 105b4e49 Christos Stathis
101 cf49bbc9 Christos Stathis
    protected ListDataProvider<Folder> firstLevelDataProvider = new ListDataProvider<Folder>();
102 c46c9e31 Christos Stathis
 
103 220831bd Christos Stathis
    protected SingleSelectionModel<Folder> selectionModel;
104 105b4e49 Christos Stathis
105 105b4e49 Christos Stathis
    public MysharedTreeViewModel(Pithos _app, SingleSelectionModel<Folder> selectionModel) {
106 105b4e49 Christos Stathis
        app = _app;
107 105b4e49 Christos Stathis
        this.selectionModel = selectionModel;
108 105b4e49 Christos Stathis
    }
109 105b4e49 Christos Stathis
110 105b4e49 Christos Stathis
    @Override
111 105b4e49 Christos Stathis
    public <T> NodeInfo<?> getNodeInfo(T value) {
112 105b4e49 Christos Stathis
        if (value == null) {
113 cf49bbc9 Christos Stathis
                fetchSharedContainers(null);
114 02b9d162 Christos Stathis
                if (firstLevelDataProvider.getList().get(0).equals(dummy))
115 02b9d162 Christos Stathis
                return new DefaultNodeInfo<Folder>(firstLevelDataProvider, folderCell, null, null);
116 105b4e49 Christos Stathis
            return new DefaultNodeInfo<Folder>(firstLevelDataProvider, folderCell, selectionModel, null);
117 105b4e49 Christos Stathis
        }
118 c46c9e31 Christos Stathis
        return null;
119 105b4e49 Christos Stathis
    }
120 105b4e49 Christos Stathis
121 7c54d867 Christos Stathis
        private void fetchSharedContainers(final Command callback) {
122 86432004 Christos KK Loverdos
        app.LOG("MysharedTreeViewModel::fetchSharedContainers(), callback=", callback);
123 8282005f Christos Stathis
        String path = "?format=json&shared=&public=";
124 28e270f9 Christos KK Loverdos
        GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, Pithos.getStorageAPIURL(), app.getUserID(), path) {
125 56f9134b Christos Stathis
            @Override
126 56f9134b Christos Stathis
            public void onSuccess(final AccountResource _result) {
127 c46c9e31 Christos Stathis
                                firstLevelDataProvider.getList().clear();
128 c46c9e31 Christos Stathis
                for (Folder c : _result.getContainers()) {
129 c46c9e31 Christos Stathis
                        if (c.isHome())
130 c46c9e31 Christos Stathis
                                firstLevelDataProvider.getList().add(0, c); //Pithos is always first
131 c46c9e31 Christos Stathis
                        else if (!c.isTrash())
132 c46c9e31 Christos Stathis
                                firstLevelDataProvider.getList().add(c);
133 c46c9e31 Christos Stathis
                }
134 3f62b626 Christos Stathis
                if (firstLevelDataProvider.getList().isEmpty())
135 3f62b626 Christos Stathis
                        firstLevelDataProvider.getList().add(dummy);
136 86432004 Christos KK Loverdos
                                if (callback != null) {
137 86432004 Christos KK Loverdos
                    app.LOG("MysharedTreeViewModel::fetchSharedContainers(), executing callback");
138 86432004 Christos KK Loverdos
                    callback.execute();
139 86432004 Christos KK Loverdos
                }
140 56f9134b Christos Stathis
            }
141 56f9134b Christos Stathis
142 56f9134b Christos Stathis
            @Override
143 56f9134b Christos Stathis
            public void onError(Throwable t) {
144 56f9134b Christos Stathis
                GWT.log("Error getting account", t);
145 56f9134b Christos Stathis
                                app.setError(t);
146 56f9134b Christos Stathis
                if (t instanceof RestException)
147 56f9134b Christos Stathis
                    app.displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
148 56f9134b Christos Stathis
                else
149 56f9134b Christos Stathis
                    app.displayError("System error fetching user data: " + t.getMessage());
150 56f9134b Christos Stathis
            }
151 56f9134b Christos Stathis
152 ff06bcd5 Christos Stathis
                        @Override
153 56f9134b Christos Stathis
                        protected void onUnauthorized(Response response) {
154 56f9134b Christos Stathis
                                app.sessionExpired();
155 ff06bcd5 Christos Stathis
                        }
156 56f9134b Christos Stathis
        };
157 86432004 Christos KK Loverdos
        getAccount.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
158 56f9134b Christos Stathis
        Scheduler.get().scheduleDeferred(getAccount);
159 105b4e49 Christos Stathis
        }
160 105b4e49 Christos Stathis
161 105b4e49 Christos Stathis
        @Override
162 105b4e49 Christos Stathis
    public boolean isLeaf(Object o) {
163 7c54d867 Christos Stathis
                if (o == null)
164 cf027879 Christos Stathis
                        return firstLevelDataProvider.getList().isEmpty();
165 c46c9e31 Christos Stathis
                return true;
166 105b4e49 Christos Stathis
    }
167 105b4e49 Christos Stathis
168 56f9134b Christos Stathis
    protected void fetchFolder(final Iterator<Folder> iter, final Command callback) {
169 86432004 Christos KK Loverdos
        app.LOG("MysharedTreeViewModel::fetchFolder(), iter=", iter.hasNext(), ", callback=", callback);
170 105b4e49 Christos Stathis
        if (iter.hasNext()) {
171 105b4e49 Christos Stathis
            final Folder f = iter.next();
172 105b4e49 Christos Stathis
173 8282005f Christos Stathis
            String path = "/" + f.getContainer() + "?format=json&shared=&public=&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix());
174 28e270f9 Christos KK Loverdos
            GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, Pithos.getStorageAPIURL(), f.getOwnerID(), path, f) {
175 105b4e49 Christos Stathis
                @Override
176 7811b9d1 Christos Stathis
                public void onSuccess(Folder _result) {
177 56f9134b Christos Stathis
                    fetchFolder(iter, callback);
178 105b4e49 Christos Stathis
                }
179 105b4e49 Christos Stathis
180 105b4e49 Christos Stathis
                @Override
181 105b4e49 Christos Stathis
                public void onError(Throwable t) {
182 105b4e49 Christos Stathis
                    GWT.log("Error getting folder", t);
183 3f8622d4 Christos Stathis
                                        app.setError(t);
184 105b4e49 Christos Stathis
                    if (t instanceof RestException)
185 105b4e49 Christos Stathis
                        app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
186 105b4e49 Christos Stathis
                    else
187 105b4e49 Christos Stathis
                        app.displayError("System error fetching folder: " + t.getMessage());
188 105b4e49 Christos Stathis
                }
189 9539e23d Christos Stathis
190 9539e23d Christos Stathis
                                @Override
191 9539e23d Christos Stathis
                                protected void onUnauthorized(Response response) {
192 9539e23d Christos Stathis
                                        app.sessionExpired();
193 9539e23d Christos Stathis
                                }
194 105b4e49 Christos Stathis
            };
195 86432004 Christos KK Loverdos
            getFolder.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
196 105b4e49 Christos Stathis
            Scheduler.get().scheduleDeferred(getFolder);
197 105b4e49 Christos Stathis
        }
198 86432004 Christos KK Loverdos
        else if(callback != null) {
199 105b4e49 Christos Stathis
            callback.execute();
200 86432004 Christos KK Loverdos
        }
201 105b4e49 Christos Stathis
    }
202 105b4e49 Christos Stathis
203 105b4e49 Christos Stathis
    public Folder getSelection() {
204 105b4e49 Christos Stathis
        return selectionModel.getSelectedObject();
205 105b4e49 Christos Stathis
    }
206 105b4e49 Christos Stathis
207 3586ad5e Christos Stathis
    public void updateFolder(Folder folder, boolean showfiles, Command callback) {
208 3586ad5e Christos Stathis
        fetchFolder(folder, showfiles, callback);
209 105b4e49 Christos Stathis
    }
210 105b4e49 Christos Stathis
211 3586ad5e Christos Stathis
    public void fetchFolder(final Folder f, final boolean showfiles, final Command callback) {
212 86432004 Christos KK Loverdos
        app.LOG("MysharedTreeViewModel::fetchFolder(), folder=", f, ", showfiles=", showfiles, ", callback=", callback);
213 8282005f Christos Stathis
        String path = "/" + f.getContainer() + "?format=json&shared=&public=" + URL.encodeQueryString(f.getPrefix());
214 28e270f9 Christos KK Loverdos
        GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, Pithos.getStorageAPIURL(), f.getOwnerID(), path, f) {
215 105b4e49 Christos Stathis
            @Override
216 7811b9d1 Christos Stathis
            public void onSuccess(final Folder _result) {
217 3586ad5e Christos Stathis
                    for (File file : _result.getFiles()) {
218 3586ad5e Christos Stathis
                            String name = file.getName();
219 3586ad5e Christos Stathis
                                        if (name.lastIndexOf("/") != -1) {
220 3586ad5e Christos Stathis
                                                file.setName(name.substring(name.lastIndexOf("/") + 1, name.length()));
221 3586ad5e Christos Stathis
                                        }
222 3586ad5e Christos Stathis
                    }
223 3586ad5e Christos Stathis
224 105b4e49 Christos Stathis
                if (showfiles)
225 7811b9d1 Christos Stathis
                    app.showFiles(_result);
226 3586ad5e Christos Stathis
                if (callback != null)
227 3586ad5e Christos Stathis
                        callback.execute();
228 105b4e49 Christos Stathis
            }
229 105b4e49 Christos Stathis
230 105b4e49 Christos Stathis
            @Override
231 105b4e49 Christos Stathis
            public void onError(Throwable t) {
232 105b4e49 Christos Stathis
                GWT.log("Error getting folder", t);
233 3f8622d4 Christos Stathis
                                app.setError(t);
234 105b4e49 Christos Stathis
                if (t instanceof RestException)
235 105b4e49 Christos Stathis
                    app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
236 105b4e49 Christos Stathis
                else
237 105b4e49 Christos Stathis
                    app.displayError("System error fetching folder: " + t.getMessage());
238 105b4e49 Christos Stathis
            }
239 9539e23d Christos Stathis
240 9539e23d Christos Stathis
                        @Override
241 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
242 9539e23d Christos Stathis
                                app.sessionExpired();
243 9539e23d Christos Stathis
                        }
244 105b4e49 Christos Stathis
        };
245 cc0120ab Christos KK Loverdos
        getFolder.setHeader("X-Auth-Token", app.getUserToken());
246 105b4e49 Christos Stathis
        Scheduler.get().scheduleDeferred(getFolder);
247 105b4e49 Christos Stathis
    }
248 7c54d867 Christos Stathis
249 7c54d867 Christos Stathis
        public void initialize(Command callback) {
250 7c54d867 Christos Stathis
                fetchSharedContainers(callback);
251 7c54d867 Christos Stathis
        }
252 105b4e49 Christos Stathis
}