aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main/java/org/onap/usecaseui/server/service/impl/AlarmsHeaderServiceImpl.java
blob: 3e0777e0e2b94eaa7156e0fd33b7341afa676aa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 * Copyright (C) 2017 CMCC, Inc. and others. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onap.usecaseui.server.service.impl;


import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import jakarta.persistence.EntityManagerFactory;
import jakarta.transaction.Transactional;

import org.hibernate.query.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.onap.usecaseui.server.bean.AlarmsHeader;
import org.onap.usecaseui.server.bean.SortMaster;
import org.onap.usecaseui.server.service.AlarmsHeaderService;
import org.onap.usecaseui.server.util.Page;
import org.onap.usecaseui.server.util.UuiCommonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Service;


@Service("AlarmsHeaderService")
@Transactional
@org.springframework.context.annotation.Configuration
@EnableAspectJAutoProxy
public class   AlarmsHeaderServiceImpl implements AlarmsHeaderService {

	private static final Logger logger = LoggerFactory.getLogger(AlarmsHeaderServiceImpl.class);

	@Autowired
	private EntityManagerFactory entityManagerFactory;

	public Session getSession() {
		return entityManagerFactory.unwrap(SessionFactory.class).getCurrentSession();}

	public String saveAlarmsHeader(AlarmsHeader alarmsHeader) {
		Session session = getSession();
		try{
			if (null == alarmsHeader) {
				logger.error("AlarmsHeaderServiceImpl saveAlarmsHeader alarmsHeader is null!");
				return "0";
			}
			logger.info("AlarmsHeaderServiceImpl saveAlarmsHeader: alarmsHeader={}", alarmsHeader);
			session.save(alarmsHeader);
			session.flush();
			return "1";
		} catch (Exception e) {
			logger.error("exception occurred while performing AlarmsHeaderServiceImpl saveAlarmsHeader. Details:" + e.getMessage());
			return "0";
		}
	}

	@Override
	public String updateAlarmsHeader(AlarmsHeader alarmsHeader) {
		Session session = getSession();
		try{
			if (null == alarmsHeader){
				logger.error("AlarmsHeaderServiceImpl updateAlarmsHeader alarmsHeader is null!");
				return "0";
			}
			logger.info("AlarmsHeaderServiceImpl updateAlarmsHeader: alarmsHeader={}", alarmsHeader);
			session.update(alarmsHeader);
			session.flush();
			return "1";
		} catch (Exception e) {
			logger.error("exception occurred while performing AlarmsHeaderServiceImpl updateAlarmsHeader. Details:" + e.getMessage());
			return "0";
		}
	}

