View Javadoc
1   /*
2    * Copyright (c) 2002-2016, 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.response.processors;
35  
36  import fr.paris.lutece.plugins.easyrulesbot.service.response.exceptions.ResponseNotUnderstoodException;
37  import fr.paris.lutece.plugins.easyrulesbot.service.response.exceptions.ResponseProcessingException;
38  import fr.paris.lutece.plugins.easyrulesbot.util.FileUtils;
39  import fr.paris.lutece.portal.service.i18n.I18nService;
40  
41  import java.util.List;
42  import java.util.Locale;
43  import java.util.Map;
44  
45  /**
46   * Multiple Values ResponseProcessor
47   */
48  public class MultipleValuesResponseProcessor extends AbstractProcessor implements ResponseProcessor
49  {
50      private String _strMutipleValuesMapFile;
51      private Map<String, List<String>> _mapMultipleValues;
52      private String _strInvalidResponseMessage;
53      private String _strInvalidResponseMessageI18nKey;
54  
55      /**
56       * Set the map file path
57       * 
58       * @param strMapFile
59       */
60      public void setMutipleValuesMapFile( String strMapFile )
61      {
62          _strMutipleValuesMapFile = strMapFile;
63      }
64  
65      /**
66       * Set the map of values / terms
67       * 
68       * @param map
69       *            the map
70       */
71      public void setValueTermsMap( Map<String, List<String>> map )
72      {
73          _mapMultipleValues = map;
74      }
75  
76      /**
77       * Set the Invalid Response Message
78       * 
79       * @param strMessage
80       *            The message
81       */
82      public void setInvalidResponseMessage( String strMessage )
83      {
84          _strInvalidResponseMessage = strMessage;
85      }
86  
87      /**
88       * Set the Invalid Response Message
89       * 
90       * @param strMessage
91       *            The message
92       */
93      public void setInvalidResponseMessageI18nKey( String strMessage )
94      {
95          _strInvalidResponseMessageI18nKey = strMessage;
96      }
97  
98      /**
99       * {@inheritDoc }
100      */
101     @Override
102     public String processResponse( String strResponse, Locale locale, Map mapData ) throws ResponseProcessingException
103     {
104         String strResponseToCheck = strResponse.toLowerCase( );
105         Map<String, List<String>> map = getMultipleValuesMap( );
106 
107         for ( String strValue : map.keySet( ) )
108         {
109             List<String> listTerms = map.get( strValue );
110 
111             for ( String strTerm : listTerms )
112             {
113                 if ( strResponseToCheck.contains( strTerm ) )
114                 {
115                     return strValue;
116                 }
117             }
118         }
119 
120         throw new ResponseNotUnderstoodException( getInvalidResponse( locale ) );
121     }
122 
123     /**
124      * Return Map loaded from a file
125      * 
126      * @return The map
127      */
128     private Map<String, List<String>> getMultipleValuesMap( )
129     {
130         if ( _mapMultipleValues == null && _strMutipleValuesMapFile != null )
131         {
132             _mapMultipleValues = FileUtils.loadMapFromFile( _strMutipleValuesMapFile );
133         }
134         return _mapMultipleValues;
135     }
136 
137     /**
138      * Returns invalid response message
139      * 
140      * @param locale
141      *            The locale
142      * @return The message
143      */
144     private String getInvalidResponse( Locale locale )
145     {
146         String strResponse;
147 
148         if ( _strInvalidResponseMessageI18nKey != null )
149         {
150             strResponse = I18nService.getLocalizedString( _strInvalidResponseMessageI18nKey, locale );
151         }
152         else
153         {
154             strResponse = _strInvalidResponseMessage;
155         }
156 
157         return strResponse;
158     }
159 
160 }