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.portal.service.portal;
35
36 import fr.paris.lutece.portal.business.XmlContent;
37 import fr.paris.lutece.portal.business.page.Page;
38 import fr.paris.lutece.portal.business.page.PageHome;
39 import fr.paris.lutece.portal.business.portalcomponent.PortalComponentHome;
40 import fr.paris.lutece.portal.business.style.ModeHome;
41 import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
42 import fr.paris.lutece.portal.service.cache.AbstractCacheableService;
43 import fr.paris.lutece.portal.service.html.XmlTransformerService;
44 import fr.paris.lutece.portal.service.page.PageEvent;
45 import fr.paris.lutece.portal.service.page.PageEventListener;
46 import fr.paris.lutece.portal.service.page.PageService;
47 import fr.paris.lutece.portal.service.security.LuteceUser;
48 import fr.paris.lutece.portal.service.security.SecurityService;
49 import fr.paris.lutece.util.xml.XmlUtil;
50
51 import org.apache.commons.lang3.StringUtils;
52
53 import java.util.Arrays;
54 import java.util.Collection;
55 import java.util.HashMap;
56 import java.util.Map;
57 import java.util.Properties;
58
59 import javax.servlet.http.HttpServletRequest;
60
61
62
63
64 public final class PortalMenuService extends AbstractCacheableService implements PageEventListener
65 {
66 public static final int MENU_INIT = 0;
67 public static final int MENU_MAIN = 1;
68 public static final int MODE_NORMAL = 0;
69 public static final int MODE_ADMIN = 1;
70 private static final int PORTAL_COMPONENT_MENU_INIT_ID = 3;
71 private static final int PORTAL_COMPONENT_MAIN_MENU_ID = 4;
72 private static final String SERVICE_NAME = "PortalMenuService";
73
74
75 private static PortalMenuService _singleton;
76
77
78 private PortalMenuService( )
79 {
80 initCache( getName( ) );
81 PageService.addPageEventListener( this );
82 }
83
84
85
86
87
88
89 public static synchronized PortalMenuService getInstance( )
90 {
91 if ( _singleton == null )
92 {
93 _singleton = new PortalMenuService( );
94 }
95
96 return _singleton;
97 }
98
99
100
101
102
103
104 public String getName( )
105 {
106 return SERVICE_NAME;
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 public String getMenuContent( int nCurrentPageId, int nMode, int nPart, HttpServletRequest request )
123 {
124 String strKey = getKey( nMode, nPart, request );
125 String strMenu = (String) getFromCache( strKey );
126
127
128 if ( strMenu == null )
129 {
130
131 strMenu = buildMenuContent( nCurrentPageId, nMode, nPart, request );
132
133
134 putInCache( strKey, strMenu );
135
136 return strMenu;
137 }
138
139
140 return strMenu;
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 private String buildMenuContent( int nCurrentPageId, int nMode, int nPart, HttpServletRequest request )
157 {
158 Collection<Page> listPagesMenu = PageHome.getChildPagesMinimalData( PortalService.getRootPageId( ) );
159
160 StringBuffer strXml = new StringBuffer( );
161 strXml.append( XmlUtil.getXmlHeader( ) );
162 XmlUtil.beginElement( strXml, XmlContent.TAG_MENU_LIST );
163
164 int nMenuIndex = 1;
165
166 for ( Page menuPage : listPagesMenu )
167 {
168 if ( ( menuPage.isVisible( request ) ) || ( nMode == MODE_ADMIN ) )
169 {
170 buildPageXml( menuPage, strXml, nMode, nMenuIndex, nCurrentPageId, request );
171
172 nMenuIndex++;
173 }
174 }
175
176 XmlUtil.endElement( strXml, XmlContent.TAG_MENU_LIST );
177
178
179 StyleSheet xslSource = getMenuXslSource( nMode, nPart );
180
181 Properties outputProperties = ModeHome.getOuputXslProperties( nMode );
182
183
184
185 Map<String, String> mapParamRequest = new HashMap<>( );
186 PortalService.setXslPortalPath( mapParamRequest, nMode );
187
188 XmlTransformerServicervice.html#XmlTransformerService">XmlTransformerService xmlTransformerService = new XmlTransformerService( );
189
190 return xmlTransformerService.transformBySourceWithXslCache( strXml.toString( ), xslSource, mapParamRequest, outputProperties );
191 }
192
193 private void buildPageXml( Page menuPage, StringBuffer strXml, int nMode, int nMenuIndex, int nCurrentPageId, HttpServletRequest request )
194 {
195 XmlUtil.beginElement( strXml, XmlContent.TAG_MENU );
196 XmlUtil.addElement( strXml, XmlContent.TAG_MENU_INDEX, nMenuIndex );
197 XmlUtil.addElement( strXml, XmlContent.TAG_PAGE_ID, menuPage.getId( ) );
198 XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_NAME, menuPage.getName( ) );
199 XmlUtil.addElementHtml( strXml, XmlContent.TAG_CURRENT_PAGE_ID, String.valueOf( nCurrentPageId ) );
200
201 Collection<Page> listSubLevelMenuPages = PageHome.getChildPagesMinimalData( menuPage.getId( ) );
202
203
204 if ( !listSubLevelMenuPages.isEmpty( ) )
205 {
206
207 XmlUtil.beginElement( strXml, XmlContent.TAG_SUBLEVEL_MENU_LIST );
208
209 int nSubLevelMenuIndex = 1;
210
211 for ( Page subLevelMenuPage : listSubLevelMenuPages )
212 {
213 if ( ( subLevelMenuPage.isVisible( request ) ) || ( nMode == MODE_ADMIN ) )
214 {
215 XmlUtil.beginElement( strXml, XmlContent.TAG_SUBLEVEL_MENU );
216 XmlUtil.addElement( strXml, XmlContent.TAG_MENU_INDEX, nMenuIndex );
217 XmlUtil.addElement( strXml, XmlContent.TAG_SUBLEVEL_INDEX, nSubLevelMenuIndex );
218 XmlUtil.addElement( strXml, XmlContent.TAG_PAGE_ID, subLevelMenuPage.getId( ) );
219 XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_NAME, subLevelMenuPage.getName( ) );
220 XmlUtil.endElement( strXml, XmlContent.TAG_SUBLEVEL_MENU );
221 XmlUtil.addElementHtml( strXml, XmlContent.TAG_CURRENT_PAGE_ID, String.valueOf( nCurrentPageId ) );
222 }
223 }
224
225 XmlUtil.endElement( strXml, XmlContent.TAG_SUBLEVEL_MENU_LIST );
226 }
227
228 XmlUtil.endElement( strXml, XmlContent.TAG_MENU );
229 }
230
231 private StyleSheet getMenuXslSource( int nMode, int nPart )
232 {
233
234 StyleSheet xslSource;
235
236
237 switch( nMode )
238 {
239 case MODE_NORMAL:
240 case MODE_ADMIN:
241 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_MAIN_MENU_ID, MODE_NORMAL );
242
243 break;
244
245 default:
246 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_MAIN_MENU_ID, nMode );
247
248 break;
249 }
250
251 if ( nPart == MENU_INIT )
252 {
253 switch( nMode )
254 {
255 case MODE_NORMAL:
256 case MODE_ADMIN:
257 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_MENU_INIT_ID, MODE_NORMAL );
258
259 break;
260
261 default:
262 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_MENU_INIT_ID, nMode );
263
264 break;
265 }
266 }
267 return xslSource;
268 }
269
270
271
272
273
274
275
276
277
278
279
280
281 private String getKey( int nMode, int nPart, HttpServletRequest request )
282 {
283 String strRoles = "-";
284
285 if ( SecurityService.isAuthenticationEnable( ) && request != null )
286 {
287 LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
288
289 if ( ( user != null ) && ( user.getRoles( ) != null ) )
290 {
291 String [ ] roles = user.getRoles( );
292 Arrays.sort( roles );
293 strRoles = StringUtils.join( roles, ',' );
294 }
295 }
296
297 StringBuilder sbKey = new StringBuilder( );
298 sbKey.append( "[menu:" ).append( nPart ).append( "][m:" ).append( nMode ).append( "][roles:" ).append( strRoles ).append( ']' );
299
300 return sbKey.toString( );
301 }
302
303 @Override
304 public void processPageEvent( PageEvent event )
305 {
306
307 resetCache( );
308 }
309 }