e32cd766684bdcb9362531c1b9f4ea518f349dac
[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.transaction.Transactional;\r
25 \r
26 import com.google.common.base.Throwables;\r
27 import org.hibernate.Query;\r
28 import org.hibernate.Session;\r
29 import org.hibernate.SessionFactory;\r
30 import org.hibernate.Transaction;\r
31 import org.onap.usecaseui.server.bean.PerformanceInformation;\r
32 import org.onap.usecaseui.server.service.PerformanceInformationService;\r
33 import org.onap.usecaseui.server.util.UuiCommonUtil;\r
34 import org.slf4j.Logger;\r
35 import org.slf4j.LoggerFactory;\r
36 import org.springframework.beans.factory.annotation.Autowired;\r
37 import org.springframework.context.annotation.EnableAspectJAutoProxy;\r
38 import org.springframework.stereotype.Service;\r
39 \r
40 \r
41 @Service("PerformanceInformationService")\r
42 @Transactional\r
43 @org.springframework.context.annotation.Configuration\r
44 @EnableAspectJAutoProxy\r
45 public class PerformanceInformationServiceImpl implements PerformanceInformationService {\r
46 \r
47         private static final Logger logger = LoggerFactory.getLogger(PerformanceInformationServiceImpl.class);\r
48 \r
49         @Autowired\r
50         private SessionFactory sessionFactory;\r
51 \r
52         private Session getSession() {\r
53                 return sessionFactory.openSession();\r
54         }\r
55 \r
56         @Override\r
57         public String savePerformanceInformation(PerformanceInformation performanceInformation) {\r
58         try(Session session = getSession()) {\r
59                         if (null == performanceInformation) {\r
60                         }\r
61                         Transaction tx = session.beginTransaction();\r
62                         session.save(performanceInformation);\r
63                         tx.commit();\r
64                         session.flush();\r
65                         return "1";\r
66                 } catch (Exception e) {\r
67                         logger.error("exception occurred while performing PerformanceInformationServiceImpl savePerformanceInformation. Details:" + e.getMessage());\r
68                         return "0";\r
69                 }\r
70         }\r
71 \r
72         @Override\r
73         public String updatePerformanceInformation(PerformanceInformation performanceInformation) {\r
74                 try(Session session = getSession()) {\r
75                         if (null == performanceInformation) {\r
76                         }\r
77                         logger.info("PerformanceInformationServiceImpl updatePerformanceInformation: performanceInformation={}", performanceInformation);\r
78                         Transaction tx = session.beginTransaction();\r
79                         session.update(performanceInformation);\r
80                         tx.commit();\r
81                         session.flush();\r
82                         return "1";\r
83                 } catch (Exception e) {\r
84                         logger.error("exception occurred while performing PerformanceInformationServiceImpl updatePerformanceInformation. Details:" + e.getMessage());\r
85                         return "0";\r
86                 }\r
87         }\r
88 \r
89         @SuppressWarnings("unchecked")\r
90         @Override\r
91         public List<PerformanceInformation> queryId(String[] id) {\r
92                 try(Session session = getSession()) {\r
93                         if(id.length==0) {\r
94                         }\r
95                         List<PerformanceInformation> list = new ArrayList<>();\r
96                         Query query = session.createQuery("from PerformanceInformation a where a.sourceId IN (:alist)");\r
97                         list = query.setParameterList("alist", id).list();\r
98                         return list;\r
99                 } catch (Exception e) {\r
100                         logger.error("exception occurred while performing PerformanceInformationServiceImpl queryId. Details:" + Throwables.getStackTraceAsString(e));\r
101                         logger.error("exception occurred while performing PerformanceInformationServiceImpl queryId. Details:" + e.getMessage());\r
102                         return null;\r
103                 }\r
104         }\r
105 \r
106         @SuppressWarnings("unchecked")\r
107         @Override\r
108         public List<PerformanceInformation> queryDateBetween(String sourceId, Date startDate, Date endDate) {\r
109                 try(Session session = getSession()) {\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                 try(Session session = getSession()) {\r
124                         int sum = 0;\r
125                         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
126                         sum = Integer.parseInt(query.setParameter("sourceId",sourceId).setParameter("name",name).setParameter("startDate", startDate).setParameter("endDate",endDate).uniqueResult().toString());\r
127                         return sum;\r
128                 } catch (Exception e) {\r
129                         logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDataBetweenSum. Details:" + e.getMessage());\r
130                         return 0;\r
131                 }\r
132         }\r
133 \r
134         @Override\r
135         public List<PerformanceInformation> queryDateBetween(String resourceId, String name, String startTime, String endTime) {\r
136                 try(Session session = getSession()) {\r
137                         String hql = "from PerformanceInformation a where 1=1 ";\r
138                         if (resourceId != null && !"".equals(resourceId)){\r
139                                 hql += " and a.sourceId = :resourceId";\r
140                         }\r
141                         if (name != null && !"".equals(name)){\r
142                                 hql += " and a.name = :name ";\r
143                         }\r
144                         if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){\r
145                                 hql += " and a.createTime between :startTime and :endTime ";\r
146                         }\r
147                         Query query = session.createQuery(hql);\r
148                         if (resourceId != null && !"".equals(resourceId)){\r
149                                 query.setString("resourceId",resourceId);\r
150                         }\r
151                         if (name != null && !"".equals(name)){\r
152                                 query.setString("name",name);\r
153                         }\r
154                         if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){\r
155                                 query.setString("startTime", startTime).setString("endTime", endTime);\r
156                         }\r
157                         logger.info("PerformanceInformationServiceImpl queryDateBetween: list={}", query.list());\r
158                         return query.list();\r
159                 } catch (Exception e) {\r
160                         logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDateBetween. Details:" + e.getMessage());\r
161                         return null;\r
162                 }\r
163         }\r
164         \r
165         @Override\r
166         public List<PerformanceInformation> getAllPerformanceInformationByHeaderId(String headerId) {\r
167                 try (Session session = getSession()){\r
168                         String string = "from PerformanceInformation a where 1=1 and a.headerId=:headerId";\r
169                         Query query = session.createQuery(string);\r
170                         query.setString("headerId",headerId);\r
171                         List<PerformanceInformation> list = query.list();\r
172                         session.flush();\r
173                         return list;\r
174                 }catch (Exception e){\r
175                         logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDateBetween. LIST:" + e.getMessage());\r
176 \r
177                         return null;\r
178                 }\r
179         }\r
180         \r
181     @Override\r
182     public String queryMaxValueByBetweenDate(String sourceId, String name, String startTime, String endTime) {\r
183         try(Session session = getSession()) {\r
184             String hql = "select max(a.value) from PerformanceInformation a where 1=1 ";\r
185             if (sourceId != null && !"".equals(sourceId)){\r
186                 hql += " and a.sourceId = :resourceId";\r
187             }\r
188             if (name != null && !"".equals(name)){\r
189                 hql += " and a.name = :name ";\r
190             }\r
191             if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){\r
192                 hql += " and (CASE WHEN a.startEpochMicrosec=0 THEN a.lastEpochMicroSec ELSE a.startEpochMicrosec END) between :startTime and :endTime ";\r
193             }\r
194             Query query = session.createQuery(hql);\r
195             if (sourceId != null && !"".equals(sourceId)){\r
196                 query.setString("resourceId",sourceId);\r
197             }\r
198             if (name != null && !"".equals(name)){\r
199                 query.setString("name",name);\r
200             }\r
201             if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){\r
202                 query.setString("startTime", startTime).setString("endTime", endTime);\r
203             }\r
204             String num=(String) query.uniqueResult();\r
205             return UuiCommonUtil.isNotNullOrEmpty(num)?num:0+"";\r
206         } catch (Exception e) {\r
207                         logger.error("exception occurred while performing PerformanceInformationServiceImpl queryMaxValueByBetweenDate. Details:" + Throwables.getStackTraceAsString(e));\r
208             logger.error("exception occurred while performing PerformanceInformationServiceImpl queryMaxValueByBetweenDate. Details:" + e.getMessage());\r
209             return 0+"";\r
210         }\r
211     }\r
212 }\r