Statistics
| Branch: | Tag: | Revision:

root / gss / src / gr / ebs / gss / client / VersionsList.java @ da62cc7e

History | View | Annotate | Download (7.4 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
package gr.ebs.gss.client;
20

    
21
import gr.ebs.gss.client.FilePropertiesDialog.Images;
22
import gr.ebs.gss.client.rest.AbstractRestCommand;
23
import gr.ebs.gss.client.rest.ExecuteDelete;
24
import gr.ebs.gss.client.rest.ExecutePost;
25
import gr.ebs.gss.client.rest.RestException;
26
import gr.ebs.gss.client.rest.resource.FileResource;
27

    
28
import java.util.Collections;
29
import java.util.Comparator;
30
import java.util.Date;
31
import java.util.List;
32

    
33
import com.google.gwt.core.client.GWT;
34
import com.google.gwt.http.client.URL;
35
import com.google.gwt.i18n.client.DateTimeFormat;
36
import com.google.gwt.user.client.DeferredCommand;
37
import com.google.gwt.user.client.ui.ClickListener;
38
import com.google.gwt.user.client.ui.Composite;
39
import com.google.gwt.user.client.ui.FlexTable;
40
import com.google.gwt.user.client.ui.HTML;
41
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
42
import com.google.gwt.user.client.ui.VerticalPanel;
43
import com.google.gwt.user.client.ui.Widget;
44

    
45
/**
46
 * @author kman
47
 */
48
public class VersionsList extends Composite {
49

    
50
        int selectedRow = -1;
51

    
52
        int permissionCount = -1;
53

    
54
        List<FileResource> versions = null;
55

    
56
        final Images images;
57

    
58
        final VerticalPanel permPanel = new VerticalPanel();
59

    
60
        final FlexTable permTable = new FlexTable();
61

    
62
        FileResource toRemove = null;
63

    
64
        FilePropertiesDialog container;
65

    
66
        public VersionsList(FilePropertiesDialog aContainer, final Images theImages, List<FileResource> theVersions) {
67
                images = theImages;
68
                container = aContainer;
69
                versions = theVersions;
70
                Collections.sort(theVersions, new Comparator<FileResource>(){
71

    
72
                        public int compare(FileResource o1, FileResource o2) {
73
                                return o1.getVersion().compareTo(o2.getVersion());
74
                        }
75

    
76
                });
77
                permTable.setText(0, 0, "Version");
78
                permTable.setText(0, 1, "Created");
79
                permTable.setText(0, 2, "Modified");
80
                permTable.setText(0, 3, "Size");
81
                permTable.setText(0, 4, "");
82
                permTable.setText(0, 5, "");
83
                permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
84
                permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
85
                permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
86
                permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
87
                permTable.getFlexCellFormatter().setColSpan(0, 1, 2);
88
                permTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
89
                permTable.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
90
                permTable.getFlexCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
91
                permTable.getFlexCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_CENTER);
92
                permPanel.add(permTable);
93
                permPanel.addStyleName("gwt-TabPanelBottom");
94
                permTable.addStyleName("gss-permList");
95
                initWidget(permPanel);
96
                updateTable();
97
        }
98

    
99
        public void updateTable() {
100
                int i = 1;
101
                if (toRemove != null) {
102
                        versions.remove(toRemove);
103
                        toRemove = null;
104
                }
105
                for (final FileResource dto : versions) {
106
                        HTML restoreVersion = new HTML("<a href='#' class='hidden-link info'>"+images.restore().getHTML()+"<span>Restore this Version</span></a>");
107
                        restoreVersion.addClickListener( new ClickListener() {
108

    
109
                                public void onClick(Widget sender) {
110
                                        restoreVersion(dto);
111
                                }
112
                        });
113

    
114
                        permTable.setHTML(i, 0, "<span>" + dto.getVersion() + "</span>");
115
                        permTable.setHTML(i, 1, "<span>" + formatDate(dto.getCreationDate()) + "</span>");
116
                        permTable.setHTML(i, 2, "<span>" + formatDate(dto.getModificationDate()) + "</span>");
117
                        permTable.setHTML(i, 3, "<span>" + dto.getFileSizeAsString() + "</span>");
118
                        String[] link = {"", ""};
119
                        createDownloadLink(link, dto);
120
                        HTML downloadHtml = new HTML(link[0]+images.download().getHTML()+"<span>View This Version</span>"+link[1]);
121
                        permTable.setWidget(i, 4, downloadHtml);
122
                        permTable.setWidget(i, 5, restoreVersion);
123
                        permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
124
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 0, HasHorizontalAlignment.ALIGN_CENTER);
125
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
126
                        permTable.getFlexCellFormatter().setColSpan(i, 1, 2);
127
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
128
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
129
                        i++;
130
                }
131
                for (; i < permTable.getRowCount(); i++)
132
                        permTable.removeRow(i);
133
        }
