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.util.Collections;
29 import java.util.List;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.jdbc.core.JdbcTemplate;
34 import org.springframework.jdbc.core.PreparedStatementCreator;
35 import org.springframework.jdbc.core.RowMapper;
36 import org.springframework.jdbc.support.GeneratedKeyHolder;
37 import org.springframework.jdbc.support.KeyHolder;
39 public class ResourceJdbcDaoImpl implements ResourceJdbcDao {
41 @SuppressWarnings("unused")
42 private static final Logger log = LoggerFactory.getLogger(ResourceJdbcDaoImpl.class);
44 private static final String RESOURCE_SQL = "SELECT * FROM RESOURCE WHERE asset_id = ? AND resource_name = ?";
46 private static final String RESOURCE_SET_SQL = "SELECT * FROM RESOURCE WHERE resource_id IN (\n"
47 + "SELECT DISTINCT resource_id FROM ALLOCATION_ITEM WHERE resource_set_id = ?)";
49 private static final String RESOURCE_UNION_SQL = "SELECT * FROM RESOURCE WHERE resource_id IN (\n"
50 + "SELECT DISTINCT resource_id FROM ALLOCATION_ITEM WHERE resource_union_id = ?)";
52 private static final String INSERT_SQL = "INSERT INTO RESOURCE (\n"
53 + " asset_id, resource_name, resource_type, lt_used, ll_label, ll_reference_count, rr_used)\n"
54 + "VALUES (?, ?, ?, ?, ?, ?, ?)";
56 private static final String UPDATE_SQL = "UPDATE RESOURCE SET\n"
57 + " lt_used = ?, ll_label = ?, ll_reference_count = ?, rr_used = ?\nWHERE resource_id = ?";
59 private static final String DELETE_SQL = "DELETE FROM RESOURCE WHERE resource_id = ?";
61 private JdbcTemplate jdbcTemplate;
62 private ResourceRowMapper resourceRowMapper = new ResourceRowMapper();
65 public Resource getResource(String assetId, String resourceName) {
66 if (assetId == null || assetId.trim().length() == 0 || resourceName == null ||
67 resourceName.trim().length() == 0)
70 List<Resource> ll = jdbcTemplate.query(RESOURCE_SQL, new Object[] { assetId, resourceName }, resourceRowMapper);
71 return ll.isEmpty() ? null : ll.get(0);
75 public List<Resource> getResourceSet(String resourceSetId) {
76 if (resourceSetId == null)
77 return Collections.emptyList();
79 return jdbcTemplate.query(RESOURCE_SET_SQL, new Object[] { resourceSetId }, resourceRowMapper);
83 public List<Resource> getResourceUnion(String resourceUnionId) {
84 if (resourceUnionId == null)
85 return Collections.emptyList();
87 return jdbcTemplate.query(RESOURCE_UNION_SQL, new Object[] { resourceUnionId }, resourceRowMapper);
91 public void add(final Resource r) {
92 PreparedStatementCreator psc = new PreparedStatementCreator() {
95 public PreparedStatement createPreparedStatement(Connection dbc) throws SQLException {
96 PreparedStatement ps = dbc.prepareStatement(INSERT_SQL, new String[] { "resource_id" });
97 ps.setString(1, r.assetId);
98 ps.setString(2, r.name);
99 ps.setString(3, r.type);
100 ps.setLong(4, r.ltUsed);
101 ps.setString(5, r.llLabel);
102 ps.setInt(6, r.llReferenceCount);
103 ps.setString(7, r.rrUsed);
107 KeyHolder keyHolder = new GeneratedKeyHolder();
108 jdbcTemplate.update(psc, keyHolder);
109 r.id = keyHolder.getKey().longValue();
113 public void update(Resource r) {
114 Long ltUsed = r.ltUsed <= 0 ? null : r.ltUsed;
115 Integer llRefCount = r.llReferenceCount <= 0 ? null : r.llReferenceCount;
116 jdbcTemplate.update(UPDATE_SQL, ltUsed, r.llLabel, llRefCount, r.rrUsed, r.id);
120 public void delete(long id) {
121 jdbcTemplate.update(DELETE_SQL, id);
124 private static class ResourceRowMapper implements RowMapper<Resource> {
127 public Resource mapRow(ResultSet rs, int arg1) throws SQLException {
128 Resource r = new Resource();
129 r.id = rs.getLong("resource_id");
130 r.assetId = rs.getString("asset_id");
131 r.name = rs.getString("resource_name");
132 r.type = rs.getString("resource_type");
133 r.ltUsed = rs.getLong("lt_used");
134 r.llLabel = rs.getString("ll_label");
135 r.llReferenceCount = rs.getInt("ll_reference_count");
136 r.rrUsed = rs.getString("rr_used");
141 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
142 this.jdbcTemplate = jdbcTemplate;