Code Improvements-Vnfsdk-refrepo sonar issue fixes
[vnfsdk/refrepo.git] / vnfmarket-be / vnf-sdk-marketplace / src / main / java / org / onap / vnfsdk / marketplace / db / impl / MarketplaceDaoImpl.java
1 /**
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.vnfsdk.marketplace.db.impl;
18
19 import java.util.List;
20 import java.util.Map;
21
22 import javax.persistence.PersistenceException;
23
24 import org.apache.ibatis.session.SqlSession;
25 import org.apache.ibatis.session.SqlSessionFactory;
26 import org.onap.vnfsdk.marketplace.db.connection.ConnectionUtil;
27 import org.onap.vnfsdk.marketplace.db.entity.PackageData;
28 import org.onap.vnfsdk.marketplace.db.inf.IMarketplaceDao;
29 import org.onap.vnfsdk.marketplace.db.mapper.IMarketplaceMapper;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * This class is the implementation for the DAO Layer for Driver Manager.
35  * <br/>
36  *
37  * @author
38  * @version
39  */
40 public class MarketplaceDaoImpl implements IMarketplaceDao {
41
42     private static final Logger LOGGER = LoggerFactory.getLogger(MarketplaceDaoImpl.class);
43
44     private SqlSessionFactory sqlSessionFactory = null;
45
46     private static final String EXCEPTION = "Exception caught {}";
47
48     /**
49      *
50      * Constructor<br/>
51      * <p>
52      * </p>
53      *
54      * @since
55      */
56     public MarketplaceDaoImpl() {
57         sqlSessionFactory = ConnectionUtil.getSession();
58     }
59
60     /**
61      * get all package data.
62      * <br/>
63      *
64      * @return
65      * @since
66      */
67     @Override
68     public List<PackageData> getAllPackageData() {
69         SqlSession session = sqlSessionFactory.openSession();
70         List<PackageData> csars = null;
71         try {
72             IMarketplaceMapper mapper = session.getMapper(IMarketplaceMapper.class);
73             csars = mapper.getAllPackageData();
74             session.commit();
75         } catch(PersistenceException e) {
76             LOGGER.error(EXCEPTION, e);
77         } finally {
78             session.close();
79         }
80         return csars;
81     }
82
83     @Override
84     public List<PackageData> getPackageDataSubset(Map<String, String> paramsMap) {
85         SqlSession session = sqlSessionFactory.openSession();
86         List<PackageData> csars = null;
87         try {
88             IMarketplaceMapper mapper = session.getMapper(IMarketplaceMapper.class);
89             csars = mapper.getPackageDataSubset(paramsMap);
90             session.commit();
91         } catch(PersistenceException e) {
92             LOGGER.error(EXCEPTION, e);
93         } finally {
94             session.close();
95         }
96         return csars;
97     }
98
99     /**
100      * saving the package data object to the DB using the mybatis.
101      * <br/>
102      *
103      * @param dirverInstance
104      * @since
105      */
106     @Override
107     public void savePackageData(PackageData lPackageData) {
108         SqlSession session = sqlSessionFactory.openSession();
109         try {
110             IMarketplaceMapper mapper = session.getMapper(IMarketplaceMapper.class);
111             mapper.savePackageData(lPackageData);
112             session.commit();
113         } catch(PersistenceException e) {
114             LOGGER.error(EXCEPTION, e);
115         } finally {
116             session.close();
117         }
118
119     }
120
121     @Override
122     public List<PackageData> getPackageData(String csarId) {
123         SqlSession session = sqlSessionFactory.openSession();
124         List<PackageData> csars = null;
125         try {
126             IMarketplaceMapper mapper = session.getMapper(IMarketplaceMapper.class);
127             csars = mapper.getPackageData(csarId);
128             session.commit();
129         } catch(PersistenceException e) {
130             LOGGER.error(EXCEPTION, e);
131         } finally {
132             session.close();
133         }
134         return csars;
135     }
136
137     @Override
138     public void deletePackageData(String csarId) {
139      SqlSession session = sqlSessionFactory.openSession();
140         try {
141             IMarketplaceMapper mapper = session.getMapper(IMarketplaceMapper.class);
142             mapper.deletePackageData(csarId);
143             session.commit();
144         } catch(PersistenceException e) {
145             LOGGER.error(EXCEPTION, e);
146         } finally {
147             session.close();
148         }
149
150     }
151
152     @Override
153     public void updatePackageData(PackageData oPackageData) {
154         SqlSession session = sqlSessionFactory.openSession();
155         try {
156             IMarketplaceMapper mapper = session.getMapper(IMarketplaceMapper.class);
157             mapper.updatePackageData(oPackageData);
158             session.commit();
159         } catch(PersistenceException e) {
160             LOGGER.error(EXCEPTION, e);
161         } finally {
162             session.close();
163         }
164
165     }
166 }
167