67cfb355a6be46a92499d639509539b4f6089990
[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
21 import javax.persistence.PersistenceException;
22
23 import org.apache.ibatis.session.SqlSession;
24 import org.apache.ibatis.session.SqlSessionFactory;
25 import org.onap.vnfsdk.marketplace.db.connection.ConnectionUtil;
26 import org.onap.vnfsdk.marketplace.db.entity.PackageData;
27 import org.onap.vnfsdk.marketplace.db.inf.IMarketplaceDao;
28 import org.onap.vnfsdk.marketplace.db.mapper.IMarketplaceMapper;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * This class is the implementation for the DAO Layer for Driver Manager.
34  * <br/>
35  * 
36  * @author
37  * @version  
38  */
39 public class MarketplaceDaoImpl implements IMarketplaceDao {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(MarketplaceDaoImpl.class);
42
43     private SqlSessionFactory sqlSessionFactory = null;
44
45     /**
46      * 
47      * Constructor<br/>
48      * <p>
49      * </p>
50      * 
51      * @since   
52      */
53     public MarketplaceDaoImpl() {
54         sqlSessionFactory = ConnectionUtil.getSession();
55     }
56
57     /**
58      * get all package data.
59      * <br/>
60      * 
61      * @return
62      * @since    
63      */
64     public List<PackageData> getAllPackageData() {
65         SqlSession session = sqlSessionFactory.openSession();
66         List<PackageData> csars = null; 
67         try {
68             IMarketplaceMapper mapper = session.getMapper(IMarketplaceMapper.class);
69             csars = mapper.getAllPackageData();
70             session.commit();
71         } catch(PersistenceException e) {
72             LOGGER.error("Exception caught {}", e);
73 //            throw new DriverManagerException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
74 //                    ErrorCode.INVALID_DB);
75         } finally {
76             session.close();
77         }
78         return csars;
79     }
80
81     /**
82      * saving the package data object to the DB using the mybatis.
83      * <br/>
84      * 
85      * @param dirverInstance
86      * @since    
87      */
88         public void savePackageData(PackageData lPackageData) {
89                 SqlSession session = sqlSessionFactory.openSession();
90         try {
91             IMarketplaceMapper mapper = session.getMapper(IMarketplaceMapper.class);
92             mapper.savePackageData(lPackageData);
93             session.commit();
94         } catch(PersistenceException e) {
95             LOGGER.error("Exception caught {}", e);
96 //            throw new DriverManagerException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
97 //                    ErrorCode.INVALID_DB);
98         } finally {
99             session.close();
100         }
101                 
102         }
103
104         public List<PackageData> getPackageData(String csarId) {
105         SqlSession session = sqlSessionFactory.openSession();
106         List<PackageData> csars = null; 
107         try {
108             IMarketplaceMapper mapper = session.getMapper(IMarketplaceMapper.class);
109             csars = mapper.getPackageData(csarId);
110             session.commit();
111         } catch(PersistenceException e) {
112             LOGGER.error("Exception caught {}", e);
113 //            throw new DriverManagerException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
114 //                    ErrorCode.INVALID_DB);
115         } finally {
116             session.close();
117         }
118         return csars;
119         }
120
121         public void deletePackageData(String csarId) {
122          SqlSession session = sqlSessionFactory.openSession();
123         try {
124             IMarketplaceMapper mapper = session.getMapper(IMarketplaceMapper.class);
125             mapper.deletePackageData(csarId);
126             session.commit();
127         } catch(PersistenceException e) {
128             LOGGER.error("Exception caught {}", e);
129 //                  throw new DriverManagerException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
130 //                          ErrorCode.INVALID_DB);
131         } finally {
132             session.close();
133         }
134                 
135         }
136
137     public void updatePackageData(PackageData oPackageData) {
138         SqlSession session = sqlSessionFactory.openSession();
139         try {
140             IMarketplaceMapper mapper = session.getMapper(IMarketplaceMapper.class);
141             mapper.updatePackageData(oPackageData);
142             session.commit();
143         } catch(PersistenceException e) {
144             LOGGER.error("Exception caught {}", e);
145         } finally {
146             session.close();
147         }
148         
149     }    
150 }