root / src / gr / ebs / gss / client / FileUploadGearsDialog.java @ 5210ea4c
History | View | Annotate | Download (13.4 kB)
1 | 023f6f1e | Panagiotis Astithas | /*
|
---|---|---|---|
2 | 023f6f1e | Panagiotis Astithas | * Copyright 2009 Electronic Business Systems Ltd.
|
3 | 023f6f1e | Panagiotis Astithas | *
|
4 | 023f6f1e | Panagiotis Astithas | * This file is part of GSS.
|
5 | 023f6f1e | Panagiotis Astithas | *
|
6 | 023f6f1e | Panagiotis Astithas | * GSS is free software: you can redistribute it and/or modify
|
7 | 023f6f1e | Panagiotis Astithas | * it under the terms of the GNU General Public License as published by
|
8 | 023f6f1e | Panagiotis Astithas | * the Free Software Foundation, either version 3 of the License, or
|
9 | 023f6f1e | Panagiotis Astithas | * (at your option) any later version.
|
10 | 023f6f1e | Panagiotis Astithas | *
|
11 | 023f6f1e | Panagiotis Astithas | * GSS is distributed in the hope that it will be useful,
|
12 | 023f6f1e | Panagiotis Astithas | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 023f6f1e | Panagiotis Astithas | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 | 023f6f1e | Panagiotis Astithas | * GNU General Public License for more details.
|
15 | 023f6f1e | Panagiotis Astithas | *
|
16 | 023f6f1e | Panagiotis Astithas | * You should have received a copy of the GNU General Public License
|
17 | 023f6f1e | Panagiotis Astithas | * along with GSS. If not, see <http://www.gnu.org/licenses/>.
|
18 | 023f6f1e | Panagiotis Astithas | */
|
19 | 023f6f1e | Panagiotis Astithas | package gr.ebs.gss.client; |
20 | 023f6f1e | Panagiotis Astithas | |
21 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.client.rest.PostCommand; |
22 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.client.rest.RestCommand; |
23 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.client.rest.RestException; |
24 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.client.rest.resource.FileResource; |
25 | 023f6f1e | Panagiotis Astithas | import gr.ebs.gss.client.rest.resource.FolderResource; |
26 | 023f6f1e | Panagiotis Astithas | |
27 | 023f6f1e | Panagiotis Astithas | import java.util.ArrayList; |
28 | 023f6f1e | Panagiotis Astithas | import java.util.Arrays; |
29 | 023f6f1e | Panagiotis Astithas | import java.util.HashMap; |
30 | 023f6f1e | Panagiotis Astithas | import java.util.List; |
31 | 023f6f1e | Panagiotis Astithas | import java.util.Map; |
32 | 023f6f1e | Panagiotis Astithas | |
33 | 023f6f1e | Panagiotis Astithas | import com.google.gwt.core.client.GWT; |
34 | 023f6f1e | Panagiotis Astithas | import com.google.gwt.event.dom.client.ClickEvent; |
35 | 023f6f1e | Panagiotis Astithas | import com.google.gwt.event.dom.client.ClickHandler; |
36 | 023f6f1e | Panagiotis Astithas | import com.google.gwt.gears.client.Factory; |
37 | 023f6f1e | Panagiotis Astithas | import com.google.gwt.gears.client.desktop.Desktop; |
38 | 023f6f1e | Panagiotis Astithas | import com.google.gwt.gears.client.desktop.File; |
39 | 023f6f1e | Panagiotis Astithas | import com.google.gwt.gears.client.desktop.OpenFilesHandler; |
40 | 023f6f1e | Panagiotis Astithas | import com.google.gwt.gears.client.httprequest.HttpRequest; |
41 | 023f6f1e | Panagiotis Astithas | import com.google.gwt.gears.client.httprequest.ProgressEvent; |
42 | 023f6f1e | Panagiotis Astithas | import com.google.gwt.gears.client.httprequest.ProgressHandler; |
43 | 023f6f1e | Panagiotis Astithas | import com.google.gwt.gears.client.httprequest.RequestCallback; |
44 | 023f6f1e | Panagiotis Astithas | import com.google.gwt.http.client.URL; |
45 | 023f6f1e | Panagiotis Astithas | import com.google.gwt.json.client.JSONObject; |
46 | 023f6f1e | Panagiotis Astithas | import com.google.gwt.json.client.JSONString; |
47 | 023f6f1e | Panagiotis Astithas | import com.google.gwt.user.client.DeferredCommand; |
48 | 023f6f1e | Panagiotis Astithas | import com.google.gwt.user.client.ui.Button; |
49 | 023f6f1e | Panagiotis Astithas | import com.google.gwt.user.client.ui.FlexTable; |
50 | 023f6f1e | Panagiotis Astithas | import com.google.gwt.user.client.ui.HTML; |
51 | 023f6f1e | Panagiotis Astithas | import com.google.gwt.user.client.ui.HasHorizontalAlignment; |
52 | 023f6f1e | Panagiotis Astithas | import com.google.gwt.user.client.ui.HorizontalPanel; |
53 | 023f6f1e | Panagiotis Astithas | import com.google.gwt.user.client.ui.VerticalPanel; |
54 | 023f6f1e | Panagiotis Astithas | |
55 | 023f6f1e | Panagiotis Astithas | /**
|
56 | 023f6f1e | Panagiotis Astithas | * The 'File upload' dialog box implementation with Google Gears support.
|
57 | 023f6f1e | Panagiotis Astithas | */
|
58 | 023f6f1e | Panagiotis Astithas | public class FileUploadGearsDialog extends FileUploadDialog implements Updateable { |
59 | 023f6f1e | Panagiotis Astithas | |
60 | 023f6f1e | Panagiotis Astithas | protected final Factory factory = Factory.getInstance(); |
61 | 023f6f1e | Panagiotis Astithas | |
62 | 023f6f1e | Panagiotis Astithas | /**
|
63 | 023f6f1e | Panagiotis Astithas | * The array of files to upload.
|
64 | 023f6f1e | Panagiotis Astithas | */
|
65 | 023f6f1e | Panagiotis Astithas | private File[] fileObjects; |
66 | 023f6f1e | Panagiotis Astithas | |
67 | 023f6f1e | Panagiotis Astithas | /**
|
68 | 023f6f1e | Panagiotis Astithas | * A list of files to upload, created from files array. Used to signal
|
69 | 023f6f1e | Panagiotis Astithas | * finished state when empty.
|
70 | 023f6f1e | Panagiotis Astithas | */
|
71 | 023f6f1e | Panagiotis Astithas | protected List<File> selectedFiles = new ArrayList<File>(); |
72 | 023f6f1e | Panagiotis Astithas | |
73 | 023f6f1e | Panagiotis Astithas | /**
|
74 | 023f6f1e | Panagiotis Astithas | * The list of progress bars for individual files.
|
75 | 023f6f1e | Panagiotis Astithas | */
|
76 | 023f6f1e | Panagiotis Astithas | protected List<ProgressBar> progressBars = new ArrayList<ProgressBar>(); |
77 | 023f6f1e | Panagiotis Astithas | |
78 | 023f6f1e | Panagiotis Astithas | private Button browse; |
79 | 023f6f1e | Panagiotis Astithas | |
80 | 023f6f1e | Panagiotis Astithas | private Button submit; |
81 | 023f6f1e | Panagiotis Astithas | |
82 | 023f6f1e | Panagiotis Astithas | private FlexTable generalTable;
|
83 | 023f6f1e | Panagiotis Astithas | |
84 | 023f6f1e | Panagiotis Astithas | private Map<String, FileResource> toRename; |
85 | 023f6f1e | Panagiotis Astithas | |
86 | 023f6f1e | Panagiotis Astithas | protected List<HttpRequest> requests = new ArrayList<HttpRequest>(); |
87 | e0c0ebc0 | Natasa Kapravelou | |
88 | e0c0ebc0 | Natasa Kapravelou | private boolean canContinue = true; |
89 | 023f6f1e | Panagiotis Astithas | |
90 | 023f6f1e | Panagiotis Astithas | /**
|
91 | 023f6f1e | Panagiotis Astithas | * The widget's constructor.
|
92 | 023f6f1e | Panagiotis Astithas | */
|
93 | 023f6f1e | Panagiotis Astithas | public FileUploadGearsDialog() {
|
94 | 023f6f1e | Panagiotis Astithas | // Set the dialog's caption.
|
95 | 023f6f1e | Panagiotis Astithas | setText("File upload");
|
96 | 023f6f1e | Panagiotis Astithas | setAnimationEnabled(true);
|
97 | 023f6f1e | Panagiotis Astithas | // Create a panel to hold all of the dialog widgets.
|
98 | 023f6f1e | Panagiotis Astithas | VerticalPanel panel = new VerticalPanel();
|
99 | 023f6f1e | Panagiotis Astithas | final HTML info = new HTML("You may select one or more files to upload."); |
100 | 023f6f1e | Panagiotis Astithas | info.addStyleName("gss-uploadNote");
|
101 | 023f6f1e | Panagiotis Astithas | panel.add(info); |
102 | 023f6f1e | Panagiotis Astithas | // Add an informative label with the folder name.
|
103 | 023f6f1e | Panagiotis Astithas | Object selection = GSS.get().getFolders().getCurrent().getUserObject();
|
104 | 023f6f1e | Panagiotis Astithas | folder = (FolderResource) selection; |
105 | 023f6f1e | Panagiotis Astithas | |
106 | 023f6f1e | Panagiotis Astithas | browse = new Button("Browse..."); |
107 | 023f6f1e | Panagiotis Astithas | |
108 | 023f6f1e | Panagiotis Astithas | HorizontalPanel fileUploadPanel = new HorizontalPanel();
|
109 | 023f6f1e | Panagiotis Astithas | fileUploadPanel.add(browse); |
110 | 023f6f1e | Panagiotis Astithas | |
111 | 023f6f1e | Panagiotis Astithas | generalTable = new FlexTable();
|
112 | 023f6f1e | Panagiotis Astithas | generalTable.setText(0, 0, "Folder"); |
113 | 023f6f1e | Panagiotis Astithas | generalTable.setText(1, 0, "File"); |
114 | 023f6f1e | Panagiotis Astithas | generalTable.setText(0, 1, folder.getName()); |
115 | 023f6f1e | Panagiotis Astithas | generalTable.setWidget(1, 1, fileUploadPanel); |
116 | 023f6f1e | Panagiotis Astithas | generalTable.getCellFormatter().setStyleName(0, 0, "props-labels"); |
117 | 023f6f1e | Panagiotis Astithas | generalTable.getCellFormatter().setStyleName(1, 0, "props-labels"); |
118 | 023f6f1e | Panagiotis Astithas | generalTable.getCellFormatter().setStyleName(0, 1, "props-values"); |
119 | 023f6f1e | Panagiotis Astithas | generalTable.getCellFormatter().setStyleName(1, 1, "props-values"); |
120 | 023f6f1e | Panagiotis Astithas | generalTable.setCellSpacing(4);
|
121 | 023f6f1e | Panagiotis Astithas | |
122 | 023f6f1e | Panagiotis Astithas | panel.add(generalTable); |
123 | 023f6f1e | Panagiotis Astithas | |
124 | 023f6f1e | Panagiotis Astithas | // Create a panel to hold the buttons.
|
125 | 023f6f1e | Panagiotis Astithas | HorizontalPanel buttons = new HorizontalPanel();
|
126 | 023f6f1e | Panagiotis Astithas | |
127 | 023f6f1e | Panagiotis Astithas | submit = new Button("Upload"); |
128 | 023f6f1e | Panagiotis Astithas | submit.addClickHandler(new ClickHandler() {
|
129 | 023f6f1e | Panagiotis Astithas | @Override
|
130 | 023f6f1e | Panagiotis Astithas | public void onClick(ClickEvent event) { |
131 | 023f6f1e | Panagiotis Astithas | prepareAndSubmit(); |
132 | 023f6f1e | Panagiotis Astithas | } |
133 | 023f6f1e | Panagiotis Astithas | }); |
134 | 023f6f1e | Panagiotis Astithas | submit.setEnabled(false);
|
135 | 023f6f1e | Panagiotis Astithas | buttons.add(submit); |
136 | 023f6f1e | Panagiotis Astithas | buttons.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_CENTER); |
137 | 023f6f1e | Panagiotis Astithas | // Create the 'Cancel' button, along with a listener that hides the
|
138 | 023f6f1e | Panagiotis Astithas | // dialog when the button is clicked.
|
139 | 023f6f1e | Panagiotis Astithas | Button cancel = new Button("Cancel", new ClickHandler() { |
140 | 023f6f1e | Panagiotis Astithas | @Override
|
141 | 023f6f1e | Panagiotis Astithas | public void onClick(ClickEvent event) { |
142 | e0c0ebc0 | Natasa Kapravelou | canContinue = false;
|
143 | 023f6f1e | Panagiotis Astithas | cancelUpload(); |
144 | 023f6f1e | Panagiotis Astithas | } |
145 | 023f6f1e | Panagiotis Astithas | }); |
146 | 023f6f1e | Panagiotis Astithas | buttons.add(cancel); |
147 | 023f6f1e | Panagiotis Astithas | buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER); |
148 | 023f6f1e | Panagiotis Astithas | buttons.setSpacing(8);
|
149 | 023f6f1e | Panagiotis Astithas | buttons.addStyleName("gss-DialogBox");
|
150 | 023f6f1e | Panagiotis Astithas | |
151 | 023f6f1e | Panagiotis Astithas | browse.addClickHandler(new ClickHandler() {
|
152 | 023f6f1e | Panagiotis Astithas | @Override
|
153 | 023f6f1e | Panagiotis Astithas | public void onClick(ClickEvent event) { |
154 | 023f6f1e | Panagiotis Astithas | Desktop desktop = factory.createDesktop(); |
155 | 023f6f1e | Panagiotis Astithas | desktop.openFiles(new OpenFilesHandler() {
|
156 | 023f6f1e | Panagiotis Astithas | |
157 | 023f6f1e | Panagiotis Astithas | @Override
|
158 | 023f6f1e | Panagiotis Astithas | public void onOpenFiles(OpenFilesEvent ofevent) { |
159 | 023f6f1e | Panagiotis Astithas | fileObjects = ofevent.getFiles(); |
160 | 023f6f1e | Panagiotis Astithas | selectedFiles.addAll(Arrays.asList(fileObjects));
|
161 | 023f6f1e | Panagiotis Astithas | for (int i = 0; i< selectedFiles.size(); i++) { |
162 | 023f6f1e | Panagiotis Astithas | generalTable.setText(i+1, 0, "File"); |
163 | 023f6f1e | Panagiotis Astithas | generalTable.setText(i+1, 1, selectedFiles.get(i).getName()); |
164 | 023f6f1e | Panagiotis Astithas | ProgressBar progress = new ProgressBar(20, 0); |
165 | 023f6f1e | Panagiotis Astithas | generalTable.setWidget(i+1, 2, progress); |
166 | 023f6f1e | Panagiotis Astithas | progressBars.add(progress); |
167 | 023f6f1e | Panagiotis Astithas | generalTable.getCellFormatter().setStyleName(i+1, 0, "props-labels"); |
168 | 023f6f1e | Panagiotis Astithas | generalTable.getCellFormatter().setStyleName(i+1, 1, "props-values"); |
169 | 023f6f1e | Panagiotis Astithas | } |
170 | 023f6f1e | Panagiotis Astithas | submit.setEnabled(true);
|
171 | 023f6f1e | Panagiotis Astithas | } |
172 | 023f6f1e | Panagiotis Astithas | }); |
173 | 023f6f1e | Panagiotis Astithas | } |
174 | 023f6f1e | Panagiotis Astithas | }); |
175 | 023f6f1e | Panagiotis Astithas | |
176 | 023f6f1e | Panagiotis Astithas | panel.add(buttons); |
177 | 023f6f1e | Panagiotis Astithas | panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER); |
178 | 023f6f1e | Panagiotis Astithas | panel.addStyleName("gss-DialogBox");
|
179 | 023f6f1e | Panagiotis Astithas | addStyleName("gss-DialogBox");
|
180 | 023f6f1e | Panagiotis Astithas | setWidget(panel); |
181 | 023f6f1e | Panagiotis Astithas | } |
182 | 023f6f1e | Panagiotis Astithas | |
183 | 023f6f1e | Panagiotis Astithas | /**
|
184 | 023f6f1e | Panagiotis Astithas | * Cancels the file upload.
|
185 | 023f6f1e | Panagiotis Astithas | */
|
186 | 023f6f1e | Panagiotis Astithas | private void cancelUpload() { |
187 | 023f6f1e | Panagiotis Astithas | for (HttpRequest request: requests)
|
188 | 023f6f1e | Panagiotis Astithas | request.abort(); |
189 | 023f6f1e | Panagiotis Astithas | hide(); |
190 | 023f6f1e | Panagiotis Astithas | } |
191 | 023f6f1e | Panagiotis Astithas | |
192 | 023f6f1e | Panagiotis Astithas | /**
|
193 | 023f6f1e | Panagiotis Astithas | * Check whether the specified file name exists in the selected folder.
|
194 | 023f6f1e | Panagiotis Astithas | */
|
195 | 023f6f1e | Panagiotis Astithas | private boolean canContinue(File file) { |
196 | 023f6f1e | Panagiotis Astithas | String fileName = getFilename(file.getName());
|
197 | 023f6f1e | Panagiotis Astithas | if (getFileForName(fileName) == null) |
198 | 023f6f1e | Panagiotis Astithas | // For file creation, check to see if the file already exists.
|
199 | 023f6f1e | Panagiotis Astithas | for (FileResource fileRes : files)
|
200 | 023f6f1e | Panagiotis Astithas | if (!fileRes.isDeleted() && fileRes.getName().equals(fileName))
|
201 | 023f6f1e | Panagiotis Astithas | return false; |
202 | 023f6f1e | Panagiotis Astithas | return true; |
203 | 023f6f1e | Panagiotis Astithas | } |
204 | 023f6f1e | Panagiotis Astithas | |
205 | 023f6f1e | Panagiotis Astithas | @Override
|
206 | 023f6f1e | Panagiotis Astithas | public void prepareAndSubmit() { |
207 | 023f6f1e | Panagiotis Astithas | GSS app = GSS.get(); |
208 | 023f6f1e | Panagiotis Astithas | if (selectedFiles.size() == 0) { |
209 | 023f6f1e | Panagiotis Astithas | app.displayError("You must select a file!");
|
210 | 023f6f1e | Panagiotis Astithas | hide(); |
211 | 023f6f1e | Panagiotis Astithas | return;
|
212 | 023f6f1e | Panagiotis Astithas | } |
213 | 023f6f1e | Panagiotis Astithas | for (File file: selectedFiles) |
214 | 023f6f1e | Panagiotis Astithas | if (!canContinue(file)) {
|
215 | 023f6f1e | Panagiotis Astithas | app.displayError("The file name " + file.getName() +
|
216 | 023f6f1e | Panagiotis Astithas | " already exists in this folder");
|
217 | 023f6f1e | Panagiotis Astithas | hide(); |
218 | 023f6f1e | Panagiotis Astithas | return;
|
219 | 023f6f1e | Panagiotis Astithas | } |
220 | 023f6f1e | Panagiotis Astithas | submit.setEnabled(false);
|
221 | 023f6f1e | Panagiotis Astithas | browse.setVisible(false);
|
222 | 023f6f1e | Panagiotis Astithas | List<String> toUpdate = new ArrayList<String>(); |
223 | 023f6f1e | Panagiotis Astithas | toRename = new HashMap<String, FileResource>(); |
224 | 023f6f1e | Panagiotis Astithas | for (File file: selectedFiles) { |
225 | 023f6f1e | Panagiotis Astithas | String fname = getFilename(file.getName());
|
226 | 023f6f1e | Panagiotis Astithas | if (getFileForName(fname) == null) { |
227 | 023f6f1e | Panagiotis Astithas | // We are going to create a file, so we check to see if there is a
|
228 | 023f6f1e | Panagiotis Astithas | // trashed file with the same name.
|
229 | 023f6f1e | Panagiotis Astithas | FileResource same = null;
|
230 | 023f6f1e | Panagiotis Astithas | for (FileResource fres : folder.getFiles())
|
231 | 023f6f1e | Panagiotis Astithas | if (fres.isDeleted() && fres.getName().equals(fname))
|
232 | 023f6f1e | Panagiotis Astithas | same = fres; |
233 | 023f6f1e | Panagiotis Astithas | // In that case add it to the list of files to rename.
|
234 | 023f6f1e | Panagiotis Astithas | if (same != null) |
235 | 023f6f1e | Panagiotis Astithas | toRename.put(getBackupFilename(fname), same); |
236 | 023f6f1e | Panagiotis Astithas | } else
|
237 | 023f6f1e | Panagiotis Astithas | // If we are updating a file add it to the list of files to update.
|
238 | 023f6f1e | Panagiotis Astithas | toUpdate.add(fname); |
239 | 023f6f1e | Panagiotis Astithas | } |
240 | 023f6f1e | Panagiotis Astithas | |
241 | 023f6f1e | Panagiotis Astithas | if (!toUpdate.isEmpty()) {
|
242 | 023f6f1e | Panagiotis Astithas | StringBuffer sb = new StringBuffer(); |
243 | 023f6f1e | Panagiotis Astithas | for (String name: toUpdate) |
244 | 023f6f1e | Panagiotis Astithas | sb.append(name).append("<br/>");
|
245 | 023f6f1e | Panagiotis Astithas | // We are going to update existing files, so show a confirmation dialog.
|
246 | 023f6f1e | Panagiotis Astithas | ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " + |
247 | 023f6f1e | Panagiotis Astithas | "you want to update the following files?<br/><i>" + sb +
|
248 | 023f6f1e | Panagiotis Astithas | "</i>", "Update") { |
249 | 023f6f1e | Panagiotis Astithas | |
250 | 023f6f1e | Panagiotis Astithas | @Override
|
251 | 023f6f1e | Panagiotis Astithas | public void cancel() { |
252 | 023f6f1e | Panagiotis Astithas | hide(); |
253 | 023f6f1e | Panagiotis Astithas | } |
254 | 023f6f1e | Panagiotis Astithas | |
255 | 023f6f1e | Panagiotis Astithas | @Override
|
256 | 023f6f1e | Panagiotis Astithas | public void confirm() { |
257 | 023f6f1e | Panagiotis Astithas | confirmRename(); |
258 | 023f6f1e | Panagiotis Astithas | } |
259 | 023f6f1e | Panagiotis Astithas | |
260 | 023f6f1e | Panagiotis Astithas | }; |
261 | 023f6f1e | Panagiotis Astithas | confirm.center(); |
262 | 023f6f1e | Panagiotis Astithas | } else
|
263 | 023f6f1e | Panagiotis Astithas | confirmRename(); |
264 | 023f6f1e | Panagiotis Astithas | } |
265 | 023f6f1e | Panagiotis Astithas | |
266 | 023f6f1e | Panagiotis Astithas | /**
|
267 | 023f6f1e | Panagiotis Astithas | * Confirm the renames of synonymous files already in the trash.
|
268 | 023f6f1e | Panagiotis Astithas | */
|
269 | 023f6f1e | Panagiotis Astithas | private void confirmRename() { |
270 | 023f6f1e | Panagiotis Astithas | if (!toRename.isEmpty()) {
|
271 | 023f6f1e | Panagiotis Astithas | StringBuffer sb = new StringBuffer(); |
272 | 023f6f1e | Panagiotis Astithas | for (FileResource file: toRename.values())
|
273 | 023f6f1e | Panagiotis Astithas | sb.append(file.getName()).append("<br/>");
|
274 | 023f6f1e | Panagiotis Astithas | ConfirmationDialog confirm = new ConfirmationDialog("Files " + |
275 | 023f6f1e | Panagiotis Astithas | "with the following names already exist in the trash. If" +
|
276 | 023f6f1e | Panagiotis Astithas | " you continue,<br/>the trashed files will be renamed " +
|
277 | 023f6f1e | Panagiotis Astithas | "automatically for you:<br/><i>" + sb + "</i>", "Continue") { |
278 | 023f6f1e | Panagiotis Astithas | |
279 | 023f6f1e | Panagiotis Astithas | @Override
|
280 | 023f6f1e | Panagiotis Astithas | public void cancel() { |
281 | 023f6f1e | Panagiotis Astithas | hide(); |
282 | 023f6f1e | Panagiotis Astithas | } |
283 | 023f6f1e | Panagiotis Astithas | |
284 | 023f6f1e | Panagiotis Astithas | @Override
|
285 | 023f6f1e | Panagiotis Astithas | public void confirm() { |
286 | 023f6f1e | Panagiotis Astithas | updateTrashedFiles(); |
287 | 023f6f1e | Panagiotis Astithas | } |
288 | 023f6f1e | Panagiotis Astithas | |
289 | 023f6f1e | Panagiotis Astithas | }; |
290 | 023f6f1e | Panagiotis Astithas | confirm.center(); |
291 | 023f6f1e | Panagiotis Astithas | } else
|
292 | 023f6f1e | Panagiotis Astithas | uploadFiles(); |
293 | 023f6f1e | Panagiotis Astithas | } |
294 | 023f6f1e | Panagiotis Astithas | |
295 | 023f6f1e | Panagiotis Astithas | /**
|
296 | 023f6f1e | Panagiotis Astithas | * Rename the conflicting trashed files with the supplied new names.
|
297 | 023f6f1e | Panagiotis Astithas | */
|
298 | 023f6f1e | Panagiotis Astithas | private void updateTrashedFiles() { |
299 | 023f6f1e | Panagiotis Astithas | for (final String name: toRename.keySet()) { |
300 | 023f6f1e | Panagiotis Astithas | JSONObject json = new JSONObject();
|
301 | 023f6f1e | Panagiotis Astithas | json.put("name", new JSONString(name)); |
302 | 023f6f1e | Panagiotis Astithas | PostCommand cf = new PostCommand(toRename.get(name).getUri() + "?update=", json.toString(), 200) { |
303 | 023f6f1e | Panagiotis Astithas | |
304 | 023f6f1e | Panagiotis Astithas | @Override
|
305 | 023f6f1e | Panagiotis Astithas | public void onComplete() { |
306 | 023f6f1e | Panagiotis Astithas | toRename.remove(name); |
307 | 023f6f1e | Panagiotis Astithas | uploadFiles(); |
308 | 023f6f1e | Panagiotis Astithas | } |
309 | 023f6f1e | Panagiotis Astithas | |
310 | 023f6f1e | Panagiotis Astithas | @Override
|
311 | 023f6f1e | Panagiotis Astithas | public void onError(Throwable t) { |
312 | 023f6f1e | Panagiotis Astithas | GSS app = GSS.get(); |
313 | 023f6f1e | Panagiotis Astithas | GWT.log("", t);
|
314 | 023f6f1e | Panagiotis Astithas | if (t instanceof RestException) { |
315 | 023f6f1e | Panagiotis Astithas | int statusCode = ((RestException) t).getHttpStatusCode();
|
316 | 023f6f1e | Panagiotis Astithas | if (statusCode == 405) |
317 | 023f6f1e | Panagiotis Astithas | app.displayError("You don't have the necessary permissions");
|
318 | 023f6f1e | Panagiotis Astithas | else if (statusCode == 404) |
319 | 023f6f1e | Panagiotis Astithas | app.displayError("User in permissions does not exist");
|
320 | 023f6f1e | Panagiotis Astithas | else if (statusCode == 409) |
321 | 023f6f1e | Panagiotis Astithas | app.displayError("A file with the same name already exists");
|
322 | 023f6f1e | Panagiotis Astithas | else if (statusCode == 413) |
323 | 023f6f1e | Panagiotis Astithas | app.displayError("Your quota has been exceeded");
|
324 | 023f6f1e | Panagiotis Astithas | else
|
325 | 023f6f1e | Panagiotis Astithas | app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
|
326 | 023f6f1e | Panagiotis Astithas | } else
|
327 | 023f6f1e | Panagiotis Astithas | app.displayError("System error modifying file:" + t.getMessage());
|
328 | 023f6f1e | Panagiotis Astithas | } |
329 | 023f6f1e | Panagiotis Astithas | |
330 | 023f6f1e | Panagiotis Astithas | }; |
331 | 023f6f1e | Panagiotis Astithas | DeferredCommand.addCommand(cf); |
332 | 023f6f1e | Panagiotis Astithas | } |
333 | 023f6f1e | Panagiotis Astithas | } |
334 | 023f6f1e | Panagiotis Astithas | |
335 | 023f6f1e | Panagiotis Astithas | /**
|
336 | 023f6f1e | Panagiotis Astithas | * Checks if the renaming step for already trashed files is complete and
|
337 | 023f6f1e | Panagiotis Astithas | * starts file uploads.
|
338 | 023f6f1e | Panagiotis Astithas | */
|
339 | e0c0ebc0 | Natasa Kapravelou | private void uploadFiles() { |
340 | 023f6f1e | Panagiotis Astithas | if (!toRename.isEmpty()) return; |
341 | e0c0ebc0 | Natasa Kapravelou | if (canContinue){
|
342 | e0c0ebc0 | Natasa Kapravelou | doSend(selectedFiles); |
343 | 023f6f1e | Panagiotis Astithas | } |
344 | 023f6f1e | Panagiotis Astithas | } |
345 | 023f6f1e | Panagiotis Astithas | |
346 | 023f6f1e | Panagiotis Astithas | /**
|
347 | 023f6f1e | Panagiotis Astithas | * Perform the HTTP request to upload the specified file.
|
348 | 023f6f1e | Panagiotis Astithas | */
|
349 | e0c0ebc0 | Natasa Kapravelou | protected void doSend(final List<File> filesRemaining) { |
350 | 023f6f1e | Panagiotis Astithas | final GSS app = GSS.get();
|
351 | 023f6f1e | Panagiotis Astithas | HttpRequest request = factory.createHttpRequest(); |
352 | 023f6f1e | Panagiotis Astithas | requests.add(request); |
353 | 023f6f1e | Panagiotis Astithas | String method = "PUT"; |
354 | 023f6f1e | Panagiotis Astithas | |
355 | 023f6f1e | Panagiotis Astithas | String path;
|
356 | e0c0ebc0 | Natasa Kapravelou | final String filename = getFilename(filesRemaining.get(0).getName()); |
357 | 023f6f1e | Panagiotis Astithas | path = folder.getUri(); |
358 | 023f6f1e | Panagiotis Astithas | if (!path.endsWith("/")) |
359 | 023f6f1e | Panagiotis Astithas | path = path + "/";
|
360 | 023f6f1e | Panagiotis Astithas | path = path + encode(filename); |
361 | 023f6f1e | Panagiotis Astithas | |
362 | 023f6f1e | Panagiotis Astithas | String token = app.getToken();
|
363 | 023f6f1e | Panagiotis Astithas | String resource = path.substring(app.getApiPath().length()-1, path.length()); |
364 | 023f6f1e | Panagiotis Astithas | String date = RestCommand.getDate();
|
365 | 023f6f1e | Panagiotis Astithas | String sig = RestCommand.calculateSig(method, date, resource, RestCommand.base64decode(token));
|
366 | 023f6f1e | Panagiotis Astithas | request.open(method, path); |
367 | 023f6f1e | Panagiotis Astithas | request.setRequestHeader("X-GSS-Date", date);
|
368 | 023f6f1e | Panagiotis Astithas | request.setRequestHeader("Authorization", app.getCurrentUserResource().getUsername() + " " + sig); |
369 | 023f6f1e | Panagiotis Astithas | request.setRequestHeader("Accept", "application/json; charset=utf-8"); |
370 | 023f6f1e | Panagiotis Astithas | request.setCallback(new RequestCallback() {
|
371 | 023f6f1e | Panagiotis Astithas | @Override
|
372 | 023f6f1e | Panagiotis Astithas | public void onResponseReceived(HttpRequest req) { |
373 | 023f6f1e | Panagiotis Astithas | int state = req.getReadyState();
|
374 | 023f6f1e | Panagiotis Astithas | if (state != 4) return; |
375 | 023f6f1e | Panagiotis Astithas | switch(req.getStatus()) {
|
376 | 023f6f1e | Panagiotis Astithas | case 201: // Created falls through to updated. |
377 | 023f6f1e | Panagiotis Astithas | case 204: |
378 | e0c0ebc0 | Natasa Kapravelou | filesRemaining.remove(0);
|
379 | 5210ea4c | Natasa Kapravelou | if(filesRemaining.isEmpty()){
|
380 | e0c0ebc0 | Natasa Kapravelou | finish(); |
381 | e0c0ebc0 | Natasa Kapravelou | break;
|
382 | 4cef6f04 | Natasa Kapravelou | } |
383 | e0c0ebc0 | Natasa Kapravelou | doSend(filesRemaining); |
384 | 023f6f1e | Panagiotis Astithas | break;
|
385 | 023f6f1e | Panagiotis Astithas | case 403: |
386 | 023f6f1e | Panagiotis Astithas | SessionExpiredDialog dlg = new SessionExpiredDialog();
|
387 | 023f6f1e | Panagiotis Astithas | dlg.center(); |
388 | 023f6f1e | Panagiotis Astithas | break;
|
389 | 023f6f1e | Panagiotis Astithas | case 405: |
390 | 023f6f1e | Panagiotis Astithas | app.displayError("You don't have permission to " +
|
391 | 023f6f1e | Panagiotis Astithas | "upload file " + filename);
|
392 | 023f6f1e | Panagiotis Astithas | break;
|
393 | 023f6f1e | Panagiotis Astithas | case 409: |
394 | 023f6f1e | Panagiotis Astithas | app.displayError("A folder with the name " + filename +
|
395 | 023f6f1e | Panagiotis Astithas | " already exists at this level");
|
396 | 023f6f1e | Panagiotis Astithas | break;
|
397 | 023f6f1e | Panagiotis Astithas | case 413: |
398 | 023f6f1e | Panagiotis Astithas | app.displayError("There is not enough free space " +
|
399 | 023f6f1e | Panagiotis Astithas | "available for uploading " + filename);
|
400 | 023f6f1e | Panagiotis Astithas | break;
|
401 | 023f6f1e | Panagiotis Astithas | default:
|
402 | 023f6f1e | Panagiotis Astithas | app.displayError("Error uploading file " + filename +
|
403 | 023f6f1e | Panagiotis Astithas | ": " + req.getStatus());
|
404 | 023f6f1e | Panagiotis Astithas | } |
405 | 023f6f1e | Panagiotis Astithas | } |
406 | 023f6f1e | Panagiotis Astithas | }); |
407 | 023f6f1e | Panagiotis Astithas | request.getUpload().setProgressHandler(new ProgressHandler() {
|
408 | 023f6f1e | Panagiotis Astithas | @Override
|
409 | 023f6f1e | Panagiotis Astithas | public void onProgress(ProgressEvent event) { |
410 | 023f6f1e | Panagiotis Astithas | double pcnt = (double) event.getLoaded() / event.getTotal(); |
411 | e0c0ebc0 | Natasa Kapravelou | progressBars.get(0).setProgress((int) Math.floor(pcnt * 100)); |
412 | e0c0ebc0 | Natasa Kapravelou | if(pcnt*100 == 100) |
413 | e0c0ebc0 | Natasa Kapravelou | progressBars.remove(0);
|
414 | 023f6f1e | Panagiotis Astithas | } |
415 | 023f6f1e | Panagiotis Astithas | }); |
416 | e0c0ebc0 | Natasa Kapravelou | request.send(filesRemaining.get(0).getBlob());
|
417 | 023f6f1e | Panagiotis Astithas | } |
418 | 023f6f1e | Panagiotis Astithas | |
419 | 023f6f1e | Panagiotis Astithas | /**
|
420 | 023f6f1e | Panagiotis Astithas | * Perform the final actions after the files are uploaded.
|
421 | 023f6f1e | Panagiotis Astithas | */
|
422 | 023f6f1e | Panagiotis Astithas | protected void finish() { |
423 | 023f6f1e | Panagiotis Astithas | hide(); |
424 | 023f6f1e | Panagiotis Astithas | GSS.get().showFileList(true);
|
425 | 023f6f1e | Panagiotis Astithas | GSS.get().getStatusPanel().updateStats(); |
426 | 023f6f1e | Panagiotis Astithas | } |
427 | 023f6f1e | Panagiotis Astithas | |
428 | 023f6f1e | Panagiotis Astithas | /**
|
429 | 023f6f1e | Panagiotis Astithas | * Same as URL.encode, but also encode apostrophe since browsers aren't
|
430 | 023f6f1e | Panagiotis Astithas | * consistent about it (FF encodes, IE does not).
|
431 | 023f6f1e | Panagiotis Astithas | */
|
432 | 023f6f1e | Panagiotis Astithas | protected String encode(String decodedURL) { |
433 | 023f6f1e | Panagiotis Astithas | String retv = decodedURL.replaceAll("@", "_"); // Replace bad character |
434 | 023f6f1e | Panagiotis Astithas | retv = URL.encodeComponent(retv);
|
435 | 023f6f1e | Panagiotis Astithas | retv = retv.replaceAll("'", "%27"); |
436 | 023f6f1e | Panagiotis Astithas | return retv;
|
437 | 023f6f1e | Panagiotis Astithas | } |
438 | 023f6f1e | Panagiotis Astithas | |
439 | 023f6f1e | Panagiotis Astithas | } |