aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main/java/org/onap/usecaseui/server/service/impl/PerformanceHeaderServiceImpl.java
blob: a403858c59e4e2b03624f87782997eee6682dbc3 (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
/**
 * 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.util.ArrayList;
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.PerformanceHeader;
import org.onap.usecaseui.server.service.PerformanceHeaderService;
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("PerformanceHeaderService")
@Transactional
@org.springframework.context.annotation.Configuration
@EnableAspectJAutoProxy
public class PerformanceHeaderServiceImpl implements PerformanceHeaderService {

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

	@Autowired
	private EntityManagerFactory entityManagerFactory;

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

	@Override
	public String savePerformanceHeader(PerformanceHeader performanceHeder) {
		Session session = getSession();
		try{
			if (null == performanceHeder){
				logger.error("PerformanceHeaderServiceImpl savePerformanceHeader performanceHeder is null!");
				return "0";
			}
			session.save(performanceHeder);
			session.flush();
			return "1";
		} catch (Exception e) {
			logger.error("exception occurred while performing PerformanceHeaderServiceImpl savePerformanceHeader. Details:" + e.getMessage());
			return "0";
		}
	}

	@Override
	public String updatePerformanceHeader(PerformanceHeader performanceHeder) {
		Session session = getSession();
		try{
			if (null == performanceHeder){
				logger.error("PerformanceHeaderServiceImpl updatePerformanceHeader performanceHeder is null!");
				return "0";
			}
			session.update(performanceHeder);
			session.flush();
			return "1";
		} catch (Exception e) {
			logger.error("exception occurred while performing PerformanceHeaderServiceImpl updatePerformanceHeader. Details:" + e.getMessage());
			return "0";
		}
	}

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

	@SuppressWarnings("unchecked")
	@Override
	public Page<PerformanceHeader> queryPerformanceHeader(PerformanceHeader performanceHeder, int currentPage, int pageSize) {
		Page<PerformanceHeader> page = new Page<PerformanceHeader>();
		int allRow =this.getAllCount(performanceHeder,currentPage,pageSize);
		int offset = page.countOffset(currentPage, pageSize);
		Session session = getSession();
		try{
			StringBuffer hql =new StringBuffer("from PerformanceHeader a where 1=1");
				if(UuiCommonUtil.isNotNullOrEmpty(performanceHeder.getSourceName())) {
					String ver =performanceHeder.getSourceName();
					hql.append(" and a.sourceName like '%"+ver+"%'");
				}
				if(UuiCommonUtil.isNotNullOrEmpty(performanceHeder.getStartEpochMicrosec())&& UuiCommonUtil.isNotNullOrEmpty(performanceHeder.getLastEpochMicroSec())) {
					hql.append(" and (CASE WHEN a.startEpochMicrosec=0 THEN a.lastEpochMicroSec ELSE a.startEpochMicrosec END) between :startTime and :endTime ");
				}
			Query query = session.createQuery(hql.toString());
			if(UuiCommonUtil.isNotNullOrEmpty(performanceHeder.getStartEpochMicrosec())&& UuiCommonUtil.isNotNullOrEmpty(performanceHeder.getLastEpochMicroSec())) {
				query.setParameter("startTime",performanceHeder.getStartEpochMicrosec()).setParameter("endTime",performanceHeder.getLastEpochMicroSec());
			}
			query.setFirstResult(offset);
			query.setMaxResults(pageSize);
			List<PerformanceHeader> 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 PerformanceHeaderServiceImpl queryPerformanceHeader. Details:" + e.getMessage());
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PerformanceHeader> queryId(String[] id) {
		Session session = getSession();
		try {
			List<PerformanceHeader> list = new ArrayList<PerformanceHeader>();
			if(id.length==0) {
				return list;
			}
			Query query = session.createQuery("from PerformanceHeader a where a.eventName IN (:alist)");
			list = query.setParameterList("alist", id).list();
			return list;
		} catch (Exception e) {
			logger.error("exception occurred while performing PerformanceHeaderServiceImpl queryId. Details:" + e.getMessage());
			return null;
		}
	}

	@Override
	public List<String> queryAllSourceNames() {
		Session session = getSession();
		try {
			Query query = session.createQuery("select distinct a.sourceName from PerformanceHeader a");
			return query.list();
		} catch (Exception e) {
			logger.error("exception occurred while performing PerformanceHeaderServiceImpl queryAllSourceId. Details:" + e.getMessage());
			return null;
		}
	}
	
	@Override
	public PerformanceHeader getPerformanceHeaderById(String id) {
		Session session = getSession();
		try {

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

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