Revision 7ed6977e web_client/src/gr/grnet/pithos/web/client/VersionsList.java

b/web_client/src/gr/grnet/pithos/web/client/VersionsList.java
35 35
package gr.grnet.pithos.web.client;
36 36

  
37 37
import gr.grnet.pithos.web.client.FilePropertiesDialog.Images;
38
import gr.grnet.pithos.web.client.rest.DeleteCommand;
39
import gr.grnet.pithos.web.client.rest.GetCommand;
40
import gr.grnet.pithos.web.client.rest.PostCommand;
41
import gr.grnet.pithos.web.client.rest.RestCommand;
42
import gr.grnet.pithos.web.client.rest.RestException;
43
import gr.grnet.pithos.web.client.rest.resource.FileResource;
44
import gr.grnet.pithos.web.client.rest.resource.UserResource;
45
import gr.grnet.pithos.web.client.rest.resource.UserSearchResource;
46 38

  
47 39
import java.util.ArrayList;
48 40
import java.util.Collections;
......
50 42
import java.util.Date;
51 43
import java.util.List;
52 44

  
53

  
54
import com.google.gwt.core.client.GWT;
55 45
import com.google.gwt.event.dom.client.ClickEvent;
56 46
import com.google.gwt.event.dom.client.ClickHandler;
57
import com.google.gwt.http.client.URL;
58 47
import com.google.gwt.i18n.client.DateTimeFormat;
59
import com.google.gwt.user.client.DeferredCommand;
60 48
import com.google.gwt.user.client.Window;
61 49
import com.google.gwt.user.client.ui.AbstractImagePrototype;
62 50
import com.google.gwt.user.client.ui.Composite;
......
67 55

  
68 56
public class VersionsList extends Composite {
69 57

  
70
    private Pithos app;
71

  
72
	int selectedRow = -1;
73

  
74
	int permissionCount = -1;
75

  
76
	List<FileResource> versions = null;
77

  
78
	final Images images;
79

  
80
	final VerticalPanel permPanel = new VerticalPanel();
81

  
82
	final FlexTable permTable = new FlexTable();
83

  
84
	FileResource toRemove = null;
85

  
86
	FilePropertiesDialog container;
87

  
88
	public VersionsList(Pithos _app, FilePropertiesDialog aContainer, final Images theImages, List<FileResource> theVersions) {
89
        app = _app;
90
		images = theImages;
91
		container = aContainer;
92
		versions = theVersions;
93
		Collections.sort(theVersions, new Comparator<FileResource>(){
94

  
95
			@Override
96
			public int compare(FileResource o1, FileResource o2) {
97
				return o1.getVersion().compareTo(o2.getVersion());
98
			}
99

  
100
		});
101
		permTable.setText(0, 0, "Version");
102
		permTable.setText(0, 1, "Created");
103
		permTable.setText(0, 2, "Modified");
104
		permTable.setText(0, 3, "Size");
105
		permTable.setText(0, 4, "");
106
		permTable.setText(0, 5, "");
107
		permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
108
		permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
109
		permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
110
		permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
111
		permTable.getFlexCellFormatter().setColSpan(0, 1, 2);
112
		permTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
113
		permTable.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
114
		permTable.getFlexCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
115
		permTable.getFlexCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_CENTER);
116
		permPanel.add(permTable);
117
		permPanel.addStyleName("pithos-TabPanelBottom");
118
		permTable.addStyleName("pithos-permList");
119
		initWidget(permPanel);
120
		updateTable();
121
	}
122

  
123
	public void updateTable() {
124
		copyListAndContinue(versions);		
125
	}
126
	
127
	public void showVersionsTable(){
128
		int i = 1;
129
		if (toRemove != null) {
130
			versions.remove(toRemove);
131
			toRemove = null;
132
		}
133
		for (final FileResource dto : versions) {
134
			HTML restoreVersion = new HTML("<a href='#' class='hidden-link info'><span>"+AbstractImagePrototype.create(images.restore()).getHTML()+"</span><div>Restore this Version</div></a>");
135
			restoreVersion.addClickHandler(new ClickHandler() {
136
				@Override
137
				public void onClick(ClickEvent event) {
138
					restoreVersion(dto);
139
				}
140
			});
141

  
142
			permTable.setHTML(i, 0, "<span>" + dto.getVersion() + "</span>");
143
			permTable.setHTML(i, 1, "<span>" + formatDate(dto.getCreationDate()) + " by " + app.findUserFullName(dto.getCreatedBy()) + "</span>");
144
			permTable.setHTML(i, 2, "<span>" + formatDate(dto.getModificationDate()) + " by " + app.findUserFullName(dto.getModifiedBy()) + "</span>");
145
			permTable.setHTML(i, 3, "<span>" + dto.getFileSizeAsString() + "</span>");
146
			HTML downloadHtml = new HTML("<a class='hidden-link info' href='#'><span>"+AbstractImagePrototype.create(images.download()).getHTML()+"</span><div>View this Version</div></a>");
147
			downloadHtml.addClickHandler(new ClickHandler() {
148
				@Override
149
				public void onClick(ClickEvent event) {
150
					String dateString = RestCommand.getDate();
151
					String resource = dto.getUri().substring(app.getApiPath().length()-1, dto.getUri().length());
152
					String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken()));
153
					String fileUrl = dto.getUri() + "?version=" + dto.getVersion() + "&Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString);
154
					Window.open(fileUrl, "_BLANK", "");
155
				}
156
			});
