Display the full name in the ACL. The full name is displayed once right click and...
[pithos] / src / gr / ebs / gss / client / VersionsList.java
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 package gr.ebs.gss.client;
20
21 import gr.ebs.gss.client.FilePropertiesDialog.Images;
22 import gr.ebs.gss.client.commands.GetUserCommand;
23 import gr.ebs.gss.client.rest.DeleteCommand;
24 import gr.ebs.gss.client.rest.PostCommand;
25 import gr.ebs.gss.client.rest.RestCommand;
26 import gr.ebs.gss.client.rest.RestException;
27 import gr.ebs.gss.client.rest.resource.FileResource;
28
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.Date;
32 import java.util.List;
33
34 import com.google.gwt.core.client.GWT;
35 import com.google.gwt.event.dom.client.ClickEvent;
36 import com.google.gwt.event.dom.client.ClickHandler;
37 import com.google.gwt.http.client.URL;
38 import com.google.gwt.i18n.client.DateTimeFormat;
39 import com.google.gwt.user.client.DeferredCommand;
40 import com.google.gwt.user.client.Window;
41 import com.google.gwt.user.client.ui.AbstractImagePrototype;
42 import com.google.gwt.user.client.ui.Composite;
43 import com.google.gwt.user.client.ui.FlexTable;
44 import com.google.gwt.user.client.ui.HTML;
45 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
46 import com.google.gwt.user.client.ui.VerticalPanel;
47
48 /**
49  * @author kman
50  */
51 public class VersionsList extends Composite {
52
53         int selectedRow = -1;
54
55         int permissionCount = -1;
56
57         List<FileResource> versions = null;
58
59         final Images images;
60
61         final VerticalPanel permPanel = new VerticalPanel();
62
63         final FlexTable permTable = new FlexTable();
64
65         FileResource toRemove = null;
66
67         FilePropertiesDialog container;
68
69         public VersionsList(FilePropertiesDialog aContainer, final Images theImages, List<FileResource> theVersions) {
70                 images = theImages;
71                 container = aContainer;
72                 versions = theVersions;
73                 Collections.sort(theVersions, new Comparator<FileResource>(){
74
75                         @Override
76                         public int compare(FileResource o1, FileResource o2) {
77                                 return o1.getVersion().compareTo(o2.getVersion());
78                         }
79
80                 });
81                 permTable.setText(0, 0, "Version");
82                 permTable.setText(0, 1, "Created");
83                 permTable.setText(0, 2, "Modified");
84                 permTable.setText(0, 3, "Size");
85                 permTable.setText(0, 4, "");
86                 permTable.setText(0, 5, "");
87                 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
88                 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
89                 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
90                 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
91                 permTable.getFlexCellFormatter().setColSpan(0, 1, 2);
92                 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
93                 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
94                 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
95                 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_CENTER);
96                 permPanel.add(permTable);
97                 permPanel.addStyleName("gss-TabPanelBottom");
98                 permTable.addStyleName("gss-permList");
99                 initWidget(permPanel);
100                 updateTable();
101         }
102
103         public void updateTable() {
104                 int i = 1;
105                 if (toRemove != null) {
106                         versions.remove(toRemove);
107                         toRemove = null;
108                 }
109                 for (final FileResource dto : versions) {
110                         HTML restoreVersion = new HTML("<a href='#' class='hidden-link info'><span>"+AbstractImagePrototype.create(images.restore()).getHTML()+"</span><div>Restore this Version</div></a>");
111                         restoreVersion.addClickHandler(new ClickHandler() {
112                                 @Override
113                                 public void onClick(ClickEvent event) {
114                                         restoreVersion(dto);
115                                 }
116                         });
117
118                         permTable.setHTML(i, 0, "<span>" + dto.getVersion() + "</span>");
119
120                         if(GSS.get().findUserFullName(dto.getCreatedBy()) == null){
121                                 GetUserCommand guc = new GetUserCommand(dto.getCreatedBy());
122                                 guc.execute();
123                                 GSS.get().putUserToMap(dto.getCreatedBy(), dto.getCreatedBy());
124                         }
125
126                         if(GSS.get().findUserFullName(dto.getModifiedBy()) == null){
127                                 GetUserCommand guc = new GetUserCommand(dto.getModifiedBy());
128                                 guc.execute();
129                                 GSS.get().putUserToMap(dto.getModifiedBy(), dto.getModifiedBy());
130                         }
131                         permTable.setHTML(i, 1, "<span>" + formatDate(dto.getCreationDate()) + " by " + GSS.get().findUserFullName(dto.getCreatedBy()) + "</span>");
132                         permTable.setHTML(i, 2, "<span>" + formatDate(dto.getModificationDate()) + " by " + GSS.get().findUserFullName(dto.getModifiedBy()) + "</span>");
133                         permTable.setHTML(i, 3, "<span>" + dto.getFileSizeAsString() + "</span>");
134                         HTML downloadHtml = new HTML("<a class='hidden-link info' href='#'><span>"+AbstractImagePrototype.create(images.download()).getHTML()+"</span><div>View this Version</div></a>");
135                         downloadHtml.addClickHandler(new ClickHandler() {
136                                 @Override
137                                 public void onClick(ClickEvent event) {
138                                         GSS app = GSS.get();
139                                         String dateString = RestCommand.getDate();
140                                         String resource = dto.getUri().substring(app.getApiPath().length()-1, dto.getUri().length());
141                                         String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken()));
142                                         String fileUrl = dto.getUri() + "?version=" + dto.getVersion() + "&Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString);
143                                         Window.open(fileUrl, "_BLANK", "");
144                                 }
145                         });
146                         permTable.setWidget(i, 4, downloadHtml);
147                         permTable.setWidget(i, 5, restoreVersion);
148                         permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
149                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 0, HasHorizontalAlignment.ALIGN_CENTER);
150                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
151                         permTable.getFlexCellFormatter().setColSpan(i, 1, 2);
152                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
153                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
154                         i++;
155                 }
156                 for (; i < permTable.getRowCount(); i++)
157                         permTable.removeRow(i);
158         }
159
160
161         void removeVersion(final FileResource version) {
162                 DeleteCommand df = new DeleteCommand(version.getUri()){
163
164                         @Override
165                         public void onComplete() {
166                                 toRemove = version;
167                                 updateTable();
168                                 GSS.get().getFileList().updateFileCache(false, true /*clear selection*/);
169                         }
170
171                         @Override
172                         public void onError(Throwable t) {
173                                 GWT.log("", t);
174                                 if(t instanceof RestException){
175                                         int statusCode = ((RestException)t).getHttpStatusCode();
176                                         if(statusCode == 405)
177                                                 GSS.get().displayError("You don't have the necessary permissions");
178                                         else if(statusCode == 404)
179                                                 GSS.get().displayError("Versions does not exist");
180                                         else
181                                                 GSS.get().displayError("Unable to remove version:"+((RestException)t).getHttpStatusText());
182                                 }
183                                 else
184                                         GSS.get().displayError("System error removing version:"+t.getMessage());
185                         }
186                 };
187                 DeferredCommand.addCommand(df);
188
189         }
190
191         void restoreVersion(final FileResource version) {
192                 FileResource selectedFile = (FileResource) GSS.get().getCurrentSelection();
193                 PostCommand ep = new PostCommand(selectedFile.getUri()+"?restoreVersion="+version.getVersion(),"",200){
194
195
196                         @Override
197                         public void onComplete() {
198                                 container.hide();
199                 GSS.get().getFileList().updateFileCache(true, true /*clear selection*/);
200                         }
201
202                         @Override
203                         public void onError(Throwable t) {
204                                 GWT.log("", t);
205                                 if(t instanceof RestException)
206                                         GSS.get().displayError("Unable to restore version:"+((RestException)t).getHttpStatusText());
207                                 else
208                                         GSS.get().displayError("System error restoring version:"+t.getMessage());
209                         }
210
211                 };
212                 DeferredCommand.addCommand(ep);
213         }
214
215         private String formatDate(Date date){
216                 DateTimeFormat format = DateTimeFormat.getFormat("dd/MM/yyyy : HH:mm");
217                 return format.format(date);
218         }
219
220 }