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.lock.dao;
 
  24 import java.sql.ResultSet;
 
  25 import java.sql.SQLException;
 
  26 import java.util.Date;
 
  27 import java.util.List;
 
  29 import org.onap.ccsdk.sli.adaptors.lock.data.ResourceLock;
 
  30 import org.onap.ccsdk.sli.adaptors.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;
 
  36 public class ResourceLockDaoImpl implements ResourceLockDao {
 
  38     private static final Logger log = LoggerFactory.getLogger(ResourceLockDaoImpl.class);
 
  40     private JdbcTemplate jdbcTemplate;
 
  41     private boolean testing = false;
 
  44     public void lockTable() {
 
  46             jdbcTemplate.update("LOCK TABLES RESOURCE_LOCK WRITE");
 
  47         log.info("Table RESOURCE_LOCK locked.");
 
  52     public void unlockTable() {
 
  54             jdbcTemplate.update("UNLOCK TABLES");
 
  55         log.info("Table RESOURCE_LOCK unlocked.");
 
  57             CachedDataSourceWrap ds = (CachedDataSourceWrap) jdbcTemplate.getDataSource();
 
  58             ds.releaseConnection();
 
  63     public void add(ResourceLock l) {
 
  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 });
 
  71     public void update(long id, Date lockTime, Date expirationTime, int lockCount) {
 
  73                 "UPDATE RESOURCE_LOCK SET lock_time = ?, expiration_time = ?, lock_count = ? WHERE resource_lock_id = ?",
 
  74                 new Object[] { lockTime, expirationTime, lockCount, id });
 
  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>() {
 
  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");
 
  94         return ll != null && !ll.isEmpty() ? ll.get(0) : null;
 
  98     public void delete(long id) {
 
  99         jdbcTemplate.update("DELETE FROM RESOURCE_LOCK WHERE resource_lock_id = ?", new Object[] { id });
 
 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 });
 
 108     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
 
 109         this.jdbcTemplate = jdbcTemplate;
 
 112     public void setTesting(boolean testing) {
 
 113         this.testing = testing;