View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.xmlpage.business.portlet;
35  
36  import fr.paris.lutece.plugins.xmlpage.service.XmlPageElement;
37  import fr.paris.lutece.plugins.xmlpage.service.XmlPageService;
38  import fr.paris.lutece.plugins.xmlpage.util.XmlPageContentUtils;
39  import fr.paris.lutece.portal.business.portlet.Portlet;
40  import fr.paris.lutece.portal.service.util.AppPropertiesService;
41  import fr.paris.lutece.util.xml.XmlUtil;
42  
43  import java.util.HashMap;
44  import java.util.Map;
45  
46  import javax.servlet.http.HttpServletRequest;
47  
48  
49  /**
50   * This class represents business objects RssPortlet
51   */
52  public class XmlPagePortlet extends Portlet
53  {
54      //properties
55      private static final String PROPERTY_TRANSFORM_CACHE = "xmlpage.transform.cache";
56      private static final String PROPERTY_PARAMETERS_LIST = "xmlpage.parameters.list";
57      private static final String PROPERTY_PARAMETERS_LIST_SEPARATOR = "xmlpage.parameters.list.separator";
58  
59      //Utils
60      private static final String TAG_PORTLET_XMLPAGE = "xmlpage-portlet";
61      private static final String TAG_PORTLET_XMLPAGE_CONTENT = "xmlpage-portlet-content";
62      private static final String EMPTY_STRING = "";
63      private String _strPageName;
64      private String _strStyle;
65  
66      /**
67       * Sets the identifier of the portlet type to value specified
68       */
69      public XmlPagePortlet(  )
70      {
71          setPortletTypeId( XmlPagePortletHome.getInstance(  ).getPortletTypeId(  ) );
72      }
73  
74      /**
75       * Sets the page's name of the portlet
76       *
77       * @param strPageName the XmlPage name portlet
78       */
79      public void setPageName( String strPageName )
80      {
81          _strPageName = strPageName;
82      }
83  
84      /**
85       * Returns the page's name of the XmlPage portlet
86       *
87       * @return the page's name
88       */
89      public String getPageName(  )
90      {
91          return _strPageName;
92      }
93  
94      /**
95       * Sets the style of the portlet
96       *
97       * @param strStyle the XmlPage style portlet
98       */
99      public void setStyle( String strStyle )
100     {
101         _strStyle = strStyle;
102     }
103 
104     /**
105      * Returns the style of the XmlPage portlet
106      *
107      * @return the style
108      */
109     public String getStyle(  )
110     {
111         return _strStyle;
112     }
113 
114     /**
115      * Returns the Xml code of the portlet with XML heading
116      *
117      * @param request The HTTP servlet request
118      * @return the Xml code of the portlet
119      */
120     public String getXmlDocument( HttpServletRequest request )
121     {
122         return XmlUtil.getXmlHeader(  ) + getXml( request );
123     }
124 
125     /**
126      * Returns the Xml code of the portlet without XML heading
127      *
128      * @param request The HTTP servlet request
129      * @return the Xml code of the portlet content
130      */
131     public String getXml( HttpServletRequest request )
132     {
133         StringBuffer strXml = new StringBuffer(  );
134 
135         String strTransformCache = AppPropertiesService.getProperty( PROPERTY_TRANSFORM_CACHE );
136         boolean bTransformCacheActive = Boolean.parseBoolean( strTransformCache );
137         Map<String, String> mapParameters = null;
138 
139         if ( !bTransformCacheActive )
140         {
141             String strParametersList = AppPropertiesService.getProperty( PROPERTY_PARAMETERS_LIST );
142             String strParametersListSeparator = AppPropertiesService.getProperty( PROPERTY_PARAMETERS_LIST_SEPARATOR );
143             String[] listParameters = strParametersList.split( strParametersListSeparator );
144 
145             mapParameters = new HashMap<String, String>(  );
146 
147             if ( request != null )
148             {
149                 for ( String currentParameter : listParameters )
150                 {
151                     if ( request.getParameter( currentParameter ) != null )
152                     {
153                         mapParameters.put( currentParameter, request.getParameter( currentParameter ) );
154                     }
155                 }
156             }
157         }
158 
159         XmlUtil.beginElement( strXml, TAG_PORTLET_XMLPAGE );
160 
161         XmlPageElement xmlPageElement = XmlPageService.getInstance(  ).getXmlPageResource( getPageName(  ) );
162 
163         String strContent = EMPTY_STRING;
164 
165         if ( xmlPageElement != null )
166         {
167             //TODO add boolean active transform hashMap de parameters for portlet ?
168             strContent = XmlPageContentUtils.getContent( xmlPageElement, getStyle(  ), bTransformCacheActive,
169                     mapParameters );
170         }
171 
172         XmlUtil.addElementHtml( strXml, TAG_PORTLET_XMLPAGE_CONTENT, strContent );
173         XmlUtil.endElement( strXml, TAG_PORTLET_XMLPAGE );
174 
175         return addPortletTags( strXml );
176     }
177 
178     /**
179      * Updates the current instance of the Portlet object
180      */
181     public void update(  )
182     {
183         XmlPagePortletHome.getInstance(  ).update( this );
184     }
185 
186     /**
187      * Removes the current instance of the Portlet object
188      */
189     public void remove(  )
190     {
191         XmlPagePortletHome.getInstance(  ).remove( this );
192     }
193 }