1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
57
58
59 public class TaskFactory implements ITaskFactory
60 {
61
62
63
64 public static final String BEAN_SERVICE = "workflow.taskFactory";
65
66
67
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
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
141
142 @Override
143 public Collection<ITaskType> getAllTaskTypes( )
144 {
145 return SpringContextService.getBeansOfType( ITaskType.class );
146 }
147
148
149
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
173
174
175
176
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 }