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.htmlpage.web;
35
36 import fr.paris.lutece.plugins.htmlpage.business.HtmlPage;
37 import fr.paris.lutece.plugins.htmlpage.service.HtmlPageService;
38 import fr.paris.lutece.plugins.htmlpage.utils.HtmlPageUtil;
39 import fr.paris.lutece.portal.service.message.SiteMessage;
40 import fr.paris.lutece.portal.service.message.SiteMessageException;
41 import fr.paris.lutece.portal.service.message.SiteMessageService;
42 import fr.paris.lutece.portal.service.plugin.Plugin;
43 import fr.paris.lutece.portal.service.plugin.PluginService;
44 import fr.paris.lutece.portal.service.search.SearchEngine;
45 import fr.paris.lutece.portal.service.search.SearchResult;
46 import fr.paris.lutece.portal.service.spring.SpringContextService;
47 import fr.paris.lutece.portal.service.template.AppTemplateService;
48 import fr.paris.lutece.portal.service.util.AppPropertiesService;
49 import fr.paris.lutece.portal.web.xpages.XPage;
50 import fr.paris.lutece.portal.web.xpages.XPageApplication;
51 import fr.paris.lutece.util.html.HtmlTemplate;
52
53 import java.util.ArrayList;
54 import java.util.Collection;
55 import java.util.HashMap;
56 import java.util.List;
57
58 import javax.servlet.http.HttpServletRequest;
59
60 import org.apache.commons.lang3.StringUtils;
61
62
63
64
65 public class HtmlPageApp implements XPageApplication
66 {
67
68
69
70
71 private static final long serialVersionUID = 4891750061103627788L;
72
73
74 private static final String PARAMETER_PAGE = "page";
75 private static final String PARAMETER_HTMLPAGE_ID = "htmlpage_id";
76 private static final String PARAMETER_QUERY = "query";
77
78
79 private static final String PROPERTY_PAGE_TITLE = "htmlpage.pageTitle";
80 private static final String PROPERTY_PAGE_PATH = "htmlpage.pagePathLabel";
81
82
83 private static final String PROPERTY_MESSAGE_ERROR_HTMLPAGE = "htmlpage.message.pageInvalid";
84 private static final String PROPERTY_MESSAGE_NOT_AUTHORIZED = "htmlpage.message.notAuthorized";
85
86
87 private static final String MARK_HTMLPAGE_LIST = "htmlpages_list";
88 private static final String MARK_HTMLPAGE = "htmlpage";
89 private static final String MARK_PAGE = "page";
90 private static final String MARK_RESULT = "result";
91 private static final String MARK_QUERY = "query";
92
93
94 private static final String TEMPLATE_XPAGE_HTMLPAGE = "skin/plugins/htmlpage/page_htmlpage.html";
95 private static final String TEMPLATE_XPAGE_HTMLPAGE_LISTS = "skin/plugins/htmlpage/htmlpages_list.html";
96
97 private static final String BEAN_SEARCH_ENGINE = "htmlpage.htmlpageSearchEngine";
98
99
100 private Plugin _plugin;
101
102
103 public HtmlPageApp( )
104 {
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin ) throws SiteMessageException
121 {
122 XPage page = new XPage( );
123
124 String strPluginName = request.getParameter( PARAMETER_PAGE );
125 String strQuery = request.getParameter( PARAMETER_QUERY );
126 _plugin = PluginService.getPlugin( strPluginName );
127
128 page.setTitle( AppPropertiesService.getProperty( PROPERTY_PAGE_TITLE ) );
129 page.setPathLabel( AppPropertiesService.getProperty( PROPERTY_PAGE_PATH ) );
130
131 String strHtmlPageId = request.getParameter( PARAMETER_HTMLPAGE_ID );
132
133 if ( StringUtils.isNotBlank( strQuery ) )
134 {
135 page.setContent( getSearch( strQuery, strPluginName, request ) );
136 }
137 else
138 {
139 if ( strHtmlPageId == null )
140 {
141 page.setContent( getHtmlPagesLists( request ) );
142 }
143
144 if ( strHtmlPageId != null )
145 {
146 page.setContent( getHtmlPage( request, strHtmlPageId ) );
147 }
148 }
149
150 return page;
151 }
152
153 private String getSearch( String strQuery, String strPluginName, HttpServletRequest request )
154 {
155 SearchEngine engine = SpringContextService.getBean( BEAN_SEARCH_ENGINE );
156 List<SearchResult> listResults = engine.getSearchResults( strQuery, request );
157
158 HashMap<String, Object> model = new HashMap<String, Object>( );
159 model.put( MARK_RESULT, listResults );
160 model.put( MARK_QUERY, strQuery );
161
162 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_XPAGE_HTMLPAGE_LISTS, request.getLocale( ), model );
163 return template.getHtml( );
164 }
165
166 private String getHtmlPagesLists( HttpServletRequest request ) throws SiteMessageException
167 {
168 HashMap<String, Object> model = new HashMap<String, Object>( );
169
170 Collection<HtmlPage> htmlPageList = HtmlPageService.getInstance( ).getHtmlPageListCache( );
171 Collection<HtmlPage> visibleHtmlPageList = new ArrayList<HtmlPage>( );
172
173 for ( HtmlPage htmlpage : htmlPageList )
174 {
175 if( HtmlPageUtil.isVisible( request, htmlpage.getRole( ) ) )
176 {
177 visibleHtmlPageList.add( htmlpage );
178 }
179 }
180
181 model.put( MARK_HTMLPAGE_LIST, visibleHtmlPageList );
182
183 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_XPAGE_HTMLPAGE_LISTS, request.getLocale( ), model );
184
185 return template.getHtml( );
186 }
187
188
189
190
191
192
193
194
195
196
197 private String getHtmlPage( HttpServletRequest request, String strHtmlPageId ) throws SiteMessageException
198 {
199 HashMap<String, Object> model = new HashMap<String, Object>( );
200
201 int nHtmlPageId = Integer.parseInt( strHtmlPageId );
202 HtmlPage htmlpage = HtmlPageService.getInstance( ).getHtmlPageCache( nHtmlPageId );
203
204 if ( htmlpage != null )
205 {
206 if ( HtmlPageUtil.isVisible( request, htmlpage.getRole( ) ) )
207 {
208 model.put( MARK_HTMLPAGE, htmlpage );
209 model.put( MARK_PAGE, _plugin.getName( ) );
210 }
211 else
212 {
213 SiteMessageService.setMessage( request, PROPERTY_MESSAGE_NOT_AUTHORIZED, SiteMessage.TYPE_ERROR );
214 }
215 }
216 else
217 {
218 SiteMessageService.setMessage( request, PROPERTY_MESSAGE_ERROR_HTMLPAGE, SiteMessage.TYPE_ERROR );
219 }
220
221 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_XPAGE_HTMLPAGE, request.getLocale( ), model );
222
223 return template.getHtml( );
224 }
225 }