1 |
package dk.daoas.daoadresseservice.admin; |
2 |
|
3 |
import java.io.IOException; |
4 |
import java.io.PrintWriter; |
5 |
import java.net.URLEncoder; |
6 |
|
7 |
import javax.servlet.ServletException; |
8 |
import javax.servlet.annotation.WebServlet; |
9 |
import javax.servlet.http.HttpServlet; |
10 |
import javax.servlet.http.HttpServletRequest; |
11 |
import javax.servlet.http.HttpServletResponse; |
12 |
|
13 |
import dk.daoas.daoadresseservice.AddressSearch; |
14 |
import dk.daoas.daoadresseservice.beans.Address; |
15 |
|
16 |
/** |
17 |
* Servlet implementation class CsvUdtraek |
18 |
*/ |
19 |
@WebServlet("/CsvUdtraek") |
20 |
public class CsvUdtraek extends HttpServlet { |
21 |
private static final long serialVersionUID = 1L; |
22 |
|
23 |
|
24 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
25 |
String type = request.getParameter("type"); |
26 |
String str = null; |
27 |
|
28 |
if (type != null) { |
29 |
switch( type ) { |
30 |
case "ikkedaekket": |
31 |
str = udtraekIkkeDaekket(); |
32 |
break; |
33 |
/*case "sammenlign": |
34 |
str = udtraekSammenligning(request); |
35 |
break;*/ |
36 |
default: |
37 |
str = null; |
38 |
break; |
39 |
} |
40 |
} |
41 |
|
42 |
if (str != null) { |
43 |
type = URLEncoder.encode(type, "UTF-8"); |
44 |
response.setContentType("text/csv"); |
45 |
response.setHeader("Content-Disposition", "filename=" + type + ".csv"); |
46 |
response.getWriter().print(str); |
47 |
} else { |
48 |
PrintWriter w = response.getWriter(); |
49 |
response.setContentType("text/html"); |
50 |
w.print("Type parameter is blank or invalid value<br>"); |
51 |
w.print("Type: <a href='CsvUdtraek?type=ikkedaekket'>ikkedaekket</a> <br>"); |
52 |
//w.print("Type: <a href='CsvUdtraek?type=sammenlign&antaldage=5'>sammenlign</a> <br>"); - Deprecated |
53 |
} |
54 |
} |
55 |
|
56 |
/* ******************************************* |
57 |
* |
58 |
*/ |
59 |
private String udtraekIkkeDaekket() { |
60 |
AddressSearch search = (AddressSearch) getServletContext().getAttribute("search"); |
61 |
if (search == null) { |
62 |
return "data struktur ikke indlæst"; |
63 |
} |
64 |
|
65 |
StringBuilder sb = new StringBuilder(); |
66 |
sb.append("gadeid;postnr;vejnavn;husnr;husnrbogstav;distributor\r\n"); |
67 |
|
68 |
for (Address addr : search.getNonCoveredAddresses() ) { |
69 |
sb.append( addr.gadeid ).append(";") |
70 |
.append( addr.postnr ).append(";") |
71 |
.append( addr.vejnavn ).append(";") |
72 |
.append( addr.husnr ).append(";") |
73 |
.append( addr.husnrbogstav ).append(";") |
74 |
.append( addr.distributor != null ? addr.distributor : "" ) |
75 |
.append( "\n" ); |
76 |
} |
77 |
|
78 |
return sb.toString(); |
79 |
} |
80 |
|
81 |
/* ******************************************* |
82 |
* |
83 |
|
84 |
|
85 |
@Deprecated |
86 |
protected String udtraekSammenligning(HttpServletRequest request) throws IOException { |
87 |
|
88 |
AddressSearch search = (AddressSearch) getServletContext().getAttribute("search"); |
89 |
if (search == null) { |
90 |
return "data struktur ikke indlæst"; |
91 |
} |
92 |
|
93 |
int antal_dage = 2; |
94 |
|
95 |
if (request.getParameter("antaldage") != null ) { |
96 |
antal_dage = Integer.parseInt( request.getParameter("antaldage") ); |
97 |
} |
98 |
|
99 |
int antal_nyfejl_glok = 0; |
100 |
int antal_nyfejl_glfejl = 0; |
101 |
int antal_nyok_glfejl = 0; |
102 |
int antal_nyok_glgoogle = 0; |
103 |
int antal_nygoogle_glgoogle = 0; |
104 |
int antal_nyok_glok = 0; |
105 |
|
106 |
try { |
107 |
TimingHelper timer = new TimingHelper(); |
108 |
|
109 |
StringBuilder sb = new StringBuilder(); |
110 |
sb.append("besked;postnr;adresse;gl status;ny status;ny split;ny vej\n"); |
111 |
|
112 |
List<LoggedAddress> addrList = DatabaseLayerImplementation.getLoggedAdresses(antal_dage); |
113 |
|
114 |
for(LoggedAddress addr : addrList) { |
115 |
SearchResult res = search.search("" + addr.postnr, addr.adresse); |
116 |
|
117 |
if (res.status != Status.STATUS_OK) { //Ny service fandt ikke adressen) { |
118 |
|
119 |
if (addr.status != 10 ) {//Hvis gl service kunne finde adressen så er vi ude i en regression |
120 |
|
121 |
appendLogLine("ny fejler, gl ok", sb, addr, res); |
122 |
antal_nyfejl_glok++; |
123 |
|
124 |
} else { |
125 |
appendLogLine("ny fejler, gl fejler !!!", sb, addr, res); |
126 |
antal_nyfejl_glfejl++; |
127 |
} |
128 |
|
129 |
} else { |
130 |
if (addr.status == 10) { |
131 |
appendLogLine("ny OK, gl fejler", sb, addr, res); |
132 |
antal_nyok_glfejl++; |
133 |
|
134 |
} else if (addr.status == 12 ) { |
135 |
|
136 |
if (res.google == false && res.osm == false) { |
137 |
|
138 |
appendLogLine("ny Direkte, gl Google", sb, addr, res); |
139 |
antal_nyok_glgoogle++; |
140 |
} else { |
141 |
appendLogLine("ny Google, gl Google", sb, addr, res); |
142 |
antal_nygoogle_glgoogle++; |
143 |
} |
144 |
} else { |
145 |
antal_nyok_glok++; |
146 |
} |
147 |
} |
148 |
|
149 |
if (res.osm || res.google) { |
150 |
try { |
151 |
Thread.sleep(50);//Back-off external services |
152 |
} catch(InterruptedException e) { |
153 |
// |
154 |
} |
155 |
} |
156 |
|
157 |
} |
158 |
|
159 |
|
160 |
|
161 |
sb.append("Forbrugt tid: ;").append( timer.getElapsed() ).append(";;;\n"); |
162 |
sb.append("SearchData genereret: ;").append( search.getStatistics().buildTime ).append(";;;\n"); |
163 |
sb.append("Sammenligning csv genereret: ;").append( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( new Date() ) ).append(";;;\n"); |
164 |
|
165 |
|
166 |
sb.append("Antal dage: ;").append( antal_dage ).append(";;;\n"); |
167 |
sb.append("Antal adresser testet: ;").append(addrList.size()).append(";;;\n"); |
168 |
double sekunder = timer.getElapsed() / 1000.0; |
169 |
int adresserPrSekund = (int) Math.round( addrList.size() / sekunder ); |
170 |
sb.append("Antal adresser testet/s: ;").append(adresserPrSekund).append(";;;\n"); |
171 |
|
172 |
|
173 |
sb.append("Antal ny ok, gl ok: ;").append( antal_nyok_glok ).append(";;;\n"); |
174 |
sb.append("Antal ny ok, gl google: ;").append( antal_nyok_glgoogle ).append(";;;\n"); |
175 |
sb.append("Antal ny ok, gl fejl: ;").append( antal_nyok_glfejl ).append(";;;\n"); |
176 |
sb.append("Antal ny google, gl google: ;").append( antal_nygoogle_glgoogle ).append(";;;\n"); |
177 |
sb.append("Antal ny fejl, gl ok: ;").append( antal_nyfejl_glok ).append(";;;\n"); |
178 |
sb.append("Antal ny fejl, gl fejl: ;").append( antal_nyfejl_glfejl ).append(";;;\n"); |
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
return sb.toString(); |
185 |
|
186 |
} catch(SQLException e) { |
187 |
throw new IOException(e); |
188 |
} |
189 |
} |
190 |
|
191 |
|
192 |
private void appendLogLine(String msg, StringBuilder sb, LoggedAddress addr, SearchResult res) { |
193 |
|
194 |
sb.append(msg).append(";") |
195 |
.append(addr.postnr).append(";") |
196 |
.append(addr.adresse).append(";") |
197 |
.append(addr.status).append(";") |
198 |
.append(res.status).append(";") |
199 |
.append(res.splitResult).append(";") |
200 |
.append(res.vej) |
201 |
.append("\n"); |
202 |
|
203 |
} |
204 |
*/ |
205 |
|
206 |
} |