Fix no transaction is in progress bug
[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                         session.save(performanceInformation);\r
63                         session.flush();\r
64                         return "1";\r
65                 } catch (Exception e) {\r
66                         logger.error("exception occurred while performing PerformanceInformationServiceImpl savePerformanceInformation. Details:" + e.getMessage());\r
67                         return "0";\r
68                 }\r
69         }\r
70 \r
71         @Override\r
72         public String updatePerformanceInformation(PerformanceInformation performanceInformation) {\r
73                 Session session = getSession();\r
74                 try {\r
75                         if (null == performanceInformation) {\r
76                         }\r
77                         logger.info("PerformanceInformationServiceImpl updatePerformanceInformation: performanceInformation={}", performanceInformation);\r
78                         session.update(performanceInformation);\r
79                         session.flush();\r
80                         return "1";\r
81                 } catch (Exception e) {\r
82                         logger.error("exception occurred while performing PerformanceInformationServiceImpl updatePerformanceInformation. Details:" + e.getMessage());\r
83                         return "0";\r
84                 }\r
85         }\r
86 \r
87         @SuppressWarnings("unchecked")\r
88         @Override\r
89         public List<PerformanceInformation> queryId(String[] id) {\r
90                 Session session = getSession();\r
91                 try {\r
92                         if(id.length==0) {\r
93                         }\r
94                         List<PerformanceInformation> list = new ArrayList<>();\r
95                         Query query = session.createQuery("from PerformanceInformation a where a.sourceId IN (:alist)");\r
96                         list = query.setParameterList("alist", id).list();\r
97                         return list;\r
98                 } catch (Exception e) {\r
99                         logger.error("exception occurred while performing PerformanceInformationServiceImpl queryId. Details:" + Throwables.getStackTraceAsString(e));\r
100                         logger.error("exception occurred while performing PerformanceInformationServiceImpl queryId. Details:" + e.getMessage());\r
101                         return null;\r
102                 }\r
103         }\r
104 \r
105         @SuppressWarnings("unchecked")\r
106         @Override\r
107         public List<PerformanceInformation> queryDateBetween(String sourceId, Date startDate, Date endDate) {\r
108                 Session session = getSession();\r
109                 try {\r
110                         List<PerformanceInformation> list = new ArrayList<>();\r
111                         Query query = session.createQuery("from PerformanceInformation a where a.sourceId = :sourceId and a.createTime BETWEEN :startDate and :endDate");\r
112                         list = query.setParameter("sourceId",sourceId).setParameter("startDate", startDate).setParameter("endDate",endDate).list();\r
113                         return list;\r
114                 } catch (Exception e) {\r
115                         logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDateBetween. Details:" + e.getMessage());\r
116                         return null;\r
117                 }\r
118         }\r
119 \r
120         @SuppressWarnings("unchecked")\r
121         @Override\r
122         public int queryDataBetweenSum(String sourceId, String name, Date startDate, Date endDate){\r
123                 Session session = getSession();\r
124                 try {\r
125                         int sum = 0;\r
126                         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
127                         sum = Integer.parseInt(query.setParameter("sourceId",sourceId).setParameter("name",name).setParameter("startDate", startDate).setParameter("endDate",endDate).uniqueResult().toString());\r
128                         return sum;\r
129                 } catch (Exception e) {\r
130                         logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDataBetweenSum. Details:" + e.getMessage());\r
131                         return 0;\r
132                 }\r
133         }\r
134 \r
135         @Override\r
136         public List<PerformanceInformation> queryDateBetween(String resourceId, String name, String startTime, String endTime) {\r
137                 Session session = getSession();\r
138                 try {\r
139                         String hql = "from PerformanceInformation a where 1=1 ";\r
140                         if (resourceId != null && !"".equals(resourceId)){\r
141                                 hql += " and a.sourceId = :resourceId";\r
142                         }\r
143                         if (name != null && !"".equals(name)){\r
144                                 hql += " and a.name = :name ";\r
145                         }\r
146                         if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){\r
147                                 hql += " and a.createTime between :startTime and :endTime ";\r
148                         }\r
149                         Query query = session.createQuery(hql);\r
150                         if (resourceId != null && !"".equals(resourceId)){\r
151                                 query.setString("resourceId",resourceId);\r
152                         }\r
153                         if (name != null && !"".equals(name)){\r
154                                 query.setString("name",name);\r
155                         }\r
156                         if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){\r
157                                 query.setString("startTime", startTime).setString("endTime", endTime);\r
158                         }\r
159                         logger.info("PerformanceInformationServiceImpl queryDateBetween: list={}", query.list());\r
160                         return query.list();\r
161                 } catch (Exception e) {\r
162                         logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDateBetween. Details:" + e.getMessage());\r
163                         return null;\r
164                 }\r
165         }\r
166         \r
167         @Override\r
168         public List<PerformanceInformation> getAllPerformanceInformationByHeaderId(String headerId) {\r
169                 Session session = getSession();\r
170                 try {\r
171                         String string = "from PerformanceInformation a where 1=1 and a.headerId=:headerId";\r
172                         Query query = session.createQuery(string);\r
173                         query.setString("headerId",headerId);\r
174                         List<PerformanceInformation> list = query.list();\r
175                         session.flush();\r
176                         return list;\r
177                 }catch (Exception e){\r
178                         logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDateBetween. LIST:" + e.getMessage());\r
179 \r
180                         return null;\r
181                 }\r
182         }\r
183         \r
184     @Override\r
185     public String queryMaxValueByBetweenDate(String sourceId, String name, String startTime, String endTime) {\r
186                         Session session = getSession();\r
187                      try {\r
188             String hql = "select max(a.value) from PerformanceInformation a where 1=1 ";\r
189             if (sourceId != null && !"".equals(sourceId)){\r
190                 hql += " and a.sourceId = :resourceId";\r
191             }\r
192             if (name != null && !"".equals(name)){\r
193                 hql += " and a.name = :name ";\r
194             }\r
195             if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){\r
196                 hql += " and (CASE WHEN a.startEpochMicrosec=0 THEN a.lastEpochMicroSec ELSE a.startEpochMicrosec END) between :startTime and :endTime ";\r
197             }\r
198             Query query = session.createQuery(hql);\r
199             if (sourceId != null && !"".equals(sourceId)){\r
200                 query.setString("resourceId",sourceId);\r
201             }\r
202             if (name != null && !"".equals(name)){\r
203                 query.setString("name",name);\r
204             }\r
205             if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){\r
206                 query.setString("startTime", startTime).setString("endTime", endTime);\r
207             }\r
208             String num=(String) query.uniqueResult();\r
209             return UuiCommonUtil.isNotNullOrEmpty(num)?num:0+"";\r
210         } catch (Exception e) {\r
211                         logger.error("exception occurred while performing PerformanceInformationServiceImpl queryMaxValueByBetweenDate. Details:" + Throwables.getStackTraceAsString(e));\r
212             logger.error("exception occurred while performing PerformanceInformationServiceImpl queryMaxValueByBetweenDate. Details:" + e.getMessage());\r
213             return 0+"";\r
214         }\r
215     }\r
216 }\r