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.PerformanceInformationPm;
 
  27 import org.onap.usecaseui.server.service.PerformanceInformationPmService;
 
  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("PerformanceInformationPmService")
 
  39 @org.springframework.context.annotation.Configuration
 
  40 @EnableAspectJAutoProxy
 
  41 public class PerformanceInformationPmServiceImpl implements PerformanceInformationPmService {
 
  42     private static final Logger logger = LoggerFactory.getLogger(PerformanceInformationPmServiceImpl.class);
 
  45     private SessionFactory sessionFactory;
 
  49         public List<PerformanceInformationPm> getAllPerformanceInformationByeventId(String eventId) {
 
  50                 try (Session session = sessionFactory.openSession()){
 
  51                         String string = "from PerformanceInformationPm a where 1=1 and a.eventId=:eventId";
 
  52                         Query query = session.createQuery(string);
 
  53                         query.setString("eventId",eventId);
 
  54                         List<PerformanceInformationPm> list = query.list();
 
  58                         logger.error("exception occurred while performing PerformanceInformationPmServiceImpl queryDateBetween. LIST:" + e.getMessage());
 
  69         public String savePerformanceInformationPm(PerformanceInformationPm performanceInformationPm) {
 
  70                  try(Session session = sessionFactory.openSession();) {
 
  71                     if (null == performanceInformationPm) {
 
  72                         logger.error("performanceInformationPm savePerformanceInformationPm performanceInformationPm is null!");
 
  74                     logger.info("PerformanceInformationPmServiceImpl savePerformanceInformationPm: performanceInformationPm={}", performanceInformationPm);
 
  75                     Transaction tx = session.beginTransaction();     
 
  76                     session.save(performanceInformationPm);
 
  80                 } catch (Exception e) {
 
  81                     logger.error("exception occurred while performing PerformanceInformationPmServiceImpl savePerformanceInformationPm. Details:" + e.getMessage());
 
  89         public String updatePerformanceInformationPm(PerformanceInformationPm performanceInformationPm) {
 
  90                 try(Session session = sessionFactory.openSession();) {
 
  91             if (null == performanceInformationPm) {
 
  92                 logger.error("performanceInformationPm updatePerformanceInformationPm performanceInformationPm is null!");
 
  94             logger.info("PerformanceInformationPmServiceImpl updatePerformanceInformationPm: performanceInformationPm={}", performanceInformationPm);
 
  95             Transaction tx = session.beginTransaction();     
 
  96             session.update(performanceInformationPm);
 
 100         } catch (Exception e) {
 
 101             logger.error("exception occurred while performing PerformanceInformationPmServiceImpl updatePerformanceInformationPm. Details:" + e.getMessage());
 
 107         public int getAllCount(PerformanceInformationPm performanceInformationPm, int currentPage, int pageSize) {
 
 108                 try(Session session = sessionFactory.openSession();){
 
 109                         StringBuffer hql = new StringBuffer("select count(*) from PerformanceInformationPm a where 1=1");
 
 110                         if (null == performanceInformationPm) {
 
 111                 //logger.error("AlarmsInformationServiceImpl getAllCount performanceInformationPm is null!");
 
 113                 if(null!=performanceInformationPm.getName()) {
 
 114                         String ver=performanceInformationPm.getName();
 
 115                         hql.append(" and a.name like '%"+ver+"%'");
 
 117                 if(null!=performanceInformationPm.getValue()) {
 
 118                         String ver=performanceInformationPm.getValue();
 
 119                         hql.append(" and a.value like '%"+ver+"%'");
 
 121                 if(null!=performanceInformationPm.getEventId()) {
 
 122                         String ver=performanceInformationPm.getEventId();
 
 123                         hql.append(" and a.eventId = '"+ver+"'");
 
 125                 if(null!=performanceInformationPm.getCreateTime()) {
 
 126                         Date ver =performanceInformationPm.getCreateTime();
 
 127                         hql.append(" and a.createTime > '%"+ver+"%'");
 
 129                 if(null!=performanceInformationPm.getUpdateTime()) {
 
 130                         Date ver =performanceInformationPm.getUpdateTime();
 
 131                         hql.append(" and a.updateTime like '%"+ver+"%'");
 
 134             long q=(long)session.createQuery(hql.toString()).uniqueResult();
 
 137         } catch (Exception e) {
 
 138             logger.error("exception occurred while performing PerformanceInformationPmServiceImpl getAllCount. Details:" + e.getMessage());
 
 143         @SuppressWarnings("unchecked")  
 
 145         public Page<PerformanceInformationPm> queryPerformanceInformationPm(PerformanceInformationPm performanceInformationPm,
 
 146                         int currentPage, int pageSize) {
 
 147                 Page<PerformanceInformationPm> page = new Page<PerformanceInformationPm>();
 
 148                 int allRow =this.getAllCount(performanceInformationPm,currentPage,pageSize);
 
 149                 int offset = page.countOffset(currentPage, pageSize);
 
 151                 try(Session session = sessionFactory.openSession()){
 
 152                         StringBuffer hql =new StringBuffer("from PerformanceInformationPm a where 1=1 ");
 
 153             if (null == performanceInformationPm) {
 
 156                 if(null!=performanceInformationPm.getName()) {
 
 157                         String ver=performanceInformationPm.getName();
 
 158                         hql.append(" and a.name like '%"+ver+"%'");
 
 160                 if(null!=performanceInformationPm.getValue()) {
 
 161                         String ver=performanceInformationPm.getValue();
 
 162                         hql.append(" and a.value like '%"+ver+"%'");
 
 164                 if(null!=performanceInformationPm.getEventId()) {
 
 165                         String ver=performanceInformationPm.getEventId();
 
 166                         hql.append(" and a.eventId = '"+ver+"'");
 
 168                 if(null!=performanceInformationPm.getCreateTime()) {
 
 169                         Date ver =performanceInformationPm.getCreateTime();
 
 170                         hql.append(" and a.createTime > '%"+ver+"%'");
 
 172                 if(null!=performanceInformationPm.getUpdateTime()) {
 
 173                         Date ver =performanceInformationPm.getUpdateTime();
 
 174                         hql.append(" and a.updateTime like '%"+ver+"%'");
 
 177             logger.info("PerformanceInformationPmServiceImpl queryPerformanceInformationPm: performanceInformationPm={}", performanceInformationPm);
 
 178             Query query = session.createQuery(hql.toString());
 
 179             query.setFirstResult(offset);
 
 180             query.setMaxResults(pageSize);
 
 181             List<PerformanceInformationPm> list= query.list();
 
 183             page.setPageNo(currentPage);
 
 184             page.setPageSize(pageSize);
 
 185             page.setTotalRecords(allRow);
 
 189         } catch (Exception e) {
 
 190             logger.error("exception occurred while performing PerformanceInformationPmServiceImpl queryPerformanceInformationPm. Details:" + e.getMessage());
 
 196         @SuppressWarnings("unchecked")
 
 198         public List<PerformanceInformationPm> queryId(String[] id) {
 
 199                 try(Session session = sessionFactory.openSession();) {
 
 200                         List<PerformanceInformationPm> list;
 
 201                         Query query = session.createQuery("from PerformanceInformationPm a where a.eventId IN (:alist)");
 
 202                         list = query.setParameterList("alist", id).list();
 
 204                 } catch (Exception e) {
 
 206                         logger.error("exception occurred while performing PerformanceInformationPmServiceImpl queryId. Details:" + e.getMessage());
 
 212         @SuppressWarnings("unchecked")
 
 214         public List<PerformanceInformationPm> queryDateBetween(String eventId,Date startDate, Date endDate) {
 
 215                 try(Session session = sessionFactory.openSession()) {
 
 216                         List<PerformanceInformationPm> list ;
 
 217                         Query query = session.createQuery("from PerformanceInformationPm a where a.eventId = :eventId and a.createTime BETWEEN :startDate and :endDate");
 
 218                         list = query.setParameter("eventId",eventId).setParameter("startDate", startDate).setParameter("endDate",endDate).list();
 
 219                         logger.info("PerformanceInformationPmServiceImpl queryDateBetween: list={}", list);
 
 221                 } catch (Exception e) {
 
 222                         logger.error("exception occurred while performing PerformanceInformationPmServiceImpl queryDateBetween. Details:" + e.getMessage());
 
 230         public List<PerformanceInformationPm> queryDateBetween(String resourceId, String name, String startTime, String endTime) {
 
 231                 try(Session session = sessionFactory.openSession()) {
 
 232                         String hql = "from PerformanceInformationPm a where 1=1 ";
 
 233                         if (resourceId != null && !"".equals(resourceId)){
 
 234                                 hql += " and a.eventId = :resourceId";
 
 236                         if (name != null && !"".equals(name)){
 
 237                                 hql += " and a.name = :name ";
 
 239                         if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
 
 240                                 hql += " and a.createTime between :startTime and :endTime ";
 
 242                         Query query = session.createQuery(hql);
 
 243                         if (resourceId != null && !"".equals(resourceId)){
 
 244                                 query.setString("resourceId",resourceId);
 
 246                         if (name != null && !"".equals(name)){
 
 247                                 query.setString("name",name);
 
 249                         if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
 
 250                                 query.setString("startTime", startTime).setString("endTime", endTime);
 
 252                         logger.info("PerformanceInformationPmServiceImpl queryDateBetween: list={}", query.list());
 
 254                 } catch (Exception e) {
 
 255                         logger.error("exception occurred while performing PerformanceInformationPmServiceImpl queryDateBetween. Details:" + e.getMessage());
 
 261     public List<Map<String,String>> queryMaxValueByBetweenDate(String sourceId, String name, String startTime, String endTime) {
 
 262         try(Session session = sessionFactory.openSession()) {
 
 263             List<Map<String,String>> mapList = new ArrayList<>();
 
 264             String hql = "select a.createTime,max(a.value) from PerformanceInformationPm a where 1=1 ";
 
 265             if (sourceId != null && !"".equals(sourceId)){
 
 266                 hql += " and a.eventId = :resourceId";
 
 268             if (name != null && !"".equals(name)){
 
 269                 hql += " and a.name = :name ";
 
 271             if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
 
 272                 hql += " and a.createTime between :startTime and :endTime ";
 
 274             hql += " group by a.createTime";
 
 275             Query query = session.createQuery(hql);
 
 276             if (sourceId != null && !"".equals(sourceId)){
 
 277                 query.setString("resourceId",sourceId);
 
 279             if (name != null && !"".equals(name)){
 
 280                 query.setString("name",name);
 
 282             if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
 
 283                 query.setString("startTime", startTime).setString("endTime", endTime);
 
 285             Iterator it= query.list().iterator();
 
 287                 Object[] res=(Object[]) it.next();
 
 288                 Map<String,String> map = new HashMap<>();
 
 289                 map.put("Time",res[0].toString());
 
 290                 map.put("Max",res[1].toString());
 
 293             logger.info("PerformanceInformationPmServiceImpl queryMaxValueByBetweenDate: maxValue={}", mapList.size());
 
 295         } catch (Exception e) {
 
 297             logger.error("exception occurred while performing PerformanceInformationPmServiceImpl queryMaxValueByBetweenDate. Details:" + e.getMessage());