From 9bf9d5f39b2acd50b22f24ca2468ee7ed231ccf5 Mon Sep 17 00:00:00 2001 From: Anas BENJELLOUN Date: Tue, 25 Mar 2025 16:41:31 +0100 Subject: [PATCH] LUT-29523 A form with a single step generates an error on submission --- .../plugins/forms/business/IStepDAO.java | 11 ++++++++++ .../plugins/forms/business/StepDAO.java | 21 +++++++++++++++++++ .../plugins/forms/business/StepHome.java | 19 +++++++++++++++++ .../forms/resources/forms_messages.properties | 3 ++- .../resources/forms_messages_fr.properties | 4 +++- .../service/cache/FormsCacheService.java | 5 +++++ .../plugins/forms/util/FormsConstants.java | 1 + .../lutece/plugins/forms/web/FormXPage.java | 19 ++++++++++++++++- .../forms/web/admin/FormResponseJspBean.java | 17 +++++++++++++++ 9 files changed, 97 insertions(+), 3 deletions(-) diff --git a/src/java/fr/paris/lutece/plugins/forms/business/IStepDAO.java b/src/java/fr/paris/lutece/plugins/forms/business/IStepDAO.java index b0056d2a7..3cf20693a 100644 --- a/src/java/fr/paris/lutece/plugins/forms/business/IStepDAO.java +++ b/src/java/fr/paris/lutece/plugins/forms/business/IStepDAO.java @@ -97,6 +97,17 @@ public interface IStepDAO */ Step selectInitialStep( int nIdForm, Plugin plugin ); + /** + * Load the final step of the given form + * + * @param nIdForm + * The identifier of the step form + * @param plugin + * the Plugin + * @return The final step of the given form + */ + Step selectFinalStep( int nIdForm, Plugin plugin ); + /** * Load the data of all the step objects and returns them as a list * diff --git a/src/java/fr/paris/lutece/plugins/forms/business/StepDAO.java b/src/java/fr/paris/lutece/plugins/forms/business/StepDAO.java index 05371abc2..e8e00316f 100644 --- a/src/java/fr/paris/lutece/plugins/forms/business/StepDAO.java +++ b/src/java/fr/paris/lutece/plugins/forms/business/StepDAO.java @@ -50,6 +50,7 @@ public final class StepDAO implements IStepDAO private static final String SQL_QUERY_SELECTALL = "SELECT id_step, title, description, id_form, is_initial, is_final FROM forms_step"; private static final String SQL_QUERY_SELECT = SQL_QUERY_SELECTALL + " WHERE id_step = ?"; private static final String SQL_QUERY_SELECT_INITIAL_STEP = SQL_QUERY_SELECTALL + " WHERE id_form = ? AND is_initial = 1"; + private static final String SQL_QUERY_SELECT_FINAL_STEP = SQL_QUERY_SELECTALL + " WHERE id_form = ? AND is_final = 1"; private static final String SQL_QUERY_INSERT = "INSERT INTO forms_step ( title, description, id_form, is_initial, is_final ) VALUES ( ?, ?, ?, ?, ? ) "; private static final String SQL_QUERY_DELETE = "DELETE FROM forms_step WHERE id_step = ? "; private static final String SQL_QUERY_UPDATE = "UPDATE forms_step SET id_step = ?, title = ?, description = ?, id_form = ? ,is_initial = ?, is_final = ? WHERE id_step = ?"; @@ -120,6 +121,26 @@ public Step selectInitialStep( int nIdForm, Plugin plugin ) return step; } + /** + * {@inheritDoc } + */ + @Override + public Step selectFinalStep( int nIdForm, Plugin plugin ) + { + Step step = null; + try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_FINAL_STEP, plugin ) ) + { + daoUtil.setInt( 1, nIdForm ); + daoUtil.executeQuery( ); + + if ( daoUtil.next( ) ) + { + step = dataToObject( daoUtil ); + } + } + return step; + } + /** * {@inheritDoc } */ diff --git a/src/java/fr/paris/lutece/plugins/forms/business/StepHome.java b/src/java/fr/paris/lutece/plugins/forms/business/StepHome.java index 1ebfbfa16..b376f7627 100644 --- a/src/java/fr/paris/lutece/plugins/forms/business/StepHome.java +++ b/src/java/fr/paris/lutece/plugins/forms/business/StepHome.java @@ -136,6 +136,25 @@ public static Step getInitialStep( int nIdForm ) return step; } + /** + * Returns the initial step of the given form id + * + * @param nIdForm + * The step form primary key + * @return the initial step of the given form id + */ + public static Step getFinalStep( int nIdForm ) + { + String stepCacheKey = _cache.getFinalStepCacheKey( nIdForm ); + Step step = (Step) _cache.getFromCache( stepCacheKey ); + if ( step == null ) + { + step = _dao.selectFinalStep( nIdForm, _plugin ); + _cache.putInCache( stepCacheKey, step ); + } + return step; + } + /** * Load the data of all the step objects and returns them as a list * diff --git a/src/java/fr/paris/lutece/plugins/forms/resources/forms_messages.properties b/src/java/fr/paris/lutece/plugins/forms/resources/forms_messages.properties index 60bab51a0..22cd1e00d 100644 --- a/src/java/fr/paris/lutece/plugins/forms/resources/forms_messages.properties +++ b/src/java/fr/paris/lutece/plugins/forms/resources/forms_messages.properties @@ -922,6 +922,7 @@ error.step.isnot.final=The current step is not final. error.form.inactive=Form inactive error.form.MaxResponse=Form unavailable because maximum number of responses reached error.form.noStep=No content available +error.form.noFinalStep=No final step found warning.deleteComposite.confirmRemoveActiveForm=The form {0} is currently active. warning.deleteComposite.confirmRemoveQuestion=Do you really want to delete the question "{0}" and all its associated responses? @@ -1055,4 +1056,4 @@ error_form_response.pageTitle=Error # Portlet portlet.formsList.name=List of forms portlet.formsList.titleAvailableFormsList=Available forms -portlet.formsList.titlePublishedFormsList=Published forms \ No newline at end of file +portlet.formsList.titlePublishedFormsList=Published forms diff --git a/src/java/fr/paris/lutece/plugins/forms/resources/forms_messages_fr.properties b/src/java/fr/paris/lutece/plugins/forms/resources/forms_messages_fr.properties index ab2d38583..d6e065696 100644 --- a/src/java/fr/paris/lutece/plugins/forms/resources/forms_messages_fr.properties +++ b/src/java/fr/paris/lutece/plugins/forms/resources/forms_messages_fr.properties @@ -925,6 +925,8 @@ error.step.isnot.final=L'\u00e9tape courante n'est pas finale. error.form.inactive=Formulaire indisponible error.form.MaxResponse=Formulaire indisponible car nombre de reponses est atteint error.form.noStep=Pas de contenu disponible +error.form.noFinalStep=Aucune \u00e9tape finale trouv\u00e9e + warning.deleteComposite.confirmRemoveActiveForm=Le formulaire {0} est actuellement activ\u00e9. warning.deleteComposite.confirmRemoveQuestion=Voulez-vous vraiment supprimer la question "{0}" et toutes ses r\u00e9ponses associ\u00e9es ? @@ -1060,4 +1062,4 @@ error_form_response.pageTitle=Erreur # Portlet portlet.formsList.name=Liste de formulaires portlet.formsList.titleAvailableFormsList=Formulaires disponibles -portlet.formsList.titlePublishedFormsList=Formulaires publi\u00e9s \ No newline at end of file +portlet.formsList.titlePublishedFormsList=Formulaires publi\u00e9s diff --git a/src/java/fr/paris/lutece/plugins/forms/service/cache/FormsCacheService.java b/src/java/fr/paris/lutece/plugins/forms/service/cache/FormsCacheService.java index ce9542af6..dcb4595b8 100644 --- a/src/java/fr/paris/lutece/plugins/forms/service/cache/FormsCacheService.java +++ b/src/java/fr/paris/lutece/plugins/forms/service/cache/FormsCacheService.java @@ -68,6 +68,11 @@ public String getInitialStepCacheKey( int nIdForm ) return new StringBuilder( "Initial-Step-For-Form-id:" ).append( nIdForm ).toString( ); } + public String getFinalStepCacheKey( int nIdForm ) + { + return new StringBuilder( "Final-Step-For-Form-id:" ).append( nIdForm ).toString( ); + } + public String getFormCacheKey( int nIdForm ) { return new StringBuilder( "Form-id:" ).append( nIdForm ).toString( ); diff --git a/src/java/fr/paris/lutece/plugins/forms/util/FormsConstants.java b/src/java/fr/paris/lutece/plugins/forms/util/FormsConstants.java index c9877b05a..5310a3fd2 100644 --- a/src/java/fr/paris/lutece/plugins/forms/util/FormsConstants.java +++ b/src/java/fr/paris/lutece/plugins/forms/util/FormsConstants.java @@ -190,6 +190,7 @@ public final class FormsConstants public static final String MESSAGE_LOAD_BACKUP = "forms.message.view.loadBackUp"; public static final String MESSAGE_SUMMARY_TITLE = "forms.summary.title"; public static final String MESSAGE_ERROR_NO_STEP = "forms.error.form.noStep"; + public static final String MESSAGE_ERROR_NO_FINAL_STEP = "forms.error.form.noFinalStep"; // Jsp diff --git a/src/java/fr/paris/lutece/plugins/forms/web/FormXPage.java b/src/java/fr/paris/lutece/plugins/forms/web/FormXPage.java index ef07764de..4bc588d31 100644 --- a/src/java/fr/paris/lutece/plugins/forms/web/FormXPage.java +++ b/src/java/fr/paris/lutece/plugins/forms/web/FormXPage.java @@ -291,6 +291,12 @@ public synchronized XPage getStepView( HttpServletRequest request ) throws SiteM return null; } + if ( !isFinalStepExist( nIdForm ) ) + { + SiteMessageService.setMessage( request, FormsConstants.MESSAGE_ERROR_NO_FINAL_STEP, SiteMessage.TYPE_ERROR ); + return null; + } + Form form = FormHome.findByPrimaryKey( _currentStep.getIdForm( ) ); checkAuthentication( form, request ); @@ -1394,7 +1400,7 @@ private void init( int nIdForm ) } /** - * ckeck if the session has expired + * check if the session has expired * * */ @@ -1403,4 +1409,15 @@ private boolean isSessionLost( ) return ( _currentStep == null && _formResponseManager == null && _stepDisplayTree == null && _breadcrumb == null ); } + /** + * check if the final step exist + * + * @param nIdForm + * id form + */ + private boolean isFinalStepExist( int nIdForm ) + { + return StepHome.getFinalStep(nIdForm) != null; + } + } diff --git a/src/java/fr/paris/lutece/plugins/forms/web/admin/FormResponseJspBean.java b/src/java/fr/paris/lutece/plugins/forms/web/admin/FormResponseJspBean.java index 566eb66ea..66c1a46cb 100644 --- a/src/java/fr/paris/lutece/plugins/forms/web/admin/FormResponseJspBean.java +++ b/src/java/fr/paris/lutece/plugins/forms/web/admin/FormResponseJspBean.java @@ -147,6 +147,12 @@ public String getStepView( HttpServletRequest request ) return redirectView(request, VIEW_ERROR ); } + if ( !isFinalStepExist( nIdForm ) ) + { + addError( FormsConstants.MESSAGE_ERROR_NO_FINAL_STEP, getLocale( ) ); + return redirectView(request, VIEW_ERROR ); + } + Form form = FormHome.findByPrimaryKey( _currentStep.getIdForm( ) ); if ( !(FormsResponseUtils.checkNumberMaxResponseForm( form ) ) ) { @@ -747,4 +753,15 @@ private String getBackUrl( Form form, boolean bIsEndMessageDisplayed ) return url.getUrl( ); } } + + /** + * check if the final step exist + * + * @param nIdForm + * id form + */ + private boolean isFinalStepExist( int nIdForm ) + { + return StepHome.getFinalStep(nIdForm) != null; + } }