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.dbpage.business.portlet;
35  
36  import fr.paris.lutece.plugins.dbpage.business.DbPage;
37  import fr.paris.lutece.plugins.dbpage.service.DbPageService;
38  import fr.paris.lutece.portal.business.portlet.Portlet;
39  import fr.paris.lutece.util.xml.XmlUtil;
40  
41  import java.util.ArrayList;
42  import java.util.List;
43  import java.util.StringTokenizer;
44  
45  import javax.servlet.http.HttpServletRequest;
46  
47  
48  /**
49   * This class represents business objects DbPagePortlet
50   */
51  public class DbPagePortlet extends Portlet
52  {
53      private static final String TAG_PORTLET_DBPAGE = "dbpage-portlet";
54      private static final String TAG_PORTLET_DBPAGE_CONTENT = "dbpage-portlet-content";
55      private static final String VALUES_DELIMITER = ";";
56      private String _strDbPageName;
57      private String _strValues;
58  
59      /**
60       * Creates a new DbPagePortlet object.
61       */
62      public DbPagePortlet(  )
63      {
64          setPortletTypeId( DbPagePortletHome.getInstance(  ).getPortletTypeId(  ) );
65      }
66  
67      /**
68       * Sets the name of the dbpage
69       *
70       * @param strDbPageName the dbpage name
71       */
72      public void setDbPageName( String strDbPageName )
73      {
74          _strDbPageName = strDbPageName;
75      }
76  
77      /**
78       * Returns the name of this dbpage
79       *
80       * @return the dbpage name
81       */
82      public String getDbPageName(  )
83      {
84          return _strDbPageName;
85      }
86  
87      /**
88       * Returns values of this dbpage
89       *
90       * @return the dbpage values
91       */
92      public String getDbValues(  )
93      {
94          return _strValues;
95      }
96  
97      /**
98       * Sets values of the dbpage
99       *
100      * @param strValues the values
101      */
102     public void setValues( String strValues )
103     {
104         _strValues = ( strValues != null ) ? strValues : "";
105     }
106 
107     /**
108      * Returns values of this dbpage
109      *
110      * @return the dbpage values
111      */
112     public List<String> getValuesList(  )
113     {
114         List<String> list = new ArrayList<String>(  );
115 
116         if ( ( _strValues != null ) && ( !_strValues.equals( "" ) ) )
117         {
118             StringTokenizer st = new StringTokenizer( _strValues, VALUES_DELIMITER );
119 
120             while ( st.hasMoreTokens(  ) )
121             {
122                 list.add( st.nextToken(  ) );
123             }
124         }
125 
126         return list;
127     }
128 
129     /**
130      * Returns the Xml code of the DbPage portlet without XML heading
131      *
132      * @param request The http request
133      * @return the Xml code of the DbPage portlet content
134      */
135     public String getXml( HttpServletRequest request )
136     {
137         StringBuffer strXml = new StringBuffer(  );
138         XmlUtil.beginElement( strXml, TAG_PORTLET_DBPAGE );
139 
140         DbPage dbpage = DbPageService.getInstance(  ).getDbPage( getDbPageName(  ) );
141         List<String> listValues = getValuesList(  );
142         String strContent = "";
143 
144         if ( ( dbpage != null ) && ( request != null ) )
145         {
146             strContent = dbpage.getContent( listValues, request );
147         }
148 
149         XmlUtil.addElementHtml( strXml, TAG_PORTLET_DBPAGE_CONTENT, strContent );
150         XmlUtil.endElement( strXml, TAG_PORTLET_DBPAGE );
151 
152         return addPortletTags( strXml );
153     }
154 
155     /**
156      * Returns the Xml code of the DbPage portlet with XML heading
157      *
158      * @param request the HttpServletRequest
159      * @return the Xml code of the DbPage portlet
160      */
161     public String getXmlDocument( HttpServletRequest request )
162     {
163         return XmlUtil.getXmlHeader(  ) + getXml( request );
164     }
165 
166     /**
167      * Remove portlet
168      */
169     public void remove(  )
170     {
171         DbPagePortletHome.getInstance(  ).remove( this );
172     }
173 
174     /**
175      * Update the portlet
176      */
177     public void update(  )
178     {
179         DbPagePortletHome.getInstance(  ).update( this );
180     }
181 }