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.myapps.business.portlet;
35  
36  import fr.paris.lutece.plugins.myapps.business.MyApps;
37  import fr.paris.lutece.plugins.myapps.service.MyAppsManager;
38  import fr.paris.lutece.plugins.myapps.service.MyAppsPlugin;
39  import fr.paris.lutece.plugins.myapps.service.MyAppsProvider;
40  import fr.paris.lutece.plugins.myapps.service.parameter.MyAppsParameterService;
41  import fr.paris.lutece.plugins.myapps.service.portlet.MyAppsPortletService;
42  import fr.paris.lutece.portal.business.portlet.Portlet;
43  import fr.paris.lutece.portal.service.i18n.I18nService;
44  import fr.paris.lutece.portal.service.plugin.Plugin;
45  import fr.paris.lutece.portal.service.plugin.PluginService;
46  import fr.paris.lutece.portal.service.security.LuteceUser;
47  import fr.paris.lutece.portal.service.security.SecurityService;
48  import fr.paris.lutece.portal.service.security.UserNotSignedException;
49  import fr.paris.lutece.util.ReferenceItem;
50  import fr.paris.lutece.util.url.UrlItem;
51  import fr.paris.lutece.util.xml.XmlUtil;
52  
53  import org.apache.commons.lang.StringUtils;
54  
55  import java.util.List;
56  
57  import javax.servlet.http.HttpServletRequest;
58  
59  
60  /**
61   *
62   * MyAppsPortlet
63   *
64   */
65  public class MyAppsPortlet extends Portlet
66  {
67      // CONSTANTS
68      private static final String TRUE = "1";
69      private static final String FALSE = "0";
70  
71      // TAGS
72      private static final String TAG_MYAPPS_PROVIDERS_LIST = "myapps-providers-list";
73      private static final String TAG_MYAPPS_PROVIDER = "myapps-provider";
74      private static final String TAG_MYAPPS_PROVIDERS_NAME = "myapps-provider-name";
75      private static final String TAG_MYAPPS_LIST = "myapps-list";
76      private static final String TAG_MYAPP = "myapp";
77      private static final String TAG_MYAPP_NAME = "myapp-name";
78      private static final String TAG_MYAPP_ICON = "myapp-icon";
79      private static final String TAG_MYAPP_DESCRIPTION = "myapp-description";
80      private static final String TAG_MYAPP_LINK = "myapp-link";
81      private static final String TAG_MYAPP_BUTTON = "myapp-button";
82      private static final String TAG_MYAPP_HAS_ICON = "myapp-has-icon";
83  
84      // PARAMETERS
85      private static final String PARAMETER_MYAPP_ID = "myapp_id";
86      private static final String PARAMETER_PLUGIN_NAME = "plugin_name";
87      private static final String PARAMETER_IS_ASC_SORT = "is_asc_sort";
88  
89      // PROPERTIES
90      private static final String PROPERTY_MANAGE_MYAPPS_BUTTON_LABEL = "myapps.portlet.buttonManageMyApps";
91  
92      // JSP
93      private static final String JSP_OPEN_MYAPP = "jsp/site/plugins/myapps/DoOpenMyApp.jsp";
94  
95      /**
96       * Constructor
97       */
98      public MyAppsPortlet(  )
99      {
100     }
101 
102     /**
103      * Returns the Xml code of the MyLutece portlet without XML heading
104      *
105      * @param request The HTTP Servlet request
106      * @return the Xml code of the MyLutece portlet content
107      */
108     public String getXml( HttpServletRequest request )
109     {
110         String strUserName;
111 
112         try
113         {
114             LuteceUser user = SecurityService.getInstance(  ).getRemoteUser( request );
115 
116             if ( user != null )
117             {
118                 strUserName = user.getName(  );
119             }
120             else
121             {
122                 strUserName = StringUtils.EMPTY;
123             }
124         }
125         catch ( UserNotSignedException e )
126         {
127             strUserName = StringUtils.EMPTY;
128         }
129 
130         StringBuffer sbXml = new StringBuffer(  );
131 
132         if ( StringUtils.isNotBlank( strUserName ) )
133         {
134             List<MyAppsProvider> listProviders = MyAppsManager.getInstance(  ).getProvidersList(  );
135 
136             XmlUtil.beginElement( sbXml, TAG_MYAPPS_PROVIDERS_LIST );
137 
138             String strButtonLabel = I18nService.getLocalizedString( PROPERTY_MANAGE_MYAPPS_BUTTON_LABEL,
139                     request.getLocale(  ) );
140             Plugin plugin = PluginService.getPlugin( MyAppsPlugin.PLUGIN_NAME );
141             ReferenceItem isAscSort = MyAppsParameterService.getInstance(  )
142                                                             .getParamDefaultValue( PARAMETER_IS_ASC_SORT, plugin );
143 
144             for ( MyAppsProvider provider : listProviders )
145             {
146                 XmlUtil.beginElement( sbXml, TAG_MYAPPS_PROVIDER );
147                 XmlUtil.addElement( sbXml, TAG_MYAPPS_PROVIDERS_NAME, provider.getProviderName( request.getLocale(  ) ) );
148                 XmlUtil.beginElement( sbXml, TAG_MYAPPS_LIST );
149 
150                 for ( MyApps myApp : provider.getMyAppsListByUserName( strUserName, isAscSort.isChecked(  ) ) )
151                 {
152                     XmlUtil.beginElement( sbXml, TAG_MYAPP );
153 
154                     UrlItem urlLink = new UrlItem( JSP_OPEN_MYAPP );
155                     urlLink.addParameter( PARAMETER_MYAPP_ID, myApp.getIdApplication(  ) );
156                     urlLink.addParameter( PARAMETER_PLUGIN_NAME, provider.getPluginName(  ) );
157                     XmlUtil.addElement( sbXml, TAG_MYAPP_ICON,
158                         provider.getResourceImage( String.valueOf( myApp.getIdApplication(  ) ) ) );
159                     XmlUtil.addElement( sbXml, TAG_MYAPP_NAME, myApp.getName(  ) );
160                     XmlUtil.addElement( sbXml, TAG_MYAPP_DESCRIPTION, myApp.getDescription(  ) );
161                     XmlUtil.addElement( sbXml, TAG_MYAPP_LINK, urlLink.getUrlWithEntity(  ) );
162                     XmlUtil.addElement( sbXml, TAG_MYAPP_HAS_ICON, myApp.hasIcon(  ) ? TRUE : FALSE );
163                     XmlUtil.endElement( sbXml, TAG_MYAPP );
164                 }
165 
166                 XmlUtil.endElement( sbXml, TAG_MYAPPS_LIST );
167 
168                 XmlUtil.endElement( sbXml, TAG_MYAPPS_PROVIDER );
169             }
170 
171             XmlUtil.addElement( sbXml, TAG_MYAPP_BUTTON, strButtonLabel );
172             XmlUtil.endElement( sbXml, TAG_MYAPPS_PROVIDERS_LIST );
173         }
174 
175         return addPortletTags( sbXml );
176     }
177 
178     /**
179      * Returns the Xml code of the MyApps portlet with XML heading
180      *
181      * @param request The HTTP servlet request
182      * @return the Xml code of the MyLutece portlet
183      */
184     public String getXmlDocument( HttpServletRequest request )
185     {
186         return XmlUtil.getXmlHeader(  ) + getXml( request );
187     }
188 
189     /**
190      * Update portlet's data
191      */
192     public void update(  )
193     {
194         MyAppsPortletService.getInstance(  ).update( this );
195     }
196 
197     /**
198      * Remove of this portlet
199      */
200     public void remove(  )
201     {
202         MyAppsPortletService.getInstance(  ).remove( this );
203     }
204 }