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.formengine.modules.sample.web;
35  
36  import fr.paris.lutece.plugins.formengine.modules.sample.business.jaxb.ObjectFactory;
37  import fr.paris.lutece.plugins.formengine.modules.sample.business.jaxb.Sample;
38  import fr.paris.lutece.plugins.formengine.modules.sample.service.output.SampleIdGenerator;
39  import fr.paris.lutece.plugins.formengine.service.output.IdGenerator;
40  import fr.paris.lutece.plugins.formengine.web.SharedConstants;
41  import fr.paris.lutece.plugins.formengine.web.SubForm;
42  import fr.paris.lutece.portal.service.template.AppTemplateService;
43  import fr.paris.lutece.portal.service.util.AppPropertiesService;
44  import fr.paris.lutece.util.html.HtmlTemplate;
45  
46  import org.apache.commons.lang.StringUtils;
47  
48  import javax.servlet.http.HttpServletRequest;
49  import javax.servlet.http.HttpSession;
50  
51  
52  /**
53   * MiddleSubForm
54   */
55  public class MiddleSubForm extends SubForm
56  {
57      private static String _strXslFileForm = ".xsl.file.form.middle";
58      private static final String SUMMARY_MESSAGE = "Renseigner votre nom et votre chiffre";
59  
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public String doAction( String strActionName, HttpServletRequest request )
65      {
66          String strNextSubForm = StringUtils.EMPTY;
67  
68          if ( strActionName == null )
69          {
70              strNextSubForm = this.getName(  );
71          }
72          else if ( strActionName.equals( Constants.ACTION_NAME_NEXT ) )
73          {
74              this.fillFields( request );
75  
76              if ( this.validateFields( request ) )
77              {
78                  doSetData( request );
79                  strNextSubForm = this.getNextSubForm(  ).getName(  );
80  
81                  HttpSession session = request.getSession( true );
82                  session.setAttribute( Constants.SESSION_ATTRIBUTE_SAVE_BIS, Constants.SESSION_ATTRIBUTE_SAVE_VALUE );
83              }
84              else
85              {
86                  strNextSubForm = this.getName(  );
87              }
88          }
89          else if ( strActionName.equals( Constants.ACTION_NAME_PREVIOUS ) )
90          {
91              strNextSubForm = this.getPreviousSubForm(  ).getName(  );
92          }
93          else if ( strActionName.equals( Constants.ACTION_NAME_ENREGISTREMENT ) )
94          {
95              this.fillFields( request );
96              doSetData( request );
97  
98              HttpSession session = request.getSession( true );
99              session.setAttribute( Constants.SESSION_ATTRIBUTE_SAVE, Constants.SESSION_ATTRIBUTE_SAVE_VALUE );
100             strNextSubForm = this.getName(  );
101         }
102 
103         this.getParentForm(  ).setIsAccessToSubFormAllowed( request, strNextSubForm, true );
104 
105         return strNextSubForm;
106     }
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     protected String displaySummary( HttpServletRequest request )
113     {
114         StringBuilder sbResult = new StringBuilder( SUMMARY_MESSAGE );
115         HttpSession session = request.getSession( true );
116 
117         if ( StringUtils.isBlank( (String) session.getAttribute( SharedConstants.SESSION_ATTRIBUTE_WARNING_ERROR_WS ) ) )
118         {
119             if ( StringUtils.isNotBlank( (String) session.getAttribute( Constants.SESSION_ATTRIBUTE_SAVE ) ) )
120             {
121                 HtmlTemplate template = AppTemplateService.getTemplate( Constants.TEMPLATE_MESSAGE_SAVE,
122                         request.getLocale(  ) );
123                 sbResult.append( template.getHtml(  ) );
124                 session.removeAttribute( Constants.SESSION_ATTRIBUTE_SAVE );
125             }
126         }
127 
128         return sbResult.toString(  );
129     }
130 
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     protected String getXslFormElementsFileName(  )
136     {
137         return AppPropertiesService.getProperty( Constants.SHARED_PROPERTY_PREFIX + _strXslFileForm );
138     }
139 
140     /**
141      * update data to object
142      * @param request request
143      */
144     private void doSetData( HttpServletRequest request )
145     {
146         Sample sample = (Sample) getParentForm(  ).getFormDocument( request );
147 
148         if ( sample == null )
149         {
150             // create the ObjectFactory
151             ObjectFactory factory = new ObjectFactory(  );
152             sample = factory.createSample(  );
153         }
154 
155         IdGenerator idGenerator = new SampleIdGenerator(  );
156 
157         sample.setIdentifiant( idGenerator.getNewId( this.getParentForm(  ) ) );
158 
159         if ( StringUtils.isNotBlank( request.getParameter( Constants.PARAMETER_FORM_NAME ) ) )
160         {
161             sample.setNom( request.getParameter( Constants.PARAMETER_FORM_NAME ) );
162         }
163 
164         if ( StringUtils.isNotBlank( request.getParameter( Constants.PARAMETER_FORM_NUMERO ) ) )
165         {
166             sample.setNumero( Integer.valueOf( request.getParameter( Constants.PARAMETER_FORM_NUMERO ) ).intValue(  ) );
167         }
168 
169         this.getParentForm(  ).setFormDocument( request, sample );
170     }
171 }