157
			permTable.setWidget(i, 4, downloadHtml);
158
			permTable.setWidget(i, 5, restoreVersion);
159
			permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
160
			permTable.getFlexCellFormatter().setHorizontalAlignment(i, 0, HasHorizontalAlignment.ALIGN_CENTER);
161
			permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
162
			permTable.getFlexCellFormatter().setColSpan(i, 1, 2);
163
			permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
164
			permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
165
			i++;
166
		}
167
		for (; i < permTable.getRowCount(); i++)
168
			permTable.removeRow(i);
169
	}
170

  
171
	void removeVersion(final FileResource version) {
172
		DeleteCommand df = new DeleteCommand(app, version.getUri()){
173

  
174
			@Override
175
			public void onComplete() {
176
				toRemove = version;
177
				updateTable();
178
				app.getTreeView().refreshCurrentNode(false);
179
			}
180

  
181
			@Override
182
			public void onError(Throwable t) {
183
				GWT.log("", t);
184
				if(t instanceof RestException){
185
					int statusCode = ((RestException)t).getHttpStatusCode();
186
					if(statusCode == 405)
187
						app.displayError("You don't have the necessary permissions");
188
					else if(statusCode == 404)
189
						app.displayError("Versions does not exist");
190
					else
191
						app.displayError("Unable to remove version:"+((RestException)t).getHttpStatusText());
192
				}
193
				else
194
					app.displayError("System error removing version:"+t.getMessage());
195
			}
196
		};
197
		DeferredCommand.addCommand(df);
198

  
199
	}
200

  
201
	void restoreVersion(final FileResource version) {
202
		FileResource selectedFile = (FileResource) app.getCurrentSelection();
203
		PostCommand ep = new PostCommand(app, selectedFile.getUri()+"?restoreVersion="+version.getVersion(),"",200){
204

  
205

  
206
			@Override
207
			public void onComplete() {
208
				container.hide();
209
                app.getTreeView().refreshCurrentNode(false);
210
			}
211

  
212
			@Override
213
			public void onError(Throwable t) {
214
				GWT.log("", t);
215
				if(t instanceof RestException)
216
					app.displayError("Unable to restore version:"+((RestException)t).getHttpStatusText());
217
				else
218
					app.displayError("System error restoring version:"+t.getMessage());
219
			}
220

  
221
		};
222
		DeferredCommand.addCommand(ep);
223
	}
224

  
225
	private String formatDate(Date date){
226
		DateTimeFormat format = DateTimeFormat.getFormat("dd/MM/yyyy : HH:mm");
227
		return format.format(date);
228
	}
229
	
230
	/**
231
	 * Copies the input List to a new List
232
	 * @param input
233
	 */
234
	private void copyListAndContinue(List<FileResource> input){
235
		List<FileResource> copiedInput = new ArrayList<FileResource>();		
236
		for(FileResource dto : input) {
237
			copiedInput.add(dto);
238
		}
239
		handleFullNames(copiedInput);
240
	}
241
	
242
	/**
243
	 * Examines whether or not the user's full name exists in the 
244
	 * userFullNameMap in the Pithos.java for every element of the input list.
245
	 * If the user's full name does not exist in the map then a request is being made
246
	 * for the specific username.  
247
	 * 
248
	 * @param input
249
	 */
250
	private void handleFullNames(List<FileResource> input){		
251
		if(input.isEmpty()){
252
			showVersionsTable();
253
			return;
254
		}
255
		
256
		if(app.findUserFullName(input.get(0).getOwner()) == null){
257
			findFullNameAndUpdate(input);		
258
			return;
259
		}
260
				
261
		if(input.size() >= 1){
262
			input.remove(input.get(0));
263
			if(input.isEmpty()){
264
				showVersionsTable();			
265
			}else{
266
				handleFullNames(input);
267
			}
268
		}					
269
	}
