Merge "Replicate demo/boot/portal_vm_init.sh"
[portal.git] / ecomp-portal-widget-ms / src / main / java / org / openecomp / portalapp / widget / service / impl / WidgetCatalogServiceImpl.java
1 package org.openecomp.portalapp.widget.service.impl;
2
3 import java.util.List;
4 import java.util.Set;
5
6 import javax.transaction.Transactional;
7
8 import org.hibernate.Criteria;
9 import org.hibernate.Query;
10 import org.hibernate.Session;
11 import org.hibernate.SessionFactory;
12 import org.hibernate.Transaction;
13 import org.hibernate.criterion.Restrictions;
14 import org.openecomp.portalapp.widget.domain.MicroserviceData;
15 import org.openecomp.portalapp.widget.domain.RoleApp;
16 import org.openecomp.portalapp.widget.domain.WidgetCatalog;
17 import org.openecomp.portalapp.widget.service.WidgetCatalogService;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.springframework.beans.factory.annotation.Autowired;
21 import org.springframework.context.annotation.EnableAspectJAutoProxy;
22 import org.springframework.stereotype.Service;
23
24 @Service("widgetCatalogService")
25 @Transactional
26 @org.springframework.context.annotation.Configuration
27 @EnableAspectJAutoProxy
28 public class WidgetCatalogServiceImpl implements WidgetCatalogService {
29
30         private static final Logger logger = LoggerFactory.getLogger(WidgetCatalogServiceImpl.class);
31         
32         @Autowired
33         private SessionFactory sessionFactory;
34         
35         @SuppressWarnings("unchecked")
36         @Override
37         @Transactional
38         public List<WidgetCatalog> getWidgetCatalog(){          
39                 Session session = sessionFactory.getCurrentSession();
40                 Criteria criteria = session.createCriteria(WidgetCatalog.class)
41                                 .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
42                 List<WidgetCatalog> widgets = criteria.list();
43                 logger.debug("WidgetCatalogServiceImpl.getWidgetCatalog: result={}", widgets);
44                 return widgets;
45         }
46         
47         @SuppressWarnings("unchecked")
48         @Transactional
49         public List<WidgetCatalog> getUserWidgetCatalog(String loginName){
50                 Session session = sessionFactory.getCurrentSession();
51                 StringBuilder sql = new StringBuilder()
52                                 
53
54                                 .append("  select userWidgets.widget_id, userWidgets.wdg_name, userWidgets.wdg_desc, b.x, b.status_cd, b.y, b.height, b.width  from                                        ") 
55                                 .append("  (                                                                                                                                                               ")
56                                 .append("  select distinct w.widget_id, w.wdg_name, w.wdg_desc from                                                                                                                                                        ")                    
57                                 .append("  ep_widget_catalog w,                                                                                                                                            ")
58                                 .append("  ep_widget_catalog_role wr,                                                                                                                                              ")
59                                 .append("  fn_user_role ur,                                                                                                                                                                                                                                                                                ")
60                                 .append("  fn_user u                                                                                                                                                               ")
61                                 .append("  where                                                                                                                                                   ")
62                                 .append("  w.widget_id = wr.WIDGET_ID and                                                                                                                                                                                                                                                  ")
63                                 .append("  wr.role_id = ur.role_id and                                                                                                                                                                                                                                             ")                                                                                                            
64                                 .append("  ur.user_id = u.user_id and                                                                                                                                                                                                                                              ")                                                                                                              
65                                 .append("  u.login_id = '" + loginName + "' and (w.all_user_flag = 'N' or w.all_user_flag is null)                                                                 ")                   
66                                 .append("                                                                                                                                                              ") 
67                                 .append("       union all                                                                                                                                              ") 
68                                 .append("                                                                                                                                                              ") 
69                                 .append("                                                                                                                                                              ") 
70                                 .append("  select distinct w.widget_id, w.wdg_name, w.wdg_desc from                                                                                                                        ") 
71                                 .append("       ep_widget_catalog w                                                                                                                                    ") 
72                                 .append("       where w.all_user_flag = 'Y'                                                                                                                            ") 
73                                 .append("                                                                                                                                                              ") 
74                                 .append("        ) userWidgets                                                                                                                                         ") 
75                                 .append("                                                                                                                                                              ") 
76                                 .append("  left join                                                                                                                                               ") 
77                                 .append("                                                                                                                                                              ") 
78                                 .append("  (                                                                                                                                                       ") 
79                                 .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                    ") 
80                                 .append("                       pers.widget_id else sel.widget_id end as  'widget_id', pers.x, sel.status_cd, pers.y, pers.height, pers.width                                  ") 
81                                 .append("                               from (select * from ep_pers_user_widget_placement where user_id = (select user_id from fn_user where login_id = '" + loginName + "')) pers ") 
82                                 .append("                                       left outer join                                                                                                                        ") 
83                                 .append("                               (select * from ep_pers_user_widget_sel where user_id = (select user_id from fn_user where login_id = '" + loginName + "')) sel             ") 
84                                 .append("               on (pers.user_id = sel.user_id and pers.widget_id = sel.widget_id)                                                                                 ") 
85                                 .append("                                                                                                                                                                      ") 
86                                 .append("               union                                                                                                                                              ") 
87                                 .append("                                                                                                                                                                      ") 
88                                 .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                       ") 
89                                 .append("                       then pers.widget_id else sel.widget_id end as  'widget_id', pers.x, sel.status_cd, pers.y, pers.height, pers.width                             ") 
90                                 .append("                               from (select * from ep_pers_user_widget_placement where user_id = (select user_id from fn_user where login_id = '" + loginName + "')) pers ") 
91                                 .append("                                       right outer join                                                                                                                       ") 
92                                 .append("                               (select * from ep_pers_user_widget_sel where user_id = (select user_id from fn_user where login_id = '" + loginName + "')) sel             ") 
93                                 .append("               on (pers.user_id = sel.user_id and pers.widget_id = sel.widget_id)                                                                                 ") 
94                                 .append("                                                                                                                                                                  ") 
95                                 .append("                order by user_id, widget_id                                                                                                                       ") 
96                                 .append(" )b                                                                                                                                                       ") 
97                                 .append("  on                                                                                                                                                      ") 
98                                 .append("  (userWidgets.widget_id = b.widget_id) order by b.x;                                                                                                     ");  
99                                 
100                                 
101                                 
102                 Query query = session.createSQLQuery(sql.toString());
103                 List<WidgetCatalog> widgets = query.list();
104                 logger.debug("WidgetCatalogServiceImpl.getUserWidgetCatalog: result size={}", widgets);
105                 return widgets;         
106         }
107
108         @Override
109         public WidgetCatalog getWidgetCatalog(Long widgetCatalogId) {
110                 Session session = sessionFactory.getCurrentSession();
111                 Transaction tx = session.beginTransaction();
112                 WidgetCatalog widget = (WidgetCatalog) session.get(WidgetCatalog.class, widgetCatalogId);
113                 tx.commit();
114                 logger.debug("WidgetCatalogServiceImpl.getWidgetCatalog: getting widget={}", widget);
115                 return widget;
116         }
117         
118         @Override
119         public void deleteWidgetCatalog(long widgetCatalogId) {
120                 logger.debug("WidgetCatalogServiceImpl.deleteWidgetCatalog: deleting the widget with widgetId={}", widgetCatalogId);
121                 WidgetCatalog widget = getWidgetCatalog(widgetCatalogId);
122                 if (widget == null){
123                         logger.error("No widget found in database while performing WidgetCatalogServiceImpl.deleteWidgetCatalog.");
124                         return;
125                 }
126                 Session session = sessionFactory.getCurrentSession();
127                 Transaction tx = session.beginTransaction();
128                 Query query = session.createSQLQuery("delete from ep_pers_user_widget_sel where widget_id = :widgetId ").setParameter("widgetId", widgetCatalogId);
129                 query.executeUpdate();
130                 query = session.createSQLQuery("delete from ep_pers_user_widget_placement where widget_id = :widgetId ").setParameter("widgetId", widgetCatalogId);
131                 query.executeUpdate();
132                 query = session.createSQLQuery("delete from ep_widget_catalog_files where widget_id = :widgetId ").setParameter("widgetId", widgetCatalogId);
133                 query.executeUpdate();
134                 query = session.createSQLQuery("delete from ep_widget_catalog_parameter where widget_id = :widgetId ").setParameter("widgetId", widgetCatalogId);
135                 query.executeUpdate();
136                 session.delete(widget);
137                 tx.commit();
138         }
139         
140         @Override
141         public long saveWidgetCatalog(WidgetCatalog newWidgetCatalog) {
142                 
143                 try{
144                         if(newWidgetCatalog.getAllowAllUser().equals("1"))
145                                 newWidgetCatalog.setAllowAllUser("Y");
146                         else
147                                 newWidgetCatalog.setAllowAllUser("N");
148                         
149                         logger.debug("WidgetCatalogServiceImpl.saveWidgetCatalog: widget={}", newWidgetCatalog);
150                         Session session = sessionFactory.openSession();
151                         Transaction tx = session.beginTransaction();            
152                         session.save(newWidgetCatalog);
153                         tx.commit();
154                         session.flush();
155                         session.close();
156                         updateAppId(newWidgetCatalog.getId(), newWidgetCatalog.getWidgetRoles());
157                 }
158                 catch(Exception e){
159                         logger.error("Exception occurred while performing WidgetCatalogServiceImpl.saveWidgetCatalog in widget microservices. Details:" + e.getMessage());
160                 }
161                 return newWidgetCatalog.getId();
162         }
163
164         @Override
165         public void updateWidgetCatalog(Long widgetCatalogId, WidgetCatalog newWidgetCatalog) {
166                 logger.debug("WidgetCatalogServiceImpl.updateWidgetCatalog: widget={}", newWidgetCatalog);
167                 WidgetCatalog oldWidget = getWidgetCatalog(widgetCatalogId);
168                 try{
169                         if (newWidgetCatalog.getAllowAllUser().equals("1")) 
170                                 newWidgetCatalog.setAllowAllUser("Y");
171                         else 
172                                 newWidgetCatalog.setAllowAllUser("N");
173                         
174                         newWidgetCatalog.setId(widgetCatalogId);
175                         newWidgetCatalog.setServiceId(oldWidget.getServiceId());
176                         Session session = sessionFactory.openSession();
177                         Transaction tx = session.beginTransaction();
178                         session.update(newWidgetCatalog);
179                         tx.commit();
180                         session.flush();
181                         session.close();
182                         updateAppId(newWidgetCatalog.getId(), newWidgetCatalog.getWidgetRoles());
183                 }catch(Exception e){
184                         logger.error("Exception occurred while performing WidgetCatalogServiceImpl.updateWidgetCatalog in widget microservices. Details:" + e.getMessage());
185                 }
186                 
187         }       
188         
189         @Override
190         public Long getServiceIdByWidget(Long widgetCatalogId) {
191                 Session session = sessionFactory.getCurrentSession();
192                 WidgetCatalog widget = (WidgetCatalog) session.get(WidgetCatalog.class, widgetCatalogId);
193                 logger.debug("WidgetCatalogServiceImpl.getServiceIdByWidget: result={}", widget);
194                 return widget.getServiceId();
195         }
196         
197         @Override
198         public List<WidgetCatalog> getWidgetsByServiceId(Long serviceId) {
199                 Session session = sessionFactory.getCurrentSession();
200                 Criteria criteria = session.createCriteria(WidgetCatalog.class)
201                                 .add(Restrictions.eq("serviceId", serviceId))
202                                 .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
203                 List<WidgetCatalog> widgets = criteria.list();
204                 logger.debug("WidgetCatalogServiceImpl.getWidgetCatalog: result={}", widgets);
205                 return widgets;
206         }
207         
208         
209         
210         
211         
212         private void updateAppId(long widgetId, Set<RoleApp> roles){
213                 Session session = sessionFactory.openSession();
214                 for(RoleApp role: roles){
215                         String sql = "UPDATE ep_widget_catalog_role SET app_id = " + role.getApp().getAppId() + " WHERE widget_id = " + widgetId + " AND ROLE_ID = " + role.getRoleId() ;
216                         Query query = session.createSQLQuery(sql);
217                         query.executeUpdate();
218                 }
219                 session.flush();
220                 session.close();
221         }
222
223         @Override
224         public boolean getWidgetIdByName(String newWidgetName) {
225                 Session session = sessionFactory.openSession();
226                 Criteria criteria = session.createCriteria(WidgetCatalog.class)
227                                 .add(Restrictions.eq("name", newWidgetName))
228                                 .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
229                 
230                 List<MicroserviceData> widgets = criteria.list();
231                 logger.debug("WidgetCatalogServiceImpl.getWidgetIdByName: result={}", widgets);
232                 session.flush();
233                 session.close();
234                 
235                 return (widgets.size() > 0) ? true : false;
236         }
237
238         
239 }
240