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.easyrulesbot.service.bot.rules;
35
36 import fr.paris.lutece.plugins.easyrulesbot.service.bot.BotExecutor;
37 import fr.paris.lutece.plugins.easyrulesbot.service.yaml.model.YamlButton;
38 import fr.paris.lutece.plugins.easyrulesbot.service.bot.rules.conditions.Condition;
39 import fr.paris.lutece.plugins.easyrulesbot.service.response.processors.ResponseProcessor;
40 import fr.paris.lutece.portal.service.template.AppTemplateService;
41 import fr.paris.lutece.portal.web.l10n.LocaleService;
42 import fr.paris.lutece.util.html.HtmlTemplate;
43
44 import org.easyrules.api.Rule;
45
46 import java.io.Serializable;
47 import java.util.HashMap;
48
49 import java.util.List;
50 import java.util.Locale;
51 import java.util.Map;
52
53
54
55
56 public class BotRule implements Rule, Comparable, Serializable
57 {
58 private static final String TEMPLATE_BUTTONS = "/skin/plugins/easyrulesbot/buttons_message.html";
59 private static final String MARK_BUTTONS = "buttons";
60 private static final String DEFAULT_MESSAGE_TYPE = "text";
61 private static final long serialVersionUID = 1L;
62
63
64 private String _strName;
65 private String _strDescription;
66 private String _strMessageTemplate;
67 private String _strMessageType = DEFAULT_MESSAGE_TYPE;
68 private String _strResponseCommentTemplate;
69 private String _strDataKey;
70 private List<YamlButton> _listButtons;
71 private int _nPriority;
72 private BotExecutor _executor;
73 private ResponseProcessor _responseProcessor;
74 private List<Condition> _listConditions;
75
76
77
78
79 @Override
80 public String getName( )
81 {
82 return _strName;
83 }
84
85
86
87
88
89
90
91 public void setName( String strName )
92 {
93 _strName = strName;
94 }
95
96
97
98
99 @Override
100 public String getDescription( )
101 {
102 return _strDescription;
103 }
104
105
106
107
108
109
110
111 public void setDescription( String strDescription )
112 {
113 _strDescription = strDescription;
114 }
115
116
117
118
119
120
121 public String getMessageType( )
122 {
123 return _strMessageType;
124 }
125
126
127
128
129
130
131
132 public void setMessageType( String strMessageType )
133 {
134 _strMessageType = strMessageType;
135 }
136
137
138
139
140 @Override
141 public int getPriority( )
142 {
143 return _nPriority;
144 }
145
146
147
148
149
150
151
152 public void setPriority( int nPriority )
153 {
154 _nPriority = nPriority;
155 }
156
157
158
159
160
161
162 public String getDataKey( )
163 {
164 return _strDataKey;
165 }
166
167
168
169
170
171
172
173 public void setDataKey( String strDataKey )
174 {
175 _strDataKey = strDataKey;
176 }
177
178
179
180
181
182
183 public List<YamlButton> getButtons( )
184 {
185 return _listButtons;
186 }
187
188
189
190
191
192
193
194 public void setButtons( List<YamlButton> listButtons )
195 {
196 _listButtons = listButtons;
197 }
198
199
200
201
202
203
204
205
206
207
208 public String getMessage( Map<String, String> mapData, Locale locale )
209 {
210 String strMessage = fillTemplate( _strMessageTemplate , mapData );
211
212
213 if( (_listButtons != null) && ! _listButtons.isEmpty() )
214 {
215 Map< String , Object> model = new HashMap<>();
216 model.put( MARK_BUTTONS , _listButtons );
217 HtmlTemplate tButtons = AppTemplateService.getTemplate( TEMPLATE_BUTTONS, locale, model );
218 String strButtons = tButtons.getHtml();
219 strMessage += "\n" + strButtons;
220 }
221
222 return strMessage;
223 }
224
225
226
227
228
229
230
231 public void setMessageTemplate( String strQuestion )
232 {
233 _strMessageTemplate = strQuestion;
234 }
235
236
237
238
239
240
241
242 public void setExecutor( BotExecutor executor )
243 {
244 _executor = executor;
245 }
246
247
248
249
250
251
252
253 public void setResponseProcessor( ResponseProcessor responseProcessor )
254 {
255 _responseProcessor = responseProcessor;
256 }
257
258
259
260
261
262
263 public ResponseProcessor getResponseProcessor( )
264 {
265 return _responseProcessor;
266 }
267
268
269
270
271
272
273
274 public void setListConditions( List<Condition> listConditions )
275 {
276 _listConditions = listConditions;
277 }
278
279
280
281
282
283
284
285 public void setResponseCommentTemplate( String strResponseCommentTemplate )
286 {
287 _strResponseCommentTemplate = strResponseCommentTemplate;
288 }
289
290
291
292
293
294
295
296
297
298
299 public String getResponseComment( Map<String, String> mapData, Locale locale )
300 {
301 String strTemplate = _strResponseCommentTemplate;
302
303 if ( strTemplate != null )
304 {
305 return fillTemplate( strTemplate, mapData );
306 }
307
308 return null;
309 }
310
311
312
313
314 @Override
315 public boolean evaluate( )
316 {
317 Map<String, String> mapData = _executor.getDataMap( );
318
319 for ( Condition condition : _listConditions )
320 {
321 if ( !condition.evaluate( mapData, _strDataKey ) )
322 {
323 return false;
324 }
325 }
326
327 return true;
328 }
329
330
331
332
333 @Override
334 public void execute( ) throws Exception
335 {
336 _executor.setCurrentRule( this );
337 }
338
339
340
341
342 @Override
343 public int compareTo( Object object )
344 {
345 Rule rule = (Rule) object;
346
347 return getPriority( ) - rule.getPriority( );
348 }
349
350
351
352
353
354
355
356
357
358
359 private String fillTemplate( String strTemplateString, Map<String, String> model )
360 {
361 if ( !strTemplateString.contains( "{" ) && !strTemplateString.contains( "<#" ) )
362 {
363
364 return strTemplateString;
365 }
366
367 String strTemplate = strTemplateString.replace( "{", "${" );
368 HtmlTemplate template = AppTemplateService.getTemplateFromStringFtl( strTemplate, LocaleService.getDefault( ), model );
369
370 return template.getHtml( );
371 }
372
373
374
375
376 @Override
377 public String toString()
378 {
379 StringBuilder sbOutput = new StringBuilder( "\n RULE");
380 sbOutput.append( "\n name : " ).append( _strName );
381 sbOutput.append( "\n description : " ).append( _strDescription );
382 sbOutput.append( "\n message : " ).append( _strMessageTemplate );
383 sbOutput.append( "\n priority : " ).append( _nPriority );
384 sbOutput.append( "\n data key : " ).append( _strDataKey );
385 sbOutput.append( "\n Conditions : " );
386 if( _listConditions != null )
387 {
388 for( Condition condition : _listConditions )
389 {
390 sbOutput.append( "\n " ).append( condition );
391 }
392 }
393 return sbOutput.toString();
394 }
395
396
397 }