Skip to content

Commit

Permalink
Checkin for 0.1.2
Browse files Browse the repository at this point in the history
Correctly implement ChangePropertyCommand for Undo/Redo.
Prevent search if address is empty so that API won't throw an error back at us.
  • Loading branch information
kmpoppe committed Jun 26, 2020
1 parent 8a2f368 commit ce91c84
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 64 deletions.
4 changes: 2 additions & 2 deletions fhrsPlugin/MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ Manifest-Version: 1.0
Ant-Version: Apache Ant 1.10.7
Created-By: 1.8.0_251-b08 (Oracle Corporation)
Plugin-Mainversion: 16538
Plugin-Version: 0.1.1
Plugin-Version: 0.1.2
Plugin-Class: org.openstreetmap.josm.plugins.fhrs.FHRSPlugin
Plugin-Description: Search establishments in FHRS API and merge data i
nto OSM
Plugin-Date: 2020-06-25T23:05:23.203
Plugin-Date: 2020-06-26T06:50:04.248
Author: Kai Michael Poppe
Plugin-Link: https://wiki.openstreetmap.org/wiki/User:Kmpoppe/Plugins#
fhrsPlugin
Expand Down
2 changes: 1 addition & 1 deletion fhrsPlugin/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project name="fhrsPlugin" default="dist" basedir=".">
<property name="commit.message" value="FHRS"/>
<property name="plugin.main.version" value="16538"/>
<property name="version.entry.commit.revision" value="0.1.1"/>
<property name="version.entry.commit.revision" value="0.1.2"/>
<property name="plugin.author" value="Kai Michael Poppe"/>
<property name="plugin.class" value="org.openstreetmap.josm.plugins.fhrs.FHRSPlugin"/>
<property name="plugin.description" value="Search establishments in FHRS API and merge data into OSM"/>
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
133 changes: 72 additions & 61 deletions fhrsPlugin/src/org/openstreetmap/josm/plugins/fhrs/FHRSPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import java.util.Map;

import org.openstreetmap.josm.actions.*;

import org.openstreetmap.josm.command.ChangePropertyCommand;
import org.openstreetmap.josm.data.UndoRedoHandler;
import org.openstreetmap.josm.data.osm.*;

