79b08afd1d80af22d2a2da58bcf8010fad2da4e3
[vnfsdk/refrepo.git] /
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     /**
47      *
48      * Constructor<br/>
49      * <p>
50      * </p>
51      *
52      * @since
53      */
54     public MarketplaceDaoImpl() {
55         sqlSessionFactory = ConnectionUtil.getSession();
56     }
57
58     /**
59      * get all package data.
60      * <br/>
61      *
62      * @return
63      * @since
64      */
65     @Override
66     public List<PackageData> getAllPackageData() {
67         SqlSession session = sqlSessionFactory.openSession();
68         List<PackageData> csars = null;
69         try {
70             IMarketplaceMapper mapper = session.getMapper(IMarketplaceMapper.class);
71             csars = mapper.getAllPackageData();
72             session.commit();
73         } catch(PersistenceException e) {
74             LOGGER.error("Exception caught {}", e);
75         } finally {
76             session.close();
77         }
78         return csars;
79     }
80
81     @Override
82     public List<PackageData> getPackageDataSubset(Map<String, String> paramsMap) {
83         SqlSession session = sqlSessionFactory.openSession();
84         List<PackageData> csars = null;
85         try {
86             IMarketplaceMapper mapper = session.getMapper(IMarketplaceMapper.class);
87             csars = mapper.getPackageDataSubset(paramsMap);
88             session.commit();
89         } catch(PersistenceException e) {
90             LOGGER.error("Exception caught {}", e);
91         } finally {
92             session.close();
93         }
94         return csars;
95     }
96
97     /**
98      * saving the package data object to the DB using the mybatis.
99      * <br/>
100      *
101      * @param dirverInstance
102      * @since
103      */
104     @Override
105     public void savePackageData(PackageData lPackageData) {
106         SqlSession session = sqlSessionFactory.openSession();
107         try {
108             IMarketplaceMapper mapper = session.getMapper(IMarketplaceMapper.class);
109             mapper.savePackageData(lPackageData);
110             session.commit();
111         } catch(PersistenceException e) {
112             LOGGER.error("Exception caught {}", e);
113         } finally {
114             session.close();
115         }
116
117     }
118
119     @Override
120     public List<PackageData> getPackageData(String csarId) {
121         SqlSession session = sqlSessionFactory.openSession();
122         List<PackageData> csars = null;
123         try {
124             IMarketplaceMapper mapper = session.getMapper(IMarketplaceMapper.class);
125             csars = mapper.getPackageData(csarId);
126             session.commit();
127         } catch(PersistenceException e) {
128             LOGGER.error("Exception caught {}", e);
129         } finally {
130             session.close();
131         }
132         return csars;
133     }
134
135     @Override
136     public void deletePackageData(String csarId) {
137      SqlSession session = sqlSessionFactory.openSession();
138         try {
139             IMarketplaceMapper mapper = session.getMapper(IMarketplaceMapper.class);
140             mapper.deletePackageData(csarId);
141             session.commit();
142         } catch(PersistenceException e) {
143             LOGGER.error("Exception caught {}", e);;
144         } finally {
145             session.close();
146         }
147
148     }
149
150     @Override
151     public void updatePackageData(PackageData oPackageData) {
152         SqlSession session = sqlSessionFactory.openSession();
153         try {
154             IMarketplaceMapper mapper = session.getMapper(IMarketplaceMapper.class);
155             mapper.updatePackageData(oPackageData);
156             session.commit();
157         } catch(PersistenceException e) {
158             LOGGER.error("Exception caught {}", e);
159         } finally {
160             session.close();
161         }
162
163     }
164 }
165