270
	
271
	/**
272
	 * Makes a request to search for full name from a given username
273
	 * and continues checking the next element of the List.
274
	 *  
275
	 * @param input
276
	 */
277

  
278
	private void findFullNameAndUpdate(final List<FileResource> input){				
279
		final String aUserName = input.get(0).getOwner();
280
		String path = app.getApiPath() + "users/" + aUserName;
281

  
282
		GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(app, UserSearchResource.class, path, false,null) {
283
			@Override
284
			public void onComplete() {
285
				final UserSearchResource result = getResult();
286
				for (UserResource user : result.getUsers()){
287
					String username = user.getUsername();
288
					String userFullName = user.getName();
289
					app.putUserToMap(username, userFullName);
290
					if(input.size() >= 1){
291
						input.remove(input.get(0));						
292
						if(input.isEmpty()){
293
							showVersionsTable();
294
							return;
295
						}
296
						handleFullNames(input);										
297
					}									
298
				}
299
			}
300
			@Override
301
			public void onError(Throwable t) {				
302
				app.displayError("Unable to fetch user's full name from the given username " + aUserName);
303
				if(input.size() >= 1){
304
					input.remove(input.get(0));
305
					handleFullNames(input);					
306
				}
307
			}
308
		};
309
		DeferredCommand.addCommand(gg);
310
	
311
	}
