Skip to content

Commit

Permalink
LUT-27866 : refactor to use new version of GenericFormsProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
seboo committed Aug 8, 2024
1 parent 1165445 commit 0f557d5
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ modify.template.buttonCancel=Cancel
modify.template.name=Template name
modify.template.generic=Associate the template with a form
modify.template.content=Content
modify.template.noRTE=Edit without a rich text editor. The macros to display the answers are between "<>". Example: <displayEntry q=position_1>
modify.template.withRTE=Edit with a rich text editor. The macros to display the answers are between "[]". Example: [displayEntry q=position_1]
modify.template.noRTE=Edit without a rich text editor. The macros can use "<>". Example: <&#64;displayEntry q=position_1>
modify.template.withRTE=Edit with a rich text editor. The macros must use brackets "[]". Example: [&#64;displayEntry q=position_1]
markers.label_table_title=Available bookmarks
markers.label_description=Description
markers.label_marker=Bookmark
Original file line number Diff line number Diff line change
Expand Up @@ -68,154 +68,114 @@
public class FormsPDFTask extends Task
{

/**
* The task title
*/
private static final String PROPERTY_LABEL_TITLE = "module.workflow.formspdf.title";
private static final String PROPERTY_LABEL_DESCRIPTION = "module.workflow.formspdf.export.pdf.description";
private static final String FTL_SQUARE_BRACKET_TAG = "[#ftl]";

/**
* the FormJasperConfigService to manage the task configuration
*/
private static final ITaskConfigService _formsPDFTaskConfigService = SpringContextService.getBean( "workflow-formspdf.formsPDFTaskConfigService" );

/**
* the ResourceHistoryService to get the forms to process
*/
private static final IResourceHistoryService _resourceHistoryService = SpringContextService.getBean( ResourceHistoryService.BEAN_SERVICE );

@Override
public void processTask( int nIdResourceHistory, HttpServletRequest request, Locale locale )
{

// Get the resourceHistory to find the resource to work with
ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( nIdResourceHistory );

// Get the task configuration
FormsPDFTaskConfig formsPDFTaskConfig = _formsPDFTaskConfigService.findByPrimaryKey( getId( ) );

// Id of the Form to modify
int nIdFormResponse = 0;
String strError = "";
AdminUser user = null;
FormsPDFTaskTemplate formsPDFTaskTemplate = null;
try
{
nIdFormResponse = resourceHistory.getIdResource( );

FormResponse frep = FormResponseHome.findByPrimaryKey( nIdFormResponse );
Form form = FormHome.findByPrimaryKey( frep.getFormId( ) );

// TODO Gerer le cas null quand il s'agit d'une action automatique
if ( request != null )
{
user = AdminUserService.getAdminUser( request );
}
FormResponse formResponse = FormResponseHome.findByPrimaryKey( nIdFormResponse );
Map<String, InfoMarker> collectionMarkersValue = GenericFormsProvider.provideMarkerValues( formResponse, request );
Map<String, Object> model = new HashMap<>( );
markersToModels(model, collectionMarkersValue);
formsPDFTaskTemplate = FormsPDFTaskTemplateHome.findByPrimaryKey( formsPDFTaskConfig.getIdTemplate( ) );
if(formsPDFTaskTemplate.isRte())
{
formsPDFTaskTemplate.setContent( addSquareBracketTag( formsPDFTaskTemplate.getContent( ) ) );
}
formsPDFTaskTemplate.setContent(AppTemplateService.getTemplateFromStringFtl(formsPDFTaskTemplate.getContent(), Locale.getDefault( ), model).getHtml());
HtmlToPDFGenerator htmltopdf = new HtmlToPDFGenerator( form.getTitle( ), I18nService.getLocalizedString( PROPERTY_LABEL_DESCRIPTION, locale ), frep,
formsPDFTaskTemplate );
TemporaryFileGeneratorService.getInstance( ).generateFile( htmltopdf, user );
}
catch( Exception e )
{
// print the error in a pdf
formsPDFTaskTemplate.setContent( e.getMessage());
HtmlToPDFGenerator htmltopdf = new HtmlToPDFGenerator( "error", I18nService.getLocalizedString( PROPERTY_LABEL_DESCRIPTION, locale ), new FormResponse(), formsPDFTaskTemplate );
TemporaryFileGeneratorService.getInstance( ).generateFile( htmltopdf, user );
throw new RuntimeException( strError, e );
}
}

@Override
public String getTitle( Locale locale )
{
return I18nService.getLocalizedString( PROPERTY_LABEL_TITLE, locale );
}

@Override
public Map<String, String> getTaskFormEntries( Locale locale )
{
return null;
}

@Override
public void init( )
{
// Do nothing

}

@Override
public void doRemoveTaskInformation( int nIdHistory )
{
// Do Nothing

}

@Override
public void doRemoveConfig( )
{
// _formsJasperTaskConfigService.remove( getId( ) );
_formsPDFTaskConfigService.remove( getId( ) );
}

/**
* In a loop, call the markersToModel method to add the markers to the model
* @param model
* @param collectionMarkersValue
*/
private void markersToModels( Map<String, Object> model, Map<String, InfoMarker> collectionMarkersValue )
{
for ( int i = 0; i < collectionMarkersValue.size(); i++ )
{
String key = collectionMarkersValue.keySet().toArray()[i].toString();
markersToModel( model, collectionMarkersValue, key );

}
}

/**
* Add the markers to the model
* @param model
* @param collectionMarkersValue
* @param key
*/
private void markersToModel( Map<String, Object> model, Map<String, InfoMarker> collectionMarkersValue, String key )
{
if(key.contains( "position_" ) )
{
FormQuestionResponse formQuestionResponse = (FormQuestionResponse) collectionMarkersValue.get( key ).getData( );
if(formQuestionResponse.getQuestion().getEntry() != null)
{
model.put( key, formQuestionResponse );
}
}
else
{
model.put( key, collectionMarkersValue.get( key ).getData( ) );
}
}

/**
* Add square bracket tag at the beginning of the template to process the template with the brackets included in the RTE
* @param strtemplate
* @return
*/

private String addSquareBracketTag(String strtemplate)
{
strtemplate = FTL_SQUARE_BRACKET_TAG+strtemplate;

return strtemplate;
}
/**
* The task title
*/
private static final String PROPERTY_LABEL_TITLE = "module.workflow.formspdf.title";
private static final String PROPERTY_LABEL_DESCRIPTION = "module.workflow.formspdf.export.pdf.description";
private static final String FTL_SQUARE_BRACKET_TAG = "[#ftl]";
private static final String MARK_QUESTION_LIST = "question_list";

/**
* the FormJasperConfigService to manage the task configuration
*/
private static final ITaskConfigService _formsPDFTaskConfigService = SpringContextService.getBean( "workflow-formspdf.formsPDFTaskConfigService" );

/**
* the ResourceHistoryService to get the forms to process
*/
private static final IResourceHistoryService _resourceHistoryService = SpringContextService.getBean( ResourceHistoryService.BEAN_SERVICE );


@Override
public void processTask( int nIdResourceHistory, HttpServletRequest request, Locale locale )
{

// Get the resourceHistory to find the resource to work with
ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( nIdResourceHistory );

// Get the task configuration
FormsPDFTaskConfig formsPDFTaskConfig = _formsPDFTaskConfigService.findByPrimaryKey( getId( ) );

// Id of the Form to modify
int nIdFormResponse = 0;
String strError = "";
AdminUser user = null;
FormsPDFTaskTemplate formsPDFTaskTemplate = null;
try
{
nIdFormResponse = resourceHistory.getIdResource( );

FormResponse frep = FormResponseHome.findByPrimaryKey( nIdFormResponse );
Form form = FormHome.findByPrimaryKey( frep.getFormId( ) );

// request could be null if the task is launched by daemon
if ( request != null )
{
user = AdminUserService.getAdminUser( request );
}
FormResponse formResponse = FormResponseHome.findByPrimaryKey( nIdFormResponse );

Map<String, Object> model = new HashMap<>( );
model.putAll( GenericFormsProvider.getValuesModel( formResponse, request ) );
model.put( MARK_QUESTION_LIST, GenericFormsProvider.getTitlesModel( form ) );

formsPDFTaskTemplate = FormsPDFTaskTemplateHome.findByPrimaryKey( formsPDFTaskConfig.getIdTemplate( ) );

if ( formsPDFTaskTemplate.isRte( ) )
{
// if RTE is the choosen edit mode, square brackets are used for markers, its must be announced in FTL template
formsPDFTaskTemplate.setContent( FTL_SQUARE_BRACKET_TAG + formsPDFTaskTemplate.getContent( ) );
}

formsPDFTaskTemplate.setContent( AppTemplateService.getTemplateFromStringFtl( formsPDFTaskTemplate.getContent(), Locale.getDefault( ), model).getHtml());

HtmlToPDFGenerator htmltopdf = new HtmlToPDFGenerator(
form.getTitle( ), I18nService.getLocalizedString( PROPERTY_LABEL_DESCRIPTION, locale ),
frep, formsPDFTaskTemplate );
TemporaryFileGeneratorService.getInstance( ).generateFile( htmltopdf, user );
}
catch( Exception e )
{
// print the error in a pdf
formsPDFTaskTemplate.setContent( e.getMessage());
HtmlToPDFGenerator htmltopdf = new HtmlToPDFGenerator( "error", I18nService.getLocalizedString( PROPERTY_LABEL_DESCRIPTION, locale ), new FormResponse(), formsPDFTaskTemplate );
TemporaryFileGeneratorService.getInstance( ).generateFile( htmltopdf, user );
throw new RuntimeException( strError, e );
}
}

@Override
public String getTitle( Locale locale )
{
return I18nService.getLocalizedString( PROPERTY_LABEL_TITLE, locale );
}

@Override
public Map<String, String> getTaskFormEntries( Locale locale )
{
return null;
}

@Override
public void init( )
{
// Do nothing

}

@Override
public void doRemoveTaskInformation( int nIdHistory )
{
// Do Nothing

}

@Override
public void doRemoveConfig( )
{
// _formsJasperTaskConfigService.remove( getId( ) );
_formsPDFTaskConfigService.remove( getId( ) );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ public class FormsPDFTaskTemplateJspBean extends MVCAdminJspBean{

private static final String PROPERTY_PAGE_TITLE_MANAGE_FORMS_PDF_TEMPLATES = "module.workflow.formspdf.manage.template.title";
private static final String PROPERTY_PAGE_TITLE_MODIFY_FORMS_PDF_TEMPLATES = "module.workflow.formspdf.modify.template.title";

@View( value = VIEW_MANAGE_TEMPLATES, defaultView = true )
public String getManageTemplates( HttpServletRequest request )
{
Locale locale = getLocale( );
Map<String, Object> model = getModel( );

if (_nIdTask == 0)
Expand All @@ -113,14 +113,16 @@ public String getManageTemplates( HttpServletRequest request )
@View( value = VIEW_MODIFY_TEMPLATE )
public String getModifyTemplate( HttpServletRequest request )
{
Locale locale = getLocale( );
Map<String, Object> model = getModel( );
FormsPDFTaskTemplate formsPDFTaskTemplate = null;
int nIdTemplate = NumberUtils.toInt( request.getParameter( PARAMETER_TEMPLATE_ID ), DEFAULT_ID_VALUE );
int nIdTemplate = NumberUtils.toInt( request.getParameter( PARAMETER_TEMPLATE_ID ), DEFAULT_ID_VALUE );

if (nIdTemplate > 0)
{
formsPDFTaskTemplate = FormsPDFTaskTemplateHome.findByPrimaryKey(nIdTemplate);
} else {
}
else
{
formsPDFTaskTemplate = new FormsPDFTaskTemplate();
formsPDFTaskTemplate.setGeneric(true);
formsPDFTaskTemplate.setRte(true);
Expand All @@ -146,8 +148,8 @@ public String getModifyTemplate( HttpServletRequest request )
model.put( MARK_FORMS_LIST, FormHome.getFormsReferenceList( ) );

// markers
Form form = FormHome.findByPrimaryKey( formsPDFTaskTemplate.getIdForm());
model.put(MARK_LIST_MARKERS, GenericFormsProvider.getProviderMarkerDescriptions(form != null ? form : new Form()));
Form form = FormHome.findByPrimaryKey( formsPDFTaskTemplate.getIdForm( ) );
model.put(MARK_LIST_MARKERS, GenericFormsProvider.getProviderMarkerDescriptions( form ) );

return getPage(PROPERTY_PAGE_TITLE_MODIFY_FORMS_PDF_TEMPLATES, TEMPLATE_MODIFY_FORMS_PDF_TEMPLATE, model);
}
Expand Down Expand Up @@ -183,6 +185,12 @@ public String doRemoveTemplate( HttpServletRequest request )
return redirectView( request, VIEW_MANAGE_TEMPLATES );
}

/**
*
* @param request
* @param formsPDFTaskTemplateToEdit
* @return the populated template
*/
private FormsPDFTaskTemplate populateFormsPDFTaskTemplate(HttpServletRequest request, FormsPDFTaskTemplate formsPDFTaskTemplateToEdit)
{
formsPDFTaskTemplateToEdit.setName(request.getParameter( PARAMETER_TEMPLATE_NAME ));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
DELETE FROM workflow_task_formspdf_template;
INSERT INTO workflow_task_formspdf_template (name, content) VALUES ('Default Template','<!DOCTYPE html><html><head><title>Export PDF formulaire ${form_title} le ${creation_date!}</title></head><body><h1 style="font-size: 30px; text-align: center; background-color: #f0f0f0;">${form_title}</h1><!-- division pour les reponses du formulaire a copier depuis les signets disponibles --><div id="form-response-summary" style="margin-bottom: 3em;"><h2 style="font-size: 18px;"> signet rep 1 </h2><h2 style="font-size: 18px;"> signet rep 2 </h2><h2 style="font-size: 18px;"> signet rep n </h2></div><!-- division pour le texte du copyright --><div id="copyright" style="position: fixed; bottom: 0; width: 100%; text-align: center; background-color: #f0f0f0; padding: 5px;"> Copyright (c) 2002-2023, City of Paris All rights reserved</div></body></html>');
INSERT INTO workflow_task_formspdf_template (name, id_form, is_generic, is_rte, content) VALUES ('Default Template',-1,1,1,'<!DOCTYPE html>\n[#-- Example of default macro override for specific display (brackets are used for RTE edition) --]\n[#macro displayEntryTypeDate entry, list_responses ]\n [#if list_responses?has_content]\n [#assign iteration = 0]\n [#list list_responses as response]\n [#if response.responseValue??]\n [#if iteration != 0 && response.file.title?has_content]\n <span>; </span>\n [/#if]\n [#assign iteration = iteration + 1]\n <span>${response.responseValue?number?number_to_date?string(\'dd-MM-yyyy\')}</span>\n [/#if]\n [/#list]\n [/#if]\n[/#macro]\n\n\n<html>\n<head>\n <title>Export PDF ${form_title} - ${creation_date!}</title>\n <style>body { }</style>\n</head>\n<body>\n <h1 style=\"font-size: 30px; text-align: center; background-color: #f0f0f0;font-family: Arial, Helvetica, sans-serif;\">${form_title} (${creation_date!})</h1>\n \n\n <div id=\"form-response-summary\" style=\"margin-bottom: 3em;font-family: Arial, Helvetica, sans-serif;\">\n [#list question_list as question_key,question_value ]\n <h2 style=\"font-size: 18px;\">${question_value!}</h2>\n <p>[@displayEntry q=.vars[question_key]/]</p> \n [/#list]\n </div>\n\n\n <div id=\"copyright\" style=\"position: fixed; bottom: 0; width: 100%; text-align: center; background-color: #f0f0f0; padding: 5px;\"> Copyright (c) 2002-2024, City of Paris All rights reserved</div>\n</body>\n</html>');



Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
<br/>
<@input type='hidden' name='rte' value=rte?c />
<#if rte>
<@checkBox orientation='switch' labelFor='rte' labelKey='#i18n{module.workflow.formspdf.modify.template.noRTE}' id='rte' name='macro_checkbox_rte' checked=rte params='onchange="javascript:toggleRTE();"' />
<@checkBox orientation='switch' labelFor='rte' labelKey='#i18n{module.workflow.formspdf.modify.template.withRTE}' id='rte' name='macro_checkbox_rte' checked=rte params='onchange="javascript:toggleRTE();"' />
<@formGroup labelFor='template_content' labelKey='#i18n{module.workflow.formspdf.modify.template.content}' mandatory=true rows=2>
<@input type='textarea' richtext=true name='template_content' id='template_content' rows=15 cols=70>${forms_pdf_task_template.content!}</@input>
</@formGroup>
<#else>
<@checkBox orientation='switch' labelFor='rte' labelKey='#i18n{module.workflow.formspdf.modify.template.withRTE}' id='rte' name='macro_checkbox_rte' checked=rte params='onchange="javascript:toggleRTE();"' />
<@checkBox orientation='switch' labelFor='rte' labelKey='#i18n{module.workflow.formspdf.modify.template.noRTE}' id='rte' name='macro_checkbox_rte' checked=rte params='onchange="javascript:toggleRTE();"' />
<@formGroup labelFor='template_content' labelKey='#i18n{module.workflow.formspdf.modify.template.content}' mandatory=true rows=2>
<@input type='textarea' name='template_content' id='template_content' rows=15 cols=70>${forms_pdf_task_template.content!}</@input>
</@formGroup>
Expand Down

0 comments on commit 0f557d5

Please sign in to comment.