瀏覽代碼

Improved PDF reports, added ability to search for voyages, setting last update timestamp on cargo reports at store time, added sample data, improved look of web page.

peter_backlund 16 年之前
父節點
當前提交
1f65afeb13

+ 2
- 1
dddsample/external/reporting/src/main/java/com/reporting/db/ReportDAO.java 查看文件

@@ -13,6 +13,7 @@ import javax.sql.DataSource;
13 13
 import java.sql.ResultSet;
14 14
 import java.sql.SQLException;
15 15
 import java.util.ArrayList;
16
+import java.util.Date;
16 17
 import java.util.List;
17 18
 
18 19
 public class ReportDAO {
@@ -80,7 +81,7 @@ public class ReportDAO {
80 81
         cargoDetails.getCurrentStatus(),
81 82
         cargoDetails.getCurrentVoyage(),
82 83
         cargoDetails.getCurrentLocation(),
83
-        cargoDetails.getLastUpdatedOn(),
84
+        new Date(),
84 85
         cargoDetails.getTrackingId()
85 86
       };
86 87
     } else {

+ 5
- 1
dddsample/external/reporting/src/main/java/com/reporting/pdf/PDFCargoReportProvider.java 查看文件

@@ -28,14 +28,17 @@ public class PDFCargoReportProvider extends PDFMessageBodyWriter<CargoReport> {
28 28
     try {
29 29
       CargoDetails cargoDetails = cargoReport.getCargo();
30 30
       document.add(new Paragraph("Cargo " + cargoDetails.getTrackingId(), getFont(HELVETICA_BOLD, 18.0f)));
31
+      document.add(new Paragraph(" "));
31 32
       document.add(new Paragraph("Status: " + cargoDetails.getCurrentStatus()));
32 33
       document.add(new Paragraph("Location: " + cargoDetails.getCurrentLocation()));
33 34
       document.add(new Paragraph("Voyage: " + cargoDetails.getCurrentVoyage()));
34 35
       document.add(new Paragraph("Destination: " + cargoDetails.getFinalDestination()));
35 36
       document.add(new Paragraph("Arrival deadline: " + cargoDetails.getArrivalDeadline()));
36 37
       document.add(new Paragraph("ETA: " + cargoDetails.getEta()));
37
-
38
+      document.add(new Paragraph(" "));
38 39
       document.add(new Paragraph("Handling history", getFont(HELVETICA_BOLD)));
40
+      document.add(new Paragraph(" "));
41
+
39 42
       PdfPTable table = new PdfPTable(4);
40 43
       for (Handling handling : cargoReport.getHandlings()) {
41 44
         table.addCell(handling.getType());
@@ -43,6 +46,7 @@ public class PDFCargoReportProvider extends PDFMessageBodyWriter<CargoReport> {
43 46
         table.addCell(handling.getVoyage());
44 47
         table.addCell(handling.getCompletedOnAsString());
45 48
       }
49
+      document.add(table);
46 50
     } catch (DocumentException e) {
47 51
       throw new RuntimeException(e);
48 52
     }

+ 23
- 1
dddsample/external/reporting/src/main/java/com/reporting/pdf/PDFVoyageReportProvider.java 查看文件

@@ -3,11 +3,17 @@ package com.reporting.pdf;
3 3
 import com.lowagie.text.Document;
4 4
 import com.lowagie.text.DocumentException;
5 5
 import com.lowagie.text.Paragraph;
6
+import com.lowagie.text.pdf.PdfPTable;
6 7
 import com.reporting.reports.VoyageReport;
8
+import se.citerus.dddsample.reporting.api.OnboardCargo;
9
+import se.citerus.dddsample.reporting.api.VoyageDetails;
7 10
 
8 11
 import javax.ws.rs.Produces;
9 12
 import javax.ws.rs.ext.Provider;
10 13
 
14
+import static com.lowagie.text.FontFactory.HELVETICA_BOLD;
15
+import static com.lowagie.text.FontFactory.getFont;
16
+
11 17
 @Produces("application/pdf")
12 18
 @Provider
13 19
 public class PDFVoyageReportProvider extends PDFMessageBodyWriter<VoyageReport> {
@@ -20,7 +26,23 @@ public class PDFVoyageReportProvider extends PDFMessageBodyWriter<VoyageReport>
20 26
   @Override
21 27
   protected void buildDocument(VoyageReport voyageReport, Document document) {
22 28
     try {
23
-      document.add(new Paragraph("Voyage " + voyageReport.getVoyage().getVoyageNumber()));
29
+      VoyageDetails voyage = voyageReport.getVoyage();
30
+      document.add(new Paragraph("Voyage " + voyage.getVoyageNumber(), getFont(HELVETICA_BOLD, 18.0f)));
31
+      document.add(new Paragraph(" "));
32
+      document.add(new Paragraph("Status: " + voyage.getCurrentStatus()));
33
+      document.add(new Paragraph("Next stop: " + voyage.getNextStop() + " at " + voyage.getEtaNextStopAsString()));
34
+      document.add(new Paragraph("Delay: " + voyage.getDelayedByMinutes() + " minutes"));
35
+      document.add(new Paragraph("Last updated: " + voyage.getLastUpdatedOnAsString()));
36
+      document.add(new Paragraph(" "));
37
+      document.add(new Paragraph("Onboard cargos", getFont(HELVETICA_BOLD)));
38
+      document.add(new Paragraph(" "));
39
+
40
+      PdfPTable table = new PdfPTable(2);
41
+      for (OnboardCargo cargo : voyageReport.getOnboardCargos()) {
42
+        table.addCell(cargo.getTrackingId());
43
+        table.addCell(cargo.getFinalDestination());
44
+      }
45
+      document.add(table);
24 46
     } catch (DocumentException e) {
25 47
       throw new RuntimeException(e);
26 48
     }

+ 33
- 10
dddsample/external/reporting/src/main/resources/sample_data.sql 查看文件

@@ -43,15 +43,38 @@ insert into cargo (
43 43
   current_location,
44 44
   last_updated_on
45 45
 ) values (
46
-  'DEF',
47
-  'Hongkong',
48
-  'Stockholm',
49
-  '2009-06-15 12:00:00.000',
50
-  '2009-06-12 18:30:00.000',
51
-  'In port',
52
-  null,
53
-  'Tokyo',
54
-  '2009-06-08 14:23:12.123'
46
+  'C0217', 'Hongkong', 'Hamburg', '2009-06-15 12:00:00.000',
47
+  '2009-06-12 18:30:00.000', 'Onboard voyage', 'V0200', null, '2009-06-08 14:23:12.123'
48
+);
49
+
50
+insert into cargo (
51
+  cargo_tracking_id,
52
+  received_in,
53
+  destination,
54
+  arrival_deadline,
55
+  eta,
56
+  current_status,
57
+  current_voyage_number,
58
+  current_location,
59
+  last_updated_on
60
+) values (
61
+  'C0219', 'Hongkong', 'Helsinki', '2009-06-15 12:00:00.000',
62
+  '2009-06-12 18:30:00.000', 'Onboard voyage', 'V0200', null, '2009-06-08 14:23:12.123'
63
+);
64
+
65
+insert into cargo (
66
+  cargo_tracking_id,
67
+  received_in,
68
+  destination,
69
+  arrival_deadline,
70
+  eta,
71
+  current_status,
72
+  current_voyage_number,
73
+  current_location,
74
+  last_updated_on
75
+) values (
76
+  'C0301', 'Hongkong', 'Rotterdam', '2009-06-15 12:00:00.000',
77
+  '2009-06-12 18:30:00.000', 'Onboard voyage', 'V0200', null, '2009-06-08 14:23:12.123'
55 78
 );
56 79
 
57 80
 
@@ -85,4 +108,4 @@ insert into voyage (
85 108
   'In transit',
86 109
   '0',
87 110
   '2009-06-06 14:01:19.901'
88
-);
111
+);

二進制
dddsample/external/reporting/src/main/webapp/background.png 查看文件


二進制
dddsample/external/reporting/src/main/webapp/dddsample.png 查看文件


+ 1
- 1
dddsample/external/reporting/src/main/webapp/index.jsp 查看文件

@@ -17,7 +17,7 @@
17 17
     <div id="form">
18 18
       <form action="" method="get" onsubmit="search(); return false;">
19 19
         Track cargo or voyage:
20
-        <input id="query" name="query" type="text" size="30" value="ABC"/>
20
+        <input id="query" name="query" type="text" value="ABC"/>
21 21
         <input id="submit" name="submit" type="submit" value="Track"/>
22 22
       </form>
23 23
     </div>

+ 1
- 0
dddsample/external/reporting/src/main/webapp/main.css 查看文件

@@ -57,6 +57,7 @@ div#footer {
57 57
 input#query {
58 58
   padding: 5px;
59 59
   font-size: large;
60
+  width: 250px;
60 61
 }
61 62
 
62 63
 input#submit {

+ 105
- 46
dddsample/external/reporting/src/main/webapp/reporting.js 查看文件

@@ -1,56 +1,115 @@
1
+var cargoResult = 0;
2
+var voyageResult= 0;
3
+var cargoJson = '';
4
+var voyageJson = '';
5
+
1 6
 function search() {
2
-  $('#result').empty();
3 7
   if (!$("#query").val()) {
8
+    $('#result').empty();
4 9
     return;
5 10
   }
6
-  
11
+
12
+  cargoResult = 0;
13
+  voyageResult= 0;
14
+  cargoJson = '';
15
+  voyageJson = '';
16
+
7 17
   $.ajax({
8 18
       url: 'http://localhost:8080/reporting/rest/cargo/' + $("#query").val() + '.json',
9 19
       type: 'GET',
10 20
       contentType: 'application/json',
11
-      success: function(response) {
12
-        var json = eval('(' + response + ')');
13
-        var cargo = json.cargoReport.cargo;
14
-        var pdfUrl = 'http://localhost:8080/reporting/rest/cargo/' + $("#query").val() + '.pdf';
15
-        $('#result').append('<h2>Cargo: ' + cargo.trackingId + '</h2>').append('<table>');
16
-
17
-        if (cargo.currentLocation) {
18
-          $('#result').append('<tr><td>Status</td><td>' + cargo.currentStatus + ' ' + cargo.currentLocation + '</td></tr>');
19
-        } else if (cargo.currentVoyage) {
20
-          $('#result').append('<tr><td>Status</td><td>' + cargo.currentStatus + ' ' + cargo.currentVoyage + '</td></tr>');
21
-        }
22
-
23
-        $('#result').
24
-          append('<tr><td>Destination</td><td>' + cargo.finalDestination + ' by ' + cargo.arrivalDeadline + '</td></tr>').
25
-          append('<tr><td>ETA</td><td>' + cargo.eta + '</td></tr>');
26
-
27
-
28
-        $('#result').
29
-          append('<tr><td>Updated on</td><td>' + cargo.lastUpdatedOn + '</td></tr>').
30
-          append('</table>');
31
-
32
-        $('#result').append(json.cargoReport.misdirected);
33
-
34
-        $('#result').
35
-          append('<h2>Handling history</h2>');
36
-        for (i in json.cargoReport.handlings) {
37
-          var handling = json.cargoReport.handlings[i];
38
-          $('#result').
39
-            append('<tr>').
40
-            append('<td>' + handling.completedOn + '</td>').
41
-            append('<td>' + handling.type + '</td>').
42
-            append('<td>' + handling.voyage + '</td>').
43
-            append('<td>' + handling.location + '</td>').
44
-            append('</tr>');
45
-        }
46
-        $('#result').
47
-          append('</table>').
48
-          append('<p><a href="' + pdfUrl + '">Download as PDF</a></p>');
49
-      },
50
-      error: function(response) {
51
-        if (response.status == 404) {
52
-          $('#result').append('<p>No cargo found with tracking ID ' + $("#query").val() + '</p>');
53
-        }
54
-      }
21
+      success: function(response) { cargoResult = 1; cargoJson = eval('(' + response + ')'); present(response); },
22
+      error: function(response) { cargoResult = 2; present(response); }
55 23
   });
24
+
25
+  $.ajax({
26
+      url: 'http://localhost:8080/reporting/rest/voyage/' + $("#query").val() + '.json',
27
+      type: 'GET',
28
+      contentType: 'application/json',
29
+      success: function(response) { voyageResult = 1; voyageJson = eval('(' + response + ')'); present(response); },
30
+      error: function(response) { voyageResult = 2; present(response); }
31
+  });
32
+
33
+}
34
+
35
+function present(response) {
36
+  if (cargoResult == 1 && voyageResult > 0) {
37
+    presentCargo();
38
+  } else if (cargoResult == 2 && voyageResult == 1) {
39
+    presentVoyage();
40
+  } else if (cargoResult == 2 && voyageResult == 2) {
41
+    err(response);
42
+  } else {
43
+    // alert(cargoResult + ':' + voyageResult);
44
+  }
45
+}
46
+
47
+function presentCargo(response) {
48
+  var cargo = cargoJson.cargoReport.cargo;
49
+  var pdfUrl = 'http://localhost:8080/reporting/rest/cargo/' + $("#query").val() + '.pdf';
50
+  $('#result').empty();
51
+  $('#result').append('<h2>Cargo: ' + cargo.trackingId + '</h2>').append('<table>');
52
+
53
+  if (cargo.currentLocation) {
54
+    $('#result').append('<tr><td>Status</td><td>' + cargo.currentStatus + ' ' + cargo.currentLocation + '</td></tr>');
55
+  } else if (cargo.currentVoyage) {
56
+    $('#result').append('<tr><td>Status</td><td>' + cargo.currentStatus + ' ' + cargo.currentVoyage + '</td></tr>');
57
+  }
58
+
59
+  $('#result').
60
+    append('<tr><td>Destination</td><td>' + cargo.finalDestination + ' by ' + cargo.arrivalDeadline + '</td></tr>').
61
+    append('<tr><td>ETA</td><td>' + cargo.eta + '</td></tr>');
62
+
63
+  $('#result').
64
+    append('<tr><td>Updated on</td><td>' + cargo.lastUpdatedOn + '</td></tr>').
65
+    append('</table>');
66
+
67
+  $('#result').append(cargoJson.cargoReport.misdirected);
68
+
69
+  $('#result').
70
+    append('<h2>Handling history</h2><table>');
71
+  for (i in cargoJson.cargoReport.handlings) {
72
+    var handling = cargoJson.cargoReport.handlings[i];
73
+    $('#result').
74
+      append('<tr>').
75
+      append('<td>' + handling.completedOn + '</td>').
76
+      append('<td>' + handling.type + '</td>').
77
+      append('<td>' + (handling.voyage ? handling.voyage : '') + '</td>').
78
+      append('<td>' + handling.location + '</td>').
79
+      append('</tr>');
80
+  }
81
+  $('#result').
82
+    append('</table>').
83
+    append('<p><a href="' + pdfUrl + '">Download as PDF</a></p>');
56 84
 }
85
+
86
+function presentVoyage(response) {
87
+  var voyage = voyageJson.voyageReport.voyage;
88
+  var pdfUrl = 'http://localhost:8080/reporting/rest/voyage/' + $("#query").val() + '.pdf';
89
+
90
+  $('#result').empty();
91
+  $('#result').
92
+    append('<h2>Voyage: ' + voyage.voyageNumber + '</h2>').append('<table>').
93
+    append('<tr><td>Status</td><td>' + voyage.currentStatus + '</td></tr>').
94
+    append('<tr><td>Next stop</td><td>' + voyage.nextStop + ' at ' + voyage.etaNextStop + '</td></tr>').
95
+    append('<tr><td>Delay</td><td>' + voyage.delayedByMinutes + ' min</td></tr>').
96
+    append('<tr><td>Updated on</td><td>' + voyage.lastUpdatedOn + '</td></tr>').
97
+    append('</table>').
98
+    append('<h2>Onboard cargos</h2><table>');
99
+  for (i in voyageJson.voyageReport.onboardCargos) {
100
+    var cargo = voyageJson.voyageReport.onboardCargos[i];
101
+    $('#result').append('<tr><td>' + cargo.trackingId + '</td><td>' + cargo.finalDestination + '</td></tr>');
102
+  }
103
+  $('#result').
104
+    append('</table>').
105
+    append('<p><a href="' + pdfUrl + '">Download as PDF</a></p>');
106
+}
107
+
108
+function err(response) {
109
+  if (response.status == 404) {
110
+    if (cargoResult == 2 && voyageResult == 2) {
111
+      $('#result').empty();
112
+      $('#result').append('<p>No cargo or voyage found</p>');
113
+    }
114
+  }
115
+}