2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.sdnc.rm.dao.jdbc;
24 import java.sql.Connection;
25 import java.sql.PreparedStatement;
26 import java.sql.ResultSet;
27 import java.sql.SQLException;
28 import java.sql.Timestamp;
29 import java.util.Collections;
30 import java.util.List;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.jdbc.core.JdbcTemplate;
35 import org.springframework.jdbc.core.PreparedStatementCreator;
36 import org.springframework.jdbc.core.RowMapper;
37 import org.springframework.jdbc.support.GeneratedKeyHolder;
38 import org.springframework.jdbc.support.KeyHolder;
40 public class ResourceLoadJdbcDaoImpl implements ResourceLoadJdbcDao {
42 @SuppressWarnings("unused")
43 private static final Logger log = LoggerFactory.getLogger(ResourceJdbcDaoImpl.class);
45 private static final String INSERT_SQL = "INSERT INTO RESOURCE_LOAD (\n"
46 + " resource_id, application_id, resource_load_time, resource_expiration_time)\nVALUES (?, ?, ?, ?)";
48 private static final String UPDATE_SQL = "UPDATE RESOURCE_LOAD SET\n"
49 + " resource_load_time = ?, resource_expiration_time = ?\nWHERE resource_id = ?";
51 private static final String DELETE_SQL = "DELETE FROM RESOURCE_LOAD WHERE resource_load_id = ?";
53 private static final String GET_SQL = "SELECT * FROM RESOURCE_LOAD WHERE resource_id = ?";
55 private JdbcTemplate jdbcTemplate;
56 private ResourceLoadRowMapper resourceLoadRowMapper = new ResourceLoadRowMapper();
59 public void add(final ResourceLoad rl) {
60 PreparedStatementCreator psc = new PreparedStatementCreator() {
63 public PreparedStatement createPreparedStatement(Connection dbc) throws SQLException {
64 PreparedStatement ps = dbc.prepareStatement(INSERT_SQL, new String[] { "resource_load_id" });
65 ps.setLong(1, rl.resourceId);
66 ps.setString(2, rl.applicationId);
67 ps.setTimestamp(3, new Timestamp(rl.loadTime.getTime()));
68 ps.setTimestamp(4, new Timestamp(rl.expirationTime.getTime()));
72 KeyHolder keyHolder = new GeneratedKeyHolder();
73 jdbcTemplate.update(psc, keyHolder);
74 rl.id = keyHolder.getKey().longValue();
78 public void update(ResourceLoad rl) {
79 jdbcTemplate.update(UPDATE_SQL, rl.loadTime, rl.expirationTime, rl.id);
83 public void delete(long id) {
84 jdbcTemplate.update(DELETE_SQL, id);
88 public List<ResourceLoad> getResourceLoads(long resourceId) {
90 return Collections.emptyList();
92 return jdbcTemplate.query(GET_SQL, new Object[] { resourceId }, resourceLoadRowMapper);
95 private static class ResourceLoadRowMapper implements RowMapper<ResourceLoad> {
98 public ResourceLoad mapRow(ResultSet rs, int n) throws SQLException {
99 ResourceLoad rl = new ResourceLoad();
100 rl.id = rs.getLong("allocation_item_id");
101 rl.resourceId = rs.getLong("resource_id");
102 rl.applicationId = rs.getString("application_id");
103 rl.loadTime = rs.getTimestamp("resource_load_time");
104 rl.expirationTime = rs.getTimestamp("resource_expiration_time");
109 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
110 this.jdbcTemplate = jdbcTemplate;