View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.directory.business;
35  
36  import fr.paris.lutece.plugins.directory.service.DirectoryPlugin;
37  import fr.paris.lutece.plugins.directory.utils.DirectoryErrorException;
38  import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
39  import fr.paris.lutece.portal.business.regularexpression.RegularExpression;
40  import fr.paris.lutece.portal.service.datastore.DatastoreService;
41  import fr.paris.lutece.portal.service.i18n.I18nService;
42  import fr.paris.lutece.portal.service.message.AdminMessage;
43  import fr.paris.lutece.portal.service.message.AdminMessageService;
44  import fr.paris.lutece.portal.service.plugin.Plugin;
45  import fr.paris.lutece.portal.service.plugin.PluginService;
46  import fr.paris.lutece.portal.service.regularexpression.RegularExpressionService;
47  import fr.paris.lutece.portal.service.util.AppPropertiesService;
48  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
49  import fr.paris.lutece.util.ReferenceList;
50  import fr.paris.lutece.util.html.Paginator;
51  import fr.paris.lutece.util.string.StringUtil;
52  
53  import org.apache.commons.lang.StringUtils;
54  
55  import java.util.ArrayList;
56  import java.util.List;
57  import java.util.Locale;
58  
59  import javax.servlet.http.HttpServletRequest;
60  
61  /**
62   *
63   * class EntryTypeMail
64   *
65   */
66  public class EntryTypeMail extends Entry
67  {
68      // Property
69      private static final String PROPERTY_MAILTO_FOR_ENTRY_MAIL = "directory.entry_type_mail.mailto";
70      private static final String PARAMETER_BANNED_DOMAIN_NAME = "banned_domain_names";
71      private static final String MESSAGE_WRONG_EMAIL_DOMAIN_NAME = "directory.message.entryTypeMail.wrongEmailDomainName";
72  
73      // HTML constants
74      private static final String HTML_LINK_OPEN_BEGIN = "<a href=\"mailto:";
75      private static final String HTML_LINK_OPEN_END = "\">";
76      private static final String HTML_LINK_CLOSE = "</a>";
77      private static final String SEMI_COLON = ";";
78      private static final String UNDERSCORE = "_";
79  
80      // Templates
81      private final String _template_create = "admin/plugins/directory/entrytypemail/create_entry_type_mail.html";
82      private final String _template_modify = "admin/plugins/directory/entrytypemail/modify_entry_type_mail.html";
83      private final String _template_html_code_form_entry = "admin/plugins/directory/entrytypemail/html_code_form_entry_type_mail.html";
84      private final String _template_html_code_form_search_entry = "admin/plugins/directory/entrytypemail/html_code_form_search_entry_type_mail.html";
85      private final String _template_html_code_entry_value = "admin/plugins/directory/entrytypemail/html_code_entry_value_type_mail.html";
86      private final String _template_html_front_code_form_entry = "skin/plugins/directory/entrytypemail/html_code_form_entry_type_mail.html";
87      private final String _template_html_front_code_form_search_entry = "skin/plugins/directory/entrytypemail/html_code_form_search_entry_type_mail.html";
88      private final String _template_html_front_code_entry_value = "skin/plugins/directory/entrytypemail/html_code_entry_value_type_mail.html";
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public String getTemplateHtmlFormEntry( boolean isDisplayFront )
95      {
96          if ( isDisplayFront )
97          {
98              return _template_html_front_code_form_entry;
99          }
100 
101         return _template_html_code_form_entry;
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public String getTemplateHtmlRecordFieldValue( boolean isDisplayFront )
109     {
110         if ( isDisplayFront )
111         {
112             return _template_html_front_code_entry_value;
113         }
114 
115         return _template_html_code_entry_value;
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     @Override
122     public String getTemplateHtmlFormSearchEntry( boolean isDisplayFront )
123     {
124         if ( isDisplayFront )
125         {
126             return _template_html_front_code_form_search_entry;
127         }
128 
129         return _template_html_code_form_search_entry;
130     }
131 
132     /**
133      * {@inheritDoc}
134      */
135     @Override
136     public String getEntryData( HttpServletRequest request, Locale locale )
137     {
138         String strTitle = request.getParameter( PARAMETER_TITLE );
139         String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
140         String strHelpMessageSearch = ( request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH )
141                 .trim( ) : null;
142         String strComment = request.getParameter( PARAMETER_COMMENT );
143         String strValue = request.getParameter( PARAMETER_VALUE );
144         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
145         String strIndexed = request.getParameter( PARAMETER_INDEXED );
146         String strIndexedAsTitle = request.getParameter( PARAMETER_INDEXED_AS_TITLE );
147         String strIndexedAsSummary = request.getParameter( PARAMETER_INDEXED_AS_SUMMARY );
148         String strShowInAdvancedSearch = request.getParameter( PARAMETER_SHOWN_IN_ADVANCED_SEARCH );
149         String strShowInResultList = request.getParameter( PARAMETER_SHOWN_IN_RESULT_LIST );
150         String strShowInResultRecord = request.getParameter( PARAMETER_SHOWN_IN_RESULT_RECORD );
151         String strWidth = request.getParameter( PARAMETER_WIDTH );
152         String strMaxSizeEnter = request.getParameter( PARAMETER_MAX_SIZE_ENTER );
153         String strShowInHistory = request.getParameter( PARAMETER_SHOWN_IN_HISTORY );
154         String strShowInExport = request.getParameter( PARAMETER_SHOWN_IN_EXPORT );
155         String strShowInCompleteness = request.getParameter( PARAMETER_SHOWN_IN_COMPLETENESS );
156 
157         int nWidth = DirectoryUtils.convertStringToInt( strWidth );
158         int nMaxSizeEnter = DirectoryUtils.convertStringToInt( strMaxSizeEnter );
159 
160         String strFieldError = DirectoryUtils.EMPTY_STRING;
161 
162         if ( ( strTitle == null ) || strTitle.trim( ).equals( DirectoryUtils.EMPTY_STRING ) )
163         {
164             strFieldError = FIELD_TITLE;
165         }
166 
167         else
168             if ( ( strWidth == null ) || strWidth.trim( ).equals( DirectoryUtils.EMPTY_STRING ) )
169             {
170                 strFieldError = FIELD_WIDTH;
171             }
172 
173         if ( !strFieldError.equals( DirectoryUtils.EMPTY_STRING ) )
174         {
175             Object [ ] tabRequiredFields = {
176                 I18nService.getLocalizedString( strFieldError, locale )
177             };
178 
179             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
180         }
181 
182         if ( nWidth == -1 )
183         {
184             strFieldError = FIELD_WIDTH;
185         }
186         else
187             if ( ( strMaxSizeEnter != null ) && !strMaxSizeEnter.trim( ).equals( DirectoryUtils.EMPTY_STRING ) && ( nMaxSizeEnter == -1 ) )
188             {
189                 strFieldError = FIELD_MAX_SIZE_ENTER;
190             }
191 
192         if ( !strFieldError.equals( DirectoryUtils.EMPTY_STRING ) )
193         {
194             Object [ ] tabRequiredFields = {
195                 I18nService.getLocalizedString( strFieldError, locale )
196             };
197 
198             return AdminMessageService.getMessageUrl( request, MESSAGE_NUMERIC_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
199         }
200 
201         this.setTitle( strTitle );
202         this.setHelpMessage( strHelpMessage );
203         this.setHelpMessageSearch( strHelpMessageSearch );
204         this.setComment( strComment );
205 
206         if ( this.getFields( ) == null )
207         {
208             ArrayList<Field> listFields = new ArrayList<Field>( );
209             Field field = new Field( );
210             listFields.add( field );
211             this.setFields( listFields );
212         }
213 
214         this.getFields( ).get( 0 ).setValue( strValue );
215         this.getFields( ).get( 0 ).setWidth( nWidth );
216         this.getFields( ).get( 0 ).setMaxSizeEnter( nMaxSizeEnter );
217         this.setMandatory( strMandatory != null );
218         this.setIndexed( strIndexed != null );
219         this.setIndexedAsTitle( strIndexedAsTitle != null );
220         this.setIndexedAsSummary( strIndexedAsSummary != null );
221         this.setShownInAdvancedSearch( strShowInAdvancedSearch != null );
222         this.setShownInResultList( strShowInResultList != null );
223         this.setShownInResultRecord( strShowInResultRecord != null );
224         this.setShownInHistory( strShowInHistory != null );
225         this.setShownInExport( strShowInExport != null );
226         this.setShownInCompleteness( strShowInCompleteness != null );
227 
228         return null;
229     }
230 
231     /**
232      * {@inheritDoc}
233      */
234     @Override
235     public String getTemplateCreate( )
236     {
237         return _template_create;
238     }
239 
240     /**
241      * {@inheritDoc}
242      */
243     @Override
244     public String getTemplateModify( )
245     {
246         return _template_modify;
247     }
248 
249     /**
250      * {@inheritDoc}
251      */
252     @Override
253     public Paginator getPaginator( int nItemPerPage, String strBaseUrl, String strPageIndexParameterName, String strPageIndex )
254     {
255         return new Paginator( this.getFields( ).get( 0 ).getRegularExpressionList( ), nItemPerPage, strBaseUrl, strPageIndexParameterName, strPageIndex );
256     }
257 
258     /**
259      * {@inheritDoc}
260      */
261     @Override
262     public ReferenceList getReferenceListRegularExpression( IEntry entry, Plugin plugin )
263     {
264         ReferenceList refListRegularExpression = null;
265 
266         if ( RegularExpressionService.getInstance( ).isAvailable( ) )
267         {
268             refListRegularExpression = new ReferenceList( );
269 
270             List<RegularExpression> listRegularExpression = RegularExpressionService.getInstance( ).getAllRegularExpression( );
271 
272             for ( RegularExpression regularExpression : listRegularExpression )
273             {
274                 if ( !entry.getFields( ).get( 0 ).getRegularExpressionList( ).contains( regularExpression ) )
275                 {
276                     refListRegularExpression.addItem( regularExpression.getIdExpression( ), regularExpression.getTitle( ) );
277                 }
278             }
279         }
280 
281         return refListRegularExpression;
282     }
283 
284     /**
285      * {@inheritDoc}
286      */
287     @Override
288     public void getRecordFieldData( Record record, List<String> lstValue, boolean bTestDirectoryError, boolean bAddNewValue, List<RecordField> listRecordField,
289             Locale locale ) throws DirectoryErrorException
290     {
291         Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
292 
293         String strValueEntry = ( ( lstValue != null ) && ( lstValue.size( ) > 0 ) ) ? lstValue.get( 0 ) : null;
294         List<RegularExpression> listRegularExpression = this.getFields( ).get( 0 ).getRegularExpressionList( );
295         RecordField response = new RecordField( );
296         response.setEntry( this );
297 
298         if ( ( record != null ) && bAddNewValue )
299         {
300             RecordFieldFilter recordFieldFilter = new RecordFieldFilter( );
301             recordFieldFilter.setIdDirectory( record.getDirectory( ).getIdDirectory( ) );
302             recordFieldFilter.setIdEntry( this.getIdEntry( ) );
303             recordFieldFilter.setIdRecord( record.getIdRecord( ) );
304 
305             List<RecordField> recordFieldList = RecordFieldHome.getRecordFieldList( recordFieldFilter, plugin );
306 
307             if ( ( recordFieldList != null ) && !recordFieldList.isEmpty( ) && StringUtils.isNotBlank( recordFieldList.get( 0 ).getValue( ) ) )
308             {
309                 strValueEntry = recordFieldList.get( 0 ).getValue( ) + ", " + strValueEntry;
310             }
311         }
312 
313         if ( strValueEntry != null )
314         {
315             if ( bTestDirectoryError && this.isMandatory( ) && strValueEntry.equals( DirectoryUtils.EMPTY_STRING ) )
316             {
317                 throw new DirectoryErrorException( this.getTitle( ) );
318             }
319 
320             if ( bTestDirectoryError && ( !strValueEntry.equals( DirectoryUtils.EMPTY_STRING ) ) && ( listRegularExpression != null )
321                     && ( listRegularExpression.size( ) != 0 ) && RegularExpressionService.getInstance( ).isAvailable( ) )
322             {
323                 for ( RegularExpression regularExpression : listRegularExpression )
324                 {
325                     if ( !RegularExpressionService.getInstance( ).isMatches( strValueEntry, regularExpression ) )
326                     {
327                         throw new DirectoryErrorException( this.getTitle( ), regularExpression.getErrorMessage( ) );
328                     }
329                 }
330             }
331 
332             String strBannedDomainNames = DatastoreService.getDataValue( plugin.getName( ) + UNDERSCORE + PARAMETER_BANNED_DOMAIN_NAME, StringUtils.EMPTY );
333 
334             if ( StringUtils.isNotBlank( strBannedDomainNames ) )
335             {
336                 String [ ] listBannedDomainName = strBannedDomainNames.split( SEMI_COLON );
337 
338                 if ( !StringUtil.checkEmailDomainName( strValueEntry, listBannedDomainName ) )
339                 {
340                     Object [ ] args = {
341                         strBannedDomainNames
342                     };
343                     String strErrorMessage = I18nService.getLocalizedString( MESSAGE_WRONG_EMAIL_DOMAIN_NAME, args, locale );
344                     throw new DirectoryErrorException( this.getTitle( ), strErrorMessage );
345                 }
346             }
347 
348             response.setValue( strValueEntry );
349         }
350 
351         listRecordField.add( response );
352     }
353 
354     /**
355      * {@inheritDoc}
356      */
357     @Override
358     public String convertRecordFieldTitleToString( RecordField recordField, Locale locale, boolean bDisplayFront )
359     {
360         String strTitle = super.convertRecordFieldTitleToString( recordField, locale, bDisplayFront );
361         boolean bIsMailto = Boolean.parseBoolean( AppPropertiesService.getProperty( PROPERTY_MAILTO_FOR_ENTRY_MAIL ) );
362 
363         // Add a mailto tag in front-office if the relative property is set to true
364         if ( StringUtils.isNotBlank( strTitle ) && bDisplayFront && bIsMailto )
365         {
366             strTitle = HTML_LINK_OPEN_BEGIN + strTitle + HTML_LINK_OPEN_END + strTitle + HTML_LINK_CLOSE;
367         }
368 
369         return strTitle;
370     }
371 
372     /**
373      * {@inheritDoc}
374      */
375     @Override
376     public boolean isSortable( )
377     {
378         return true;
379     }
380 
381     /**
382      * {@inheritDoc}
383      */
384     @Override
385     public LocalizedPaginator getPaginator( int nItemPerPage, String strBaseUrl, String strPageIndexParameterName, String strPageIndex, Locale locale )
386     {
387         return new LocalizedPaginator( this.getFields( ).get( 0 ).getRegularExpressionList( ), nItemPerPage, strBaseUrl, strPageIndexParameterName,
388                 strPageIndex, locale );
389     }
390 
391     /**
392      * Check if entries of this type are anonymizable or not.
393      * 
394      * @return True if the entry type is anonymizable, false otherwise
395      */
396     @Override
397     public boolean isAnonymizable( )
398     {
399         return true;
400     }
401 }