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