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.web;
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.MyAppsProvider;
39  import fr.paris.lutece.plugins.myapps.service.parameter.MyAppsParameterService;
40  import fr.paris.lutece.portal.service.i18n.I18nService;
41  import fr.paris.lutece.portal.service.message.SiteMessageException;
42  import fr.paris.lutece.portal.service.plugin.Plugin;
43  import fr.paris.lutece.portal.service.security.LuteceUser;
44  import fr.paris.lutece.portal.service.security.SecurityService;
45  import fr.paris.lutece.portal.service.security.UserNotSignedException;
46  import fr.paris.lutece.portal.service.template.AppTemplateService;
47  import fr.paris.lutece.portal.web.xpages.XPage;
48  import fr.paris.lutece.portal.web.xpages.XPageApplication;
49  import fr.paris.lutece.util.ReferenceItem;
50  import fr.paris.lutece.util.html.HtmlTemplate;
51  import fr.paris.lutece.util.sort.AttributeComparator;
52  
53  import java.util.ArrayList;
54  import java.util.Collections;
55  import java.util.HashMap;
56  import java.util.List;
57  import java.util.Map;
58  
59  import javax.servlet.http.HttpServletRequest;
60  
61  
62  /**
63   *
64   * MyAppsApp
65   *
66   */
67  public class MyAppsApp implements XPageApplication
68  {
69      // TEMPLATES
70      private static final String TEMPLATE_MYAPPS_IN_DIVIDED_LISTS = "skin/plugins/myapps/page_myapps_in_divided_lists.html";
71      private static final String TEMPLATE_MYAPPS_IN_ONE_LIST = "skin/plugins/myapps/page_myapps_in_one_list.html";
72  
73      // MARKS
74      private static final String MARK_MYAPPS_PROVIDERS_LIST = "myapps_providers_list";
75      private static final String MARK_USER_NAME = "user_name";
76      private static final String MARK_LOCALE = "locale";
77      private static final String MARK_MYAPPS_LIST = "myapps_list";
78      private static final String MARK_IS_ASC_SORT = "is_asc_sort";
79  
80      // PARAMETERS
81      private static final String PARAMETER_IS_SHOWN_IN_ONE_LIST = "is_shown_in_one_list";
82      private static final String PARAMETER_IS_ASC_SORT = "is_asc_sort";
83      private static final String PARAMETER_NAME = "name";
84  
85      // PROPERTIES
86      private static final String PROPERTY_PAGE_TITLE = "myapps.page_myapps.pageTitle";
87      private static final String PROPERTY_PAGE_PATH = "myapps.page_myapps.pagePathLabel";
88  
89      /**
90       * Front Office application to manage myapps application
91       *
92       * @param request The request
93       * @param nMode The mode
94       * @param plugin The plugin
95       * @return The Xpage
96       * @throws SiteMessageException exception if some parameters are not correctly filled
97       * @throws UserNotSignedException exception if the current user is not connected
98       */
99      public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin )
100         throws SiteMessageException, UserNotSignedException
101     {
102         XPage page = new XPage(  );
103         page.setTitle( I18nService.getLocalizedString( PROPERTY_PAGE_TITLE, request.getLocale(  ) ) );
104         page.setPathLabel( I18nService.getLocalizedString( PROPERTY_PAGE_PATH, request.getLocale(  ) ) );
105 
106         page = getManageMyAppsPage( page, request, plugin );
107 
108         return page;
109     }
110 
111     /**
112      * Get the manage myApps interface
113      *
114      * @param page the {@link XPage}
115      * @param request {@link HttpServletRequest}
116      * @param plugin {@link Plugin}
117      * @return a {@link XPage}
118      * @throws UserNotSignedException exception if the current user is not connected
119      */
120     private XPage getManageMyAppsPage( XPage page, HttpServletRequest request, Plugin plugin )
121         throws UserNotSignedException
122     {
123         LuteceUser user = SecurityService.getInstance(  ).getRemoteUser( request );
124 
125         if ( user != null )
126         {
127             ReferenceItem isShownInOneList = MyAppsParameterService.getInstance(  )
128                                                                    .getParamDefaultValue( PARAMETER_IS_SHOWN_IN_ONE_LIST,
129                     plugin );
130             ReferenceItem isAscSort = MyAppsParameterService.getInstance(  )
131                                                             .getParamDefaultValue( PARAMETER_IS_ASC_SORT, plugin );
132             List<MyAppsProvider> listProviders = MyAppsManager.getInstance(  ).getProvidersList(  );
133 
134             HtmlTemplate template;
135             Map<String, Object> model = new HashMap<String, Object>(  );
136             model.put( MARK_USER_NAME, user.getName(  ) );
137             model.put( MARK_LOCALE, request.getLocale(  ) );
138             model.put( MARK_MYAPPS_PROVIDERS_LIST, listProviders );
139 
140             // Checking if we display in one list or if we display by modules
141             if ( isShownInOneList.isChecked(  ) )
142             {
143                 List<MyApps> listMyApps = new ArrayList<MyApps>(  );
144 
145                 for ( MyAppsProvider provider : listProviders )
146                 {
147                     listMyApps.addAll( provider.getMyAppsListByUserName( user.getName(  ), isAscSort.isChecked(  ) ) );
148                 }
149 
150                 Collections.sort( listMyApps, new AttributeComparator( PARAMETER_NAME, isAscSort.isChecked(  ) ) );
151                 model.put( MARK_MYAPPS_LIST, listMyApps );
152                 template = AppTemplateService.getTemplate( TEMPLATE_MYAPPS_IN_ONE_LIST, request.getLocale(  ), model );
153             }
154             else
155             {
156                 model.put( MARK_IS_ASC_SORT, isAscSort.isChecked(  ) );
157                 template = AppTemplateService.getTemplate( TEMPLATE_MYAPPS_IN_DIVIDED_LISTS, request.getLocale(  ),
158                         model );
159             }
160 
161             page.setContent( template.getHtml(  ) );
162         }
163         else
164         {
165             throw new UserNotSignedException(  );
166         }
167 
168         return page;
169     }
170 }