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.childpages.business.portlet;
35  
36  import fr.paris.lutece.portal.business.page.Page;
37  import fr.paris.lutece.portal.business.page.PageHome;
38  import fr.paris.lutece.portal.business.portlet.Portlet;
39  import fr.paris.lutece.portal.web.admin.AdminPageJspBean;
40  import fr.paris.lutece.util.xml.XmlUtil;
41  
42  import java.util.Collection;
43  
44  import javax.servlet.http.HttpServletRequest;
45  
46  
47  /**
48   * This class represents business objects ChildPagesPortlet
49   */
50  public class ChildPagesPortlet extends Portlet
51  {
52      /////////////////////////////////////////////////////////////////////////////////
53      // Xml Tags
54      public static final String TAG_CHILD_PAGE = "child-page";
55      public static final String TAG_CHILD_PAGE_ID = "child-page-id";
56      public static final String TAG_CHILD_PAGE_NAME = "child-page-name";
57      public static final String TAG_CHILD_PAGE_DESCRIPTION = "child-page-description";
58      public static final String TAG_CHILD_PAGE_IMAGE = "child-page-image";
59      public static final String TAG_CHILD_PAGES_PORTLET_LIST = "child-pages-portlet";
60  
61      /////////////////////////////////////////////////////////////////////////////////
62      // Constants
63      private int _nParentPageId;
64  
65      /**
66       * Sets the identifier of the portlet type to the value specified in the ChildPagesPortletHome class
67       */
68      public ChildPagesPortlet(  )
69      {
70          setPortletTypeId( ChildPagesPortletHome.getInstance(  ).getPortletTypeId(  ) );
71      }
72  
73      /**
74       * Sets the parent page identifier of the portlet to the value specified in parameter
75       *
76       * @param nParentPageId new parent page identifier
77       */
78      public void setParentPageId( int nParentPageId )
79      {
80          _nParentPageId = nParentPageId;
81      }
82  
83      /**
84       * Returns the identifier of the parent page of the portlet
85       *
86       * @return the parent page identifier
87       */
88      public int getParentPageId(  )
89      {
90          return _nParentPageId;
91      }
92  
93      /**
94       * Returns the Xml code of the Child pages portlet without XML heading
95       *
96       * @param request The HTTP Servlet Request
97       * @return the Xml code of the child pages portlet content
98       */
99      public String getXml( HttpServletRequest request )
100     {
101         StringBuffer strXml = new StringBuffer(  );
102         XmlUtil.beginElement( strXml, TAG_CHILD_PAGES_PORTLET_LIST );
103 
104         Collection<Page> pages;
105         if ( getParentPageId(  ) == 0 )
106         {
107             pages = PageHome.getChildPages( getPageId(  ) );
108         } else
109         {
110             pages = PageHome.getChildPages( getParentPageId(  ) );
111         }
112 
113         AdminPageJspBean adminPage = new AdminPageJspBean(  );
114         for ( Page page : pages )
115         {
116             if ( request != null )
117             {
118 	            if ( page.isVisible( request ) )
119 	            {
120 	                XmlUtil.beginElement( strXml, TAG_CHILD_PAGE );
121 	                XmlUtil.addElement( strXml, TAG_CHILD_PAGE_ID, page.getId(  ) );
122 	                XmlUtil.addElement( strXml, TAG_CHILD_PAGE_NAME, page.getName(  ) );
123 	                XmlUtil.addElement( strXml, TAG_CHILD_PAGE_DESCRIPTION, page.getDescription(  ) );
124 
125 	                if ( page.getImageContent(  ) != null )
126 	                {
127 	                    int nImageLength = page.getImageContent(  ).length;
128 	
129 	                    if ( nImageLength >= 1 )
130 	                    {
131 	                        String strPageId = new Integer( page.getId(  ) ).toString(  );
132 	                        XmlUtil.addElement( strXml, TAG_CHILD_PAGE_IMAGE,
133 	                            adminPage.getResourceImagePage( page, strPageId ) );
134 	                    }
135 	                }
136 	
137 	                XmlUtil.endElement( strXml, TAG_CHILD_PAGE );
138 	            }
139             }
140         }
141 
142         XmlUtil.endElement( strXml, TAG_CHILD_PAGES_PORTLET_LIST );
143 
144         return addPortletTags( strXml );
145     }
146 
147     /**
148      * Returns the Xml code of the ChildPage portlet with XML heading
149      *
150      * @param request The HTTP Servlet Request
151      * @return the Xml code of the ChildPage portlet
152      */
153     public String getXmlDocument( HttpServletRequest request )
154     {
155         return XmlUtil.getXmlHeader(  ) + getXml( request );
156     }
157 
158     /**
159      * Updates the current instance of the ChildPage Portlet object
160      */
161     public void update(  )
162     {
163         ChildPagesPortletHome.getInstance(  ).update( this );
164     }
165 
166     /**
167      * Removes the current instance of the ChildPage Portlet object
168      */
169     public void remove(  )
170     {
171         ChildPagesPortletHome.getInstance(  ).remove( this );
172     }
173 }