import org.openstreetmap.josm.gui.*;
Expand Down Expand Up @@ -99,71 +100,77 @@ public void actionPerformed(ActionEvent event) {
thisName = selectedObject.get("name");
}
String[] addrTags = new String[] { "housenumber", "street", "city", "postcode"};
boolean moreThanHousenumber = false;
for(String addrTag : addrTags) {
if (selectedObject.getKeys().containsKey("addr:" + addrTag)) {
thisAddress += (thisAddress != "" ? " " : "") + selectedObject.get("addr:" + addrTag);
if (addrTag != "housenumber") moreThanHousenumber = true;
}
}
String returnedJson = "{}";
String cEncodedName = "";
String cEncodedAddress = "";
try {
cEncodedName = URLEncoder.encode(thisName, StandardCharsets.UTF_8.toString());
cEncodedAddress = URLEncoder.encode(thisAddress, StandardCharsets.UTF_8.toString());
} catch (Exception e) {
// This shouldn't fail
}
try {
Gson gson = new Gson();
String cUrl = "";
JsonArray jEstablishmentsArray;
for (String[] pars: new String[][] {
{ "&name=" + cEncodedName, "&address=" + cEncodedAddress },
{ "&address=" + cEncodedAddress },
{ "&name=" + cEncodedName }
} )
{
cUrl = cApiEst + "?pagesize=10";
for(String par: pars) cUrl = cUrl + par;
returnedJson = fhrsApiCall(cUrl);
if (gson.fromJson(returnedJson, JsonObject.class).getAsJsonArray("establishments").size() > 0) break;
if (moreThanHousenumber) {
String returnedJson = "{}";
String cEncodedName = "";
String cEncodedAddress = "";
try {
cEncodedName = URLEncoder.encode(thisName, StandardCharsets.UTF_8.toString());
cEncodedAddress = URLEncoder.encode(thisAddress, StandardCharsets.UTF_8.toString());
} catch (Exception e) {
// This shouldn't fail
}
jEstablishmentsArray =
gson
.fromJson(returnedJson, JsonObject.class)
.getAsJsonArray("establishments");
List<List<String>> searchResults = new ArrayList<List<String>>();
for(JsonElement jEstablishmentElement: jEstablishmentsArray) {
JsonObject jEstablishmentObject = jEstablishmentElement.getAsJsonObject();
List<String> tableEntry = new ArrayList<String>();
String ApiFHRSID = jEstablishmentObject.get("FHRSID").toString();
String ApiBusinessName = jEstablishmentObject.get("BusinessName").toString().replaceAll("\"([^\"]*)\"", "$1");
tableEntry.add(ApiFHRSID);
tableEntry.add(ApiBusinessName);
String ApiFullAddress = "";
for (int iAddrLine = 1; iAddrLine < 5; iAddrLine++) {
String ApiAddressEntry = jEstablishmentObject
.get("AddressLine" + Integer.toString(iAddrLine))
.toString()
.replaceAll("\"([^\"]*)\"", "$1");
ApiFullAddress += " " + ApiAddressEntry.trim();
ApiFullAddress = ApiFullAddress.trim();
try {
Gson gson = new Gson();
String cUrl = "";
JsonArray jEstablishmentsArray;
for (String[] pars: new String[][] {
{ "&name=" + cEncodedName, "&address=" + cEncodedAddress },
{ "&address=" + cEncodedAddress },
{ "&name=" + cEncodedName }
} )
{
cUrl = cApiEst + "?pagesize=10";
for(String par: pars) cUrl = cUrl + par;
returnedJson = fhrsApiCall(cUrl);
if (gson.fromJson(returnedJson, JsonObject.class).getAsJsonArray("establishments").size() > 0) break;
}
tableEntry.add(ApiFullAddress);
searchResults.add(tableEntry);
jEstablishmentsArray =
gson
.fromJson(returnedJson, JsonObject.class)
.getAsJsonArray("establishments");
List<List<String>> searchResults = new ArrayList<List<String>>();
for(JsonElement jEstablishmentElement: jEstablishmentsArray) {
JsonObject jEstablishmentObject = jEstablishmentElement.getAsJsonObject();
List<String> tableEntry = new ArrayList<String>();
String ApiFHRSID = jEstablishmentObject.get("FHRSID").toString();
String ApiBusinessName = jEstablishmentObject.get("BusinessName").toString().replaceAll("\"([^\"]*)\"", "$1");
tableEntry.add(ApiFHRSID);
tableEntry.add(ApiBusinessName);
String ApiFullAddress = "";
for (int iAddrLine = 1; iAddrLine < 5; iAddrLine++) {
String ApiAddressEntry = jEstablishmentObject
.get("AddressLine" + Integer.toString(iAddrLine))
.toString()
.replaceAll("\"([^\"]*)\"", "$1");
ApiFullAddress += " " + ApiAddressEntry.trim();
ApiFullAddress = ApiFullAddress.trim();
}
tableEntry.add(ApiFullAddress);
searchResults.add(tableEntry);
}
String selectedFhrsId = searchResultsDialog.showSearchDialog(searchResults);
if (selectedFhrsId != null)
updateObjectData(selectedFhrsId);
else
msgBox("Setting values was cancelled.", JOptionPane.INFORMATION_MESSAGE);
} catch (FileNotFoundException e) {
msgBox("FHRS ID " + selectedObject.get("fhrs:id").toString() + " not found in database.", JOptionPane.ERROR_MESSAGE);
} catch (Exception e) {
String cStackTrace = "";
for(StackTraceElement s: e.getStackTrace())
cStackTrace += s.toString() + crLf;
msgBox(e.toString() + cStackTrace, JOptionPane.ERROR_MESSAGE);
}
String selectedFhrsId = searchResultsDialog.showSearchDialog(searchResults);
if (selectedFhrsId != null)
updateObjectData(selectedFhrsId);
else
msgBox("Setting values was cancelled.", JOptionPane.INFORMATION_MESSAGE);
} catch (FileNotFoundException e) {
msgBox("FHRS ID " + selectedObject.get("fhrs:id").toString() + " not found in database.", JOptionPane.ERROR_MESSAGE);
} catch (Exception e) {
String cStackTrace = "";
for(StackTraceElement s: e.getStackTrace())
cStackTrace += s.toString() + crLf;
msgBox(e.toString() + cStackTrace, JOptionPane.ERROR_MESSAGE);
} else {
msgBox("This object doesn't have an address. The FHRS API will not return data.", JOptionPane.ERROR_MESSAGE);
}
}
} else {
Expand Down Expand Up @@ -214,11 +221,15 @@ public void updateObjectData(String fhrsId) {
);
}
}
TagMap existingTags = selectedObject.getKeys();
Map<String, String> osmTagsToSet = mergeTagsDialog.showTagsDialog(osmTags);
if (osmTagsToSet != null) {
existingTags.putAll(osmTagsToSet);
selectedObject.setKeys(existingTags);
ChangePropertyCommand changePropertyCommand =
new ChangePropertyCommand(
currentDataSet,
java.util.Collections.singleton(selectedObject),
osmTagsToSet
);
UndoRedoHandler.getInstance().add(changePropertyCommand);
} else {
msgBox("Setting values was cancelled.", JOptionPane.INFORMATION_MESSAGE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static Map<String, String> showTagsDialog (final Map<String, FHRSPlugin.o
panel.setLayout(new BorderLayout());
panel.add(osmTagsTable.getTableHeader(), BorderLayout.PAGE_START);
panel.add(osmTagsTable, BorderLayout.CENTER);
panel.add(new JLabel("Select which values to merge from FHRS to OSM. You can also change the \"New value\" if you need to before merging."), BorderLayout.PAGE_END);
final int result = JOptionPane.showOptionDialog(null, panel, "Select which values to merge from FHRS to OSM",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
null, null, null);
Expand Down

0 comments on commit ce91c84

Please sign in to comment.