Statistics
| Branch: | Tag: | Revision:

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

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
                        public boolean execute() {
57
                                return updateInit();
58
                        }
59
                });
60
        }
61

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

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

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

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

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

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

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

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

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

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

    
144
                        };
145

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

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

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

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

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

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

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

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

    
205
        }
206

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

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

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

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

    
240

    
241
}