Search in sources :

Example 1 with NoteDto

use of io.jawg.osmcontributor.rest.dtos.osm.NoteDto in project osm-contributor by jawg.

the class NoteMapper method convertNoteToNoteDto.

/**
 * @param note the note to convert
 * @return the converted NoteDto
 */
public NoteDto convertNoteToNoteDto(Note note) {
    NoteDto noteDto = new NoteDto();
    noteDto.setLat(note.getLatitude());
    noteDto.setLon(note.getLongitude());
    noteDto.setId(note.getBackendId());
    noteDto.setStatus(note.getStatus());
    noteDto.setCommentDtoList(commentMapper.convertFromComment(note.getComments()));
    return noteDto;
}
Also used : NoteDto(io.jawg.osmcontributor.rest.dtos.osm.NoteDto)

Example 2 with NoteDto

use of io.jawg.osmcontributor.rest.dtos.osm.NoteDto in project osm-contributor by jawg.

the class OSMSyncNoteManager method syncDownloadNotesInBox.

/**
 * {@inheritDoc}
 */
@Override
public List<Note> syncDownloadNotesInBox(final Box box) {
    Timber.d("Requesting osm for notes download");
    String strBox = box.getWest() + "," + box.getSouth() + "," + box.getEast() + "," + box.getNorth();
    Call<OsmDto> osmDtoCall = osmRestClient.getNotes(strBox);
    try {
        Response<OsmDto> response = osmDtoCall.execute();
        if (response.isSuccessful()) {
            OsmDto osmDto = response.body();
            if (osmDto != null) {
                List<NoteDto> noteDtos = osmDto.getNoteDtoList();
                if (noteDtos != null && !noteDtos.isEmpty()) {
                    Timber.d("Updating %d note(s)", noteDtos.size());
                    bus.post(new SyncDownloadRetrofitErrorEvent());
                    return noteMapper.convertNoteDtosToNotes(noteDtos);
                }
            }
        }
    } catch (IOException e) {
        Timber.e(e, e.getMessage());
    }
    return null;
}
Also used : OsmDto(io.jawg.osmcontributor.rest.dtos.osm.OsmDto) SyncDownloadRetrofitErrorEvent(io.jawg.osmcontributor.rest.events.error.SyncDownloadRetrofitErrorEvent) IOException(java.io.IOException) NoteDto(io.jawg.osmcontributor.rest.dtos.osm.NoteDto)

Example 3 with NoteDto

use of io.jawg.osmcontributor.rest.dtos.osm.NoteDto in project osm-contributor by jawg.

the class OSMSyncNoteManager method remoteAddComment.

/**
 * {@inheritDoc}
 */
public Note remoteAddComment(final Comment comment) {
    Call<OsmDto> osmDtoCall;
    switch(comment.getAction()) {
        case Comment.ACTION_CLOSE:
            osmDtoCall = osmRestClient.closeNote(comment.getNote().getBackendId(), comment.getText(), "");
            break;
        case Comment.ACTION_REOPEN:
            osmDtoCall = osmRestClient.reopenNote(comment.getNote().getBackendId(), comment.getText(), "");
            break;
        case Comment.ACTION_OPEN:
            osmDtoCall = osmRestClient.addNote(comment.getNote().getLatitude(), comment.getNote().getLongitude(), comment.getText(), "");
            break;
        default:
            osmDtoCall = osmRestClient.addComment(comment.getNote().getBackendId(), comment.getText(), "");
            break;
    }
    try {
        Response<OsmDto> response = osmDtoCall.execute();
        if (response.isSuccessful()) {
            OsmDto osmDto = response.body();
            if (osmDto != null) {
                NoteDto noteDto = osmDto.getNoteDtoList().get(0);
                Note note = noteMapper.convertNoteDtoToNote(noteDto);
                note.setUpdated(false);
                note.setId(comment.getNote().getId());
                return note;
            }
        } else if (response.code() == 409) {
            bus.post(new SyncConflictingNoteErrorEvent(comment.getNote()));
        }
    } catch (IOException e) {
        Timber.e(e, "Retrofit error, couldn't create comment !");
    }
    bus.post(new SyncUploadNoteRetrofitErrorEvent(comment.getNote().getId()));
    return null;
}
Also used : OsmDto(io.jawg.osmcontributor.rest.dtos.osm.OsmDto) SyncConflictingNoteErrorEvent(io.jawg.osmcontributor.rest.events.error.SyncConflictingNoteErrorEvent) Note(io.jawg.osmcontributor.model.entities.Note) SyncUploadNoteRetrofitErrorEvent(io.jawg.osmcontributor.rest.events.error.SyncUploadNoteRetrofitErrorEvent) IOException(java.io.IOException) NoteDto(io.jawg.osmcontributor.rest.dtos.osm.NoteDto)

