cleanup, correctly update filesize
[pithos] / src / gr / ebs / gss / client / CellTreeViewModel.java
1 /*
2  * Copyright 2011 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 package gr.ebs.gss.client;
20
21 import gr.ebs.gss.client.CellTreeView.Images;
22 import gr.ebs.gss.client.CellTreeView.RefreshHandler;
23 import gr.ebs.gss.client.rest.GetCommand;
24 import gr.ebs.gss.client.rest.MultipleGetCommand;
25 import gr.ebs.gss.client.rest.RestException;
26 import gr.ebs.gss.client.rest.resource.FolderResource;
27 import gr.ebs.gss.client.rest.resource.MyFolderResource;
28 import gr.ebs.gss.client.rest.resource.OtherUserResource;
29 import gr.ebs.gss.client.rest.resource.OthersFolderResource;
30 import gr.ebs.gss.client.rest.resource.OthersResource;
31 import gr.ebs.gss.client.rest.resource.RestResource;
32 import gr.ebs.gss.client.rest.resource.RestResourceWrapper;
33 import gr.ebs.gss.client.rest.resource.SharedFolderResource;
34 import gr.ebs.gss.client.rest.resource.SharedResource;
35 import gr.ebs.gss.client.rest.resource.TrashFolderResource;
36 import gr.ebs.gss.client.rest.resource.TrashResource;
37 import gwtquery.plugins.droppable.client.DroppableOptions;
38 import gwtquery.plugins.droppable.client.DroppableOptions.DroppableFunction;
39 import gwtquery.plugins.droppable.client.events.DragAndDropContext;
40 import gwtquery.plugins.droppable.client.gwt.DragAndDropNodeInfo;
41
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.Map;
46
47 import com.google.gwt.cell.client.AbstractCell;
48 import com.google.gwt.cell.client.Cell;
49 import com.google.gwt.cell.client.ValueUpdater;
50 import com.google.gwt.core.client.GWT;
51 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
52 import com.google.gwt.user.client.DeferredCommand;
53 import com.google.gwt.user.client.ui.AbstractImagePrototype;
54 import com.google.gwt.view.client.AsyncDataProvider;
55 import com.google.gwt.view.client.HasData;
56 import com.google.gwt.view.client.ListDataProvider;
57 import com.google.gwt.view.client.ProvidesKey;
58 import com.google.gwt.view.client.SingleSelectionModel;
59 import com.google.gwt.view.client.TreeViewModel;
60
61
62
63 /**
64  * @author kman
65  *
66  */
67 public class CellTreeViewModel implements TreeViewModel{
68         private final ListDataProvider<RestResource> rootNodes = new ListDataProvider<RestResource>();
69         final Images images;
70         SingleSelectionModel<RestResource> selectionModel;
71         Map<String, MyFolderDataProvider> mymap = new HashMap<String, MyFolderDataProvider>();
72         /**
73          * 
74          */
75         public CellTreeViewModel(final Images _images,SingleSelectionModel<RestResource> selectionModel ) {
76                 super();
77                 images=_images;
78                 this.selectionModel=selectionModel;
79         }
80         
81         private final Cell<RestResource> departmentCell = new AbstractCell<RestResource>("contextmenu"){
82                 
83                 @Override
84                 public void render(com.google.gwt.cell.client.Cell.Context arg0, RestResource arg1, SafeHtmlBuilder arg2) {
85                         String html=null;
86                         String name=null;
87                         if(arg1 instanceof TrashFolderResource){
88                                 html = AbstractImagePrototype.create(images.folderYellow()).getHTML();
89                                 FolderResource res = ((RestResourceWrapper)arg1).getResource();
90                                 name=res.getName();
91                         }
92                         else if(arg1 instanceof RestResourceWrapper){
93                                 FolderResource res = ((RestResourceWrapper)arg1).getResource();
94                                 if(res.isShared())
95                                         html = AbstractImagePrototype.create(images.sharedFolder()).getHTML();
96                                 else if(res.getParentName()==null){
97                                         html = AbstractImagePrototype.create(images.home()).getHTML();
98                                 }
99                                 else
100                                         html = AbstractImagePrototype.create(images.folderYellow()).getHTML();
101                                 name = res.getName();
102                                 
103                         }
104                         else if(arg1 instanceof TrashResource){
105                                 html = AbstractImagePrototype.create(images.trash()).getHTML();
106                                 name="Trash";
107                         }
108                         
109                         else if(arg1 instanceof SharedResource){
110                                 html = AbstractImagePrototype.create(images.myShared()).getHTML();
111                                 name="My Shared";
112                         }
113                         else if(arg1 instanceof OthersResource){
114                                 html = AbstractImagePrototype.create(images.othersShared()).getHTML();
115                                 name = "Other's Shared";
116                         }
117                         else if(arg1 instanceof OtherUserResource){
118                                 html = AbstractImagePrototype.create(images.permUser()).getHTML();
119                                 name = ((OtherUserResource)arg1).getName();
120                         }
121                         arg2.appendHtmlConstant(html);
122                         arg2.appendHtmlConstant("<span class='papala'>");
123                         arg2.appendEscaped(name);
124                         arg2.appendHtmlConstant("</span>");
125                 }
126                 
127                 public void onBrowserEvent(Cell.Context context, com.google.gwt.dom.client.Element parent, RestResource value, com.google.gwt.dom.client.NativeEvent event, com.google.gwt.cell.client.ValueUpdater<RestResource> valueUpdater) {
128                         if(event.getType().equals("contextmenu")){
129                                 selectionModel.setSelected(value, true);
130                                 GSS.get().getTreeView().showPopup(event.getClientX(), event.getClientY());
131                         }
132                 };
133                 
134         };
135         
136         
137         @Override
138         public <T> NodeInfo<?> getNodeInfo(T value) {
139                 
140                 if(value==null){
141                         return new DragAndDropNodeInfo<RestResource>(getRootNodes(), departmentCell,
142                                     selectionModel, null);
143                 }
144                 else if (value instanceof MyFolderResource) {
145                 // Second level.
146                         MyFolderDataProvider dataProvider = new MyFolderDataProvider(
147                     ((MyFolderResource) value),MyFolderResource.class);
148                 DragAndDropNodeInfo<RestResource> n =  new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
149                     selectionModel, new ResourceValueUpdater());
150                 //nodeInfos.put(((MyFolderResource) value).getUri(), n);
151                 mymap.put(((MyFolderResource) value).getUri(), dataProvider);
152                 DroppableOptions options = n.getDroppableOptions();
153                 options.setDroppableHoverClass("droppableHover");
154                 // use a DroppableFunction here. We can also add a DropHandler in the tree
155                 // itself
156                 options.setOnDrop(new DroppableFunction() {
157
158                   public void f(DragAndDropContext context) {
159                           GWT.log("DROPPED");
160                     
161                   }
162                 });
163                 // permission cell are not draggable
164                 n.setCellDroppableOnly();
165                 return n;
166                 }
167                 else if (value instanceof SharedResource) {
168                 // Second level.
169                         MyFolderDataProvider dataProvider = new MyFolderDataProvider(
170                     ((SharedResource) value), SharedFolderResource.class);
171                 return new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
172                     selectionModel, new ResourceValueUpdater());
173                 }
174                 else if (value instanceof TrashResource) {
175                 // Second level.
176                         ListDataProvider<RestResource> trashProvider = new ListDataProvider<RestResource>();
177                         List<RestResource> r = new ArrayList<RestResource>();
178                         for(FolderResource f : GSS.get().getTreeView().getTrash().getFolders()){
179                                 r.add(new TrashFolderResource(f));
180                         }
181                         trashProvider.setList(r);
182                 return new DragAndDropNodeInfo<RestResource>(trashProvider, departmentCell,
183                     selectionModel, new ResourceValueUpdater());
184                 }
185                 else if (value instanceof OthersResource) {
186                 // Second level.
187                         OthersDataProvider dataProvider = new OthersDataProvider(
188                     ((OthersResource) value), SharedFolderResource.class);
189                 return new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
190                     selectionModel, null);
191                 }
192                 else if (value instanceof SharedFolderResource) {
193                 // Second level.
194                         MyFolderDataProvider dataProvider = new MyFolderDataProvider(
195                     ((SharedFolderResource) value),SharedFolderResource.class);
196                 DragAndDropNodeInfo<RestResource> n =  new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
197                     selectionModel, new ResourceValueUpdater());
198                 //nodeInfos.put(((SharedFolderResource) value).getUri(), n);
199                 DroppableOptions options = n.getDroppableOptions();
200                 options.setDroppableHoverClass("droppableHover");
201                 // use a DroppableFunction here. We can also add a DropHandler in the tree
202                 // itself
203                 options.setOnDrop(new DroppableFunction() {
204
205                   public void f(DragAndDropContext context) {
206                           GWT.log("DROPPED");
207                     
208                   }
209                 });
210                 // permission cell are not draggable
211                 n.setCellDroppableOnly();
212                 return n;
213                 }
214                 else if (value instanceof OthersFolderResource) {
215                 // Second level.
216                         MyFolderDataProvider dataProvider = new MyFolderDataProvider(
217                     ((OthersFolderResource) value),OthersFolderResource.class);
218                 DragAndDropNodeInfo<RestResource> n =  new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
219                     selectionModel, new ResourceValueUpdater());
220                 //nodeInfos.put(((OthersFolderResource) value).getUri(), n);
221                 DroppableOptions options = n.getDroppableOptions();
222                 options.setDroppableHoverClass("droppableHover");
223                 // use a DroppableFunction here. We can also add a DropHandler in the tree
224                 // itself
225                 options.setOnDrop(new DroppableFunction() {
226
227                   public void f(DragAndDropContext context) {
228                           GWT.log("DROPPED");
229                     
230                   }
231                 });
232                 // permission cell are not draggable
233                 n.setCellDroppableOnly();
234                 return n;
235                 }
236                 else if (value instanceof OtherUserResource) {
237                 // Second level.
238                         MyFolderDataProvider dataProvider = new MyFolderDataProvider(
239                     ((OtherUserResource) value),OthersFolderResource.class);
240                 DragAndDropNodeInfo<RestResource> n =  new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
241                     selectionModel, new ResourceValueUpdater());
242                 //nodeInfos.put(((OtherUserResource) value).getUri(), n);
243                 DroppableOptions options = n.getDroppableOptions();
244                 options.setDroppableHoverClass("droppableHover");
245                 // use a DroppableFunction here. We can also add a DropHandler in the tree
246                 // itself
247                 options.setOnDrop(new DroppableFunction() {
248
249                   public void f(DragAndDropContext context) {
250                           GWT.log("DROPPED");
251                     
252                   }
253                 });
254                 // permission cell are not draggable
255                 n.setCellDroppableOnly();
256                 return n;
257                 }
258                 // TODO Auto-generated method stub
259                 return null;
260         }
261
262         @Override
263         public boolean isLeaf(Object value) {
264                 if(value instanceof RestResourceWrapper)
265                         return ((RestResourceWrapper)value).getResource().getFolders().size()==0;
266                 else if(value instanceof TrashResource)
267                         return ((TrashResource)value).getFolders().size()==0;
268                 else if(value instanceof SharedResource)
269                         return ((SharedResource)value).getFolders().size()==0;
270                 else if(value instanceof OthersResource)
271                         return ((OthersResource)value).getOtherUsers().size()==0;
272                 else if(value instanceof OtherUserResource)
273                         return ((OtherUserResource)value).getFolders().size()==0;
274                 return false;
275         }
276         
277         class ResourceValueUpdater implements  ValueUpdater<RestResource>{
278
279                 @Override
280                 public void update(final RestResource value) {
281                         if(value instanceof MyFolderResource){
282                                 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, value.getUri(), null) {
283
284                                         @Override
285                                         public void onComplete() {
286                                                 FolderResource rootResource = getResult();
287                                                 ((MyFolderResource)value).getResource().setFiles(rootResource.getFiles());
288                                                 if(GSS.get().getTreeView().getSelection().getUri().equals(value.getUri()))
289                                                         selectionModel.setSelected(value, true);
290                                                 GWT.log("UPDATYING");
291                                                 GSS.get().onResourceUpdate(value);
292                                                 
293                                         }
294         
295                                         @Override
296                                         public void onError(Throwable t) {
297                                                 GWT.log("Error fetching root folder", t);
298                                                 GSS.get().displayError("Unable to fetch root folder");
299                                         }
300         
301                                 };
302                                 DeferredCommand.addCommand(gf);
303                         }
304                         if(value instanceof TrashResource){
305                                 DeferredCommand.addCommand(new GetCommand<TrashResource>(TrashResource.class, GSS.get().getCurrentUserResource().getTrashPath(), null) {
306                                         @Override
307                                         public void onComplete() {
308                                                 //trash = getResult();
309                                                 for(RestResource r : getRootNodes().getList()){
310                                                         if(r instanceof TrashResource)
311                                                                 getRootNodes().getList().set(getRootNodes().getList().indexOf(r),GSS.get().getTreeView().getTrash());
312                                                 }
313                                                 GSS.get().getTreeView().updateNodeChildren(GSS.get().getTreeView().getTrash());
314                                         }
315
316                                         @Override
317                                         public void onError(Throwable t) {
318                                                 if(t instanceof RestException){
319                                                         int statusCode = ((RestException)t).getHttpStatusCode();
320                                                         // On IE status code 1223 may be returned instead of 204.
321                                                         if(statusCode == 204 || statusCode == 1223){
322                                                                 //trash = new TrashResource(GSS.get().getCurrentUserResource().getTrashPath());
323                                                 }
324                                                 else{
325                                                         GWT.log("", t);
326                                                         GSS.get().displayError("Unable to fetch trash folder:"+t.getMessage());
327                                                         //GSS.get().getTreeView().getTrash() = new TrashResource(GSS.get().getCurrentUserResource().getTrashPath());
328                                                 }
329                                         }
330                                 }
331                                 });
332                         }
333                         
334                 }
335                 
336         }
337         class MyFolderDataProvider extends AsyncDataProvider<RestResource>{
338                 private RestResource restResource;
339                 private Class resourceClass;
340                   public MyFolderDataProvider(RestResource department, Class resourceClass) {
341                     super(new ProvidesKey<RestResource>() {
342
343                                 @Override
344                                 public Object getKey(RestResource item) {
345                                         return item.getUri();
346                                 }});
347                     this.restResource = department;
348                     this.resourceClass=resourceClass;
349                     //CellTreeView.this.mymap.put(department.getUri(), MyFolderDataProvider.this);
350                   }
351
352                   @Override
353                   protected void onRangeChanged(final HasData<RestResource> view) {
354                         refresh(null);
355                   }
356                   
357                 /**
358                  * Retrieve the restResource.
359                  *
360                  * @return the restResource
361                  */
362                 public RestResource getRestResource() {
363                         return restResource;
364                 }
365                 
366                 
367                 /**
368                  * Modify the restResource.
369                  *
370                  * @param restResource the restResource to set
371                  */
372                 public void setRestResource(RestResource restResource) {
373                         this.restResource = restResource;
374                 }
375                 
376                   public void refresh(final RefreshHandler refresh){
377                           GWT.log("******************************************");
378                           GWT.log("[REFRESHING]:"+restResource.getUri());
379                           GWT.log("******************************************");
380                           GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, restResource.getUri(), null) {
381
382                                         @Override
383                                         public void onComplete() {
384                                                 if(restResource instanceof RestResourceWrapper)
385                                                         ((RestResourceWrapper)restResource).setResource(getResult());//restResource = getResult();
386                                                 
387                                                 //if(CellTreeView.this.mymap.get(restResource.getUri())!=null)
388                                                         //CellTreeView.this.mymap.get(restResource.getUri()).setRestResource(restResource);
389                                                 String[] folderPaths = null;
390                                                 if(resourceClass.equals(MyFolderResource.class))
391                                                         folderPaths=((MyFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
392                                                 else if(resourceClass.equals(SharedFolderResource.class) && restResource instanceof SharedResource)
393                                                         folderPaths=((SharedResource) restResource).getSubfolderPaths().toArray(new String[] {});
394                                                 else if(resourceClass.equals(SharedFolderResource.class)){
395                                                         folderPaths=((SharedFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
396                                                         GWT.log("------------>"+folderPaths);
397                                                 }
398                                                 else if(resourceClass.equals(TrashFolderResource.class))
399                                                         folderPaths=((TrashFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
400                                                 else if(resourceClass.equals(OthersFolderResource.class) && restResource instanceof OtherUserResource)
401                                                         folderPaths=((OtherUserResource) restResource).getSubfolderPaths().toArray(new String[] {});
402                                                 else if(resourceClass.equals(OthersFolderResource.class))
403                                                         folderPaths=((OthersFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
404                                                 MultipleGetCommand<FolderResource> gf2 = new MultipleGetCommand<FolderResource>(FolderResource.class,
405                                                                         folderPaths, null) {
406
407                                                         @Override
408                                                         public void onComplete() {
409                                                                 List<RestResource> res = new ArrayList<RestResource>();
410                                                                 for(FolderResource r : getResult()){
411                                                                         if(r.isDeleted()){
412                                                                                 
413                                                                         }
414                                                                         else if(resourceClass.equals(MyFolderResource.class))
415                                                                                 res.add(new MyFolderResource(r));
416                                                                         else if(resourceClass.equals(SharedFolderResource.class)){
417                                                                                 GWT.log("ADDING:"+r.getUri());
418                                                                                 res.add(new SharedFolderResource(r));
419                                                                         }
420                                                                         else if(resourceClass.equals(TrashFolderResource.class))
421                                                                                 res.add(new TrashFolderResource(r));
422                                                                         else if(resourceClass.equals(OthersFolderResource.class))
423                                                                                 res.add(new OthersFolderResource(r));
424                                                                 }
425                                                                 updateRowCount(res.size(), true);
426                                                                 updateRowData(0,res);
427                                                                 if(refresh!=null)
428                                                                         refresh.onRefresh();
429                                                         }
430
431                                                         @Override
432                                                         public void onError(Throwable t) {
433                                                                 GSS.get().displayError("Unable to fetch subfolders");
434                                                                 GWT.log("Unable to fetch subfolders", t);
435                                                         }
436
437                                                         @Override
438                                                         public void onError(String p, Throwable throwable) {
439                                                                 GWT.log("Path:"+p, throwable);
440                                                         }
441
442                                                 };
443                                                 DeferredCommand.addCommand(gf2);
444                                                 
445                                         }
446
447                                         @Override
448                                         public void onError(Throwable t) {
449                                                 GWT.log("Error fetching root folder", t);
450                                                 GSS.get().displayError("Unable to fetch root folder");
451                                         }
452
453                                 };
454                                 DeferredCommand.addCommand(gf);
455                   }               
456         }
457         
458         
459         class OthersDataProvider extends AsyncDataProvider<RestResource>{
460                 private RestResource restResource;
461                 private Class resourceClass;
462                   public OthersDataProvider(RestResource department, Class resourceClass) {
463                     super(new ProvidesKey<RestResource>() {
464
465                                 @Override
466                                 public Object getKey(RestResource item) {
467                                         return item.getUri();
468                                 }});
469                     this.restResource = department;
470                     this.resourceClass=resourceClass;
471                     //CellTreeView.this.mymap.put(department.getUri(), OthersDataProvider.this);
472                   }
473
474                   @Override
475                   protected void onRangeChanged(final HasData<RestResource> view) {
476                         refresh(null);
477                   }
478                   
479                 /**
480                  * Retrieve the restResource.
481                  *
482                  * @return the restResource
483                  */
484                 public RestResource getRestResource() {
485                         return restResource;
486                 }
487                 
488                 
489                 /**
490                  * Modify the restResource.
491                  *
492                  * @param restResource the restResource to set
493                  */
494                 public void setRestResource(RestResource restResource) {
495                         this.restResource = restResource;
496                 }
497                 
498                   public void refresh(final RefreshHandler refresh){
499                           GWT.log("******************************************");
500                           GWT.log("[REFRESHING]:"+restResource.getUri());
501                           GWT.log("******************************************");
502                           GetCommand<OthersResource> go = new GetCommand<OthersResource>(OthersResource.class,
503                           restResource.getUri(), null) {
504
505                                   @Override
506                                   public void onComplete() {
507                                           final OthersResource others = getResult();
508                           MultipleGetCommand<OtherUserResource> gogo = new MultipleGetCommand<OtherUserResource>(OtherUserResource.class,
509                                                   others.getOthers().toArray(new String[] {}), null) {
510
511                                   @Override
512                                   public void onComplete() {
513                                           List<OtherUserResource> res = getResult();
514                                           updateRowCount(res.size(), true);
515                                           List<RestResource> r = new ArrayList<RestResource>();
516                                           r.addAll(res);
517                                                                           updateRowData(0,r);
518                                   }
519
520                                   @Override
521                                   public void onError(Throwable t) {
522                                           GWT.log("Error fetching Others Root folder", t);
523                                           GSS.get().displayError("Unable to fetch Others Root folder");
524                                   }
525
526                                   @Override
527                                   public void onError(String p, Throwable throwable) {
528                                           GWT.log("Path:"+p, throwable);
529                                   }
530                           };
531                           DeferredCommand.addCommand(gogo);
532                                   }
533                         
534                                   @Override
535                                   public void onError(Throwable t) {
536                                           GWT.log("Error fetching Others Root folder", t);
537                                           GSS.get().displayError("Unable to fetch Others Root folder");
538                                   }
539                           };
540                           DeferredCommand.addCommand(go);
541                   }               
542         }
543
544
545         
546         /**
547          * Retrieve the rootNodes.
548          *
549          * @return the rootNodes
550          */
551         public ListDataProvider<RestResource> getRootNodes() {
552                 return rootNodes;
553         }
554
555         
556         /**
557          * Retrieve the mymap.
558          *
559          * @return the mymap
560          */
561         public Map<String, MyFolderDataProvider> getMymap() {
562                 return mymap;
563         }
564         
565         
566         
567         
568 }