View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.genericattributes.service.entrytype;
35  
36  import fr.paris.lutece.plugins.genericattributes.business.Entry;
37  import fr.paris.lutece.plugins.genericattributes.business.Field;
38  import fr.paris.lutece.plugins.genericattributes.business.GenericAttributeError;
39  import fr.paris.lutece.plugins.genericattributes.business.MandatoryError;
40  import fr.paris.lutece.plugins.genericattributes.business.Response;
41  import fr.paris.lutece.plugins.genericattributes.util.GenericAttributesUtils;
42  import fr.paris.lutece.portal.service.i18n.I18nService;
43  import fr.paris.lutece.portal.service.message.AdminMessage;
44  import fr.paris.lutece.portal.service.message.AdminMessageService;
45  import fr.paris.lutece.portal.service.util.AppLogService;
46  
47  import org.apache.commons.lang3.StringUtils;
48  
49  import java.util.List;
50  import java.util.Locale;
51  
52  import javax.servlet.http.HttpServletRequest;
53  
54  /**
55   * Abstract entry type for radio buttons
56   */
57  public abstract class AbstractEntryTypeRadioButton extends AbstractEntryTypeChoice
58  {
59      /**
60       * {@inheritDoc}
61       */
62      @Override
63      public String getRequestData( Entry entry, HttpServletRequest request, Locale locale )
64      {
65          initCommonRequestData( entry, request );
66          String strTitle = request.getParameter( PARAMETER_TITLE );
67          String strCode = request.getParameter( PARAMETER_ENTRY_CODE );
68          String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
69          String strComment = request.getParameter( PARAMETER_COMMENT );
70          String strMandatory = request.getParameter( PARAMETER_MANDATORY );
71          String strFieldInLine = request.getParameter( PARAMETER_FIELD_IN_LINE );
72          String strCSSClass = request.getParameter( PARAMETER_CSS_CLASS );
73          String strOnlyDisplayInBack = request.getParameter( PARAMETER_ONLY_DISPLAY_IN_BACK );
74  
75          int nFieldInLine = -1;
76  
77          String strFieldError = StringUtils.EMPTY;
78  
79          if ( StringUtils.isBlank( strTitle ) )
80          {
81              strFieldError = ERROR_FIELD_TITLE;
82          }
83  
84          if ( StringUtils.isNotBlank( strFieldError ) )
85          {
86              Object [ ] tabRequiredFields = {
87                      I18nService.getLocalizedString( strFieldError, locale )
88              };
89  
90              return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
91          }
92  
93          strFieldError = createFieldsUseRefList( entry, request );
94          if ( StringUtils.isNotBlank( strFieldError ) )
95          {
96              return AdminMessageService.getMessageUrl( request, strFieldError, ERROR_FIELD_REF_LIST, AdminMessage.TYPE_STOP );
97          }
98  
99          entry.setCode( strCode );
100         entry.setTitle( strTitle );
101         entry.setHelpMessage( strHelpMessage );
102         entry.setComment( strComment );
103         entry.setCSSClass( strCSSClass );
104 
105         entry.setMandatory( strMandatory != null );
106         entry.setOnlyDisplayInBack( strOnlyDisplayInBack != null );
107 
108         try
109         {
110             nFieldInLine = Integer.parseInt( strFieldInLine );
111         }
112         catch( NumberFormatException ne )
113         {
114             AppLogService.error( ne.getMessage( ), ne );
115         }
116 
117         entry.setFieldInLine( nFieldInLine == 1 );
118         return null;
119     }
120 
121     /**
122      * {@inheritDoc}
123      */
124     @Override
125     public GenericAttributeError getResponseData( Entry entry, HttpServletRequest request, List<Response> listResponse, Locale locale )
126     {
127         String strIdField = request.getParameter( PREFIX_ATTRIBUTE + entry.getIdEntry( ) );
128         int nIdField = -1;
129         Field field = null;
130         Response/genericattributes/business/Response.html#Response">Response response = new Response( );
131         response.setEntry( entry );
132 
133         if ( strIdField != null )
134         {
135             try
136             {
137                 nIdField = Integer.parseInt( strIdField );
138             }
139             catch( NumberFormatException ne )
140             {
141                 AppLogService.error( ne.getMessage( ), ne );
142             }
143         }
144 
145         if ( nIdField != -1 )
146         {
147             field = GenericAttributesUtils.findFieldByIdInTheList( nIdField, entry.getFields( ) );
148         }
149 
150         if ( field != null )
151         {
152             response.setResponseValue( field.getValue( ) );
153             response.setField( field );
154         }
155 
156         response.setIterationNumber( getResponseIterationValue( request ) );
157 
158         listResponse.add( response );
159 
160         if ( entry.isMandatory( ) && ( ( field == null ) || StringUtils.isBlank( field.getValue( ) ) ) )
161         {
162             return new MandatoryError( entry, locale );
163         }
164 
165         return null;
166     }
167 }