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.task;
35  
36  import fr.paris.lutece.plugins.workflowcore.business.config.ITaskConfig;
37  import fr.paris.lutece.plugins.workflowcore.business.task.ITaskType;
38  import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
39  import fr.paris.lutece.plugins.workflowcore.service.task.ITaskFactory;
40  import fr.paris.lutece.portal.service.i18n.I18nService;
41  import fr.paris.lutece.portal.service.spring.SpringContextService;
42  import fr.paris.lutece.portal.service.util.AppLogService;
43  
44  import org.apache.commons.collections.CollectionUtils;
45  import org.apache.commons.lang3.StringUtils;
46  import org.springframework.beans.BeansException;
47  import org.springframework.beans.factory.BeanDefinitionStoreException;
48  import org.springframework.beans.factory.CannotLoadBeanClassException;
49  import org.springframework.beans.factory.NoSuchBeanDefinitionException;
50  
51  import java.util.Collection;
52  import java.util.Locale;
53  
54  /**
55   *
56   * TaskFactory
57   *
58   */
59  public class TaskFactory implements ITaskFactory
60  {
61      /**
62       * The name of the bean of this service
63       */
64      public static final String BEAN_SERVICE = "workflow.taskFactory";
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public ITask newTask( String strKey, Locale locale )
71      {
72          ITask task = this.newTask( strKey );
73          if ( task == null )
74          {
75              return null;
76          }
77  
78          if ( ( locale != null ) && ( task.getTaskType( ) != null ) && StringUtils.isNotBlank( task.getTaskType( ).getTitleI18nKey( ) ) )
79          {
80              task.getTaskType( ).setTitle( I18nService.getLocalizedString( task.getTaskType( ).getTitleI18nKey( ), locale ) );
81          }
82  
83          return task;
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public ITaskConfig newTaskConfig( String strKey )
91      {
92          if ( StringUtils.isBlank( strKey ) )
93          {
94              AppLogService.error( "TaskFactory ERROR : The key is empty." );
95  
96              return null;
97          }
98  
99          Collection<ITaskType> listTaskType = getAllTaskTypes( );
100 
101         for ( ITaskType taskType : listTaskType )
102         {
103             if ( strKey.equals( taskType.getKey( ) ) )
104             {
105                 try
106                 {
107                     return SpringContextService.getBean( taskType.getConfigBeanName( ) );
108                 }
109                 catch( BeansException e )
110                 {
111                     logBeanException( e );
112                 }
113             }
114         }
115 
116         AppLogService.error( "TaskFactory ERROR : The task type is not found." );
117 
118         return null;
119     }
120 
121     private void logBeanException( BeansException e )
122     {
123         String beanName = "unknown bean";
124         if ( e instanceof NoSuchBeanDefinitionException )
125         {
126             beanName = ( (NoSuchBeanDefinitionException) e ).getBeanName( );
127         }
128         if ( e instanceof CannotLoadBeanClassException )
129         {
130             beanName = ( (CannotLoadBeanClassException) e ).getBeanName( );
131         }
132         if ( e instanceof BeanDefinitionStoreException )
133         {
134             beanName = ( (BeanDefinitionStoreException) e ).getBeanName( );
135         }
136         AppLogService.error( "TaskFactory ERROR : could not load bean '" + beanName + "' - CAUSE : " + e.getMessage( ), e );
137     }
138 
139     /**
140      * {@inheritDoc}
141      */
142     @Override
143     public Collection<ITaskType> getAllTaskTypes( )
144     {
145         return SpringContextService.getBeansOfType( ITaskType.class );
146     }
147 
148     /**
149      * {@inheritDoc}
150      */
151     @Override
152     public Collection<ITaskType> getAllTaskTypes( Locale locale )
153     {
154         Collection<ITaskType> listTaskTypes = getAllTaskTypes( );
155         if ( locale == null )
156         {
157             return listTaskTypes;
158         }
159 
160         if ( CollectionUtils.isNotEmpty( listTaskTypes ) )
161         {
162             for ( ITaskType taskType : listTaskTypes )
163             {
164                 taskType.setTitle( I18nService.getLocalizedString( taskType.getTitleI18nKey( ), locale ) );
165             }
166         }
167 
168         return listTaskTypes;
169     }
170 
171     /**
172      * Get new instance of {@link ITask}
173      * 
174      * @param strKey
175      *            the task type key
176      * @return a new instance of {@link ITask}
177      */
178     private ITask newTask( String strKey )
179     {
180         if ( StringUtils.isBlank( strKey ) )
181         {
182             AppLogService.error( "TaskFactory ERROR : The key is empty." );
183 
184             return null;
185         }
186 
187         Collection<ITaskType> listTaskType = getAllTaskTypes( );
188 
189         for ( ITaskType taskType : listTaskType )
190         {
191             if ( strKey.equals( taskType.getKey( ) ) )
192             {
193                 try
194                 {
195                     ITask task = SpringContextService.getBean( taskType.getBeanName( ) );
196                     task.setTaskType( taskType );
197 
198                     return task;
199                 }
200                 catch( BeansException e )
201                 {
202                     logBeanException( e );
203                 }
204             }
205         }
206 
207         AppLogService.error( "TaskFactory ERROR : The task type is not found." );
208 
209         return null;
210     }
211 }