2  * Copyright (C) 2017 CMCC, Inc. and others. All rights reserved.
 
   4  * Licensed under the Apache License, Version 2.0 (the "License");
 
   5  * you may not use this file except in compliance with the License.
 
   6  * You may obtain a copy of the License at
 
   8  *     http://www.apache.org/licenses/LICENSE-2.0
 
  10  * Unless required by applicable law or agreed to in writing, software
 
  11  * distributed under the License is distributed on an "AS IS" BASIS,
 
  12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  13  * See the License for the specific language governing permissions and
 
  14  * limitations under the License.
 
  16 package org.onap.usecaseui.server.service.impl;
 
  19 import java.text.SimpleDateFormat;
 
  22 import javax.persistence.Id;
 
  23 import javax.transaction.Transactional;
 
  25 import org.hibernate.*;
 
  26 import org.onap.usecaseui.server.bean.PerformanceInformationVm;
 
  27 import org.onap.usecaseui.server.service.PerformanceInformationVmService;
 
  28 import org.onap.usecaseui.server.util.DateUtils;
 
  29 import org.onap.usecaseui.server.util.Page;
 
  30 import org.slf4j.Logger;
 
  31 import org.slf4j.LoggerFactory;
 
  32 import org.springframework.beans.factory.annotation.Autowired;
 
  33 import org.springframework.context.annotation.EnableAspectJAutoProxy;
 
  34 import org.springframework.stereotype.Service;
 
  37 @Service("PerformanceInformationVmService")
 
  39 @org.springframework.context.annotation.Configuration
 
  40 @EnableAspectJAutoProxy
 
  41 public class PerformanceInformationVmServiceImpl implements PerformanceInformationVmService {
 
  42     private static final Logger logger = LoggerFactory.getLogger(PerformanceInformationVmServiceImpl.class);
 
  45     private SessionFactory sessionFactory;
 
  49         public List<PerformanceInformationVm> getAllPerformanceInformationByeventId(String eventId) {
 
  50                 try (Session session = sessionFactory.openSession()){
 
  51                         String string = "from PerformanceInformationVm a where 1=1 and a.eventId=:eventId";
 
  52                         Query query = session.createQuery(string);
 
  53                         query.setString("eventId",eventId);
 
  54                         List<PerformanceInformationVm> list = query.list();
 
  58                         logger.error("exception occurred while performing PerformanceInformationVmServiceImpl queryDateBetween. LIST:" + e.getMessage());
 
  70         public String savePerformanceInformationVm(PerformanceInformationVm performanceInformationVm) {
 
  71                  try(Session session = sessionFactory.openSession();) {
 
  72                     if (null == performanceInformationVm) {
 
  73                         logger.error("performanceInformationVm savePerformanceInformationVm performanceInformationVm is null!");
 
  75                     logger.info("PerformanceInformationVmServiceImpl savePerformanceInformationVm: performanceInformationVm={}", performanceInformationVm);
 
  76                     Transaction tx = session.beginTransaction();     
 
  77                     session.save(performanceInformationVm);
 
  81                 } catch (Exception e) {
 
  82                     logger.error("exception occurred while performing PerformanceInformationVmServiceImpl savePerformanceInformationVm. Details:" + e.getMessage());
 
  90         public String updatePerformanceInformationVm(PerformanceInformationVm performanceInformationVm) {
 
  91                 try(Session session = sessionFactory.openSession();) {
 
  92             if (null == performanceInformationVm) {
 
  93                 logger.error("performanceInformationVm updatePerformanceInformationVm performanceInformationVm is null!");
 
  95             logger.info("PerformanceInformationVmServiceImpl updatePerformanceInformationVm: performanceInformationVm={}", performanceInformationVm);
 
  96             Transaction tx = session.beginTransaction();     
 
  97             session.update(performanceInformationVm);
 
 101         } catch (Exception e) {
 
 102             logger.error("exception occurred while performing PerformanceInformationVmServiceImpl updatePerformanceInformationVm. Details:" + e.getMessage());
 
 108         public int getAllCount(PerformanceInformationVm performanceInformationVm, int currentPage, int pageSize) {
 
 109                 try(Session session = sessionFactory.openSession();){
 
 110                         StringBuffer hql = new StringBuffer("select count(*) from PerformanceInformationVm a where 1=1");
 
 111                         if (null == performanceInformationVm) {
 
 112                 //logger.error("AlarmsInformationServiceImpl getAllCount performanceInformationVm is null!");
 
 114                 if(null!=performanceInformationVm.getName()) {
 
 115                         String ver=performanceInformationVm.getName();
 
 116                         hql.append(" and a.name like '%"+ver+"%'");
 
 118                 if(null!=performanceInformationVm.getValue()) {
 
 119                         String ver=performanceInformationVm.getValue();
 
 120                         hql.append(" and a.value like '%"+ver+"%'");
 
 122                 if(null!=performanceInformationVm.getEventId()) {
 
 123                         String ver=performanceInformationVm.getEventId();
 
 124                         hql.append(" and a.eventId = '"+ver+"'");
 
 126                 if(null!=performanceInformationVm.getCreateTime()) {
 
 127                         Date ver =performanceInformationVm.getCreateTime();
 
 128                         hql.append(" and a.createTime > '%"+ver+"%'");
 
 130                 if(null!=performanceInformationVm.getUpdateTime()) {
 
 131                         Date ver =performanceInformationVm.getUpdateTime();
 
 132                         hql.append(" and a.updateTime like '%"+ver+"%'");
 
 135             long q=(long)session.createQuery(hql.toString()).uniqueResult();
 
 138         } catch (Exception e) {
 
 139             logger.error("exception occurred while performing PerformanceInformationVmServiceImpl getAllCount. Details:" + e.getMessage());
 
 144         @SuppressWarnings("unchecked")  
 
 146         public Page<PerformanceInformationVm> queryPerformanceInformationVm(PerformanceInformationVm performanceInformationVm,
 
 147                         int currentPage, int pageSize) {
 
 148                 Page<PerformanceInformationVm> page = new Page<PerformanceInformationVm>();
 
 149                 int allRow =this.getAllCount(performanceInformationVm,currentPage,pageSize);
 
 150                 int offset = page.countOffset(currentPage, pageSize);
 
 152                 try(Session session = sessionFactory.openSession()){
 
 153                         StringBuffer hql =new StringBuffer("from PerformanceInformationVm a where 1=1 ");
 
 154             if (null == performanceInformationVm) {
 
 157                 if(null!=performanceInformationVm.getName()) {
 
 158                         String ver=performanceInformationVm.getName();
 
 159                         hql.append(" and a.name like '%"+ver+"%'");
 
 161                 if(null!=performanceInformationVm.getValue()) {
 
 162                         String ver=performanceInformationVm.getValue();
 
 163                         hql.append(" and a.value like '%"+ver+"%'");
 
 165                 if(null!=performanceInformationVm.getEventId()) {
 
 166                         String ver=performanceInformationVm.getEventId();
 
 167                         hql.append(" and a.eventId = '"+ver+"'");
 
 169                 if(null!=performanceInformationVm.getCreateTime()) {
 
 170                         Date ver =performanceInformationVm.getCreateTime();
 
 171                         hql.append(" and a.createTime > '%"+ver+"%'");
 
 173                 if(null!=performanceInformationVm.getUpdateTime()) {
 
 174                         Date ver =performanceInformationVm.getUpdateTime();
 
 175                         hql.append(" and a.updateTime like '%"+ver+"%'");
 
 178             logger.info("PerformanceInformationVmServiceImpl queryPerformanceInformationVm: performanceInformationVm={}", performanceInformationVm);
 
 179             Query query = session.createQuery(hql.toString());
 
 180             query.setFirstResult(offset);
 
 181             query.setMaxResults(pageSize);
 
 182             List<PerformanceInformationVm> list= query.list();
 
 184             page.setPageNo(currentPage);
 
 185             page.setPageSize(pageSize);
 
 186             page.setTotalRecords(allRow);
 
 190         } catch (Exception e) {
 
 191             logger.error("exception occurred while performing PerformanceInformationVmServiceImpl queryPerformanceInformationVm. Details:" + e.getMessage());
 
 197         @SuppressWarnings("unchecked")
 
 199         public List<PerformanceInformationVm> queryId(String[] id) {
 
 200                 try(Session session = sessionFactory.openSession();) {
 
 201                         List<PerformanceInformationVm> list;
 
 202                         Query query = session.createQuery("from PerformanceInformationVm a where a.eventId IN (:alist)");
 
 203                         list = query.setParameterList("alist", id).list();
 
 205                 } catch (Exception e) {
 
 207                         logger.error("exception occurred while performing PerformanceInformationVmServiceImpl queryId. Details:" + e.getMessage());
 
 213         @SuppressWarnings("unchecked")
 
 215         public List<PerformanceInformationVm> queryDateBetween(String eventId,Date startDate, Date endDate) {
 
 216                 try(Session session = sessionFactory.openSession()) {
 
 217                         List<PerformanceInformationVm> list ;
 
 218                         Query query = session.createQuery("from PerformanceInformationVm a where a.eventId = :eventId and a.createTime BETWEEN :startDate and :endDate");
 
 219                         list = query.setParameter("eventId",eventId).setParameter("startDate", startDate).setParameter("endDate",endDate).list();
 
 220                         logger.info("PerformanceInformationVmServiceImpl queryDateBetween: list={}", list);
 
 222                 } catch (Exception e) {
 
 223                         logger.error("exception occurred while performing PerformanceInformationVmServiceImpl queryDateBetween. Details:" + e.getMessage());
 
 231         public List<PerformanceInformationVm> queryDateBetween(String resourceId, String name, String startTime, String endTime) {
 
 232                 try(Session session = sessionFactory.openSession()) {
 
 233                         String hql = "from PerformanceInformationVm a where 1=1 ";
 
 234                         if (resourceId != null && !"".equals(resourceId)){
 
 235                                 hql += " and a.eventId = :resourceId";
 
 237                         if (name != null && !"".equals(name)){
 
 238                                 hql += " and a.name = :name ";
 
 240                         if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
 
 241                                 hql += " and a.createTime between :startTime and :endTime ";
 
 243                         Query query = session.createQuery(hql);
 
 244                         if (resourceId != null && !"".equals(resourceId)){
 
 245                                 query.setString("resourceId",resourceId);
 
 247                         if (name != null && !"".equals(name)){
 
 248                                 query.setString("name",name);
 
 250                         if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
 
 251                                 query.setString("startTime", startTime).setString("endTime", endTime);
 
 253                         logger.info("PerformanceInformationVmServiceImpl queryDateBetween: list={}", query.list());
 
 255                 } catch (Exception e) {
 
 256                         logger.error("exception occurred while performing PerformanceInformationVmServiceImpl queryDateBetween. Details:" + e.getMessage());
 
 262     public List<Map<String,String>> queryMaxValueByBetweenDate(String sourceId, String name, String startTime, String endTime) {
 
 263         try(Session session = sessionFactory.openSession()) {
 
 264             List<Map<String,String>> mapList = new ArrayList<>();
 
 265             String hql = "select a.createTime,max(a.value) from PerformanceInformationVm a where 1=1 ";
 
 266             if (sourceId != null && !"".equals(sourceId)){
 
 267                 hql += " and a.eventId = :resourceId";
 
 269             if (name != null && !"".equals(name)){
 
 270                 hql += " and a.name = :name ";
 
 272             if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
 
 273                 hql += " and a.createTime between :startTime and :endTime ";
 
 275             hql += " group by a.createTime";
 
 276             Query query = session.createQuery(hql);
 
 277             if (sourceId != null && !"".equals(sourceId)){
 
 278                 query.setString("resourceId",sourceId);
 
 280             if (name != null && !"".equals(name)){
 
 281                 query.setString("name",name);
 
 283             if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
 
 284                 query.setString("startTime", startTime).setString("endTime", endTime);
 
 286             Iterator it= query.list().iterator();
 
 288                 Object[] res=(Object[]) it.next();
 
 289                 Map<String,String> map = new HashMap<>();
 
 290                 map.put("Time",res[0].toString());
 
 291                 map.put("Max",res[1].toString());
 
 294             logger.info("PerformanceInformationVmServiceImpl queryMaxValueByBetweenDate: maxValue={}", mapList.size());
 
 296         } catch (Exception e) {
 
 298             logger.error("exception occurred while performing PerformanceInformationVmServiceImpl queryMaxValueByBetweenDate. Details:" + e.getMessage());