aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main/java/org/onap/usecaseui/server/service/impl/PerformanceInformationServiceImpl.java
blob: 0cc9919334ce9d6c0ebd03d8345cdf66fdfc2e71 (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
/*
 * 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.Date;
import java.util.List;


import javax.persistence.EntityManagerFactory;
import javax.transaction.Transactional;

import com.google.common.base.Throwables;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.onap.usecaseui.server.bean.PerformanceInformation;
import org.onap.usecaseui.server.service.PerformanceInformationService;
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("PerformanceInformationService")
@Transactional
@org.springframework.context.annotation.Configuration
@EnableAspectJAutoProxy
public class PerformanceInformationServiceImpl implements PerformanceInformationService {

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

	@Autowired
	private EntityManagerFactory entityManagerFactory;

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

	@Override
	public String savePerformanceInformation(PerformanceInformation performanceInformation) {
		Session session = getSession();
		try {
			if (null == performanceInformation) {
			}
			session.save(performanceInformation);
			session.flush();
			return "1";
		} catch (Exception e) {
			logger.error("exception occurred while performing PerformanceInformationServiceImpl savePerformanceInformation. Details:" + e.getMessage());
			return "0";
		}
	}

	@Override
	public String updatePerformanceInformation(PerformanceInformation performanceInformation) {
		Session session = getSession();
		try {
			if (null == performanceInformation) {
			}
			logger.info("PerformanceInformationServiceImpl updatePerformanceInformation: performanceInformation={}", performanceInformation);
			session.update(performanceInformation);
			session.flush();
			return "1";
		} catch (Exception e) {
			logger.error("exception occurred while performing PerformanceInformationServiceImpl updatePerformanceInformation. Details:" + e.getMessage());
			return "0";
		}
	}

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

	@SuppressWarnings("unchecked")
	@Override
	public List<PerformanceInformation> queryDateBetween(String sourceId, Date startDate, Date endDate) {
		Session session = getSession();
		try {
			List<PerformanceInformation> list = new ArrayList<>();
			Query query = session.createQuery("from PerformanceInformation a where a.sourceId = :sourceId and a.createTime BETWEEN :startDate and :endDate");
			list = query.setParameter("sourceId",sourceId).setParameter("startDate", startDate).setParameter("endDate",endDate).list();
			return list;
		} catch (Exception e) {
			logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDateBetween. Details:" + e.getMessage());
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public int queryDataBetweenSum(String sourceId, String name, Date startDate, Date endDate){
		Session session = getSession();
		try {
			int sum = 0;
			Query query = session.createQuery("select sum(a.value) from PerformanceInformation a where a.sourceId = :sourceId and a.name = :name and a.createTime BETWEEN :startDate and :endDate");
			sum = Integer.parseInt(query.setParameter("sourceId",sourceId).setParameter("name",name).setParameter("startDate", startDate).setParameter("endDate",endDate).uniqueResult().toString());
			return sum;
		} catch (Exception e) {
			logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDataBetweenSum. Details:" + e.getMessage());
			return 0;
		}
	}

	@Override
	public List<PerformanceInformation> queryDateBetween(String resourceId, String name, String startTime, String endTime) {
		Session session = getSession();
		try {
			String hql = "from PerformanceInformation a where 1=1 ";
			if (resourceId != null && !"".equals(resourceId)){
				hql += " and a.sourceId = :resourceId";
			}
			if (name != null && !"".equals(name)){
				hql += " and a.name = :name ";
			}
			if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
				hql += " and a.createTime between :startTime and :endTime ";
			}
			Query query = session.createQuery(hql);
			if (resourceId != null && !"".equals(resourceId)){
				query.setString("resourceId",resourceId);
			}
			if (name != null && !"".equals(name)){
				query.setString("name",name);
			}
			if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
				query.setString("startTime", startTime).setString("endTime", endTime);
			}
			logger.info("PerformanceInformationServiceImpl queryDateBetween: list={}", query.list());
			return query.list();
		} catch (Exception e) {
			logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDateBetween. Details:" + e.getMessage());
			return null;
		}
	}
	
	@Override
	public List<PerformanceInformation> getAllPerformanceInformationByHeaderId(String headerId) {
		Session session = getSession();
		try {
			String string = "from PerformanceInformation a where 1=1 and a.headerId=:headerId";
			Query query = session.createQuery(string);
			query.setString("headerId",headerId);
			List<PerformanceInformation> list = query.list();
			session.flush();
			return list;
		}catch (Exception e){
			logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDateBetween. LIST:" + e.getMessage());

			return null;
		}
	}
	
    @Override
    public String queryMaxValueByBetweenDate(String sourceId, String name, String startTime, String endTime) {
			Session session = getSession();
		     try {
            String hql = "select max(a.value) from PerformanceInformation a where 1=1 ";
            if (sourceId != null && !"".equals(sourceId)){
                hql += " and a.sourceId = :resourceId";
            }
            if (name != null && !"".equals(name)){
                hql += " and a.name = :name ";
            }
            if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
            	hql += " and (CASE WHEN a.startEpochMicrosec=0 THEN a.lastEpochMicroSec ELSE a.startEpochMicrosec END) between :startTime and :endTime ";
            }
            Query query = session.createQuery(hql);
            if (sourceId != null && !"".equals(sourceId)){
                query.setString("resourceId",sourceId);
            }
            if (name != null && !"".equals(name)){
                query.setString("name",name);
            }
            if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
                query.setString("startTime", startTime).setString("endTime", endTime);
            }
            String num=(String) query.uniqueResult();
            return UuiCommonUtil.isNotNullOrEmpty(num)?num:0+"";
        } catch (Exception e) {
			logger.error("exception occurred while performing PerformanceInformationServiceImpl queryMaxValueByBetweenDate. Details:" + Throwables.getStackTraceAsString(e));
            logger.error("exception occurred while performing PerformanceInformationServiceImpl queryMaxValueByBetweenDate. Details:" + e.getMessage());
            return 0+"";
        }
    }
}