View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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 fr.paris.lutece.plugins.broadcastproxy.business.Subscription;
37  import fr.paris.lutece.plugins.broadcastproxy.service.BroadcastService;
38  import fr.paris.lutece.plugins.mydashboard.service.MyDashboardComponent;
39  import fr.paris.lutece.portal.service.i18n.I18nService;
40  import fr.paris.lutece.portal.service.security.LuteceUser;
41  import fr.paris.lutece.portal.service.security.SecurityService;
42  import fr.paris.lutece.portal.service.template.AppTemplateService;
43  import fr.paris.lutece.portal.service.util.AppLogService;
44  import fr.paris.lutece.portal.service.util.AppPropertiesService;
45  import fr.paris.lutece.portal.util.mvc.utils.MVCMessage;
46  import fr.paris.lutece.portal.web.l10n.LocaleService;
47  import fr.paris.lutece.util.ErrorMessage;
48  import fr.paris.lutece.util.html.HtmlTemplate;
49  import java.util.ArrayList;
50  import java.util.HashMap;
51  import java.util.List;
52  import java.util.Locale;
53  import java.util.Map;
54  
55  import javax.servlet.http.HttpServletRequest;
56  import org.codehaus.plexus.util.StringUtils;
57  
58  public class MyDashboardBroadcastproxy extends MyDashboardComponent
59  {
60      /**
61       * 
62       */
63      private static final long serialVersionUID = 1L;
64  
65      // PROPERTIES 
66      private static final String PROPERTY_MYDASHBOARD_DESCRIPTION = "broadcastproxy.component.broadcastproxy.description";
67      private static final String PROPERTY_MSG_ERROR_GET_USER_SUBSCRIPTIONS = "broadcastproxy.msg.ERROR_GET_USER_SUBSCRIPTIONS";
68  
69      // Markers
70      private static final String MARK_USER_SUBSCRIPTIONS = "user_subscriptions";
71      private static final String MARK_SUBSCRIPTION_VIEW_ORDER = "subscriptions_order";
72      private static final String MARK_BROADCASTPROXY = "broadcastproxy";
73      private static final String MARK_LUTECE_USER = "user";
74      private static final String MARK_INFOS = "infos";
75      private static final String MARK_SUBSCRIPTIONS_DESCRIPTION = "descriptions";
76  
77      // instance variables
78      private List<ErrorMessage> _listInfos = new ArrayList<>( );
79  
80      // constants
81      private static final String MYDASHBOARD_BROADCASTPROXY_ID = "broadcastproxy.myDashboard";
82      private static final String KEY_USER_INFO_MAIL = "broadcastproxy.userInfoKeys.mail";
83  
84      private static final String SUB_DESCRIPTION_CONSTANT_PREFIX = AppPropertiesService.getProperty( "subscription.description.CONSTANT_PREFIX" );
85  
86      // Templates
87      private static final String TEMPLATE_DASHBOARD = "skin/plugins/mydashboard/modules/broadcastproxy/broadcastproxy_mydashboard.html";
88  
89      // SESSION DATA
90      private LuteceUser _luteceUser;
91  
92      @Override
93      public String getDashboardData( HttpServletRequest request )
94      {
95          _listInfos.clear( );
96          Map<String, Object> model = new HashMap<>( );
97          Map<String, String> subscriptionsDescription = new HashMap<String, String>( );
98          List<String> subscriptionViewOrder = new ArrayList<String>( );
99  
100         if ( SecurityService.isAuthenticationEnable( ) )
101         {
102             _luteceUser = SecurityService.getInstance( ).getRegisteredUser( request );
103             if ( _luteceUser != null )
104             {
105                 model.put( MARK_LUTECE_USER, _luteceUser );
106             }
107         }
108 
109         if ( _luteceUser != null )
110         {
111             Map<String, Map<String, List<Subscription>>> userSubscriptions = new HashMap<>( );
112 
113             String userMail = _luteceUser.getEmail( );
114             if ( StringUtils.isBlank( userMail ) )
115             {
116                 String mailUserInfoKey = AppPropertiesService.getProperty( KEY_USER_INFO_MAIL );
117                 if ( !StringUtils.isBlank( mailUserInfoKey ) )
118                 {
119                     userMail = _luteceUser.getUserInfo( mailUserInfoKey );
120                 }
121             }
122 
123             try
124             {
125                 BroadcastService broadcastService = BroadcastService.getInstance( );
126 
127                 userSubscriptions = broadcastService.getUserSubscriptionsAsList( userMail );
128 
129                 subscriptionViewOrder = broadcastService.getSubscriptionViewOrder( );
130 
131                 subscriptionsDescription = getSubscriptionsDescription( );
132 
133                 model.put( MARK_BROADCASTPROXY, broadcastService.getName( ) );
134             }
135             catch( Exception e )
136             {
137                 addInfo( I18nService.getLocalizedString( PROPERTY_MSG_ERROR_GET_USER_SUBSCRIPTIONS, LocaleService.getDefault( ) ) );
138                 AppLogService.error( e.getMessage( ) );
139             }
140 
141             model.put( MARK_USER_SUBSCRIPTIONS, userSubscriptions );
142             model.put( MARK_SUBSCRIPTION_VIEW_ORDER, subscriptionViewOrder );
143             model.put( MARK_INFOS, _listInfos );
144             model.put( MARK_SUBSCRIPTIONS_DESCRIPTION, subscriptionsDescription );
145         }
146 
147         HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_DASHBOARD, LocaleService.getUserSelectedLocale( request ), model );
148 
149         return t.getHtml( );
150     }
151 
152     @Override
153     public String getComponentId( )
154     {
155         return MYDASHBOARD_BROADCASTPROXY_ID;
156     }
157 
158     @Override
159     public String getComponentDescription( Locale locale )
160     {
161         return I18nService.getLocalizedString( PROPERTY_MYDASHBOARD_DESCRIPTION, locale );
162     }
163 
164     private Map<String, String> getSubscriptionsDescription( )
165     {
166         Map<String, String> descriptions = new HashMap<String, String>( );
167 
168         List<String> descriptionsList = AppPropertiesService.getKeys( SUB_DESCRIPTION_CONSTANT_PREFIX );
169         for ( String description : descriptionsList )
170         {
171             descriptions.put( description.substring( SUB_DESCRIPTION_CONSTANT_PREFIX.length( ) + 1 ), AppPropertiesService.getProperty( description ) );
172         }
173 
174         return descriptions;
175     }
176 
177     /**
178      * Add an info message
179      * 
180      * @param strMessage
181      *            The message
182      */
183     protected void addInfo( String strMessage )
184     {
185         _listInfos.add( new MVCMessage( strMessage ) );
186     }
187 
188     /**
189      * Add an info message
190      * 
191      * @param strMessageKey
192      *            The message key
193      * @param locale
194      *            The locale
195      */
196     protected void addInfo( String strMessageKey, Locale locale )
197     {
198         _listInfos.add( new MVCMessage( I18nService.getLocalizedString( strMessageKey, locale ) ) );
199     }
200 }