|
68 | 68 | public class FormsPDFTask extends Task
|
69 | 69 | {
|
70 | 70 |
|
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 | + |
221 | 181 | }
|
0 commit comments