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  
40  import javax.servlet.http.HttpServletRequest;
41  
42  import org.apache.commons.lang3.StringUtils;
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.GenericAttributeError;
47  import fr.paris.lutece.plugins.genericattributes.business.MandatoryError;
48  import fr.paris.lutece.plugins.genericattributes.business.Response;
49  import fr.paris.lutece.plugins.genericattributes.util.GenericAttributesUtils;
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  import fr.paris.lutece.portal.service.util.AppException;
54  import fr.paris.lutece.util.sql.DAOUtil;
55  
56  /**
57   * Abstract entry type for selects that take values from SQL requests
58   */
59  public abstract class AbstractEntryTypeSelectSQL extends EntryTypeService
60  {
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public String getRequestData( Entry entry, HttpServletRequest request, Locale locale )
66      {
67          initCommonRequestData( entry, request );
68          String strTitle = request.getParameter( PARAMETER_TITLE );
69          String strCode = request.getParameter( PARAMETER_ENTRY_CODE );
70          String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
71          String strComment = request.getParameter( PARAMETER_COMMENT );
72          String strMandatory = request.getParameter( PARAMETER_MANDATORY );
73          String strCSSClass = request.getParameter( PARAMETER_CSS_CLASS );
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          // for don't update fields listFields=null
92          entry.setCode( strCode );
93          entry.setTitle( strTitle );
94          entry.setHelpMessage( strHelpMessage );
95          entry.setComment( strComment );
96          entry.setCSSClass( strCSSClass );
97  
98          entry.setMandatory( strMandatory != null );
99  
100         try
101         {
102             getSqlQueryFields( entry );
103         }
104         catch( AppException ae )
105         {
106             String strErrorMsg = ae.getMessage( );
107 
108             if ( ( strErrorMsg != null ) && strErrorMsg.contains( System.getProperty( "line.separator" ) ) )
109             {
110                 strErrorMsg = strErrorMsg.substring( 0, strErrorMsg.indexOf( System.getProperty( "line.separator" ) ) );
111             }
112 
113             Object [ ] tabErrorSQLMsg = {
114                     strErrorMsg
115             };
116 
117             return AdminMessageService.getMessageUrl( request, MESSAGE_INVALID_SQL_QUERY, tabErrorSQLMsg, AdminMessage.TYPE_STOP );
118         }
119 
120         return null;
121     }
122 
123     /**
124      * {@inheritDoc}
125      */
126     @Override
127     public GenericAttributeError getResponseData( Entry entry, HttpServletRequest request, List<Response> listResponse, Locale locale )
128     {
129         String strIdField = request.getParameter( PREFIX_ATTRIBUTE + entry.getIdEntry( ) );
130         int nIdField = -1;
131         Field field = null;
132         Response/genericattributes/business/Response.html#Response">Response response = new Response( );
133         response.setEntry( entry );
134 
135         if ( StringUtils.isNotEmpty( strIdField ) && StringUtils.isNumeric( strIdField ) )
136         {
137             nIdField = Integer.parseInt( strIdField );
138         }
139 
140         if ( nIdField != -1 )
141         {
142             field = GenericAttributesUtils.findFieldByIdInTheList( nIdField, getSqlQueryFields( entry ) );
143         }
144 
145         if ( field != null )
146         {
147             response.setResponseValue( field.getValue( ) );
148             response.setField( field );
149         }
150 
151         response.setIterationNumber( getResponseIterationValue( request ) );
152 
153         listResponse.add( response );
154 
155         if ( entry.isMandatory( ) && ( field == null || StringUtils.isBlank( field.getValue( ) ) ) )
156         {
157             return new MandatoryError( entry, locale );
158         }
159 
160         return null;
161     }
162 
163     /**
164      * {@inheritDoc}
165      */
166     @Override
167     public String getResponseValueForExport( Entry entry, HttpServletRequest request, Response response, Locale locale )
168     {
169         return response.getResponseValue( );
170     }
171 
172     /**
173      * {@inheritDoc}
174      */
175     @Override
176     public String getResponseValueForRecap( Entry entry, HttpServletRequest request, Response response, Locale locale )
177     {
178         return ( response.getField( ) != null ) ? response.getField( ).getTitle( ) : StringUtils.EMPTY;
179     }
180 
181     /**
182      * Return fields from a SQL query
183      * 
184      * @param entry
185      *            The entry
186      * @return A list of fields
187      */
188     protected List<Field> getSqlQueryFields( Entry entry )
189     {
190         List<Field> list = new ArrayList<>( );
191         String strSQL = entry.getComment( );
192 
193         try ( DAOUtil daoUtil = new DAOUtil( strSQL, GenericAttributesUtils.getPlugin( ) ) )
194         {
195             daoUtil.executeQuery( );
196 
197             while ( daoUtil.next( ) )
198             {
199                 Fieldlugins/genericattributes/business/Field.html#Field">Field field = new Field( );
200                 field.setIdField( daoUtil.getInt( 1 ) );
201                 field.setTitle( daoUtil.getString( 2 ) );
202                 field.setValue( field.getTitle( ) );
203                 list.add( field );
204             }
205 
206         }
207 
208         return list;
209     }
210 }