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;
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.LinksPortletHome;
39  import fr.paris.lutece.portal.business.portlet.Portlet;
40  import fr.paris.lutece.portal.service.i18n.I18nService;
41  import fr.paris.lutece.portal.service.plugin.Plugin;
42  import fr.paris.lutece.portal.service.template.AppTemplateService;
43  import fr.paris.lutece.portal.service.util.AppPathService;
44  import fr.paris.lutece.portal.web.xpages.XPage;
45  import fr.paris.lutece.portal.web.xpages.XPageApplication;
46  import fr.paris.lutece.util.html.HtmlTemplate;
47  
48  import java.util.ArrayList;
49  import java.util.Collection;
50  import java.util.HashMap;
51  
52  import javax.servlet.http.HttpServletRequest;
53  
54  
55  /**
56   * This class implements the links XPage
57   */
58  public class LinksApp implements XPageApplication
59  {
60      /**
61       * 
62       */
63      private static final long serialVersionUID = -4031785213438754274L;
64      ////////////////////////////////////////////////////////////////////////////
65      // Constants
66      private static final String PROPERTY_LINKS_PATHLABEL = "links.xPageLinks.pagePathLabel";
67      private static final String PROPERTY_LINKS_TITLE = "links.xPageLinks.pageTitle";
68      private static final String TEMPLATE_PORTLET_BLOCK = "skin/plugins/links/block_portlet.html";
69      private static final String TEMPLATE_XPAGE_LINKS = "skin/plugins/links/page_links_summary.html";
70      private static final String MARK_PORTLET_NAME = "portlet_name";
71      private static final String MARK_LINKS_LIST = "links_list";
72      private static final String MARK_PORTLETS_LIST = "list_portlets";
73  
74      /**
75       * Cr�e un nouvel objet LinksApp
76       */
77      public LinksApp(  )
78      {
79      }
80  
81      /**
82       * Returns the Links Page content depending on the request parameters and the current mode.
83       *
84       * @param request The HTTP request.
85       * @param nMode The current mode.
86       * @param plugin The plugin
87       * @return The page content.
88       */
89      public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin )
90      {
91          String strServerKey = AppPathService.getVirtualHostKey( request );
92          XPage page = new XPage(  );
93  
94          page.setContent( getPortletBlock( request, strServerKey ) );
95          page.setTitle( I18nService.getLocalizedString( PROPERTY_LINKS_TITLE, request.getLocale(  ) ) );
96          page.setPathLabel( I18nService.getLocalizedString( PROPERTY_LINKS_PATHLABEL, request.getLocale(  ) ) );
97  
98          return page;
99      }
100 
101     /**
102      * Return all the links portlets
103      *
104      * @param strServerKey the virtual host key or null for default host
105      * @return The Html block portlet
106      */
107     public String getPortletBlock( HttpServletRequest request, String strServerKey )
108     {
109         StringBuffer strBlocPortlet = new StringBuffer(  );
110         Collection<Portlet> listePortlet = LinksPortletHome.getPortletsInLinksPage(  );
111         Collection<Link> listLinksForVhost = new ArrayList<>(  );
112         HashMap<String, Object> modelTemplateXpageLinks = new HashMap<>(  );
113 
114         for ( Portlet portlet : listePortlet )
115         {
116             int nPortletId = portlet.getId(  );
117             HashMap<String, Object> model = new HashMap<>(  );
118             Collection<Link> listLinks = LinkHome.findByPortlet( nPortletId );
119 
120             for ( Link link : listLinks )
121             {
122                 String urlVhost = link.getUrl( strServerKey );
123 
124                 if ( urlVhost != null )
125                 {
126                     link.setUrl( urlVhost );
127                     listLinksForVhost.add( link );
128                 }
129             }
130 
131             if ( !listLinksForVhost.isEmpty( ) )
132             {
133                 model.put( MARK_LINKS_LIST, listLinksForVhost );
134                 model.put( MARK_PORTLET_NAME, portlet.getName(  ) );
135 
136                 HtmlTemplate tListLinks = AppTemplateService.getTemplate( TEMPLATE_PORTLET_BLOCK,
137                         request.getLocale(  ), model );
138                 strBlocPortlet.append( tListLinks.getHtml(  ) );
139                 listLinksForVhost.clear(  );
140             }
141         }
142 
143         modelTemplateXpageLinks.put( MARK_PORTLETS_LIST, strBlocPortlet.toString(  ) );
144 
145         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_XPAGE_LINKS, request.getLocale(  ),
146                 modelTemplateXpageLinks );
147 
148         return template.getHtml(  );
149     }
150 }