1 package org.openecomp.portalapp.widget.service.impl;
 
   6 import javax.transaction.Transactional;
 
   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;
 
  24 @Service("widgetCatalogService")
 
  26 @org.springframework.context.annotation.Configuration
 
  27 @EnableAspectJAutoProxy
 
  28 public class WidgetCatalogServiceImpl implements WidgetCatalogService {
 
  30         private static final Logger logger = LoggerFactory.getLogger(WidgetCatalogServiceImpl.class);
 
  33         private SessionFactory sessionFactory;
 
  35         @SuppressWarnings("unchecked")
 
  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);
 
  47         @SuppressWarnings("unchecked")
 
  49         public List<WidgetCatalog> getUserWidgetCatalog(String loginName){
 
  50                 Session session = sessionFactory.getCurrentSession();
 
  51                 StringBuilder sql = new StringBuilder()
 
  54                                 .append("  select userWidgets.widget_id, userWidgets.wdg_name, userWidgets.wdg_desc, b.x, b.status_cd, b.y, b.height, b.width  from                                        ") 
 
  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                                                                                                                                                               ")
 
  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)                                                                 ")                   
 
  67                                 .append("       union all                                                                                                                                              ") 
 
  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'                                                                                                                            ") 
 
  74                                 .append("        ) userWidgets                                                                                                                                         ") 
 
  76                                 .append("  left join                                                                                                                                               ") 
 
  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)                                                                                 ") 
 
  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)                                                                                 ") 
 
  95                                 .append("                order by user_id, widget_id                                                                                                                       ") 
 
  98                                 .append("  (userWidgets.widget_id = b.widget_id) order by b.x;                                                                                                     ");  
 
 102                 Query query = session.createSQLQuery(sql.toString());
 
 103                 List<WidgetCatalog> widgets = query.list();
 
 104                 logger.debug("WidgetCatalogServiceImpl.getUserWidgetCatalog: result size={}", widgets);
 
 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);
 
 114                 logger.debug("WidgetCatalogServiceImpl.getWidgetCatalog: getting widget={}", widget);
 
 119         public void deleteWidgetCatalog(long widgetCatalogId) {
 
 120                 logger.debug("WidgetCatalogServiceImpl.deleteWidgetCatalog: deleting the widget with widgetId={}", widgetCatalogId);
 
 121                 WidgetCatalog widget = getWidgetCatalog(widgetCatalogId);
 
 123                         logger.error("No widget found in database while performing WidgetCatalogServiceImpl.deleteWidgetCatalog.");
 
 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);
 
 141         public long saveWidgetCatalog(WidgetCatalog newWidgetCatalog) {
 
 144                         if(newWidgetCatalog.getAllowAllUser().equals("1"))
 
 145                                 newWidgetCatalog.setAllowAllUser("Y");
 
 147                                 newWidgetCatalog.setAllowAllUser("N");
 
 149                         logger.debug("WidgetCatalogServiceImpl.saveWidgetCatalog: widget={}", newWidgetCatalog);
 
 150                         Session session = sessionFactory.openSession();
 
 151                         Transaction tx = session.beginTransaction();            
 
 152                         session.save(newWidgetCatalog);
 
 156                         updateAppId(newWidgetCatalog.getId(), newWidgetCatalog.getWidgetRoles());
 
 159                         logger.error("Exception occurred while performing WidgetCatalogServiceImpl.saveWidgetCatalog in widget microservices. Details:" + e.getMessage());
 
 161                 return newWidgetCatalog.getId();
 
 165         public void updateWidgetCatalog(Long widgetCatalogId, WidgetCatalog newWidgetCatalog) {
 
 166                 logger.debug("WidgetCatalogServiceImpl.updateWidgetCatalog: widget={}", newWidgetCatalog);
 
 167                 WidgetCatalog oldWidget = getWidgetCatalog(widgetCatalogId);
 
 169                         if (newWidgetCatalog.getAllowAllUser().equals("1")) 
 
 170                                 newWidgetCatalog.setAllowAllUser("Y");
 
 172                                 newWidgetCatalog.setAllowAllUser("N");
 
 174                         newWidgetCatalog.setId(widgetCatalogId);
 
 175                         newWidgetCatalog.setServiceId(oldWidget.getServiceId());
 
 176                         Session session = sessionFactory.openSession();
 
 177                         Transaction tx = session.beginTransaction();
 
 178                         session.update(newWidgetCatalog);
 
 182                         updateAppId(newWidgetCatalog.getId(), newWidgetCatalog.getWidgetRoles());
 
 184                         logger.error("Exception occurred while performing WidgetCatalogServiceImpl.updateWidgetCatalog in widget microservices. Details:" + e.getMessage());
 
 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();
 
 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);
 
 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();
 
 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);
 
 230                 List<MicroserviceData> widgets = criteria.list();
 
 231                 logger.debug("WidgetCatalogServiceImpl.getWidgetIdByName: result={}", widgets);
 
 235                 return (widgets.size() > 0) ? true : false;