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 session.save(performanceInformation);
\r
65 } catch (Exception e) {
\r
66 logger.error("exception occurred while performing PerformanceInformationServiceImpl savePerformanceInformation. Details:" + e.getMessage());
\r
72 public String updatePerformanceInformation(PerformanceInformation performanceInformation) {
\r
73 Session session = getSession();
\r
75 if (null == performanceInformation) {
\r
77 logger.info("PerformanceInformationServiceImpl updatePerformanceInformation: performanceInformation={}", performanceInformation);
\r
78 session.update(performanceInformation);
\r
81 } catch (Exception e) {
\r
82 logger.error("exception occurred while performing PerformanceInformationServiceImpl updatePerformanceInformation. Details:" + e.getMessage());
\r
87 @SuppressWarnings("unchecked")
\r
89 public List<PerformanceInformation> queryId(String[] id) {
\r
90 Session session = getSession();
\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
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
105 @SuppressWarnings("unchecked")
\r
107 public List<PerformanceInformation> queryDateBetween(String sourceId, Date startDate, Date endDate) {
\r
108 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
114 } catch (Exception e) {
\r
115 logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDateBetween. Details:" + e.getMessage());
\r
120 @SuppressWarnings("unchecked")
\r
122 public int queryDataBetweenSum(String sourceId, String name, Date startDate, Date endDate){
\r
123 Session session = getSession();
\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
129 } catch (Exception e) {
\r
130 logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDataBetweenSum. Details:" + e.getMessage());
\r
136 public List<PerformanceInformation> queryDateBetween(String resourceId, String name, String startTime, String endTime) {
\r
137 Session session = getSession();
\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
143 if (name != null && !"".equals(name)){
\r
144 hql += " and a.name = :name ";
\r
146 if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
\r
147 hql += " and a.createTime between :startTime and :endTime ";
\r
149 Query query = session.createQuery(hql);
\r
150 if (resourceId != null && !"".equals(resourceId)){
\r
151 query.setString("resourceId",resourceId);
\r
153 if (name != null && !"".equals(name)){
\r
154 query.setString("name",name);
\r
156 if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
\r
157 query.setString("startTime", startTime).setString("endTime", endTime);
\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
168 public List<PerformanceInformation> getAllPerformanceInformationByHeaderId(String headerId) {
\r
169 Session session = getSession();
\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
177 }catch (Exception e){
\r
178 logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDateBetween. LIST:" + e.getMessage());
\r
185 public String queryMaxValueByBetweenDate(String sourceId, String name, String startTime, String endTime) {
\r
186 Session session = getSession();
\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
192 if (name != null && !"".equals(name)){
\r
193 hql += " and a.name = :name ";
\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
198 Query query = session.createQuery(hql);
\r
199 if (sourceId != null && !"".equals(sourceId)){
\r
200 query.setString("resourceId",sourceId);
\r
202 if (name != null && !"".equals(name)){
\r
203 query.setString("name",name);
\r
205 if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
\r
206 query.setString("startTime", startTime).setString("endTime", endTime);
\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