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.document.modules.geoloc.service;
35
36 import fr.paris.lutece.plugins.document.business.Document;
37 import fr.paris.lutece.plugins.document.business.DocumentHome;
38 import fr.paris.lutece.plugins.document.business.DocumentType;
39 import fr.paris.lutece.plugins.document.business.DocumentTypeHome;
40 import fr.paris.lutece.plugins.document.business.attributes.DocumentAttribute;
41 import fr.paris.lutece.plugins.document.business.attributes.DocumentAttributeHome;
42 import fr.paris.lutece.plugins.document.business.portlet.DocumentListPortletHome;
43 import fr.paris.lutece.plugins.document.service.publishing.PublishingService;
44 import fr.paris.lutece.plugins.leaflet.rest.service.IPopupContentProvider;
45 import fr.paris.lutece.portal.business.XmlContent;
46 import fr.paris.lutece.portal.business.page.Page;
47 import fr.paris.lutece.portal.business.page.PageHome;
48 import fr.paris.lutece.portal.business.portlet.Portlet;
49 import fr.paris.lutece.portal.business.style.ModeHome;
50 import fr.paris.lutece.portal.business.style.StyleHome;
51 import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
52 import fr.paris.lutece.portal.business.stylesheet.StyleSheetHome;
53 import fr.paris.lutece.portal.service.html.XmlTransformerService;
54 import fr.paris.lutece.portal.service.portal.PortalMenuService;
55 import fr.paris.lutece.portal.service.security.SecurityService;
56 import fr.paris.lutece.portal.service.util.AppLogService;
57 import fr.paris.lutece.portal.service.util.AppPathService;
58 import fr.paris.lutece.portal.web.constants.Parameters;
59 import fr.paris.lutece.portal.web.l10n.LocaleService;
60 import fr.paris.lutece.util.xml.XmlUtil;
61
62 import java.util.Collection;
63 import java.util.HashMap;
64 import java.util.Iterator;
65 import java.util.Map;
66 import java.util.Map.Entry;
67
68 import javax.servlet.http.HttpServletRequest;
69
70 public class DocumentPopupContentProvider implements IPopupContentProvider
71 {
72
73 private static final String ATTR_PARAMETER_STYLE = "style";
74
75
76 private static final String DOCUMENT_STYLE_PREFIX_ID = "document-popup-";
77
78
79 private static final String TAG_DOCUMENT_PORTLET = "document-portlet";
80 private static final String VALUE_TRUE = "1";
81 private static final String VALUE_FALSE = "0";
82
83
84 private static final String MARKER_IS_USER_AUTHENTICATED = "is-user-authenticated";
85 private static final String PARAMETER_SITE_PATH = "site-path";
86 private static final String PARAMETER_USER_SELECTED_LOCALE = "user-selected-language";
87 private static final String PARAMETER_PLUGIN_NAME = "plugin-name";
88 private static final String PARAMETER_PORTLET = "portlet";
89
90 public String getPopup( HttpServletRequest request, String strIdDocument, String strCode )
91 {
92 int nDocId;
93
94 try
95 {
96 nDocId = Integer.parseInt( strIdDocument );
97 }
98 catch( NumberFormatException nfe )
99 {
100 AppLogService.error( "Document popup rest API: invalid docId: " + strIdDocument + " exeception " + nfe );
101
102 return null;
103 }
104
105 Document document = DocumentHome.findByPrimaryKeyWithoutBinaries( nDocId );
106
107 if ( ( document == null ) || ( !document.isValid( ) ) )
108 {
109 AppLogService.error( "Document popup rest API: invalid document " + strIdDocument );
110
111 return null;
112 }
113
114
115 Collection<Portlet> portlets = PublishingService.getInstance( ).getPortletsByDocumentId( strIdDocument );
116
117 if ( portlets.size( ) == 0 )
118 {
119 AppLogService.error( "Document popup rest API: no portlets for doc " + strIdDocument );
120
121 return null;
122 }
123
124 Iterator<Portlet> iterator = portlets.iterator( );
125
126
127 Portlet portlet = null;
128
129 do
130 {
131 Portlet p = iterator.next( );
132
133 if ( p.getStatus( ) == Portlet.STATUS_UNPUBLISHED )
134 {
135 AppLogService.debug( "Document popup rest API: refuse unpublished portlet for " + strIdDocument + ", portlet " + p.getId( ) );
136
137 continue;
138 }
139
140 if ( SecurityService.isAuthenticationEnable( ) )
141 {
142 String strRolePortlet = p.getRole( );
143
144 if ( !strRolePortlet.equals( Page.ROLE_NONE ) )
145 {
146 if ( !SecurityService.getInstance( ).isUserInRole( request, strRolePortlet ) )
147 {
148 AppLogService.debug(
149 "Document popup rest API: refuse portlet role for " + strIdDocument + ", portlet " + p.getId( ) + ", role " + strRolePortlet );
150
151 continue;
152 }
153 }
154
155 String strRolePage = PageHome.getPage( p.getPageId( ) ).getRole( );
156
157 if ( !strRolePage.equals( Page.ROLE_NONE ) )
158 {
159 if ( !SecurityService.getInstance( ).isUserInRole( request, strRolePage ) )
160 {
161 AppLogService.debug(
162 "Document popup rest API: refuse page role for " + strIdDocument + ", portlet " + p.getId( ) + ", role " + strRolePage );
163
164 continue;
165 }
166 }
167 }
168
169 portlet = p;
170 }
171 while ( ( portlet == null ) && iterator.hasNext( ) );
172
173 if ( portlet == null )
174 {
175 AppLogService.error( "Document popup rest API: no matching DocumentList portlets for doc " + strIdDocument );
176
177 return null;
178 }
179
180 DocumentType type = DocumentTypeHome.findByPrimaryKey( document.getCodeDocumentType( ) );
181 DocumentAttribute attribute = null;
182
183 for ( DocumentAttribute _attribute : type.getAttributes( ) )
184 {
185 if ( _attribute.getCode( ).equals( strCode ) )
186 {
187 attribute = _attribute;
188
189 break;
190 }
191 }
192
193 if ( attribute == null )
194 {
195 AppLogService.error( "Document popup rest API: no attribute " + strCode + " for doc " + strIdDocument + " of type " + type.getCode( ) );
196
197 return null;
198 }
199
200 String strStyleId = DocumentAttributeHome.getAttributeParameterValues( attribute.getId( ), ATTR_PARAMETER_STYLE ).get( 0 );
201 Collection<StyleSheet> listStyleSheet = StyleHome.getStyleSheetList( Integer.parseInt( strStyleId ) );
202
203 if ( listStyleSheet.isEmpty( ) )
204 {
205 AppLogService.error( "Document popup rest API: no stylesheet for style " + strStyleId );
206
207 return null;
208 }
209
210 StyleSheet stylesheet = StyleSheetHome.findByPrimaryKey( listStyleSheet.iterator( ).next( ).getId( ) );
211
212
213 StringBuffer strXml = new StringBuffer( );
214 XmlUtil.beginElement( strXml, XmlContent.TAG_PORTLET );
215 XmlUtil.addElementHtml( strXml, XmlContent.TAG_PORTLET_NAME, portlet.getName( ) );
216 XmlUtil.addElement( strXml, XmlContent.TAG_PORTLET_ID, portlet.getId( ) );
217 XmlUtil.addElement( strXml, XmlContent.TAG_PAGE_ID, portlet.getPageId( ) );
218 XmlUtil.addElement( strXml, XmlContent.TAG_PLUGIN_NAME, portlet.getPluginName( ) );
219 XmlUtil.addElement( strXml, XmlContent.TAG_DISPLAY_PORTLET_TITLE, portlet.getDisplayPortletTitle( ) );
220
221 String strDisplayOnSmallDevice = ( ( portlet.getDeviceDisplayFlags( ) & Portlet.FLAG_DISPLAY_ON_SMALL_DEVICE ) != 0 ) ? VALUE_TRUE : VALUE_FALSE;
222 XmlUtil.addElement( strXml, XmlContent.TAG_DISPLAY_ON_SMALL_DEVICE, strDisplayOnSmallDevice );
223
224 String strDisplayOnNormalDevice = ( ( portlet.getDeviceDisplayFlags( ) & Portlet.FLAG_DISPLAY_ON_NORMAL_DEVICE ) != 0 ) ? VALUE_TRUE : VALUE_FALSE;
225 XmlUtil.addElement( strXml, XmlContent.TAG_DISPLAY_ON_NORMAL_DEVICE, strDisplayOnNormalDevice );
226
227 String strDisplayOnLargeDevice = ( ( portlet.getDeviceDisplayFlags( ) & Portlet.FLAG_DISPLAY_ON_LARGE_DEVICE ) != 0 ) ? VALUE_TRUE : VALUE_FALSE;
228 XmlUtil.addElement( strXml, XmlContent.TAG_DISPLAY_ON_LARGE_DEVICE, strDisplayOnLargeDevice );
229
230 String strDisplayOnXLargeDevice = ( ( portlet.getDeviceDisplayFlags( ) & Portlet.FLAG_DISPLAY_ON_XLARGE_DEVICE ) != 0 ) ? VALUE_TRUE : VALUE_FALSE;
231 XmlUtil.addElement( strXml, XmlContent.TAG_DISPLAY_ON_XLARGE_DEVICE, strDisplayOnXLargeDevice );
232
233 XmlUtil.beginElement( strXml, TAG_DOCUMENT_PORTLET );
234 strXml.append( document.getXml( request, portlet.getId( ) ) );
235 XmlUtil.endElement( strXml, TAG_DOCUMENT_PORTLET );
236 XmlUtil.endElement( strXml, XmlContent.TAG_PORTLET );
237
238 XmlTransformerService xmlTransformerService = new XmlTransformerService( );
239 String strXslUniquePrefix = DOCUMENT_STYLE_PREFIX_ID + stylesheet.getId( );
240
241
242 Map<String, String> mapModifyParam = new HashMap<String, String>( );
243 mapModifyParam.put( PARAMETER_USER_SELECTED_LOCALE, LocaleService.getUserSelectedLocale( request ).getLanguage( ) );
244
245 mapModifyParam.put( PARAMETER_SITE_PATH, AppPathService.getPortalUrl( ) );
246
247 if ( SecurityService.isAuthenticationEnable( ) )
248 {
249 mapModifyParam.put( MARKER_IS_USER_AUTHENTICATED,
250 ( SecurityService.getInstance( ).getRegisteredUser( request ) != null ) ? VALUE_TRUE : VALUE_FALSE );
251 }
252
253 mapModifyParam.put( Parameters.PAGE_ID, Integer.toString( portlet.getPageId( ) ) );
254
255 Map<String, String> mapXslParams = portlet.getXslParams( );
256 Map<String, String> mapParams = mapModifyParam;
257
258 if ( mapXslParams != null )
259 {
260 for ( Entry<String, String> entry : mapXslParams.entrySet( ) )
261 {
262 mapParams.put( entry.getKey( ), entry.getValue( ) );
263 }
264 }
265
266 String result = xmlTransformerService.transformBySourceWithXslCache( strXml.toString( ), stylesheet.getSource( ), strXslUniquePrefix, mapParams,
267 ModeHome.getOuputXslProperties( PortalMenuService.MODE_NORMAL ) );
268
269 return result;
270 }
271 }