Changeset 13488 in josm
- Timestamp:
- 2018-03-03T19:45:55+01:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/OsmApi.java
r13207 r13488 17 17 import java.nio.charset.StandardCharsets; 18 18 import java.util.Collection; 19 import java.util.Collections;20 19 import java.util.HashMap; 21 20 import java.util.List; 22 21 import java.util.Map; 22 import java.util.function.Consumer; 23 import java.util.function.Function; 23 24 24 25 import javax.xml.parsers.ParserConfigurationException; … … 361 362 } 362 363 363 /** 364 * Creates an OSM primitive on the server. The OsmPrimitive object passed in 365 * is modified by giving it the server-assigned id. 366 * 367 * @param osm the primitive 368 * @param monitor the progress monitor 369 * @throws OsmTransferException if something goes wrong 370 */ 371 public void createPrimitive(IPrimitive osm, ProgressMonitor monitor) throws OsmTransferException { 364 private void individualPrimitiveModification(String method, String verb, IPrimitive osm, ProgressMonitor monitor, 365 Consumer<String> consumer, Function<String, String> errHandler) throws OsmTransferException { 372 366 String ret = ""; 373 367 try { 374 368 ensureValidChangeset(); 375 369 initialize(monitor); 376 ret = sendRequest("PUT", OsmPrimitiveType.from(osm).getAPIName()+"/create", toXml(osm, true), monitor); 370 // Perform request 371 ret = sendRequest(method, OsmPrimitiveType.from(osm).getAPIName() + '/' + verb, toXml(osm, true), monitor); 372 // Unlock dataset if needed 373 boolean locked = false; 374 if (osm instanceof OsmPrimitive) { 375 locked = ((OsmPrimitive) osm).getDataSet().isLocked(); 376 if (locked) { 377 ((OsmPrimitive) osm).getDataSet().unlock(); 378 } 379 } 380 try { 381 // Update local primitive 382 consumer.accept(ret); 383 } finally { 384 // Lock dataset back if needed 385 if (locked) { 386 ((OsmPrimitive) osm).getDataSet().lock(); 387 } 388 } 389 } catch (NumberFormatException e) { 390 throw new OsmTransferException(errHandler.apply(ret), e); 391 } 392 } 393 394 /** 395 * Creates an OSM primitive on the server. The OsmPrimitive object passed in 396 * is modified by giving it the server-assigned id. 397 * 398 * @param osm the primitive 399 * @param monitor the progress monitor 400 * @throws OsmTransferException if something goes wrong 401 */ 402 public void createPrimitive(IPrimitive osm, ProgressMonitor monitor) throws OsmTransferException { 403 individualPrimitiveModification("PUT", "create", osm, monitor, ret -> { 377 404 osm.setOsmId(Long.parseLong(ret.trim()), 1); 378 405 osm.setChangesetId(getChangeset().getId()); 379 } catch (NumberFormatException e) { 380 throw new OsmTransferException(tr("Unexpected format of ID replied by the server. Got ''{0}''.", ret), e); 381 } 406 }, ret -> tr("Unexpected format of ID replied by the server. Got ''{0}''.", ret)); 382 407 } 383 408 … … 390 415 */ 391 416 public void modifyPrimitive(IPrimitive osm, ProgressMonitor monitor) throws OsmTransferException { 392 String ret = null; 393 try { 394 ensureValidChangeset(); 395 initialize(monitor); 396 // normal mode (0.6 and up) returns new object version. 397 ret = sendRequest("PUT", OsmPrimitiveType.from(osm).getAPIName()+'/' + osm.getId(), toXml(osm, true), monitor); 417 individualPrimitiveModification("PUT", Long.toString(osm.getId()), osm, monitor, ret -> { 418 // API returns new object version 398 419 osm.setOsmId(osm.getId(), Integer.parseInt(ret.trim())); 399 420 osm.setChangesetId(getChangeset().getId()); 400 421 osm.setVisible(true); 401 } catch (NumberFormatException e) { 402 throw new OsmTransferException(tr("Unexpected format of new version of modified primitive ''{0}''. Got ''{1}''.", 403 osm.getId(), ret), e); 404 } 422 }, ret -> tr("Unexpected format of new version of modified primitive ''{0}''. Got ''{1}''.", osm.getId(), ret)); 405 423 } 406 424 407 425 /** 408 426 * Deletes an OSM primitive on the server. 427 * 409 428 * @param osm the primitive 410 429 * @param monitor the progress monitor … … 412 431 */ 413 432 public void deletePrimitive(OsmPrimitive osm, ProgressMonitor monitor) throws OsmTransferException { 414 ensureValidChangeset(); 415 initialize(monitor); 416 // can't use a the individual DELETE method in the 0.6 API. Java doesn't allow 417 // submitting a DELETE request with content, the 0.6 API requires it, however. Falling back 418 // to diff upload. 419 // 420 uploadDiff(Collections.singleton(osm), monitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false)); 433 individualPrimitiveModification("DELETE", Long.toString(osm.getId()), osm, monitor, ret -> { 434 // API returns new object version 435 osm.setOsmId(osm.getId(), Integer.parseInt(ret.trim())); 436 osm.setChangesetId(getChangeset().getId()); 437 osm.setVisible(false); 438 }, ret -> tr("Unexpected format of new version of deleted primitive ''{0}''. Got ''{1}''.", osm.getId(), ret)); 421 439 } 422 440 423 441 /** 424 442 * Creates a new changeset based on the keys in <code>changeset</code>. If this 425 * method succeeds, changeset.getId() replies the id the server assigned to the new 426 * changeset 443 * method succeeds, changeset.getId() replies the id the server assigned to the new changeset 427 444 * 428 445 * The changeset must not be null, but its key/value-pairs may be empty.
Note:
See TracChangeset
for help on using the changeset viewer.