| 301 | | ArrayList<OsmPrimitive> processed = new ArrayList<OsmPrimitive>(); |
| 302 | | |
| 303 | | for (OsmPrimitive osm : list) { |
| 304 | | int progress = Main.pleaseWaitDlg.progress.getValue(); |
| 305 | | Main.pleaseWaitDlg.currentAction.setText(tr("Preparing...")); |
| 306 | | if (cancel) throw new OsmTransferCancelledException(); |
| 307 | | osm.visit(duv); |
| 308 | | Main.pleaseWaitDlg.progress.setValue(progress+1); |
| | 304 | final ArrayList<OsmPrimitive> processed = new ArrayList<OsmPrimitive>(); |
| | 305 | |
| | 306 | // this is the asynchronous update task |
| | 307 | // |
| | 308 | class UploadDiffTask extends PleaseWaitRunnable { |
| | 309 | |
| | 310 | private boolean uploadCancelled = false; |
| | 311 | private boolean uploadFailed = false; |
| | 312 | private Throwable lastThrowable = null; |
| | 313 | |
| | 314 | public UploadDiffTask(String title) { |
| | 315 | super(title,false /* don't ignore exceptions */); |
| | 316 | } |
| | 317 | |
| | 318 | @Override protected void realRun() throws SAXException, IOException { |
| | 319 | CreateOsmChangeVisitor duv = new CreateOsmChangeVisitor(changeset, OsmApi.this); |
| | 320 | |
| | 321 | for (OsmPrimitive osm : list) { |
| | 322 | int progress = Main.pleaseWaitDlg.progress.getValue(); |
| | 323 | Main.pleaseWaitDlg.currentAction.setText(tr("Preparing...")); |
| | 324 | osm.visit(duv); |
| | 325 | Main.pleaseWaitDlg.progress.setValue(progress+1); |
| | 326 | } |
| | 327 | |
| | 328 | Main.pleaseWaitDlg.currentAction.setText(tr("Uploading...")); |
| | 329 | |
| | 330 | String diff = duv.getDocument(); |
| | 331 | try { |
| | 332 | String diffresult = sendRequest("POST", "changeset/" + changeset.id + "/upload", diff); |
| | 333 | DiffResultReader.parseDiffResult(diffresult, list, processed, duv.getNewIdMap(), Main.pleaseWaitDlg); |
| | 334 | } catch (Exception sxe) { |
| | 335 | if (isUploadCancelled()) { |
| | 336 | // ignore exceptions thrown because the connection is aborted, |
| | 337 | // i.e. IOExceptions or SocketExceptions |
| | 338 | // |
| | 339 | System.out.println("Ignoring exception caught because upload is cancelled. Exception is: " + sxe.toString()); |
| | 340 | return; |
| | 341 | } |
| | 342 | uploadFailed = true; |
| | 343 | // remember last exception and don't throw it. If it was thrown again it would |
| | 344 | // have to be encapsulated in a RuntimeException which would be nested in yet |
| | 345 | // another RuntimeException by parent classes. |
| | 346 | // Rather check isUploadFailed() and retrieve getLastThrowable() after the task |
| | 347 | // is completed |
| | 348 | // |
| | 349 | lastThrowable = sxe; |
| | 350 | } |
| | 351 | } |
| | 352 | |
| | 353 | @Override protected void finish() { |
| | 354 | // do nothing |
| | 355 | } |
| | 356 | |
| | 357 | @Override protected void cancel() { |
| | 358 | activeConnection.disconnect(); |
| | 359 | uploadCancelled = true; |
| | 360 | } |
| | 361 | |
| | 362 | public boolean isUploadCancelled() { |
| | 363 | return uploadCancelled; |
| | 364 | } |
| | 365 | |
| | 366 | public boolean isUploadFailed() { |
| | 367 | return uploadFailed; |
| | 368 | } |
| | 369 | |
| | 370 | public Throwable getLastThrowable() { |
| | 371 | return lastThrowable; |
| | 372 | } |
| 310 | | |
| 311 | | Main.pleaseWaitDlg.currentAction.setText(tr("Uploading...")); |
| 312 | | if (cancel) throw new OsmTransferCancelledException(); |
| 313 | | |
| 314 | | String diff = duv.getDocument(); |
| 315 | | String diffresult = sendRequest("POST", "changeset/" + changeset.id + "/upload", diff); |
| 316 | | try { |
| 317 | | DiffResultReader.parseDiffResult(diffresult, list, processed, duv.getNewIdMap(), Main.pleaseWaitDlg); |
| 318 | | } catch (Exception sxe) { |
| 319 | | throw new OsmTransferException(tr("Error processing changeset upload response"), sxe); |
| | 374 | |
| | 375 | UploadDiffTask uploadTask = new UploadDiffTask(tr("Uploading data")); |
| | 376 | |
| | 377 | // run data upload as asynchronous task |
| | 378 | // |
| | 379 | try { |
| | 380 | Void result = null; |
| | 381 | FutureTask<Void> task = new FutureTask<Void>(uploadTask, result); |
| | 382 | task.run(); |
| | 383 | task.get(); // wait for the task to complete, no return value expected, though |
| | 384 | } catch(Throwable e) { |
| | 385 | if (uploadTask.isUploadCancelled()) { |
| | 386 | throw new OsmTransferCancelledException(); |
| | 387 | } |
| | 388 | throw new OsmTransferException(e); |
| | 389 | } |
| | 390 | |
| | 391 | // handle failed upload |
| | 392 | // |
| | 393 | if (uploadTask.isUploadFailed()) { |
| | 394 | if (uploadTask.getLastThrowable() != null && uploadTask.getLastThrowable() instanceof OsmTransferException) { |
| | 395 | OsmTransferException e = (OsmTransferException)uploadTask.getLastThrowable(); |
| | 396 | throw e; |
| | 397 | } |
| | 398 | // shouldn't happen, but just in case |
| | 399 | // |
| | 400 | throw new OsmTransferException("Data upload failed for an unknown reason"); |