View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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  
71  public class DocumentPopupContentProvider implements IPopupContentProvider
72  {
73      //Attribute parameter pointing to a style
74      private static final String ATTR_PARAMETER_STYLE = "style";
75  
76      //Xsl cache key
77      private static final String DOCUMENT_STYLE_PREFIX_ID = "document-popup-";
78  
79      //To mimic a DocumentPortlet
80      private static final String TAG_DOCUMENT_PORTLET = "document-portlet";
81      private static final String VALUE_TRUE = "1";
82      private static final String VALUE_FALSE = "0";
83  
84      // Parameters, copy from PageService.java
85      private static final String MARKER_IS_USER_AUTHENTICATED = "is-user-authenticated";
86      private static final String PARAMETER_SITE_PATH = "site-path";
87      private static final String PARAMETER_USER_SELECTED_LOCALE = "user-selected-language";
88      private static final String PARAMETER_PLUGIN_NAME = "plugin-name";
89      private static final String PARAMETER_PORTLET = "portlet";
90  
91      public String getPopup( HttpServletRequest request, String strIdDocument, String strCode )
92      {
93          int nDocId;
94  
95          try
96          {
97              nDocId = Integer.parseInt( strIdDocument );
98          }
99          catch ( NumberFormatException nfe )
100         {
101             AppLogService.error( "Document popup rest API: invalid docId: " + strIdDocument + " exeception " + nfe );
102 
103             return null;
104         }
105 
106         Document document = DocumentHome.findByPrimaryKeyWithoutBinaries( nDocId );
107 
108         if ( ( document == null ) || ( !document.isValid(  ) ) )
109         {
110             AppLogService.error( "Document popup rest API: invalid document " + strIdDocument );
111 
112             return null;
113         }
114 
115         //Find first portlet of type DocumentList
116         Collection<Portlet> portlets = PublishingService.getInstance(  ).getPortletsByDocumentId( strIdDocument );
117 
118         if ( portlets.size(  ) == 0 )
119         {
120             AppLogService.error( "Document popup rest API: no portlets for doc " + strIdDocument );
121 
122             return null;
123         }
124 
125         Iterator<Portlet> iterator = portlets.iterator(  );
126 
127         //Find first portlet with correct settings
128         Portlet portlet = null;
129 
130         do
131         {
132             Portlet p = iterator.next(  );
133 
134             if ( p.getStatus(  ) == Portlet.STATUS_UNPUBLISHED )
135             {
136                 AppLogService.debug( "Document popup rest API: refuse unpublished portlet for " + strIdDocument +
137                     ", portlet " + p.getId(  ) );
138 
139                 continue;
140             }
141 
142             if ( SecurityService.isAuthenticationEnable(  ) )
143             {
144                 String strRolePortlet = p.getRole(  );
145 
146                 if ( !strRolePortlet.equals( Page.ROLE_NONE ) )
147                 {
148                     if ( !SecurityService.getInstance(  ).isUserInRole( request, strRolePortlet ) )
149                     {
150                         AppLogService.debug( "Document popup rest API: refuse portlet role for " + strIdDocument +
151                             ", portlet " + p.getId(  ) + ", role " + strRolePortlet );
152 
153                         continue;
154                     }
155                 }
156 
157                 String strRolePage = PageHome.getPage( p.getPageId(  ) ).getRole(  );
158 
159                 if ( !strRolePage.equals( Page.ROLE_NONE ) )
160                 {
161                     if ( !SecurityService.getInstance(  ).isUserInRole( request, strRolePage ) )
162                     {
163                         AppLogService.debug( "Document popup rest API: refuse page role for " + strIdDocument +
164                             ", portlet " + p.getId(  ) + ", role " + strRolePage );
165 
166                         continue;
167                     }
168                 }
169             }
170 
171             portlet = p;
172         }
173         while ( ( portlet == null ) && iterator.hasNext(  ) );
174 
175         if ( portlet == null )
176         {
177             AppLogService.error( "Document popup rest API: no matching DocumentList portlets for doc " + strIdDocument );
178 
179             return null;
180         }
181 
182         DocumentType type = DocumentTypeHome.findByPrimaryKey( document.getCodeDocumentType(  ) );
183         DocumentAttribute attribute = null;
184 
185         for ( DocumentAttribute _attribute : type.getAttributes(  ) )
186         {
187             if ( _attribute.getCode(  ).equals( strCode ) )
188             {
189                 attribute = _attribute;
190 
191                 break;
192             }
193         }
194 
195         if ( attribute == null )
196         {
197             AppLogService.error( "Document popup rest API: no attribute " + strCode + " for doc " + strIdDocument +
198                 " of type " + type.getCode(  ) );
199 
200             return null;
201         }
202 
203         String strStyleId = DocumentAttributeHome.getAttributeParameterValues( attribute.getId(  ), ATTR_PARAMETER_STYLE )
204                                                  .get( 0 );
205         Collection<StyleSheet> listStyleSheet = StyleHome.getStyleSheetList( Integer.parseInt( strStyleId ) );
206 
207         if ( listStyleSheet.isEmpty(  ) )
208         {
209             AppLogService.error( "Document popup rest API: no stylesheet for style " + strStyleId );
210 
211             return null;
212         }
213 
214         StyleSheet stylesheet = StyleSheetHome.findByPrimaryKey( listStyleSheet.iterator(  ).next(  ).getId(  ) );
215 
216         //Copy from Portlet addPortletTags
217         StringBuffer strXml = new StringBuffer(  );
218         XmlUtil.beginElement( strXml, XmlContent.TAG_PORTLET );
219         XmlUtil.addElementHtml( strXml, XmlContent.TAG_PORTLET_NAME, portlet.getName(  ) );
220         XmlUtil.addElement( strXml, XmlContent.TAG_PORTLET_ID, portlet.getId(  ) );
221         XmlUtil.addElement( strXml, XmlContent.TAG_PAGE_ID, portlet.getPageId(  ) );
222         XmlUtil.addElement( strXml, XmlContent.TAG_PLUGIN_NAME, portlet.getPluginName(  ) );
223         XmlUtil.addElement( strXml, XmlContent.TAG_DISPLAY_PORTLET_TITLE, portlet.getDisplayPortletTitle(  ) );
224 
225         String strDisplayOnSmallDevice = ( ( portlet.getDeviceDisplayFlags(  ) & Portlet.FLAG_DISPLAY_ON_SMALL_DEVICE ) != 0 )
226             ? VALUE_TRUE : VALUE_FALSE;
227         XmlUtil.addElement( strXml, XmlContent.TAG_DISPLAY_ON_SMALL_DEVICE, strDisplayOnSmallDevice );
228 
229         String strDisplayOnNormalDevice = ( ( portlet.getDeviceDisplayFlags(  ) &
230             Portlet.FLAG_DISPLAY_ON_NORMAL_DEVICE ) != 0 ) ? VALUE_TRUE : VALUE_FALSE;
231         XmlUtil.addElement( strXml, XmlContent.TAG_DISPLAY_ON_NORMAL_DEVICE, strDisplayOnNormalDevice );
232 
233         String strDisplayOnLargeDevice = ( ( portlet.getDeviceDisplayFlags(  ) & Portlet.FLAG_DISPLAY_ON_LARGE_DEVICE ) != 0 )
234             ? VALUE_TRUE : VALUE_FALSE;
235         XmlUtil.addElement( strXml, XmlContent.TAG_DISPLAY_ON_LARGE_DEVICE, strDisplayOnLargeDevice );
236 
237         String strDisplayOnXLargeDevice = ( ( portlet.getDeviceDisplayFlags(  ) &
238             Portlet.FLAG_DISPLAY_ON_XLARGE_DEVICE ) != 0 ) ? VALUE_TRUE : VALUE_FALSE;
239         XmlUtil.addElement( strXml, XmlContent.TAG_DISPLAY_ON_XLARGE_DEVICE, strDisplayOnXLargeDevice );
240 
241         XmlUtil.beginElement( strXml, TAG_DOCUMENT_PORTLET );
242         strXml.append( document.getXml( request, portlet.getId(  ) ) );
243         XmlUtil.endElement( strXml, TAG_DOCUMENT_PORTLET );
244         XmlUtil.endElement( strXml, XmlContent.TAG_PORTLET );
245 
246         XmlTransformerService xmlTransformerService = new XmlTransformerService(  );
247         String strXslUniquePrefix = DOCUMENT_STYLE_PREFIX_ID + stylesheet.getId(  );
248 
249         //Copy from PageService getParams
250         Map<String, String> mapModifyParam = new HashMap<String, String>(  );
251         mapModifyParam.put( PARAMETER_USER_SELECTED_LOCALE,
252             LocaleService.getUserSelectedLocale( request ).getLanguage(  ) );
253 
254         mapModifyParam.put( PARAMETER_SITE_PATH, AppPathService.getPortalUrl(  ) );
255 
256         if ( SecurityService.isAuthenticationEnable(  ) )
257         {
258             mapModifyParam.put( MARKER_IS_USER_AUTHENTICATED,
259                 ( SecurityService.getInstance(  ).getRegisteredUser( request ) != null ) ? VALUE_TRUE : VALUE_FALSE );
260         }
261 
262         mapModifyParam.put( Parameters.PAGE_ID, Integer.toString( portlet.getPageId(  ) ) );
263 
264         Map<String, String> mapXslParams = portlet.getXslParams(  );
265         Map<String, String> mapParams = mapModifyParam;
266 
267         if ( mapXslParams != null )
268         {
269             for ( Entry<String, String> entry : mapXslParams.entrySet(  ) )
270             {
271                 mapParams.put( entry.getKey(  ), entry.getValue(  ) );
272             }
273         }
274 
275         String result = xmlTransformerService.transformBySourceWithXslCache( strXml.toString(  ),
276                 stylesheet.getSource(  ), strXslUniquePrefix, mapParams,
277                 ModeHome.getOuputXslProperties( PortalMenuService.MODE_NORMAL ) );
278 
279         return result;
280     }
281 }