View Javadoc
1   /*
2    * Copyright (c) 2002-2018, 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.appointment.web.portlet;
35  
36  import java.util.Collection;
37  import java.util.HashMap;
38  import java.util.Map;
39  
40  import javax.servlet.http.HttpServletRequest;
41  
42  import org.apache.commons.lang.StringUtils;
43  
44  import fr.paris.lutece.plugins.appointment.business.form.Form;
45  import fr.paris.lutece.plugins.appointment.business.portlet.AppointmentFormPortlet;
46  import fr.paris.lutece.plugins.appointment.business.portlet.AppointmentFormPortletHome;
47  import fr.paris.lutece.plugins.appointment.service.FormService;
48  import fr.paris.lutece.portal.business.portlet.PortletHome;
49  import fr.paris.lutece.portal.service.message.AdminMessage;
50  import fr.paris.lutece.portal.service.message.AdminMessageService;
51  import fr.paris.lutece.util.ReferenceList;
52  import fr.paris.lutece.util.html.HtmlTemplate;
53  
54  /**
55   * This class provides the user interface to manage AppointmentPortlet features
56   * 
57   * @author Laurent Payen
58   *
59   */
60  public class AppointmentFormPortletJspBean extends AbstractPortletJspBean
61  {
62      /**
63       * Serial version UID
64       */
65      private static final long serialVersionUID = 5342937491389478335L;
66  
67      // Marks
68      private static final String MARK_LIST_APPOINTMENT_FORM = "refListAppointmentForm";
69  
70      // Parameters
71      private static final String PARAMETER_FORM = "id_form";
72  
73      // Messages
74      private static final String MESSAGE_ERROR_NO_APPOINTMENT_FORM_SELECTED = "appointment.message.error.noAppointmentFormSelected";
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public String getCreate( HttpServletRequest request )
81      {
82          String strPageId = request.getParameter( PARAMETER_PAGE_ID );
83          String strPortletTypeId = request.getParameter( PARAMETER_PORTLET_TYPE_ID );
84  
85          Collection<Form> listIsActiveAndIsDisplayedOnPortletAppointmentForm = FormService.findAllActiveAndDisplayedOnPortletForms( );
86  
87          ReferenceList refListAppointmentForm = new ReferenceList( );
88  
89          for ( Form form : listIsActiveAndIsDisplayedOnPortletAppointmentForm )
90          {
91              refListAppointmentForm.addItem( form.getIdForm( ), form.getTitle( ) );
92          }
93  
94          Map<String, Object> model = new HashMap<String, Object>( );
95          model.put( MARK_LIST_APPOINTMENT_FORM, refListAppointmentForm );
96  
97          HtmlTemplate template = getCreateTemplate( strPageId, strPortletTypeId, model );
98  
99          return template.getHtml( );
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public String getModify( HttpServletRequest request )
107     {
108         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
109         int nPortletId = Integer.parseInt( strPortletId );
110         AppointmentFormPortlet portlet = (AppointmentFormPortlet) PortletHome.findByPrimaryKey( nPortletId );
111 
112         Collection<Form> listIsActiveAndIsDisplayedOnPortletAppointmentForm = FormService.findAllActiveAndDisplayedOnPortletForms( );
113 
114         ReferenceList refListAppointmentForm = new ReferenceList( );
115 
116         for ( Form form : listIsActiveAndIsDisplayedOnPortletAppointmentForm )
117         {
118             refListAppointmentForm.addItem( form.getIdForm( ), form.getTitle( ) );
119         }
120 
121         Map<String, Object> model = new HashMap<String, Object>( );
122         model.put( MARK_LIST_APPOINTMENT_FORM, refListAppointmentForm );
123 
124         HtmlTemplate template = getModifyTemplate( portlet, model );
125 
126         return template.getHtml( );
127     }
128 
129     /**
130      * {@inheritDoc}
131      */
132     @Override
133     public String doCreate( HttpServletRequest request )
134     {
135         AppointmentFormPortlet portlet = new AppointmentFormPortlet( );
136 
137         // recovers portlet specific attributes
138         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
139         int nPageId = Integer.parseInt( strPageId );
140 
141         String strFormId = request.getParameter( PARAMETER_FORM );
142 
143         if ( StringUtils.isNotEmpty( strFormId ) && StringUtils.isNumeric( strFormId ) )
144         {
145             int nIdForm = Integer.parseInt( strFormId );
146             portlet.setIdAppointmentForm( nIdForm );
147         }
148         else
149         {
150             return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_NO_APPOINTMENT_FORM_SELECTED, AdminMessage.TYPE_STOP );
151         }
152 
153         // get portlet common attributes
154         String strErrorUrl = setPortletCommonData( request, portlet );
155 
156         if ( strErrorUrl != null )
157         {
158             return strErrorUrl;
159         }
160 
161         portlet.setPageId( nPageId );
162 
163         // Creates the portlet
164         AppointmentFormPortletHome.getInstance( ).create( portlet );
165 
166         // Displays the page with the new Portlet
167         return getPageUrl( nPageId );
168     }
169 
170     /**
171      * {@inheritDoc}
172      */
173     @Override
174     public String doModify( HttpServletRequest request )
175     {
176         // fetches portlet attributes
177         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
178         int nPortletId = Integer.parseInt( strPortletId );
179         AppointmentFormPortlet portlet = (AppointmentFormPortlet) PortletHome.findByPrimaryKey( nPortletId );
180 
181         String strFormId = request.getParameter( PARAMETER_FORM );
182 
183         if ( StringUtils.isNotEmpty( strFormId ) && StringUtils.isNumeric( strFormId ) )
184         {
185             int nIdForm = Integer.parseInt( strFormId );
186             portlet.setIdAppointmentForm( nIdForm );
187         }
188         else
189         {
190             return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_NO_APPOINTMENT_FORM_SELECTED, AdminMessage.TYPE_STOP );
191         }
192 
193         // retrieve portlet common attributes
194         String strErrorUrl = setPortletCommonData( request, portlet );
195 
196         if ( strErrorUrl != null )
197         {
198             return strErrorUrl;
199         }
200 
201         // updates the portlet
202         portlet.update( );
203 
204         // displays the page with the updated portlet
205         return getPageUrl( portlet.getPageId( ) );
206     }
207 }