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.collections.CollectionUtils;
48  import org.apache.commons.lang3.StringUtils;
49  
50  import java.util.ArrayList;
51  import java.util.List;
52  import java.util.Locale;
53  import java.util.Objects;
54  import java.util.stream.Collectors;
55  
56  import javax.servlet.http.HttpServletRequest;
57  
58  /**
59   * Abstract entry type for check boxes
60   */
61  public abstract class AbstractEntryTypeCheckBox extends AbstractEntryTypeChoice
62  {
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      public String getRequestData( Entry entry, HttpServletRequest request, Locale locale )
68      {
69          initCommonRequestData( entry, request );
70          String strCode = request.getParameter( PARAMETER_ENTRY_CODE );
71          String strTitle = request.getParameter( PARAMETER_TITLE );
72          String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
73          String strComment = request.getParameter( PARAMETER_COMMENT );
74          String strMandatory = request.getParameter( PARAMETER_MANDATORY );
75          String strErrorMessage = request.getParameter( PARAMETER_ERROR_MESSAGE );
76          String strFieldInLine = request.getParameter( PARAMETER_FIELD_IN_LINE );
77          String strCSSClass = request.getParameter( PARAMETER_CSS_CLASS );
78          String strOnlyDisplayInBack = request.getParameter( PARAMETER_ONLY_DISPLAY_IN_BACK );
79  
80          int nFieldInLine = -1;
81  
82          String strFieldError = StringUtils.EMPTY;
83  
84          if ( StringUtils.isBlank( strTitle ) )
85          {
86              strFieldError = ERROR_FIELD_TITLE;
87          }
88  
89          if ( StringUtils.isNotBlank( strFieldError ) )
90          {
91              Object [ ] tabRequiredFields = {
92                      I18nService.getLocalizedString( strFieldError, locale )
93              };
94  
95              return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
96          }
97  
98          strFieldError = createFieldsUseRefList( entry, request );
99          if ( StringUtils.isNotBlank( strFieldError ) )
100         {
101             return AdminMessageService.getMessageUrl( request, strFieldError, ERROR_FIELD_REF_LIST, AdminMessage.TYPE_STOP );
102         }
103 
104         entry.setCode( strCode );
105         entry.setTitle( strTitle );
106         entry.setHelpMessage( strHelpMessage );
107         entry.setComment( strComment );
108         entry.setCSSClass( strCSSClass );
109 
110         entry.setMandatory( strMandatory != null );
111         entry.setOnlyDisplayInBack( strOnlyDisplayInBack != null );
112         entry.setErrorMessage( strErrorMessage );
113 
114         try
115         {
116             nFieldInLine = Integer.parseInt( strFieldInLine );
117         }
118         catch( NumberFormatException ne )
119         {
120             AppLogService.error( ne );
121         }
122 
123         entry.setFieldInLine( nFieldInLine == 1 );
124         return null;
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     @Override
131     public GenericAttributeError getResponseData( Entry entry, HttpServletRequest request, List<Response> listResponse, Locale locale )
132     {
133         String [ ] strTabIdField = request.getParameterValues( PREFIX_ATTRIBUTE + entry.getIdEntry( ) );
134         List<Integer> listFieldIdInResponse = new ArrayList<>( );
135         List<Field> listFieldInResponse = new ArrayList<>( );
136 
137         if ( strTabIdField != null )
138         {
139             for ( int cpt = 0; cpt < strTabIdField.length; cpt++ )
140             {
141                 try
142                 {
143                     int nIdField = Integer.parseInt( strTabIdField [cpt] );
144                     listFieldIdInResponse.add( nIdField );
145                 }
146                 catch( NumberFormatException ne )
147                 {
148                     AppLogService.error( ne.getMessage( ), ne );
149                 }
150 
151             }
152             listFieldInResponse = listFieldIdInResponse.stream( ).map( id -> GenericAttributesUtils.findFieldByIdInTheList( id, entry.getFields( ) ) )
153                     .filter( Objects::nonNull ).collect( Collectors.toList( ) );
154         }
155 
156         if ( CollectionUtils.isNotEmpty( listFieldInResponse ) )
157         {
158             for ( Field fieldInResponse : listFieldInResponse )
159             {
160                 Response/genericattributes/business/Response.html#Response">Response response = new Response( );
161                 response.setEntry( entry );
162                 response.setResponseValue( fieldInResponse.getValue( ) );
163                 response.setField( fieldInResponse );
164                 response.setIterationNumber( getResponseIterationValue( request ) );
165                 listResponse.add( response );
166             }
167         }
168         else
169         {
170             Response/genericattributes/business/Response.html#Response">Response response = new Response( );
171             response.setEntry( entry );
172             response.setIterationNumber( getResponseIterationValue( request ) );
173             listResponse.add( response );
174         }
175 
176         if ( !entry.isMandatory( ) )
177         {
178             return null;
179         }
180 
181         boolean bAllFieldEmpty = listFieldInResponse.stream( ).map( Field::getValue ).allMatch( StringUtils::isEmpty );
182 
183         if ( bAllFieldEmpty )
184         {
185             if ( StringUtils.isNotBlank( entry.getErrorMessage( ) ) )
186             {
187                 GenericAttributeErrortributes/business/GenericAttributeError.html#GenericAttributeError">GenericAttributeError error = new GenericAttributeError( );
188                 error.setMandatoryError( true );
189                 error.setErrorMessage( entry.getErrorMessage( ) );
190 
191                 return error;
192             }
193 
194             return new MandatoryError( entry, locale );
195         }
196 
197         return null;
198     }
199 }