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 javax.servlet.http.HttpServletRequest;
37  
38  import org.apache.commons.lang3.StringUtils;
39  
40  import fr.paris.lutece.portal.business.dashboard.DashboardFactory;
41  import fr.paris.lutece.portal.business.dashboard.DashboardHome;
42  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
43  import fr.paris.lutece.portal.service.dashboard.DashboardService;
44  import fr.paris.lutece.portal.service.dashboard.IDashboardComponent;
45  import fr.paris.lutece.portal.service.message.AdminMessage;
46  import fr.paris.lutece.portal.service.message.AdminMessageService;
47  import fr.paris.lutece.portal.service.security.SecurityTokenService;
48  import fr.paris.lutece.portal.service.util.AppLogService;
49  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
50  import fr.paris.lutece.portal.web.constants.Messages;
51  import fr.paris.lutece.util.string.StringUtil;
52  
53  /**
54   * Provides technical admin dashboard managements and display. Display is NOT managed as an admin feature (no right required).
55   *
56   */
57  public class DashboardJspBean extends AdminFeaturesPageJspBean
58  {
59      // Right
60      public static final String RIGHT_MANAGE_DASHBOARD = "CORE_DASHBOARD_MANAGEMENT";
61  
62      // Parameters
63      private static final String PARAMETER_DASHBOARD_NAME = "dashboard_name";
64      private static final String PARAMETER_DASHBOARD_COLUMN = "dashboard_column";
65      private static final String PARAMETER_DASHBOARD_ORDER = "dashboard_order";
66      private static final String PARAMETER_COLUMN = "column";
67  
68      // Messages
69      private static final String MESSAGE_DASHBOARD_NOT_FOUND = "portal.dashboard.message.dashboardNotFound";
70  
71      // Templates
72      private static final String TEMPLATE_MANAGE_DASHBOARDS = "/admin/dashboard/manage_dashboards.html";
73  
74      // JSP
75      private static final String ANCHOR_ADMIN_DASHBOARDS = "adminHomePageManagement";
76      private transient DashboardService _service = DashboardService.getInstance( );
77  
78      /**
79       * Reorders columns
80       * 
81       * @param request
82       *            the request
83       * @return url
84       * @throws AccessDeniedException
85       *             if the security token is invalid
86       */
87      public String doReorderColumn( HttpServletRequest request ) throws AccessDeniedException
88      {
89          String strColumnName = request.getParameter( PARAMETER_COLUMN );
90  
91          if ( StringUtils.isBlank( strColumnName ) )
92          {
93              return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
94          }
95  
96          int nColumn;
97  
98          try
99          {
100             nColumn = Integer.parseInt( strColumnName );
101         }
102         catch( NumberFormatException nfe )
103         {
104             AppLogService.error( "DashboardJspBean.doReorderColumn : {}", nfe.getMessage( ), nfe );
105 
106             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
107         }
108         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MANAGE_DASHBOARDS ) )
109         {
110             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
111         }
112         getDashboardService( ).doReorderColumn( nColumn );
113 
114         return getAdminDashboardsUrl( request, ANCHOR_ADMIN_DASHBOARDS );
115     }
116 
117     /**
118      * Moves the dashboard
119      * 
120      * @param request
121      *            the request
122      * @return url
123      * @throws AccessDeniedException
124      *             if the security token is invalid
125      */
126     public String doMoveDashboard( HttpServletRequest request ) throws AccessDeniedException
127     {
128         String strDashboardName = request.getParameter( PARAMETER_DASHBOARD_NAME );
129 
130         if ( StringUtils.isBlank( strDashboardName ) )
131         {
132             return AdminMessageService.getMessageUrl( request, MESSAGE_DASHBOARD_NOT_FOUND, AdminMessage.TYPE_STOP );
133         }
134 
135         // retrieve dashboard from database. If not found, will use Spring.
136         IDashboardComponent dashboard = DashboardHome.findByPrimaryKey( strDashboardName );
137         int nOldOrder = 0;
138         int nOldColumn = 0;
139         boolean bCreate = false;
140 
141         if ( dashboard == null )
142         {
143             bCreate = true;
144 
145             if ( AppLogService.isDebugEnabled( ) )
146             {
147                 AppLogService.debug( "Dashboard " + strDashboardName + " has no property set. Retrieving from SpringContext" );
148             }
149 
150             dashboard = DashboardFactory.getDashboardComponent( strDashboardName );
151 
152             if ( dashboard == null )
153             {
154                 return AdminMessageService.getMessageUrl( request, MESSAGE_DASHBOARD_NOT_FOUND, AdminMessage.TYPE_STOP );
155             }
156         }
157         else
158         {
159             nOldOrder = dashboard.getOrder( );
160             nOldColumn = dashboard.getZone( );
161         }
162         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MANAGE_DASHBOARDS ) )
163         {
164             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
165         }
166         // set order and column
167         String strOrder = request.getParameter( PARAMETER_DASHBOARD_ORDER );
168         String strColumn = request.getParameter( PARAMETER_DASHBOARD_COLUMN );
169 
170         int nOrder = StringUtil.getIntValue( strOrder, -1 );
171         int nColumn = StringUtil.getIntValue( strColumn, -1 );
172 
173         dashboard.setOrder( nOrder );
174         dashboard.setZone( nColumn );
175 
176         getDashboardService( ).doMoveDashboard( dashboard, nOldColumn, nOldOrder, bCreate );
177 
178         return getAdminDashboardsUrl( request, ANCHOR_ADMIN_DASHBOARDS );
179     }
180 
181     private DashboardService getDashboardService( )
182     {
183         if ( _service == null )
184         {
185             _service = DashboardService.getInstance( );
186         }
187 
188         return _service;
189     }
190 }