1601177c91026bb25298e68e217dbb06cb102a8d
[appc.git] / appc-dispatcher / appc-dispatcher-common / transaction-recorder / src / main / java / org / openecomp / appc / transactionrecorder / impl / TransactionRecorderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.transactionrecorder.impl;
26
27 import org.openecomp.appc.dao.util.DBUtils;
28 import org.openecomp.appc.transactionrecorder.TransactionRecorder;
29 import org.openecomp.appc.transactionrecorder.objects.TransactionRecord;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import java.sql.Connection;
34 import java.sql.PreparedStatement;
35 import java.sql.SQLException;
36
37
38
39 public class TransactionRecorderImpl implements TransactionRecorder {
40
41     private static String APPCCTL_SCHEMA = "appcctl";
42
43     private static final Logger logger = LoggerFactory.getLogger(TransactionRecorderImpl.class);
44
45     /**
46      * Stores transaction record to appc database by calling APPC Dao layer.
47      * @param record Transaction record data.
48      */
49     @Override
50     public void store(TransactionRecord record) {
51         Connection connection = null;
52         PreparedStatement stmt = null;
53         String queryString = "INSERT INTO transactions VALUES (?,?,?,?,?,?,?,?,?,?)";
54         try {
55             if (logger.isDebugEnabled()) {
56                 logger.debug("Transaction Data started Inserting Successfully into DB");
57             }
58             connection = DBUtils.getConnection(APPCCTL_SCHEMA);
59             stmt = connection.prepareStatement(queryString);
60             stmt.setTimestamp(1, new java.sql.Timestamp(record.getTimeStamp().toEpochMilli()));
61             stmt.setString(2, record.getRequestID());
62             stmt.setTimestamp(3, new java.sql.Timestamp(record.getStartTime().toEpochMilli()));
63             stmt.setTimestamp(4, new java.sql.Timestamp(record.getEndTime().toEpochMilli()));
64             stmt.setString(5, record.getTargetID());
65             stmt.setString(6, record.getTargetType());
66             stmt.setString(7, record.getSubComponent());
67             stmt.setString(8, record.getOperation());
68             stmt.setString(9, record.getResultCode());
69             stmt.setString(10, record.getDescription());
70             stmt.execute();
71             if (logger.isDebugEnabled()) {
72                 logger.debug("Transaction Data Inserted Successfully into DB");
73             }
74         } catch (SQLException e) {
75             logger.error("Error Accessing Database " + e);
76             throw new RuntimeException(e);
77         } finally {
78             DBUtils.clearResources(null, stmt, connection);
79         }
80     }
81 }