Revision 31477486 src/gr/grnet/pithos/web/client/commands/RestoreTrashCommand.java
b/src/gr/grnet/pithos/web/client/commands/RestoreTrashCommand.java | ||
---|---|---|
34 | 34 |
*/ |
35 | 35 |
package gr.grnet.pithos.web.client.commands; |
36 | 36 |
|
37 |
import java.util.Iterator; |
|
38 |
import java.util.List; |
|
39 |
|
|
37 | 40 |
import gr.grnet.pithos.web.client.Pithos; |
41 |
import gr.grnet.pithos.web.client.foldertree.File; |
|
42 |
import gr.grnet.pithos.web.client.foldertree.Folder; |
|
43 |
import gr.grnet.pithos.web.client.foldertree.Resource; |
|
44 |
import gr.grnet.pithos.web.client.rest.DeleteRequest; |
|
45 |
import gr.grnet.pithos.web.client.rest.PutRequest; |
|
46 |
import gr.grnet.pithos.web.client.rest.RestException; |
|
38 | 47 |
|
48 |
import com.google.gwt.core.client.GWT; |
|
49 |
import com.google.gwt.core.client.Scheduler; |
|
39 | 50 |
import com.google.gwt.user.client.Command; |
40 | 51 |
import com.google.gwt.user.client.ui.PopupPanel; |
41 | 52 |
|
... | ... | |
45 | 56 |
* Restore trashed files and folders. |
46 | 57 |
* |
47 | 58 |
*/ |
48 |
public class RestoreTrashCommand implements Command{ |
|
59 |
public class RestoreTrashCommand implements Command {
|
|
49 | 60 |
private PopupPanel containerPanel; |
50 |
private Pithos app; |
|
61 |
protected Pithos app; |
|
62 |
protected Object resource; |
|
51 | 63 |
|
52 |
public RestoreTrashCommand(Pithos _app, PopupPanel _containerPanel){ |
|
53 |
app = _app; |
|
64 |
public RestoreTrashCommand(Pithos _app, PopupPanel _containerPanel, Object _resource){ |
|
54 | 65 |
containerPanel = _containerPanel; |
66 |
app = _app; |
|
67 |
resource = _resource; |
|
55 | 68 |
} |
56 | 69 |
|
57 | 70 |
@Override |
58 | 71 |
public void execute() { |
59 |
containerPanel.hide(); |
|
60 |
// Object selection = app.getCurrentSelection(); |
|
61 |
// if (selection == null){ |
|
62 |
// // Check to see if Trash Node is selected. |
|
63 |
// List folderList = new ArrayList(); |
|
64 |
// TrashResource trashItem = app.getTreeView().getTrash(); |
|
65 |
// for(int i=0 ; i < trashItem.getFolders().size() ; i++) |
|
66 |
// folderList.add(trashItem.getFolders().get(i)); |
|
67 |
// return; |
|
68 |
// } |
|
69 |
// GWT.log("selection: " + selection.toString(), null); |
|
70 |
// if (selection instanceof FileResource) { |
|
71 |
// final FileResource resource = (FileResource)selection; |
|
72 |
// PostCommand rt = new PostCommand(app, resource.getUri()+"?restore=","", 200){ |
|
73 |
// |
|
74 |
// @Override |
|
75 |
// public void onComplete() { |
|
76 |
// //TODO:CELLTREE |
|
77 |
// //app.getFolders().update(app.getFolders().getTrashItem()); |
|
78 |
// |
|
79 |
// app.showFileList(true); |
|
80 |
// } |
|
81 |
// |
|
82 |
// @Override |
|
83 |
// public void onError(Throwable t) { |
|
84 |
// GWT.log("", t); |
|
85 |
// if(t instanceof RestException){ |
|
86 |
// int statusCode = ((RestException)t).getHttpStatusCode(); |
|
87 |
// if(statusCode == 405) |
|
88 |
// app.displayError("You don't have the necessary permissions"); |
|
89 |
// else if(statusCode == 404) |
|
90 |
// app.displayError("File does not exist"); |
|
91 |
// else if(statusCode == 409) |
|
92 |
// app.displayError("A file with the same name already exists"); |
|
93 |
// else if(statusCode == 413) |
|
94 |
// app.displayError("Your quota has been exceeded"); |
|
95 |
// else |
|
96 |
// app.displayError("Unable to restore file:"+((RestException)t).getHttpStatusText()); |
|
97 |
// } |
|
98 |
// else |
|
99 |
// app.displayError("System error restoring file:"+t.getMessage()); |
|
100 |
// } |
|
101 |
// }; |
|
102 |
// DeferredCommand.addCommand(rt); |
|
103 |
// } |
|
104 |
// else if (selection instanceof List) { |
|
105 |
// final List<FileResource> fdtos = (List<FileResource>) selection; |
|
106 |
// final List<String> fileIds = new ArrayList<String>(); |
|
107 |
// for(FileResource f : fdtos) |
|
108 |
// fileIds.add(f.getUri()+"?restore="); |
|
109 |
// MultiplePostCommand rt = new MultiplePostCommand(app, fileIds.toArray(new String[0]), 200){ |
|
110 |
// |
|
111 |
// @Override |
|
112 |
// public void onComplete() { |
|
113 |
// //TODO:CELLTREE |
|
114 |
// //app.getFolders().update(app.getFolders().getTrashItem()); |
|
115 |
// app.showFileList(true); |
|
116 |
// } |
|
117 |
// |
|
118 |
// @Override |
|
119 |
// public void onError(String p, Throwable t) { |
|
120 |
// GWT.log("", t); |
|
121 |
// if(t instanceof RestException){ |
|
122 |
// int statusCode = ((RestException)t).getHttpStatusCode(); |
|
123 |
// if(statusCode == 405) |
|
124 |
// app.displayError("You don't have the necessary permissions"); |
|
125 |
// else if(statusCode == 404) |
|
126 |
// app.displayError("File does not exist"); |
|
127 |
// else if(statusCode == 409) |
|
128 |
// app.displayError("A file with the same name already exists"); |
|
129 |
// else if(statusCode == 413) |
|
130 |
// app.displayError("Your quota has been exceeded"); |
|
131 |
// else |
|
132 |
// app.displayError("Unable to restore file::"+((RestException)t).getHttpStatusText()); |
|
133 |
// } |
|
134 |
// else |
|
135 |
// app.displayError("System error restoring file:"+t.getMessage()); |
|
136 |
// } |
|
137 |
// }; |
|
138 |
// DeferredCommand.addCommand(rt); |
|
139 |
// } |
|
140 |
// else if (selection instanceof TrashFolderResource) { |
|
141 |
// final FolderResource resource = ((TrashFolderResource)selection).getResource(); |
|
142 |
// PostCommand rt = new PostCommand(app, resource.getUri()+"?restore=","", 200){ |
|
143 |
// |
|
144 |
// @Override |
|
145 |
// public void onComplete() { |
|
146 |
// //TODO:CELLTREE |
|
147 |
// /* |
|
148 |
// app.getFolders().updateFolder((DnDTreeItem) app.getFolders().getRootItem()); |
|
149 |
// |
|
150 |
// app.getFolders().update(app.getFolders().getTrashItem()); |
|
151 |
// */ |
|
152 |
// |
|
153 |
// app.getTreeView().updateTrashNode(); |
|
154 |
// app.getTreeView().updateRootNode(); |
|
155 |
// } |
|
156 |
// |
|
157 |
// @Override |
|
158 |
// public void onError(Throwable t) { |
|
159 |
// GWT.log("", t); |
|
160 |
// if(t instanceof RestException){ |
|
161 |
// int statusCode = ((RestException)t).getHttpStatusCode(); |
|
162 |
// if(statusCode == 405) |
|
163 |
// app.displayError("You don't have the necessary permissions"); |
|
164 |
// else if(statusCode == 404) |
|
165 |
// app.displayError("Folder does not exist"); |
|
166 |
// else if(statusCode == 409) |
|
167 |
// app.displayError("A folder with the same name already exists"); |
|
168 |
// else if(statusCode == 413) |
|
169 |
// app.displayError("Your quota has been exceeded"); |
|
170 |
// else |
|
171 |
// app.displayError("Unable to restore folder::"+((RestException)t).getHttpStatusText()); |
|
172 |
// } |
|
173 |
// else |
|
174 |
// app.displayError("System error restoring folder:"+t.getMessage()); |
|
175 |
// } |
|
176 |
// }; |
|
177 |
// DeferredCommand.addCommand(rt); |
|
178 |
// } |
|
72 |
if (containerPanel != null) |
|
73 |
containerPanel.hide(); |
|
74 |
if (resource instanceof List) { |
|
75 |
@SuppressWarnings("unchecked") |
|
76 |
Iterator<File> iter = ((List<File>) resource).iterator(); |
|
77 |
untrashFiles(iter, new Command() { |
|
78 |
@SuppressWarnings("unchecked") |
|
79 |
@Override |
|
80 |
public void execute() { |
|
81 |
app.updateFolder(((List<File>) resource).get(0).getParent(), true, null); |
|
82 |
} |
|
83 |
}); |
|
84 |
} |
|
85 |
else if (resource instanceof Folder) { |
|
86 |
final Folder toBeUnTrashed = (Folder) resource; |
|
87 |
untrashFolder(toBeUnTrashed, new Command() { |
|
88 |
@Override |
|
89 |
public void execute() { |
|
90 |
app.updateFolder(toBeUnTrashed.getParent(), true, null); |
|
91 |
} |
|
92 |
}); |
|
179 | 93 |
|
94 |
} |
|
180 | 95 |
} |
181 | 96 |
|
97 |
private void untrashFolder(final Folder f, final Command callback) { |
|
98 |
String path = "/" + Pithos.HOME_CONTAINER + "/" + f.getPrefix(); |
|
99 |
PutRequest createFolder = new PutRequest(app.getApiPath(), app.getUsername(), path) { |
|
100 |
@Override |
|
101 |
public void onSuccess(@SuppressWarnings("unused") Resource result) { |
|
102 |
Iterator<File> iter = f.getFiles().iterator(); |
|
103 |
untrashFiles(iter, new Command() { |
|
104 |
@Override |
|
105 |
public void execute() { |
|
106 |
Iterator<Folder> iterf = f.getSubfolders().iterator(); |
|
107 |
untrashSubfolders(iterf, new Command() { |
|
108 |
|
|
109 |
@Override |
|
110 |
public void execute() { |
|
111 |
DeleteRequest deleteFolder = new DeleteRequest(app.getApiPath(), f.getOwner(), f.getUri()) { |
|
112 |
|
|
113 |
@Override |
|
114 |
public void onSuccess(Resource result) { |
|
115 |
if (callback != null) |
|
116 |
callback.execute(); |
|
117 |
} |
|
118 |
|
|
119 |
@Override |
|
120 |
public void onError(Throwable t) { |
|
121 |
GWT.log("", t); |
|
122 |
if (t instanceof RestException) { |
|
123 |
app.displayError("Unable to delete folder: " + ((RestException) t).getHttpStatusText()); |
|
124 |
} |
|
125 |
else |
|
126 |
app.displayError("System error unable to delete folder: "+t.getMessage()); |
|
127 |
} |
|
128 |
}; |
|
129 |
deleteFolder.setHeader("X-Auth-Token", app.getToken()); |
|
130 |
Scheduler.get().scheduleDeferred(deleteFolder); |
|
131 |
} |
|
132 |
}); |
|
133 |
} |
|
134 |
}); |
|
135 |
} |
|
136 |
|
|
137 |
@Override |
|
138 |
public void onError(Throwable t) { |
|
139 |
GWT.log("", t); |
|
140 |
if (t instanceof RestException) { |
|
141 |
app.displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText()); |
|
142 |
} |
|
143 |
else |
|
144 |
app.displayError("System error creating folder:" + t.getMessage()); |
|
145 |
} |
|
146 |
}; |
|
147 |
createFolder.setHeader("X-Auth-Token", app.getToken()); |
|
148 |
createFolder.setHeader("Accept", "*/*"); |
|
149 |
createFolder.setHeader("Content-Length", "0"); |
|
150 |
createFolder.setHeader("Content-Type", "application/folder"); |
|
151 |
Scheduler.get().scheduleDeferred(createFolder); |
|
152 |
} |
|
153 |
|
|
154 |
protected void untrashFiles(final Iterator<File> iter, final Command callback) { |
|
155 |
if (iter.hasNext()) { |
|
156 |
File file = iter.next(); |
|
157 |
String path = "/" + Pithos.HOME_CONTAINER + "/" + file.getPath(); |
|
158 |
PutRequest untrashFile = new PutRequest(app.getApiPath(), app.getUsername(), path) { |
|
159 |
@Override |
|
160 |
public void onSuccess(@SuppressWarnings("unused") Resource result) { |
|
161 |
untrashFiles(iter, callback); |
|
162 |
} |
|
163 |
|
|
164 |
@Override |
|
165 |
public void onError(Throwable t) { |
|
166 |
GWT.log("", t); |
|
167 |
if (t instanceof RestException) { |
|
168 |
app.displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText()); |
|
169 |
} |
|
170 |
else |
|
171 |
app.displayError("System error unable to copy file: "+t.getMessage()); |
|
172 |
} |
|
173 |
}; |
|
174 |
untrashFile.setHeader("X-Auth-Token", app.getToken()); |
|
175 |
untrashFile.setHeader("X-Move-From", file.getUri()); |
|
176 |
Scheduler.get().scheduleDeferred(untrashFile); |
|
177 |
} |
|
178 |
else if (callback != null) { |
|
179 |
callback.execute(); |
|
180 |
} |
|
181 |
} |
|
182 |
|
|
183 |
protected void untrashSubfolders(final Iterator<Folder> iter, final Command callback) { |
|
184 |
if (iter.hasNext()) { |
|
185 |
final Folder f = iter.next(); |
|
186 |
untrashFolder(f, callback); |
|
187 |
} |
|
188 |
else { |
|
189 |
if (callback != null) |
|
190 |
callback.execute(); |
|
191 |
} |
|
192 |
} |
|
182 | 193 |
} |
Also available in: Unified diff