View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.workflow.modules.archive.web;
35  
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Locale;
39  import java.util.Map;
40  
41  import javax.inject.Inject;
42  import javax.servlet.http.HttpServletRequest;
43  
44  import org.apache.commons.lang3.StringUtils;
45  
46  import fr.paris.lutece.plugins.workflow.modules.archive.ArchivalType;
47  import fr.paris.lutece.plugins.workflow.modules.archive.business.ArchiveConfig;
48  import fr.paris.lutece.plugins.workflow.modules.archive.business.ArchiveResource;
49  import fr.paris.lutece.plugins.workflow.modules.archive.service.IArchiveService;
50  import fr.paris.lutece.plugins.workflow.web.task.NoFormTaskComponent;
51  import fr.paris.lutece.plugins.workflowcore.business.action.Action;
52  import fr.paris.lutece.plugins.workflowcore.business.state.State;
53  import fr.paris.lutece.plugins.workflowcore.business.state.StateFilter;
54  import fr.paris.lutece.plugins.workflowcore.service.action.IActionService;
55  import fr.paris.lutece.plugins.workflowcore.service.state.IStateService;
56  import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
57  import fr.paris.lutece.portal.service.i18n.I18nService;
58  import fr.paris.lutece.portal.service.message.AdminMessage;
59  import fr.paris.lutece.portal.service.message.AdminMessageService;
60  import fr.paris.lutece.portal.service.template.AppTemplateService;
61  import fr.paris.lutece.util.ReferenceList;
62  import fr.paris.lutece.util.date.DateUtil;
63  import fr.paris.lutece.util.html.HtmlTemplate;
64  
65  public class ArchiveTaskComponent extends NoFormTaskComponent
66  {
67      // TEMPLATES
68      private static final String TEMPLATE_TASK_ARCHIVE_CONFIG = "admin/plugins/workflow/modules/archive/task_archive_config.html";
69  
70      // MARKS
71      private static final String MARK_LIST_TYPE = "type_list";
72      private static final String MARK_CONFIG = "config";
73      private static final String MARK_LIST_STATE = "state_list";
74  
75      // PARAMETERS
76      private static final String PARAMETER_TYPE = "archival_type";
77      private static final String PARAMETER_DELAY = "archival_delay";
78      private static final String PARAMETER_NEXT_STATE = "next_state";
79  
80      // PROPERTIES
81      private static final String FIELD_TYPE = "module.workflow.archive.task.config.type.label";
82      private static final String FIELD_DELAY = "module.workflow.archive.task.config.delay.label";
83      private static final String FIELD_STATE = "module.workflow.archive.task.config.state.label";
84      private static final String PREFIX_TYPE = "module.workflow.archive.task.type.";
85  
86      // MESSAGES
87      private static final String MESSAGE_MANDATORY_FIELD = "module.workflow.archive.task.config.message.mandatory.field";
88      private static final String MESSAGE_NUMERIC_FIELD = "module.workflow.archive.task.config.message.numeric.field";
89      private static final String MESSAGE_INFORMATION_ARCHIVED = "module.workflow.archive.task.information.archived.message";
90      private static final String MESSAGE_INFORMATION_NOT_ARCHIVED = "module.workflow.archive.task.information.notarchived.message";
91  
92      @Inject
93      private IArchiveService _archiveService;
94  
95      @Inject
96      private IActionService _actionService;
97  
98      @Inject
99      private IStateService _stateService;
100 
101     @Override
102     public String getDisplayConfigForm( HttpServletRequest request, Locale locale, ITask task )
103     {
104         ArchiveConfig config = _archiveService.loadConfig( task );
105         if ( config == null )
106         {
107             config = new ArchiveConfig( );
108         }
109 
110         Map<String, Object> model = new HashMap<>( );
111         model.put( MARK_CONFIG, config );
112         model.put( MARK_LIST_TYPE, getTypeArchivalList( locale ) );
113         model.put( MARK_LIST_STATE, getListStates( task.getAction( ).getId( ) ) );
114 
115         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_ARCHIVE_CONFIG, locale, model );
116         return template.getHtml( );
117     }
118 
119     @Override
120     public String doSaveConfig( HttpServletRequest request, Locale locale, ITask task )
121     {
122         String strError = null;
123         String strType = request.getParameter( PARAMETER_TYPE );
124         String strDelay = request.getParameter( PARAMETER_DELAY );
125         String strState = request.getParameter( PARAMETER_NEXT_STATE );
126 
127         if ( StringUtils.isBlank( strType ) )
128         {
129             strError = FIELD_TYPE;
130         }
131 
132         if ( StringUtils.isBlank( strDelay ) )
133         {
134             strError = FIELD_DELAY;
135         }
136 
137         if ( StringUtils.isBlank( strState ) )
138         {
139             strError = FIELD_STATE;
140         }
141 
142         if ( strError != null )
143         {
144             Object [ ] tabRequiredFields = {
145                     I18nService.getLocalizedString( strError, locale )
146             };
147 
148             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
149         }
150         if ( !StringUtils.isNumeric( strDelay ) )
151         {
152             Object [ ] tabNumericFields = {
153                     I18nService.getLocalizedString( FIELD_DELAY, locale )
154             };
155             return AdminMessageService.getMessageUrl( request, MESSAGE_NUMERIC_FIELD, tabNumericFields, AdminMessage.TYPE_STOP );
156         }
157 
158         ArchiveConfig config = _archiveService.loadConfig( task );
159         boolean create = config == null;
160         if ( create )
161         {
162             config = new ArchiveConfig( );
163             config.setIdTask( task.getId( ) );
164         }
165         config.setTypeArchival( ArchivalType.valueOf( strType ) );
166         config.setNextState( Integer.valueOf( strState ) );
167         config.setDelayArchival( Integer.valueOf( strDelay ) );
168         if ( create )
169         {
170             getTaskConfigService( ).create( config );
171         }
172         else
173         {
174             getTaskConfigService( ).update( config );
175         }
176 
177         return null;
178     }
179 
180     private ReferenceList getTypeArchivalList( Locale locale )
181     {
182         ReferenceList refList = new ReferenceList( );
183         for ( ArchivalType archivalType : ArchivalType.values( ) )
184         {
185             refList.addItem( archivalType.name( ), I18nService.getLocalizedString( PREFIX_TYPE + archivalType.name( ), locale ) );
186         }
187         return refList;
188     }
189 
190     private ReferenceList getListStates( int nIdAction )
191     {
192         ReferenceList referenceListStates = new ReferenceList( );
193         Action action = _actionService.findByPrimaryKey( nIdAction );
194 
195         if ( ( action != null ) && ( action.getWorkflow( ) != null ) )
196         {
197             StateFilter stateFilter = new StateFilter( );
198             stateFilter.setIdWorkflow( action.getWorkflow( ).getId( ) );
199 
200             List<State> listStates = _stateService.getListStateByFilter( stateFilter );
201 
202             referenceListStates.addItem( -1, StringUtils.EMPTY );
203             referenceListStates.addAll( ReferenceList.convert( listStates, "id", "name", true ) );
204         }
205 
206         return referenceListStates;
207     }
208 
209     @Override
210     public String getDisplayTaskInformation( int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
211     {
212         ArchiveResource archiveResource = _archiveService.getArchiveResource( nIdHistory, task.getId( ) );
213         if ( archiveResource == null )
214         {
215             return null;
216         }
217         ArchiveConfig config = _archiveService.loadConfig( task );
218         if ( archiveResource.isArchived( ) )
219         {
220             String [ ] params = new String [ ] {
221                     DateUtil.getDateString( archiveResource.getArchivalDate( ), locale ),
222                     I18nService.getLocalizedString( PREFIX_TYPE + config.getTypeArchival( ), locale )
223             };
224             return I18nService.getLocalizedString( MESSAGE_INFORMATION_ARCHIVED, params, locale );
225         }
226         else
227         {
228             String [ ] params = new String [ ] {
229                     DateUtil.getDateString( archiveResource.getInitialDate( ), locale ),
230                     I18nService.getLocalizedString( PREFIX_TYPE + config.getTypeArchival( ), locale )
231             };
232             return I18nService.getLocalizedString( MESSAGE_INFORMATION_NOT_ARCHIVED, params, locale );
233         }
234     }
235 }