2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.widget.service.impl;
40 import java.util.List;
43 import javax.transaction.Transactional;
45 import org.hibernate.Criteria;
46 import org.hibernate.Query;
47 import org.hibernate.Session;
48 import org.hibernate.SessionFactory;
49 import org.hibernate.Transaction;
50 import org.hibernate.criterion.Restrictions;
51 import org.onap.portalapp.widget.domain.MicroserviceData;
52 import org.onap.portalapp.widget.domain.RoleApp;
53 import org.onap.portalapp.widget.domain.WidgetCatalog;
54 import org.onap.portalapp.widget.service.WidgetCatalogService;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.context.annotation.EnableAspectJAutoProxy;
59 import org.springframework.stereotype.Service;
61 @Service("widgetCatalogService")
63 @org.springframework.context.annotation.Configuration
64 @EnableAspectJAutoProxy
65 public class WidgetCatalogServiceImpl implements WidgetCatalogService {
67 private static final Logger logger = LoggerFactory.getLogger(WidgetCatalogServiceImpl.class);
70 private SessionFactory sessionFactory;
72 @SuppressWarnings("unchecked")
75 public List<WidgetCatalog> getWidgetCatalog(){
76 Session session = sessionFactory.getCurrentSession();
77 Criteria criteria = session.createCriteria(WidgetCatalog.class)
78 .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
79 List<WidgetCatalog> widgets = criteria.list();
80 logger.debug("WidgetCatalogServiceImpl.getWidgetCatalog: result={}", widgets);
84 @SuppressWarnings("unchecked")
86 public List<WidgetCatalog> getUserWidgetCatalog(String loginName){
87 Session session = sessionFactory.getCurrentSession();
88 StringBuilder sql = new StringBuilder()
91 .append(" select userWidgets.widget_id, userWidgets.wdg_name, userWidgets.wdg_desc, b.x, b.status_cd, b.y, b.height, b.width from ")
93 .append(" select distinct w.widget_id, w.wdg_name, w.wdg_desc from ")
94 .append(" ep_widget_catalog w, ")
95 .append(" ep_widget_catalog_role wr, ")
96 .append(" fn_user_role ur, ")
97 .append(" fn_app app, ")
98 .append(" fn_user u ")
100 .append(" w.widget_id = wr.WIDGET_ID and ")
101 .append(" ur.app_id = app.app_id and ")
102 .append(" app.enabled = 'Y' and ")
103 .append(" wr.role_id = ur.role_id and ")
104 .append(" ur.user_id = u.user_id and ")
105 .append(" u.login_id = '" + loginName + "' and (w.all_user_flag = 'N' or w.all_user_flag is null) ")
107 .append(" union all ")
110 .append(" select distinct w.widget_id, w.wdg_name, w.wdg_desc from ")
111 .append(" ep_widget_catalog w ")
112 .append(" where w.all_user_flag = 'Y' ")
114 .append(" ) userWidgets ")
116 .append(" left join ")
119 .append(" select case when pers.user_id is null then sel.user_id else pers.user_id end as 'user_id', case when sel.widget_id is null then ")
120 .append(" pers.widget_id else sel.widget_id end as 'widget_id', pers.x, sel.status_cd, pers.y, pers.height, pers.width ")
121 .append(" from (select * from ep_pers_user_widget_placement where user_id = (select user_id from fn_user where login_id = '" + loginName + "')) pers ")
122 .append(" left outer join ")
123 .append(" (select * from ep_pers_user_widget_sel where user_id = (select user_id from fn_user where login_id = '" + loginName + "')) sel ")
124 .append(" on (pers.user_id = sel.user_id and pers.widget_id = sel.widget_id) ")
128 .append(" select case when pers.user_id is null then sel.user_id else pers.user_id end as 'user_id', case when sel.widget_id is null ")
129 .append(" then pers.widget_id else sel.widget_id end as 'widget_id', pers.x, sel.status_cd, pers.y, pers.height, pers.width ")
130 .append(" from (select * from ep_pers_user_widget_placement where user_id = (select user_id from fn_user where login_id = '" + loginName + "')) pers ")
131 .append(" right outer join ")
132 .append(" (select * from ep_pers_user_widget_sel where user_id = (select user_id from fn_user where login_id = '" + loginName + "')) sel ")
133 .append(" on (pers.user_id = sel.user_id and pers.widget_id = sel.widget_id) ")
135 .append(" order by user_id, widget_id ")
138 .append(" (userWidgets.widget_id = b.widget_id) order by b.x; ");
142 Query query = session.createSQLQuery(sql.toString());
143 List<WidgetCatalog> widgets = query.list();
144 logger.debug("WidgetCatalogServiceImpl.getUserWidgetCatalog: result size={}", widgets);
149 public WidgetCatalog getWidgetCatalog(Long widgetCatalogId) {
150 Session session = sessionFactory.getCurrentSession();
151 WidgetCatalog widget = (WidgetCatalog) session.get(WidgetCatalog.class, widgetCatalogId);
152 logger.debug("WidgetCatalogServiceImpl.getWidgetCatalog: getting widget={}", widget);
158 public void deleteWidgetCatalog(long widgetCatalogId) {
159 logger.debug("WidgetCatalogServiceImpl.deleteWidgetCatalog: deleting the widget with widgetId={}", widgetCatalogId);
160 WidgetCatalog widget = getWidgetCatalog(widgetCatalogId);
162 logger.error("No widget found in database while performing WidgetCatalogServiceImpl.deleteWidgetCatalog.");
165 Session session = sessionFactory.getCurrentSession();
166 //Transaction tx = session.beginTransaction();
167 Query query = session.createSQLQuery("delete from ep_pers_user_widget_sel where widget_id = :widgetId ").setParameter("widgetId", widgetCatalogId);
168 query.executeUpdate();
169 query = session.createSQLQuery("delete from ep_pers_user_widget_placement where widget_id = :widgetId ").setParameter("widgetId", widgetCatalogId);
170 query.executeUpdate();
171 query = session.createSQLQuery("delete from ep_widget_catalog_files where widget_id = :widgetId ").setParameter("widgetId", widgetCatalogId);
172 query.executeUpdate();
173 query = session.createSQLQuery("delete from ep_widget_catalog_parameter where widget_id = :widgetId ").setParameter("widgetId", widgetCatalogId);
174 query.executeUpdate();
175 session.delete(widget);
180 public long saveWidgetCatalog(WidgetCatalog newWidgetCatalog) {
183 if(newWidgetCatalog.getAllowAllUser().equals("1"))
184 newWidgetCatalog.setAllowAllUser("Y");
186 newWidgetCatalog.setAllowAllUser("N");
188 logger.debug("WidgetCatalogServiceImpl.saveWidgetCatalog: widget={}", newWidgetCatalog);
189 Session session = sessionFactory.openSession();
190 Transaction tx = session.beginTransaction();
191 session.save(newWidgetCatalog);
195 updateAppId(newWidgetCatalog.getId(), newWidgetCatalog.getWidgetRoles());
198 logger.error("Exception occurred while performing WidgetCatalogServiceImpl.saveWidgetCatalog in widget microservices. Details:" + e.getMessage());
200 return newWidgetCatalog.getId();
204 public void updateWidgetCatalog(Long widgetCatalogId, WidgetCatalog newWidgetCatalog) {
205 logger.debug("WidgetCatalogServiceImpl.updateWidgetCatalog: widget={}", newWidgetCatalog);
206 WidgetCatalog oldWidget = getWidgetCatalog(widgetCatalogId);
208 if (newWidgetCatalog.getAllowAllUser().equals("1"))
209 newWidgetCatalog.setAllowAllUser("Y");
211 newWidgetCatalog.setAllowAllUser("N");
213 newWidgetCatalog.setId(widgetCatalogId);
214 newWidgetCatalog.setServiceId(oldWidget.getServiceId());
215 Session session = sessionFactory.openSession();
216 Transaction tx = session.beginTransaction();
217 session.update(newWidgetCatalog);
221 updateAppId(newWidgetCatalog.getId(), newWidgetCatalog.getWidgetRoles());
223 logger.error("Exception occurred while performing WidgetCatalogServiceImpl.updateWidgetCatalog in widget microservices. Details:" + e.getMessage());
229 public Long getServiceIdByWidget(Long widgetCatalogId) {
230 Session session = sessionFactory.getCurrentSession();
231 WidgetCatalog widget = (WidgetCatalog) session.get(WidgetCatalog.class, widgetCatalogId);
232 logger.debug("WidgetCatalogServiceImpl.getServiceIdByWidget: result={}", widget);
233 return widget.getServiceId();
237 public List<WidgetCatalog> getWidgetsByServiceId(Long serviceId) {
238 Session session = sessionFactory.getCurrentSession();
239 Criteria criteria = session.createCriteria(WidgetCatalog.class)
240 .add(Restrictions.eq("serviceId", serviceId))
241 .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
242 List<WidgetCatalog> widgets = criteria.list();
243 logger.debug("WidgetCatalogServiceImpl.getWidgetCatalog: result={}", widgets);
247 private void updateAppId(long widgetId, Set<RoleApp> roles){
248 Session session = sessionFactory.openSession();
249 for(RoleApp role: roles){
250 String sql = "UPDATE ep_widget_catalog_role SET app_id = :appId WHERE widget_id = :widgetId AND ROLE_ID = :roleId" ;
251 Query query = session.createSQLQuery(sql);
252 query.setParameter("appId", role.getApp().getAppId());
253 query.setParameter("widgetId", widgetId);
254 query.setParameter("roleId", role.getRoleId());
255 query.executeUpdate();
262 public boolean getWidgetIdByName(String newWidgetName) {
263 Session session = sessionFactory.openSession();
264 Criteria criteria = session.createCriteria(WidgetCatalog.class)
265 .add(Restrictions.eq("name", newWidgetName))
266 .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
268 List<MicroserviceData> widgets = criteria.list();
269 logger.debug("WidgetCatalogServiceImpl.getWidgetIdByName: result={}", widgets);
273 return (widgets.size() > 0) ? true : false;