9c9818638a6feff5cd5657c76044d73d550d5017
[usecase-ui/server.git] /
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.text.SimpleDateFormat;
20 import java.util.*;
21
22 import javax.persistence.Id;
23 import javax.transaction.Transactional;
24
25 import org.hibernate.*;
26 import org.onap.usecaseui.server.bean.PerformanceInformationPm;
27 import org.onap.usecaseui.server.service.PerformanceInformationPmService;
28
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;
35
36
37 @Service("PerformanceInformationPmService")
38 @Transactional
39 @org.springframework.context.annotation.Configuration
40 @EnableAspectJAutoProxy
41 public class PerformanceInformationPmServiceImpl implements PerformanceInformationPmService {
42     private static final Logger logger = LoggerFactory.getLogger(PerformanceInformationPmServiceImpl.class);
43
44     @Autowired
45     private SessionFactory sessionFactory;
46
47
48         @Override
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();
55                         session.flush();
56                         return list;
57                 }catch (Exception e){
58                         logger.error("exception occurred while performing PerformanceInformationPmServiceImpl queryDateBetween. LIST:" + e.getMessage());
59
60                         return null;
61                 }
62
63
64
65         }
66
67
68         @Override
69         public String savePerformanceInformationPm(PerformanceInformationPm performanceInformationPm) {
70                  try(Session session = sessionFactory.openSession();) {
71                     if (null == performanceInformationPm) {
72                         logger.error("performanceInformationPm savePerformanceInformationPm performanceInformationPm is null!");
73                     }
74                     logger.info("PerformanceInformationPmServiceImpl savePerformanceInformationPm: performanceInformationPm={}", performanceInformationPm);
75                     Transaction tx = session.beginTransaction();     
76                     session.save(performanceInformationPm);
77                     tx.commit();
78                     session.flush();
79                     return "1";
80                 } catch (Exception e) {
81                     logger.error("exception occurred while performing PerformanceInformationPmServiceImpl savePerformanceInformationPm. Details:" + e.getMessage());
82                     return "0";
83                 }
84                 
85         }
86
87
88         @Override
89         public String updatePerformanceInformationPm(PerformanceInformationPm performanceInformationPm) {
90                 try(Session session = sessionFactory.openSession();) {
91             if (null == performanceInformationPm) {
92                 logger.error("performanceInformationPm updatePerformanceInformationPm performanceInformationPm is null!");
93             }
94             logger.info("PerformanceInformationPmServiceImpl updatePerformanceInformationPm: performanceInformationPm={}", performanceInformationPm);
95             Transaction tx = session.beginTransaction();     
96             session.update(performanceInformationPm);
97             tx.commit();
98             session.flush();
99             return "1";
100         } catch (Exception e) {
101             logger.error("exception occurred while performing PerformanceInformationPmServiceImpl updatePerformanceInformationPm. Details:" + e.getMessage());
102             return "0";
103         }
104         }
105
106
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!");
112             }else {
113                 if(null!=performanceInformationPm.getName()) {
114                         String ver=performanceInformationPm.getName();
115                         hql.append(" and a.name like '%"+ver+"%'");
116                 }
117                 if(null!=performanceInformationPm.getValue()) {
118                         String ver=performanceInformationPm.getValue();
119                         hql.append(" and a.value like '%"+ver+"%'");
120                 }
121                 if(null!=performanceInformationPm.getEventId()) {
122                         String ver=performanceInformationPm.getEventId();
123                         hql.append(" and a.eventId = '"+ver+"'");
124                 }
125                 if(null!=performanceInformationPm.getCreateTime()) {
126                         Date ver =performanceInformationPm.getCreateTime();
127                         hql.append(" and a.createTime > '%"+ver+"%'");
128                 }
129                 if(null!=performanceInformationPm.getUpdateTime()) {
130                         Date ver =performanceInformationPm.getUpdateTime();
131                         hql.append(" and a.updateTime like '%"+ver+"%'");
132                 }
133             }
134             long q=(long)session.createQuery(hql.toString()).uniqueResult();
135             session.flush();
136             return (int)q;
137         } catch (Exception e) {
138             logger.error("exception occurred while performing PerformanceInformationPmServiceImpl getAllCount. Details:" + e.getMessage());
139             return 0;
140         }
141         }
142
143         @SuppressWarnings("unchecked")  
144         @Override
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);
150                 
151                 try(Session session = sessionFactory.openSession()){
152                         StringBuffer hql =new StringBuffer("from PerformanceInformationPm a where 1=1 ");
153             if (null == performanceInformationPm) {
154
155             }else {
156                 if(null!=performanceInformationPm.getName()) {
157                         String ver=performanceInformationPm.getName();
158                         hql.append(" and a.name like '%"+ver+"%'");
159                 }
160                 if(null!=performanceInformationPm.getValue()) {
161                         String ver=performanceInformationPm.getValue();
162                         hql.append(" and a.value like '%"+ver+"%'");
163                 }
164                 if(null!=performanceInformationPm.getEventId()) {
165                         String ver=performanceInformationPm.getEventId();
166                         hql.append(" and a.eventId = '"+ver+"'");
167                 }
168                 if(null!=performanceInformationPm.getCreateTime()) {
169                         Date ver =performanceInformationPm.getCreateTime();
170                         hql.append(" and a.createTime > '%"+ver+"%'");
171                 }
172                 if(null!=performanceInformationPm.getUpdateTime()) {
173                         Date ver =performanceInformationPm.getUpdateTime();
174                         hql.append(" and a.updateTime like '%"+ver+"%'");
175                 }
176             } 
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();
182
183             page.setPageNo(currentPage);
184             page.setPageSize(pageSize);
185             page.setTotalRecords(allRow);
186             page.setList(list);
187             session.flush();
188             return page;
189         } catch (Exception e) {
190             logger.error("exception occurred while performing PerformanceInformationPmServiceImpl queryPerformanceInformationPm. Details:" + e.getMessage());
191             return null;
192         }
193         }
194
195
196         @SuppressWarnings("unchecked")
197         @Override
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();
203                         return list;
204                 } catch (Exception e) {
205                         e.printStackTrace();
206                         logger.error("exception occurred while performing PerformanceInformationPmServiceImpl queryId. Details:" + e.getMessage());
207                         return null;
208                 }
209         }
210
211
212         @SuppressWarnings("unchecked")
213         @Override
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);
220                         return list;
221                 } catch (Exception e) {
222                         logger.error("exception occurred while performing PerformanceInformationPmServiceImpl queryDateBetween. Details:" + e.getMessage());
223                         return null;
224                 }
225         }
226
227
228
229         @Override
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";
235                         }
236                         if (name != null && !"".equals(name)){
237                                 hql += " and a.name = :name ";
238                         }
239                         if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
240                                 hql += " and a.createTime between :startTime and :endTime ";
241                         }
242                         Query query = session.createQuery(hql);
243                         if (resourceId != null && !"".equals(resourceId)){
244                                 query.setString("resourceId",resourceId);
245                         }
246                         if (name != null && !"".equals(name)){
247                                 query.setString("name",name);
248                         }
249                         if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
250                                 query.setString("startTime", startTime).setString("endTime", endTime);
251                         }
252                         logger.info("PerformanceInformationPmServiceImpl queryDateBetween: list={}", query.list());
253                         return query.list();
254                 } catch (Exception e) {
255                         logger.error("exception occurred while performing PerformanceInformationPmServiceImpl queryDateBetween. Details:" + e.getMessage());
256                         return null;
257                 }
258         }
259
260     @Override
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";
267             }
268             if (name != null && !"".equals(name)){
269                 hql += " and a.name = :name ";
270             }
271             if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
272                 hql += " and a.createTime between :startTime and :endTime ";
273             }
274             hql += " group by a.createTime";
275             Query query = session.createQuery(hql);
276             if (sourceId != null && !"".equals(sourceId)){
277                 query.setString("resourceId",sourceId);
278             }
279             if (name != null && !"".equals(name)){
280                 query.setString("name",name);
281             }
282             if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
283                 query.setString("startTime", startTime).setString("endTime", endTime);
284             }
285             Iterator it= query.list().iterator();
286             while(it.hasNext()){
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());
291                 mapList.add(map);
292             }
293             logger.info("PerformanceInformationPmServiceImpl queryMaxValueByBetweenDate: maxValue={}", mapList.size());
294             return mapList;
295         } catch (Exception e) {
296             e.printStackTrace();
297             logger.error("exception occurred while performing PerformanceInformationPmServiceImpl queryMaxValueByBetweenDate. Details:" + e.getMessage());
298             return null;
299         }
300     }
301 }