View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.portal.web.dashboard;
35  
36  import fr.paris.lutece.portal.business.dashboard.DashboardFactory;
37  import fr.paris.lutece.portal.business.dashboard.DashboardHome;
38  import fr.paris.lutece.portal.business.user.AdminUser;
39  import fr.paris.lutece.portal.service.admin.AdminUserService;
40  import fr.paris.lutece.portal.service.dashboard.DashboardService;
41  import fr.paris.lutece.portal.service.dashboard.IDashboardComponent;
42  import fr.paris.lutece.portal.service.message.AdminMessage;
43  import fr.paris.lutece.portal.service.message.AdminMessageService;
44  import fr.paris.lutece.portal.service.template.AppTemplateService;
45  import fr.paris.lutece.portal.service.util.AppLogService;
46  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
47  import fr.paris.lutece.portal.web.constants.Messages;
48  import fr.paris.lutece.util.html.HtmlTemplate;
49  import fr.paris.lutece.util.string.StringUtil;
50  
51  import org.apache.commons.lang.StringUtils;
52  
53  import java.util.HashMap;
54  import java.util.List;
55  import java.util.Map;
56  
57  import javax.servlet.http.HttpServletRequest;
58  
59  
60  /**
61   * Provides technical admin dashboard managements and display. Display is NOT
62   * managed as an admin feature (no right required).
63   *
64   */
65  public class DashboardJspBean extends AdminFeaturesPageJspBean
66  {
67      // Right
68      public static final String RIGHT_MANAGE_DASHBOARD = "CORE_DASHBOARD_MANAGEMENT";
69  
70      // Parameters
71      private static final String PARAMETER_DASHBOARD_NAME = "dashboard_name";
72      private static final String PARAMETER_DASHBOARD_COLUMN = "dashboard_column";
73      private static final String PARAMETER_DASHBOARD_ORDER = "dashboard_order";
74      private static final String PARAMETER_COLUMN = "column";
75  
76      // Messages
77      private static final String MESSAGE_DASHBOARD_NOT_FOUND = "portal.dashboard.message.dashboardNotFound";
78  
79      // MARKS
80      private static final String MARK_MAP_DASHBOARDS = "map_dashboards";
81      private static final String MARK_NOT_SET_DASHBOARDS = "not_set_dashboards";
82      private static final String MARK_COLUMN_COUNT = "column_count";
83      private static final String MARK_LIST_AVAILABLE_COLUMNS = "list_available_columns";
84      private static final String MARK_MAP_AVAILABLE_ORDERS = "map_available_orders";
85      private static final String MARK_MAP_COLUMN_ORDER_STATUS = "map_column_order_status";
86  
87      // Templates
88      private static final String TEMPLATE_MANAGE_DASHBOARDS = "/admin/dashboard/manage_dashboards.html";
89  
90      // JSP
91      private static final String JSP_MANAGE_DASHBOARDS = "ManageDashboards.jsp";
92      private DashboardService _service = DashboardService.getInstance(  );
93  
94      /**
95       * Manages dashboard
96       * @param request the request
97       * @return html code
98       */
99      public String getManageDashboards( HttpServletRequest request )
100     {
101         AdminUser user = AdminUserService.getAdminUser( request );
102 
103         Map<String, Object> model = new HashMap<String, Object>(  );
104 
105         Map<String, List<IDashboardComponent>> mapDashboards = _service.getAllSetDashboards( getUser(  ) );
106         model.put( MARK_MAP_DASHBOARDS, mapDashboards );
107 
108         List<IDashboardComponent> listNotSetDashboards = _service.getNotSetDashboards(  );
109         model.put( MARK_NOT_SET_DASHBOARDS, listNotSetDashboards );
110 
111         model.put( MARK_COLUMN_COUNT, _service.getColumnCount(  ) );
112         model.put( MARK_MAP_AVAILABLE_ORDERS, _service.getMapAvailableOrders(  ) );
113         model.put( MARK_LIST_AVAILABLE_COLUMNS, _service.getListAvailableColumns(  ) );
114         model.put( MARK_MAP_COLUMN_ORDER_STATUS, _service.getOrderedColumnsStatus(  ) );
115 
116         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_DASHBOARDS, user.getLocale(  ), model );
117 
118         return getAdminPage( template.getHtml(  ) );
119     }
120 
121     /**
122      * Reorders columns
123      * @param request the request
124      * @return url
125      */
126     public String doReorderColumn( HttpServletRequest request )
127     {
128         String strColumnName = request.getParameter( PARAMETER_COLUMN );
129 
130         if ( StringUtils.isBlank( strColumnName ) )
131         {
132             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
133         }
134 
135         int nColumn = 0;
136 
137         try
138         {
139             nColumn = Integer.parseInt( strColumnName );
140         }
141         catch ( NumberFormatException nfe )
142         {
143             AppLogService.error( "DashboardJspBean.doReorderColumn : " + nfe.getMessage(  ), nfe );
144 
145             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
146         }
147 
148         _service.doReorderColumn( nColumn );
149 
150         return JSP_MANAGE_DASHBOARDS;
151     }
152 
153     /**
154      * Moves the dashboard
155      * @param request the request
156      * @return url
157      */
158     public String doMoveDashboard( HttpServletRequest request )
159     {
160         String strDashboardName = request.getParameter( PARAMETER_DASHBOARD_NAME );
161 
162         if ( StringUtils.isBlank( strDashboardName ) )
163         {
164             return AdminMessageService.getMessageUrl( request, MESSAGE_DASHBOARD_NOT_FOUND, AdminMessage.TYPE_STOP );
165         }
166 
167         // retrieve dashboard from database. If not found, will use Spring.
168         IDashboardComponent dashboard = DashboardHome.findByPrimaryKey( strDashboardName );
169         int nOldOrder = 0;
170         int nOldColumn = 0;
171         boolean bCreate = false;
172 
173         if ( dashboard == null )
174         {
175             bCreate = true;
176 
177             if ( AppLogService.isDebugEnabled(  ) )
178             {
179                 AppLogService.debug( "Dashboard " + strDashboardName +
180                     " has no property set. Retrieving from SpringContext" );
181             }
182 
183             dashboard = DashboardFactory.getDashboardComponent( strDashboardName );
184 
185             if ( dashboard == null )
186             {
187                 return AdminMessageService.getMessageUrl( request, MESSAGE_DASHBOARD_NOT_FOUND, AdminMessage.TYPE_STOP );
188             }
189         }
190         else
191         {
192             nOldOrder = dashboard.getOrder(  );
193             nOldColumn = dashboard.getZone(  );
194         }
195 
196         // set order and column
197         String strOrder = request.getParameter( PARAMETER_DASHBOARD_ORDER );
198         String strColumn = request.getParameter( PARAMETER_DASHBOARD_COLUMN );
199 
200         int nOrder = StringUtil.getIntValue( strOrder, -1 );
201         int nColumn = StringUtil.getIntValue( strColumn, -1 );
202 
203         dashboard.setOrder( nOrder );
204         dashboard.setZone( nColumn );
205 
206         _service.doMoveDashboard( dashboard, nOldColumn, nOldOrder, bCreate );
207 
208         return JSP_MANAGE_DASHBOARDS;
209     }
210 }