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.googleapi.web;
35  
36  import fr.paris.lutece.plugins.googleapi.business.FeedProvider;
37  import fr.paris.lutece.plugins.googleapi.business.Item;
38  import fr.paris.lutece.plugins.googleapi.service.FeedsService;
39  import fr.paris.lutece.portal.service.i18n.I18nService;
40  import fr.paris.lutece.portal.service.plugin.Plugin;
41  import fr.paris.lutece.portal.service.template.AppTemplateService;
42  import fr.paris.lutece.portal.service.util.AppLogService;
43  import fr.paris.lutece.portal.web.xpages.XPage;
44  import fr.paris.lutece.portal.web.xpages.XPageApplication;
45  import fr.paris.lutece.util.html.HtmlTemplate;
46  
47  import org.xml.sax.SAXException;
48  
49  import java.io.IOException;
50  
51  import java.util.ArrayList;
52  import java.util.HashMap;
53  import java.util.List;
54  import java.util.Locale;
55  
56  import javax.servlet.http.HttpServletRequest;
57  
58  import javax.xml.parsers.ParserConfigurationException;
59  
60  
61  /**
62   * Google Base API Application
63   *
64   */
65  public class GoogleApiApp implements XPageApplication
66  {
67      private static final String TEMPLATE_PROJECT = "skin/plugins/googleapi/home.html";
68      private static final String KEY_PAGE_TITLE = "googleapi.pageTitle";
69      private static final String KEY_PAGE_PATH = "googleapi.pagePath";
70      private static final String MARK_QUERY = "query";
71      private static final String MARK_PROVIDER = "provider";
72      private static final String MARK_ITEMS_LIST = "items_list";
73      private static final String MARK_FEEDS_LIST = "feeds_list";
74      private static final String PARAMETER_SEARCH = "search";
75      private static final String PARAMETER_PROVIDER = "provider";
76  
77      /**
78       * Build the XPage
79       * @param request The HTTP request
80       * @param nMode The current mode
81       * @param plugin The current plugin
82       * @return The XPage
83       */
84      public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin )
85      {
86          XPage page = new XPage(  );
87  
88          Locale locale = request.getLocale(  );
89          HashMap model = new HashMap(  );
90  
91          String strQuery = request.getParameter( PARAMETER_SEARCH );
92          String strProvider = request.getParameter( PARAMETER_PROVIDER );
93  
94          FeedProvider fp = FeedsService.getInstance(  ).getProvider( strProvider );
95  
96          List<Item> listItems = new ArrayList<Item>(  );
97  
98          if ( strQuery != null )
99          {
100             try
101             {
102                 FeedsService.getInstance(  ).getItems( strQuery, listItems, fp );
103             }
104             catch ( ParserConfigurationException e )
105             {
106                 AppLogService.error( "Error retrieving items : " + e.getMessage(  ), e );
107             }
108             catch ( SAXException e )
109             {
110                 AppLogService.error( "Error retrieving items : " + e.getMessage(  ), e );
111             }
112             catch ( IOException e )
113             {
114                 AppLogService.error( "Error retrieving items : " + e.getMessage(  ), e );
115             }
116         }
117 
118         model.put( MARK_QUERY, ( strQuery != null ) ? strQuery : "" );
119         model.put( MARK_PROVIDER, ( strProvider != null ) ? strProvider : "" );
120         model.put( MARK_ITEMS_LIST, listItems );
121         model.put( MARK_FEEDS_LIST, FeedsService.getInstance(  ).getProviders(  ) );
122 
123         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_PROJECT, locale, model );
124         page.setContent( template.getHtml(  ) );
125 
126         // Set page title and path
127         Object[] args = { model.get( "name" ) };
128         String strTitle = I18nService.getLocalizedString( KEY_PAGE_TITLE, args, locale );
129         String strPath = I18nService.getLocalizedString( KEY_PAGE_PATH, args, locale );
130         page.setTitle( strTitle );
131         page.setPathLabel( strPath );
132 
133         return page;
134     }
135 }