Example 4 with NoteDto

use of io.jawg.osmcontributor.rest.dtos.osm.NoteDto in project osm-contributor by jawg.

the class NoteMapper method convertNoteDtosToNotes.

public List<Note> convertNoteDtosToNotes(List<NoteDto> dtos) {
    List<Note> result = new ArrayList<>();
    if (dtos != null) {
        for (NoteDto dto : dtos) {
            Note note = new Note();
            note.setLatitude(dto.getLat());
            note.setLongitude(dto.getLon());
            note.setBackendId(dto.getId());
            note.setUpdated(false);
            note.setStatus(dto.getStatus());
            DateTime dt = null;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z", Locale.US);
            try {
                Date date = simpleDateFormat.parse(dto.getDate_created());
                dt = new DateTime(date);
            } catch (ParseException ex) {
                System.out.println("Exception " + ex);
            }
            note.setCreatedDate(dt);
            ArrayList<Comment> comments = new ArrayList<>(dto.getCommentDtoList().size());
            for (CommentDto commentDto : dto.getCommentDtoList()) {
                Comment comment = new Comment();
                comment.setNote(note);
                comment.setUpdated(false);
                comment.setAction(commentDto.getAction());
                comment.setText(commentDto.getText());
                try {
                    Date date = simpleDateFormat.parse(commentDto.getDate());
                    dt = new DateTime(date);
                } catch (ParseException ex) {
                    System.out.println("Exception " + ex);
                }
                comment.setCreatedDate(dt);
                comments.add(comment);
            }
            note.setComments(comments);
            result.add(note);
        }
    }
    return result;
}
Also used : Comment(io.jawg.osmcontributor.model.entities.Comment) Note(io.jawg.osmcontributor.model.entities.Note) ArrayList(java.util.ArrayList) CommentDto(io.jawg.osmcontributor.rest.dtos.osm.CommentDto) ParseException(java.text.ParseException) NoteDto(io.jawg.osmcontributor.rest.dtos.osm.NoteDto) SimpleDateFormat(java.text.SimpleDateFormat) DateTime(org.joda.time.DateTime) Date(java.util.Date)

Aggregations

NoteDto (io.jawg.osmcontributor.rest.dtos.osm.NoteDto)4 Note (io.jawg.osmcontributor.model.entities.Note)2 OsmDto (io.jawg.osmcontributor.rest.dtos.osm.OsmDto)2 IOException (java.io.IOException)2 Comment (io.jawg.osmcontributor.model.entities.Comment)1 CommentDto (io.jawg.osmcontributor.rest.dtos.osm.CommentDto)1 SyncConflictingNoteErrorEvent (io.jawg.osmcontributor.rest.events.error.SyncConflictingNoteErrorEvent)1 SyncDownloadRetrofitErrorEvent (io.jawg.osmcontributor.rest.events.error.SyncDownloadRetrofitErrorEvent)1 SyncUploadNoteRetrofitErrorEvent (io.jawg.osmcontributor.rest.events.error.SyncUploadNoteRetrofitErrorEvent)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 DateTime (org.joda.time.DateTime)1