	public int getAllCount(AlarmsHeader alarmsHeader,int currentPage,int pageSize) {
		Session session = getSession();
		try{
			StringBuffer count=new StringBuffer("select count(*) from AlarmsHeader a where 1=1");
			if (null == alarmsHeader) {
				logger.error("AlarmsHeaderServiceImpl getAllCount alarmsHeader is null!");
				return -1;
			}else {
				if(UuiCommonUtil.isNotNullOrEmpty(alarmsHeader.getSourceName())) {
					String ver =alarmsHeader.getSourceName();
					count.append(" and a.sourceName like '%"+ver+"%'");
				}
				if(UuiCommonUtil.isNotNullOrEmpty(alarmsHeader.getPriority())) {
					String ver =alarmsHeader.getPriority();
					count.append(" and a.priority like '%"+ver+"%'");
				}
				if(UuiCommonUtil.isNotNullOrEmpty(alarmsHeader.getStatus())) {
					String ver =alarmsHeader.getStatus();
					count.append(" and a.status = '"+ver+"'");
				}
				if(UuiCommonUtil.isNotNullOrEmpty(alarmsHeader.getStartEpochMicrosec())&&UuiCommonUtil.isNotNullOrEmpty(alarmsHeader.getLastEpochMicroSec())) {
					count.append(" and (CASE WHEN a.startEpochMicrosec=0 THEN a.lastEpochMicroSec ELSE a.startEpochMicrosec END) between :startTime and :endTime ");
				}
			}
			Query query = session.createQuery(count.toString());
			if(UuiCommonUtil.isNotNullOrEmpty(alarmsHeader.getStartEpochMicrosec())&&UuiCommonUtil.isNotNullOrEmpty(alarmsHeader.getLastEpochMicroSec())) {
				query.setParameter("startTime",alarmsHeader.getStartEpochMicrosec());
				query.setParameter("endTime",alarmsHeader.getLastEpochMicroSec());
			}
			long q=(long)query.uniqueResult();
			session.flush();
			return (int)q;
		} catch (Exception e) {
			logger.error("exception occurred while performing AlarmsHeaderServiceImpl getAllCount. Details:" + e.getMessage());
			return -1;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Page<AlarmsHeader> queryAlarmsHeader(AlarmsHeader alarmsHeader,int currentPage,int pageSize) {
		Page<AlarmsHeader> page = new Page<AlarmsHeader>();
		int allRow =this.getAllCount(alarmsHeader,currentPage,pageSize);
		int offset = page.countOffset(currentPage, pageSize);
		Session session = getSession();
		try{
			StringBuffer hql =new StringBuffer("from AlarmsHeader a where 1=1");
			if (null == alarmsHeader) {
				logger.error("AlarmsHeaderServiceImpl queryAlarmsHeader alarmsHeader is null!");
				return null;
			}else {
				if(UuiCommonUtil.isNotNullOrEmpty(alarmsHeader.getSourceName())) {
					String ver =alarmsHeader.getSourceName();
					hql.append(" and a.sourceName like '%"+ver+"%'");
				}
				if(UuiCommonUtil.isNotNullOrEmpty(alarmsHeader.getVfStatus())) {
					String ver =alarmsHeader.getVfStatus();
					hql.append(" and a.vfStatus = '"+ver+"'");
				}
				if(UuiCommonUtil.isNotNullOrEmpty(alarmsHeader.getStatus())) {
					String ver =alarmsHeader.getStatus();
					hql.append(" and a.status = '"+ver+"'");
				}
				if(UuiCommonUtil.isNotNullOrEmpty(alarmsHeader.getStartEpochMicrosec())&&UuiCommonUtil.isNotNullOrEmpty(alarmsHeader.getLastEpochMicroSec())) {
					hql.append(" and (CASE WHEN a.startEpochMicrosec=0 THEN a.lastEpochMicroSec ELSE a.startEpochMicrosec END) between :startTime and :endTime ");
				}
			}
			logger.info("AlarmsHeaderServiceImpl queryAlarmsHeader: alarmsHeader={}", alarmsHeader);
			Query query = session.createQuery(hql.toString());
			if(UuiCommonUtil.isNotNullOrEmpty(alarmsHeader.getStartEpochMicrosec())&&UuiCommonUtil.isNotNullOrEmpty(alarmsHeader.getLastEpochMicroSec())) {
				query.setParameter("startTime",alarmsHeader.getStartEpochMicrosec());
				query.setParameter("endTime",alarmsHeader.getLastEpochMicroSec());
			}
			query.setFirstResult(offset);
			query.setMaxResults(pageSize);
			List<AlarmsHeader> list= query.list();
			page.setPageNo(currentPage);
			page.setPageSize(pageSize);
			page.setTotalRecords(allRow);
			page.setList(list);
			session.flush();
			return page;
		} catch (Exception e) {
			logger.error("exception occurred while performing AlarmsHeaderServiceImpl queryAlarmsHeader. Details:" + e.getMessage());
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AlarmsHeader> queryId(String[] id) {
		Session session = getSession();
		try{
			List<AlarmsHeader> list = new ArrayList<AlarmsHeader>();
			if(id.length==0) {
				logger.error("AlarmsHeaderServiceImpl queryId is null!");
				return list;
			}
			Query query = session.createQuery("from AlarmsHeader a where a.eventName IN (:alist)");
			list = query.setParameterList("alist", id).list();
			return list;
		} catch (Exception e) {
			logger.error("exception occurred while performing AlarmsHeaderServiceImpl queryId. Details:" + e.getMessage());
			return null;
		}
	}
	
	@Override
	public String updateAlarmsHeader2018(String status, Timestamp date, String startEpochMicrosecCleared, String lastEpochMicroSecCleared, String eventName, String reportingEntityName, String specificProblem) {
		Session session = getSession();
		try{
			//try(Session session = sessionFactory.getCurrentSession();){

			//Query q=session.createQuery("update AlarmsHeader set status='"+status+"', updateTime='"+date+"' , startEpochMicrosecCleared='"+startEpochMicrosecCleared+"'  ,lastEpochMicroSecCleared='"+lastEpochMicroSecCleared+"'    where eventName='"+eventName+"' and reportingEntityName='"+reportingEntityName+"' and specificProblem ='"+specificProblem+"'");
            Query q=session.createQuery("update AlarmsHeader set status=:status, startEpochMicrosecCleared=:startEpochMicrosecCleared  ,lastEpochMicroSecCleared=:lastEpochMicroSecCleared    where eventName=:eventName and reportingEntityName=:reportingEntityName and specificProblem =:specificProblem");

            q.setParameter("status",status);

            q.setParameter("startEpochMicrosecCleared",startEpochMicrosecCleared);
            q.setParameter("lastEpochMicroSecCleared",lastEpochMicroSecCleared);
            q.setParameter("eventName",eventName);
            q.setParameter("reportingEntityName",reportingEntityName);
            q.setParameter("specificProblem",specificProblem);


            q.executeUpdate();
			session.flush();
			return "1";
		} catch (Exception e) {
			logger.error("exception occurred while performing AlarmsInformationServiceImpl updateAlarmsInformation. Details:" + e.getMessage());
			return "0";
		}
	}

    @Override
    public String queryStatusCount(String status) {
        Session session = getSession();
        try{
            String hql = "select count(status) from AlarmsHeader a";
            if (!status.equals("0"))
                hql+=" where a.status = :status";
            Query query = session.createQuery(hql);
            if (!status.equals("0"))
                query.setParameter("status",status);
            return query.uniqueResult().toString();
        } catch (Exception e) {
            logger.error("exception occurred while performing AlarmsHeaderServiceImpl queryStatusCount. Details:" + e.getMessage());
            return null;
        }
    }
    
	@Override
	public AlarmsHeader getAlarmsHeaderById(String id) {
		Session session = getSession();
		try {

			String string = "from AlarmsHeader a where 1=1 and a.id=:id";
			Query q = session.createQuery(string);
			q.setParameter("id",id);
			AlarmsHeader alarmsHeader =(AlarmsHeader)q.uniqueResult();
 			return alarmsHeader;

		}catch (Exception e){
			logger.error("exception occurred while performing AlarmsHeaderServiceImpl getAlarmsHeaderDetail."+e.getMessage());
			return null;
		}
	}

	@Override
	public List<SortMaster> listSortMasters(String sortType) {
		Session session = getSession();
		try{
			StringBuffer hql =new StringBuffer("from SortMaster a where 1=1 and a.sortType=:sortType");
			Query query = session.createQuery(hql.toString());
			query.setParameter("sortType",sortType);
			List<SortMaster> list= query.list();
			return list;
		} catch (Exception e) {
			logger.error("exception occurred while performing AlarmsInformationServiceImpl listSortMasters. Details:" + e.getMessage());
			return Collections.emptyList();
		}
	}
}