View Javadoc
1   /*
2    * Copyright (c) 2002-2020, City of 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.jasper.web.portlet;
35  
36  import fr.paris.lutece.plugins.jasper.business.portlet.JasperPortlet;
37  import fr.paris.lutece.plugins.jasper.business.portlet.JasperPortletHome;
38  import fr.paris.lutece.portal.business.portlet.PortletHome;
39  import fr.paris.lutece.portal.service.template.AppTemplateService;
40  import fr.paris.lutece.portal.web.portlet.PortletJspBean;
41  import fr.paris.lutece.util.ReferenceList;
42  import fr.paris.lutece.util.html.HtmlTemplate;
43  
44  import java.util.*;
45  
46  import javax.servlet.http.HttpServletRequest;
47  
48  /**
49   * This class provides the user interface to manage Jasper Portlet features
50   */
51  public class JasperPortletJspBean extends PortletJspBean
52  {
53      // /////////////////////////////////////////////////////////////////////////////////
54      // Constants
55  
56      /**
57       * The rights required to use JasperPortletJspBean
58       */
59      public static final String RIGHT_MANAGE_ADMIN_SITE = "CORE_ADMIN_SITE";
60  
61      // //////////////////////////////
62      private static final String PARAMETER_PAGE_ID = "page_id";
63      private static final String PARAMETER_PORTLET_ID = "portlet_id";
64      private static final String PARAMETER_PORTLET_TYPE_ID = "portlet_type_id";
65      private static final String PARAMETER_FEED_ID = "feed_id";
66      private static final String COMBO_FEED_LIST = "@combo_feeds@";
67      private static final String MARK_FEED_LIST = "feed_list";
68      private static final String MARK_FEED_ID = "default_feed_id";
69  
70      // ////////////////////////////////////////////////////////////////////////////////
71      // Templates
72      private static final String TEMPLATE_COMBO_FEEDS = "admin/plugins/jasper/portlet/combo_feed_jasper.html";
73  
74      /**
75       * Returns the properties prefix used for jasper portlet and defined in lutece.properties file
76       *
77       * @return the value of the property prefix
78       */
79      public String getPropertiesPrefix( )
80      {
81          return "portlet.jasper";
82      }
83  
84      /**
85       * Returns the Jasper Portlet form of creation
86       *
87       * @param request
88       *            The Http rquest
89       * @return the html code of the jasper portlet form
90       */
91      public String getCreate( HttpServletRequest request )
92      {
93          String strPageId = request.getParameter( PARAMETER_PAGE_ID );
94          String strPortletTypeId = request.getParameter( PARAMETER_PORTLET_TYPE_ID );
95          HtmlTemplate template = getCreateTemplate( strPageId, strPortletTypeId );
96          ReferenceList listFeeds = getDummyList( );
97          String strHtmlCombo = getFeedIndexCombo( listFeeds, "" );
98          template.substitute( COMBO_FEED_LIST, strHtmlCombo );
99  
100         return template.getHtml( );
101     }
102 
103     /**
104      * Returns the Jasper Portlet form for update
105      * 
106      * @param request
107      *            The Http request
108      * @return the html code of the jasper portlet form
109      */
110     public String getModify( HttpServletRequest request )
111     {
112         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
113         int nPortletId = Integer.parseInt( strPortletId );
114         JasperPortlet/../../../../fr/paris/lutece/plugins/jasper/business/portlet/JasperPortlet.html#JasperPortlet">JasperPortlet portlet = (JasperPortlet) PortletHome.findByPrimaryKey( nPortletId );
115         HtmlTemplate template = getModifyTemplate( portlet );
116 
117         // fills the template with specific values
118         ReferenceList listFeeds = getDummyList( );
119         String strHtmlCombo = getFeedIndexCombo( listFeeds, portlet.getJasperFeedId( ) );
120         template.substitute( COMBO_FEED_LIST, strHtmlCombo );
121 
122         return template.getHtml( );
123     }
124 
125     /**
126      * Treats the creation form of a new jasper portlet
127      *
128      * @param request
129      *            The Http request
130      * @return The jsp URL which displays the view of the created jasper portlet
131      */
132     public String doCreate( HttpServletRequest request )
133     {
134         JasperPortletper/business/portlet/JasperPortlet.html#JasperPortlet">JasperPortlet portlet = new JasperPortlet( );
135 
136         // recovers portlet specific attributes
137         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
138         int nPageId = Integer.parseInt( strPageId );
139         String strFeedId = request.getParameter( PARAMETER_FEED_ID );
140 
141         // get portlet common attributes
142         String strErrorUrl = setPortletCommonData( request, portlet );
143 
144         if ( strErrorUrl != null )
145         {
146             return strErrorUrl;
147         }
148 
149         portlet.setPageId( nPageId );
150         portlet.setJasperFeedId( strFeedId );
151 
152         // Creates the portlet
153         JasperPortletHome.getInstance( ).create( portlet );
154 
155         // Displays the page with the new Portlet
156         return getPageUrl( nPageId );
157     }
158 
159     /**
160      * Treats the update form of the jasper portlet whose identifier is in the http request
161      *
162      * @param request
163      *            The Http request
164      * @return The jsp URL which displays the view of the updated portlet
165      */
166     public String doModify( HttpServletRequest request )
167     {
168         // fetches portlet attributes
169         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
170         int nPortletId = Integer.parseInt( strPortletId );
171         JasperPortlet/../../../../fr/paris/lutece/plugins/jasper/business/portlet/JasperPortlet.html#JasperPortlet">JasperPortlet portlet = (JasperPortlet) PortletHome.findByPrimaryKey( nPortletId );
172 
173         // retrieve portlet common attributes
174         String strErrorUrl = setPortletCommonData( request, portlet );
175 
176         if ( strErrorUrl != null )
177         {
178             return strErrorUrl;
179         }
180 
181         // fetches portlet specific attributes
182         String strFeedId = request.getParameter( PARAMETER_FEED_ID );
183         portlet.setJasperFeedId( strFeedId );
184 
185         // updates the portlet
186         portlet.update( );
187 
188         // displays the page with the updated portlet
189         return getPageUrl( portlet.getPageId( ) );
190     }
191 
192     /**
193      * Return the feed listing depending on rights
194      * 
195      * @param listFeeds
196      *            list of available jasper feeds
197      * @param strDefaultFeedId
198      *            The id of the feed used in the context
199      * @return The html code of the combo
200      */
201     String getFeedIndexCombo( ReferenceList listFeeds, String strDefaultFeedId )
202     {
203         Map<String, Object> model = new HashMap<String, Object>( );
204         model.put( MARK_FEED_LIST, listFeeds );
205         model.put( MARK_FEED_ID, strDefaultFeedId );
206 
207         HtmlTemplate templateCombo = AppTemplateService.getTemplate( TEMPLATE_COMBO_FEEDS, getLocale( ), model );
208 
209         return templateCombo.getHtml( );
210     }
211 
212     // In a real case this method should be collecting Data from a Home
213     ReferenceList getDummyList( )
214     {
215         ReferenceList list = new ReferenceList( );
216 
217         list.addItem( "1", "Jasper" );
218         list.addItem( "2", "Two" );
219 
220         return list;
221     }
222 }