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.onap.ccsdk.sli.adaptors.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 AllocationItemJdbcDaoImpl implements AllocationItemJdbcDao {
 
  42     @SuppressWarnings("unused")
 
  43     private static final Logger log = LoggerFactory.getLogger(ResourceJdbcDaoImpl.class);
 
  45     private static final String INSERT_SQL = "INSERT INTO ALLOCATION_ITEM (\n"
 
  46             + "  resource_id, application_id, resource_set_id, resource_union_id, resource_share_group_list,\n"
 
  47             + "  lt_used, ll_label, rr_used, allocation_time)\nVALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
 
  49     private static final String UPDATE_SQL = "UPDATE ALLOCATION_ITEM SET\n"
 
  50             + "  resource_share_group_list = ?, lt_used = ?, ll_label = ?, rr_used = ?, allocation_time = ?\n"
 
  51             + "WHERE allocation_item_id = ?";
 
  53     private static final String DELETE_SQL = "DELETE FROM ALLOCATION_ITEM WHERE allocation_item_id = ?";
 
  55     private static final String GET_SQL = "SELECT * FROM ALLOCATION_ITEM WHERE resource_id = ?";
 
  57     private JdbcTemplate jdbcTemplate;
 
  58     private AllocationItemRowMapper allocationItemRowMapper = new AllocationItemRowMapper();
 
  61     public void add(final AllocationItem ai) {
 
  62         PreparedStatementCreator psc = new PreparedStatementCreator() {
 
  65             public PreparedStatement createPreparedStatement(Connection dbc) throws SQLException {
 
  66                 PreparedStatement ps = dbc.prepareStatement(INSERT_SQL, new String[] { "allocation_item_id" });
 
  67                 ps.setLong(1, ai.resourceId);
 
  68                 ps.setString(2, ai.applicationId);
 
  69                 ps.setString(3, ai.resourceSetId);
 
  70                 ps.setString(4, ai.resourceUnionId);
 
  71                 ps.setString(5, ai.resourceShareGroupList);
 
  72                 ps.setLong(6, ai.ltUsed);
 
  73                 ps.setString(7, ai.llLabel);
 
  74                 ps.setString(8, ai.rrUsed);
 
  75                 ps.setTimestamp(9, new Timestamp(ai.allocationTime.getTime()));
 
  79         KeyHolder keyHolder = new GeneratedKeyHolder();
 
  80         jdbcTemplate.update(psc, keyHolder);
 
  81         ai.id = keyHolder.getKey().longValue();
 
  85     public void update(AllocationItem ai) {
 
  86         Long ltUsed = ai.ltUsed <= 0 ? null : ai.ltUsed;
 
  87         jdbcTemplate.update(UPDATE_SQL, ai.resourceShareGroupList, ltUsed, ai.llLabel, ai.rrUsed, ai.allocationTime,
 
  92     public void delete(long id) {
 
  93         jdbcTemplate.update(DELETE_SQL, id);
 
  97     public List<AllocationItem> getAllocationItems(long resourceId) {
 
  99             return Collections.emptyList();
 
 101         return jdbcTemplate.query(GET_SQL, new Object[] { resourceId }, allocationItemRowMapper);
 
 104     private static class AllocationItemRowMapper implements RowMapper<AllocationItem> {
 
 107         public AllocationItem mapRow(ResultSet rs, int n) throws SQLException {
 
 108             AllocationItem ai = new AllocationItem();
 
 109             ai.id = rs.getLong("allocation_item_id");
 
 110             ai.resourceId = rs.getLong("resource_id");
 
 111             ai.applicationId = rs.getString("application_id");
 
 112             ai.resourceSetId = rs.getString("resource_set_id");
 
 113             ai.resourceUnionId = rs.getString("resource_union_id");
 
 114             ai.resourceShareGroupList = rs.getString("resource_share_group_list");
 
 115             ai.ltUsed = rs.getLong("lt_used");
 
 116             ai.llLabel = rs.getString("ll_label");
 
 117             ai.rrUsed = rs.getString("rr_used");
 
 118             ai.allocationTime = rs.getTimestamp("allocation_time");
 
 123     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
 
 124         this.jdbcTemplate = jdbcTemplate;