Search in sources :

Example 1 with SyncUploadRetrofitErrorEvent

use of io.jawg.osmcontributor.rest.events.error.SyncUploadRetrofitErrorEvent in project osm-contributor by jawg.

the class SyncManager method remoteUpdatePoi.

/**
 * Update a Poi of the backend.
 *
 * @param poi         The Poi to update.
 * @param changeSetId The changeSet in which the Poi is sent.
 * @return Whether the update was a success or not.
 */
private boolean remoteUpdatePoi(final Poi poi, String changeSetId) {
    Backend.UpdateResult updateResult = backend.updatePoi(poi, changeSetId);
    poiManager.deleteOldPoiAssociated(poi);
    switch(updateResult.getStatus()) {
        case SUCCESS:
            poi.setVersion(updateResult.getVersion());
            poi.setUpdated(false);
            poiManager.savePoi(poi);
            OsmAnswers.remotePoiAction(poi.getType().getTechnicalName(), "update");
            return true;
        case FAILURE_CONFLICT:
            bus.post(new SyncConflictingNodeErrorEvent(poi.getName(), poi.getId()));
            Timber.e("Couldn't update poi %s: conflict, redownloading last version of poi", poi);
            deleteAndRetrieveUnmodifiedPoi(poi);
            return false;
        case FAILURE_NOT_EXISTING:
            Timber.e("Couldn't update poi %s, it didn't exist. Deleting the incriminated poi", poi);
            poiManager.deletePoi(poi);
            bus.post(new SyncConflictingNodeErrorEvent(poi.getName(), poi.getId()));
            return false;
        case FAILURE_UNKNOWN:
        default:
            Timber.e("Couldn't update poi %s. Deleting the incriminated poi", poi);
            poiManager.deletePoi(poi);
            bus.post(new SyncUploadRetrofitErrorEvent(poi.getId()));
            return false;
    }
}
Also used : Backend(io.jawg.osmcontributor.rest.Backend) SyncConflictingNodeErrorEvent(io.jawg.osmcontributor.rest.events.error.SyncConflictingNodeErrorEvent) SyncUploadRetrofitErrorEvent(io.jawg.osmcontributor.rest.events.error.SyncUploadRetrofitErrorEvent)

Example 2 with SyncUploadRetrofitErrorEvent

use of io.jawg.osmcontributor.rest.events.error.SyncUploadRetrofitErrorEvent in project osm-contributor by jawg.

the class OsmBackend method initializeTransaction.

/**
 * {@inheritDoc}
 */
@Override
public String initializeTransaction(String comment) {
    final OsmDto osmDto = new OsmDto();
    ChangeSetDto changeSetDto = new ChangeSetDto();
    List<TagDto> tagDtos = new ArrayList<>();
    osmDto.setChangeSetDto(changeSetDto);
    tagDtos.add(new TagDto(comment, "comment"));
    tagDtos.add(new TagDto(BuildConfig.APP_NAME + " " + BuildConfig.VERSION_NAME, "created_by"));
    changeSetDto.setTagDtoList(tagDtos);
    Call<ResponseBody> callChangeSet;
    if (loginPreferences.retrieveOAuthParams() != null) {
        callChangeSet = osmRestClient.addChangeSet(AuthenticationRequestInterceptor.getOAuthRequest(loginPreferences, BuildConfig.BASE_OSM_URL + "changeset/create", Verb.PUT).getOAuthHeader(), osmDto);
    } else {
        callChangeSet = osmRestClient.addChangeSet(osmDto);
    }
    try {
        Response<ResponseBody> response = callChangeSet.execute();
        if (response.isSuccessful() && response.body() != null) {
            return response.body().string();
        }
    } catch (IOException e) {
        Timber.e("Retrofit error, couldn't create Changeset!");
    }
    bus.post(new SyncUploadRetrofitErrorEvent(-1L));
    return null;
}
Also used : OsmDto(io.jawg.osmcontributor.rest.dtos.osm.OsmDto) ChangeSetDto(io.jawg.osmcontributor.rest.dtos.osm.ChangeSetDto) TagDto(io.jawg.osmcontributor.rest.dtos.osm.TagDto) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SyncUploadRetrofitErrorEvent(io.jawg.osmcontributor.rest.events.error.SyncUploadRetrofitErrorEvent) ResponseBody(okhttp3.ResponseBody)

Example 3 with SyncUploadRetrofitErrorEvent

use of io.jawg.osmcontributor.rest.events.error.SyncUploadRetrofitErrorEvent in project osm-contributor by jawg.

the class SyncManager method remoteDeletePoi.

/**
 * Delete a Poi of the backend.
 *
 * @param poi         The Poi to delete.
 * @param changeSetId The changeSet in which the Poi is sent.
 * @return Whether the delete was a success or not.
 */
private boolean remoteDeletePoi(final Poi poi, String changeSetId) {
    Backend.ModificationStatus modificationStatus = backend.deletePoi(poi, changeSetId);
    poiManager.deleteOldPoiAssociated(poi);
    switch(modificationStatus) {
        case SUCCESS:
        case FAILURE_NOT_EXISTING:
            OsmAnswers.remotePoiAction(poi.getType().getTechnicalName(), "delete");
            poiManager.deletePoi(poi);
            return true;
        case FAILURE_CONFLICT:
            bus.post(new SyncConflictingNodeErrorEvent(poi.getName(), poi.getId()));
            Timber.e("Couldn't update poi %s: conflict, redownloading last version of poi", poi);
            deleteAndRetrieveUnmodifiedPoi(poi);
            return false;
        case FAILURE_UNKNOWN:
        default:
            Timber.e("Couldn't delete poi %s", poi);
            bus.post(new SyncUploadRetrofitErrorEvent(poi.getId()));
            return false;
    }
}
Also used : Backend(io.jawg.osmcontributor.rest.Backend) SyncConflictingNodeErrorEvent(io.jawg.osmcontributor.rest.events.error.SyncConflictingNodeErrorEvent) SyncUploadRetrofitErrorEvent(io.jawg.osmcontributor.rest.events.error.SyncUploadRetrofitErrorEvent)

Aggregations

SyncUploadRetrofitErrorEvent (io.jawg.osmcontributor.rest.events.error.SyncUploadRetrofitErrorEvent)3 Backend (io.jawg.osmcontributor.rest.Backend)2 SyncConflictingNodeErrorEvent (io.jawg.osmcontributor.rest.events.error.SyncConflictingNodeErrorEvent)2 ChangeSetDto (io.jawg.osmcontributor.rest.dtos.osm.ChangeSetDto)1 OsmDto (io.jawg.osmcontributor.rest.dtos.osm.OsmDto)1 TagDto (io.jawg.osmcontributor.rest.dtos.osm.TagDto)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ResponseBody (okhttp3.ResponseBody)1