[CCSDK-6] Populate seed code
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / openecomp / sdnc / lock / dao / ResourceLockDaoImpl.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.lock.dao;
23
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26 import java.util.Date;
27 import java.util.List;
28
29 import org.openecomp.sdnc.lock.data.ResourceLock;
30 import org.openecomp.sdnc.util.db.CachedDataSourceWrap;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.jdbc.core.JdbcTemplate;
34 import org.springframework.jdbc.core.RowMapper;
35
36 public class ResourceLockDaoImpl implements ResourceLockDao {
37
38         private static final Logger log = LoggerFactory.getLogger(ResourceLockDaoImpl.class);
39
40         private JdbcTemplate jdbcTemplate;
41         private boolean testing = false;
42
43         @Override
44         public void lockTable() {
45                 if (!testing) {
46                         jdbcTemplate.update("LOCK TABLES RESOURCE_LOCK WRITE");
47                 log.info("Table RESOURCE_LOCK locked.");
48         }
49         }
50
51         @Override
52         public void unlockTable() {
53                 if (!testing) {
54                         jdbcTemplate.update("UNLOCK TABLES");
55                 log.info("Table RESOURCE_LOCK unlocked.");
56
57                         CachedDataSourceWrap ds = (CachedDataSourceWrap) jdbcTemplate.getDataSource();
58                         ds.releaseConnection();
59                 }
60         }
61
62         @Override
63         public void add(ResourceLock l) {
64                 jdbcTemplate.update(
65                         "INSERT INTO RESOURCE_LOCK (resource_name, lock_holder, lock_count, lock_time, expiration_time)\n" +
66                                 "VALUES (?, ?, ?, ?, ?)",
67                         new Object[] { l.resourceName, l.lockHolder, l.lockCount, l.lockTime, l.expirationTime });
68         }
69
70         @Override
71         public void update(long id, Date lockTime, Date expirationTime, int lockCount) {
72                 jdbcTemplate.update(
73                         "UPDATE RESOURCE_LOCK SET lock_time = ?, expiration_time = ?, lock_count = ? WHERE resource_lock_id = ?",
74                         new Object[] { lockTime, expirationTime, lockCount, id });
75         }
76
77         @Override
78         public ResourceLock getByResourceName(String resourceName) {
79                 List<ResourceLock> ll = jdbcTemplate.query("SELECT * FROM RESOURCE_LOCK WHERE resource_name = ?",
80                         new Object[] { resourceName }, new RowMapper<ResourceLock>() {
81
82                                 @Override
83                                 public ResourceLock mapRow(ResultSet rs, int rowNum) throws SQLException {
84                                         ResourceLock rl = new ResourceLock();
85                                         rl.id = rs.getLong("resource_lock_id");
86                                         rl.resourceName = rs.getString("resource_name");
87                                         rl.lockHolder = rs.getString("lock_holder");
88                                         rl.lockCount = rs.getInt("lock_count");
89                                         rl.lockTime = rs.getTimestamp("lock_time");
90                                         rl.expirationTime = rs.getTimestamp("expiration_time");
91                                         return rl;
92                                 }
93                         });
94                 return ll != null && !ll.isEmpty() ? ll.get(0) : null;
95         }
96
97         @Override
98         public void delete(long id) {
99                 jdbcTemplate.update("DELETE FROM RESOURCE_LOCK WHERE resource_lock_id = ?", new Object[] { id });
100         }
101
102         @Override
103         public void decrementLockCount(long id) {
104                 jdbcTemplate.update("UPDATE RESOURCE_LOCK SET lock_count = lock_count - 1 WHERE resource_lock_id = ?",
105                         new Object[] { id });
106         }
107
108         public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
109                 this.jdbcTemplate = jdbcTemplate;
110         }
111
112         public void setTesting(boolean testing) {
113                 this.testing = testing;
114         }
115 }