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.jsr168.web.portlet;
35  
36  import fr.paris.lutece.plugins.jsr168.business.portlet.Jsr168Portlet;
37  import fr.paris.lutece.plugins.jsr168.business.portlet.Jsr168PortletHome;
38  import fr.paris.lutece.plugins.jsr168.pluto.LuteceToPlutoConnector;
39  import fr.paris.lutece.plugins.jsr168.pluto.core.PortalURL;
40  import fr.paris.lutece.portal.business.portlet.PortletHome;
41  import fr.paris.lutece.portal.service.template.AppTemplateService;
42  import fr.paris.lutece.portal.web.portlet.PortletJspBean;
43  import fr.paris.lutece.util.ReferenceList;
44  import fr.paris.lutece.util.html.HtmlTemplate;
45  
46  import java.util.HashMap;
47  
48  import javax.servlet.http.HttpServletRequest;
49  
50  
51  /**
52   * This class provides the user interface to manage Jsr 168 Portlet features
53   */
54  public class Jsr168PortletJspBean extends PortletJspBean
55  {
56      ////////////////////////////////////////////////////////////////////////////
57      // Constants
58  
59      //  Rights
60      public static final String RIGHT_MANAGE_ADMIN_SITE = "CORE_ADMIN_SITE";
61      private static final String ADMIN_SITE = "../../site/AdminSite.jsp";
62      private static final String MESSAGE_MANDATORY_PORTLET_TITLE = "../../Message.jsp?message=mandatory.portlet.title";
63      private static final String PARAM_PAGE_ID = "page_id";
64      private static final String PARAM_PORTLET_ID = "portlet_id";
65      private static final String PARAM_PORTLET_TYPE_ID = "portlet_type_id";
66      private static final String PARAM_JSR168_NAME = "jsr168_name";
67      private static final String BOOKMARK_JSR168_TITLE_COMBO = "@jsr168_title_combo@";
68  
69      //Markers
70      private static final String MARK_PORTLET_LIST = "portlet_list";
71      private static final String MARK_PORTLET_ID = "default_portlet_id";
72  
73      // Templates
74      private static final String TEMPLATE_COMBO_PORTLETS = "admin/plugins/jsr168/portlet/combo_portletsJSR.html";
75      private static final String COMBO_PORTLETS_LIST = "@combo_portletsJSR@";
76  
77      /**
78       * Creates a new Jsr168PortletJspBean object.
79       */
80      public Jsr168PortletJspBean(  )
81      {
82      }
83  
84      /**
85       * Returns the properties prefix used for jsr168 portlet and defined in lutece.properties file
86       *
87       * @return the value of the property prefix
88       */
89      public String getPropertiesPrefix(  )
90      {
91          return "portlet.jsr168";
92      }
93  
94      /**
95       * Returns the Jsr 168 Portlet form of creation
96       *
97       * @param request The current Http request
98       * @return the html code of the jsr 168 portlet form
99       */
100     public String getCreate( HttpServletRequest request )
101     {
102         System.out.println( "in getCreate" );
103 
104         String strPageId = request.getParameter( PARAM_PAGE_ID );
105         String strPortletTypeId = request.getParameter( PARAM_PORTLET_TYPE_ID );
106         HtmlTemplate template = getCreateTemplate( strPageId, strPortletTypeId );
107 
108         System.out.println( "Template before :" );
109         System.out.println( template.getHtml(  ) );
110 
111         ReferenceList portletsList = LuteceToPlutoConnector.getPortletTitles(  );
112 
113         String strHtmlCombo = getPortletIndexCombo( portletsList, "" );
114         template.substitute( BOOKMARK_JSR168_TITLE_COMBO, strHtmlCombo );
115         System.out.println( "out getCreate" );
116         System.out.println( "Template after :" );
117         System.out.println( template.getHtml(  ) );
118 
119         return template.getHtml(  );
120     }
121 
122     /**
123      * Returns the Jsr 168 Portlet form for update
124      *
125      * @param request The current Http request
126      * @return the html code of the jsr 168 portlet form
127      */
128     public String getModify( HttpServletRequest request )
129     {
130         String strPortletId = request.getParameter( PARAM_PORTLET_ID );
131         int nPortletId = Integer.parseInt( strPortletId );
132         Jsr168Portlet portlet = (Jsr168Portlet) PortletHome.findByPrimaryKey( nPortletId );
133         HtmlTemplate template = getModifyTemplate( portlet );
134 
135         ReferenceList portletsList = LuteceToPlutoConnector.getPortletTitles(  );
136 
137         String strHtmlCombo = getPortletIndexCombo( portletsList, portlet.getJsr168Name(  ) );
138         template.substitute( BOOKMARK_JSR168_TITLE_COMBO, strHtmlCombo );
139 
140         return template.getHtml(  );
141     }
142 
143     /**
144      * Processes the creation form of a new jsr 168 portlet
145      *
146      * @param request The current Http request
147      * @return The jsp URL which displays the view of the created jsr 168 portlet
148      */
149     public String doCreate( HttpServletRequest request )
150     {
151         Jsr168Portlet portlet = new Jsr168Portlet(  );
152 
153         // Recovers portlet common attributes
154         setPortletCommonData( request, portlet );
155 
156         // mandatory field
157         String strName = portlet.getName(  );
158 
159         if ( ( strName == null ) || "".equals( strName.trim(  ) ) )
160         {
161             return MESSAGE_MANDATORY_PORTLET_TITLE;
162         }
163 
164         // Recovers portlet specific attributes
165         String strPageId = request.getParameter( PARAM_PAGE_ID );
166         int nPageId = Integer.parseInt( strPageId );
167         portlet.setPageId( nPageId );
168 
169         // html code cleaning
170         String strContent = request.getParameter( PARAM_JSR168_NAME );
171         portlet.setJsr168Name( strContent );
172 
173         // creates the portlet
174         Jsr168PortletHome.getInstance(  ).create( portlet );
175 
176         // displays the page with the new portlet
177         return ADMIN_SITE + "?" + PARAM_PAGE_ID + "=" + portlet.getPageId(  );
178     }
179 
180     /**
181      * Processes the update form of the jsr 168 portlet whose identifier is in the http request
182      *
183      * @param request The current Http request
184      * @return The jsp URL which displays the view of the updated jsr 168 portlet
185      */
186     public String doModify( HttpServletRequest request )
187     {
188         // recovers portlet attributes
189         String strPortletId = request.getParameter( PARAM_PORTLET_ID );
190         int nPortletId = Integer.parseInt( strPortletId );
191         Jsr168Portlet portlet = (Jsr168Portlet) PortletHome.findByPrimaryKey( nPortletId );
192 
193         // recovers portlet common attributes
194         setPortletCommonData( request, portlet );
195 
196         // mandatory field
197         String strName = portlet.getName(  );
198 
199         if ( ( strName == null ) || "".equals( strName.trim(  ) ) )
200         {
201             return MESSAGE_MANDATORY_PORTLET_TITLE;
202         }
203 
204         // html code cleaning
205         String strContent = request.getParameter( PARAM_JSR168_NAME );
206         portlet.setJsr168Name( strContent );
207 
208         // updates the portlet
209         portlet.update(  );
210 
211         // displays the page with the portlet updated
212         return ADMIN_SITE + "?" + PARAM_PAGE_ID + "=" + portlet.getPageId(  );
213     }
214 
215     /**
216     * Call {@link LuteceToPlutoConnector} method targeted by the request: <code>action</code>
217     * or <code>render</code>
218     *
219     * @param request The current Http request
220     * @return Indicate if this action has generated a fragment,
221     *         <code>true</code> no redirect to send,
222     *         <code>false</code> for a send redirect required (for <code>action</code> request)
223      */
224     public boolean realiseAction( HttpServletRequest request )
225     {
226         final String strPortletId = PortalURL.extractPortletId( request );
227 
228         final int nPortletId = Integer.parseInt( strPortletId );
229         final Jsr168Portlet portlet = (Jsr168Portlet) PortletHome.findByPrimaryKey( nPortletId );
230 
231         return LuteceToPlutoConnector.request( nPortletId, portlet.getJsr168Name(  ) );
232     }
233 
234     /**
235      * Return the feed listing depending on rights
236      * @param listFeeds list of available rss feeds
237      * @param strDefaultFeedId The id of the feed used in the context
238      * @return The html code of the combo
239      */
240     String getPortletIndexCombo( ReferenceList listPortlets, String strDefaultPortletId )
241     {
242         HashMap model = new HashMap(  );
243         model.put( MARK_PORTLET_LIST, listPortlets );
244         model.put( MARK_PORTLET_ID, strDefaultPortletId );
245 
246         HtmlTemplate templateCombo = AppTemplateService.getTemplate( TEMPLATE_COMBO_PORTLETS, getLocale(  ), model );
247 
248         return templateCombo.getHtml(  );
249     }
250 }