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.lang.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
65 public final class PortalMenuService extends AbstractCacheableService implements PageEventListener
66 {
67 public static final int MENU_INIT = 0;
68 public static final int MENU_MAIN = 1;
69 public static final int MODE_NORMAL = 0;
70 public static final int MODE_ADMIN = 1;
71 private static final int PORTAL_COMPONENT_MENU_INIT_ID = 3;
72 private static final int PORTAL_COMPONENT_MAIN_MENU_ID = 4;
73 private static final String SERVICE_NAME = "PortalMenuService";
74
75
76 private static PortalMenuService _singleton;
77
78
79 private PortalMenuService( )
80 {
81 initCache( getName( ) );
82 PageService.addPageEventListener( this );
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 public String getName( )
104 {
105 return SERVICE_NAME;
106 }
107
108
109
110
111
112
113
114
115
116 public String getMenuContent( int nCurrentPageId, int nMode, int nPart, HttpServletRequest request )
117 {
118 String strKey = getKey( nMode, nPart, request );
119 String strMenu = (String) getFromCache( strKey );
120
121
122 if ( strMenu == null )
123 {
124
125 strMenu = buildMenuContent( nCurrentPageId, nMode, nPart, request );
126
127
128 putInCache( strKey, strMenu );
129
130 return strMenu;
131 }
132
133
134 return strMenu;
135 }
136
137
138
139
140
141
142
143
144
145
146 private String buildMenuContent( int nCurrentPageId, int nMode, int nPart, HttpServletRequest request )
147 {
148 StringBuffer strXml = new StringBuffer( );
149 Collection<Page> listPagesMenu = PageHome.getChildPagesMinimalData( PortalService.getRootPageId( ) );
150
151 String strCurrentPageId = Integer.toString( nCurrentPageId );
152
153 strXml.append( XmlUtil.getXmlHeader( ) );
154 XmlUtil.beginElement( strXml, XmlContent.TAG_MENU_LIST );
155
156 int nMenuIndex = 1;
157
158 for ( Page menuPage : listPagesMenu )
159 {
160 if ( ( menuPage.isVisible( request ) ) || ( nMode == MODE_ADMIN ) )
161 {
162
163 XmlUtil.beginElement( strXml, XmlContent.TAG_MENU );
164 XmlUtil.addElement( strXml, XmlContent.TAG_MENU_INDEX, nMenuIndex );
165 XmlUtil.addElement( strXml, XmlContent.TAG_PAGE_ID, menuPage.getId( ) );
166 XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_NAME, menuPage.getName( ) );
167 XmlUtil.addElementHtml( strXml, XmlContent.TAG_CURRENT_PAGE_ID, strCurrentPageId );
168
169 Collection<Page> listSubLevelMenuPages = PageHome.getChildPagesMinimalData( menuPage.getId( ) );
170
171
172 if ( !listSubLevelMenuPages.isEmpty( ) )
173 {
174
175 XmlUtil.beginElement( strXml, XmlContent.TAG_SUBLEVEL_MENU_LIST );
176
177 int nSubLevelMenuIndex = 1;
178
179 for ( Page subLevelMenuPage : listSubLevelMenuPages )
180 {
181 if ( ( subLevelMenuPage.isVisible( request ) ) || ( nMode == MODE_ADMIN ) )
182 {
183 XmlUtil.beginElement( strXml, XmlContent.TAG_SUBLEVEL_MENU );
184 XmlUtil.addElement( strXml, XmlContent.TAG_MENU_INDEX, nMenuIndex );
185 XmlUtil.addElement( strXml, XmlContent.TAG_SUBLEVEL_INDEX, nSubLevelMenuIndex );
186 XmlUtil.addElement( strXml, XmlContent.TAG_PAGE_ID, subLevelMenuPage.getId( ) );
187 XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_NAME, subLevelMenuPage.getName( ) );
188 XmlUtil.endElement( strXml, XmlContent.TAG_SUBLEVEL_MENU );
189 XmlUtil.addElementHtml( strXml, XmlContent.TAG_CURRENT_PAGE_ID, strCurrentPageId );
190 }
191 }
192
193 XmlUtil.endElement( strXml, XmlContent.TAG_SUBLEVEL_MENU_LIST );
194 }
195
196 XmlUtil.endElement( strXml, XmlContent.TAG_MENU );
197 nMenuIndex++;
198 }
199 }
200
201 XmlUtil.endElement( strXml, XmlContent.TAG_MENU_LIST );
202
203
204
205 StyleSheet xslSource;
206
207
208 switch ( nMode )
209 {
210 case MODE_NORMAL:
211 case MODE_ADMIN:
212 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_MAIN_MENU_ID, MODE_NORMAL );
213
214 break;
215
216 default:
217 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_MAIN_MENU_ID, nMode );
218
219 break;
220 }
221
222 if ( nPart == MENU_INIT )
223 {
224 switch ( nMode )
225 {
226 case MODE_NORMAL:
227 case MODE_ADMIN:
228 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_MENU_INIT_ID, MODE_NORMAL );
229
230 break;
231
232 default:
233 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_MENU_INIT_ID, nMode );
234
235 break;
236 }
237 }
238
239 Properties outputProperties = ModeHome.getOuputXslProperties( nMode );
240
241
242
243 Map<String, String> mapParamRequest = new HashMap<String, String>( );
244 PortalService.setXslPortalPath( mapParamRequest, nMode );
245
246 XmlTransformerService xmlTransformerService = new XmlTransformerService( );
247
248 return xmlTransformerService.transformBySourceWithXslCache( strXml.toString( ), xslSource, mapParamRequest,
249 outputProperties );
250 }
251
252
253
254
255
256
257
258
259
260 private String getKey( int nMode, int nPart, HttpServletRequest request )
261 {
262 String strRoles = "-";
263
264 if ( SecurityService.isAuthenticationEnable( ) )
265 {
266 if ( request != null )
267 {
268 LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
269
270 if ( ( user != null ) && ( user.getRoles( ) != null ) )
271 {
272 String[] roles = user.getRoles( );
273 Arrays.sort( roles );
274 strRoles = StringUtils.join( roles, ',' );
275 }
276 }
277 }
278
279 StringBuilder sbKey = new StringBuilder( );
280 sbKey.append( "[menu:" ).append( nPart ).append( "][m:" ).append( nMode ).append( "][roles:" ).append( strRoles )
281 .append( ']' );
282
283 return sbKey.toString( );
284 }
285
286 @Override
287 public void processPageEvent( PageEvent event )
288 {
289
290 resetCache( );
291 }
292 }