View Javadoc

1   /*
2    * Copyright (c) 2002-2013, 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      // Constants
62      private static final String PROPERTY_LINKS_PATHLABEL = "links.xPageLinks.pagePathLabel";
63      private static final String PROPERTY_LINKS_TITLE = "links.xPageLinks.pageTitle";
64      private static final String TEMPLATE_PORTLET_BLOCK = "skin/plugins/links/block_portlet.html";
65      private static final String TEMPLATE_XPAGE_LINKS = "skin/plugins/links/page_links_summary.html";
66      private static final String MARK_PORTLET_NAME = "portlet_name";
67      private static final String MARK_LINKS_LIST = "links_list";
68      private static final String MARK_PORTLETS_LIST = "list_portlets";
69  
70      /**
71       * Cr�e un nouvel objet LinksApp
72       */
73      public LinksApp(  )
74      {
75      }
76  
77      /**
78       * Returns the Links Page content depending on the request parameters and the current mode.
79       *
80       * @param request The HTTP request.
81       * @param nMode The current mode.
82       * @param plugin The plugin
83       * @return The page content.
84       */
85      public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin )
86      {
87          String strServerKey = AppPathService.getVirtualHostKey( request );
88          XPage page = new XPage(  );
89  
90          page.setContent( getPortletBlock( request, strServerKey ) );
91          page.setTitle( I18nService.getLocalizedString( PROPERTY_LINKS_TITLE, request.getLocale(  ) ) );
92          page.setPathLabel( I18nService.getLocalizedString( PROPERTY_LINKS_PATHLABEL, request.getLocale(  ) ) );
93  
94          return page;
95      }
96  
97      /**
98       * Return all the links portlets
99       *
100      * @param strServerKey the virtual host key or null for default host
101      * @return The Html block portlet
102      */
103     public String getPortletBlock( HttpServletRequest request, String strServerKey )
104     {
105         StringBuffer strBlocPortlet = new StringBuffer(  );
106         Collection<Portlet> listePortlet = LinksPortletHome.getPortletsInLinksPage(  );
107         Collection<Link> listLinksForVhost = new ArrayList<Link>(  );
108         HashMap<String, Object> modelTemplateXpageLinks = new HashMap<String, Object>(  );
109 
110         for ( Portlet portlet : listePortlet )
111         {
112             int nPortletId = portlet.getId(  );
113             HashMap model = new HashMap(  );
114             Collection<Link> listLinks = LinkHome.findByPortlet( nPortletId );
115 
116             for ( Link link : listLinks )
117             {
118                 String urlVhost = link.getUrl( strServerKey );
119 
120                 if ( urlVhost != null )
121                 {
122                     link.setUrl( urlVhost );
123                     listLinksForVhost.add( link );
124                 }
125             }
126 
127             if ( listLinksForVhost.size(  ) > 0 )
128             {
129                 model.put( MARK_LINKS_LIST, listLinksForVhost );
130                 model.put( MARK_PORTLET_NAME, portlet.getName(  ) );
131 
132                 HtmlTemplate tListLinks = AppTemplateService.getTemplate( TEMPLATE_PORTLET_BLOCK,
133                         request.getLocale(  ), model );
134                 strBlocPortlet.append( tListLinks.getHtml(  ) );
135                 listLinksForVhost.clear(  );
136             }
137         }
138 
139         modelTemplateXpageLinks.put( MARK_PORTLETS_LIST, strBlocPortlet.toString(  ) );
140 
141         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_XPAGE_LINKS, request.getLocale(  ),
142                 modelTemplateXpageLinks );
143 
144         return template.getHtml(  );
145     }
146 }