2 * Copyright (C) 2017 CMCC, Inc. and others. All rights reserved.
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
16 package org.onap.usecaseui.server.service.impl;
\r
19 import java.util.ArrayList;
\r
20 import java.util.Date;
\r
21 import java.util.List;
\r
24 import javax.persistence.EntityManagerFactory;
\r
25 import javax.transaction.Transactional;
\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
42 @Service("PerformanceInformationService")
\r
44 @org.springframework.context.annotation.Configuration
\r
45 @EnableAspectJAutoProxy
\r
46 public class PerformanceInformationServiceImpl implements PerformanceInformationService {
\r
48 private static final Logger logger = LoggerFactory.getLogger(PerformanceInformationServiceImpl.class);
\r
51 private EntityManagerFactory entityManagerFactory;
\r
53 public Session getSession() {
\r
54 return entityManagerFactory.unwrap(SessionFactory.class).getCurrentSession();}
\r
57 public String savePerformanceInformation(PerformanceInformation performanceInformation) {
\r
58 Session session = getSession();
\r
60 if (null == performanceInformation) {
\r
62 Transaction tx = session.beginTransaction();
\r
63 session.save(performanceInformation);
\r
67 } catch (Exception e) {
\r
68 logger.error("exception occurred while performing PerformanceInformationServiceImpl savePerformanceInformation. Details:" + e.getMessage());
\r
74 public String updatePerformanceInformation(PerformanceInformation performanceInformation) {
\r
75 Session session = getSession();
\r
77 if (null == performanceInformation) {
\r
79 logger.info("PerformanceInformationServiceImpl updatePerformanceInformation: performanceInformation={}", performanceInformation);
\r
80 Transaction tx = session.beginTransaction();
\r
81 session.update(performanceInformation);
\r
85 } catch (Exception e) {
\r
86 logger.error("exception occurred while performing PerformanceInformationServiceImpl updatePerformanceInformation. Details:" + e.getMessage());
\r
91 @SuppressWarnings("unchecked")
\r
93 public List<PerformanceInformation> queryId(String[] id) {
\r
94 Session session = getSession();
\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
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
109 @SuppressWarnings("unchecked")
\r
111 public List<PerformanceInformation> queryDateBetween(String sourceId, Date startDate, Date endDate) {
\r
112 Session session = getSession();
\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
118 } catch (Exception e) {
\r
119 logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDateBetween. Details:" + e.getMessage());
\r
124 @SuppressWarnings("unchecked")
\r
126 public int queryDataBetweenSum(String sourceId, String name, Date startDate, Date endDate){
\r
127 Session session = getSession();
\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
133 } catch (Exception e) {
\r
134 logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDataBetweenSum. Details:" + e.getMessage());
\r
140 public List<PerformanceInformation> queryDateBetween(String resourceId, String name, String startTime, String endTime) {
\r
141 Session session = getSession();
\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
147 if (name != null && !"".equals(name)){
\r
148 hql += " and a.name = :name ";
\r
150 if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
\r
151 hql += " and a.createTime between :startTime and :endTime ";
\r
153 Query query = session.createQuery(hql);
\r
154 if (resourceId != null && !"".equals(resourceId)){
\r
155 query.setString("resourceId",resourceId);
\r
157 if (name != null && !"".equals(name)){
\r
158 query.setString("name",name);
\r
160 if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
\r
161 query.setString("startTime", startTime).setString("endTime", endTime);
\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
172 public List<PerformanceInformation> getAllPerformanceInformationByHeaderId(String headerId) {
\r
173 Session session = getSession();
\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
181 }catch (Exception e){
\r
182 logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDateBetween. LIST:" + e.getMessage());
\r
189 public String queryMaxValueByBetweenDate(String sourceId, String name, String startTime, String endTime) {
\r
190 Session session = getSession();
\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
196 if (name != null && !"".equals(name)){
\r
197 hql += " and a.name = :name ";
\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
202 Query query = session.createQuery(hql);
\r
203 if (sourceId != null && !"".equals(sourceId)){
\r
204 query.setString("resourceId",sourceId);
\r
206 if (name != null && !"".equals(name)){
\r
207 query.setString("name",name);
\r
209 if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
\r
210 query.setString("startTime", startTime).setString("endTime", endTime);
\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