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.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
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
57
58
59
60 public void setMutipleValuesMapFile( String strMapFile )
61 {
62 _strMutipleValuesMapFile = strMapFile;
63 }
64
65
66
67
68
69
70
71 public void setValueTermsMap( Map<String, List<String>> map )
72 {
73 _mapMultipleValues = map;
74 }
75
76
77
78
79
80
81
82 public void setInvalidResponseMessage( String strMessage )
83 {
84 _strInvalidResponseMessage = strMessage;
85 }
86
87
88
89
90
91
92
93 public void setInvalidResponseMessageI18nKey( String strMessage )
94 {
95 _strInvalidResponseMessageI18nKey = strMessage;
96 }
97
98
99
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
125
126
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
139
140
141
142
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 }