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.announce.web.portlet;
35  
36  import fr.paris.lutece.plugins.announce.business.portlet.LastAnnouncesPortlet;
37  import fr.paris.lutece.plugins.announce.business.portlet.LastAnnouncesPortletHome;
38  import fr.paris.lutece.portal.business.portlet.PortletHome;
39  import fr.paris.lutece.portal.service.message.AdminMessage;
40  import fr.paris.lutece.portal.service.message.AdminMessageService;
41  import fr.paris.lutece.portal.service.util.AppPropertiesService;
42  import fr.paris.lutece.portal.web.constants.Messages;
43  import fr.paris.lutece.portal.web.portlet.PortletJspBean;
44  import fr.paris.lutece.util.html.HtmlTemplate;
45  
46  import org.apache.commons.lang.StringUtils;
47  
48  import java.util.HashMap;
49  import java.util.Map;
50  
51  import javax.servlet.http.HttpServletRequest;
52  
53  /**
54   * This class provides the user interface to manage MyAnnouncesPortlet features
55   */
56  public class LastAnnouncesPortletJspBean extends PortletJspBean
57  {
58      /**
59       * Serial version UID
60       */
61      private static final long serialVersionUID = 6187858592847623844L;
62  
63      // Marks
64      private static final String MARK_DEFAULT_NB_ANNOUNCES_TO_DISPLAY = "defaultNbAnnouncesToDisplay";
65      private static final String MARK_PORTLET = "portlet";
66  
67      // Parameters
68      private static final String PARAMETER_NB_ANNOUNCES_TO_DISPLAY = "nb_announces_to_display";
69  
70      // Properties
71      private static final String PROPERTY_DEFAULT_NB_ANNOUNCES_TO_DISPLAY = "announce.portletLastAnnounces.defaultNbAnnouncesToDisplay";
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      public String getCreate( HttpServletRequest request )
78      {
79          String strPageId = request.getParameter( PARAMETER_PAGE_ID );
80          String strPortletTypeId = request.getParameter( PARAMETER_PORTLET_TYPE_ID );
81          Map<String, Object> model = new HashMap<>( );
82          model.put( MARK_DEFAULT_NB_ANNOUNCES_TO_DISPLAY, AppPropertiesService.getProperty( PROPERTY_DEFAULT_NB_ANNOUNCES_TO_DISPLAY ) );
83  
84          HtmlTemplate template = getCreateTemplate( strPageId, strPortletTypeId, model );
85  
86          return template.getHtml( );
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      @Override
93      public String getModify( HttpServletRequest request )
94      {
95          String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
96          int nPortletId = Integer.parseInt( strPortletId );
97          LastAnnouncesPortlet../../fr/paris/lutece/plugins/announce/business/portlet/LastAnnouncesPortlet.html#LastAnnouncesPortlet">LastAnnouncesPortlet portlet = (LastAnnouncesPortlet) PortletHome.findByPrimaryKey( nPortletId );
98  
99          Map<String, Object> model = new HashMap<>( );
100         model.put( MARK_PORTLET, portlet );
101 
102         HtmlTemplate template = getModifyTemplate( portlet, model );
103 
104         return template.getHtml( );
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public String doCreate( HttpServletRequest request )
112     {
113         LastAnnouncesPortletusiness/portlet/LastAnnouncesPortlet.html#LastAnnouncesPortlet">LastAnnouncesPortlet portlet = new LastAnnouncesPortlet( );
114 
115         // recovers portlet specific attributes
116         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
117         int nPageId = Integer.parseInt( strPageId );
118 
119         // get portlet common attributes
120         String strErrorUrl = setPortletCommonData( request, portlet );
121 
122         if ( strErrorUrl != null )
123         {
124             return strErrorUrl;
125         }
126 
127         String strNbItemsToDisplay = request.getParameter( PARAMETER_NB_ANNOUNCES_TO_DISPLAY );
128 
129         if ( StringUtils.isEmpty( strNbItemsToDisplay ) || !StringUtils.isNumeric( strNbItemsToDisplay ) )
130         {
131             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
132         }
133 
134         int nNbAnnouncesToDisplay = Integer.parseInt( strNbItemsToDisplay );
135 
136         portlet.setPageId( nPageId );
137         portlet.setNbAnnouncesToDisplay( nNbAnnouncesToDisplay );
138 
139         // Creates the portlet
140         LastAnnouncesPortletHome.getInstance( ).create( portlet );
141 
142         // Displays the page with the new Portlet
143         return "../" + getPageUrl( nPageId );
144     }
145 
146     /**
147      * {@inheritDoc}
148      */
149     @Override
150     public String doModify( HttpServletRequest request )
151     {
152         // fetches portlet attributes
153         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
154         int nPortletId = Integer.parseInt( strPortletId );
155         LastAnnouncesPortlet../../fr/paris/lutece/plugins/announce/business/portlet/LastAnnouncesPortlet.html#LastAnnouncesPortlet">LastAnnouncesPortlet portlet = (LastAnnouncesPortlet) PortletHome.findByPrimaryKey( nPortletId );
156 
157         // retrieve portlet common attributes
158         String strErrorUrl = setPortletCommonData( request, portlet );
159 
160         if ( strErrorUrl != null )
161         {
162             return strErrorUrl;
163         }
164 
165         String strNbItemsToDisplay = request.getParameter( PARAMETER_NB_ANNOUNCES_TO_DISPLAY );
166 
167         if ( StringUtils.isEmpty( strNbItemsToDisplay ) || !StringUtils.isNumeric( strNbItemsToDisplay ) )
168         {
169             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
170         }
171 
172         int nNbAnnouncesToDisplay = Integer.parseInt( strNbItemsToDisplay );
173 
174         portlet.setNbAnnouncesToDisplay( nNbAnnouncesToDisplay );
175 
176         // updates the portlet
177         portlet.update( );
178 
179         // displays the page with the updated portlet
180         return "../" + getPageUrl( portlet.getPageId( ) );
181     }
182 }