1   package nl.mineleni.cbsviewer.servlet.gazetteer.lusclient;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import nl.mineleni.openls.databinding.openls.Address;
7   import nl.mineleni.openls.databinding.openls.GeocodeResponse;
8   import nl.mineleni.openls.databinding.openls.GeocodeResponseList;
9   import nl.mineleni.openls.databinding.openls.GeocodedAddress;
10  
11  
12  
13  
14  
15  
16  
17  public final class OpenLSClientUtil {
18  
19  	
20  	public static final String PLACE_TYPE_COUNTRYSUBDIVISION = "CountrySubdivision";
21  
22  	
23  	public static final String PLACE_TYPE_MUNICIPALITY = "Municipality";
24  
25  	
26  	public static final String PLACE_TYPE_MUNICIPALITYSUBDIVISION = "MunicipalitySubdivision";
27  
28  	
29  
30  
31  	private OpenLSClientUtil() {
32  		
33  	}
34  
35  	
36  
37  
38  
39  
40  
41  
42  	public static List<GeocodedAddress> getGeocodedAddressList(
43  			final GeocodeResponse gcr) {
44  		final List<GeocodedAddress> addressList = new ArrayList<>();
45  		for (int i = 0; i < gcr.getGeocodeResponseListSize(); i++) {
46  			final GeocodeResponseList gcrl = gcr.getGeocodeResponseListAt(i);
47  			for (int j = 0; j < gcrl.getGeocodedAddressSize(); j++) {
48  				final GeocodedAddress gca = gcrl.getGeocodedAddressAt(j);
49  				addressList.add(gca);
50  			}
51  		}
52  		return addressList;
53  	}
54  
55  	
56  
57  
58  
59  
60  
61  
62  
63  
64  	public static List<OpenLSClientAddress> getOpenLSClientAddressList(
65  			final GeocodeResponse gcr, final int... max) {
66  		final List<OpenLSClientAddress> addressList = new ArrayList<>();
67  		final List<GeocodedAddress> gcal = getGeocodedAddressList(gcr);
68  
69  		int listSize = gcal.size();
70  		if (max != null && max.length > 0) {
71  			listSize = (max[0] > listSize) ? listSize : max[0];
72  		}
73  
74  		for (int i = 0; i < listSize; i++) {
75  			final GeocodedAddress gca = gcal.get(i);
76  			final OpenLSClientAddress addr = new OpenLSClientAddress();
77  
78  			if (!gca.hasPoint() || (gca.getPoint().getPosSize() == 0)) {
79  				continue;
80  			}
81  
82  			if (gca.getPoint().getSrsName().equalsIgnoreCase("EPSG:28992")) {
83  				
84  				
85  				
86  				addr.setxCoord((gca.getPoint().getPosAt(0).getX()).intValue()
87  						+ "");
88  				addr.setyCoord((gca.getPoint().getPosAt(0).getY()).intValue()
89  						+ "");
90  			} else {
91  				addr.setxCoord((gca.getPoint().getPosAt(0).getX()).toString());
92  				addr.setyCoord((gca.getPoint().getPosAt(0).getY()).toString());
93  			}
94  
95  			if (gca.getAddress() != null) {
96  				final Address adr = gca.getAddress();
97  				if (adr.hasStreetAddress()
98  						&& adr.getStreetAddress().hasStreet()) {
99  					addr.setStreetName(adr.getStreetAddress().getStreet()
100 							.getStreet());
101 				}
102 				if (adr.hasStreetAddress()
103 						&& adr.getStreetAddress().hasBuilding()
104 						&& adr.getStreetAddress().getBuilding().hasNumber()) {
105 					addr.setStreetNumber(adr.getStreetAddress().getBuilding()
106 							.getNumber());
107 				}
108 				if (adr.hasPostalCode() && adr.getPostalCode().hasPostalCode()) {
109 					addr.setPostalCode(adr.getPostalCode().getPostalCode());
110 				}
111 				if (adr.getPlaceByType(PLACE_TYPE_COUNTRYSUBDIVISION) != null) {
112 					addr.setCountrySubdivision(adr
113 							.getPlaceByType(PLACE_TYPE_COUNTRYSUBDIVISION));
114 				}
115 				if (adr.getPlaceByType(PLACE_TYPE_MUNICIPALITY) != null) {
116 					addr.setMunicipality(adr
117 							.getPlaceByType(PLACE_TYPE_MUNICIPALITY));
118 				}
119 				if (adr.getPlaceByType(PLACE_TYPE_MUNICIPALITYSUBDIVISION) != null) {
120 					addr.setMunicipalitySubdivision(adr
121 							.getPlaceByType(PLACE_TYPE_MUNICIPALITYSUBDIVISION));
122 				}
123 			}
124 			if (addr.isValidClientAddress()) {
125 				addressList.add(addr);
126 			}
127 		}
128 		return addressList;
129 	}
130 }