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                 Transaction tx = session.beginTransaction();
152                 WidgetCatalog widget = (WidgetCatalog) session.get(WidgetCatalog.class, widgetCatalogId);
153                 tx.commit();
154                 logger.debug("WidgetCatalogServiceImpl.getWidgetCatalog: getting widget={}", widget);
155                 return widget;
156         }
157         
158         @Override
159         @Transactional
160         public void deleteWidgetCatalog(long widgetCatalogId) {
161                 logger.debug("WidgetCatalogServiceImpl.deleteWidgetCatalog: deleting the widget with widgetId={}", widgetCatalogId);
162                 WidgetCatalog widget = getWidgetCatalog(widgetCatalogId);
163                 if (widget == null){
164                         logger.error("No widget found in database while performing WidgetCatalogServiceImpl.deleteWidgetCatalog.");
165                         return;
166                 }
167                 Session session = sessionFactory.getCurrentSession();
168                 //Transaction tx = session.beginTransaction();
169                 Query query = session.createSQLQuery("delete from ep_pers_user_widget_sel where widget_id = :widgetId ").setParameter("widgetId", widgetCatalogId);
170                 query.executeUpdate();
171                 query = session.createSQLQuery("delete from ep_pers_user_widget_placement where widget_id = :widgetId ").setParameter("widgetId", widgetCatalogId);
172                 query.executeUpdate();
173                 query = session.createSQLQuery("delete from ep_widget_catalog_files where widget_id = :widgetId ").setParameter("widgetId", widgetCatalogId);
174                 query.executeUpdate();
175                 query = session.createSQLQuery("delete from ep_widget_catalog_parameter where widget_id = :widgetId ").setParameter("widgetId", widgetCatalogId);
176                 query.executeUpdate();
177                 session.delete(widget);
178                 //tx.commit();
179         }
180         
181         @Override
182         public long saveWidgetCatalog(WidgetCatalog newWidgetCatalog) {
183                 
184                 try{
185                         if(newWidgetCatalog.getAllowAllUser().equals("1"))
186                                 newWidgetCatalog.setAllowAllUser("Y");
187                         else
188                                 newWidgetCatalog.setAllowAllUser("N");
189                         
190                         logger.debug("WidgetCatalogServiceImpl.saveWidgetCatalog: widget={}", newWidgetCatalog);
191                         Session session = sessionFactory.openSession();
192                         Transaction tx = session.beginTransaction();            
193                         session.save(newWidgetCatalog);
194                         tx.commit();
195                         //session.flush();
196                         session.close();
197                         updateAppId(newWidgetCatalog.getId(), newWidgetCatalog.getWidgetRoles());
198                 }
199                 catch(Exception e){
200                         logger.error("Exception occurred while performing WidgetCatalogServiceImpl.saveWidgetCatalog in widget microservices. Details:" + e.getMessage());
201                 }
202                 return newWidgetCatalog.getId();
203         }
204
205         @Override
206         public void updateWidgetCatalog(Long widgetCatalogId, WidgetCatalog newWidgetCatalog) {
207                 logger.debug("WidgetCatalogServiceImpl.updateWidgetCatalog: widget={}", newWidgetCatalog);
208                 WidgetCatalog oldWidget = getWidgetCatalog(widgetCatalogId);
209                 try{
210                         if (newWidgetCatalog.getAllowAllUser().equals("1")) 
211                                 newWidgetCatalog.setAllowAllUser("Y");
212                         else 
213                                 newWidgetCatalog.setAllowAllUser("N");
214                         
215                         newWidgetCatalog.setId(widgetCatalogId);
216                         newWidgetCatalog.setServiceId(oldWidget.getServiceId());
217                         Session session = sessionFactory.openSession();
218                         Transaction tx = session.beginTransaction();
219                         session.update(newWidgetCatalog);
220                         tx.commit();
221                         //session.flush();
222                         session.close();
223                         updateAppId(newWidgetCatalog.getId(), newWidgetCatalog.getWidgetRoles());
224                 }catch(Exception e){
225                         logger.error("Exception occurred while performing WidgetCatalogServiceImpl.updateWidgetCatalog in widget microservices. Details:" + e.getMessage());
226                 }
227                 
228         }       
229         
230         @Override
231         public Long getServiceIdByWidget(Long widgetCatalogId) {
232                 Session session = sessionFactory.getCurrentSession();
233                 WidgetCatalog widget = (WidgetCatalog) session.get(WidgetCatalog.class, widgetCatalogId);
234                 logger.debug("WidgetCatalogServiceImpl.getServiceIdByWidget: result={}", widget);
235                 return widget.getServiceId();
236         }
237         
238         @Override
239         public List<WidgetCatalog> getWidgetsByServiceId(Long serviceId) {
240                 Session session = sessionFactory.getCurrentSession();
241                 Criteria criteria = session.createCriteria(WidgetCatalog.class)
242                                 .add(Restrictions.eq("serviceId", serviceId))
243                                 .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
244                 List<WidgetCatalog> widgets = criteria.list();
245                 logger.debug("WidgetCatalogServiceImpl.getWidgetCatalog: result={}", widgets);
246                 return widgets;
247         }
248
249         private void updateAppId(long widgetId, Set<RoleApp> roles){
250                 Session session = sessionFactory.openSession();
251                 for(RoleApp role: roles){
252                         String sql = "UPDATE ep_widget_catalog_role SET app_id = :appId WHERE widget_id = :widgetId AND ROLE_ID = :roleId" ;
253                         Query query = session.createSQLQuery(sql);
254                         query.setParameter("appId", role.getApp().getAppId());
255                         query.setParameter("widgetId", widgetId);
256                         query.setParameter("roleId", role.getRoleId());
257                         query.executeUpdate();
258                 }
259                 session.flush();
260                 session.close();
261         }
262
263         @Override
264         public boolean getWidgetIdByName(String newWidgetName) {
265                 Session session = sessionFactory.openSession();
266                 Criteria criteria = session.createCriteria(WidgetCatalog.class)
267                                 .add(Restrictions.eq("name", newWidgetName))
268                                 .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
269                 
270                 List<MicroserviceData> widgets = criteria.list();
271                 logger.debug("WidgetCatalogServiceImpl.getWidgetIdByName: result={}", widgets);
272 //              session.flush();
273                 session.close();
274                 
275                 return (widgets.size() > 0) ? true : false;
276         }
277
278         
279 }
280