134

    
135
        void createDownloadLink(String[] link, FileResource file) {
136
                String dateString = AbstractRestCommand.getDate();
137
                String resource = file.getUri().substring(GSS.GSS_REST_PATH.length()-1,file.getUri().length());
138
                String sig = GSS.get().getCurrentUserResource().getUsername()+" "+AbstractRestCommand.calculateSig("GET", dateString, resource, AbstractRestCommand.base64decode(GSS.get().getToken()));
139
                link[0] = "<a class='hidden-link info' href='" + file.getUri() + "&Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString) + "' target='_blank'>";
140
                link[1] = "</a>";
141
        }
142

    
143
        void removeVersion(final FileResource version) {
144
                ExecuteDelete df = new ExecuteDelete(version.getUri()){
145

    
146
                        @Override
147
                        public void onComplete() {
148
                                toRemove = version;
149
                                updateTable();
150
                                GSS.get().getFileList().updateFileCache(false, true /*clear selection*/);
151
                        }
152

    
153
                        @Override
154
                        public void onError(Throwable t) {
155
                                GWT.log("", t);
156
                                if(t instanceof RestException){
157
                                        int statusCode = ((RestException)t).getHttpStatusCode();
158
                                        if(statusCode == 405)
159
                                                GSS.get().displayError("You don't have the necessary permissions");
160
                                        else if(statusCode == 404)
161
                                                GSS.get().displayError("Versions does not exist");
162
                                        else
163
                                                GSS.get().displayError("Unable to remove version:"+((RestException)t).getHttpStatusText());
164
                                }
165
                                else
166
                                        GSS.get().displayError("System error removing version:"+t.getMessage());
167
                        }
168
                };
169
                DeferredCommand.addCommand(df);
170

    
171
        }
172

    
173
        void restoreVersion(final FileResource version) {
174
                FileResource selectedFile = (FileResource) GSS.get().getCurrentSelection();
175
                ExecutePost ep = new ExecutePost(selectedFile.getUri()+"?restoreVersion="+version.getVersion(),"",200){
176

    
177

    
178
                        @Override
179
                        public void onComplete() {
180
                                container.hide();
181
                GSS.get().getFileList().updateFileCache(true, true /*clear selection*/);
182
                        }
183

    
184
                        @Override
185
                        public void onError(Throwable t) {
186
                                GWT.log("", t);
187
                                if(t instanceof RestException)
188
                                        GSS.get().displayError("Unable to restore version:"+((RestException)t).getHttpStatusText());
189
                                else
190
                                        GSS.get().displayError("System error restoring version:"+t.getMessage());
191
                        }
192

    
193
                };
194
                DeferredCommand.addCommand(ep);
195
        }
196

    
197
        private String formatDate(Date date){
198
                DateTimeFormat format = DateTimeFormat.getFormat("dd/MM/yyyy : HH:mm");
199
                return format.format(date);
200
        }
201

    
202
}