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.duration.service;
35  
36  import fr.paris.lutece.plugins.workflow.modules.duration.business.PrerequisiteDurationConfig;
37  import fr.paris.lutece.plugins.workflowcore.business.prerequisite.IPrerequisiteConfig;
38  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
39  import fr.paris.lutece.plugins.workflowcore.service.action.IActionService;
40  import fr.paris.lutece.plugins.workflowcore.service.prerequisite.IAutomaticActionPrerequisiteService;
41  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceHistoryService;
42  import fr.paris.lutece.portal.service.template.AppTemplateService;
43  import fr.paris.lutece.util.html.HtmlTemplate;
44  
45  import java.sql.Timestamp;
46  
47  import java.util.HashMap;
48  import java.util.Locale;
49  import java.util.Map;
50  
51  import javax.inject.Inject;
52  
53  import javax.servlet.http.HttpServletRequest;
54  
55  public class PrerequisiteDuration implements IAutomaticActionPrerequisiteService
56  {
57      public static final String PREREQUISITE_TITLE_I18N = "module.workflow.duration.prerequisite_title";
58      public static final String CONFIG_DAO_BEAN_NAME = "workflow.PrerequisiteDurationConfigDAO";
59      private static final String TEMPLATE_DURATION_PREREQUISITE_CONFIG = "admin/plugins/workflow/modules/duration/prerequisite_duration_config.html";
60      private static final String MARK_CONFIG = "config";
61      @Inject
62      private IResourceHistoryService _resourceHistoryService;
63      @Inject
64      private IActionService _actionService;
65  
66      public String getPrerequisiteType( )
67      {
68          return PrerequisiteDurationConfig.PREREQUISITE_TYPE;
69      }
70  
71      public String getTitleI18nKey( )
72      {
73          return PREREQUISITE_TITLE_I18N;
74      }
75  
76      public boolean hasConfiguration( )
77      {
78          return true;
79      }
80  
81      public IPrerequisiteConfig getEmptyConfiguration( )
82      {
83          return new PrerequisiteDurationConfig( );
84      }
85  
86      public String getConfigurationDaoBeanName( )
87      {
88          return CONFIG_DAO_BEAN_NAME;
89      }
90  
91      public String getConfigHtml( IPrerequisiteConfig config, HttpServletRequest request, Locale locale )
92      {
93          Map<String, Object> model = new HashMap<>( );
94          model.put( MARK_CONFIG, config );
95  
96          HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_DURATION_PREREQUISITE_CONFIG, locale, model );
97  
98          return template.getHtml( );
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public boolean canActionBePerformed( int nIdResource, String strResourceType, IPrerequisiteConfig config, int nIdAction )
106     {
107         int idWorkflow = _actionService.findByPrimaryKeyWithoutIcon( nIdAction ).getWorkflow( ).getId( );
108         ResourceHistory resourceHistory = _resourceHistoryService.getLastHistoryResource( nIdResource, strResourceType, idWorkflow );
109 
110         if ( resourceHistory == null )
111         {
112             // The prerequisite is configured on an automatic action on the initial state without AutomaticReflexiveActions,
113             // so we don't have a resourceHistory yet. Since state creation occurs only after a call
114             // to getState (state creation is lazy), it is not very meaningful to have a duration
115             // for the initial state anyway. So don't delay.
116             //
117             // Note: If the client forces the state creation by calling getState to have a coherent behavior,
118             // it could also use an extra state and execute an action or use automatic actions, or even use
119             // AutomaticReflexiveAction on the initial state to ensure the resourceHistory is created.
120             return true;
121         }
122 
123         long configNbMinutes = ( (PrerequisiteDurationConfig) config ).getDuration( );
124         long configNbMillis = configNbMinutes * 1000;
125         Timestamp timestampInState = resourceHistory.getCreationDate( );
126         long nowMillis = System.currentTimeMillis( );
127         long millisInState = nowMillis - timestampInState.getTime( );
128 
129         return millisInState >= configNbMillis;
130     }
131 }