04ef2fae7943e8c4faef09bbf56532b7a123d4f4
[usecase-ui/server.git] / src / main / java / org / onap / usecaseui / server / service / impl / PerformanceInformationServiceImpl.java
1 /*
2  * Copyright (C) 2017 CMCC, Inc. and others. All rights reserved.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.usecaseui.server.service.impl;
17
18
19 import java.util.Date;
20 import java.util.List;
21
22 import javax.transaction.Transactional;
23
24 import org.hibernate.Query;
25 import org.hibernate.Session;
26 import org.hibernate.SessionFactory;
27 import org.hibernate.Transaction;
28 import org.onap.usecaseui.server.bean.PerformanceInformation;
29 import org.onap.usecaseui.server.service.PerformanceInformationService;
30 import org.onap.usecaseui.server.util.Page;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.context.annotation.EnableAspectJAutoProxy;
35 import org.springframework.stereotype.Service;
36
37
38 @Service("PerformanceInformationService")
39 @Transactional
40 @org.springframework.context.annotation.Configuration
41 @EnableAspectJAutoProxy
42 public class PerformanceInformationServiceImpl implements PerformanceInformationService {
43     private static final Logger logger = LoggerFactory.getLogger(PerformanceInformationServiceImpl.class);
44
45     @Autowired
46     private SessionFactory sessionFactory;
47
48
49         @Override
50         public String savePerformanceInformation(PerformanceInformation performanceInformation) {
51                  try {
52                     if (null == performanceInformation) {
53                         logger.error("performanceInformation PerformanceInformation performanceInformation is null!");
54                     }
55                     logger.info("PerformanceInformationServiceImpl savePerformanceInformation: performanceInformation={}", performanceInformation);
56                     Session session = sessionFactory.openSession();
57                     Transaction tx = session.beginTransaction();     
58                     session.save(performanceInformation);
59                     tx.commit();
60                     session.flush();
61                     session.close();
62                     return "1";
63                 } catch (Exception e) {
64                     logger.error("Exception occurred while performing PerformanceInformationServiceImpl savePerformanceInformation. Details:" + e.getMessage());
65                     return "0";
66                 }
67                 
68         }
69
70
71         @Override
72         public String updatePerformanceInformation(PerformanceInformation performanceInformation) {
73                 try {
74             if (null == performanceInformation) {
75                 logger.error("performanceInformation PerformanceInformation performanceInformation is null!");
76             }
77             logger.info("PerformanceInformationServiceImpl savePerformanceInformation: performanceInformation={}", performanceInformation);
78             Session session = sessionFactory.openSession();
79             Transaction tx = session.beginTransaction();     
80             session.update(performanceInformation);
81             tx.commit();
82             session.flush();
83             session.close();
84             return "1";
85         } catch (Exception e) {
86             logger.error("Exception occurred while performing PerformanceInformationServiceImpl updatePerformanceInformation. Details:" + e.getMessage());
87             return "0";
88         }
89         }
90
91
92         public int getAllCount() {
93                 try{
94             Session session = sessionFactory.openSession();
95             Transaction tx = session.beginTransaction();     
96             long q=(long)session.createQuery("select count(*) from PerformanceInformation").uniqueResult();
97             tx.commit();
98             session.flush();
99             session.close();
100             return (int)q;
101         } catch (Exception e) {
102             logger.error("Exception occurred while performing PerformanceInformationServiceImpl getAllCount. Details:" + e.getMessage());
103             return 0;
104         }
105         }
106
107         @SuppressWarnings("unchecked")  
108         @Override
109         public Page<PerformanceInformation> queryPerformanceInformation(PerformanceInformation performanceInformation,
110                         int currentPage, int pageSize) {
111                 Page<PerformanceInformation> page = new Page<PerformanceInformation>();
112                 int allRow =this.getAllCount();
113                 int offset = page.countOffset(currentPage, pageSize);
114                 
115                 try{
116                         StringBuffer hql =new StringBuffer("from PerformanceInformation a where 1=1");
117             if (null == performanceInformation) {
118                 logger.error("AlarmsInformationServiceImpl queryPerformanceInformation performanceInformation is null!");
119             }else if(null!=performanceInformation.getName()) {
120                 String ver=performanceInformation.getName();
121                 hql.append(" and a.name like '%"+ver+"%'");
122             }else if(null!=performanceInformation.getValue()) {
123                 String ver=performanceInformation.getValue();
124                 hql.append(" and a.value like '%"+ver+"%'");
125             }else if(null!=performanceInformation.getEventId()) {
126                 String ver=performanceInformation.getEventId();
127                 hql.append(" and a.eventId like '%"+ver+"%'");
128             }else if(null!=performanceInformation.getCreateTime()) {
129                 Date ver =performanceInformation.getCreateTime();
130                 hql.append(" and a.createTime like '%"+ver+"%'");
131             }else if(null!=performanceInformation.getUpdateTime()) {
132                 Date ver =performanceInformation.getUpdateTime();
133                 hql.append(" and a.updateTime like '%"+ver+"%'");
134             }
135             logger.info("PerformanceInformationServiceImpl queryPerformanceInformation: performanceInformation={}", performanceInformation);
136             Session session = sessionFactory.openSession();
137             Transaction tx = session.beginTransaction(); 
138             Query query = session.createQuery(hql.toString());
139             query.setFirstResult(offset);
140             query.setMaxResults(pageSize);
141             List<PerformanceInformation> list= query.list();
142             page.setPageNo(currentPage);
143             page.setPageSize(pageSize);
144             page.setTotalRecords(allRow);
145             page.setList(list);
146             tx.commit();
147             session.flush();
148             session.close();
149             return page;
150         } catch (Exception e) {
151             logger.error("Exception occurred while performing PerformanceInformationServiceImpl queryPerformanceInformation. Details:" + e.getMessage());
152             return null;
153         }
154         }
155
156
157     
158     
159     
160 }