View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.portal.web.dashboard;
35  
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  
40  import javax.servlet.http.HttpServletRequest;
41  
42  import org.apache.commons.lang3.StringUtils;
43  
44  import fr.paris.lutece.portal.business.dashboard.AdminDashboardFactory;
45  import fr.paris.lutece.portal.business.dashboard.AdminDashboardHome;
46  import fr.paris.lutece.portal.business.user.AdminUser;
47  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
48  import fr.paris.lutece.portal.service.admin.AdminUserService;
49  import fr.paris.lutece.portal.service.dashboard.admin.AdminDashboardService;
50  import fr.paris.lutece.portal.service.dashboard.admin.IAdminDashboardComponent;
51  import fr.paris.lutece.portal.service.message.AdminMessage;
52  import fr.paris.lutece.portal.service.message.AdminMessageService;
53  import fr.paris.lutece.portal.service.security.SecurityTokenService;
54  import fr.paris.lutece.portal.service.template.AppTemplateService;
55  import fr.paris.lutece.portal.service.util.AppLogService;
56  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
57  import fr.paris.lutece.portal.web.constants.Messages;
58  import fr.paris.lutece.util.ReferenceList;
59  import fr.paris.lutece.util.html.HtmlTemplate;
60  import fr.paris.lutece.util.string.StringUtil;
61  
62  /**
63   * Provides technical admin dashboard managements and display. Display is NOT managed as an admin feature (no right required).
64   *
65   */
66  public class AdminDashboardJspBean extends AdminFeaturesPageJspBean
67  {
68      private static final long serialVersionUID = 5312273100074724058L;
69  
70      public static final String TEMPLATE_MANAGE_DASHBOARDS = "/admin/dashboard/admin/manage_dashboards.html";
71  
72      // Constants
73      public static final String RIGHT_MANAGE_ADMINDASHBOARD = "CORE_ADMINDASHBOARD_MANAGEMENT";
74  
75      // Parameters
76      private static final String PARAMETER_DASHBOARD_NAME = "dashboard_name";
77      private static final String PARAMETER_DASHBOARD_COLUMN = "dashboard_column";
78      private static final String PARAMETER_DASHBOARD_ORDER = "dashboard_order";
79      private static final String PARAMETER_COLUMN = "column";
80  
81      // Messages
82      private static final String MESSAGE_DASHBOARD_NOT_FOUND = "portal.admindashboard.message.dashboardNotFound";
83  
84      // Markers
85      private static final String MARK_COLUMN_CONTENT_PREFIX = "column_content_";
86      private static final String MARK_MAP_DASHBOARDS = "map_dashboards";
87      private static final String MARK_NOT_SET_DASHBOARDS = "not_set_dashboards";
88      private static final String MARK_COLUMN_COUNT = "column_count";
89      private static final String MARK_LIST_AVAILABLE_COLUMNS = "list_available_columns";
90      private static final String MARK_MAP_AVAILABLE_ORDERS = "map_available_orders";
91      private static final String MARK_MAP_COLUMN_ORDER_STATUS = "map_column_order_status";
92  
93      // Templates
94      private static final String TEMPLATE_VIEW_DASHBOARDS = "/admin/dashboard/admin/view_dashboards.html";
95  
96      // Jsp
97      private static final String JSP_MANAGE_DASHBOARDS = "ManageAdminDashboards.jsp";
98      private static final String EMPTY_STRING = "";
99      private transient AdminDashboardService _service = AdminDashboardService.getInstance( );
100 
101     /**
102      * Displays admin dashboards
103      * 
104      * @param request
105      *            the request
106      * @return html code [NOT AS A FEATURE]
107      */
108     public String getAdminDashboards( HttpServletRequest request )
109     {
110         AdminUser user = AdminUserService.getAdminUser( request );
111         Map<String, Object> model = new HashMap<>( );
112 
113         // put each column data
114         for ( int nColumn = 1; nColumn <= getAdminDashboardService( ).getColumnCount( ); nColumn++ )
115         {
116             String strColumnData = getAdminDashboardService( ).getDashboardData( user, nColumn, request );
117 
118             model.put( MARK_COLUMN_CONTENT_PREFIX + nColumn, strColumnData );
119         }
120 
121         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_VIEW_DASHBOARDS, user.getLocale( ), model );
122 
123         return template.getHtml( );
124     }
125 
126     /**
127      * Manages dashboard
128      * 
129      * @param request
130      *            the request
131      * @return html code
132      */
133     public String getManageDashboards( HttpServletRequest request )
134     {
135         AdminUser user = AdminUserService.getAdminUser( request );
136 
137         Map<String, Object> model = new HashMap<>( );
138 
139         Map<String, List<IAdminDashboardComponent>> mapAdminDashboards = getAdminDashboardService( ).getAllSetDashboards( );
140         model.put( MARK_MAP_DASHBOARDS, mapAdminDashboards );
141 
142         List<IAdminDashboardComponent> listNotSetDashboards = getAdminDashboardService( ).getNotSetDashboards( );
143         model.put( MARK_NOT_SET_DASHBOARDS, listNotSetDashboards );
144 
145         model.put( MARK_COLUMN_COUNT, getAdminDashboardService( ).getColumnCount( ) );
146         model.put( MARK_MAP_AVAILABLE_ORDERS, getMapAvailableOrders( ) );
147         model.put( MARK_LIST_AVAILABLE_COLUMNS, getListAvailableColumns( ) );
148         model.put( MARK_MAP_COLUMN_ORDER_STATUS, getAdminDashboardService( ).getOrderedColumnsStatus( ) );
149         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MANAGE_DASHBOARDS ) );
150 
151         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_DASHBOARDS, user.getLocale( ), model );
152 
153         return getAdminPage( template.getHtml( ) );
154     }
155 
156     /**
157      * Reorders columns
158      * 
159      * @param request
160      *            the request
161      * @return url
162      * @throws AccessDeniedException
163      *             if the security token is invalid
164      */
165     public String doReorderColumn( HttpServletRequest request ) throws AccessDeniedException
166     {
167         String strColumnName = request.getParameter( PARAMETER_COLUMN );
168 
169         if ( StringUtils.isBlank( strColumnName ) )
170         {
171             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
172         }
173 
174         int nColumn = 0;
175 
176         try
177         {
178             nColumn = Integer.parseInt( strColumnName );
179         }
180         catch( NumberFormatException nfe )
181         {
182             AppLogService.error( "AdminDashboardJspBean.doReorderColumn : {}", nfe.getMessage( ), nfe );
183 
184             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
185         }
186         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MANAGE_DASHBOARDS ) )
187         {
188             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
189         }
190         getAdminDashboardService( ).doReorderColumn( nColumn );
191 
192         return JSP_MANAGE_DASHBOARDS;
193     }
194 
195     /**
196      * Moves the dashboard
197      * 
198      * @param request
199      *            the request
200      * @return url
201      * @throws AccessDeniedException
202      *             if the security token is invalid
203      */
204     public String doMoveAdminDashboard( HttpServletRequest request ) throws AccessDeniedException
205     {
206         String strDashboardName = request.getParameter( PARAMETER_DASHBOARD_NAME );
207 
208         if ( StringUtils.isBlank( strDashboardName ) )
209         {
210             return AdminMessageService.getMessageUrl( request, MESSAGE_DASHBOARD_NOT_FOUND, AdminMessage.TYPE_STOP );
211         }
212 
213         // retrieve dashboard from database. If not found, will use Spring.
214         IAdminDashboardComponent dashboard = AdminDashboardHome.findByPrimaryKey( strDashboardName );
215         int nOldOrder = 0;
216         int nOldColumn = 0;
217         boolean bCreate = false;
218 
219         if ( dashboard == null )
220         {
221             bCreate = true;
222 
223             if ( AppLogService.isDebugEnabled( ) )
224             {
225                 AppLogService.debug( "Dashboard " + strDashboardName + " has no property set. Retrieving from SpringContext" );
226             }
227 
228             dashboard = AdminDashboardFactory.getDashboardComponent( strDashboardName );
229 
230             if ( dashboard == null )
231             {
232                 return AdminMessageService.getMessageUrl( request, MESSAGE_DASHBOARD_NOT_FOUND, AdminMessage.TYPE_STOP );
233             }
234         }
235         else
236         {
237             nOldOrder = dashboard.getOrder( );
238             nOldColumn = dashboard.getZone( );
239         }
240         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MANAGE_DASHBOARDS ) )
241         {
242             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
243         }
244 
245         // set order and column
246         String strOrder = request.getParameter( PARAMETER_DASHBOARD_ORDER );
247         String strColumn = request.getParameter( PARAMETER_DASHBOARD_COLUMN );
248 
249         int nOrder = StringUtil.getIntValue( strOrder, -1 );
250         int nColumn = StringUtil.getIntValue( strColumn, -1 );
251 
252         dashboard.setOrder( nOrder );
253         dashboard.setZone( nColumn );
254 
255         getAdminDashboardService( ).doMoveDashboard( dashboard, nOldColumn, nOldOrder, bCreate );
256 
257         return JSP_MANAGE_DASHBOARDS;
258     }
259 
260     /**
261      * Returns list with available column
262      * 
263      * @return all available columns
264      */
265     private ReferenceList getListAvailableColumns( )
266     {
267         ReferenceListList.html#ReferenceList">ReferenceList refList = new ReferenceList( );
268 
269         // add empty item
270         refList.addItem( EMPTY_STRING, EMPTY_STRING );
271 
272         for ( int nColumnIndex = 1; nColumnIndex <= getAdminDashboardService( ).getColumnCount( ); nColumnIndex++ )
273         {
274             refList.addItem( nColumnIndex, Integer.toString( nColumnIndex ) );
275         }
276 
277         return refList;
278     }
279 
280     /**
281      * Builds all refList order for all columns
282      * 
283      * @return the map with column id as key
284      */
285     private Map<String, ReferenceList> getMapAvailableOrders( )
286     {
287         Map<String, ReferenceList> mapAvailableOrders = new HashMap<>( );
288 
289         // get columns
290         for ( Integer nColumn : AdminDashboardHome.findColumns( ) )
291         {
292             // get orders
293             mapAvailableOrders.put( nColumn.toString( ), getListAvailableOrders( nColumn ) );
294         }
295 
296         return mapAvailableOrders;
297     }
298 
299     /**
300      * Orders reference list for the given column
301      * 
302      * @param nColumn
303      *            column
304      * @return the refList
305      */
306     private ReferenceList getListAvailableOrders( int nColumn )
307     {
308         ReferenceListList.html#ReferenceList">ReferenceList refList = new ReferenceList( );
309 
310         // add empty item
311         refList.addItem( EMPTY_STRING, EMPTY_STRING );
312 
313         int nMaxOrder = AdminDashboardHome.findMaxOrder( nColumn );
314 
315         for ( int nOrder = 1; nOrder <= nMaxOrder; nOrder++ )
316         {
317             refList.addItem( nOrder, Integer.toString( nOrder ) );
318         }
319 
320         return refList;
321     }
322 
323     private AdminDashboardService getAdminDashboardService( )
324     {
325         if ( _service == null )
326         {
327             _service = AdminDashboardService.getInstance( );
328         }
329         return _service;
330     }
331 }