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.List;
37  import java.util.Locale;
38  
39  import javax.servlet.http.HttpServletRequest;
40  
41  import org.apache.commons.lang3.StringUtils;
42  import org.apache.commons.lang3.math.NumberUtils;
43  
44  import fr.paris.lutece.plugins.genericattributes.business.Entry;
45  import fr.paris.lutece.plugins.genericattributes.business.Field;
46  import fr.paris.lutece.plugins.genericattributes.business.FieldHome;
47  import fr.paris.lutece.plugins.genericattributes.business.GenericAttributeError;
48  import fr.paris.lutece.plugins.genericattributes.business.MandatoryError;
49  import fr.paris.lutece.plugins.genericattributes.business.Response;
50  import fr.paris.lutece.portal.service.i18n.I18nService;
51  import fr.paris.lutece.portal.service.message.AdminMessage;
52  import fr.paris.lutece.portal.service.message.AdminMessageService;
53  
54  /**
55   * Abstract entry type for selects
56   */
57  public abstract class AbstractEntryTypeSelectOrder 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 strCSSClass = request.getParameter( PARAMETER_CSS_CLASS );
72          String strOnlyDisplayInBack = request.getParameter( PARAMETER_ONLY_DISPLAY_IN_BACK );
73          String strIndexed = request.getParameter( PARAMETER_INDEXED );
74  
75          String strFieldError = StringUtils.EMPTY;
76  
77          if ( StringUtils.isBlank( strTitle ) )
78          {
79              strFieldError = ERROR_FIELD_TITLE;
80          }
81  
82          if ( StringUtils.isNotBlank( strFieldError ) )
83          {
84              Object [ ] tabRequiredFields = {
85                      I18nService.getLocalizedString( strFieldError, locale )
86              };
87  
88              return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
89          }
90  
91          strFieldError = createFieldsUseRefList( entry, request );
92          if ( StringUtils.isNotBlank( strFieldError ) )
93          {
94              return AdminMessageService.getMessageUrl( request, strFieldError, ERROR_FIELD_REF_LIST, AdminMessage.TYPE_STOP );
95          }
96  
97          entry.setCode( strCode );
98          entry.setTitle( strTitle );
99          entry.setHelpMessage( strHelpMessage );
100         entry.setComment( strComment );
101         entry.setCSSClass( strCSSClass );
102 
103         entry.setMandatory( strMandatory != null );
104         entry.setOnlyDisplayInBack( strOnlyDisplayInBack != null );
105         entry.setIndexed( strIndexed != null );
106 
107         return null;
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public GenericAttributeError getResponseData( Entry entry, HttpServletRequest request, List<Response> listResponse, Locale locale )
115     {
116         String strIdField = request.getParameter( PREFIX_ATTRIBUTE + entry.getIdEntry( ) );
117         
118         String[] fieldsIds = strIdField.split( ";" );
119         
120         boolean hasResponse = false;
121         int order = 1;
122         for ( String idField : fieldsIds )
123         {
124             String id = idField.substring( idField.indexOf( '=' ) + 1 );
125             Field field = FieldHome.findByPrimaryKey( NumberUtils.toInt( id, -1 ) );
126             if ( field != null )
127             {
128                 Response/genericattributes/business/Response.html#Response">Response response = new Response( );
129                 response.setEntry( entry );
130                 response.setResponseValue( field.getValue( ) );
131                 response.setField( field );
132                 response.setIterationNumber( getResponseIterationValue( request ) );
133                 response.setSortOrder( order++ );
134 
135                 listResponse.add( response );
136                 hasResponse = true;
137             }
138         }
139         
140         if ( entry.isMandatory( ) && !hasResponse )
141         {
142             return new MandatoryError( entry, locale );
143         }
144         
145         return null;
146     }
147 }