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.transaction.Transactional;
\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
41 @Service("PerformanceInformationService")
\r
43 @org.springframework.context.annotation.Configuration
\r
44 @EnableAspectJAutoProxy
\r
45 public class PerformanceInformationServiceImpl implements PerformanceInformationService {
\r
47 private static final Logger logger = LoggerFactory.getLogger(PerformanceInformationServiceImpl.class);
\r
50 private SessionFactory sessionFactory;
\r
52 private Session getSession() {
\r
53 return sessionFactory.openSession();
\r
57 public String savePerformanceInformation(PerformanceInformation performanceInformation) {
\r
58 try(Session session = getSession()) {
\r
59 if (null == performanceInformation) {
\r
61 Transaction tx = session.beginTransaction();
\r
62 session.save(performanceInformation);
\r
66 } catch (Exception e) {
\r
67 logger.error("exception occurred while performing PerformanceInformationServiceImpl savePerformanceInformation. Details:" + e.getMessage());
\r
73 public String updatePerformanceInformation(PerformanceInformation performanceInformation) {
\r
74 try(Session session = getSession()) {
\r
75 if (null == performanceInformation) {
\r
77 logger.info("PerformanceInformationServiceImpl updatePerformanceInformation: performanceInformation={}", performanceInformation);
\r
78 Transaction tx = session.beginTransaction();
\r
79 session.update(performanceInformation);
\r
83 } catch (Exception e) {
\r
84 logger.error("exception occurred while performing PerformanceInformationServiceImpl updatePerformanceInformation. Details:" + e.getMessage());
\r
89 @SuppressWarnings("unchecked")
\r
91 public List<PerformanceInformation> queryId(String[] id) {
\r
92 try(Session session = getSession()) {
\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
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
106 @SuppressWarnings("unchecked")
\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
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 try(Session session = getSession()) {
\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
128 } catch (Exception e) {
\r
129 logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDataBetweenSum. Details:" + e.getMessage());
\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
141 if (name != null && !"".equals(name)){
\r
142 hql += " and a.name = :name ";
\r
144 if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
\r
145 hql += " and a.createTime between :startTime and :endTime ";
\r
147 Query query = session.createQuery(hql);
\r
148 if (resourceId != null && !"".equals(resourceId)){
\r
149 query.setString("resourceId",resourceId);
\r
151 if (name != null && !"".equals(name)){
\r
152 query.setString("name",name);
\r
154 if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
\r
155 query.setString("startTime", startTime).setString("endTime", endTime);
\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
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
174 }catch (Exception e){
\r
175 logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDateBetween. LIST:" + e.getMessage());
\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
188 if (name != null && !"".equals(name)){
\r
189 hql += " and a.name = :name ";
\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
194 Query query = session.createQuery(hql);
\r
195 if (sourceId != null && !"".equals(sourceId)){
\r
196 query.setString("resourceId",sourceId);
\r
198 if (name != null && !"".equals(name)){
\r
199 query.setString("name",name);
\r
201 if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
\r
202 query.setString("startTime", startTime).setString("endTime", endTime);
\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