View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de 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.myportal.modules.myapps.services;
35  
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Locale;
39  import java.util.Map;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  import org.apache.commons.lang.StringUtils;
44  import org.apache.commons.lang3.math.NumberUtils;
45  
46  import fr.paris.lutece.plugins.myapps.business.MyApps;
47  import fr.paris.lutece.plugins.myapps.modules.database.business.MyAppsDatabaseHome;
48  import fr.paris.lutece.plugins.myapps.modules.database.service.MyAppsDatabasePlugin;
49  import fr.paris.lutece.plugins.myportal.business.Widget;
50  import fr.paris.lutece.plugins.myportal.service.handler.WidgetHandler;
51  import fr.paris.lutece.portal.service.i18n.I18nService;
52  import fr.paris.lutece.portal.service.plugin.Plugin;
53  import fr.paris.lutece.portal.service.plugin.PluginService;
54  import fr.paris.lutece.portal.service.security.LuteceUser;
55  import fr.paris.lutece.portal.service.template.AppTemplateService;
56  import fr.paris.lutece.portal.service.util.AppPropertiesService;
57  import fr.paris.lutece.util.html.HtmlTemplate;
58  
59  /**
60   *
61   * MyApps Widget Handler
62   *
63   */
64  public class MyPortalMyAppsWidgetHandler implements WidgetHandler
65  {
66      // Constants
67      private static final String NAME = "module.myportal.myapps.widget.name";
68      private static final String DESCRIPTION = "module.myportal.myapps.widget.description";
69      private static final boolean IS_CUSTOMIZABLE = true;
70  
71      // Templates
72      private static final String TEMPLATE_WIDGET_MYAPPS = "skin/plugins/myportal/modules/myapps/widget_myapps.html";
73  
74      // Properties
75      private static final String PROPERTY_MYAPPS_URL_RETURN = "myportal-myapps.urlReturn.myapps";
76      private static final String PROPERTY_MYPORTAL_URL_RETURN = "myportal-myapps.urlReturn.myportal";
77  
78      // Marks
79      private static final String MARK_USER_APPLICATIONS = "user_list_applications";
80      private static final String MARK_USER_TYPE_MESSAGERIE = "typeMessagerie";
81  
82      private static final String MARK_ID_WIDGET = "id_widget";
83      private static final String MARK_MYAPPS_URL_RETURN = "myapps_url_return";
84      private static final String MARK_MYPORTAL_URL_RETURN = "myportal_url_return";
85      public static final String BUSINESS_INFO_ONLINE_TYPE_MESSAGERIE = "user.business-info.mdpTypeMessagerie";
86  
87  
88      /**
89       * {@inheritDoc}
90       */
91      @Override
92      public String getName( )
93      {
94          return I18nService.getLocalizedString( NAME, Locale.FRENCH );
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public String getDescription( )
102     {
103         return I18nService.getLocalizedString( DESCRIPTION, Locale.FRENCH );
104     }
105 
106     /**
107      * {@inheritDoc}
108      */
109     @Override
110     public boolean isCustomizable( )
111     {
112         return IS_CUSTOMIZABLE;
113     }
114 
115     /**
116      * {@inheritDoc}
117      */
118     @Override
119     public String renderWidget( Widget widget, LuteceUser luteceUser, HttpServletRequest request )
120     {
121         // Retrieve the list of the application of the user
122         String strUserName = ( luteceUser != null ) ? luteceUser.getName( ) : StringUtils.EMPTY;
123         String typeMessagerie= ( luteceUser != null ) ? luteceUser.getUserInfos( ).get( BUSINESS_INFO_ONLINE_TYPE_MESSAGERIE ) : StringUtils.EMPTY;
124         typeMessagerie= ( typeMessagerie != null ) ? typeMessagerie : StringUtils.EMPTY;
125         Plugin plugin = PluginService.getPlugin( MyAppsDatabasePlugin.PLUGIN_NAME );
126         List<MyApps> listUserMyApps = MyAppsDatabaseHome.getMyAppsListByUser( strUserName, plugin );
127 
128         // Generate the model
129         Map<String, Object> model = new HashMap<String, Object>( );
130         model.put( MARK_USER_TYPE_MESSAGERIE, typeMessagerie );
131         model.put( MARK_USER_APPLICATIONS, listUserMyApps );
132         model.put( MARK_ID_WIDGET, ( widget != null ) ? widget.getIdWidget( ) : NumberUtils.INTEGER_MINUS_ONE );
133         model.put( MARK_MYAPPS_URL_RETURN, AppPropertiesService.getProperty( PROPERTY_MYAPPS_URL_RETURN ) );
134         model.put( MARK_MYPORTAL_URL_RETURN, AppPropertiesService.getProperty( PROPERTY_MYPORTAL_URL_RETURN ) );
135 
136         // Populate the template with the model
137         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_WIDGET_MYAPPS, request.getLocale( ), model );
138 
139         return template.getHtml( );
140     }
141 
142 }