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