View Javadoc
1   /*
2    * Copyright (c) 2002-2018, 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.stock.modules.recommendation.web;
35  
36  import fr.paris.lutece.plugins.stock.modules.recommendation.business.RecommendedProduct;
37  import fr.paris.lutece.plugins.stock.modules.recommendation.service.StockRecommendationService;
38  import fr.paris.lutece.portal.service.security.LuteceUser;
39  import fr.paris.lutece.portal.service.security.SecurityService;
40  import fr.paris.lutece.portal.service.security.UserNotSignedException;
41  import fr.paris.lutece.portal.service.util.AppLogService;
42  import fr.paris.lutece.portal.service.util.AppPropertiesService;
43  import fr.paris.lutece.portal.web.xpages.XPage;
44  import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
45  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
46  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
47  import fr.paris.lutece.portal.web.l10n.LocaleService;
48  import java.util.List;
49  import java.util.Map;
50  
51  import javax.servlet.http.HttpServletRequest;
52  import org.apache.mahout.cf.taste.common.TasteException;
53  
54  /**
55   * This class provides a simple implementation of an XPage
56   */
57  @Controller( xpageName = "recommendation", pageTitleI18nKey = "module.stock.recommendation.xpage.recommendation.pageTitle", pagePathI18nKey = "module.stock.recommendation.xpage.recommendation.pagePathLabel" )
58  public class RecommendationApp extends MVCApplication
59  {
60      public static final String MARK_PRODUCTS_LIST = "products_list";
61      public static final String MARK_PRODUCT_LINK_URL = "product_link_url";
62      public static final String PROPERTY_LINK_URL = "stock-recommendation.product.linkUrl";
63      public static final String PRODUCT_LINK_URL = AppPropertiesService.getProperty( PROPERTY_LINK_URL );
64  
65      private static final String TEMPLATE_XPAGE = "/skin/plugins/stock/modules/recommendation/recommendation.html";
66      private static final String PROPERTY_TEST_USER = "stock-recommendation.testuser";
67      private static final String VIEW_HOME = "home";
68      private static final String PARAMETER_USERNAME = "username";
69  
70      /**
71       * Returns the content of the page recommendation.
72       * 
73       * @param request
74       *            The HTTP request
75       * @return The view
76       * @throws UserNotSignedException
77       *             if no user connected
78       */
79      @View( value = VIEW_HOME, defaultView = true )
80      public XPage viewHome( HttpServletRequest request ) throws UserNotSignedException
81      {
82  
83          String strUserName = getUsername( request );
84  
85          List<RecommendedProduct> listProducts = null;
86          try
87          {
88              listProducts = StockRecommendationService.instance( ).getRecommendedProducts( strUserName );
89          }
90          catch( TasteException ex )
91          {
92              // User not found
93              addError( "User not found" );
94              AppLogService.info( "Recommendation error : " + ex.getMessage( ) );
95          }
96          Map<String, Object> model = getModel( );
97          model.put( MARK_PRODUCTS_LIST, listProducts );
98          model.put( MARK_PRODUCT_LINK_URL, PRODUCT_LINK_URL );
99          return getXPage( TEMPLATE_XPAGE, LocaleService.getDefault( ), model );
100     }
101 
102     /**
103      * Gets the user name
104      * 
105      * @param request
106      *            The HTTP request
107      * @return the user name
108      * @throws UserNotSignedException
109      */
110     public static String getUsername( HttpServletRequest request ) throws UserNotSignedException
111     {
112         String strUserName;
113         // ////////// Test features - begin
114         strUserName = AppPropertiesService.getProperty( PROPERTY_TEST_USER );
115 
116         if ( request != null )
117         {
118             String strRequestUser = request.getParameter( PARAMETER_USERNAME );
119             if ( strRequestUser != null )
120             {
121                 strUserName = strRequestUser;
122             }
123         }
124         // ////////// Test features - end
125 
126         if ( strUserName == null )
127         {
128             LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
129             if ( user == null )
130             {
131                 throw new UserNotSignedException( );
132             }
133             strUserName = user.getName( );
134         }
135         return strUserName;
136     }
137 }