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  
46  import org.apache.commons.lang3.StringUtils;
47  
48  import java.util.List;
49  import java.util.Locale;
50  
51  import javax.servlet.http.HttpServletRequest;
52  
53  /**
54   * Abstract entry type for selects
55   */
56  public abstract class AbstractEntryTypeSelect extends AbstractEntryTypeChoice
57  {
58      /**
59       * {@inheritDoc}
60       */
61      @Override
62      public String getRequestData( Entry entry, HttpServletRequest request, Locale locale )
63      {
64          initCommonRequestData( entry, request );
65          String strTitle = request.getParameter( PARAMETER_TITLE );
66          String strCode = request.getParameter( PARAMETER_ENTRY_CODE );
67          String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
68          String strComment = request.getParameter( PARAMETER_COMMENT );
69          String strMandatory = request.getParameter( PARAMETER_MANDATORY );
70          String strCSSClass = request.getParameter( PARAMETER_CSS_CLASS );
71          String strOnlyDisplayInBack = request.getParameter( PARAMETER_ONLY_DISPLAY_IN_BACK );
72          String strIndexed = request.getParameter( PARAMETER_INDEXED );
73  
74          String strFieldError = StringUtils.EMPTY;
75  
76          if ( StringUtils.isBlank( strTitle ) )
77          {
78              strFieldError = ERROR_FIELD_TITLE;
79          }
80  
81          if ( StringUtils.isNotBlank( strFieldError ) )
82          {
83              Object [ ] tabRequiredFields = {
84                      I18nService.getLocalizedString( strFieldError, locale )
85              };
86  
87              return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
88          }
89  
90          strFieldError = createFieldsUseRefList( entry, request );
91          if ( StringUtils.isNotBlank( strFieldError ) )
92          {
93              return AdminMessageService.getMessageUrl( request, strFieldError, ERROR_FIELD_REF_LIST, AdminMessage.TYPE_STOP );
94          }
95  
96          entry.setCode( strCode );
97          entry.setTitle( strTitle );
98          entry.setHelpMessage( strHelpMessage );
99          entry.setComment( strComment );
100         entry.setCSSClass( strCSSClass );
101 
102         entry.setMandatory( strMandatory != null );
103         entry.setOnlyDisplayInBack( strOnlyDisplayInBack != null );
104         entry.setIndexed( strIndexed != null );
105 
106         return null;
107     }
108 
109     /**
110      * {@inheritDoc}
111      */
112     @Override
113     public GenericAttributeError getResponseData( Entry entry, HttpServletRequest request, List<Response> listResponse, Locale locale )
114     {
115         String strIdField = request.getParameter( PREFIX_ATTRIBUTE + entry.getIdEntry( ) );
116         int nIdField = -1;
117         Field field = null;
118         Response/genericattributes/business/Response.html#Response">Response response = new Response( );
119         response.setEntry( entry );
120 
121         if ( StringUtils.isNotEmpty( strIdField ) && StringUtils.isNumeric( strIdField ) )
122         {
123             nIdField = Integer.parseInt( strIdField );
124         }
125 
126         if ( nIdField != -1 )
127         {
128             field = GenericAttributesUtils.findFieldByIdInTheList( nIdField, entry.getFields( ) );
129         }
130 
131         if ( field != null )
132         {
133             response.setResponseValue( field.getValue( ) );
134             response.setField( field );
135         }
136 
137         response.setIterationNumber( getResponseIterationValue( request ) );
138 
139         listResponse.add( response );
140 
141         if ( entry.isMandatory( ) && ( ( field == null ) || StringUtils.isBlank( field.getValue( ) ) ) )
142         {
143             return new MandatoryError( entry, locale );
144         }
145 
146         return null;
147     }
148 }