Skip to content

Commit

Permalink
LUT-26831: simplify get response from backup
Browse files Browse the repository at this point in the history
  • Loading branch information
TimotheeHrl committed Nov 22, 2023
1 parent 4b64abb commit b0314c4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 111 deletions.
49 changes: 11 additions & 38 deletions src/java/fr/paris/lutece/plugins/forms/service/FormService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,51 +33,18 @@
*/
package fr.paris.lutece.plugins.forms.service;

import java.util.List;
import java.util.stream.Collectors;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;

import fr.paris.lutece.api.user.User;
import fr.paris.lutece.plugins.forms.business.Form;
import fr.paris.lutece.plugins.forms.business.FormDisplay;
import fr.paris.lutece.plugins.forms.business.FormHome;
import fr.paris.lutece.plugins.forms.business.FormMessageHome;
import fr.paris.lutece.plugins.forms.business.FormQuestionResponse;
import fr.paris.lutece.plugins.forms.business.FormQuestionResponseHome;
import fr.paris.lutece.plugins.forms.business.FormResponse;
import fr.paris.lutece.plugins.forms.business.FormResponseHome;
import fr.paris.lutece.plugins.forms.business.FormResponseStep;
import fr.paris.lutece.plugins.forms.business.FormResponseStepHome;
import fr.paris.lutece.plugins.forms.business.Question;
import fr.paris.lutece.plugins.forms.business.Step;
import fr.paris.lutece.plugins.forms.business.StepHome;
import fr.paris.lutece.plugins.forms.business.*;
import fr.paris.lutece.plugins.forms.business.export.FormExportConfigHome;
import fr.paris.lutece.plugins.forms.exception.MaxFormResponseException;
import fr.paris.lutece.plugins.forms.service.workflow.IFormWorkflowService;
import fr.paris.lutece.plugins.forms.util.FormsConstants;
import fr.paris.lutece.plugins.forms.util.FormsResponseUtils;
import fr.paris.lutece.plugins.forms.web.CompositeGroupDisplay;
import fr.paris.lutece.plugins.forms.web.CompositeQuestionDisplay;
import fr.paris.lutece.plugins.forms.web.FormResponseManager;
import fr.paris.lutece.plugins.forms.web.ICompositeDisplay;
import fr.paris.lutece.plugins.forms.web.StepDisplayTree;
import fr.paris.lutece.plugins.forms.web.*;
import fr.paris.lutece.plugins.forms.web.admin.MultiviewFormResponseDetailsJspBean;
import fr.paris.lutece.plugins.forms.web.entrytype.IEntryDataService;
import fr.paris.lutece.plugins.genericattributes.business.Entry;
import fr.paris.lutece.plugins.genericattributes.business.EntryHome;
import fr.paris.lutece.plugins.genericattributes.business.FieldHome;
import fr.paris.lutece.plugins.genericattributes.business.Response;
import fr.paris.lutece.plugins.genericattributes.business.ResponseHome;
import fr.paris.lutece.plugins.genericattributes.service.entrytype.AbstractEntryTypeFile;
import fr.paris.lutece.plugins.genericattributes.service.entrytype.AbstractEntryTypeGalleryImage;
import fr.paris.lutece.plugins.genericattributes.service.entrytype.AbstractEntryTypeImage;
import fr.paris.lutece.plugins.genericattributes.service.entrytype.EntryTypeServiceManager;
import fr.paris.lutece.plugins.genericattributes.service.entrytype.IEntryTypeService;
import fr.paris.lutece.plugins.genericattributes.business.*;
import fr.paris.lutece.plugins.genericattributes.service.entrytype.*;
import fr.paris.lutece.plugins.genericattributes.util.GenericAttributesUtils;
import fr.paris.lutece.portal.business.event.ResourceEvent;
import fr.paris.lutece.portal.business.file.FileHome;
Expand All @@ -88,6 +55,13 @@
import fr.paris.lutece.portal.service.spring.SpringContextService;
import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
import fr.paris.lutece.util.sql.TransactionManager;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
* This is the service class related to the form
Expand Down Expand Up @@ -474,7 +448,6 @@ public FormResponseManager createFormResponseManagerFromBackUp( Form form, Strin
if ( CollectionUtils.isNotEmpty( listFormResponse ) )
{
formResponseManager = new FormResponseManager( listFormResponse.get( 0 ) );
formResponseManager.setIsResponseLoadedFromBackup(true);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@
*/
package fr.paris.lutece.plugins.forms.web;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import fr.paris.lutece.plugins.forms.business.*;
import fr.paris.lutece.portal.service.util.AppLogService;
import org.apache.commons.collections.CollectionUtils;

import fr.paris.lutece.plugins.forms.service.EntryServiceManager;
import fr.paris.lutece.plugins.forms.util.FormsConstants;
import fr.paris.lutece.plugins.forms.validation.IValidator;
import fr.paris.lutece.plugins.genericattributes.business.GenericAttributeError;
import fr.paris.lutece.portal.service.util.AppLogService;
import org.apache.commons.collections.CollectionUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
*
Expand All @@ -55,8 +55,6 @@ public class FormResponseManager
{
private final List<Step> _listValidatedStep;
private final FormResponse _formResponse;
private boolean _bIsResponseLoadedFromBackup = false;
private boolean _isBackupResponseAlreadyInitiated = false;
/**
* Constructor
*
Expand Down Expand Up @@ -153,35 +151,11 @@ public Timestamp getFormResponseUpdateDate()
}
return updateDate;
}

public void setFormResponseUpdateDate(Timestamp updateDate)
{
FormResponse formResponse = getFormResponse();
formResponse.setUpdate(updateDate);
}
/**
* Give a boolean indicating that indicates if view (getViewStep) has been initialized from backup
* So with _isBackupResponseAlreadyInitiated and _bIsResponseLoadedFromBackup we can deduce if it's the first time the getViewStep is loaded with the backup response
*
* @return a boolean indicating that indicates if view has been initialized from backup
*/
public Boolean getIsBackupResponseAlreadyInitiated() {
return _isBackupResponseAlreadyInitiated;
}
public void setBackupResponseAlreadyInitiated(Boolean isBackupResponseAlreadyInitiated) {
_isBackupResponseAlreadyInitiated = isBackupResponseAlreadyInitiated;
}
/**
* Gives a boolean indicating if the response is loaded from backup
*
* @return a boolean indicating if the response is loaded from backup
*/
public Boolean getIsResponseLoadedFromBackup () {
return _bIsResponseLoadedFromBackup;
}
public void setIsResponseLoadedFromBackup (Boolean bIsResponseLoadedFromBackup) {
_bIsResponseLoadedFromBackup = bIsResponseLoadedFromBackup;
}

/**
* Initializes the steps order
Expand Down
68 changes: 28 additions & 40 deletions src/java/fr/paris/lutece/plugins/forms/web/FormXPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,7 @@
*/
package fr.paris.lutece.plugins.forms.web;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import fr.paris.lutece.plugins.forms.business.*;
import fr.paris.lutece.plugins.genericattributes.business.ResponseHome;
import fr.paris.lutece.portal.service.util.AppLogService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import fr.paris.lutece.plugins.forms.exception.FormNotFoundException;
import fr.paris.lutece.plugins.forms.exception.MaxFormResponseException;
import fr.paris.lutece.plugins.forms.exception.QuestionValidationException;
Expand Down Expand Up @@ -77,6 +64,7 @@
import fr.paris.lutece.portal.service.security.SecurityTokenService;
import fr.paris.lutece.portal.service.security.UserNotSignedException;
import fr.paris.lutece.portal.service.spring.SpringContextService;
import fr.paris.lutece.portal.service.util.AppLogService;
import fr.paris.lutece.portal.service.util.AppPathService;
import fr.paris.lutece.portal.service.util.AppPropertiesService;
import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
Expand All @@ -87,7 +75,15 @@
import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
import fr.paris.lutece.portal.web.xpages.XPage;
import fr.paris.lutece.util.url.UrlItem;
import org.apache.james.mime4j.dom.datetime.DateTime;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
*
Expand Down Expand Up @@ -143,7 +139,7 @@ public class FormXPage extends MVCApplication
private static final String MARK_FORM_LIST = "form_list";
private static final String MARK_DISPLAY_CAPTCHA = "display_captcha";
private static final String MARK_CAPTCHA = "captcha";

private static final String AUTH_PROVIDER = "auth_provider";
// Other
private static FormService _formService = SpringContextService.getBean( FormService.BEAN_NAME );
private ICaptchaSecurityService _captchaSecurityService = new CaptchaSecurityService( );
Expand All @@ -153,7 +149,8 @@ public class FormXPage extends MVCApplication
private StepDisplayTree _stepDisplayTree;
private IBreadcrumb _breadcrumb;
private boolean _bInactiveStateBypassed;
private boolean IsRequestComingFromAction = false;
private Boolean isRequestFromLoginPage = false;


/**
* Return the default XPage with the list of all available Form
Expand Down Expand Up @@ -312,9 +309,6 @@ public synchronized XPage getStepView( HttpServletRequest request ) throws SiteM
{
_breadcrumb = SpringContextService.getBean( form.getBreadcrumbName( ) );
}
if(_formResponseManager != null && !_formResponseManager.getIsBackupResponseAlreadyInitiated() && _formResponseManager.getIsResponseLoadedFromBackup()) {
_formResponseManager.setBackupResponseAlreadyInitiated(true);
}
initFormResponseManager( request, form );
if ( _formResponseManager.getFormResponse( ).isFromSave( ) )
{
Expand Down Expand Up @@ -361,7 +355,6 @@ public synchronized XPage getStepView( HttpServletRequest request ) throws SiteM
SiteMessageService.setMessage( request, FormsConstants.MESSAGE_ERROR_INACTIVE_FORM, SiteMessage.TYPE_ERROR );
}
}
IsRequestComingFromAction = true;
XPage xPage = getXPage( TEMPLATE_VIEW_STEP, getLocale( request ), model );
xPage.setTitle( strTitleForm );
xPage.setPathLabel( strPathForm );
Expand Down Expand Up @@ -451,7 +444,6 @@ private void getFormStepModel( Form form, HttpServletRequest request, Map<String
@Action( value = ACTION_PREVIOUS_STEP )
public synchronized XPage doReturnStep( HttpServletRequest request ) throws SiteMessageException, UserNotSignedException
{
IsRequestComingFromAction = true;
boolean bSessionLost = isSessionLost( );
try
{
Expand All @@ -471,8 +463,7 @@ public synchronized XPage doReturnStep( HttpServletRequest request ) throws Site
{
// The condition below : We don't want to fill the FormResponseManager when just logged in with response made when user wasn't logged in
//for exemple in case you are at step 2, you log in and you have to go back to step 1 that you already saved in backup
if(_formResponseManager.getIsBackupResponseAlreadyInitiated() && _formResponseManager.getIsResponseLoadedFromBackup()
|| !_formResponseManager.getIsResponseLoadedFromBackup() && _formResponseManager.getFormResponse().getCreation() != null) {
if(_formResponseManager.getFormResponse().getCreation() != null) {
FormsResponseUtils.fillResponseManagerWithResponses(request, false, _formResponseManager, _stepDisplayTree.getQuestions(), false);
}
}
Expand Down Expand Up @@ -504,7 +495,6 @@ public synchronized XPage doReturnStep( HttpServletRequest request ) throws Site
@Action( value = ACTION_GO_TO_STEP )
public synchronized XPage doGoToStep( HttpServletRequest request ) throws SiteMessageException, UserNotSignedException
{
IsRequestComingFromAction = true;

boolean bSessionLost = isSessionLost( );

Expand Down Expand Up @@ -554,7 +544,6 @@ public synchronized XPage doGoToStep( HttpServletRequest request ) throws SiteMe
@Action( value = ACTION_FORM_RESPONSE_SUMMARY )
public synchronized XPage doFormResponseSummary( HttpServletRequest request ) throws SiteMessageException, UserNotSignedException
{
IsRequestComingFromAction = true;
Form form = null;
try
{
Expand Down Expand Up @@ -649,7 +638,6 @@ private Map<String, Object> buildModelForSummary( Form form, HttpServletRequest
@Action( value = ACTION_SAVE_FORM_RESPONSE )
public synchronized XPage doSaveFormResponse( HttpServletRequest request ) throws SiteMessageException, UserNotSignedException, AccessDeniedException
{
IsRequestComingFromAction = true;
// CSRF Token control
if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_SAVE_FORM_RESPONSE ) )
{
Expand Down Expand Up @@ -696,7 +684,6 @@ public synchronized XPage doSaveFormResponse( HttpServletRequest request ) throw
@Action( value = ACTION_SAVE_FORM_RESPONSE_SUMMARY )
public synchronized XPage doSaveFormResponseSummary( HttpServletRequest request ) throws SiteMessageException, UserNotSignedException, AccessDeniedException
{
IsRequestComingFromAction = true;
// CSRF Token control
if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_SAVE_FORM_RESPONSE ) )
{
Expand Down Expand Up @@ -936,7 +923,6 @@ public synchronized XPage doSaveStep( HttpServletRequest request ) throws SiteMe
errorList.stream( ).collect( Collectors.joining( ) )
}, null, null, null, SiteMessage.TYPE_ERROR, null, getViewFullUrl( VIEW_STEP ) );
}
IsRequestComingFromAction = true;
return getStepView( request );
}

Expand Down Expand Up @@ -964,7 +950,6 @@ private boolean isCaptchaKO( HttpServletRequest request, boolean needValidation
@Action( value = ACTION_SAVE_FOR_BACKUP )
public synchronized XPage doSaveForBackup( HttpServletRequest request ) throws SiteMessageException, UserNotSignedException, AccessDeniedException
{
IsRequestComingFromAction = true;
// CSRF Token control
if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_SAVE_FORM_RESPONSE ) )
{
Expand Down Expand Up @@ -1012,7 +997,6 @@ public synchronized XPage doSaveForBackup( HttpServletRequest request ) throws S

_formService.saveFormForBackup( formResponse );
_formResponseManager.setFormResponseUpdateDate( formResponse.getUpdateStatus( ) );
_formResponseManager.setIsResponseLoadedFromBackup(false);
_formService.saveFormForBackup(formResponse);
}
return getStepView( request );
Expand All @@ -1034,7 +1018,6 @@ public synchronized XPage doSaveForBackup( HttpServletRequest request ) throws S
@Action( value = ACTION_RESET_BACKUP )
public synchronized XPage doResetBackup( HttpServletRequest request ) throws SiteMessageException, UserNotSignedException, AccessDeniedException
{
IsRequestComingFromAction = true;
// CSRF Token control
if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_SAVE_FORM_RESPONSE ) )
{
Expand Down Expand Up @@ -1078,8 +1061,6 @@ public synchronized XPage doResetBackup( HttpServletRequest request ) throws Sit
@Action( value = ACTION_ADD_ITERATION )
public synchronized XPage doAddIteration( HttpServletRequest request ) throws SiteMessageException, UserNotSignedException
{
IsRequestComingFromAction = true;

try
{
boolean bSessionLost = isSessionLost( );
Expand Down Expand Up @@ -1122,8 +1103,6 @@ public synchronized XPage doAddIteration( HttpServletRequest request ) throws Si
@Action( value = ACTION_REMOVE_ITERATION )
public synchronized XPage doRemoveIteration( HttpServletRequest request ) throws SiteMessageException, UserNotSignedException
{
IsRequestComingFromAction = true;

try
{
boolean bSessionLost = isSessionLost( );
Expand Down Expand Up @@ -1166,7 +1145,6 @@ public synchronized XPage doRemoveIteration( HttpServletRequest request ) throws
@Action( value = ACTION_UPLOAD )
public synchronized XPage doSynchronousUploadDocument( HttpServletRequest request ) throws SiteMessageException, UserNotSignedException
{
IsRequestComingFromAction = true;

boolean bSessionLost = isSessionLost( );
if ( bSessionLost )
Expand Down Expand Up @@ -1248,12 +1226,22 @@ public synchronized XPage doSynchronousUploadDocument( HttpServletRequest reques

return getStepView( request );
}

/*
* Check if the request is from a login page
*/
private void isRequestFromALoginPage (HttpServletRequest request)
{
String urlFromRequestReferer = request.getHeader("referer");
if (urlFromRequestReferer != null && urlFromRequestReferer.contains(AUTH_PROVIDER))
{
isRequestFromLoginPage = true;
}
}
private void initFormResponseManager( HttpServletRequest request, Form form )
{
LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );

if ( _formResponseManager == null || !_formResponseManager.getIsResponseLoadedFromBackup() && !IsRequestComingFromAction)
isRequestFromALoginPage(request);
if ( _formResponseManager == null || isRequestFromLoginPage)
{
if ( user != null && form.isBackupEnabled() )
{
Expand All @@ -1263,6 +1251,7 @@ private void initFormResponseManager( HttpServletRequest request, Form form )
{
_formResponseManager = new FormResponseManager( form );
}
isRequestFromLoginPage = false;
}
}

Expand Down Expand Up @@ -1328,7 +1317,6 @@ private void init( int nIdForm )
_formResponseManager = null;
_stepDisplayTree = null;
_breadcrumb = null;
IsRequestComingFromAction = false;
}

/**
Expand Down

0 comments on commit b0314c4

Please sign in to comment.