/
OrderMapper.java
110 lines (99 loc) · 4.02 KB
/
OrderMapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* This work was created by participants in the DataONE project, and is
* jointly copyrighted by participating institutions in DataONE. For
* more information on DataONE, see our web site at http://dataone.org.
*
* Copyright 2019. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dataone.bookkeeper.jdbi.mappers;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.dropwizard.jackson.Jackson;
import org.dataone.bookkeeper.api.Order;
import org.dataone.bookkeeper.api.OrderItem;
import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class OrderMapper implements RowMapper<Order> {
/**
* Construct an OrderMapper
*/
public OrderMapper() {
}
/**
* Map the current row of the result set.
* This method should not cause the result set to advance; allow Jdbi to do that, please.
*
* @param rs the result set being iterated
* @param ctx the statement context
* @return the value to produce for this row
* @throws SQLException if anything goes wrong go ahead and let this percolate; Jdbi will handle it
*/
@Override
public Order map(ResultSet rs, StatementContext ctx) throws SQLException {
ObjectMapper mapper = Jackson.newObjectMapper();
/* The order to return */
Order order;
/* The list of order items */
List<OrderItem> items = new LinkedList<OrderItem>();
/* The list of items as a JSON array */
ArrayNode itemsArray;
try {
itemsArray =
(ArrayNode) mapper.readTree(rs.getString("items"));
Iterator<JsonNode> iterator = itemsArray.elements();
while ( iterator.hasNext() ) {
ObjectNode item = (ObjectNode) iterator.next();
items.add(mapper.readValue(item.toString(), OrderItem.class));
}
order = new Order(
new Integer(rs.getInt("id")),
rs.getString("object"),
new Integer(rs.getInt("amount")),
new Integer(rs.getInt("amountReturned")),
rs.getString("charge") != null ?
(ObjectNode) mapper.readTree(rs.getString("charge")) : null,
new Integer(rs.getInt("created")),
rs.getString("currency"),
rs.getString("subject"),
new Integer(rs.getInt("customer")),
rs.getString("email"),
items,
rs.getString("metadata") != null ?
(ObjectNode) mapper.readTree(rs.getString("metadata")) : null,
rs.getString("name"),
rs.getString("status"),
rs.getString("statusTransitions") != null ?
(ObjectNode) mapper.readTree(rs.getString("statusTransitions")) : null,
new Integer(rs.getInt("updated")),
rs.getString("seriesId"),
new Integer(rs.getInt("startDate")),
new Integer(rs.getInt("endDate")),
null // let the row reducer populate quotas
);
} catch (IOException e) {
throw new SQLException(e);
}
return order;
}
}