View Javadoc
1   /*
2    * Copyright (c) 2002-2023, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.broadcastproxy.web;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Locale;
40  import java.util.Map;
41  
42  import javax.servlet.http.HttpServletRequest;
43  
44  import org.apache.commons.lang3.StringUtils;
45  
46  import fr.paris.lutece.plugins.broadcastproxy.business.SubscriptionLinkHome;
47  import fr.paris.lutece.plugins.broadcastproxy.service.BroadcastService;
48  import fr.paris.lutece.plugins.mydashboard.service.MyDashboardComponent;
49  import fr.paris.lutece.portal.service.fileimage.FileImagePublicService;
50  import fr.paris.lutece.portal.service.i18n.I18nService;
51  import fr.paris.lutece.portal.service.security.LuteceUser;
52  import fr.paris.lutece.portal.service.security.SecurityService;
53  import fr.paris.lutece.portal.service.template.AppTemplateService;
54  import fr.paris.lutece.portal.service.util.AppPropertiesService;
55  import fr.paris.lutece.portal.util.mvc.utils.MVCMessage;
56  import fr.paris.lutece.portal.web.l10n.LocaleService;
57  import fr.paris.lutece.util.ErrorMessage;
58  import fr.paris.lutece.util.html.HtmlTemplate;
59  
60  public class MyDashboardBroadcastproxy extends MyDashboardComponent
61  {
62      /**
63       * 
64       */
65      private static final long serialVersionUID = 1L;
66  
67      // PROPERTIES
68      private static final String PROPERTY_MYDASHBOARD_DESCRIPTION = "broadcastproxy.component.broadcastproxy.description";
69      // Markers
70      private static final String MARK_BROADCASTPROXY = "broadcastproxy";
71      private static final String MARK_LUTECE_USER = "user";
72      private static final String MARK_INFOS = "infos";
73      private static final String MARK_LIST_SUBSCRIPTIONS = "listSubscriptions";
74  
75      // instance variables
76      private List<ErrorMessage> _listInfos = new ArrayList<>( );
77  
78      // constants
79      private static final String MYDASHBOARD_BROADCASTPROXY_ID = "broadcastproxy.myDashboard";
80      private static final String KEY_USER_INFO_MAIL = "broadcastproxy.userInfoKeys.mail";
81  
82      // Templates
83      private static final String TEMPLATE_DASHBOARD = "skin/plugins/mydashboard/modules/broadcastproxy/broadcastproxy_mydashboard.html";
84  
85      // SESSION DATA
86      private LuteceUser _luteceUser;
87  
88      @Override
89      public String getDashboardData( HttpServletRequest request )
90      {
91          _listInfos.clear( );
92          Map<String, Object> model = new HashMap<>( );
93  
94          if ( SecurityService.isAuthenticationEnable( ) )
95          {
96              _luteceUser = SecurityService.getInstance( ).getRegisteredUser( request );
97              if ( _luteceUser != null )
98              {
99                  model.put( MARK_LUTECE_USER, _luteceUser );
100             }
101         }
102 
103         if ( _luteceUser != null )
104         {
105             String userMail = _luteceUser.getEmail( );
106             if ( StringUtils.isBlank( userMail ) )
107             {
108                 String mailUserInfoKey = AppPropertiesService.getProperty( KEY_USER_INFO_MAIL );
109                 if ( !StringUtils.isBlank( mailUserInfoKey ) )
110                 {
111                     userMail = _luteceUser.getUserInfo( mailUserInfoKey );
112                 }
113             }
114             FileImagePublicService.init( );
115             BroadcastService broadcastService = BroadcastService.getInstance( );
116             model.put( MARK_BROADCASTPROXY, broadcastService.getName( ) );
117             model.put( MARK_LIST_SUBSCRIPTIONS, SubscriptionLinkHome.getSubscriptionLinksList( ) );
118             
119             model.put( MARK_INFOS, _listInfos );
120         }
121 
122         HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_DASHBOARD, LocaleService.getUserSelectedLocale( request ), model );
123 
124         return t.getHtml( );
125     }
126 
127     @Override
128     public String getComponentId( )
129     {
130         return MYDASHBOARD_BROADCASTPROXY_ID;
131     }
132 
133     @Override
134     public String getComponentDescription( Locale locale )
135     {
136         return I18nService.getLocalizedString( PROPERTY_MYDASHBOARD_DESCRIPTION, locale );
137     }
138 
139     /**
140      * Add an info message
141      * 
142      * @param strMessage
143      *            The message
144      */
145     protected void addInfo( String strMessage )
146     {
147         _listInfos.add( new MVCMessage( strMessage ) );
148     }
149 
150     /**
151      * Add an info message
152      * 
153      * @param strMessageKey
154      *            The message key
155      * @param locale
156      *            The locale
157      */
158     protected void addInfo( String strMessageKey, Locale locale )
159     {
160         _listInfos.add( new MVCMessage( I18nService.getLocalizedString( strMessageKey, locale ) ) );
161     }
162 }