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.links.web.portlet;
35  
36  import fr.paris.lutece.plugins.links.business.Link;
37  import fr.paris.lutece.plugins.links.business.LinkHome;
38  import fr.paris.lutece.plugins.links.business.portlet.LinksPortlet;
39  import fr.paris.lutece.plugins.links.business.portlet.LinksPortletHome;
40  import fr.paris.lutece.portal.business.portlet.Portlet;
41  import fr.paris.lutece.portal.business.portlet.PortletHome;
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.service.util.AppPathService;
47  import fr.paris.lutece.portal.service.util.AppPropertiesService;
48  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
49  import fr.paris.lutece.portal.web.constants.Messages;
50  import fr.paris.lutece.portal.web.constants.Parameters;
51  import fr.paris.lutece.portal.web.portlet.PortletJspBean;
52  import fr.paris.lutece.util.ReferenceItem;
53  import fr.paris.lutece.util.ReferenceList;
54  import fr.paris.lutece.util.html.HtmlTemplate;
55  
56  import java.io.Serializable;
57  import java.util.Collection;
58  import java.util.HashMap;
59  import javax.servlet.http.HttpServletRequest;
60  
61  
62  /**
63   * This class provides the user interface to manage Link Portlet
64   */
65  public class LinksPortletJspBean extends PortletJspBean
66  {
67      ////////////////////////////////////////////////////////////////////////////
68      // Constants
69  
70      /**
71       * 
72       */
73      private static final long serialVersionUID = -3756390204133716455L;
74  
75      // Right
76      public static final String RIGHT_MANAGE_ADMIN_SITE = "CORE_ADMIN_SITE";
77  
78      // JSP
79      private static final String JSP_DO_MODIFY_PORTLET = "../../DoModifyPortlet.jsp";
80  
81      // Templates
82      private static final String TEMPLATE_LINKS_LIST = "/admin/plugins/links/links_list_portlet.html";
83      private static final String TEMPLATE_UNSELECTED_LINKS = "/admin/plugins/links/unselected_links.html";
84  
85      // Parameters
86      private static final String PARAMETER_LINKS_ORDER = "links_order";
87      private static final String PARAMETER_PORTLET_ID = "portlet_id";
88      private static final String PARAMETER_PAGE_ID = "page_id";
89      private static final String PARAMETER_PORTLET_TYPE_ID = "portlet_type_id";
90      private static final String PARAMETER_LINKS = "links";
91      private static final String PARAMETER_LINK_ID = "link_id";
92  
93      // Properties
94      private static final String PROPERTY_NO_IMAGE_ADMIN = "links.no.image.admin";
95      private static final String MESSAGE_PORTLET_LINK_SELECTED = "links.message.already_selected";
96  
97      // Marker
98      private static final String MARK_LINKS_LIST = "links_list";
99      private static final String MARK_LINK = "link";
100     private static final String MARK_LINK_DEFAULT_ORDER = "link_default_order";
101     private static final String MARK_ORDER_LIST = "order_list";
102     private static final String MARK_PORTLET_ID = "portlet_id";
103     private static final String MARK_NEW_LINK = "new_link";
104     private static final String MARK_DEFAULT_LINK = "default_link";
105     private static final String MARK_DEFAULT_ORDER = "default_order";
106     private static final String MARK_NO_IMAGE = "no_image";
107     private static final String MARK_UNSELECTED_LINKS = "unselected_links";
108 
109     /**
110      * Creates a new LinkPortletJspBean object.
111      */
112     public LinksPortletJspBean(  )
113     {
114     }
115 
116     /**
117      * Returns portlet links's creation form
118      *
119      * @param request request
120      * @return Html form
121      */
122     public String getCreate( HttpServletRequest request )
123     {
124         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
125         String strPortletTypeId = request.getParameter( PARAMETER_PORTLET_TYPE_ID );
126 
127         HtmlTemplate template = getCreateTemplate( strPageId, strPortletTypeId );
128 
129         return template.getHtml(  );
130     }
131 
132     /**
133      * Returns portlet links's modification form
134      *
135      * @param request request
136      * @return Html form
137      */
138     public String getModify( HttpServletRequest request )
139     {
140         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
141         int nPortletId = Integer.parseInt( strPortletId );
142         Portlet portlet = PortletHome.findByPrimaryKey( nPortletId );
143         HashMap<String, Object> model = new HashMap<>(  );
144 
145         // get links list
146         model.put( MARK_LINKS_LIST, getLinksInPortletList( nPortletId ) );
147 
148         //get unselected links
149         model.put( MARK_UNSELECTED_LINKS, getUnselectedLinks( nPortletId ) );
150 
151         HtmlTemplate template = getModifyTemplate( portlet, model );
152 
153         return template.getHtml(  );
154     }
155 
156     /**
157      * Process linksPortlet's creation
158      *
159      * @param request request
160      * @return Portlet's modification url
161      */
162     public String doCreate( HttpServletRequest request )
163     {
164         LinksPortletnks/business/portlet/LinksPortlet.html#LinksPortlet">LinksPortlet portlet = new LinksPortlet(  );
165         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
166         int nPageId = Integer.parseInt( strPageId );
167 
168         // get portlet common attributes
169         String strErrorUrl = setPortletCommonData( request, portlet );
170 
171         if ( strErrorUrl != null )
172         {
173             return strErrorUrl;
174         }
175 
176         portlet.setPageId( nPageId );
177 
178         // Creating portlet
179         LinksPortletHome.getInstance(  ).create( portlet );
180 
181         //Displays the page with the new Portlet
182         return getPageUrl( nPageId );
183     }
184 
185     /**
186      * Process links order modification
187      *
188      * @param request request
189      * @return Portlet's modification url
190      */
191     public String doModifyOrderLinks( HttpServletRequest request )
192     {
193         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
194         int nPortletId = Integer.parseInt( strPortletId );
195         String strLinkId = request.getParameter( PARAMETER_LINK_ID );
196         int nLinkId = Integer.parseInt( strLinkId );
197         int nOldOrder = LinksPortletHome.getLinkOrder( nPortletId, nLinkId );
198         String strOrderLink = request.getParameter( PARAMETER_LINKS_ORDER );
199         int nOrder = Integer.parseInt( strOrderLink );
200 
201         if ( nOrder < nOldOrder )
202         {
203             for ( int i = nOldOrder - 1; i > ( nOrder - 1 ); i-- )
204             {
205                 int nLinkIdTemp = LinksPortletHome.getLinkIdByOrder( nPortletId, i );
206                 LinksPortletHome.updateLinkOrder( i + 1, nPortletId, nLinkIdTemp );
207             }
208 
209             LinksPortletHome.updateLinkOrder( nOrder, nPortletId, nLinkId );
210         }
211         else if ( nOrder > nOldOrder )
212         {
213             for ( int i = nOldOrder; i < ( nOrder + 1 ); i++ )
214             {
215                 int nLinkIdTemp = LinksPortletHome.getLinkIdByOrder( nPortletId, i );
216                 LinksPortletHome.updateLinkOrder( i - 1, nPortletId, nLinkIdTemp );
217             }
218 
219             LinksPortletHome.updateLinkOrder( nOrder, nPortletId, nLinkId );
220         }
221 
222         return JSP_DO_MODIFY_PORTLET + "?" + PARAMETER_PORTLET_ID + "=" + nPortletId;
223     }
224 
225     /**
226      * Process portlet's modification
227      *
228      * @param request request
229      * @return The Jsp management URL of the process result
230      */
231     public String doModify( HttpServletRequest request )
232     {
233         // Getting portlet
234         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
235         int nPortletId = Integer.parseInt( strPortletId );
236         LinksPortlet./../../../../fr/paris/lutece/plugins/links/business/portlet/LinksPortlet.html#LinksPortlet">LinksPortlet portlet = (LinksPortlet) PortletHome.findByPrimaryKey( nPortletId );
237 
238         String strStyleId = request.getParameter( Parameters.STYLE );
239 
240         if ( ( strStyleId == null ) || strStyleId.trim(  ).equals( "" ) )
241         {
242             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_ERROR );
243         }
244 
245         // get portlet common attributes
246         String strErrorUrl = setPortletCommonData( request, portlet );
247 
248         if ( strErrorUrl != null )
249         {
250             return strErrorUrl;
251         }
252 
253         // mandatory fields
254         String strName = portlet.getName(  );
255 
256         if ( strName.trim(  ).equals( "" ) )
257         {
258             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_ERROR );
259         }
260 
261         // Modificating portlet
262         portlet.update(  );
263 
264         return getPageUrl( portlet.getPageId(  ) );
265     }
266 
267     /**
268      *
269      *
270      * @param nPortletId The identifier of the portlet
271      * @return
272      */
273     private String getUnselectedLinks( int nPortletId )
274     {
275         HashMap<String, Serializable> model = new HashMap<>(  );
276         ReferenceList ordersList = getNewLinkOrdersList( nPortletId );
277 
278         ReferenceList linksList = new ReferenceList(  );
279 
280         Collection<Link> links = LinkHome.getLinksList(  );
281 
282         links = (Collection<Link>) AdminWorkgroupService.getAuthorizedCollection( links, getUser(  ) );
283 
284         for ( Link link : links )
285         {
286             linksList.addItem( link.getId(  ), link.getName(  ) + " " + link.getUrl(  ) );
287         }
288 
289         model.put( MARK_ORDER_LIST, ordersList );
290         model.put( MARK_LINKS_LIST, linksList );
291         model.put( MARK_PORTLET_ID, nPortletId );
292         model.put( MARK_DEFAULT_ORDER, 0 );
293         model.put( MARK_DEFAULT_LINK, 0 );
294 
295         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_UNSELECTED_LINKS, getLocale(  ), model );
296 
297         return template.getHtml(  );
298     }
299 
300     /**
301      * Returns The list of links wich belong to a specified portlet
302      *
303      * @param nPortletId The identifier of the portlet
304      * @return A list of links
305      */
306     private String getLinksInPortletList( int nPortletId )
307     {
308         StringBuffer strLinksList = new StringBuffer(  );
309         Collection<Link> list = LinksPortletHome.getLinksInPortletList( nPortletId );
310 
311         list = (Collection<Link>) AdminWorkgroupService.getAuthorizedCollection( list, getUser(  ) );
312 
313         ReferenceList ordersList = getOrdersList( nPortletId );
314         ReferenceList optUrls = null;
315         ReferenceList virtualHosts = new ReferenceList(  );
316 
317         for ( Link link : list )
318         {
319             HashMap model = new HashMap(  );
320             Integer nOrderLink = new Integer( LinksPortletHome.getLinkOrder( nPortletId, link.getId(  ) ) );
321 
322             //Replace links optional url code by url name
323             optUrls = link.getOptionalUrls(  );
324 
325             try
326             {
327                 if ( AppPathService.getAvailableVirtualHosts(  ) != null )
328                 {
329                     virtualHosts = AppPathService.getAvailableVirtualHosts(  );
330 
331                     for ( ReferenceItem item : virtualHosts )
332                     {
333                         for ( ReferenceItem optUrl : optUrls )
334                         {
335                             if ( optUrl.getCode(  ).equals( item.getCode(  ) ) )
336                             {
337                                 optUrl.setCode( item.getName(  ) );
338                             }
339                         }
340                     }
341                 }
342             }
343             catch ( NullPointerException e )
344             {
345                 AppLogService.error( e.getMessage(  ), e );
346             }
347 
348             link.setOptionalUrls( optUrls );
349 
350             model.put( MARK_LINK, link );
351             model.put( MARK_NO_IMAGE, AppPropertiesService.getProperty( PROPERTY_NO_IMAGE_ADMIN ) );
352             model.put( MARK_ORDER_LIST, ordersList );
353             model.put( MARK_LINK_DEFAULT_ORDER, nOrderLink );
354             model.put( MARK_PORTLET_ID, nPortletId );
355             model.put( MARK_NEW_LINK, "0" );
356 
357             HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_LINKS_LIST, getLocale(  ), model );
358             strLinksList.append( template.getHtml(  ) );
359         }
360 
361         return strLinksList.toString(  );
362     }
363 
364     /**
365      * Returns an orders list
366      *
367      * @param nPortletId The identifier of the portlet
368      * @return A list of orders
369      */
370     private ReferenceList getOrdersList( int nPortletId )
371     {
372         int nMax = LinksPortletHome.getMaxOrder( nPortletId );
373         ReferenceList list = new ReferenceList(  );
374 
375         for ( int i = 1; i < ( nMax + 1 ); i++ )
376         {
377             list.addItem( i, Integer.toString( i ) );
378         }
379 
380         return list;
381     }
382 
383     /**
384      * Returns an orders list for a new link in a specified portlet
385      *
386      * @param nPortletId The identifier of the portlet
387      * @return A list of orders
388      */
389     private ReferenceList getNewLinkOrdersList( int nPortletId )
390     {
391         int nMax = LinksPortletHome.getMaxOrder( nPortletId );
392         ReferenceList list = new ReferenceList(  );
393 
394         for ( int i = 1; i < ( nMax + 2 ); i++ )
395         {
396             list.addItem( i, Integer.toString( i ) );
397         }
398 
399         return list;
400     }
401 
402     /**
403      * Process links's selecting
404      *
405      * @param request request
406      * @return Portlet's modification url
407      */
408     public String doSelectLink( HttpServletRequest request )
409     {
410         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
411         int nPortletId = Integer.parseInt( strPortletId );
412         String strLinkId = request.getParameter( PARAMETER_LINKS );
413         int nLinkId = Integer.parseInt( strLinkId );
414         String strOrder = request.getParameter( PARAMETER_LINKS_ORDER );
415         int nOrder = Integer.parseInt( strOrder );
416 
417         // Checking duplicate
418         if ( LinksPortletHome.testDuplicate( nPortletId, nLinkId ) )
419         {
420             return AdminMessageService.getMessageUrl( request, MESSAGE_PORTLET_LINK_SELECTED, AdminMessage.TYPE_ERROR );
421         }
422 
423         int nMax = LinksPortletHome.getMaxOrder( nPortletId );
424 
425         for ( int i = nOrder; i < ( nMax + 1 ); i++ )
426         {
427             int nLinkIdTemp = LinksPortletHome.getLinkIdByOrder( nPortletId, i );
428             LinksPortletHome.updateLinkOrder( i + 1, nPortletId, nLinkIdTemp );
429         }
430 
431         LinksPortletHome.insertLink( nPortletId, nLinkId, nOrder );
432 
433         return JSP_DO_MODIFY_PORTLET + "?" + PARAMETER_PORTLET_ID + "=" + nPortletId;
434     }
435 
436     /**
437      * Process Link's unselecting
438      *
439      * @param request request
440      * @return Portlet's modification url
441      */
442     public String doUnselectLinks( HttpServletRequest request )
443     {
444         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
445         int nPortletId = Integer.parseInt( strPortletId );
446         String strLinkId = request.getParameter( PARAMETER_LINK_ID );
447         int nLinkId = Integer.parseInt( strLinkId );
448         int nOrder = LinksPortletHome.getLinkOrder( nPortletId, nLinkId );
449         int nMax = LinksPortletHome.getMaxOrder( nPortletId );
450 
451         // Updating Database
452         LinksPortletHome.removeLink( nPortletId, nLinkId );
453 
454         for ( int i = nOrder + 1; i < ( nMax + 1 ); i++ )
455         {
456             int nLinkIdTemp = LinksPortletHome.getLinkIdByOrder( nPortletId, i );
457             LinksPortletHome.updateLinkOrder( i - 1, nPortletId, nLinkIdTemp );
458         }
459 
460         return JSP_DO_MODIFY_PORTLET + "?" + PARAMETER_PORTLET_ID + "=" + nPortletId;
461     }
462 
463     /**
464      * Returns portlet's properties prefix
465      *
466      * @return prefix
467      */
468     public String getPropertiesPrefix(  )
469     {
470         return "portlet.links";
471     }
472 }