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 Transaction tx = session.beginTransaction();
152 WidgetCatalog widget = (WidgetCatalog) session.get(WidgetCatalog.class, widgetCatalogId);
154 logger.debug("WidgetCatalogServiceImpl.getWidgetCatalog: getting widget={}", widget);
159 public void deleteWidgetCatalog(long widgetCatalogId) {
160 logger.debug("WidgetCatalogServiceImpl.deleteWidgetCatalog: deleting the widget with widgetId={}", widgetCatalogId);
161 WidgetCatalog widget = getWidgetCatalog(widgetCatalogId);
163 logger.error("No widget found in database while performing WidgetCatalogServiceImpl.deleteWidgetCatalog.");
166 Session session = sessionFactory.getCurrentSession();
167 Transaction tx = session.beginTransaction();
168 Query query = session.createSQLQuery("delete from ep_pers_user_widget_sel where widget_id = :widgetId ").setParameter("widgetId", widgetCatalogId);
169 query.executeUpdate();
170 query = session.createSQLQuery("delete from ep_pers_user_widget_placement where widget_id = :widgetId ").setParameter("widgetId", widgetCatalogId);
171 query.executeUpdate();
172 query = session.createSQLQuery("delete from ep_widget_catalog_files where widget_id = :widgetId ").setParameter("widgetId", widgetCatalogId);
173 query.executeUpdate();
174 query = session.createSQLQuery("delete from ep_widget_catalog_parameter where widget_id = :widgetId ").setParameter("widgetId", widgetCatalogId);
175 query.executeUpdate();
176 session.delete(widget);
181 public long saveWidgetCatalog(WidgetCatalog newWidgetCatalog) {
184 if(newWidgetCatalog.getAllowAllUser().equals("1"))
185 newWidgetCatalog.setAllowAllUser("Y");
187 newWidgetCatalog.setAllowAllUser("N");
189 logger.debug("WidgetCatalogServiceImpl.saveWidgetCatalog: widget={}", newWidgetCatalog);
190 Session session = sessionFactory.openSession();
191 Transaction tx = session.beginTransaction();
192 session.save(newWidgetCatalog);
196 updateAppId(newWidgetCatalog.getId(), newWidgetCatalog.getWidgetRoles());
199 logger.error("Exception occurred while performing WidgetCatalogServiceImpl.saveWidgetCatalog in widget microservices. Details:" + e.getMessage());
201 return newWidgetCatalog.getId();
205 public void updateWidgetCatalog(Long widgetCatalogId, WidgetCatalog newWidgetCatalog) {
206 logger.debug("WidgetCatalogServiceImpl.updateWidgetCatalog: widget={}", newWidgetCatalog);
207 WidgetCatalog oldWidget = getWidgetCatalog(widgetCatalogId);
209 if (newWidgetCatalog.getAllowAllUser().equals("1"))
210 newWidgetCatalog.setAllowAllUser("Y");
212 newWidgetCatalog.setAllowAllUser("N");
214 newWidgetCatalog.setId(widgetCatalogId);
215 newWidgetCatalog.setServiceId(oldWidget.getServiceId());
216 Session session = sessionFactory.openSession();
217 Transaction tx = session.beginTransaction();
218 session.update(newWidgetCatalog);
222 updateAppId(newWidgetCatalog.getId(), newWidgetCatalog.getWidgetRoles());
224 logger.error("Exception occurred while performing WidgetCatalogServiceImpl.updateWidgetCatalog in widget microservices. Details:" + e.getMessage());
230 public Long getServiceIdByWidget(Long widgetCatalogId) {
231 Session session = sessionFactory.getCurrentSession();
232 WidgetCatalog widget = (WidgetCatalog) session.get(WidgetCatalog.class, widgetCatalogId);
233 logger.debug("WidgetCatalogServiceImpl.getServiceIdByWidget: result={}", widget);
234 return widget.getServiceId();
238 public List<WidgetCatalog> getWidgetsByServiceId(Long serviceId) {
239 Session session = sessionFactory.getCurrentSession();
240 Criteria criteria = session.createCriteria(WidgetCatalog.class)
241 .add(Restrictions.eq("serviceId", serviceId))
242 .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
243 List<WidgetCatalog> widgets = criteria.list();
244 logger.debug("WidgetCatalogServiceImpl.getWidgetCatalog: result={}", widgets);
248 private void updateAppId(long widgetId, Set<RoleApp> roles){
249 Session session = sessionFactory.openSession();
250 for(RoleApp role: roles){
251 String sql = "UPDATE ep_widget_catalog_role SET app_id = :appId WHERE widget_id = :widgetId AND ROLE_ID = :roleId" ;
252 Query query = session.createSQLQuery(sql);
253 query.setParameter("appId", role.getApp().getAppId());
254 query.setParameter("widgetId", widgetId);
255 query.setParameter("roleId", role.getRoleId());
256 query.executeUpdate();
263 public boolean getWidgetIdByName(String newWidgetName) {
264 Session session = sessionFactory.openSession();
265 Criteria criteria = session.createCriteria(WidgetCatalog.class)
266 .add(Restrictions.eq("name", newWidgetName))
267 .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
269 List<MicroserviceData> widgets = criteria.list();
270 logger.debug("WidgetCatalogServiceImpl.getWidgetIdByName: result={}", widgets);
274 return (widgets.size() > 0) ? true : false;