1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
72
73
74
75
76
77
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
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
104
105
106
107
108
109
110 public static String getUsername( HttpServletRequest request ) throws UserNotSignedException
111 {
112 String strUserName;
113
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
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 }