Commit 64dde87d authored by Guillaume Lépine's avatar Guillaume Lépine
Browse files

FORMS-700 : Refacto FormQuestionJspBean

parent 35afc869
/*
* Copyright (c) 2002-2021, City of Paris
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright notice
* and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice
* and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* License 1.0
*/
package fr.paris.lutece.plugins.forms.util;
package fr.paris.lutece.plugins.forms.service;
import java.util.List;
import java.util.Locale;
......@@ -41,50 +8,46 @@ import org.apache.commons.lang3.math.NumberUtils;
import fr.paris.lutece.plugins.forms.business.CompositeDisplayType;
import fr.paris.lutece.plugins.forms.business.FormDisplay;
import fr.paris.lutece.plugins.forms.business.FormDisplayHome;
import fr.paris.lutece.plugins.forms.business.Group;
import fr.paris.lutece.plugins.forms.business.GroupHome;
import fr.paris.lutece.plugins.forms.business.Question;
import fr.paris.lutece.plugins.forms.business.QuestionHome;
import fr.paris.lutece.portal.service.i18n.I18nService;
import fr.paris.lutece.util.ReferenceItem;
import fr.paris.lutece.util.ReferenceList;
/**
* Utility class for plugin Forms
*/
// TODO : Move the methods calling "Home" classes to a another class
public final class FormsDisplayUtils
public abstract class AbstractFormDisplayService implements IFormDisplayService
{
private static final String PROPERTY_POSITION_LABEL = "forms.moveComposite.position.label";
private static final String PROPERTY_ROOT_LEVEL_POSITION_LABEL = "forms.moveComposite.rootLevel.label";
/**
* FormsEntryUtils private constructor. DO not call
*
*/
private FormsDisplayUtils( )
private static final String PROPERTY_POSITION_LABEL = "forms.moveComposite.position.label";
private IFormDatabaseService _formDatabaseService = null;
@Override
public String getDisplayTitle( FormDisplay formDisplayToMove )
{
throw new AssertionError( );
}
String strDisplayTitle = StringUtils.EMPTY;
/**
* Utility Method to retrieve referenceList containing all the Displays of a given Step that could be a potential destination parent for the given
* FormDisplay. The ReferenceList begins with a first root level item, followed by all the relevant children displays of type "Group". If the Display to be
* moved is a group, this Display and its child groups will be excluded from the list
*
* @param nIdStep
* The Step identifier
*
* @param formDisplayToMove
* The Display to move.
*
* @param locale
* The Locale
*
* @return a ReferenceList containing the Groups of the step
*/
public static ReferenceList getTargetGroupDisplayListByStep( int nIdStep, FormDisplay formDisplayToMove, Locale locale )
if ( CompositeDisplayType.GROUP.getLabel( ).equalsIgnoreCase( formDisplayToMove.getCompositeType( ) ) )
{
Group groupToMove = getFormDatabaseService( ).findGroupByPrimaryKey( formDisplayToMove.getCompositeId( ) );
if ( groupToMove != null )
{
strDisplayTitle = groupToMove.getTitle( );
}
}
else
if ( CompositeDisplayType.QUESTION.getLabel( ).equalsIgnoreCase( formDisplayToMove.getCompositeType( ) ) )
{
Question questionToMove = getFormDatabaseService( ).findQuestionByPrimaryKey( formDisplayToMove.getCompositeId( ) );
if ( questionToMove != null )
{
strDisplayTitle = questionToMove.getTitle( );
}
}
return strDisplayTitle;
}
@Override
public ReferenceList getTargetGroupDisplayListByStep( int nIdStep, FormDisplay formDisplayToMove, Locale locale )
{
ReferenceList listGroupDisplay = new ReferenceList( );
......@@ -94,7 +57,7 @@ public final class FormsDisplayUtils
if ( bIsGroup )
{
for ( ReferenceItem display : FormDisplayHome.getGroupDisplayReferenceListByStep( nIdStep ) )
for ( ReferenceItem display : getGroupDisplayReferenceListByStep( nIdStep ) )
{
int nDisplayId = NumberUtils.toInt( display.getCode( ) );
if ( !isDescendantDisplay( nDisplayId, formDisplayToMove ) )
......@@ -105,12 +68,12 @@ public final class FormsDisplayUtils
}
else
{
listGroupDisplay.addAll( FormDisplayHome.getGroupDisplayReferenceListByStep( nIdStep ) );
listGroupDisplay.addAll( getGroupDisplayReferenceListByStep( nIdStep ) );
}
return listGroupDisplay;
}
/**
* Return true if the provided FormDisplay identifier A is a descendant (a child, grandchild, great-grandchild, etc.) of the second FormDisplay parameter
*
......@@ -122,7 +85,7 @@ public final class FormsDisplayUtils
*
* @return true if A is a descendant of B
*/
public static boolean isDescendantDisplay( int nDisplayId, FormDisplay formDisplay )
private boolean isDescendantDisplay( int nDisplayId, FormDisplay formDisplay )
{
if ( nDisplayId == formDisplay.getId( ) )
{
......@@ -131,7 +94,7 @@ public final class FormsDisplayUtils
boolean bIsChild = false;
List<FormDisplay> listFormDisplay = FormDisplayHome.getFormDisplayListByParent( formDisplay.getStepId( ), formDisplay.getId( ) );
List<FormDisplay> listFormDisplay = getFormDisplayListByParent( formDisplay.getStepId( ), formDisplay.getId( ) );
for ( FormDisplay displayChild : listFormDisplay )
{
......@@ -143,61 +106,12 @@ public final class FormsDisplayUtils
}
return bIsChild;
}
/**
* Utility Method to retrieve the Move composite page title from a given FormDisplay
*
* @param formDisplayToMove
* the FormDisplay
*
* @return the admin page title
*/
public static String getDisplayTitle( FormDisplay formDisplayToMove )
{
String strDisplayTitle = StringUtils.EMPTY;
if ( CompositeDisplayType.GROUP.getLabel( ).equalsIgnoreCase( formDisplayToMove.getCompositeType( ) ) )
{
Group groupToMove = GroupHome.findByPrimaryKey( formDisplayToMove.getCompositeId( ) );
if ( groupToMove != null )
{
strDisplayTitle = groupToMove.getTitle( );
}
}
else
if ( CompositeDisplayType.QUESTION.getLabel( ).equalsIgnoreCase( formDisplayToMove.getCompositeType( ) ) )
{
Question questionToMove = QuestionHome.findByPrimaryKey( formDisplayToMove.getCompositeId( ) );
if ( questionToMove != null )
{
strDisplayTitle = questionToMove.getTitle( );
}
}
return strDisplayTitle;
}
/**
* Return a ReferenceList containing all the available positions within a given parent FormDisplay, in a given Step. Includes the position after the last
* element of the group.
*
* @param nIdStep
* the identifier of the Step
*
* @param nIdDisplayGroup
* the identifier of the group DIsplay parent. Zero for the step root level
*
* @param bAddPosition
* if true a position is added at the end of the list
*
* @param locale
* The locale
*
* @return the available positions sorted in a ReferenceList
*/
public static ReferenceList getlistAvailablePositionsInGroup( int nIdStep, int nIdDisplayGroup, boolean bAddPosition, Locale locale )
@Override
public ReferenceList getlistAvailablePositionsInGroup( int nIdStep, int nIdDisplayGroup, boolean bAddPosition, Locale locale )
{
ReferenceList listAvailablePositions = new ReferenceList( );
List<FormDisplay> listDisplay = FormDisplayHome.getFormDisplayListByParent( nIdStep, nIdDisplayGroup );
List<FormDisplay> listDisplay = getFormDisplayListByParent( nIdStep, nIdDisplayGroup );
for ( int i = 1; i <= listDisplay.size( ); i++ )
{
......@@ -211,21 +125,14 @@ public final class FormsDisplayUtils
return listAvailablePositions;
}
/**
* Returns the display depth of a child display element
*
* @param nParentDisplay
* the Identifier of the parent display element (zero if we are at the step root)
*
* @return the display depth
*/
public static int getDisplayDepthFromParent( int nParentDisplay )
@Override
public int getDisplayDepthFromParent( int parentGroup )
{
int nDisplayDepth = 0;
if ( nParentDisplay > 0 )
if ( parentGroup > 0 )
{
FormDisplay formDisplayParent = FormDisplayHome.findByPrimaryKey( nParentDisplay );
FormDisplay formDisplayParent = getFormDatabaseService( ).findDisplayByPrimaryKey( parentGroup );
if ( formDisplayParent != null )
{
nDisplayDepth = formDisplayParent.getDepth( ) + 1;
......@@ -233,4 +140,79 @@ public final class FormsDisplayUtils
}
return nDisplayDepth;
}
@Override
public void setChildrenDisplayDepthAndStep( FormDisplay formDisplayParent, int nDepth, int nIdStep )
{
int nOldIdStep = formDisplayParent.getStepId( );
String strCompositeType = formDisplayParent.getCompositeType( );
formDisplayParent.setStepId( nIdStep );
formDisplayParent.setDepth( nDepth );
getFormDatabaseService( ).updateFormDisplay( formDisplayParent );;
if ( CompositeDisplayType.QUESTION.getLabel( ).equalsIgnoreCase( strCompositeType ) )
{
Question question = getFormDatabaseService( ).findQuestionByPrimaryKey( formDisplayParent.getCompositeId( ) );
question.setIdStep( nIdStep );
getFormDatabaseService( ).updateQuestion( question );
}
if ( CompositeDisplayType.GROUP.getLabel( ).equalsIgnoreCase( strCompositeType ) )
{
Group group = getFormDatabaseService( ).findGroupByPrimaryKey( formDisplayParent.getCompositeId( ) );
group.setIdStep( nIdStep );
getFormDatabaseService( ).updateGroup( group );
}
List<FormDisplay> listChildDisplay = getFormDatabaseService( ).getFormDisplayListByParent( nOldIdStep, formDisplayParent.getId( ) );
for ( FormDisplay childDisplay : listChildDisplay )
{
setChildrenDisplayDepthAndStep( childDisplay, formDisplayParent.getDepth( ) + 1, nIdStep );
}
}
@Override
public void rebuildDisplayPositionSequence( List<FormDisplay> listDisplay )
{
int nUpdatedPosition = 0;
for ( FormDisplay displayToUpdate : listDisplay )
{
nUpdatedPosition++;
displayToUpdate.setDisplayOrder( nUpdatedPosition );
getFormDatabaseService( ).updateFormDisplay( displayToUpdate );
}
}
/**
* Load the data of all the FormDisplay objects of type "Group" linked to a given FormStep and returns them as a referenceList
*
* @param nIdStep
* the step identifier
* @return the referenceList which contains the data of all the Display objects of group type
*/
protected abstract ReferenceList getGroupDisplayReferenceListByStep( int nIdStep );
/**
* Load the data of all the formDisplay that are direct children of a given parent and returns them as a list
*
* @param nIdStep
* The step primary key
* @param nIdParent
* The parent primary key
* @return the list which contains the data of all the formDisplay objects by parent
*/
protected abstract List<FormDisplay> getFormDisplayListByParent( int nIdStep, int nIdParent );
protected IFormDatabaseService getFormDatabaseService( )
{
if ( _formDatabaseService == null )
{
_formDatabaseService = initFormDatabaseService( );
}
return _formDatabaseService;
}
protected abstract IFormDatabaseService initFormDatabaseService( );
}
package fr.paris.lutece.plugins.forms.service;
import java.util.List;
import fr.paris.lutece.plugins.forms.business.Control;
import fr.paris.lutece.plugins.forms.business.ControlHome;
import fr.paris.lutece.plugins.forms.business.FormDisplay;
import fr.paris.lutece.plugins.forms.business.FormDisplayHome;
import fr.paris.lutece.plugins.forms.business.Group;
import fr.paris.lutece.plugins.forms.business.GroupHome;
import fr.paris.lutece.plugins.forms.business.Question;
import fr.paris.lutece.plugins.forms.business.QuestionHome;
import fr.paris.lutece.plugins.forms.business.Step;
import fr.paris.lutece.plugins.forms.business.StepHome;
import fr.paris.lutece.plugins.genericattributes.business.Entry;
import fr.paris.lutece.plugins.genericattributes.business.EntryHome;
import fr.paris.lutece.plugins.genericattributes.business.Field;
import fr.paris.lutece.plugins.genericattributes.business.FieldHome;
public class FormDatabaseService implements IFormDatabaseService
{
public static final String BEAN_NAME = "forms.formDatabaseService";
@Override
public void createEntry( Entry entry )
{
EntryHome.create( entry );
}
@Override
public void createField( Field field )
{
FieldHome.create( field );
}
@Override
public void createQuestion( Question question )
{
QuestionHome.create( question );
}
@Override
public void createGroup( Group group )
{
GroupHome.create( group );
}
@Override
public void updateGroup( Group group )
{
GroupHome.update( group );
}
@Override
public Group findGroupByPrimaryKey( int idGroup )
{
return GroupHome.findByPrimaryKey( idGroup );
}
@Override
public Step findStepByPrimaryKey( int idStep )
{
return StepHome.findByPrimaryKey( idStep );
}
@Override
public void updateEntry( Entry entry )
{
EntryHome.update( entry );
}
@Override
public void createFormDisplay( FormDisplay formDisplay )
{
FormDisplayHome.create( formDisplay );
}
@Override
public List<Question> getListQuestionByForm( int nIdForm )
{
return QuestionHome.getListQuestionByIdForm( nIdForm );
}
@Override
public Question findQuestionByPrimaryKey( int idQuestion )
{
return QuestionHome.findByPrimaryKey( idQuestion );
}
@Override
public Entry findEntryByPrimaryKey( int idEntry )
{
return EntryHome.findByPrimaryKey( idEntry );
}
@Override
public void updateField( Field field )
{
FieldHome.update( field );
}
@Override
public Field findFieldByPrimaryKey( int idField )
{
return FieldHome.findByPrimaryKey( idField );
}
@Override
public FormDisplay findDisplayByPrimaryKey( int idDisplay )
{
return FormDisplayHome.findByPrimaryKey( idDisplay );
}
@Override
public void updateQuestion( Question question )
{
QuestionHome.update( question );
}
@Override
public List<FormDisplay> getFormDisplayListByParent( int nIdStep, int nIdParent )
{
return FormDisplayHome.getFormDisplayListByParent( nIdStep, nIdParent );
}
@Override
public void updateFormDisplay( FormDisplay formDisplay )
{
FormDisplayHome.update( formDisplay );
}
@Override
public Entry copyEntry( Entry entry )
{
return EntryHome.copy( entry );
}
@Override
public List<Control> getControlByQuestion( int nIdQuestion )
{
return ControlHome.getControlByQuestion( nIdQuestion );
}
@Override
public FormDisplay getFormDisplayByFormStepAndComposite( int nIdForm, int nIdStep, int nIdComposite )
{
return FormDisplayHome.getFormDisplayByFormStepAndComposite( nIdForm, nIdStep, nIdComposite );
}
@Override
public void createControl( Control control )
{
ControlHome.create( control );
}
}
......@@ -42,26 +42,19 @@ import fr.paris.lutece.plugins.forms.business.ControlType;
import fr.paris.lutece.plugins.forms.business.FormDisplay;
import fr.paris.lutece.plugins.forms.business.FormDisplayHome;
import fr.paris.lutece.plugins.forms.business.FormQuestionResponseHome;
import fr.paris.lutece.plugins.forms.business.Group;
import fr.paris.lutece.plugins.forms.business.GroupHome;
import fr.paris.lutece.plugins.forms.business.Question;
import fr.paris.lutece.plugins.forms.business.QuestionHome;
import fr.paris.lutece.portal.service.spring.SpringContextService;
import fr.paris.lutece.util.ReferenceList;
/**
* Service dedicated to management of formDisplay
*/
public final class FormDisplayService
public class FormDisplayService extends AbstractFormDisplayService
{
public static final String BEAN_NAME = "forms.formDisplayService";
/**
* Constructor
*/
private FormDisplayService( )
{
}
/**
* Remove the formDisplay whose identifier is specified in parameter. The responses, group/question associated to this display will be deleted. All the
* descendants of the display will also be removed
......@@ -118,67 +111,21 @@ public final class FormDisplayService
}
/**
* Rebuild the position sequence of a given FormDisplay list and update the objects in database. The list indexes will be used to set the displayOrder
* values.
*
* @param listDisplay
* The List of FormDisplay to update
*/
public void rebuildDisplayPositionSequence( List<FormDisplay> listDisplay )
@Override
protected List<FormDisplay> getFormDisplayListByParent( int nIdStep, int nIdParent )
{
int nUpdatedPosition = 0;
for ( FormDisplay displayToUpdate : listDisplay )
{
nUpdatedPosition++;
displayToUpdate.setDisplayOrder( nUpdatedPosition );
FormDisplayHome.update( displayToUpdate );
}
return FormDisplayHome.getFormDisplayListByParent( nIdStep, nIdParent );
}
/**
* Update a given FormDisplay, and all its descendants by setting the provided depth and idStep values. Value of a children Depth is parent depth plus one.
* Value of a children idStep is same as parent idStep.
*
* @param formDisplayParent
* the parent Display
*
* @param nDepth
* the parent depth value to set
*
* @param nIdStep
* the parent idStep value to set
*
*/
public void setChildrenDisplayDepthAndStep( FormDisplay formDisplayParent, int nDepth, int nIdStep )
@Override
protected ReferenceList getGroupDisplayReferenceListByStep( int nIdStep )
{
int nOldIdStep = formDisplayParent.getStepId( );
String strCompositeType = formDisplayParent.getCompositeType( );
formDisplayParent.setStepId( nIdStep );
formDisplayParent.setDepth( nDepth );
FormDisplayHome.update( formDisplayParent );
if ( CompositeDisplayType.QUESTION.getLabel( ).equalsIgnoreCase( strCompositeType ) )
{
Question question = QuestionHome.findByPrimaryKey( formDisplayParent.getCompositeId( ) );
question.setIdStep( nIdStep );
QuestionHome.update( question );
}
if ( CompositeDisplayType.GROUP.getLabel( ).equalsIgnoreCase( strCompositeType ) )
{
Group group = GroupHome.findByPrimaryKey( formDisplayParent.getCompositeId( ) );
group.setIdStep( nIdStep );
GroupHome.update( group );
}
List<FormDisplay> listChildDisplay = FormDisplayHome.getFormDisplayListByParent( nOldIdStep, formDisplayParent.getId( ) );
for ( FormDisplay childDisplay : listChildDisplay )
{
setChildrenDisplayDepthAndStep( childDisplay, formDisplayParent.getDepth( ) + 1, nIdStep );
}
return FormDisplayHome.getGroupDisplayReferenceListByStep( nIdStep );
}