8c9af44c4343ed432f3ae791c582187e51296465
[usecase-ui/server.git] / src / main / java / org / onap / usecaseui / server / service / impl / AlarmsInformationServiceImpl.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.AlarmsInformation;
29 import org.onap.usecaseui.server.service.AlarmsInformationService;
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("AlarmsInformationService")
39 @Transactional
40 @org.springframework.context.annotation.Configuration
41 @EnableAspectJAutoProxy
42 public class AlarmsInformationServiceImpl implements AlarmsInformationService {
43     private static final Logger logger = LoggerFactory.getLogger(AlarmsInformationServiceImpl.class);
44
45     @Autowired
46     private SessionFactory sessionFactory;
47
48         @Override
49         public String saveAlarmsInformation(AlarmsInformation alarmsInformation) {
50                  try{
51                     if (null == alarmsInformation) {
52                         logger.error("alarmsInformation AlarmsInformation alarmsInformation is null!");
53                     }
54                     logger.info("AlarmsInformationServiceImpl saveAlarmsInformation: alarmsInformation={}", alarmsInformation);
55                     Session session = sessionFactory.openSession();
56                     Transaction tx = session.beginTransaction();     
57                     session.save(alarmsInformation);
58                     tx.commit();
59                     session.flush();
60                     session.close();
61                     return "1";
62                 } catch (Exception e) {
63                     logger.error("Exception occurred while performing AlarmsInformationServiceImpl saveAlarmsInformation. Details:" + e.getMessage());
64                     return "0";
65                 }
66                 
67         }
68
69         @Override
70         public String updateAlarmsInformation(AlarmsInformation alarmsInformation) {
71                 try{
72             if (null == alarmsInformation) {
73                 logger.error("alarmsInformation AlarmsInformation alarmsInformation is null!");
74             }
75             logger.info("AlarmsInformationServiceImpl updateAlarmsInformation: alarmsInformation={}", alarmsInformation);
76             Session session = sessionFactory.openSession();
77             Transaction tx = session.beginTransaction();     
78             session.update(alarmsInformation);
79             tx.commit();
80             session.flush();
81             session.close();
82             return "1";
83         } catch (Exception e) {
84             logger.error("Exception occurred while performing AlarmsInformationServiceImpl updateAlarmsInformation. Details:" + e.getMessage());
85             return "0";
86         }
87         }
88         
89
90         public int getAllCount() {
91                 try{
92             Session session = sessionFactory.openSession();
93             Transaction tx = session.beginTransaction();     
94             long q=(long)session.createQuery("select count(*) from AlarmsInformation").uniqueResult();
95             tx.commit();
96             session.flush();
97             session.close();
98             return (int)q;
99         } catch (Exception e) {
100             logger.error("Exception occurred while performing AlarmsInformationServiceImpl getAllCount. Details:" + e.getMessage());
101             return 0;
102         }
103         }
104
105         @SuppressWarnings("unchecked")
106         @Override
107         public Page<AlarmsInformation> queryAlarmsInformation(AlarmsInformation alarmsInformation, int currentPage,
108                         int pageSize) {
109                 Page<AlarmsInformation> page = new Page<AlarmsInformation>();
110                 int allRow =this.getAllCount();
111                 int offset = page.countOffset(currentPage, pageSize);
112                 
113                 try{
114                         StringBuffer hql =new StringBuffer("from AlarmsInformation a where 1=1");
115             if (null == alarmsInformation) {
116                 logger.error("AlarmsInformationServiceImpl queryAlarmsInformation alarmsInformation is null!");
117             }else if(null!=alarmsInformation.getName()) {
118                 String ver=alarmsInformation.getName();
119                 hql.append(" and a.name like '%"+ver+"%'");
120             }else if(null!=alarmsInformation.getValue()) {
121                 String ver=alarmsInformation.getValue();
122                 hql.append(" and a.value like '%"+ver+"%'");
123             }else if(null!=alarmsInformation.getEventId()) {
124                 String ver=alarmsInformation.getEventId();
125                 hql.append(" and a.eventId like '%"+ver+"%'");
126             }else if(null!=alarmsInformation.getCreateTime()) {
127                 Date ver =alarmsInformation.getCreateTime();
128                 hql.append(" and a.createTime like '%"+ver+"%'");
129             }else if(null!=alarmsInformation.getUpdateTime()) {
130                 Date ver =alarmsInformation.getUpdateTime();
131                 hql.append(" and a.updateTime like '%"+ver+"%'");
132             }
133             logger.info("AlarmsInformationServiceImpl queryAlarmsInformation: alarmsInformation={}", alarmsInformation);
134             Session session = sessionFactory.openSession();
135             Transaction tx = session.beginTransaction(); 
136             Query query = session.createQuery(hql.toString());
137             query.setFirstResult(offset);
138             query.setMaxResults(pageSize);
139             List<AlarmsInformation> list= query.list();
140             page.setPageNo(currentPage);
141             page.setPageSize(pageSize);
142             page.setTotalRecords(allRow);
143             page.setList(list);
144             tx.commit();
145             session.flush();
146             session.close();
147             return page;
148         } catch (Exception e) {
149             logger.error("Exception occurred while performing AlarmsInformationServiceImpl queryAlarmsInformation. Details:" + e.getMessage());
150             return null;
151         }
152         }
153
154     
155     
156     
157 }