312

  
58
//    private Pithos app;
59
//
60
//	int selectedRow = -1;
61
//
62
//	int permissionCount = -1;
63
//
64
//	List<FileResource> versions = null;
65
//
66
//	final Images images;
67
//
68
//	final VerticalPanel permPanel = new VerticalPanel();
69
//
70
//	final FlexTable permTable = new FlexTable();
71
//
72
//	FileResource toRemove = null;
73
//
74
//	FilePropertiesDialog container;
75
//
76
//	public VersionsList(Pithos _app, FilePropertiesDialog aContainer, final Images theImages, List<FileResource> theVersions) {
77
//        app = _app;
78
//		images = theImages;
79
//		container = aContainer;
80
//		versions = theVersions;
81
//		Collections.sort(theVersions, new Comparator<FileResource>(){
82
//
83
//			@Override
84
//			public int compare(FileResource o1, FileResource o2) {
85
//				return o1.getVersion().compareTo(o2.getVersion());
86
//			}
87
//
88
//		});
89
//		permTable.setText(0, 0, "Version");
90
//		permTable.setText(0, 1, "Created");
91
//		permTable.setText(0, 2, "Modified");
92
//		permTable.setText(0, 3, "Size");
93
//		permTable.setText(0, 4, "");
94
//		permTable.setText(0, 5, "");
95
//		permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
96
//		permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
97
//		permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
98
//		permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
99
//		permTable.getFlexCellFormatter().setColSpan(0, 1, 2);
100
//		permTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
101
//		permTable.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
102
//		permTable.getFlexCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
103
//		permTable.getFlexCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_CENTER);
104
//		permPanel.add(permTable);
105
//		permPanel.addStyleName("pithos-TabPanelBottom");
106
//		permTable.addStyleName("pithos-permList");
107
//		initWidget(permPanel);
108
//		updateTable();
109
//	}
110
//
111
//	public void updateTable() {
112
//		copyListAndContinue(versions);
113
//	}
114
//
115
//	public void showVersionsTable(){
116
//		int i = 1;
117
//		if (toRemove != null) {
118
//			versions.remove(toRemove);
119
//			toRemove = null;
120
//		}
121
//		for (final FileResource dto : versions) {
122
//			HTML restoreVersion = new HTML("<a href='#' class='hidden-link info'><span>"+AbstractImagePrototype.create(images.restore()).getHTML()+"</span><div>Restore this Version</div></a>");
123
//			restoreVersion.addClickHandler(new ClickHandler() {
124
//				@Override
125
//				public void onClick(ClickEvent event) {
126
//					restoreVersion(dto);
127
//				}
128
//			});
129
//
130
//			permTable.setHTML(i, 0, "<span>" + dto.getVersion() + "</span>");
131
//			permTable.setHTML(i, 1, "<span>" + formatDate(dto.getCreationDate()) + " by " + app.findUserFullName(dto.getCreatedBy()) + "</span>");
132
//			permTable.setHTML(i, 2, "<span>" + formatDate(dto.getModificationDate()) + " by " + app.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
//					String fileUrl = dto.getUri() + "?version=" + dto.getVersion();
139
//					Window.open(fileUrl, "_BLANK", "");
140
//				}
141
//			});
142
//			permTable.setWidget(i, 4, downloadHtml);
143
//			permTable.setWidget(i, 5, restoreVersion);
144
//			permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
145
//			permTable.getFlexCellFormatter().setHorizontalAlignment(i, 0, HasHorizontalAlignment.ALIGN_CENTER);
146
//			permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
147
//			permTable.getFlexCellFormatter().setColSpan(i, 1, 2);
148
//			permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
149
//			permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
150
//			i++;
151
//		}
152
//		for (; i < permTable.getRowCount(); i++)
153
//			permTable.removeRow(i);
154
//	}
155
//
156
//	void restoreVersion(final FileResource version) {
157
////		FileResource selectedFile = (FileResource) app.getCurrentSelection();
158
////		PostCommand ep = new PostCommand(app, selectedFile.getUri()+"?restoreVersion="+version.getVersion(),"",200){
159
////
160
////
161
////			@Override
162
////			public void onComplete() {
163
////				container.hide();
164
////                app.getTreeView().refreshCurrentNode(false);
165
////			}
166
////
167
////			@Override
168
////			public void onError(Throwable t) {
169
////				GWT.log("", t);
170
////				if(t instanceof RestException)
171
////					app.displayError("Unable to restore version:"+((RestException)t).getHttpStatusText());
172
////				else
173
////					app.displayError("System error restoring version:"+t.getMessage());
174
////			}
175
////
176
////		};
177
////		DeferredCommand.addCommand(ep);
178
//	}
179
//
180
//	private String formatDate(Date date){
181
//		DateTimeFormat format = DateTimeFormat.getFormat("dd/MM/yyyy : HH:mm");
182
//		return format.format(date);
183
//	}
184
//
185
//	/**
186
//	 * Copies the input List to a new List
187
//	 * @param input
188
//	 */
189
//	private void copyListAndContinue(List<FileResource> input){
190
//		List<FileResource> copiedInput = new ArrayList<FileResource>();
191
//		for(FileResource dto : input) {
192
//			copiedInput.add(dto);
193
//		}
194
//		handleFullNames(copiedInput);
195
//	}
196
//
197
//	/**
198
//	 * Examines whether or not the user's full name exists in the
199
//	 * userFullNameMap in the Pithos.java for every element of the input list.
200
//	 * If the user's full name does not exist in the map then a request is being made
201
//	 * for the specific username.
202
//	 *
203
//	 * @param input
204
//	 */
205
//	private void handleFullNames(List<FileResource> input){
206
//		if(input.isEmpty()){
207
//			showVersionsTable();
208
//			return;
209
//		}
210
//
211
//		if(app.findUserFullName(input.get(0).getOwner()) == null){
212
//			findFullNameAndUpdate(input);
213
//			return;
214
//		}
215
//
216
//		if(input.size() >= 1){
217
//			input.remove(input.get(0));
218
//			if(input.isEmpty()){
219
//				showVersionsTable();
220
//			}else{
221
//				handleFullNames(input);
222
//			}
223
//		}
224
//	}
225
//
226
//	/**
227
//	 * Makes a request to search for full name from a given username
228
//	 * and continues checking the next element of the List.
229
//	 *
230
//	 * @param input
231
//	 */
232
//
233
//	private void findFullNameAndUpdate(final List<FileResource> input){
234
////		final String aUserName = input.get(0).getOwner();
235
////		String path = app.getApiPath() + "users/" + aUserName;
236
////
237
////		GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(app, UserSearchResource.class, path, false,null) {
238
////			@Override
239
////			public void onComplete() {
240
////				final UserSearchResource result = getResult();
241
////				for (UserResource user : result.getUsers()){
242
////					String username = user.getUsername();
243
////					String userFullName = user.getName();
244
////					app.putUserToMap(username, userFullName);
245
////					if(input.size() >= 1){
246
////						input.remove(input.get(0));
247
////						if(input.isEmpty()){
248
////							showVersionsTable();
249
////							return;
250
////						}
251
////						handleFullNames(input);
252
////					}
253
////				}
254
////			}
255
////			@Override
256
////			public void onError(Throwable t) {
257
////				app.displayError("Unable to fetch user's full name from the given username " + aUserName);
258
////				if(input.size() >= 1){
259
////					input.remove(input.get(0));
260
////					handleFullNames(input);
261
////				}
262
////			}
263
////		};
264
////		DeferredCommand.addCommand(gg);
265
//
266
//	}
267
//
313 268
}

Also available in: Unified diff