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.service.prerequisite;
35  
36  import java.util.List;
37  
38  import org.apache.commons.lang3.StringUtils;
39  
40  import fr.paris.lutece.plugins.workflow.business.prerequisite.IPrerequisiteDAO;
41  import fr.paris.lutece.plugins.workflow.business.prerequisite.PrerequisiteDAO;
42  import fr.paris.lutece.plugins.workflow.service.WorkflowPlugin;
43  import fr.paris.lutece.plugins.workflowcore.business.prerequisite.IPrerequisiteConfig;
44  import fr.paris.lutece.plugins.workflowcore.business.prerequisite.IPrerequisiteConfigDAO;
45  import fr.paris.lutece.plugins.workflowcore.business.prerequisite.Prerequisite;
46  import fr.paris.lutece.plugins.workflowcore.service.prerequisite.IAutomaticActionPrerequisiteService;
47  import fr.paris.lutece.plugins.workflowcore.service.prerequisite.IPrerequisiteManagementService;
48  import fr.paris.lutece.portal.service.plugin.Plugin;
49  import fr.paris.lutece.portal.service.plugin.PluginService;
50  import fr.paris.lutece.portal.service.spring.SpringContextService;
51  
52  /**
53   * Implementation of the prerequisite management service
54   */
55  public class PrerequisiteManagementService implements IPrerequisiteManagementService
56  {
57      /**
58       * Name of the bean of this service
59       */
60      public static final String BEAN_NAME = "workflow.prerequisiteManagementService";
61      private IPrerequisiteDAO _dao;
62      private Plugin _plugin;
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public List<IAutomaticActionPrerequisiteService> getPrerequisiteServiceList( )
69      {
70          return SpringContextService.getBeansOfType( IAutomaticActionPrerequisiteService.class );
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      public IAutomaticActionPrerequisiteService getPrerequisiteService( String strPrerequisiteType )
78      {
79          for ( IAutomaticActionPrerequisiteService prerequisiteService : getPrerequisiteServiceList( ) )
80          {
81              if ( StringUtils.equals( strPrerequisiteType, prerequisiteService.getPrerequisiteType( ) ) )
82              {
83                  return prerequisiteService;
84              }
85          }
86  
87          return null;
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public List<Prerequisite> getListPrerequisite( int nIdAction )
95      {
96          return getPrerequisiteDao( ).findByIdAction( nIdAction, getPlugin( ) );
97      }
98  
99      @Override
100     public Prerequisite findPrerequisite( int nIdPrerequisite )
101     {
102         return getPrerequisiteDao( ).findByPrimaryKey( nIdPrerequisite, getPlugin( ) );
103     }
104 
105     @Override
106     public void createPrerequisiteConfiguration( IPrerequisiteConfig config, IAutomaticActionPrerequisiteService prerequisiteService )
107     {
108         IPrerequisiteConfigDAO configDAO = getConfigurationDAO( prerequisiteService );
109 
110         if ( configDAO != null )
111         {
112             configDAO.createConfig( config );
113         }
114     }
115 
116     @Override
117     public void updatePrerequisiteConfiguration( IPrerequisiteConfig config, IAutomaticActionPrerequisiteService prerequisiteService )
118     {
119         IPrerequisiteConfigDAO configDAO = getConfigurationDAO( prerequisiteService );
120 
121         if ( configDAO != null )
122         {
123             configDAO.updateConfig( config );
124         }
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     @Override
131     public IPrerequisiteConfig getPrerequisiteConfiguration( int nIdPrerequisite, IAutomaticActionPrerequisiteService prerequisiteService )
132     {
133         IPrerequisiteConfigDAO configDAO = getConfigurationDAO( prerequisiteService );
134 
135         if ( configDAO == null )
136         {
137             return null;
138         }
139 
140         return configDAO.findByPrimaryKey( nIdPrerequisite );
141     }
142 
143     @Override
144     public void createPrerequisite( Prerequisite prerequisite )
145     {
146         getPrerequisiteDao( ).create( prerequisite, getPlugin( ) );
147     }
148 
149     @Override
150     public void modifyPrerequisite( Prerequisite prerequisite )
151     {
152         getPrerequisiteDao( ).update( prerequisite, getPlugin( ) );
153     }
154 
155     @Override
156     public void deletePrerequisite( int nIdPrerequisite )
157     {
158         Prerequisite prerequisite = getPrerequisiteDao( ).findByPrimaryKey( nIdPrerequisite, getPlugin( ) );
159         getPrerequisiteDao( ).remove( nIdPrerequisite, getPlugin( ) );
160 
161         IAutomaticActionPrerequisiteService prerequisiteService = getPrerequisiteService( prerequisite.getPrerequisiteType( ) );
162 
163         if ( prerequisiteService.hasConfiguration( ) )
164         {
165             IPrerequisiteConfigDAO dao = getConfigurationDAO( prerequisiteService );
166 
167             if ( dao != null )
168             {
169                 dao.removeConfig( nIdPrerequisite );
170             }
171         }
172     }
173 
174     /**
175      * Get the prerequisite DAO
176      * 
177      * @return The prerequisite DAO
178      */
179     private IPrerequisiteDAO getPrerequisiteDao( )
180     {
181         if ( _dao == null )
182         {
183             _dao = SpringContextService.getBean( PrerequisiteDAO.BEAN_NAME );
184         }
185 
186         return _dao;
187     }
188 
189     /**
190      * Get the workflow plugin
191      * 
192      * @return The workflow plugin
193      */
194     private Plugin getPlugin( )
195     {
196         if ( _plugin == null )
197         {
198             _plugin = PluginService.getPlugin( WorkflowPlugin.PLUGIN_NAME );
199         }
200 
201         return _plugin;
202     }
203 
204     /**
205      * Get the configuration DAO of a prerequisite service
206      * 
207      * @param prerequisiteService
208      *            the service
209      * @return The DAO, or null if no DAO was found
210      */
211     private IPrerequisiteConfigDAO getConfigurationDAO( IAutomaticActionPrerequisiteService prerequisiteService )
212     {
213         String strDaoBeanName = prerequisiteService.getConfigurationDaoBeanName( );
214 
215         if ( StringUtils.isEmpty( strDaoBeanName ) )
216         {
217             return null;
218         }
219 
220         return SpringContextService.getBean( strDaoBeanName );
221     }
222 
223     @Override
224     public void copyPrerequisite( int nIdActionSource, int nIdActionTarget )
225     {
226         List<Prerequisite> listLinkedPrerequisite = getListPrerequisite( nIdActionSource );
227 
228         for ( Prerequisite prerequisite : listLinkedPrerequisite )
229         {
230             IAutomaticActionPrerequisiteService prerequisiteService = getPrerequisiteService( prerequisite.getPrerequisiteType( ) );
231             IPrerequisiteConfig config = getPrerequisiteConfiguration( prerequisite.getIdPrerequisite( ), prerequisiteService );
232 
233             prerequisite.setIdAction( nIdActionTarget );
234             createPrerequisite( prerequisite );
235 
236             if ( config != null )
237             {
238                 config.setIdPrerequisite( prerequisite.getIdPrerequisite( ) );
239                 createPrerequisiteConfiguration( config, prerequisiteService );
240             }
241         }
242     }
243 
244     @Override
245     public void deletePrerequisiteByAction( int nIdAction )
246     {
247         List<Prerequisite> listLinkedPrerequisite = getListPrerequisite( nIdAction );
248         for ( Prerequisite prerequisite : listLinkedPrerequisite )
249         {
250             deletePrerequisite( prerequisite.getIdPrerequisite( ) );
251         }
252     }
253 }