Skip to content

Commit 0ae8ecf

Browse files
committed
LUT-27866 : refactor to use new version of GenericFormsProvider
1 parent 1165445 commit 0ae8ecf

File tree

5 files changed

+132
-161
lines changed

5 files changed

+132
-161
lines changed

src/java/fr/paris/lutece/plugins/workflow/modules/formspdf/resources/formspdf_messages.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ modify.template.buttonCancel=Cancel
4949
modify.template.name=Template name
5050
modify.template.generic=Associate the template with a form
5151
modify.template.content=Content
52-
modify.template.noRTE=Edit without a rich text editor. The macros to display the answers are between "<>". Example: <displayEntry q=position_1>
53-
modify.template.withRTE=Edit with a rich text editor. The macros to display the answers are between "[]". Example: [displayEntry q=position_1]
52+
modify.template.noRTE=Edit without a rich text editor. The macros can use "<>". Example: <&#64;displayEntry q=position_1>
53+
modify.template.withRTE=Edit with a rich text editor. The macros must use brackets "[]". Example: [&#64;displayEntry q=position_1]
5454
markers.label_table_title=Available bookmarks
5555
markers.label_description=Description
5656
markers.label_marker=Bookmark

src/java/fr/paris/lutece/plugins/workflow/modules/formspdf/service/task/FormsPDFTask.java

Lines changed: 110 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -68,154 +68,114 @@
6868
public class FormsPDFTask extends Task
6969
{
7070

71-
/**
72-
* The task title
73-
*/
74-
private static final String PROPERTY_LABEL_TITLE = "module.workflow.formspdf.title";
75-
private static final String PROPERTY_LABEL_DESCRIPTION = "module.workflow.formspdf.export.pdf.description";
76-
private static final String FTL_SQUARE_BRACKET_TAG = "[#ftl]";
77-
78-
/**
79-
* the FormJasperConfigService to manage the task configuration
80-
*/
81-
private static final ITaskConfigService _formsPDFTaskConfigService = SpringContextService.getBean( "workflow-formspdf.formsPDFTaskConfigService" );
82-
83-
/**
84-
* the ResourceHistoryService to get the forms to process
85-
*/
86-
private static final IResourceHistoryService _resourceHistoryService = SpringContextService.getBean( ResourceHistoryService.BEAN_SERVICE );
87-
88-
@Override
89-
public void processTask( int nIdResourceHistory, HttpServletRequest request, Locale locale )
90-
{
91-
92-
// Get the resourceHistory to find the resource to work with
93-
ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( nIdResourceHistory );
94-
95-
// Get the task configuration
96-
FormsPDFTaskConfig formsPDFTaskConfig = _formsPDFTaskConfigService.findByPrimaryKey( getId( ) );
97-
98-
// Id of the Form to modify
99-
int nIdFormResponse = 0;
100-
String strError = "";
101-
AdminUser user = null;
102-
FormsPDFTaskTemplate formsPDFTaskTemplate = null;
103-
try
104-
{
105-
nIdFormResponse = resourceHistory.getIdResource( );
106-
107-
FormResponse frep = FormResponseHome.findByPrimaryKey( nIdFormResponse );
108-
Form form = FormHome.findByPrimaryKey( frep.getFormId( ) );
109-
110-
// TODO Gerer le cas null quand il s'agit d'une action automatique
111-
if ( request != null )
112-
{
113-
user = AdminUserService.getAdminUser( request );
114-
}
115-
FormResponse formResponse = FormResponseHome.findByPrimaryKey( nIdFormResponse );
116-
Map<String, InfoMarker> collectionMarkersValue = GenericFormsProvider.provideMarkerValues( formResponse, request );
117-
Map<String, Object> model = new HashMap<>( );
118-
markersToModels(model, collectionMarkersValue);
119-
formsPDFTaskTemplate = FormsPDFTaskTemplateHome.findByPrimaryKey( formsPDFTaskConfig.getIdTemplate( ) );
120-
if(formsPDFTaskTemplate.isRte())
121-
{
122-
formsPDFTaskTemplate.setContent( addSquareBracketTag( formsPDFTaskTemplate.getContent( ) ) );
123-
}
124-
formsPDFTaskTemplate.setContent(AppTemplateService.getTemplateFromStringFtl(formsPDFTaskTemplate.getContent(), Locale.getDefault( ), model).getHtml());
125-
HtmlToPDFGenerator htmltopdf = new HtmlToPDFGenerator( form.getTitle( ), I18nService.getLocalizedString( PROPERTY_LABEL_DESCRIPTION, locale ), frep,
126-
formsPDFTaskTemplate );
127-
TemporaryFileGeneratorService.getInstance( ).generateFile( htmltopdf, user );
128-
}
129-
catch( Exception e )
130-
{
131-
// print the error in a pdf
132-
formsPDFTaskTemplate.setContent( e.getMessage());
133-
HtmlToPDFGenerator htmltopdf = new HtmlToPDFGenerator( "error", I18nService.getLocalizedString( PROPERTY_LABEL_DESCRIPTION, locale ), new FormResponse(), formsPDFTaskTemplate );
134-
TemporaryFileGeneratorService.getInstance( ).generateFile( htmltopdf, user );
135-
throw new RuntimeException( strError, e );
136-
}
137-
}
138-
139-
@Override
140-
public String getTitle( Locale locale )
141-
{
142-
return I18nService.getLocalizedString( PROPERTY_LABEL_TITLE, locale );
143-
}
144-
145-
@Override
146-
public Map<String, String> getTaskFormEntries( Locale locale )
147-
{
148-
return null;
149-
}
150-
151-
@Override
152-
public void init( )
153-
{
154-
// Do nothing
155-
156-
}
157-
158-
@Override
159-
public void doRemoveTaskInformation( int nIdHistory )
160-
{
161-
// Do Nothing
162-
163-
}
164-
165-
@Override
166-
public void doRemoveConfig( )
167-
{
168-
// _formsJasperTaskConfigService.remove( getId( ) );
169-
_formsPDFTaskConfigService.remove( getId( ) );
170-
}
171-
172-
/**
173-
* In a loop, call the markersToModel method to add the markers to the model
174-
* @param model
175-
* @param collectionMarkersValue
176-
*/
177-
private void markersToModels( Map<String, Object> model, Map<String, InfoMarker> collectionMarkersValue )
178-
{
179-
for ( int i = 0; i < collectionMarkersValue.size(); i++ )
180-
{
181-
String key = collectionMarkersValue.keySet().toArray()[i].toString();
182-
markersToModel( model, collectionMarkersValue, key );
183-
184-
}
185-
}
186-
187-
/**
188-
* Add the markers to the model
189-
* @param model
190-
* @param collectionMarkersValue
191-
* @param key
192-
*/
193-
private void markersToModel( Map<String, Object> model, Map<String, InfoMarker> collectionMarkersValue, String key )
194-
{
195-
if(key.contains( "position_" ) )
196-
{
197-
FormQuestionResponse formQuestionResponse = (FormQuestionResponse) collectionMarkersValue.get( key ).getData( );
198-
if(formQuestionResponse.getQuestion().getEntry() != null)
199-
{
200-
model.put( key, formQuestionResponse );
201-
}
202-
}
203-
else
204-
{
205-
model.put( key, collectionMarkersValue.get( key ).getData( ) );
206-
}
207-
}
208-
209-
/**
210-
* Add square bracket tag at the beginning of the template to process the template with the brackets included in the RTE
211-
* @param strtemplate
212-
* @return
213-
*/
214-
215-
private String addSquareBracketTag(String strtemplate)
216-
{
217-
strtemplate = FTL_SQUARE_BRACKET_TAG+strtemplate;
218-
219-
return strtemplate;
220-
}
71+
/**
72+
* The task title
73+
*/
74+
private static final String PROPERTY_LABEL_TITLE = "module.workflow.formspdf.title";
75+
private static final String PROPERTY_LABEL_DESCRIPTION = "module.workflow.formspdf.export.pdf.description";
76+
private static final String FTL_SQUARE_BRACKET_TAG = "[#ftl]";
77+
private static final String MARK_QUESTION_LIST = "question_list";
78+
79+
/**
80+
* the FormJasperConfigService to manage the task configuration
81+
*/
82+
private static final ITaskConfigService _formsPDFTaskConfigService = SpringContextService.getBean( "workflow-formspdf.formsPDFTaskConfigService" );
83+
84+
/**
85+
* the ResourceHistoryService to get the forms to process
86+
*/
87+
private static final IResourceHistoryService _resourceHistoryService = SpringContextService.getBean( ResourceHistoryService.BEAN_SERVICE );
88+
89+
90+
@Override
91+
public void processTask( int nIdResourceHistory, HttpServletRequest request, Locale locale )
92+
{
93+
94+
// Get the resourceHistory to find the resource to work with
95+
ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( nIdResourceHistory );
96+
97+
// Get the task configuration
98+
FormsPDFTaskConfig formsPDFTaskConfig = _formsPDFTaskConfigService.findByPrimaryKey( getId( ) );
99+
100+
// Id of the Form to modify
101+
int nIdFormResponse = 0;
102+
String strError = "";
103+
AdminUser user = null;
104+
FormsPDFTaskTemplate formsPDFTaskTemplate = null;
105+
try
106+
{
107+
nIdFormResponse = resourceHistory.getIdResource( );
108+
109+
FormResponse frep = FormResponseHome.findByPrimaryKey( nIdFormResponse );
110+
Form form = FormHome.findByPrimaryKey( frep.getFormId( ) );
111+
112+
// request could be null if the task is launched by daemon
113+
if ( request != null )
114+
{
115+
user = AdminUserService.getAdminUser( request );
116+
}
117+
FormResponse formResponse = FormResponseHome.findByPrimaryKey( nIdFormResponse );
118+
119+
Map<String, Object> model = new HashMap<>( );
120+
model.putAll( GenericFormsProvider.getValuesModel( formResponse, request ) );
121+
model.put( MARK_QUESTION_LIST, GenericFormsProvider.getTitlesModel( form ) );
122+
123+
formsPDFTaskTemplate = FormsPDFTaskTemplateHome.findByPrimaryKey( formsPDFTaskConfig.getIdTemplate( ) );
124+
125+
if ( formsPDFTaskTemplate.isRte( ) )
126+
{
127+
// if RTE is the choosen edit mode, square brackets are used for markers, its must be announced in FTL template
128+
formsPDFTaskTemplate.setContent( FTL_SQUARE_BRACKET_TAG + formsPDFTaskTemplate.getContent( ) );
129+
}
130+
131+
formsPDFTaskTemplate.setContent( AppTemplateService.getTemplateFromStringFtl( formsPDFTaskTemplate.getContent(), Locale.getDefault( ), model).getHtml());
132+
133+
HtmlToPDFGenerator htmltopdf = new HtmlToPDFGenerator(
134+
form.getTitle( ), I18nService.getLocalizedString( PROPERTY_LABEL_DESCRIPTION, locale ),
135+
frep, formsPDFTaskTemplate );
136+
TemporaryFileGeneratorService.getInstance( ).generateFile( htmltopdf, user );
137+
}
138+
catch( Exception e )
139+
{
140+
// print the error in a pdf
141+
formsPDFTaskTemplate.setContent( e.getMessage());
142+
HtmlToPDFGenerator htmltopdf = new HtmlToPDFGenerator( "error", I18nService.getLocalizedString( PROPERTY_LABEL_DESCRIPTION, locale ), new FormResponse(), formsPDFTaskTemplate );
143+
TemporaryFileGeneratorService.getInstance( ).generateFile( htmltopdf, user );
144+
throw new RuntimeException( strError, e );
145+
}
146+
}
147+
148+
@Override
149+
public String getTitle( Locale locale )
150+
{
151+
return I18nService.getLocalizedString( PROPERTY_LABEL_TITLE, locale );
152+
}
153+
154+
@Override
155+
public Map<String, String> getTaskFormEntries( Locale locale )
156+
{
157+
return null;
158+
}
159+
160+
@Override
161+
public void init( )
162+
{
163+
// Do nothing
164+
165+
}
166+
167+
@Override
168+
public void doRemoveTaskInformation( int nIdHistory )
169+
{
170+
// Do Nothing
171+
172+
}
173+
174+
@Override
175+
public void doRemoveConfig( )
176+
{
177+
// _formsJasperTaskConfigService.remove( getId( ) );
178+
_formsPDFTaskConfigService.remove( getId( ) );
179+
}
180+
221181
}

src/java/fr/paris/lutece/plugins/workflow/modules/formspdf/web/task/FormsPDFTaskTemplateJspBean.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ public class FormsPDFTaskTemplateJspBean extends MVCAdminJspBean{
9393

9494
private static final String PROPERTY_PAGE_TITLE_MANAGE_FORMS_PDF_TEMPLATES = "module.workflow.formspdf.manage.template.title";
9595
private static final String PROPERTY_PAGE_TITLE_MODIFY_FORMS_PDF_TEMPLATES = "module.workflow.formspdf.modify.template.title";
96+
9697
@View( value = VIEW_MANAGE_TEMPLATES, defaultView = true )
9798
public String getManageTemplates( HttpServletRequest request )
9899
{
99-
Locale locale = getLocale( );
100100
Map<String, Object> model = getModel( );
101101

102102
if (_nIdTask == 0)
@@ -113,14 +113,16 @@ public String getManageTemplates( HttpServletRequest request )
113113
@View( value = VIEW_MODIFY_TEMPLATE )
114114
public String getModifyTemplate( HttpServletRequest request )
115115
{
116-
Locale locale = getLocale( );
117116
Map<String, Object> model = getModel( );
118117
FormsPDFTaskTemplate formsPDFTaskTemplate = null;
119-
int nIdTemplate = NumberUtils.toInt( request.getParameter( PARAMETER_TEMPLATE_ID ), DEFAULT_ID_VALUE );
118+
int nIdTemplate = NumberUtils.toInt( request.getParameter( PARAMETER_TEMPLATE_ID ), DEFAULT_ID_VALUE );
119+
120120
if (nIdTemplate > 0)
121121
{
122122
formsPDFTaskTemplate = FormsPDFTaskTemplateHome.findByPrimaryKey(nIdTemplate);
123-
} else {
123+
}
124+
else
125+
{
124126
formsPDFTaskTemplate = new FormsPDFTaskTemplate();
125127
formsPDFTaskTemplate.setGeneric(true);
126128
formsPDFTaskTemplate.setRte(true);
@@ -146,8 +148,8 @@ public String getModifyTemplate( HttpServletRequest request )
146148
model.put( MARK_FORMS_LIST, FormHome.getFormsReferenceList( ) );
147149

148150
// markers
149-
Form form = FormHome.findByPrimaryKey( formsPDFTaskTemplate.getIdForm());
150-
model.put(MARK_LIST_MARKERS, GenericFormsProvider.getProviderMarkerDescriptions(form != null ? form : new Form()));
151+
Form form = FormHome.findByPrimaryKey( formsPDFTaskTemplate.getIdForm( ) );
152+
model.put(MARK_LIST_MARKERS, GenericFormsProvider.getProviderMarkerDescriptions( form ) );
151153

152154
return getPage(PROPERTY_PAGE_TITLE_MODIFY_FORMS_PDF_TEMPLATES, TEMPLATE_MODIFY_FORMS_PDF_TEMPLATE, model);
153155
}
@@ -183,6 +185,12 @@ public String doRemoveTemplate( HttpServletRequest request )
183185
return redirectView( request, VIEW_MANAGE_TEMPLATES );
184186
}
185187

188+
/**
189+
*
190+
* @param request
191+
* @param formsPDFTaskTemplateToEdit
192+
* @return the populated template
193+
*/
186194
private FormsPDFTaskTemplate populateFormsPDFTaskTemplate(HttpServletRequest request, FormsPDFTaskTemplate formsPDFTaskTemplateToEdit)
187195
{
188196
formsPDFTaskTemplateToEdit.setName(request.getParameter( PARAMETER_TEMPLATE_NAME ));
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
DELETE FROM workflow_task_formspdf_template;
2-
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>');
2+
INSERT INTO workflow_task_formspdf_template (name, id_form, is_generic, is_rte, content) VALUES ('Default Template',-1,1,0,'<!DOCTYPE html>\n<#-- Example of default macro override for specific display -->\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_title >\n <h2 style=\"font-size: 18px;\">${question_title!}</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-2023, City of Paris. All rights reserved</div>\n</body>\n</html>\n');
3+
4+
5+

webapp/WEB-INF/templates/admin/plugins/workflow/modules/formspdf/modify_forms_pdf_template.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
<br/>
2525
<@input type='hidden' name='rte' value=rte?c />
2626
<#if rte>
27-
<@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();"' />
27+
<@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();"' />
2828
<@formGroup labelFor='template_content' labelKey='#i18n{module.workflow.formspdf.modify.template.content}' mandatory=true rows=2>
2929
<@input type='textarea' richtext=true name='template_content' id='template_content' rows=15 cols=70>${forms_pdf_task_template.content!}</@input>
3030
</@formGroup>
3131
<#else>
32-
<@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();"' />
32+
<@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();"' />
3333
<@formGroup labelFor='template_content' labelKey='#i18n{module.workflow.formspdf.modify.template.content}' mandatory=true rows=2>
3434
<@input type='textarea' name='template_content' id='template_content' rows=15 cols=70>${forms_pdf_task_template.content!}</@input>
3535
</@formGroup>

0 commit comments

Comments
 (0)