View Javadoc
1   /*
2    * Copyright (c) 2002-2018, Mairie de 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.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   * BotRule
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      // Variables declarations
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       * {@inheritDoc }
78       */
79      @Override
80      public String getName( )
81      {
82          return _strName;
83      }
84  
85      /**
86       * Sets the Name
87       * 
88       * @param strName
89       *            The Name
90       */
91      public void setName( String strName )
92      {
93          _strName = strName;
94      }
95  
96      /**
97       * {@inheritDoc }
98       */
99      @Override
100     public String getDescription( )
101     {
102         return _strDescription;
103     }
104 
105     /**
106      * Sets the Description
107      * 
108      * @param strDescription
109      *            The Description
110      */
111     public void setDescription( String strDescription )
112     {
113         _strDescription = strDescription;
114     }
115 
116     /**
117      * Returns the MessageType
118      * 
119      * @return The MessageType
120      */
121     public String getMessageType( )
122     {
123         return _strMessageType;
124     }
125 
126     /**
127      * Sets the MessageType
128      * 
129      * @param strMessageType
130      *            The MessageType
131      */
132     public void setMessageType( String strMessageType )
133     {
134         _strMessageType = strMessageType;
135     }
136 
137     /**
138      * {@inheritDoc }
139      */
140     @Override
141     public int getPriority( )
142     {
143         return _nPriority;
144     }
145 
146     /**
147      * Sets the Priority
148      * 
149      * @param nPriority
150      *            The Priority
151      */
152     public void setPriority( int nPriority )
153     {
154         _nPriority = nPriority;
155     }
156 
157     /**
158      * Returns the DataKey
159      * 
160      * @return The DataKey
161      */
162     public String getDataKey( )
163     {
164         return _strDataKey;
165     }
166 
167     /**
168      * Sets the DataKey
169      * 
170      * @param strDataKey
171      *            The DataKey
172      */
173     public void setDataKey( String strDataKey )
174     {
175         _strDataKey = strDataKey;
176     }
177 
178     /**
179      * Returns the Buttons
180      * 
181      * @return The Buttons
182      */
183     public List<YamlButton> getButtons( )
184     {
185         return _listButtons;
186     }
187 
188     /**
189      * Sets the Buttons
190      * 
191      * @param listButtons
192      *            The Buttons
193      */
194     public void setButtons( List<YamlButton> listButtons )
195     {
196         _listButtons = listButtons;
197     }
198 
199     /**
200      * Returns the Question
201      * 
202      * @param mapData
203      *            The map of data
204      * @param locale
205      *            The locale
206      * @return The Question
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      * Sets the Question
227      * 
228      * @param strQuestion
229      *            The Question
230      */
231     public void setMessageTemplate( String strQuestion )
232     {
233         _strMessageTemplate = strQuestion;
234     }
235 
236     /**
237      * Define the current executor using the rule
238      * 
239      * @param executor
240      *            The executor
241      */
242     public void setExecutor( BotExecutor executor )
243     {
244         _executor = executor;
245     }
246 
247     /**
248      * Sets the response processor
249      * 
250      * @param responseProcessor
251      *            the response processor
252      */
253     public void setResponseProcessor( ResponseProcessor responseProcessor )
254     {
255         _responseProcessor = responseProcessor;
256     }
257 
258     /**
259      * Gets the response processor
260      * 
261      * @return the response processor
262      */
263     public ResponseProcessor getResponseProcessor( )
264     {
265         return _responseProcessor;
266     }
267 
268     /**
269      * Sets the conditions list
270      * 
271      * @param listConditions
272      *            The list of condition
273      */
274     public void setListConditions( List<Condition> listConditions )
275     {
276         _listConditions = listConditions;
277     }
278 
279     /**
280      * Define the response comment template
281      * 
282      * @param strResponseCommentTemplate
283      *            The template
284      */
285     public void setResponseCommentTemplate( String strResponseCommentTemplate )
286     {
287         _strResponseCommentTemplate = strResponseCommentTemplate;
288     }
289 
290     /**
291      * Provides a comment after the user's response
292      * 
293      * @param mapData
294      *            The data
295      * @param locale
296      *            The locale
297      * @return The response
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      * {@inheritDoc }
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      * {@inheritDoc }
332      */
333     @Override
334     public void execute( ) throws Exception
335     {
336         _executor.setCurrentRule( this );
337     }
338 
339     /**
340      * {@inheritDoc }
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      * Fill a template with data contained in a model map
352      * 
353      * @param strTemplateString
354      *            The template
355      * @param model
356      *            The model
357      * @return The filled string
358      */
359     private String fillTemplate( String strTemplateString, Map<String, String> model )
360     {
361         if ( !strTemplateString.contains( "{" ) && !strTemplateString.contains( "<#" ) )
362         {
363             // No marker and no freemarker tag so nothing to transform
364             return strTemplateString;
365         }
366 
367         String strTemplate = strTemplateString.replace( "{", "${" ); // $ removed in context file
368         HtmlTemplate template = AppTemplateService.getTemplateFromStringFtl( strTemplate, LocaleService.getDefault( ), model );
369 
370         return template.getHtml( );
371     }
372 
373     /**
374      * {@inheritDoc }
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 }