|
|
@@ -3,9 +3,9 @@ package com.reporting.db;
|
|
3
|
3
|
import com.reporting.reports.CargoReport;
|
|
4
|
4
|
import com.reporting.reports.VoyageReport;
|
|
5
|
5
|
import org.springframework.dao.EmptyResultDataAccessException;
|
|
6
|
|
-import org.springframework.jdbc.core.JdbcTemplate;
|
|
7
|
6
|
import org.springframework.jdbc.core.RowCallbackHandler;
|
|
8
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
7
|
+import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
|
|
8
|
+import se.citerus.dddsample.reporting.api.CargoDetails;
|
|
9
|
9
|
import se.citerus.dddsample.reporting.api.Handling;
|
|
10
|
10
|
import se.citerus.dddsample.reporting.api.OnboardCargo;
|
|
11
|
11
|
|
|
|
@@ -17,26 +17,25 @@ import java.util.List;
|
|
17
|
17
|
|
|
18
|
18
|
public class ReportDAO {
|
|
19
|
19
|
|
|
20
|
|
- private JdbcTemplate jdbc;
|
|
|
20
|
+ private SimpleJdbcTemplate jdbc;
|
|
21
|
21
|
private HandlingRowMapper handlingRowMapper;
|
|
22
|
22
|
private CargoReportRowMapper cargoReportRowMapper;
|
|
23
|
23
|
private VoyageReportRowMapper rowMapper;
|
|
24
|
24
|
private VoyageCargoRowMapper voyageCargoRowMapper;
|
|
25
|
25
|
|
|
26
|
26
|
public ReportDAO(DataSource dataSource) {
|
|
27
|
|
- this.jdbc = new JdbcTemplate(dataSource);
|
|
|
27
|
+ this.jdbc = new SimpleJdbcTemplate(dataSource);
|
|
28
|
28
|
this.handlingRowMapper = new HandlingRowMapper();
|
|
29
|
29
|
this.cargoReportRowMapper = new CargoReportRowMapper();
|
|
30
|
30
|
this.rowMapper = new VoyageReportRowMapper();
|
|
31
|
31
|
this.voyageCargoRowMapper = new VoyageCargoRowMapper();
|
|
32
|
32
|
}
|
|
33
|
33
|
|
|
34
|
|
- @Transactional(readOnly = true)
|
|
35
|
34
|
public CargoReport loadCargoReport(String trackingId) {
|
|
36
|
35
|
try {
|
|
37
|
36
|
String[] args = {trackingId};
|
|
38
|
37
|
String sql = "select * from cargo where cargo_tracking_id = ?";
|
|
39
|
|
- CargoReport cargoReport = (CargoReport) jdbc.queryForObject(sql, args, cargoReportRowMapper);
|
|
|
38
|
+ CargoReport cargoReport = (CargoReport) jdbc.getJdbcOperations().queryForObject(sql, args, cargoReportRowMapper);
|
|
40
|
39
|
cargoReport.setHandlings(loadHandlings(trackingId));
|
|
41
|
40
|
return cargoReport;
|
|
42
|
41
|
} catch (EmptyResultDataAccessException e) {
|
|
|
@@ -44,12 +43,11 @@ public class ReportDAO {
|
|
44
|
43
|
}
|
|
45
|
44
|
}
|
|
46
|
45
|
|
|
47
|
|
- @Transactional(readOnly = true)
|
|
48
|
46
|
public VoyageReport loadVoyageReport(String voyageNumber) {
|
|
49
|
47
|
String[] args = {voyageNumber};
|
|
50
|
48
|
String sql = "select * from voyage where voyage_number = ?";
|
|
51
|
49
|
try {
|
|
52
|
|
- VoyageReport voyageReport = (VoyageReport) jdbc.queryForObject(sql, args, rowMapper);
|
|
|
50
|
+ VoyageReport voyageReport = (VoyageReport) jdbc.getJdbcOperations().queryForObject(sql, args, rowMapper);
|
|
53
|
51
|
voyageReport.setOnboardCargos(loadOnboardCargos(voyageNumber));
|
|
54
|
52
|
return voyageReport;
|
|
55
|
53
|
} catch (EmptyResultDataAccessException e) {
|
|
|
@@ -57,6 +55,68 @@ public class ReportDAO {
|
|
57
|
55
|
}
|
|
58
|
56
|
}
|
|
59
|
57
|
|
|
|
58
|
+ public void storeCargoDetals(CargoDetails cargoDetails) {
|
|
|
59
|
+ int count = jdbc.queryForInt("select count(*) from cargo where cargo_tracking_id = ?", cargoDetails.getTrackingId());
|
|
|
60
|
+
|
|
|
61
|
+ String sql;
|
|
|
62
|
+ Object[] params;
|
|
|
63
|
+ if (count > 0) {
|
|
|
64
|
+ sql =
|
|
|
65
|
+ "update cargo set " +
|
|
|
66
|
+ "received_in = ?," +
|
|
|
67
|
+ "destination = ?," +
|
|
|
68
|
+ "arrival_deadline = ?," +
|
|
|
69
|
+ "eta = ?," +
|
|
|
70
|
+ "current_status = ?," +
|
|
|
71
|
+ "current_voyage_number = ?," +
|
|
|
72
|
+ "current_location = ?," +
|
|
|
73
|
+ "last_updated_on = ? " +
|
|
|
74
|
+ "where cargo_tracking_id = ?";
|
|
|
75
|
+ params = new Object[] {
|
|
|
76
|
+ cargoDetails.getReceivedIn(),
|
|
|
77
|
+ cargoDetails.getFinalDestination(),
|
|
|
78
|
+ cargoDetails.getArrivalDeadline(),
|
|
|
79
|
+ cargoDetails.getEta(),
|
|
|
80
|
+ cargoDetails.getCurrentStatus(),
|
|
|
81
|
+ cargoDetails.getCurrentVoyage(),
|
|
|
82
|
+ cargoDetails.getCurrentLocation(),
|
|
|
83
|
+ cargoDetails.getLastUpdatedOn(),
|
|
|
84
|
+ cargoDetails.getTrackingId()
|
|
|
85
|
+ };
|
|
|
86
|
+ } else {
|
|
|
87
|
+ sql =
|
|
|
88
|
+ "insert into cargo (" +
|
|
|
89
|
+ "cargo_tracking_id," +
|
|
|
90
|
+ "received_in," +
|
|
|
91
|
+ "destination," +
|
|
|
92
|
+ "arrival_deadline," +
|
|
|
93
|
+ "eta," +
|
|
|
94
|
+ "current_status," +
|
|
|
95
|
+ "current_voyage_number," +
|
|
|
96
|
+ "current_location," +
|
|
|
97
|
+ "last_updated_on) " +
|
|
|
98
|
+ "values (?,?,?,?,?,?,?,?,?)";
|
|
|
99
|
+ params = new Object[] {
|
|
|
100
|
+ cargoDetails.getTrackingId(),
|
|
|
101
|
+ cargoDetails.getReceivedIn(),
|
|
|
102
|
+ cargoDetails.getFinalDestination(),
|
|
|
103
|
+ cargoDetails.getArrivalDeadline(),
|
|
|
104
|
+ cargoDetails.getEta(),
|
|
|
105
|
+ cargoDetails.getCurrentStatus(),
|
|
|
106
|
+ cargoDetails.getCurrentVoyage(),
|
|
|
107
|
+ cargoDetails.getCurrentLocation(),
|
|
|
108
|
+ cargoDetails.getLastUpdatedOn()
|
|
|
109
|
+ };
|
|
|
110
|
+ }
|
|
|
111
|
+
|
|
|
112
|
+ jdbc.update(sql, params);
|
|
|
113
|
+ }
|
|
|
114
|
+
|
|
|
115
|
+ public void storeHandling(String trackingId, Handling handling) {
|
|
|
116
|
+ String sql = "insert into handling (cargo_tracking_id,type,location,voyage_number,completed_on) values (?,?,?,?,?)";
|
|
|
117
|
+ jdbc.update(sql, trackingId, handling.getType(), handling.getLocation(), handling.getVoyage(), handling.getCompletedOn());
|
|
|
118
|
+ }
|
|
|
119
|
+
|
|
60
|
120
|
private List<Handling> loadHandlings(String trackingId) {
|
|
61
|
121
|
final List<Handling> handlings = new ArrayList<Handling>();
|
|
62
|
122
|
RowCallbackHandler handler = new RowCallbackHandler() {
|
|
|
@@ -67,7 +127,7 @@ public class ReportDAO {
|
|
67
|
127
|
};
|
|
68
|
128
|
String[] args = {trackingId};
|
|
69
|
129
|
String sql = "select * from handling where cargo_tracking_id = ?";
|
|
70
|
|
- jdbc.query(sql, args, handler);
|
|
|
130
|
+ jdbc.getJdbcOperations().query(sql, args, handler);
|
|
71
|
131
|
return handlings;
|
|
72
|
132
|
}
|
|
73
|
133
|
|
|
|
@@ -75,7 +135,7 @@ public class ReportDAO {
|
|
75
|
135
|
final List<OnboardCargo> onboardCargos = new ArrayList<OnboardCargo>();
|
|
76
|
136
|
String[] args = {voyageNumber};
|
|
77
|
137
|
String sql = "select cargo_tracking_id, destination from cargo where current_voyage_number = ?";
|
|
78
|
|
- jdbc.query(sql, args, new RowCallbackHandler() {
|
|
|
138
|
+ jdbc.getJdbcOperations().query(sql, args, new RowCallbackHandler() {
|
|
79
|
139
|
@Override
|
|
80
|
140
|
public void processRow(ResultSet rs) throws SQLException {
|
|
81
|
141
|
onboardCargos.add(voyageCargoRowMapper.mapRow(rs, rs.getRow()));
|