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