[CCSDK-6] Populate seed code
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / openecomp / sdnc / rm / dao / jdbc / AllocationItemJdbcDaoImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 ONAP 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 AllocationItemJdbcDaoImpl implements AllocationItemJdbcDao {
41
42         @SuppressWarnings("unused")
43         private static final Logger log = LoggerFactory.getLogger(ResourceJdbcDaoImpl.class);
44
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 (?, ?, ?, ?, ?, ?, ?, ?, ?)";
48
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 = ?";
52
53         private static final String DELETE_SQL = "DELETE FROM ALLOCATION_ITEM WHERE allocation_item_id = ?";
54
55         private static final String GET_SQL = "SELECT * FROM ALLOCATION_ITEM WHERE resource_id = ?";
56
57         private JdbcTemplate jdbcTemplate;
58         private AllocationItemRowMapper allocationItemRowMapper = new AllocationItemRowMapper();
59
60         @Override
61         public void add(final AllocationItem ai) {
62                 PreparedStatementCreator psc = new PreparedStatementCreator() {
63
64                         @Override
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()));
76                                 return ps;
77                         }
78                 };
79                 KeyHolder keyHolder = new GeneratedKeyHolder();
80                 jdbcTemplate.update(psc, keyHolder);
81                 ai.id = keyHolder.getKey().longValue();
82         }
83
84         @Override
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,
88                         ai.id);
89         }
90
91         @Override
92         public void delete(long id) {
93                 jdbcTemplate.update(DELETE_SQL, id);
94         }
95
96         @Override
97         public List<AllocationItem> getAllocationItems(long resourceId) {
98                 if (resourceId <= 0)
99                         return Collections.emptyList();
100
101                 return jdbcTemplate.query(GET_SQL, new Object[] { resourceId }, allocationItemRowMapper);
102         }
103
104         private static class AllocationItemRowMapper implements RowMapper<AllocationItem> {
105
106                 @Override
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");
119                         return ai;
120                 }
121         }
122
123         public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
124                 this.jdbcTemplate = jdbcTemplate;
125         }
126 }