1 package fr.paris.lutece.plugins.parisconnect.web.mydashboard; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Locale; 7 import java.util.Map; 8 9 import javax.servlet.http.HttpServletRequest; 10 11 import fr.paris.lutece.plugins.mydashboard.service.MyDashboardComponent; 12 import fr.paris.lutece.plugins.parisconnect.business.Alert; 13 import fr.paris.lutece.plugins.parisconnect.business.Category; 14 import fr.paris.lutece.plugins.parisconnect.business.CategoryHome; 15 import fr.paris.lutece.plugins.parisconnect.service.ParisConnectService; 16 import fr.paris.lutece.portal.service.i18n.I18nService; 17 import fr.paris.lutece.portal.service.security.LuteceUser; 18 import fr.paris.lutece.portal.service.security.SecurityService; 19 import fr.paris.lutece.portal.service.template.AppTemplateService; 20 import fr.paris.lutece.portal.web.l10n.LocaleService; 21 import fr.paris.lutece.util.html.HtmlTemplate; 22 23 public class MyDashboardAlerts extends MyDashboardComponent 24 { 25 private static final String DASHBOARD_COMPONENT_ID = "pariconnect-mydashboard.AlertsComponent"; 26 private static final String MESSAGE_DASHBOARD_COMPONENT_DESCRIPTION = "parisconnect.mydashboard.component.alerts.description"; 27 private static final String TEMPLATE_DASHBOARD_COMPONENT = "skin/plugins/parisconnect/mydashboard_alerts.html"; 28 private static final String MARK_ALERT_LIST = "alert_list"; 29 private static final String MARK_USER_ALERT_HASH = "user_alert_hash"; 30 private static final String MARK_CATEGORY_LIST = "category_list"; 31 private static final String MARK_CATEGORY_MAP = "category_map"; 32 private static final String MARK_CATEGORY_LIST_ALERT_HASH = "category_list_alert_hash"; 33 private static final String KEY_NO_CATEGORY = "no_category"; 34 35 36 @Override 37 public String getDashboardData( HttpServletRequest request ) 38 { 39 Map<String, Object> model = new HashMap<>(); 40 41 LuteceUser user = SecurityService.getInstance( ).getRegisteredUser(request); 42 43 44 45 HashMap<String,Alert> userAlertMap=new HashMap<>(); 46 47 if(user!=null) 48 { 49 List<Alert> listUserAlert=ParisConnectService.getInstance().getUserAlerts(user.getName()); 50 51 52 if(listUserAlert != null) 53 { 54 for(Alert alertUser:listUserAlert) 55 { 56 userAlertMap.put(alertUser.getIdPc(),alertUser); 57 } 58 } 59 } 60 61 List<Alert> listAlert=ParisConnectService.getInstance().getAlerts(); 62 HashMap<String ,List<Alert>> mapCategoryListAlert=new HashMap<>(); 63 mapCategoryListAlert.put(KEY_NO_CATEGORY, new ArrayList<Alert>()); 64 65 List<Category> listCategory=CategoryHome.getCategorysList(); 66 67 68 if(listCategory!=null && listCategory.size() > 0) 69 { 70 HashMap<String, Category> mapCategory=new HashMap<>(); 71 for(Category category:listCategory) 72 { 73 mapCategory.put(category.getCode(), category); 74 } 75 model.put( MARK_CATEGORY_MAP,mapCategory); 76 } 77 78 for(Alert alert:listAlert) 79 { 80 81 if(alert.getCategory()!=null) 82 { 83 if(!mapCategoryListAlert.containsKey(alert.getCategory())) 84 { 85 mapCategoryListAlert.put(alert.getCategory(), new ArrayList<Alert>()); 86 } 87 mapCategoryListAlert.get(alert.getCategory()).add(alert); 88 } 89 else 90 { 91 mapCategoryListAlert.get(KEY_NO_CATEGORY).add(alert); 92 } 93 94 } 95 96 97 98 99 model.put( MARK_USER_ALERT_HASH, userAlertMap); 100 model.put( MARK_ALERT_LIST, listAlert); 101 model.put( MARK_CATEGORY_LIST,listCategory ); 102 model.put(MARK_CATEGORY_LIST_ALERT_HASH,mapCategoryListAlert); 103 104 105 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_DASHBOARD_COMPONENT, 106 LocaleService.getDefault( ), model ); 107 108 return template.getHtml( ); 109 } 110 111 @Override 112 public String getComponentId( ) 113 { 114 return DASHBOARD_COMPONENT_ID; 115 } 116 117 @Override 118 public String getComponentDescription( Locale locale ) 119 { 120 return I18nService.getLocalizedString( MESSAGE_DASHBOARD_COMPONENT_DESCRIPTION, locale ); 121 } 122 123 }