Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / tree / OthersSharesSubtree.java @ 9ab5db6d

History | View | Annotate | Download (7.5 kB)

1
/*
2
 * Copyright 2008, 2009 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
6
 * GSS is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * GSS is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19

    
20
package gr.ebs.gss.client.tree;
21

    
22
import gr.ebs.gss.client.GSS;
23
import gr.ebs.gss.client.PopupTree;
24
import gr.ebs.gss.client.Folders.Images;
25
import gr.ebs.gss.client.dnd.DnDTreeItem;
26
import gr.ebs.gss.client.rest.GetCommand;
27
import gr.ebs.gss.client.rest.MultipleGetCommand;
28
import gr.ebs.gss.client.rest.resource.FolderResource;
29
import gr.ebs.gss.client.rest.resource.OtherUserResource;
30
import gr.ebs.gss.client.rest.resource.OthersResource;
31
import gr.ebs.gss.client.rest.resource.UserResource;
32

    
33
import java.util.List;
34

    
35
import com.google.gwt.core.client.GWT;
36
import com.google.gwt.user.client.DeferredCommand;
37
import com.google.gwt.user.client.IncrementalCommand;
38
import com.google.gwt.user.client.ui.TreeItem;
39

    
40
/**
41
 * @author kman
42
 */
43
public class OthersSharesSubtree extends Subtree {
44

    
45
        /**
46
         * A constant that denotes the completion of an IncrementalCommand.
47
         */
48
        public static final boolean DONE = false;
49

    
50
        private DnDTreeItem rootItem;
51

    
52
        public OthersSharesSubtree(PopupTree aTree, final Images _images) {
53
                super(aTree, _images);
54
                DeferredCommand.addCommand(new IncrementalCommand() {
55

    
56
                        @Override
57
                        public boolean execute() {
58
                                return updateInit();
59
                        }
60
                });
61
        }
62

    
63
        public boolean updateInit() {
64
                UserResource userResource = GSS.get().getCurrentUserResource();
65
                if (userResource == null ||
66
                                        GSS.get().getFolders().getRootItem() == null ||
67
                                        GSS.get().getFolders().getTrashItem() == null ||
68
                                        GSS.get().getFolders().getMySharesItem() == null)
69
                        return !DONE;
70

    
71
                GetCommand<OthersResource> go = new GetCommand<OthersResource>(OthersResource.class,
72
                                        userResource.getOthersPath(), null) {
73

    
74
                        @Override
75
                        public void onComplete() {
76
                                rootItem = new DnDTreeItem(imageItemHTML(images.othersShared(), "Other's Shared"), false,tree,true);
77
                                rootItem.setUserObject(getResult());
78
                                tree.addItem(rootItem);
79
                                //rootItem.removeItems();
80
                                //update(rootItem);
81
                                GSS.get().removeGlassPanel();
82
                        }
83

    
84
                        @Override
85
                        public void onError(Throwable t) {
86
                                GWT.log("Error fetching Others Root folder", t);
87
                                GSS.get().displayError("Unable to fetch Others Root folder");
88
                                if(rootItem != null){
89
                                        rootItem = new DnDTreeItem(imageItemHTML(images.othersShared(), "ERROR"), false,tree);
90
                                        tree.addItem(rootItem);
91
                                }
92
                        }
93
                };
94
                DeferredCommand.addCommand(go);
95
                return DONE;
96
        }
97

    
98
        public void update(final DnDTreeItem folderItem) {
99
                if (folderItem.getOthersResource() != null) {
100
                        UserResource userResource = GSS.get().getCurrentUserResource();
101
                        GetCommand<OthersResource> go = new GetCommand<OthersResource>(OthersResource.class,
102
                                                userResource.getOthersPath(), null) {
103

    
104
                                @Override
105
                                public void onComplete() {
106
                                        final OthersResource others = getResult();
107
                                        rootItem.setUserObject(others);
108
                                        MultipleGetCommand<OtherUserResource> gogo = new MultipleGetCommand<OtherUserResource>(OtherUserResource.class,
109
                                                                folderItem.getOthersResource().getOthers().toArray(new String[] {}), null) {
110

    
111
                                                @Override
112
                                                public void onComplete() {
113
                                                        List<OtherUserResource> res = getResult();
114
                                                        folderItem.removeItems();
115
                                                        for (OtherUserResource r : res) {
116
                                                                DnDTreeItem child = (DnDTreeItem) addImageItem(folderItem,
117
                                                                                        r.getName(), images.folderYellow(), true);
118
                                                                r.setUsername(others.getUsernameOfUri(r.getUri()));
119
                                                                GWT.log("Setting username:"+r.getUsername(), null );
120
                                                                child.setUserObject(r);
121
                                                                child.setState(false);
122
                                                        }
123
                                                }
124

    
125
                                                @Override
126
                                                public void onError(Throwable t) {
127
                                                        GWT.log("Error fetching Others Root folder", t);
128
                                                        GSS.get().displayError("Unable to fetch Others Root folder");
129
                                                }
130

    
131
                                                @Override
132
                                                public void onError(String p, Throwable throwable) {
133
                                                        GWT.log("Path:"+p, throwable);
134
                                                }
135
                                        };
136
                                        DeferredCommand.addCommand(gogo);
137
                                }
138

    
139
                                @Override
140
                                public void onError(Throwable t) {
141
                                        GWT.log("Error fetching Others Root folder", t);
142
                                        GSS.get().displayError("Unable to fetch Others Root folder");
143
                                }
144

    
145
                        };
146

    
147
                        DeferredCommand.addCommand(go);
148
                } else if (folderItem.getOtherUserResource() != null) {
149

    
150
                        GetCommand<OtherUserResource> go = new GetCommand<OtherUserResource>(OtherUserResource.class,
151
                                                folderItem.getOtherUserResource().getUri(), null) {
152

    
153
                                @Override
154
                                public void onComplete() {
155
                                        OtherUserResource res = getResult();
156
                                        folderItem.removeItems();
157
                                        for (FolderResource r : res.getFolders()) {
158
                                                DnDTreeItem child = (DnDTreeItem) addImageItem(folderItem,
159
                                                                        r.getName(), images.folderYellow(), true);
160
                                                child.setUserObject(r);
161
                                                child.doDraggable();
162
                                                updateFolderAndSubfolders(child);
163
                                        }
164
                                }
165

    
166
                                @Override
167
                                public void onError(Throwable t) {
168
                                        GWT.log("Error fetching Others Root folder", t);
169
                                        GSS.get().displayError("Unable to fetch Others Root folder");
170
                                }
171

    
172
                        };
173
                        DeferredCommand.addCommand(go);
174
                } else if (folderItem.getFolderResource() != null) {
175
                        GWT.log("UPDATING :"+folderItem.getFolderResource().getName(), null);
176
                        MultipleGetCommand<FolderResource> go = new MultipleGetCommand<FolderResource>(FolderResource.class,
177
                                                folderItem.getFolderResource().getSubfolderPaths().toArray(new String[] {}), folderItem.getFolderResource().getCache()) {
178

    
179
                                @Override
180
                                public void onComplete() {
181
                                        List<FolderResource> res = getResult();
182
                                        folderItem.removeItems();
183
                                        GWT.log("UPDATING :"+folderItem.getFolderResource().getName()+" :"+res.size(), null);
184
                                        for (FolderResource r : res) {
185
                                                DnDTreeItem child = (DnDTreeItem) addImageItem(folderItem,
186
                                                                        r.getName(), images.folderYellow(), true);
187
                                                child.setUserObject(r);
188
                                                child.doDraggable();
189
                                        }
190
                                }
191

    
192
                                @Override
193
                                public void onError(Throwable t) {
194
                                        GWT.log("Error fetching Others Root folder", t);
195
                                        GSS.get().displayError("Unable to fetch Others Root folder");
196
                                }
197

    
198
                                @Override
199
                                public void onError(String p, Throwable throwable) {
200
                                        GWT.log("Path:"+p, throwable);
201
                                }
202
                        };
203
                        DeferredCommand.addCommand(go);
204
                }
205

    
206
        }
207

    
208
        public void updateFolderAndSubfolders(final DnDTreeItem folderItem) {
209
                if (folderItem.getFolderResource() != null) {
210
                        final String path = folderItem.getFolderResource().getUri();
211
                        GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, path, folderItem.getFolderResource()) {
212

    
213
                                @Override
214
                                public void onComplete() {
215
                                        FolderResource rootResource = getResult();
216
                                        folderItem.undoDraggable();
217
                                        folderItem.updateWidget(imageItemHTML(images.folderYellow(), rootResource.getName()));
218
                                        folderItem.setUserObject(rootResource);
219
                                        folderItem.doDraggable();
220
                                }
221

    
222
                                @Override
223
                                public void onError(Throwable t) {
224
                                        GWT.log("Error fetching folder", t);
225
                                        GSS.get().displayError("Unable to fetch folder:" + folderItem.getFolderResource().getName());
226
                                }
227
                        };
228
                        DeferredCommand.addCommand(gf);
229
                }
230
        }
231

    
232
        /**
233
         * Retrieve the rootItem.
234
         *
235
         * @return the rootItem
236
         */
237
        public TreeItem getRootItem() {
238
                return rootItem;
239
        }
240

    
241

    
242
}