Merge "Widget MS startup fix and Onboarding changes"
[portal.git] / ecomp-portal-widget-ms / widget-ms / src / main / java / org / onap / portalapp / widget / service / impl / WidgetCatalogServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalapp.widget.service.impl;
39
40 import java.util.List;
41 import java.util.Set;
42
43 import javax.transaction.Transactional;
44
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;
60
61 @Service("widgetCatalogService")
62 @Transactional
63 @org.springframework.context.annotation.Configuration
64 @EnableAspectJAutoProxy
65 public class WidgetCatalogServiceImpl implements WidgetCatalogService {
66
67         private static final Logger logger = LoggerFactory.getLogger(WidgetCatalogServiceImpl.class);
68         
69         @Autowired
70         private SessionFactory sessionFactory;
71         
72         @SuppressWarnings("unchecked")
73         @Override
74         @Transactional
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);
81                 return widgets;
82         }
83         
84         @SuppressWarnings("unchecked")
85         @Transactional
86         public List<WidgetCatalog> getUserWidgetCatalog(String loginName){
87                 Session session = sessionFactory.getCurrentSession();
88                 StringBuilder sql = new StringBuilder()
89                                 
90
91                                 .append("  select userWidgets.widget_id, userWidgets.wdg_name, userWidgets.wdg_desc, b.x, b.status_cd, b.y, b.height, b.width  from                                        ") 
92                                 .append("  (                                                                                                                                                               ")
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                                                                                                                                                               ")
99                                 .append("  where                                                                                                                                                   ")
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)                                                                 ")                   
106                                 .append("                                                                                                                                                              ") 
107                                 .append("       union all                                                                                                                                              ") 
108                                 .append("                                                                                                                                                              ") 
109                                 .append("                                                                                                                                                              ") 
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'                                                                                                                            ") 
113                                 .append("                                                                                                                                                              ") 
114                                 .append("        ) userWidgets                                                                                                                                         ") 
115                                 .append("                                                                                                                                                              ") 
116                                 .append("  left join                                                                                                                                               ") 
117                                 .append("                                                                                                                                                              ") 
118                                 .append("  (                                                                                                                                                       ") 
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)                                                                                 ") 
125                                 .append("                                                                                                                                                                      ") 
126                                 .append("               union                                                                                                                                              ") 
127                                 .append("                                                                                                                                                                      ") 
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)                                                                                 ") 
134                                 .append("                                                                                                                                                                  ") 
135                                 .append("                order by user_id, widget_id                                                                                                                       ") 
136                                 .append(" )b                                                                                                                                                       ") 
137                                 .append("  on                                                                                                                                                      ") 
138                                 .append("  (userWidgets.widget_id = b.widget_id) order by b.x;                                                                                                     ");  
139                                 
140                                 
141                                 
142                 Query query = session.createSQLQuery(sql.toString());
143                 List<WidgetCatalog> widgets = query.list();
144                 logger.debug("WidgetCatalogServiceImpl.getUserWidgetCatalog: result size={}", widgets);
145                 return widgets;         
146         }
147
148         @Override
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);
153                 return widget;
154         }
155         
156         @Override
157         @Transactional
158         public void deleteWidgetCatalog(long widgetCatalogId) {
159                 logger.debug("WidgetCatalogServiceImpl.deleteWidgetCatalog: deleting the widget with widgetId={}", widgetCatalogId);
160                 WidgetCatalog widget = getWidgetCatalog(widgetCatalogId);
161                 if (widget == null){
162                         logger.error("No widget found in database while performing WidgetCatalogServiceImpl.deleteWidgetCatalog.");
163                         return;
164                 }
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);
176                 //tx.commit();
177         }
178         
179         @Override
180         public long saveWidgetCatalog(WidgetCatalog newWidgetCatalog) {
181                 
182                 try{
183                         if(newWidgetCatalog.getAllowAllUser().equals("1"))
184                                 newWidgetCatalog.setAllowAllUser("Y");
185                         else
186                                 newWidgetCatalog.setAllowAllUser("N");
187                         
188                         logger.debug("WidgetCatalogServiceImpl.saveWidgetCatalog: widget={}", newWidgetCatalog);
189                         Session session = sessionFactory.openSession();
190                         Transaction tx = session.beginTransaction();            
191                         session.save(newWidgetCatalog);
192                         tx.commit();
193                         //session.flush();
194                         session.close();
195                         updateAppId(newWidgetCatalog.getId(), newWidgetCatalog.getWidgetRoles());
196                 }
197                 catch(Exception e){
198                         logger.error("Exception occurred while performing WidgetCatalogServiceImpl.saveWidgetCatalog in widget microservices. Details:" + e.getMessage());
199                 }
200                 return newWidgetCatalog.getId();
201         }
202
203         @Override
204         public void updateWidgetCatalog(Long widgetCatalogId, WidgetCatalog newWidgetCatalog) {
205                 logger.debug("WidgetCatalogServiceImpl.updateWidgetCatalog: widget={}", newWidgetCatalog);
206                 WidgetCatalog oldWidget = getWidgetCatalog(widgetCatalogId);
207                 try{
208                         if (newWidgetCatalog.getAllowAllUser().equals("1")) 
209                                 newWidgetCatalog.setAllowAllUser("Y");
210                         else 
211                                 newWidgetCatalog.setAllowAllUser("N");
212                         
213                         newWidgetCatalog.setId(widgetCatalogId);
214                         newWidgetCatalog.setServiceId(oldWidget.getServiceId());
215                         Session session = sessionFactory.openSession();
216                         Transaction tx = session.beginTransaction();
217                         session.update(newWidgetCatalog);
218                         tx.commit();
219                         //session.flush();
220                         session.close();
221                         updateAppId(newWidgetCatalog.getId(), newWidgetCatalog.getWidgetRoles());
222                 }catch(Exception e){
223                         logger.error("Exception occurred while performing WidgetCatalogServiceImpl.updateWidgetCatalog in widget microservices. Details:" + e.getMessage());
224                 }
225                 
226         }       
227         
228         @Override
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();
234         }
235         
236         @Override
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);
244                 return widgets;
245         }
246
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();
256                 }
257                 session.flush();
258                 session.close();
259         }
260
261         @Override
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);
267                 
268                 List<MicroserviceData> widgets = criteria.list();
269                 logger.debug("WidgetCatalogServiceImpl.getWidgetIdByName: result={}", widgets);
270 //              session.flush();
271                 session.close();
272                 
273                 return (widgets.size() > 0) ? true : false;
274         }
275
276         
277 }
278