88d9c846a3ad83761b0ad7dda5bb56d3624591a1
[ccsdk/features.git] /
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
5  * in compliance with the License. You may obtain a copy of the License at\r
6  * \r
7  * http://www.apache.org/licenses/LICENSE-2.0\r
8  * \r
9  * Unless required by applicable law or agreed to in writing, software distributed under the License\r
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
11  * or implied. See the License for the specific language governing permissions and limitations under\r
12  * the License.\r
13  */\r
14 \r
15 package org.onap.ccsdk.config.data.adaptor.dao;\r
16 \r
17 import java.sql.ResultSet;\r
18 import java.sql.SQLException;\r
19 import java.util.List;\r
20 import org.apache.commons.lang3.StringUtils;\r
21 import org.onap.ccsdk.config.data.adaptor.domain.TransactionLog;\r
22 import org.onap.ccsdk.sli.core.sli.SvcLogicException;\r
23 import org.springframework.jdbc.core.JdbcTemplate;\r
24 import org.springframework.jdbc.core.RowMapper;\r
25 import com.att.eelf.configuration.EELFLogger;\r
26 import com.att.eelf.configuration.EELFManager;\r
27 \r
28 public class TransactionLogDaoImpl implements TransactionLogDao {\r
29     \r
30     private static EELFLogger logger = EELFManager.getInstance().getLogger(TransactionLogDaoImpl.class);\r
31     \r
32     private JdbcTemplate jdbcTemplate;\r
33     \r
34     public TransactionLogDaoImpl(JdbcTemplate jdbcTemplate) {\r
35         this.jdbcTemplate = jdbcTemplate;\r
36     }\r
37     \r
38     @Override\r
39     public void save(TransactionLog transactionLog) throws SvcLogicException {\r
40         if (transactionLog != null && StringUtils.isNotBlank(transactionLog.getRequestId())) {\r
41             String addSql =\r
42                     "INSERT INTO CONFIG_TRANSACTION_LOG ( config_transaction_log_id, request_id, message_type, message ) VALUES (?, ?, ?, ?) ";\r
43             jdbcTemplate.update(addSql, transactionLog.getUniqueId(), transactionLog.getRequestId(),\r
44                     transactionLog.getMessageType(), transactionLog.getMessage());\r
45             logger.trace("TransactionLog Updated Successfully for message_type {}", transactionLog.getMessageType());\r
46         }\r
47         \r
48     }\r
49     \r
50     @Override\r
51     public List<TransactionLog> getTransactionsByRequestId(String requestId) throws SvcLogicException {\r
52         if (StringUtils.isNotBlank(requestId)) {\r
53             String selectByRequestIdSql =\r
54                     "SELECT * FROM CONFIG_TRANSACTION_LOG WHERE request_id = ? ORDER BY creation_date DESC";\r
55             return this.jdbcTemplate.query(selectByRequestIdSql, new Object[] {requestId}, new TransactionLogMapper());\r
56         } else {\r
57             throw new SvcLogicException("TransactionLog Request id  (" + requestId + ")is missing ");\r
58         }\r
59     }\r
60     \r
61     @Override\r
62     public List<TransactionLog> getTransactionsByRequestId(String requestId, String messageType)\r
63             throws SvcLogicException {\r
64         if (StringUtils.isNotBlank(requestId)) {\r
65             String selectByRequestIdSql =\r
66                     "SELECT * FROM CONFIG_TRANSACTION_LOG WHERE request_id = ? and message_type = ? ORDER BY creation_date DESC";\r
67             return this.jdbcTemplate.query(selectByRequestIdSql, new Object[] {requestId, messageType},\r
68                     new TransactionLogMapper());\r
69         } else {\r
70             throw new SvcLogicException("TransactionLog Request id  (" + requestId + ")is missing ");\r
71         }\r
72     }\r
73     \r
74     private static final class TransactionLogMapper implements RowMapper<TransactionLog> {\r
75         @Override\r
76         public TransactionLog mapRow(ResultSet rs, int rowNum) throws SQLException {\r
77             TransactionLog transactionLog = new TransactionLog();\r
78             transactionLog.setCreationDate(rs.getDate("creation_date"));\r
79             transactionLog.setMessage(rs.getString("message"));\r
80             transactionLog.setMessageType(rs.getString("message_type"));\r
81             transactionLog.setRequestId(rs.getString("request_id"));\r
82             transactionLog.setTransactionLogId(rs.getString("config_transaction_log_id"));\r
83             return transactionLog;\r
84         }\r
85     }\r
86     \r
87 }\r