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 java.util.ArrayList;
37  import java.util.List;
38  import java.util.Locale;
39  import java.util.stream.Collectors;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  import org.apache.commons.lang3.StringUtils;
44  
45  import fr.paris.lutece.plugins.genericattributes.business.Entry;
46  import fr.paris.lutece.plugins.genericattributes.business.Field;
47  import fr.paris.lutece.plugins.genericattributes.business.FieldHome;
48  import fr.paris.lutece.plugins.genericattributes.business.GenericAttributeError;
49  import fr.paris.lutece.plugins.genericattributes.business.Response;
50  import fr.paris.lutece.plugins.genericattributes.util.GenericAttributesUtils;
51  import fr.paris.lutece.portal.service.i18n.I18nService;
52  import fr.paris.lutece.portal.service.message.AdminMessage;
53  import fr.paris.lutece.portal.service.message.AdminMessageService;
54  
55  /**
56   * Abstract entry type for check boxes
57   */
58  public abstract class AbstractEntryTypeArray extends EntryTypeService
59  {
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public String getRequestData( Entry entry, HttpServletRequest request, Locale locale )
65      {
66          initCommonRequestData( entry, request );
67          String strCode = request.getParameter( PARAMETER_ENTRY_CODE );
68          String strTitle = request.getParameter( PARAMETER_TITLE );
69          String strComment = request.getParameter( PARAMETER_COMMENT );
70          String strNumberRows = request.getParameter( PARAMETER_NUMBER_ROWS );
71          String strNumberColumns = request.getParameter( PARAMETER_NUMBER_COLUMNS );
72          String strFieldError = StringUtils.EMPTY;
73  
74          if ( StringUtils.isBlank( strTitle ) )
75          {
76              strFieldError = ERROR_FIELD_TITLE;
77          }
78          else
79              if ( StringUtils.isBlank( strNumberRows ) )
80              {
81                  strFieldError = FIELD_NUMBER_ROWS;
82              }
83              else
84                  if ( StringUtils.isBlank( strNumberColumns ) )
85                  {
86                      strFieldError = FIELD_NUMBER_COLUMNS;
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          else
98              if ( !isValid( strNumberRows ) )
99              {
100                 Object [ ] tabRequiredFields = {
101                         I18nService.getLocalizedString( FIELD_NUMBER_ROWS, locale )
102                 };
103 
104                 return AdminMessageService.getMessageUrl( request, MESSAGE_NUMERIC_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
105             }
106             else
107                 if ( !isValid( strNumberColumns ) )
108                 {
109                     Object [ ] tabRequiredFields = {
110                             I18nService.getLocalizedString( FIELD_NUMBER_COLUMNS, locale )
111                     };
112 
113                     return AdminMessageService.getMessageUrl( request, MESSAGE_NUMERIC_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
114                 }
115 
116         // for don't update fields listFields=null
117         int row = Integer.parseInt( strNumberRows );
118         int column = Integer.parseInt( strNumberColumns );
119         entry.setCode( strCode );
120         entry.setTitle( strTitle );
121         entry.setHelpMessage( null );
122         entry.setComment( strComment );
123         entry.setCSSClass( null );
124 
125         GenericAttributesUtils.createOrUpdateField( entry, FIELD_ARRAY_ROW, null, String.valueOf( row ) );
126         GenericAttributesUtils.createOrUpdateField( entry, FIELD_ARRAY_COLUMN, null, String.valueOf( column ) );
127 
128         List<Field> newFields = new ArrayList<>( );
129 
130         // Keep the non array_cell fields
131         newFields.addAll( entry.getFields( ).stream( ).filter( field -> !field.getCode( ).equals( FIELD_ARRAY_CELL ) ).collect( Collectors.toList( ) ) );
132 
133         // Update the array_cell fields
134         newFields.addAll( buildArrayCells( entry, row, column, request ) );
135         entry.setFields( newFields );
136 
137         return null;
138     }
139 
140     private List<Field> buildArrayCells( Entry entry, int row, int column, HttpServletRequest request )
141     {
142         List<Field> existingFields = entry.getFields( );
143         List<Field> listFields = new ArrayList<>( );
144         for ( int i = 1; i <= ( row + 1 ); i++ )
145         {
146             for ( int j = 1; j <= ( column + 1 ); j++ )
147             {
148                 String key = i + "_" + j;
149                 Field existingField = existingFields.stream( ).filter( f -> f.getValue( ).equals( key ) ).findFirst( ).orElse( null );
150 
151                 String strTitleRow = request.getParameter( "field_" + key );
152 
153                 Fieldlugins/genericattributes/business/Field.html#Field">Field field = new Field( );
154                 if ( existingField != null )
155                 {
156                     field = existingField;
157                 }
158                 field.setParentEntry( entry );
159                 field.setCode( FIELD_ARRAY_CELL );
160                 field.setValue( key );
161 
162                 if ( ( i == 1 && j != 1 ) || ( i != 1 && j == 1 ) )
163                 {
164                     field.setTitle( StringUtils.defaultString( strTitleRow ) );
165                 }
166                 listFields.add( field );
167             }
168         }
169 
170         return listFields;
171     }
172 
173     /**
174      * {@inheritDoc}
175      */
176     @Override
177     public GenericAttributeError getResponseData( Entry entry, HttpServletRequest request, List<Response> listResponse, Locale locale )
178     {
179         int row = Integer.parseInt( entry.getFieldByCode( FIELD_ARRAY_ROW ).getValue( ) );
180         int column = Integer.parseInt( entry.getFieldByCode( FIELD_ARRAY_COLUMN ).getValue( ) );
181 
182         for ( int i = 1; i <= ( row + 1 ); i++ )
183         {
184             for ( int j = 1; j <= ( column + 1 ); j++ )
185             {
186                 String strTitleRow = request.getParameter( "response_" + i + "_" + j );
187 
188                 Field existingFields = null;
189 
190                 for ( Field f : entry.getFields( ) )
191                 {
192                     if ( f.getValue( ).equals( i + "_" + j ) )
193                     {
194                         existingFields = f;
195 
196                         break;
197                     }
198                 }
199 
200                 Response/genericattributes/business/Response.html#Response">Response response = new Response( );
201                 response.setEntry( entry );
202                 response.setResponseValue( strTitleRow );
203                 response.setToStringValueResponse( strTitleRow );
204                 response.setField( existingFields );
205                 response.setIterationNumber( getResponseIterationValue( request ) );
206                 listResponse.add( response );
207             }
208         }
209 
210         return null;
211     }
212 
213     /**
214      * {@inheritDoc}
215      */
216     @Override
217     public String getResponseValueForExport( Entry entry, HttpServletRequest request, Response response, Locale locale )
218     {
219         return response.getResponseValue( );
220     }
221 
222     /**
223      * {@inheritDoc}
224      */
225     @Override
226     public String getResponseValueForRecap( Entry entry, HttpServletRequest request, Response response, Locale locale )
227     {
228         if ( response.getField( ) != null )
229         {
230             if ( response.getField( ).getTitle( ) == null )
231             {
232                 Field field = FieldHome.findByPrimaryKey( response.getField( ).getIdField( ) );
233 
234                 if ( field != null )
235                 {
236                     response.setField( field );
237                 }
238             }
239 
240             return response.getField( ).getTitle( );
241         }
242 
243         return null;
244     }
245 
246     /**
247      * Check if param is a valid integer
248      * 
249      * @param strValue
250      *            the value to check
251      * @return true if valid, false otherwise
252      */
253     private boolean isValid( String strValue )
254     {
255         return StringUtils.isNumeric( strValue ) && Integer.valueOf( strValue ) > 0;
256     }
257 }