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.plugins.linkpages.web.portlet;
35
36 import fr.paris.lutece.plugins.linkpages.business.portlet.LinkPagesPortlet;
37 import fr.paris.lutece.plugins.linkpages.business.portlet.LinkPagesPortletHome;
38 import fr.paris.lutece.portal.business.page.Page;
39 import fr.paris.lutece.portal.business.page.PageHome;
40 import fr.paris.lutece.portal.business.portlet.PortletHome;
41 import fr.paris.lutece.portal.service.message.AdminMessage;
42 import fr.paris.lutece.portal.service.message.AdminMessageService;
43 import fr.paris.lutece.portal.service.page.IPageService;
44 import fr.paris.lutece.portal.service.page.PageResourceIdService;
45 import fr.paris.lutece.portal.service.spring.SpringContextService;
46 import fr.paris.lutece.portal.service.template.AppTemplateService;
47 import fr.paris.lutece.portal.web.portlet.PortletJspBean;
48 import fr.paris.lutece.util.ReferenceItem;
49 import fr.paris.lutece.util.ReferenceList;
50 import fr.paris.lutece.util.html.HtmlTemplate;
51
52 import java.util.Collection;
53 import java.util.HashMap;
54 import java.util.Iterator;
55
56 import javax.servlet.http.HttpServletRequest;
57
58
59
60
61
62 public class LinkPagesPortletJspBean extends PortletJspBean
63 {
64
65
66
67
68 private static final String MARK_COMBO_LINKPAGES = "combo_linkpages";
69 private static final String MARK_LINKPAGE_ORDER = "linkpage_order";
70 private static final String MARK_COMBO_LINKPAGES_ORDER = "combo_linkpages_order";
71 private final static String MARK_LINKPAGE_ID = "linkpage_id";
72 private final static String MARK_LINKPAGE_NAME = "linkpage_name";
73 private final static String MARK_LINKPAGE_DESCRIPTION = "linkpage_description";
74 private final static String MARK_NEW_LINKPAGE = "new_linkpage";
75 private static final String MARK_PORTLET_ID = "portlet_id";
76 private static final String MARK_LINKPAGES_LIST = "linkpages_list";
77 private static final String MARK_PAGE_ID = "page_id";
78
79
80 private static final String PARAMETER_LINKPAGE = "linkpage";
81 private static final String PARAMETER_LINKPAGE_ORDER = "linkpage_order";
82
83
84 private static final String TEMPLATE_LINKPAGES_LIST = "admin/plugins/linkpages/linkpages_list.html";
85
86
87 private static final String MESSAGE_LINKPAGE_NOT_EXIST = "linkpages.message.mandatory.linkpageNotExisted";
88 private static final String MESSAGE_PORTLET_LINK_PAGE_SELECTED = "linkpages.message.portlet.linkpageAlreadySelected";
89
90
91 private static final String JSP_DO_MODIFY_PORTLET = "../../DoModifyPortlet.jsp";
92
93 private IPageService _pageService = (IPageService) SpringContextService.getBean("pageService");
94
95
96
97
98
99
100
101 public String getPropertiesPrefix( )
102 {
103 return "portlet.link.pages";
104 }
105
106
107
108
109
110
111
112 public String getCreate( HttpServletRequest request )
113 {
114 String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
115 String strIdPortletType = request.getParameter( PARAMETER_PORTLET_TYPE_ID );
116
117 HashMap<String, Object> model = new HashMap<>( );
118
119 ReferenceList linkPageAuthorized = new ReferenceList( );
120 ReferenceList linkPage = LinkPagesPortletHome.getLinkPagesList( );
121
122 for ( ReferenceItem item : linkPage )
123 {
124 if ( _pageService.isAuthorizedAdminPage( Integer.parseInt( item.getCode( ) ),
125 PageResourceIdService.PERMISSION_VIEW, getUser( ) ) )
126 {
127 linkPageAuthorized.add( item );
128 }
129 }
130
131 model.put( MARK_COMBO_LINKPAGES, linkPageAuthorized );
132
133 HtmlTemplate template = getCreateTemplate( strIdPage, strIdPortletType , model);
134
135 return template.getHtml( );
136 }
137
138
139
140
141
142
143
144 public String getModify( HttpServletRequest request )
145 {
146 String strIdPortlet = request.getParameter( PARAMETER_PORTLET_ID );
147 int nIdPortlet = Integer.parseInt( strIdPortlet );
148 LinkPagesPortlet/../../../fr/paris/lutece/plugins/linkpages/business/portlet/LinkPagesPortlet.html#LinkPagesPortlet">LinkPagesPortlet portlet = (LinkPagesPortlet) PortletHome.findByPrimaryKey( nIdPortlet );
149 int nPageId = portlet.getPageId( );
150
151 HashMap model = new HashMap( );
152 model.put( MARK_LINKPAGES_LIST, getLinkPagesInPortletList( nIdPortlet ) );
153 model.put( MARK_PORTLET_ID, strIdPortlet );
154 model.put( MARK_PAGE_ID, nPageId );
155
156
157 int nMax = LinkPagesPortletHome.getMaxOrder( nIdPortlet );
158 nMax = nMax + 1;
159 model.put( MARK_LINKPAGE_ORDER, Integer.toString( nMax ) );
160 model.put( MARK_COMBO_LINKPAGES_ORDER, getNewLinkPageOrdersList( nIdPortlet ) );
161
162
163 ReferenceList linkPageAuthorized = new ReferenceList( );
164 ReferenceList linkPage = LinkPagesPortletHome.getLinkPagesList( );
165
166 for ( ReferenceItem item : linkPage )
167 {
168 if ( _pageService.isAuthorizedAdminPage( Integer.parseInt( item.getCode( ) ),
169 PageResourceIdService.PERMISSION_VIEW, getUser( ) ) )
170 {
171 linkPageAuthorized.add( item );
172 }
173 }
174
175 model.put( MARK_COMBO_LINKPAGES, linkPageAuthorized );
176
177 HtmlTemplate template = getModifyTemplate( portlet, model );
178
179 return template.getHtml( );
180 }
181
182
183
184
185
186
187
188 public String doCreate( HttpServletRequest request )
189 {
190 LinkPagesPortletges/business/portlet/LinkPagesPortlet.html#LinkPagesPortlet">LinkPagesPortlet portlet = new LinkPagesPortlet( );
191 String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
192 int nIdPage = Integer.parseInt( strIdPage );
193
194
195 String strErrorUrl = setPortletCommonData( request, portlet );
196
197 if ( strErrorUrl != null )
198 {
199 return strErrorUrl;
200 }
201
202 portlet.setPageId( nIdPage );
203
204
205 LinkPagesPortletHome.getInstance( ).create( portlet );
206
207
208 String strLinkPageId = request.getParameter( PARAMETER_LINKPAGE );
209
210 if ( ( strLinkPageId == null ) )
211 {
212 return AdminMessageService.getMessageUrl( request, MESSAGE_LINKPAGE_NOT_EXIST, AdminMessage.TYPE_ERROR );
213 }
214
215 int nLinkPageId = Integer.parseInt( strLinkPageId );
216
217 LinkPagesPortletHome.insertLinkPage( portlet.getId(), nLinkPageId, 1 );
218
219 return JSP_DO_MODIFY_PORTLET + "?" + PARAMETER_PORTLET_ID + "=" + portlet.getId();
220 }
221
222
223
224
225
226
227
228 public String doModify( HttpServletRequest request )
229 {
230
231 String strIdPortlet = request.getParameter( PARAMETER_PORTLET_ID );
232 int nIdPortlet = Integer.parseInt( strIdPortlet );
233 LinkPagesPortlet/../../../fr/paris/lutece/plugins/linkpages/business/portlet/LinkPagesPortlet.html#LinkPagesPortlet">LinkPagesPortlet portlet = (LinkPagesPortlet) PortletHome.findByPrimaryKey( nIdPortlet );
234
235
236 String strErrorUrl = setPortletCommonData( request, portlet );
237
238 if ( strErrorUrl != null )
239 {
240 return strErrorUrl;
241 }
242
243
244 portlet.update( );
245
246
247 return getPageUrl( portlet.getPageId( ) );
248 }
249
250
251
252
253
254
255
256 private ReferenceList getNewLinkPageOrdersList( int nPortletId )
257 {
258 int nMax = LinkPagesPortletHome.getMaxOrder( nPortletId );
259 ReferenceList list = new ReferenceList( );
260
261 for ( int i = 1; i < ( nMax + 2 ); i++ )
262 {
263 list.addItem( i, Integer.toString( i ) );
264 }
265
266 return list;
267 }
268
269
270
271
272
273
274
275 private String getLinkPagesInPortletList( int nPortletId )
276 {
277 StringBuilder strLinkPagesList = new StringBuilder( );
278
279 for ( Page page : LinkPagesPortletHome.getLinkPagesInPortletList( nPortletId ) )
280 {
281 if ( _pageService.isAuthorizedAdminPage( page.getId( ), PageResourceIdService.PERMISSION_VIEW,
282 getUser( ) ) )
283 {
284
285 HashMap model = new HashMap( );
286 int nIdPage = page.getId( );
287 model.put( MARK_LINKPAGE_ID, page.getId( ) );
288 model.put( MARK_LINKPAGE_NAME, page.getName( ) );
289 model.put( MARK_LINKPAGE_DESCRIPTION, page.getDescription( ) );
290 model.put( MARK_NEW_LINKPAGE, "0" );
291 model.put( MARK_COMBO_LINKPAGES_ORDER, getOrdersList( nPortletId ) );
292 model.put( MARK_PORTLET_ID, nPortletId );
293
294 Integer nOrderLinkPage = new Integer( LinkPagesPortletHome.getLinkPageOrder( nPortletId, page.getId( ) ) );
295 model.put( MARK_LINKPAGE_ORDER, nOrderLinkPage.toString( ) );
296
297 strLinkPagesList.append( AppTemplateService.getTemplate( TEMPLATE_LINKPAGES_LIST, getLocale( ), model )
298 .getHtml( ) );
299 }
300 }
301
302 return strLinkPagesList.toString( );
303 }
304
305
306
307
308
309
310
311 private ReferenceList getOrdersList( int nPortletId )
312 {
313 int nMax = LinkPagesPortletHome.getMaxOrder( nPortletId );
314 ReferenceList list = new ReferenceList( );
315
316 for ( int i = 1; i < ( nMax + 1 ); i++ )
317 {
318 list.addItem( i, Integer.toString( i ) );
319 }
320
321 return list;
322 }
323
324
325
326
327
328
329
330 public String doModifyOrderLinkPage( HttpServletRequest request )
331 {
332 String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
333 int nPortletId = Integer.parseInt( strPortletId );
334 String strLinkPageId = request.getParameter( PARAMETER_PAGE_ID );
335 int nLinkPageId = Integer.parseInt( strLinkPageId );
336 int nOldOrder = LinkPagesPortletHome.getLinkPageOrder( nPortletId, nLinkPageId );
337 String strOrder = request.getParameter( PARAMETER_LINKPAGE_ORDER );
338 int nOrder = Integer.parseInt( strOrder );
339
340 if ( nOrder < nOldOrder )
341 {
342 for ( int i = nOldOrder - 1; i > ( nOrder - 1 ); i-- )
343 {
344 int nIdTemp = LinkPagesPortletHome.getLinkPageIdByOrder( nPortletId, i );
345 LinkPagesPortletHome.updateLinkPageOrder( i + 1, nPortletId, nIdTemp );
346 }
347
348 LinkPagesPortletHome.updateLinkPageOrder( nOrder, nPortletId, nLinkPageId );
349 }
350 else if ( nOrder > nOldOrder )
351 {
352 for ( int i = nOldOrder; i < ( nOrder + 1 ); i++ )
353 {
354 int nIdTemp = LinkPagesPortletHome.getLinkPageIdByOrder( nPortletId, i );
355 LinkPagesPortletHome.updateLinkPageOrder( i - 1, nPortletId, nIdTemp );
356 }
357
358 LinkPagesPortletHome.updateLinkPageOrder( nOrder, nPortletId, nLinkPageId );
359 }
360
361 return JSP_DO_MODIFY_PORTLET + "?" + PARAMETER_PORTLET_ID + "=" + nPortletId;
362 }
363
364
365
366
367
368
369
370 public String doUnselectLinkPage( HttpServletRequest request )
371 {
372 String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
373 int nPortletId = Integer.parseInt( strPortletId );
374 String strLinkPageId = request.getParameter( PARAMETER_PAGE_ID );
375 int nLinkPageId = Integer.parseInt( strLinkPageId );
376 int nOrder = LinkPagesPortletHome.getLinkPageOrder( nPortletId, nLinkPageId );
377 int nMax = LinkPagesPortletHome.getMaxOrder( nPortletId );
378
379
380 LinkPagesPortletHome.removeLinkPage( nPortletId, nLinkPageId );
381
382 for ( int i = nOrder + 1; i < ( nMax + 1 ); i++ )
383 {
384 int nLinkPageIdTemp = LinkPagesPortletHome.getLinkPageIdByOrder( nPortletId, i );
385 LinkPagesPortletHome.updateLinkPageOrder( i - 1, nPortletId, nLinkPageIdTemp );
386 }
387
388 return JSP_DO_MODIFY_PORTLET + "?" + PARAMETER_PORTLET_ID + "=" + nPortletId;
389 }
390
391
392
393
394
395
396
397 public String doSelectLinkPage( HttpServletRequest request )
398 {
399 String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
400 int nPortletId = Integer.parseInt( strPortletId );
401 String strOrder = request.getParameter( PARAMETER_LINKPAGE_ORDER );
402 int nOrder = Integer.parseInt( strOrder );
403
404 String strLinkPageId = request.getParameter( PARAMETER_LINKPAGE );
405
406 if ( ( strLinkPageId == null ) )
407 {
408 return AdminMessageService.getMessageUrl( request, MESSAGE_LINKPAGE_NOT_EXIST, AdminMessage.TYPE_ERROR );
409 }
410
411 int nLinkPageId = Integer.parseInt( strLinkPageId );
412
413
414 if ( LinkPagesPortletHome.testDuplicate( nPortletId, nLinkPageId ) )
415 {
416 return AdminMessageService.getMessageUrl( request, MESSAGE_PORTLET_LINK_PAGE_SELECTED,
417 AdminMessage.TYPE_ERROR );
418 }
419
420 int nMax = LinkPagesPortletHome.getMaxOrder( nPortletId );
421
422 for ( int i = nOrder; i < ( nMax + 1 ); i++ )
423 {
424 int nLinkPageIdTemp = LinkPagesPortletHome.getLinkPageIdByOrder( nPortletId, i );
425 LinkPagesPortletHome.updateLinkPageOrder( i + 1, nPortletId, nLinkPageIdTemp );
426 }
427
428 LinkPagesPortletHome.insertLinkPage( nPortletId, nLinkPageId, nOrder );
429
430 return JSP_DO_MODIFY_PORTLET + "?" + PARAMETER_PORTLET_ID + "=" + nPortletId;
431 }
432
433
434
435
436
437
438
439 public String doSelectAllLinkPage( HttpServletRequest request )
440 {
441 String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
442 int nPortletId = Integer.parseInt( strPortletId );
443
444 LinkPagesPortlet/../../../fr/paris/lutece/plugins/linkpages/business/portlet/LinkPagesPortlet.html#LinkPagesPortlet">LinkPagesPortlet portlet = (LinkPagesPortlet) PortletHome.findByPrimaryKey( nPortletId );
445
446 int nMax = LinkPagesPortletHome.getMaxOrder( nPortletId );
447
448 int nOrder = nMax + 1;
449
450 Collection linkPagesList = PageHome.getChildPages( portlet.getPageId( ) );
451
452 Iterator i = linkPagesList.iterator( );
453
454 while ( i.hasNext( ) )
455 {
456 Page linkPage = (Page) i.next( );
457
458
459 if ( !LinkPagesPortletHome.testDuplicate( nPortletId, linkPage.getId( ) ) )
460 {
461 LinkPagesPortletHome.insertLinkPage( nPortletId, linkPage.getId( ), nOrder );
462 nOrder++;
463 }
464 }
465
466 return JSP_DO_MODIFY_PORTLET + "?" + PARAMETER_PORTLET_ID + "=" + nPortletId;
467 }
468 }