| | 788 | |
| | 789 | /** |
| | 790 | * Create a new note on the server |
| | 791 | * @param latlon Location of note |
| | 792 | * @param text Comment entered by user to open the note |
| | 793 | * @param monitor Progress monitor |
| | 794 | * @return Note as it exists on the server after creation (ID assigned) |
| | 795 | * @throws OsmTransferException |
| | 796 | */ |
| | 797 | public Note createNote(LatLon latlon, String text, ProgressMonitor monitor) throws OsmTransferException { |
| | 798 | initialize(monitor); |
| | 799 | String url = new StringBuilder() |
| | 800 | .append("notes?lat=") |
| | 801 | .append(latlon.lat()) |
| | 802 | .append("&lon=") |
| | 803 | .append(latlon.lon()) |
| | 804 | .append("&text=") |
| | 805 | .append(urlEncode(text)).toString(); |
| | 806 | |
| | 807 | String response = sendRequest("POST", url, null, monitor, true, false); |
| | 808 | return parseSingleNote(response); |
| | 809 | } |
| | 810 | |
| | 811 | /** |
| | 812 | * Add a comment to an existing note. |
| | 813 | * @param note The note to add a comment to |
| | 814 | * @param comment Text of the comment |
| | 815 | * @param monitor Progress monitor |
| | 816 | * @return Note returned by the API after the comment was added |
| | 817 | * @throws OsmTransferException |
| | 818 | */ |
| | 819 | public Note addCommentToNote(Note note, String comment, ProgressMonitor monitor) throws OsmTransferException { |
| | 820 | initialize(monitor); |
| | 821 | String url = new StringBuilder() |
| | 822 | .append("notes/") |
| | 823 | .append(note.getId()) |
| | 824 | .append("/comment?text=") |
| | 825 | .append(urlEncode(comment)).toString(); |
| | 826 | |
| | 827 | String response = sendRequest("POST", url, null, monitor, true, false); |
| | 828 | return parseSingleNote(response); |
| | 829 | } |
| | 830 | |
| | 831 | /** |
| | 832 | * Close a note |
| | 833 | * @param note Note to close. Must currently be open |
| | 834 | * @param closeMessage Optional message supplied by the user when closing the note |
| | 835 | * @param monitor Progress monitor |
| | 836 | * @return Note returned by the API after the close operation |
| | 837 | * @throws OsmTransferException |
| | 838 | */ |
| | 839 | public Note closeNote(Note note, String closeMessage, ProgressMonitor monitor) throws OsmTransferException { |
| | 840 | initialize(monitor); |
| | 841 | String encodedMessage = urlEncode(closeMessage); |
| | 842 | StringBuilder urlBuilder = new StringBuilder() |
| | 843 | .append("notes/") |
| | 844 | .append(note.getId()) |
| | 845 | .append("/close"); |
| | 846 | if (encodedMessage != null && !encodedMessage.trim().isEmpty()) { |
| | 847 | urlBuilder.append("?text="); |
| | 848 | urlBuilder.append(encodedMessage); |
| | 849 | } |
| | 850 | |
| | 851 | String response = sendRequest("POST", urlBuilder.toString(), null, monitor, true, false); |
| | 852 | return parseSingleNote(response); |
| | 853 | } |
| | 854 | |
| | 855 | /** |
| | 856 | * Reopen a closed note |
| | 857 | * @param note Note to reopen. Must currently be closed |
| | 858 | * @param reactivateMessage Optional message supplied by the user when reopening the note |
| | 859 | * @param monitor Progress monitor |
| | 860 | * @return Note returned by the API after the reopen operation |
| | 861 | * @throws OsmTransferException |
| | 862 | */ |
| | 863 | public Note reopenNote(Note note, String reactivateMessage, ProgressMonitor monitor) throws OsmTransferException { |
| | 864 | initialize(monitor); |
| | 865 | String encodedMessage = urlEncode(reactivateMessage); |
| | 866 | StringBuilder urlBuilder = new StringBuilder() |
| | 867 | .append("notes/") |
| | 868 | .append(note.getId()) |
| | 869 | .append("/reopen"); |
| | 870 | if (encodedMessage != null && !encodedMessage.trim().isEmpty()) { |
| | 871 | urlBuilder.append("?text="); |
| | 872 | urlBuilder.append(encodedMessage); |
| | 873 | } |
| | 874 | |
| | 875 | String response = sendRequest("POST", urlBuilder.toString(), null, monitor, true, false); |
| | 876 | return parseSingleNote(response); |
| | 877 | } |
| | 878 | |
| | 879 | /** Method for parsing API responses for operations on individual notes */ |
| | 880 | private Note parseSingleNote(String xml) throws OsmTransferException { |
| | 881 | try { |
| | 882 | List<Note> newNotes = new NoteReader(xml).parse(); |
| | 883 | if(newNotes.size() == 1) { |
| | 884 | return newNotes.get(0); |
| | 885 | } |
| | 886 | //Shouldn't ever execute. Server will either respond with an error (caught elsewhere) or one note |
| | 887 | throw new OsmTransferException(tr("Note upload failed")); |
| | 888 | } catch (SAXException|IOException e) { |
| | 889 | Main.error(e, true); |
| | 890 | throw new OsmTransferException(tr("Error parsing note response from server"), e); |
| | 891 | } |
| | 892 | } |
| | 893 | |
| | 894 | /** URL encodes a string. Useful for transforming user input into URL query strings*/ |
| | 895 | private String urlEncode(String string) throws OsmTransferException { |
| | 896 | try { |
| | 897 | return URLEncoder.encode(string, "UTF-8"); |
| | 898 | } catch (UnsupportedEncodingException e) { |
| | 899 | Main.error(e, true); |
| | 900 | throw new OsmTransferException(tr("Error encoding string: {0}", string), e); |
| | 901 | } |
| | 902 | } |