1 |
torben |
2409 |
package dk.daoas.daoadresseservice;
|
2 |
|
|
|
3 |
|
|
import java.util.List;
|
4 |
|
|
|
5 |
|
|
import com.google.code.geocoder.Geocoder;
|
6 |
|
|
import com.google.code.geocoder.GeocoderRequestBuilder;
|
7 |
|
|
import com.google.code.geocoder.model.GeocodeResponse;
|
8 |
|
|
import com.google.code.geocoder.model.GeocoderAddressComponent;
|
9 |
|
|
import com.google.code.geocoder.model.GeocoderRequest;
|
10 |
|
|
import com.google.code.geocoder.model.GeocoderResult;
|
11 |
|
|
import com.google.code.geocoder.model.GeocoderStatus;
|
12 |
|
|
|
13 |
|
|
import dk.daoas.daoadresseservice.admin.ServiceConfig;
|
14 |
|
|
import dk.daoas.daoadresseservice.beans.SearchResult;
|
15 |
|
|
import dk.thoerup.circuitbreaker.CircuitBreaker;
|
16 |
|
|
import dk.thoerup.circuitbreaker.CircuitBreakerManager;
|
17 |
|
|
import dk.thoerup.circuitbreaker.CircuitInvocation;
|
18 |
|
|
|
19 |
|
|
public class GoogleStreetnameHelper implements StreetnameHelper {
|
20 |
|
|
|
21 |
|
|
ServiceConfig conf;
|
22 |
|
|
public GoogleStreetnameHelper(ServiceConfig conf) {
|
23 |
|
|
this.conf = conf;
|
24 |
|
|
}
|
25 |
|
|
|
26 |
|
|
@Override
|
27 |
|
|
public String proposeStreetName(SearchResult result, int postnr, String vejnavn) {
|
28 |
|
|
if (conf.useGoogle == false)
|
29 |
|
|
return null;
|
30 |
|
|
|
31 |
|
|
result.google = true;
|
32 |
|
|
|
33 |
|
|
try {
|
34 |
|
|
GoogleInvocation wrapper = new GoogleInvocation( conf, postnr, vejnavn );
|
35 |
|
|
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("google");
|
36 |
|
|
|
37 |
|
|
|
38 |
|
|
result.googleVej = (String) breaker.invoke(wrapper);
|
39 |
|
|
return result.googleVej;
|
40 |
|
|
|
41 |
|
|
|
42 |
|
|
} catch (Exception e) {
|
43 |
|
|
System.out.println( "GoogleError: " + e.getClass().getName() +" / "+ e.getMessage() );
|
44 |
|
|
}
|
45 |
|
|
return null;
|
46 |
|
|
}
|
47 |
|
|
|
48 |
|
|
static class GoogleInvocation implements CircuitInvocation {
|
49 |
|
|
ServiceConfig conf;
|
50 |
|
|
int postnr;
|
51 |
|
|
String vejnavn;
|
52 |
|
|
|
53 |
|
|
public GoogleInvocation(ServiceConfig conf, int postnr, String vejnavn) {
|
54 |
|
|
this.conf = conf;
|
55 |
|
|
this.postnr= postnr;
|
56 |
|
|
this.vejnavn = vejnavn;
|
57 |
|
|
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
@Override
|
61 |
|
|
public String proceed() throws Exception {
|
62 |
|
|
|
63 |
|
|
final Geocoder geocoder;
|
64 |
|
|
if ( conf.googleApiKey != null ) {
|
65 |
|
|
geocoder = new Geocoder(conf.googleApiUser, conf.googleApiKey); //Throws InvalidKeyException
|
66 |
|
|
} else {
|
67 |
|
|
geocoder = new Geocoder();
|
68 |
|
|
}
|
69 |
|
|
//TimingHelper timer = new TimingHelper();
|
70 |
|
|
|
71 |
|
|
String search = vejnavn + ", " + postnr + ", Denmark";
|
72 |
|
|
GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest();
|
73 |
|
|
GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
|
74 |
|
|
|
75 |
|
|
//timer.printElapsed("Google elapsed");
|
76 |
|
|
|
77 |
|
|
//System.out.println( "Status: >" + geocoderResponse.getStatus() + "<");
|
78 |
|
|
if ( geocoderResponse.getStatus() != GeocoderStatus.OK) {
|
79 |
|
|
|
80 |
|
|
if (geocoderResponse.getStatus() == GeocoderStatus.ZERO_RESULTS) {
|
81 |
|
|
return null;
|
82 |
|
|
} else {
|
83 |
|
|
System.out.println("Google responded with " + geocoderResponse.getStatus() );
|
84 |
|
|
//Hvis det er alvorlige fejl skal vi afbryde med exception og trigger circuitbreakeren
|
85 |
|
|
throw new Exception("Google responded with " + geocoderResponse.getStatus() );
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
List<GeocoderResult> resList = geocoderResponse.getResults();
|
91 |
|
|
//System.out.println( "Count: " + resList.size() );
|
92 |
|
|
|
93 |
|
|
if (resList.size() != 1) {
|
94 |
|
|
return null;
|
95 |
|
|
}
|
96 |
|
|
GeocoderResult res = resList.get(0);
|
97 |
|
|
|
98 |
|
|
List<GeocoderAddressComponent> compList = res.getAddressComponents();
|
99 |
|
|
for (GeocoderAddressComponent c : compList) {
|
100 |
|
|
//System.out.println(c);
|
101 |
|
|
if (c.getTypes().contains("route")) {
|
102 |
|
|
return c.getLongName();
|
103 |
|
|
}
|
104 |
|
|
}
|
105 |
|
|
return null;
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
}
|