Fix license headers
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / openecomp / sdnc / rm / dao / jdbc / ResourceLoadJdbcDaoImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-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.sdnc.rm.dao.jdbc;
23
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;
31
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;
39
40 public class ResourceLoadJdbcDaoImpl implements ResourceLoadJdbcDao {
41
42     @SuppressWarnings("unused")
43     private static final Logger log = LoggerFactory.getLogger(ResourceJdbcDaoImpl.class);
44
45     private static final String INSERT_SQL = "INSERT INTO RESOURCE_LOAD (\n"
46             + "  resource_id, application_id, resource_load_time, resource_expiration_time)\nVALUES (?, ?, ?, ?)";
47
48     private static final String UPDATE_SQL = "UPDATE RESOURCE_LOAD SET\n"
49             + "  resource_load_time = ?, resource_expiration_time = ?\nWHERE resource_id = ?";
50
51     private static final String DELETE_SQL = "DELETE FROM RESOURCE_LOAD WHERE resource_load_id = ?";
52
53     private static final String GET_SQL = "SELECT * FROM RESOURCE_LOAD WHERE resource_id = ?";
54
55     private JdbcTemplate jdbcTemplate;
56     private ResourceLoadRowMapper resourceLoadRowMapper = new ResourceLoadRowMapper();
57
58     @Override
59     public void add(final ResourceLoad rl) {
60         PreparedStatementCreator psc = new PreparedStatementCreator() {
61
62             @Override
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()));
69                 return ps;
70             }
71         };
72         KeyHolder keyHolder = new GeneratedKeyHolder();
73         jdbcTemplate.update(psc, keyHolder);
74         rl.id = keyHolder.getKey().longValue();
75     }
76
77     @Override
78     public void update(ResourceLoad rl) {
79         jdbcTemplate.update(UPDATE_SQL, rl.loadTime, rl.expirationTime, rl.id);
80     }
81
82     @Override
83     public void delete(long id) {
84         jdbcTemplate.update(DELETE_SQL, id);
85     }
86
87     @Override
88     public List<ResourceLoad> getResourceLoads(long resourceId) {
89         if (resourceId <= 0)
90             return Collections.emptyList();
91
92         return jdbcTemplate.query(GET_SQL, new Object[] { resourceId }, resourceLoadRowMapper);
93     }
94
95     private static class ResourceLoadRowMapper implements RowMapper<ResourceLoad> {
96
97         @Override
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");
105             return rl;
106         }
107     }
108
109     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
110         this.jdbcTemplate = jdbcTemplate;
111     }
112 }