Skip to content

Commit

Permalink
Issue #96 - the geoservice can now be used by gedbrowser behaviors
Browse files Browse the repository at this point in the history
* This makes the client a consumable object
* The geoservice client application is more robust for testing
* I am holding back the actual use of the client in the gebrowser
  • Loading branch information
dickschoeller committed Jan 17, 2017
1 parent fc79263 commit 628644e
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 19 deletions.
5 changes: 5 additions & 0 deletions gedbrowser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
<artifactId>gedbrowser-geographics</artifactId>
<version>1.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.schoellerfamily.gedbrowser</groupId>
<artifactId>geoservice-client</artifactId>
<version>1.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
SubmittorDocumentRepositoryMongo;
import org.schoellerfamily.gedbrowser.persistence.mongo.repository.
TrailerDocumentRepositoryMongo;
import org.schoellerfamily.geoservice.client.GeoServiceClient;
import org.schoellerfamily.geoservice.client.GeoServiceClientImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
Expand All @@ -31,6 +34,7 @@
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.repository.config.
EnableMongoRepositories;
import org.springframework.web.client.RestTemplate;

import com.mongodb.MongoClient;

Expand Down Expand Up @@ -122,4 +126,26 @@ public ApplicationInfo appInfo() {
// CHECKSTYLE:ON
return new ApplicationInfo();
}

/**
* @param builder the rest template builder that Spring provides
* @return the rest template
*/
// CHECKSTYLE:OFF
@Bean
public RestTemplate restTemplate(final RestTemplateBuilder builder) {
// CHECKSTYLE:ON
return builder.build();
}

/**
* @return the geoservice client
*/
// We turn off checkstyle because bean methods must not be final
// CHECKSTYLE:OFF
@Bean
public GeoServiceClient client() {
// CHECKSTYLE:ON
return new GeoServiceClientImpl();
}
}
16 changes: 16 additions & 0 deletions geoservice-client/.eclipse-pmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<eclipse-pmd xmlns="http://acanda.ch/eclipse-pmd/0.8" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://acanda.ch/eclipse-pmd/0.8 http://acanda.ch/eclipse-pmd/eclipse-pmd-0.8.xsd">
<analysis enabled="true" />
<rulesets>
<ruleset name="Basic" ref="gedbrowser-top/config/rulesets/java/basic.xml" refcontext="workspace" />
<ruleset name="Code Size" ref="gedbrowser-top/config/rulesets/java/codesize.xml" refcontext="workspace" />
<ruleset name="Comments" ref="gedbrowser-top/config/rulesets/java/comments.xml" refcontext="workspace" />
<ruleset name="Coupling" ref="gedbrowser-top/config/rulesets/java/coupling.xml" refcontext="workspace" />
<ruleset name="Design" ref="gedbrowser-top/config/rulesets/java/design.xml" refcontext="workspace" />
<ruleset name="Import Statements" ref="gedbrowser-top/config/rulesets/java/imports.xml" refcontext="workspace" />
<ruleset name="JUnit" ref="gedbrowser-top/config/rulesets/java/junit.xml" refcontext="workspace" />
<ruleset name="Optimization" ref="gedbrowser-top/config/rulesets/java/optimizations.xml" refcontext="workspace" />
<ruleset name="Security Code Guidelines" ref="gedbrowser-top/config/rulesets/java/sunsecure.xml" refcontext="workspace" />
<ruleset name="Unused Code" ref="gedbrowser-top/config/rulesets/java/unusedcode.xml" refcontext="workspace" />
</rulesets>
</eclipse-pmd>
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,56 @@

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* Main program for playing with the GeoService client class.
*
* @author Dick Schoeller
*/
@SpringBootApplication
public class Application {
/** Logger. */
private final transient Log logger = LogFactory.getLog(getClass());
private final transient Log logger =
LogFactory.getLog(Application.class);

/** */
@Autowired
private transient ObjectMapper mapper;

public static void main(String args[]) {
SpringApplication.run(Application.class);
/** */
@Autowired
private transient GeoServiceClient client;

/**
* @param args standard main program argument handling
*/
public static void main(final String... args) {
SpringApplication.run(Application.class, args);
}


/**
* @param builder the rest template builder that Spring provides
* @return the rest template
*/
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
public RestTemplate restTemplate(final RestTemplateBuilder builder) {
return builder.build();
}

/**
* @return the command line runner
* @throws Exception because things can blow up
*/
@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
public CommandLineRunner run() throws Exception {
return args -> {

GeoServiceItem item = restTemplate.getForObject(
"http://localhost:8088/geocode?name=Bethlhem,%20PA", GeoServiceItem.class);
// final GeoServiceAddressComponent[] comps = item.getResult().getAddressComponents();
// logger.info("there are " + comps.length + " address components");

// ObjectMapper mapper = new ObjectMapper();
logger.info("found item:" + mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(item));
for (final String arg : args) {
logger.info("Get geocode for: " + arg);
final GeoServiceItem item =
client.get(arg);
logger.info(
"found item:" + mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(item));
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.schoellerfamily.geoservice.client;

import org.schoellerfamily.geoservice.model.GeoServiceItem;

/**
* @author Dick Schoeller
*/
public interface GeoServiceClient {
/**
* @param placeName the place name
* @return the geocoding result
*/
GeoServiceItem get(String placeName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.schoellerfamily.geoservice.client;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.schoellerfamily.geoservice.model.GeoServiceItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
* @author Dick Schoeller
*/
@Component
public class GeoServiceClientImpl implements GeoServiceClient {
/** Logger. */
private final transient Log logger = LogFactory.getLog(getClass());

/** */
@Autowired
private transient RestTemplate restTemplate;

/**
* {@inheritDoc}
*/
@Override
public GeoServiceItem get(final String placeName) {
logger.info("Get: " + placeName);
final GeoServiceItem item = restTemplate.getForObject(
"http://localhost:8088/geocode?name=" + placeName,
GeoServiceItem.class);
return item;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* Copyright 2017 Richard Schoeller
* Main package for geoservice client.
*/
package org.schoellerfamily.geoservice.client;
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
@SuppressWarnings("PMD.CommentSize")
public final class GeoServiceGeocodingResult {
/**
* {@code addressComponents} is an array containing the separate address
* components.
* The {@code types} array indicates the type of the returned result. This
* array contains a set of zero or more tags identifying the type of feature
* returned in the result. For example, a geocode of "Chicago" returns
* "locality" which indicates that "Chicago" is a city, and also returns
* "political" which indicates it is a political entity.
*/
private final AddressType[] types;

Expand Down Expand Up @@ -94,7 +97,10 @@ public GeoServiceGeocodingResult(
}

/**
* @return the addressComponents
* {@code addressComponents} is an array containing the separate address
* components.
*
* @return the array of components
*/
@SuppressWarnings("PMD.MethodReturnsInternalArray")
public AddressComponent[] getAddressComponents() {
Expand Down Expand Up @@ -140,6 +146,10 @@ public String[] getPostcodeLocalities() {
}

/**
* Get the geometry. It has been transformed into a GeoJSON
* FeatureCollection. It has the location, bounds and viewport as separate
* features.
*
* @return the geometry
*/
public FeatureCollection getGeometry() {
Expand Down Expand Up @@ -202,7 +212,11 @@ public String getPlaceId() {
}

/**
* @return the location feature, which is carrying a bunch of other dreck
* Return the location. It is in the form of a GeoJSON feature.
* Much of the descriptive data from Google is in the properties
* of that feature.
*
* @return the location feature
*/
@Transient
private Feature getLocation() {
Expand Down

0 comments on commit 628644e

Please sign in to comment.