1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
63
64
65 public class AdminDashboardJspBean extends AdminFeaturesPageJspBean
66 {
67
68 public static final String RIGHT_MANAGE_ADMINDASHBOARD = "CORE_ADMINDASHBOARD_MANAGEMENT";
69
70
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
77 private static final String MESSAGE_DASHBOARD_NOT_FOUND = "portal.admindashboard.message.dashboardNotFound";
78
79
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
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
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
99
100
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
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
122
123
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
149
150
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
181
182
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
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
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
239
240
241 private ReferenceList getListAvailableColumns( )
242 {
243 ReferenceList refList = new ReferenceList( );
244
245
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
258
259
260 private Map<String, ReferenceList> getMapAvailableOrders( )
261 {
262 Map<String, ReferenceList> mapAvailableOrders = new HashMap<String, ReferenceList>( );
263
264
265 for ( Integer nColumn : AdminDashboardHome.findColumns( ) )
266 {
267
268 mapAvailableOrders.put( nColumn.toString( ), getListAvailableOrders( nColumn ) );
269 }
270
271 return mapAvailableOrders;
272 }
273
274
275
276
277
278
279 private ReferenceList getListAvailableOrders( int nColumn )
280 {
281 ReferenceList refList = new ReferenceList( );
282
283
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 }