View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.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   * This class provides the user interface to manage LinkPages Portlet
61   */
62  public class LinkPagesPortletJspBean extends PortletJspBean
63  {
64      ////////////////////////////////////////////////////////////////////////////
65      // Constants
66  
67      // MARKS
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      //Parameters
80      private static final String PARAMETER_LINKPAGE = "linkpage";
81      private static final String PARAMETER_LINKPAGE_ORDER = "linkpage_order";
82  
83      //Templates
84      private static final String TEMPLATE_LINKPAGES_LIST = "admin/plugins/linkpages/linkpages_list.html";
85  
86      //Messages
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      // Jsp
91      private static final String JSP_DO_MODIFY_PORTLET = "../../DoModifyPortlet.jsp";
92  
93      private IPageService _pageService = (IPageService) SpringContextService.getBean("pageService");
94  
95  
96      /**
97       * Returns portlet's properties prefix
98       *
99       * @return prefix
100      */
101     public String getPropertiesPrefix(  )
102     {
103         return "portlet.link.pages";
104     }
105 
106     /**
107      * Returns the Download portlet creation form
108      *
109      * @param request The http request
110      * @return The HTML form
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         // LinkPages list combo
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      * Returns the modified portlet in HTML form
140      *
141      * @param request The http request
142      * @return The HTML form
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         // LinkPages order combo
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         // LinkPages list combo
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      * Process portlet's creation
184      *
185      * @param request The Http request
186      * @return The Jsp management URL of the process result
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         // get portlet common attributes
195         String strErrorUrl = setPortletCommonData( request, portlet );
196 
197         if ( strErrorUrl != null )
198         {
199             return strErrorUrl;
200         }
201 
202         portlet.setPageId( nIdPage );
203 
204         //Portlet creation
205         LinkPagesPortletHome.getInstance(  ).create( portlet );
206 
207         // insert linkpage
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      * Process portlet's modification
224      *
225      * @param request The http request
226      * @return Management's Url
227      */
228     public String doModify( HttpServletRequest request )
229     {
230         //recovery of the portlet
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         // get portlet common attributes
236         String strErrorUrl = setPortletCommonData( request, portlet );
237 
238         if ( strErrorUrl != null )
239         {
240             return strErrorUrl;
241         }
242 
243         //Update of the portlet
244         portlet.update(  );
245 
246         //Displays the page with the updated portlet
247         return getPageUrl( portlet.getPageId(  ) );
248     }
249 
250     /**
251     * Returns an orders list for a new linkpage in a specified portlet
252     *
253     * @param nPortletId The identifier of the portlet
254     * @return A list of orders
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      * Returns The list of linkpages wich belong to a specified portlet
271      *
272      * @param nPortletId The identifier of the portlet
273      * @return A list of linkpages
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                 //Page page = (Page) i.next (  );
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      * Returns an orders list
307      *
308      * @param nPortletId The identifier of the portlet
309      * @return A list of orders
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      * Process link pages order modification
326      *
327      * @param request request
328      * @return Portlet's modification url
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      * Process link page's unselecting
366      *
367      * @param request request
368      * @return Portlet's modification url
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         // Updating Database
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      * Process link page's selecting
393      *
394      * @param request request
395      * @return Portlet's modification url
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         // Checking duplicate
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      * Process the selection of all link pages
435      *
436      * @param request request
437      * @return Portlet's modification url
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             // Checking duplicate
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 }