1 |
package dk.daoas.daoadresseservice; |
package dk.daoas.daoadresseservice; |
2 |
|
|
3 |
|
import java.sql.SQLException; |
4 |
import java.util.HashMap; |
import java.util.HashMap; |
5 |
|
import java.util.List; |
6 |
|
import java.util.Map; |
7 |
|
import java.util.concurrent.ConcurrentHashMap; |
8 |
|
|
9 |
|
import dk.daoas.daoadresseservice.AddressUtils.SplitResult; |
10 |
|
import dk.daoas.daoadresseservice.SearchResult.Status; |
11 |
|
import dk.daoas.daoadresseservice.db.DatabaseLayer; |
12 |
|
|
13 |
public class AdressSearch { |
public class AdressSearch { |
14 |
|
|
15 |
HashMap<Integer, HashMap<String,Long>> searchPostnrVejnavnDavid = new HashMap<Integer, HashMap<String,Long>>(); |
Map<Integer, Map<String,Long>> searchPostnrVejnavnGadeid; |
16 |
|
Map<Long, Map<String,Address>> searchGadeidentAdresser; |
17 |
|
|
18 |
|
List<Address> alleAdresser; |
19 |
|
|
20 |
|
/* Mapper mellem db Row ID og adresse noden */ |
21 |
|
Map<Integer,Address> idAddressMap; |
22 |
|
|
23 |
|
|
24 |
public void search(int postnr, String adresse) throws SearchException { |
public SearchResult search(String postnrStr, String adresse) { |
25 |
HashMap<String,Long> postnrVeje = searchPostnrVejnavnDavid.get(postnr); |
int postnr=0; |
26 |
|
|
27 |
|
try { |
28 |
|
postnr = Integer.parseInt(postnrStr); |
29 |
|
} catch (Exception E) { |
30 |
|
return new SearchResult(Status.ERROR_UNKNOWN_POSTAL); |
31 |
|
} |
32 |
|
|
33 |
|
Map<String,Long> postnrVeje = searchPostnrVejnavnGadeid.get(postnr); |
34 |
|
|
35 |
if (postnrVeje == null) { |
if (postnrVeje == null) { |
36 |
throw new SearchException("Ukendt postnr"); |
return new SearchResult(Status.ERROR_UNKNOWN_POSTAL); |
37 |
} |
} |
38 |
|
|
39 |
|
SplitResult split = AddressUtils.splitAdresse(adresse); |
40 |
|
String vasketVejnavn = AddressUtils.vaskVejnavn( split.vej ); |
41 |
|
|
42 |
|
if (split.husnr.length() == 0) { |
43 |
|
return new SearchResult(Status.ERROR_MISSING_HOUSENUMBER); |
44 |
|
} |
45 |
|
|
46 |
|
Long gadeident = postnrVeje.get(vasketVejnavn); |
47 |
|
if (gadeident == null) { |
48 |
|
return new SearchResult(Status.ERROR_UNKNOWN_STREETNAME); |
49 |
|
} |
50 |
|
|
51 |
|
Map<String, Address> gade = searchGadeidentAdresser.get(gadeident); //Denne søgning må ikke fejle |
52 |
|
|
53 |
|
|
54 |
|
String husnrSearch = "" + split.husnr + split.litra; |
55 |
|
Address addr = gade.get(husnrSearch); |
56 |
|
if (addr == null) { |
57 |
|
return new SearchResult(Status.ERROR_UNKNOWN_ADDRESSPOINT); |
58 |
|
} |
59 |
|
|
60 |
|
|
61 |
|
return new SearchResult(addr); |
62 |
|
} |
63 |
|
|
64 |
|
|
65 |
|
public void buildSearchStructures() throws SQLException{ |
66 |
|
searchPostnrVejnavnGadeid = new HashMap<Integer, Map<String,Long>>(); |
67 |
|
searchGadeidentAdresser = new HashMap<Long, Map<String,Address>>(); |
68 |
|
idAddressMap = new HashMap<Integer,Address>(); |
69 |
|
|
70 |
|
alleAdresser = DatabaseLayer.getAllAdresses(); |
71 |
|
for (Address a : alleAdresser) { |
72 |
|
idAddressMap.put(a.id, a); |
73 |
|
|
74 |
|
Map<String,Long> postnrVeje = searchPostnrVejnavnGadeid.get(a.postnr); |
75 |
|
|
76 |
|
if (postnrVeje == null) { |
77 |
|
postnrVeje = new ConcurrentHashMap<String,Long>(); |
78 |
|
searchPostnrVejnavnGadeid.put(a.postnr, postnrVeje); |
79 |
|
} |
80 |
|
|
81 |
|
Long gadeident = postnrVeje.get(a.vasketVejnavn); |
82 |
|
if (gadeident == null) { |
83 |
|
postnrVeje.put(a.vasketVejnavn, a.gadeid); |
84 |
|
} |
85 |
|
|
86 |
|
Map<String, Address> gade = searchGadeidentAdresser.get(a.gadeid); |
87 |
|
if (gade == null) { |
88 |
|
gade = new HashMap<String, Address>(); |
89 |
|
searchGadeidentAdresser.put(a.gadeid, gade); |
90 |
|
} |
91 |
|
String husnrSearch = "" + a.husnr + a.husnrbogstav; |
92 |
|
gade.put(husnrSearch, a); |
93 |
|
|
94 |
|
|
95 |
|
} |
96 |
|
|
97 |
} |
} |
98 |
|
|
99 |
} |
} |