use of com.esri.ges.core.geoevent.DefaultFieldDefinition in project reverse-geocoder-for-geoevent by Esri.
the class ReverseGeocoderProcessor method augmentGeoEventWithAddress.
private GeoEvent augmentGeoEventWithAddress(GeoEvent geoEvent, String addressJson) throws ConfigurationException, GeoEventDefinitionManagerException, FieldException, JSONException {
JSONObject jsonObj = new JSONObject(addressJson);
JSONObject addressJSONObject = jsonObj.getJSONObject("address");
GeoEvent newGeoEvent = null;
if (geoEventCreator != null) {
try {
GeoEventDefinition edIn = geoEvent.getGeoEventDefinition();
List<FieldDefinition> fds = new ArrayList<FieldDefinition>();
fds.add(new DefaultFieldDefinition("Address", FieldType.String));
fds.add(new DefaultFieldDefinition("Neighborhood", FieldType.String));
fds.add(new DefaultFieldDefinition("City", FieldType.String));
fds.add(new DefaultFieldDefinition("Subregion", FieldType.String));
fds.add(new DefaultFieldDefinition("Region", FieldType.String));
fds.add(new DefaultFieldDefinition("Postal", FieldType.String));
fds.add(new DefaultFieldDefinition("PostalExt", FieldType.String));
fds.add(new DefaultFieldDefinition("CountryCode", FieldType.String));
fds.add(new DefaultFieldDefinition("Match_addr", FieldType.String));
fds.add(new DefaultFieldDefinition("Loc_name", FieldType.String));
GeoEventDefinition edOut = edIn.augment(fds);
edOut.setName(newGeoEventDefinitionName);
edOut.setOwner(getId());
GeoEventDefinitionManager geoEventDefinitionManager = geoEventCreator.getGeoEventDefinitionManager();
// if (geoEventDefinitionManager.getGeoEventDefinition(newGeoEventDefinitionName) == null)
// geoEventDefinitionManager.addGeoEventDefinition(edOut);
// LOGGER.info("Checking for GED: " + edOut.getName());
String gedGuid = "";
try {
Collection<GeoEventDefinition> geoEventDefinitions = geoEventDefinitionManager.searchInRepositoryGeoEventDefinitionByName(newGeoEventDefinitionName);
// LOGGER.info("Found " + geoEventDefinitions.size() + " GEDs named " + edOut.getName());
if (geoEventDefinitions == null || geoEventDefinitions.size() == 0) {
// LOGGER.info("Didn't find it. Adding GED: " + edOut.getName());
geoEventDefinitionManager.addGeoEventDefinition(edOut);
} else
gedGuid = geoEventDefinitions.iterator().next().getGuid();
} catch (Exception ex) {
LOGGER.info("Encountered an error checking for this GED: " + edOut.getName() + ". Adding it anyway.");
geoEventDefinitionManager.addGeoEventDefinition(edOut);
gedGuid = edOut.getGuid();
}
List<Object> fieldValues = new ArrayList<Object>();
Object value;
for (int i = 0; i < edIn.getFieldDefinitions().size(); i++) {
value = geoEvent.getField(i);
fieldValues.add(value);
}
try {
fieldValues.add(addressJSONObject.get("Address"));
} catch (Exception ex) {
fieldValues.add("");
}
try {
fieldValues.add(addressJSONObject.get("Neighborhood"));
} catch (Exception ex) {
fieldValues.add("");
}
try {
fieldValues.add(addressJSONObject.get("City"));
} catch (Exception ex) {
fieldValues.add("");
}
try {
fieldValues.add(addressJSONObject.get("Subregion"));
} catch (Exception ex) {
fieldValues.add("");
}
try {
fieldValues.add(addressJSONObject.get("Region"));
} catch (Exception ex) {
fieldValues.add("");
}
try {
fieldValues.add(addressJSONObject.get("Postal"));
} catch (Exception ex) {
fieldValues.add("");
}
try {
fieldValues.add(addressJSONObject.get("PostalExt"));
} catch (Exception ex) {
fieldValues.add("");
}
try {
fieldValues.add(addressJSONObject.get("CountryCode"));
} catch (Exception ex) {
fieldValues.add("");
}
try {
fieldValues.add(addressJSONObject.get("Match_addr"));
} catch (Exception ex) {
fieldValues.add("");
}
try {
fieldValues.add(addressJSONObject.get("Loc_name"));
} catch (Exception ex) {
fieldValues.add("");
}
// LOGGER.info("Creating a Geoevent from GED: " + edOut.getName());
newGeoEvent = geoEventCreator.create(gedGuid);
// newGeoEvent = geoEventCreator.create(newGeoEventDefinitionName, definition.getUri().toString());
for (int i = 0; i < edOut.getFieldDefinitions().size(); i++) {
try {
newGeoEvent.setField(i, fieldValues.get(i));
} catch (Exception ex) {
}
}
newGeoEvent.setProperty(GeoEventPropertyName.TYPE, "event");
newGeoEvent.setProperty(GeoEventPropertyName.OWNER_ID, getId());
newGeoEvent.setProperty(GeoEventPropertyName.OWNER_URI, definition.getUri());
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return newGeoEvent;
}
Aggregations