Error of "no transaction is in progress" at Service Creation from UUI
[usecase-ui/server.git] / server / src / main / java / org / onap / usecaseui / server / service / impl / PerformanceInformationServiceImpl.java
1 /*\r
2  * Copyright (C) 2017 CMCC, Inc. and others. All rights reserved.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 package org.onap.usecaseui.server.service.impl;\r
17 \r
18 \r
19 import java.util.ArrayList;\r
20 import java.util.Date;\r
21 import java.util.List;\r
22 \r
23 \r
24 import javax.persistence.EntityManagerFactory;\r
25 import javax.transaction.Transactional;\r
26 \r
27 import com.google.common.base.Throwables;\r
28 import org.hibernate.Query;\r
29 import org.hibernate.Session;\r
30 import org.hibernate.SessionFactory;\r
31 import org.hibernate.Transaction;\r
32 import org.onap.usecaseui.server.bean.PerformanceInformation;\r
33 import org.onap.usecaseui.server.service.PerformanceInformationService;\r
34 import org.onap.usecaseui.server.util.UuiCommonUtil;\r
35 import org.slf4j.Logger;\r
36 import org.slf4j.LoggerFactory;\r
37 import org.springframework.beans.factory.annotation.Autowired;\r
38 import org.springframework.context.annotation.EnableAspectJAutoProxy;\r
39 import org.springframework.stereotype.Service;\r
40 \r
41 \r
42 @Service("PerformanceInformationService")\r
43 @Transactional\r
44 @org.springframework.context.annotation.Configuration\r
45 @EnableAspectJAutoProxy\r
46 public class PerformanceInformationServiceImpl implements PerformanceInformationService {\r
47 \r
48         private static final Logger logger = LoggerFactory.getLogger(PerformanceInformationServiceImpl.class);\r
49 \r
50         @Autowired\r
51         private EntityManagerFactory entityManagerFactory;\r
52 \r
53         public Session getSession() {\r
54                 return entityManagerFactory.unwrap(SessionFactory.class).getCurrentSession();}\r
55 \r
56         @Override\r
57         public String savePerformanceInformation(PerformanceInformation performanceInformation) {\r
58                 Session session = getSession();\r
59                 try {\r
60                         if (null == performanceInformation) {\r
61                         }\r
62                         Transaction tx = session.beginTransaction();\r
63                         session.save(performanceInformation);\r
64                         tx.commit();\r
65                         session.flush();\r
66                         return "1";\r
67                 } catch (Exception e) {\r
68                         logger.error("exception occurred while performing PerformanceInformationServiceImpl savePerformanceInformation. Details:" + e.getMessage());\r
69                         return "0";\r
70                 }\r
71         }\r
72 \r
73         @Override\r
74         public String updatePerformanceInformation(PerformanceInformation performanceInformation) {\r
75                 Session session = getSession();\r
76                 try {\r
77                         if (null == performanceInformation) {\r
78                         }\r
79                         logger.info("PerformanceInformationServiceImpl updatePerformanceInformation: performanceInformation={}", performanceInformation);\r
80                         Transaction tx = session.beginTransaction();\r
81                         session.update(performanceInformation);\r
82                         tx.commit();\r
83                         session.flush();\r
84                         return "1";\r
85                 } catch (Exception e) {\r
86                         logger.error("exception occurred while performing PerformanceInformationServiceImpl updatePerformanceInformation. Details:" + e.getMessage());\r
87                         return "0";\r
88                 }\r
89         }\r
90 \r
91         @SuppressWarnings("unchecked")\r
92         @Override\r
93         public List<PerformanceInformation> queryId(String[] id) {\r
94                 Session session = getSession();\r
95                 try {\r
96                         if(id.length==0) {\r
97                         }\r
98                         List<PerformanceInformation> list = new ArrayList<>();\r
99                         Query query = session.createQuery("from PerformanceInformation a where a.sourceId IN (:alist)");\r
100                         list = query.setParameterList("alist", id).list();\r
101                         return list;\r
102                 } catch (Exception e) {\r
103                         logger.error("exception occurred while performing PerformanceInformationServiceImpl queryId. Details:" + Throwables.getStackTraceAsString(e));\r
104                         logger.error("exception occurred while performing PerformanceInformationServiceImpl queryId. Details:" + e.getMessage());\r
105                         return null;\r
106                 }\r
107         }\r
108 \r
109         @SuppressWarnings("unchecked")\r
110         @Override\r
111         public List<PerformanceInformation> queryDateBetween(String sourceId, Date startDate, Date endDate) {\r
112                 Session session = getSession();\r
113                 try {\r
114                         List<PerformanceInformation> list = new ArrayList<>();\r
115                         Query query = session.createQuery("from PerformanceInformation a where a.sourceId = :sourceId and a.createTime BETWEEN :startDate and :endDate");\r
116                         list = query.setParameter("sourceId",sourceId).setParameter("startDate", startDate).setParameter("endDate",endDate).list();\r
117                         return list;\r
118                 } catch (Exception e) {\r
119                         logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDateBetween. Details:" + e.getMessage());\r
120                         return null;\r
121                 }\r
122         }\r
123 \r
124         @SuppressWarnings("unchecked")\r
125         @Override\r
126         public int queryDataBetweenSum(String sourceId, String name, Date startDate, Date endDate){\r
127                 Session session = getSession();\r
128                 try {\r
129                         int sum = 0;\r
130                         Query query = session.createQuery("select sum(a.value) from PerformanceInformation a where a.sourceId = :sourceId and a.name = :name and a.createTime BETWEEN :startDate and :endDate");\r
131                         sum = Integer.parseInt(query.setParameter("sourceId",sourceId).setParameter("name",name).setParameter("startDate", startDate).setParameter("endDate",endDate).uniqueResult().toString());\r
132                         return sum;\r
133                 } catch (Exception e) {\r
134                         logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDataBetweenSum. Details:" + e.getMessage());\r
135                         return 0;\r
136                 }\r
137         }\r
138 \r
139         @Override\r
140         public List<PerformanceInformation> queryDateBetween(String resourceId, String name, String startTime, String endTime) {\r
141                 Session session = getSession();\r
142                 try {\r
143                         String hql = "from PerformanceInformation a where 1=1 ";\r
144                         if (resourceId != null && !"".equals(resourceId)){\r
145                                 hql += " and a.sourceId = :resourceId";\r
146                         }\r
147                         if (name != null && !"".equals(name)){\r
148                                 hql += " and a.name = :name ";\r
149                         }\r
150                         if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){\r
151                                 hql += " and a.createTime between :startTime and :endTime ";\r
152                         }\r
153                         Query query = session.createQuery(hql);\r
154                         if (resourceId != null && !"".equals(resourceId)){\r
155                                 query.setString("resourceId",resourceId);\r
156                         }\r
157                         if (name != null && !"".equals(name)){\r
158                                 query.setString("name",name);\r
159                         }\r
160                         if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){\r
161                                 query.setString("startTime", startTime).setString("endTime", endTime);\r
162                         }\r
163                         logger.info("PerformanceInformationServiceImpl queryDateBetween: list={}", query.list());\r
164                         return query.list();\r
165                 } catch (Exception e) {\r
166                         logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDateBetween. Details:" + e.getMessage());\r
167                         return null;\r
168                 }\r
169         }\r
170         \r
171         @Override\r
172         public List<PerformanceInformation> getAllPerformanceInformationByHeaderId(String headerId) {\r
173                 Session session = getSession();\r
174                 try {\r
175                         String string = "from PerformanceInformation a where 1=1 and a.headerId=:headerId";\r
176                         Query query = session.createQuery(string);\r
177                         query.setString("headerId",headerId);\r
178                         List<PerformanceInformation> list = query.list();\r
179                         session.flush();\r
180                         return list;\r
181                 }catch (Exception e){\r
182                         logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDateBetween. LIST:" + e.getMessage());\r
183 \r
184                         return null;\r
185                 }\r
186         }\r
187         \r
188     @Override\r
189     public String queryMaxValueByBetweenDate(String sourceId, String name, String startTime, String endTime) {\r
190                         Session session = getSession();\r
191                      try {\r
192             String hql = "select max(a.value) from PerformanceInformation a where 1=1 ";\r
193             if (sourceId != null && !"".equals(sourceId)){\r
194                 hql += " and a.sourceId = :resourceId";\r
195             }\r
196             if (name != null && !"".equals(name)){\r
197                 hql += " and a.name = :name ";\r
198             }\r
199             if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){\r
200                 hql += " and (CASE WHEN a.startEpochMicrosec=0 THEN a.lastEpochMicroSec ELSE a.startEpochMicrosec END) between :startTime and :endTime ";\r
201             }\r
202             Query query = session.createQuery(hql);\r
203             if (sourceId != null && !"".equals(sourceId)){\r
204                 query.setString("resourceId",sourceId);\r
205             }\r
206             if (name != null && !"".equals(name)){\r
207                 query.setString("name",name);\r
208             }\r
209             if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){\r
210                 query.setString("startTime", startTime).setString("endTime", endTime);\r
211             }\r
212             String num=(String) query.uniqueResult();\r
213             return UuiCommonUtil.isNotNullOrEmpty(num)?num:0+"";\r
214         } catch (Exception e) {\r
215                         logger.error("exception occurred while performing PerformanceInformationServiceImpl queryMaxValueByBetweenDate. Details:" + Throwables.getStackTraceAsString(e));\r
216             logger.error("exception occurred while performing PerformanceInformationServiceImpl queryMaxValueByBetweenDate. Details:" + e.getMessage());\r
217             return 0+"";\r
218         }\r
219     }\r
220 }\r