View Javadoc

1   /*
2    * Copyright (c) 2002-2014, 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.digglike.utils;
35  
36  import fr.paris.lutece.plugins.avatar.service.AvatarService;
37  import fr.paris.lutece.plugins.digglike.business.Category;
38  import fr.paris.lutece.plugins.digglike.business.CommentSubmit;
39  import fr.paris.lutece.plugins.digglike.business.Digg;
40  import fr.paris.lutece.plugins.digglike.business.DiggSubmit;
41  import fr.paris.lutece.plugins.digglike.business.DiggSubmitType;
42  import fr.paris.lutece.plugins.digglike.business.DiggUserInfo;
43  import fr.paris.lutece.plugins.digglike.business.EntryFilter;
44  import fr.paris.lutece.plugins.digglike.business.EntryHome;
45  import fr.paris.lutece.plugins.digglike.business.EntryType;
46  import fr.paris.lutece.plugins.digglike.business.EntryTypeHome;
47  import fr.paris.lutece.plugins.digglike.business.FormError;
48  import fr.paris.lutece.plugins.digglike.business.IEntry;
49  import fr.paris.lutece.plugins.digglike.business.ReportedMessage;
50  import fr.paris.lutece.plugins.digglike.business.Response;
51  import fr.paris.lutece.plugins.digglike.business.SubmitFilter;
52  import fr.paris.lutece.plugins.digglike.business.Vote;
53  import fr.paris.lutece.plugins.digglike.business.VoteHome;
54  import fr.paris.lutece.plugins.digglike.business.attribute.DiggAttribute;
55  import fr.paris.lutece.plugins.digglike.service.DiggSubmitService;
56  import fr.paris.lutece.plugins.digglike.web.action.DigglikeAdminSearchFields;
57  import fr.paris.lutece.portal.business.mailinglist.Recipient;
58  import fr.paris.lutece.portal.service.captcha.CaptchaSecurityService;
59  import fr.paris.lutece.portal.service.i18n.I18nService;
60  import fr.paris.lutece.portal.service.mail.MailService;
61  import fr.paris.lutece.portal.service.mailinglist.AdminMailingListService;
62  import fr.paris.lutece.portal.service.message.SiteMessageException;
63  import fr.paris.lutece.portal.service.plugin.Plugin;
64  import fr.paris.lutece.portal.service.plugin.PluginService;
65  import fr.paris.lutece.portal.service.template.AppTemplateService;
66  import fr.paris.lutece.portal.service.util.AppLogService;
67  import fr.paris.lutece.portal.service.util.AppPathService;
68  import fr.paris.lutece.portal.service.util.AppPropertiesService;
69  import fr.paris.lutece.util.ReferenceItem;
70  import fr.paris.lutece.util.ReferenceList;
71  import fr.paris.lutece.util.html.HtmlTemplate;
72  import fr.paris.lutece.util.string.StringUtil;
73  
74  import org.apache.commons.lang.StringUtils;
75  import org.springframework.util.ReflectionUtils;
76  
77  import java.sql.Timestamp;
78  
79  import java.text.DateFormat;
80  import java.text.ParseException;
81  
82  import java.util.ArrayList;
83  import java.util.Calendar;
84  import java.util.Collection;
85  import java.util.Date;
86  import java.util.GregorianCalendar;
87  import java.util.HashMap;
88  import java.util.Iterator;
89  import java.util.List;
90  import java.util.Locale;
91  import java.util.Map;
92  import java.util.TreeSet;
93  
94  import javax.servlet.http.HttpServletRequest;
95  import javax.servlet.http.HttpServletResponse;
96  
97  
98  /**
99   *
100  * class FormUtils
101  *
102  */
103 public final class DiggUtils
104 {
105     public static final int CONSTANT_ID_NULL = -1;
106     public static final int CONSTANT_SUBMIT_FILTER_TO_DAY = 1;
107     public static final int CONSTANT_SUBMIT_FILTER_WEEK = 2;
108     public static final int CONSTANT_SUBMIT_FILTER_MONTH = 3;
109     public static final int CONSTANT_SUBMIT_FILTER_YESTERDAY = 4;
110     public static final String SERVLET_IMAGE_PATH = "image?resource_type=image_digg&id=";
111     public static final String EMPTY_STRING = "";
112     public static final String PROPERTY_FILTER_ALL = "digglike.diggFrame.labelFilterAll";
113     public static final String PROPERTY_FILTER_TO_DAY = "digglike.diggFrame.labelFilterTopDay";
114     public static final String PROPERTY_FILTER_WEEK = "digglike.diggFrame.labelFilterWeek";
115     public static final String PROPERTY_FILTER_MONTH = "digglike.diggFrame.labelFilterMonth";
116     public static final String PROPERTY_FILTER_YESTERDAY = "digglike.diggFrame.labelFilterYesterday";
117     private static final String MARK_LOCALE = "locale";
118     private static final String MARK_ENTRY = "entry";
119     private static final String MARK_RESPONSE = "response";
120     private static final String MARK_CATEGORY_LIST = "category_list";
121     private static final String MARK_TYPE_LIST = "types_list";
122     private static final String MARK_DIGG = "digg";
123     private static final String MARK_DIGG_SUBMIT = "digg_submit";
124     private static final String MARK_COMMENT_SUBMIT = "comment_submit";
125     private static final String MARK_REPORTED_MESSAGE = "reported_message";
126     private static final String MARK_BASE_URL = "base_url";
127     private static final String MARK_JCAPTCHA = "jcaptcha";
128     private static final String MARK_STR_ENTRY = "str_entry";
129     private static final String MARK_IS_TITLE = "is_title";
130     private static final String PARAMETER_ID_ENTRY_TYPE = "id_type";
131     private static final String JCAPTCHA_PLUGIN = "jcaptcha";
132     private static final String CONSTANT_WHERE = " WHERE ";
133     private static final String CONSTANT_AND = " AND ";
134     private static final String MARK_ID_DEFAULT_CATEGORY = "id_default_category";
135     private static final String CONSTANT_CHARACTER_DOUBLE_QUOTE = "\"";
136     private static final String CONSTANT_CHARACTER_SIMPLE_QUOTE = "'";
137     private static final String CONSTANTE_CHARACTERNEW_LINE = "\n";
138     private static final String CONSTANTE_CHARACTER_RETURN = "\r";
139     private static final String MARK_AVATAR = "avatar";
140 
141     //	 Xml Tags
142 
143     //TEMPLATE
144     private static final String TEMPLATE_NOTIFICATION_MAIL_NEW_DIGG_SUBMIT = "skin/plugins/digglike/notification_mail_new_digg_submit.html";
145     private static final String TEMPLATE_NOTIFICATION_MAIL_NEW_DIGG_SUBMIT_DISABLE = "skin/plugins/digglike/notification_mail_new_digg_submit_disable.html";
146     private static final String TEMPLATE_NOTIFICATION_MAIL_NEW_COMMENT_SUBMIT = "skin/plugins/digglike/notification_mail_new_comment_submit.html";
147     private static final String TEMPLATE_NOTIFICATION_MAIL_NEW_REPORTED_MESSAGE = "skin/plugins/digglike/notification_mail_new_reported_message.html";
148 
149     //property
150     private static final String PROPERTY_NOTIFICATION_MAIL_NEW_DIGG_SUBMIT_SUBJECT = "digglike.notificationMailNewDiggSubmit.subject";
151     private static final String PROPERTY_NOTIFICATION_MAIL_NEW_DIGG_SUBMIT_SENDER_NAME = "digglike.notificationMailNewDiggSubmit.senderName";
152     private static final String PROPERTY_NOTIFICATION_MAIL_NEW_DIGG_SUBMIT_DISABLE_SUBJECT = "digglike.notificationMailNewDiggSubmitDisable.subject";
153     private static final String PROPERTY_NOTIFICATION_MAIL_NEW_DIGG_SUBMIT_DISABLE_SENDER_NAME = "digglike.notificationMailNewDiggSubmitDisable.senderName";
154     private static final String PROPERTY_NOTIFICATION_MAIL_NEW_COMMENT_SUBMIT_SUBJECT = "digglike.notificationMailNewCommentSubmit.subject";
155     private static final String PROPERTY_NOTIFICATION_MAIL_NEW_COMMENT_SUBMIT_SENDER_NAME = "digglike.notificationMailNewCommentSubmit.senderName";
156     private static final String PROPERTY_NOTIFICATION_MAIL_NEW_REPORTED_MESSAGE_SUBJECT = "digglike.notificationMailNewReportedMessage.subject";
157     private static final String PROPERTY_NOTIFICATION_MAIL_NEW_REPORTED_MESSAGE_SENDER_NAME = "digglike.notificationMailNewReportedMessage.senderName";
158     private static final String PROPERTY_SORTER_LIST_ITEM_DATE_RESPONSE_ASC = "digglike.sorterListItemDateResponseAsc";
159     private static final String PROPERTY_SORTER_LIST_ITEM_DATE_RESPONSE_DESC = "digglike.sorterListItemDateResponseDesc";
160     private static final String PROPERTY_COMMENT_STATE_ENABLE = "digglike.manageCommentSubmit.stateEnable";
161     private static final String PROPERTY_COMMENT_STATE_DISABLE = "digglike.manageCommentSubmit.stateDisable";
162     private static final String PROPERTY_SORTER_LIST_ITEM_SCORE_ASC = "digglike.sorterListItemScoreAsc";
163     private static final String PROPERTY_SORTER_LIST_ITEM_SCORE_DESC = "digglike.sorterListItemScoreDesc";
164     private static final String PROPERTY_SORTER_LIST_ITEM_VIEW_ASC = "digglike.sorterListItemViewAsc";
165     private static final String PROPERTY_SORTER_LIST_ITEM_VIEW_DESC = "digglike.sorterListItemViewDesc";
166     private static final String PROPERTY_SORTER_LIST_ITEM_AMOUNT_COMMENT_ASC = "digglike.sorterListItemCommentAsc";
167     private static final String PROPERTY_SORTER_LIST_ITEM_AMOUNT_COMMENT_DESC = "digglike.sorterListItemCommentDesc";
168     private static final String PROPERTY_SORTER_LIST_ITEM_MANUAL = "digglike.sorterListItemManualDesc";
169     private static final String REGEX_ID = "^[\\d]+$";
170     private static final String PROPERTY_CHOOSE_CATEGORY = "digglike.diggsubmit.choose.category";
171     private static final String PROPERTY_CHOOSE_TYPE = "digglike.diggsubmit.choose.type";
172     private static final String PROPERTY_PROD_URL = "lutece.prod.url";
173 
174     /**
175      * FormUtils
176      *
177      */
178     private DiggUtils(  )
179     {
180     }
181 
182     /**
183      * sendMail of notification for new digg submit
184      * @param digg the digg
185      * @param diggSubmit the new diggSubmit
186      * @param locale the locale
187      * @param request the request
188      */
189     public static void sendNotificationNewDiggSubmit( Digg digg, DiggSubmit diggSubmit, Locale locale,
190         HttpServletRequest request )
191     {
192         try
193         {
194             String strSubject = I18nService.getLocalizedString( PROPERTY_NOTIFICATION_MAIL_NEW_DIGG_SUBMIT_SUBJECT,
195                     locale );
196             String strSenderName = I18nService.getLocalizedString( PROPERTY_NOTIFICATION_MAIL_NEW_DIGG_SUBMIT_SENDER_NAME,
197                     locale );
198             String strSenderEmail = MailService.getNoReplyEmail(  );
199 
200             //we have to replace the src='image? string by a string containing the server url
201             if ( diggSubmit.getDiggSubmitValue(  ).toString(  ).contains( "src='image?" ) )
202             {
203                 diggSubmit.setDiggSubmitValue( diggSubmit.getDiggSubmitValue(  ).toString(  )
204                                                          .replace( "src='image?",
205                         "src='" + AppPropertiesService.getProperty( PROPERTY_PROD_URL ) + "/image?" ) );
206             }
207 
208             Collection<Recipient> listRecipients = AdminMailingListService.getRecipients( digg.getIdMailingListDiggSubmit(  ) );
209             Map<String, Object> model = new HashMap<String, Object>(  );
210             model.put( MARK_DIGG, digg );
211             model.put( MARK_DIGG_SUBMIT, diggSubmit );
212             model.put( MARK_BASE_URL, AppPathService.getBaseUrl( request ) );
213 
214             HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_NOTIFICATION_MAIL_NEW_DIGG_SUBMIT, locale, model );
215 
216             // Send Mail
217             for ( Recipient recipient : listRecipients )
218             {
219                 // Build the mail message
220                 MailService.sendMailHtml( recipient.getEmail(  ), strSenderName, strSenderEmail, strSubject,
221                     t.getHtml(  ) );
222             }
223         }
224         catch ( Exception e )
225         {
226             AppLogService.error( "Error during Notify new digg submit  : " + e.getMessage(  ) );
227         }
228     }
229 
230     /**
231      * sendMail of notification for new digg submit disable
232      * @param digg the digg
233      * @param diggSubmit the digg submit disable
234      * @param locale the locale
235      */
236     public static void sendNotificationNewDiggSubmitDisable( Digg digg, DiggSubmit diggSubmit, Locale locale )
237     {
238         try
239         {
240             String strSubject = I18nService.getLocalizedString( PROPERTY_NOTIFICATION_MAIL_NEW_DIGG_SUBMIT_DISABLE_SUBJECT,
241                     locale );
242             String strSenderName = I18nService.getLocalizedString( PROPERTY_NOTIFICATION_MAIL_NEW_DIGG_SUBMIT_DISABLE_SENDER_NAME,
243                     locale );
244             String strSenderEmail = MailService.getNoReplyEmail(  );
245 
246             //we have to replace the src='image? string by a string containing the server url
247             if ( diggSubmit.getDiggSubmitValue(  ).toString(  ).contains( "src='image?" ) )
248             {
249                 diggSubmit.setDiggSubmitValue( diggSubmit.getDiggSubmitValue(  ).toString(  )
250                                                          .replace( "src='image?",
251                         "src='" + AppPropertiesService.getProperty( PROPERTY_PROD_URL ) + "/image?" ) );
252             }
253 
254             Collection<Recipient> listRecipients = AdminMailingListService.getRecipients( digg.getIdMailingListDiggSubmit(  ) );
255             Map<String, Object> model = new HashMap<String, Object>(  );
256             model.put( MARK_DIGG, digg );
257             model.put( MARK_DIGG_SUBMIT, diggSubmit );
258 
259             HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_NOTIFICATION_MAIL_NEW_DIGG_SUBMIT_DISABLE,
260                     locale, model );
261 
262             // Send Mail
263             for ( Recipient recipient : listRecipients )
264             {
265                 // Build the mail message
266                 MailService.sendMailHtml( recipient.getEmail(  ), strSenderName, strSenderEmail, strSubject,
267                     t.getHtml(  ) );
268             }
269         }
270         catch ( Exception e )
271         {
272             AppLogService.error( "Error during Notify new digg submit disable  : " + e.getMessage(  ) );
273         }
274     }
275 
276     /**
277      * sendMail of notification for new comment submit
278      *
279      * @param digg the digg
280      * @param commentSubmit the new comment submit
281      * @param locale the locale
282      * @param request the request
283      */
284     public static void sendNotificationNewCommentSubmit( Digg digg, CommentSubmit commentSubmit, Locale locale,
285         HttpServletRequest request )
286     {
287         try
288         {
289             String strSubject = I18nService.getLocalizedString( PROPERTY_NOTIFICATION_MAIL_NEW_COMMENT_SUBMIT_SUBJECT,
290                     locale );
291             String strSenderName = I18nService.getLocalizedString( PROPERTY_NOTIFICATION_MAIL_NEW_COMMENT_SUBMIT_SENDER_NAME,
292                     locale );
293             String strSenderEmail = MailService.getNoReplyEmail(  );
294 
295             Collection<Recipient> listRecipients = AdminMailingListService.getRecipients( digg.getIdMailingListDiggSubmit(  ) );
296             Map<String, Object> model = new HashMap<String, Object>(  );
297             model.put( MARK_DIGG, digg );
298             model.put( MARK_COMMENT_SUBMIT, commentSubmit );
299             model.put( MARK_BASE_URL, AppPathService.getBaseUrl( request ) );
300 
301             HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_NOTIFICATION_MAIL_NEW_COMMENT_SUBMIT, locale,
302                     model );
303 
304             // Send Mail
305             for ( Recipient recipient : listRecipients )
306             {
307                 // Build the mail message
308                 MailService.sendMailHtml( recipient.getEmail(  ), strSenderName, strSenderEmail, strSubject,
309                     t.getHtml(  ) );
310             }
311         }
312         catch ( Exception e )
313         {
314             AppLogService.error( "Error during Notify new comment : " + e.getMessage(  ) );
315         }
316     }
317 
318     /**
319      * sendMail of notification for new reported message
320      * @param digg the digg
321      * @param reportedMessage the reported Message
322      * @param locale the locale
323      * @param request the request
324      */
325     public static void sendNotificationNewReportedMessage( Digg digg, ReportedMessage reportedMessage, Locale locale,
326         HttpServletRequest request )
327     {
328         try
329         {
330             String strSubject = I18nService.getLocalizedString( PROPERTY_NOTIFICATION_MAIL_NEW_REPORTED_MESSAGE_SUBJECT,
331                     locale );
332             String strSenderName = I18nService.getLocalizedString( PROPERTY_NOTIFICATION_MAIL_NEW_REPORTED_MESSAGE_SENDER_NAME,
333                     locale );
334             String strSenderEmail = MailService.getNoReplyEmail(  );
335 
336             Collection<Recipient> listRecipients = AdminMailingListService.getRecipients( digg.getIdMailingListDiggSubmit(  ) );
337             Map<String, Object> model = new HashMap<String, Object>(  );
338             model.put( MARK_DIGG, digg );
339             model.put( MARK_REPORTED_MESSAGE, reportedMessage );
340             model.put( MARK_BASE_URL, AppPathService.getBaseUrl( request ) );
341 
342             HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_NOTIFICATION_MAIL_NEW_REPORTED_MESSAGE, locale,
343                     model );
344 
345             // Send Mail
346             for ( Recipient recipient : listRecipients )
347             {
348                 // Build the mail message
349                 MailService.sendMailHtml( recipient.getEmail(  ), strSenderName, strSenderEmail, strSubject,
350                     t.getHtml(  ) );
351             }
352         }
353         catch ( Exception e )
354         {
355             AppLogService.error( "Error during Notify new repported message  : " + e.getMessage(  ) );
356         }
357     }
358 
359     /**
360      * return a timestamp Object which correspond with the string specified in
361      * parameter.
362      * @param strDate the date who must convert
363      * @param locale the locale
364      * @return a timestamp Object which correspond with the string specified in
365      *         parameter.
366      */
367     public static Timestamp getLastMinute( String strDate, Locale locale )
368     {
369         try
370         {
371             Date date;
372             DateFormat dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, locale );
373             dateFormat.setLenient( false );
374             date = dateFormat.parse( strDate.trim(  ) );
375 
376             Calendar caldate = new GregorianCalendar(  );
377             caldate.setTime( date );
378             caldate.set( Calendar.MILLISECOND, 0 );
379             caldate.set( Calendar.SECOND, 0 );
380             caldate.set( Calendar.HOUR_OF_DAY, caldate.getActualMaximum( Calendar.HOUR_OF_DAY ) );
381             caldate.set( Calendar.MINUTE, caldate.getActualMaximum( Calendar.MINUTE ) );
382 
383             Timestamp timeStamp = new Timestamp( caldate.getTimeInMillis(  ) );
384 
385             return timeStamp;
386         }
387         catch ( ParseException e )
388         {
389             return null;
390         }
391     }
392 
393     /**
394      * return a timestamp Object which correspond with the string specified in
395      * parameter.
396      * @param strDate the date who must convert
397      * @param locale the locale
398      * @return a timestamp Object which correspond with the string specified in
399      *         parameter.
400      */
401     public static Timestamp getFirstMinute( String strDate, Locale locale )
402     {
403         try
404         {
405             Date date;
406             DateFormat dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, locale );
407             dateFormat.setLenient( false );
408             date = dateFormat.parse( strDate.trim(  ) );
409 
410             Calendar caldate = new GregorianCalendar(  );
411             caldate.setTime( date );
412             caldate.set( Calendar.MILLISECOND, 0 );
413             caldate.set( Calendar.SECOND, 0 );
414             caldate.set( Calendar.HOUR_OF_DAY, caldate.getActualMinimum( Calendar.HOUR_OF_DAY ) );
415             caldate.set( Calendar.MINUTE, caldate.getActualMinimum( Calendar.MINUTE ) );
416 
417             Timestamp timeStamp = new Timestamp( caldate.getTimeInMillis(  ) );
418 
419             return timeStamp;
420         }
421         catch ( ParseException e )
422         {
423             return null;
424         }
425     }
426 
427     /**
428      * return the first day of week function of the date .
429      * @param date the date
430      * @return the first day of week function of the date.
431      */
432     public static Timestamp getFirstDayOfWeek( Timestamp date )
433     {
434         Calendar caldate = new GregorianCalendar(  );
435         caldate.setTime( date );
436         caldate.set( Calendar.MILLISECOND, caldate.getActualMinimum( Calendar.MILLISECOND ) );
437         caldate.set( Calendar.SECOND, caldate.getActualMinimum( Calendar.SECOND ) );
438         caldate.set( Calendar.HOUR_OF_DAY, caldate.getActualMinimum( Calendar.HOUR_OF_DAY ) );
439         caldate.set( Calendar.MINUTE, caldate.getActualMinimum( Calendar.MINUTE ) );
440         caldate.set( Calendar.DAY_OF_WEEK, caldate.getFirstDayOfWeek(  ) );
441 
442         Timestamp timeStamp = new Timestamp( caldate.getTimeInMillis(  ) );
443 
444         return timeStamp;
445     }
446 
447     /**
448      * return the last day of week function of the date .
449      * @param date the date
450      * @return the last day of week function of the date.
451      */
452     public static Timestamp getLastDayOfWeek( Timestamp date )
453     {
454         Calendar caldate = new GregorianCalendar(  );
455         caldate.setTime( date );
456         caldate.set( Calendar.MILLISECOND, caldate.getActualMaximum( Calendar.MILLISECOND ) );
457         caldate.set( Calendar.SECOND, caldate.getActualMaximum( Calendar.SECOND ) );
458         caldate.set( Calendar.HOUR_OF_DAY, caldate.getActualMaximum( Calendar.HOUR_OF_DAY ) );
459         caldate.set( Calendar.MINUTE, caldate.getActualMaximum( Calendar.MINUTE ) );
460         caldate.set( Calendar.DAY_OF_WEEK, caldate.getFirstDayOfWeek(  ) + 6 );
461 
462         Timestamp timeStamp = new Timestamp( caldate.getTimeInMillis(  ) );
463 
464         return timeStamp;
465     }
466 
467     /**
468      * return a timestamp Object which correspond at the fist minute of the date
469      * .
470      * @param date the date
471      * @return a timestamp Object which correspond at the fist minute of the
472      *         date .
473      */
474     public static Timestamp getFirstMinute( Timestamp date )
475     {
476         Calendar caldate = new GregorianCalendar(  );
477         caldate.setTime( date );
478         caldate.set( Calendar.MILLISECOND, caldate.getActualMinimum( Calendar.MILLISECOND ) );
479         caldate.set( Calendar.SECOND, caldate.getActualMinimum( Calendar.SECOND ) );
480         caldate.set( Calendar.HOUR_OF_DAY, caldate.getActualMinimum( Calendar.HOUR_OF_DAY ) );
481         caldate.set( Calendar.MINUTE, caldate.getActualMinimum( Calendar.MINUTE ) );
482 
483         Timestamp timeStamp = new Timestamp( caldate.getTimeInMillis(  ) );
484 
485         return timeStamp;
486     }
487 
488     /**
489      * return a timestamp Object which correspond at the last minute of the date
490      * .
491      * @param date the date
492      * @return a timestamp Object which correspond at the last minute of the
493      *         date .
494      */
495     public static Timestamp getLastMinute( Timestamp date )
496     {
497         Calendar caldate = new GregorianCalendar(  );
498         caldate.setTime( date );
499         caldate.set( Calendar.MILLISECOND, caldate.getActualMaximum( Calendar.MILLISECOND ) );
500         caldate.set( Calendar.SECOND, caldate.getActualMaximum( Calendar.SECOND ) );
501         caldate.set( Calendar.HOUR_OF_DAY, caldate.getActualMaximum( Calendar.HOUR_OF_DAY ) );
502         caldate.set( Calendar.MINUTE, caldate.getActualMaximum( Calendar.MINUTE ) );
503 
504         Timestamp timeStamp = new Timestamp( caldate.getTimeInMillis(  ) );
505 
506         return timeStamp;
507     }
508 
509     /**
510      * return the first day of month function of the date .
511      * @param date the date
512      * @return the first day of mont function of the date.
513      */
514     public static Timestamp getFirstDayOfMonth( Timestamp date )
515     {
516         Calendar caldate = new GregorianCalendar(  );
517         caldate.setTime( date );
518         caldate.set( Calendar.MILLISECOND, caldate.getActualMinimum( Calendar.MILLISECOND ) );
519         caldate.set( Calendar.SECOND, caldate.getActualMinimum( Calendar.SECOND ) );
520         caldate.set( Calendar.HOUR_OF_DAY, caldate.getActualMinimum( Calendar.HOUR_OF_DAY ) );
521         caldate.set( Calendar.MINUTE, caldate.getActualMinimum( Calendar.MINUTE ) );
522         caldate.set( Calendar.DAY_OF_MONTH, caldate.getActualMinimum( Calendar.DAY_OF_MONTH ) );
523 
524         Timestamp timeStamp = new Timestamp( caldate.getTimeInMillis(  ) );
525 
526         return timeStamp;
527     }
528 
529     /**
530      * return the last day of month function of the date .
531      * @param date the date
532      * @return the last day of mont function of the date.
533      */
534     public static Timestamp getLastDayOfMonth( Timestamp date )
535     {
536         Calendar caldate = new GregorianCalendar(  );
537         caldate.setTime( date );
538         caldate.set( Calendar.MILLISECOND, caldate.getActualMaximum( Calendar.MILLISECOND ) );
539         caldate.set( Calendar.SECOND, caldate.getActualMaximum( Calendar.SECOND ) );
540         caldate.set( Calendar.HOUR_OF_DAY, caldate.getActualMaximum( Calendar.HOUR_OF_DAY ) );
541         caldate.set( Calendar.MINUTE, caldate.getActualMaximum( Calendar.MINUTE ) );
542         caldate.set( Calendar.DAY_OF_MONTH, caldate.getActualMaximum( Calendar.DAY_OF_MONTH ) );
543 
544         Timestamp timeStamp = new Timestamp( caldate.getTimeInMillis(  ) );
545 
546         return timeStamp;
547     }
548 
549     /**
550      * Converts une java.sql.Timestamp date in a String date in a "jj/mm/aaaa"
551      * format
552      *
553      * @param date java.sql.Timestamp date to convert
554      * @param locale the locale
555      * @return strDate The String date in the short locale format or the emmpty
556      *         String if the date is null
557      */
558     public static String getDateString( Timestamp date, Locale locale )
559     {
560         DateFormat dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, locale );
561 
562         return dateFormat.format( date );
563     }
564 
565     /**
566      * return current date
567      * @return return current date
568      */
569     public static Timestamp getCurrentDate(  )
570     {
571         return new Timestamp( GregorianCalendar.getInstance(  ).getTimeInMillis(  ) );
572     }
573 
574     /**
575      * Return a date corresponding to the date provides in parameter add with a
576      * number of day
577      * @param date the date
578      * @param nDay the number of day to add
579      * @return a timestamp Object which correspond at the date + nDay .
580      */
581     public static Timestamp getDateAfterNDay( Timestamp date, int nDay )
582     {
583         Calendar caldate = new GregorianCalendar(  );
584         caldate.setTime( date );
585         caldate.add( Calendar.DATE, nDay );
586 
587         return new Timestamp( caldate.getTimeInMillis(  ) );
588     }
589 
590     /**
591      * return an instance of IEntry function of type entry
592      * @param request the request
593      * @param plugin the plugin
594      * @return an instance of IEntry function of type entry
595      */
596     public static IEntry createEntryByType( HttpServletRequest request, Plugin plugin )
597     {
598         String strIdType = request.getParameter( PARAMETER_ID_ENTRY_TYPE );
599         int nIdType = -1;
600         IEntry entry = null;
601         EntryType entryType;
602 
603         if ( ( strIdType != null ) && !strIdType.equals( EMPTY_STRING ) )
604         {
605             try
606             {
607                 nIdType = Integer.parseInt( strIdType );
608             }
609             catch ( NumberFormatException ne )
610             {
611                 AppLogService.error( ne );
612 
613                 return null;
614             }
615         }
616 
617         if ( nIdType == -1 )
618         {
619             return null;
620         }
621 
622         entryType = EntryTypeHome.findByPrimaryKey( nIdType, plugin );
623 
624         try
625         {
626             entry = (IEntry) Class.forName( entryType.getClassName(  ) ).newInstance(  );
627             entry.setEntryType( entryType );
628         }
629         catch ( ClassNotFoundException e )
630         {
631             //  class doesn't exist
632             AppLogService.error( e );
633         }
634         catch ( InstantiationException e )
635         {
636             // Class is abstract or is an  interface or haven't accessible builder
637             AppLogService.error( e );
638         }
639         catch ( IllegalAccessException e )
640         {
641             // can't access to rhe class
642             AppLogService.error( e );
643         }
644 
645         return entry;
646     }
647 
648     /**
649      * return the index in the list of the entry whose key is specified in
650      * parameter
651      * @param nIdEntry the key of the entry
652      * @param listEntry the list of the entry
653      * @return the index in the list of the entry whose key is specified in
654      *         parameter
655      */
656     public static int getIndexEntryInTheEntryList( int nIdEntry, List<IEntry> listEntry )
657     {
658         int nIndex = 0;
659 
660         for ( IEntry entry : listEntry )
661         {
662             if ( entry.getIdEntry(  ) == nIdEntry )
663             {
664                 return nIndex;
665             }
666 
667             nIndex++;
668         }
669 
670         return nIndex;
671     }
672 
673     /**
674      *
675      * @param digg
676      * @param plugin
677      * @param locale
678      * @param nIdDefaultCategory
679      * @param bBackOffice
680      * @return
681      */
682     public static Map<String, Object> getModelHtmlForm( Digg digg, Plugin plugin, Locale locale,
683         int nIdDefaultCategory, boolean bBackOffice )
684     {
685         List<IEntry> listEntryFirstLevel;
686         Map<String, Object> model = new HashMap<String, Object>(  );
687 
688         EntryFilter filter;
689         StringBuffer strBuffer = new StringBuffer(  );
690         filter = new EntryFilter(  );
691         filter.setIdDigg( digg.getIdDigg(  ) );
692         listEntryFirstLevel = EntryHome.getEntryList( filter, plugin );
693 
694         ArrayList<Category> listCats = new ArrayList<Category>(  );
695         Category category = new Category(  );
696 
697         category.setIdCategory( -1 );
698         category.setTitle( I18nService.getLocalizedString( PROPERTY_CHOOSE_CATEGORY, locale ) );
699 
700         if ( !digg.getCategories(  ).isEmpty(  ) )
701         {
702             listCats.add( category );
703         }
704 
705         listCats.addAll( digg.getCategories(  ) );
706 
707         DiggSubmitType type = new DiggSubmitType(  );
708         List<DiggSubmitType> listTypes = digg.getDiggSubmitTypes(  );
709         List<DiggSubmitType> listTypes2Show = new ArrayList<DiggSubmitType>(  );
710 
711         type.setIdType( -1 );
712         type.setName( I18nService.getLocalizedString( PROPERTY_CHOOSE_TYPE, locale ) );
713 
714         for ( DiggSubmitType t : listTypes )
715         {
716             if ( bBackOffice || t.getParameterizableInFO(  ) )
717             {
718                 listTypes2Show.add( t );
719             }
720         }
721 
722         if ( !listTypes2Show.isEmpty(  ) )
723         {
724             listTypes2Show.add( 0, type );
725         }
726 
727         ReferenceList refCategoryList = getRefListCategory( listCats );
728         ReferenceList refTypeList = getRefListType( listTypes2Show );
729 
730         for ( IEntry entry : listEntryFirstLevel )
731         {
732             DiggUtils.getHtmlFormEntry( entry.getIdEntry(  ), plugin, strBuffer, locale );
733         }
734 
735         CaptchaSecurityService captchaSecurityService = new CaptchaSecurityService(  );
736 
737         if ( digg.isActiveCaptcha(  ) && PluginService.isPluginEnable( JCAPTCHA_PLUGIN ) )
738         {
739             model.put( MARK_JCAPTCHA, captchaSecurityService.getHtmlCode(  ) );
740         }
741 
742         model.put( MARK_CATEGORY_LIST, refCategoryList );
743         model.put( MARK_TYPE_LIST, refTypeList );
744         model.put( MARK_ID_DEFAULT_CATEGORY, nIdDefaultCategory );
745         model.put( MARK_DIGG, digg );
746         model.put( MARK_STR_ENTRY, strBuffer.toString(  ) );
747         model.put( MARK_LOCALE, locale );
748 
749         return model;
750     }
751 
752     /**
753      * insert in the string buffer the content of the html code of the entry
754      * @param nIdEntry the key of the entry which html code must be insert in
755      *            the stringBuffer
756      * @param plugin the plugin
757      * @param stringBuffer the buffer which contains the html code
758      * @param locale the locale
759      */
760     public static void getHtmlFormEntry( int nIdEntry, Plugin plugin, StringBuffer stringBuffer, Locale locale )
761     {
762         Map<String, Object> model = new HashMap<String, Object>(  );
763         HtmlTemplate template;
764         IEntry entry = EntryHome.findByPrimaryKey( nIdEntry, plugin );
765         model.put( MARK_ENTRY, entry );
766 
767         template = AppTemplateService.getTemplate( entry.getTemplateHtmlCodeForm(  ), locale, model );
768         stringBuffer.append( template.getHtml(  ) );
769     }
770 
771     /**
772      * return the content of the html code response
773      * @param response the response which html code must be generate
774      * @param stringBuffer the stringBuffer
775      * @param bTitle true if the response is a title
776      * @param locale the locale
777      */
778     public static void getHtmlResponseEntry( Response response, StringBuffer stringBuffer, Locale locale, boolean bTitle )
779     {
780         if ( ( response != null ) && ( response.getEntry(  ) != null ) )
781         {
782             Map<String, Object> model = new HashMap<String, Object>(  );
783             HtmlTemplate template;
784             model.put( MARK_RESPONSE, response );
785             model.put( MARK_IS_TITLE, bTitle );
786             template = AppTemplateService.getTemplate( response.getEntry(  ).getTemplateHtmlCodeResponse(  ), locale,
787                     model );
788             stringBuffer.append( template.getHtml(  ) );
789         }
790     }
791 
792     /**
793      * return the content of the html code of the digg submit
794      * @param diggSubmit the diggsubmit
795      * @param locale the locale
796      * @return the content of the html code of the digg submit
797      */
798     public static String getHtmlDiggSubmitValue( DiggSubmit diggSubmit, Locale locale )
799     {
800         StringBuffer strBuffer = new StringBuffer(  );
801         int ncptTitle = 1;
802 
803         for ( Response response : diggSubmit.getResponses(  ) )
804         {
805             if ( ncptTitle == 1 )
806             {
807                 getHtmlResponseEntry( response, strBuffer, locale, true );
808             }
809             else
810             {
811                 getHtmlResponseEntry( response, strBuffer, locale, false );
812             }
813 
814             ncptTitle++;
815         }
816 
817         return strBuffer.toString(  );
818     }
819 
820     /**
821      * return the content of the html code of the digg submit show in the list
822      * of digg submit
823      * @param diggSubmit the diggsubmit
824      * @param locale the locale
825      * @return the content of the html code of the digg submit show in the list
826      *         of digg submit
827      */
828     public static String getHtmlDiggSubmitValueShowInTheList( DiggSubmit diggSubmit, Locale locale )
829     {
830         StringBuffer strBuffer = new StringBuffer(  );
831         int nNumberCaractersShown = diggSubmit.getDigg(  ).getNumberDiggSubmitCaractersShown(  );
832         int nNumberCaractersInBuffer = 0;
833         int ncptTitle = 1;
834 
835         for ( Response response : diggSubmit.getResponses(  ) )
836         {
837             if ( ( response.getValueResponse(  ) != null ) && ( response.getEntry(  ) != null ) &&
838                     response.getEntry(  ).isShowInDiggSubmitList(  ) )
839             {
840                 if ( ( nNumberCaractersInBuffer + response.getValueResponse(  ).length(  ) ) <= nNumberCaractersShown )
841                 {
842                     nNumberCaractersInBuffer += response.getValueResponse(  ).length(  );
843 
844                     if ( ncptTitle == 1 )
845                     {
846                         getHtmlResponseEntry( response, strBuffer, locale, true );
847                     }
848                     else
849                     {
850                         getHtmlResponseEntry( response, strBuffer, locale, false );
851                     }
852                 }
853                 else
854                 {
855                     Response lastResponse = new Response(  );
856                     lastResponse.setEntry( response.getEntry(  ) );
857                     lastResponse.setValueResponse( response.getValueResponse(  )
858                                                            .substring( 0,
859                             nNumberCaractersShown - nNumberCaractersInBuffer ) + "..." );
860 
861                     if ( ncptTitle == 1 )
862                     {
863                         getHtmlResponseEntry( lastResponse, strBuffer, locale, true );
864                     }
865                     else
866                     {
867                         getHtmlResponseEntry( lastResponse, strBuffer, locale, false );
868                     }
869 
870                     break;
871                 }
872             }
873 
874             ncptTitle++;
875         }
876 
877         return strBuffer.toString(  );
878     }
879 
880     /**
881      * return the title of the digg submit
882      * @param diggSubmit the diggsubmit
883      * @param locale the locale
884      * @return the title of the digg submit
885      */
886     public static String getDiggSubmitTitle( DiggSubmit diggSubmit, Locale locale )
887     {
888         StringBuffer strBuffer = new StringBuffer(  );
889 
890         if ( ( diggSubmit.getResponses(  ) != null ) && ( diggSubmit.getResponses(  ).size(  ) != 0 ) &&
891                 ( diggSubmit.getResponses(  ).get( 0 ) != null ) )
892         {
893             strBuffer.append( diggSubmit.getResponses(  ).get( 0 ).getValueResponse(  ) );
894         }
895 
896         return strBuffer.toString(  );
897     }
898 
899     /**
900      * perform in the object diggSubmit the responses associates to the
901      * diggsubmit
902      * @param request
903      * @param diggSubmit
904      * @param plugin
905      * @param locale
906      * @return
907      * @throws SiteMessageException
908      */
909     public static FormError getAllResponsesData( HttpServletRequest request, DiggSubmit diggSubmit, Plugin plugin,
910         Locale locale )
911     {
912         List<IEntry> listEntry;
913         EntryFilter filter;
914         FormError formError = null;
915 
916         filter = new EntryFilter(  );
917         filter.setIdDigg( diggSubmit.getDigg(  ).getIdDigg(  ) );
918         listEntry = EntryHome.getEntryList( filter, plugin );
919 
920         List<Response> listResponse = new ArrayList<Response>(  );
921         diggSubmit.setResponses( listResponse );
922 
923         for ( IEntry entry : listEntry )
924         {
925             formError = getResponseEntry( request, entry.getIdEntry(  ), plugin, diggSubmit, false, locale );
926 
927             if ( formError != null )
928             {
929                 return formError;
930             }
931         }
932 
933         return null;
934     }
935 
936     /**
937      * perform in the object diggSubmit the responses associates with a entry
938      * specify in parameter.
939      * return null if there is no error in the response else return a FormError
940      * Object
941      * @param request the request
942      * @param nIdEntry the key of the entry
943      * @param plugin the plugin
944      * @param diggSubmit digg Submit Object
945      * @param bResponseNull true if the response create must be null
946      * @param locale the locale
947      * @return null if there is no error in the response else return a FormError
948      *         Object
949      */
950     public static FormError getResponseEntry( HttpServletRequest request, int nIdEntry, Plugin plugin,
951         DiggSubmit diggSubmit, boolean bResponseNull, Locale locale )
952     {
953         FormError formError = null;
954         Response response = null;
955         IEntry entry = null;
956         List<Response> listResponse = new ArrayList<Response>(  );
957         entry = EntryHome.findByPrimaryKey( nIdEntry, plugin );
958 
959         if ( !bResponseNull )
960         {
961             formError = entry.getResponseData( diggSubmit.getIdDiggSubmit(  ), request, listResponse, locale, plugin );
962         }
963         else
964         {
965             response = new Response(  );
966             response.setEntry( entry );
967             listResponse.add( response );
968         }
969 
970         if ( formError != null )
971         {
972             return formError;
973         }
974 
975         diggSubmit.getResponses(  ).addAll( listResponse );
976 
977         return null;
978     }
979 
980     /**
981      * convert a string to int
982      * @param strParameter the string parameter to convert
983      * @return the conversion
984      */
985     public static int getIntegerParameter( String strParameter )
986     {
987         int nIdParameter = -1;
988 
989         try
990         {
991             if ( ( strParameter != null ) && strParameter.matches( REGEX_ID ) )
992             {
993                 nIdParameter = Integer.parseInt( strParameter );
994             }
995         }
996         catch ( NumberFormatException ne )
997         {
998             AppLogService.error( ne );
999         }
1000 
1001         return nIdParameter;
1002     }
1003 
1004     /**
1005      * Returns a copy of the string , with leading and trailing whitespace
1006      * omitted.
1007      * @param strParameter the string parameter to convert
1008      * @return null if the strParameter is null other return with leading and
1009      *         trailing whitespace omitted.
1010      */
1011     public static String trim( String strParameter )
1012     {
1013         if ( strParameter != null )
1014         {
1015             return strParameter.trim(  );
1016         }
1017 
1018         return strParameter;
1019     }
1020 
1021     /**
1022      * initialized the submit filter object with the period specified in
1023      * parameter
1024      * @param submitFilter the filter to initialized
1025      * @param nIdPeriod the id of the period (DAY,WEEK,MONTH)
1026      */
1027     public static void initSubmitFilterByPeriod( SubmitFilter submitFilter, int nIdPeriod )
1028     {
1029         Timestamp date = getCurrentDate(  );
1030 
1031         switch ( nIdPeriod )
1032         {
1033             case CONSTANT_SUBMIT_FILTER_YESTERDAY:
1034                 date = getDateAfterNDay( date, -1 );
1035                 submitFilter.setDateFirst( getFirstMinute( date ) );
1036                 submitFilter.setDateLast( getLastMinute( date ) );
1037 
1038                 break;
1039 
1040             case CONSTANT_SUBMIT_FILTER_TO_DAY:
1041                 submitFilter.setDateFirst( getFirstMinute( date ) );
1042                 submitFilter.setDateLast( getLastMinute( date ) );
1043 
1044                 break;
1045 
1046             case CONSTANT_SUBMIT_FILTER_WEEK:
1047                 submitFilter.setDateFirst( getFirstDayOfWeek( date ) );
1048                 submitFilter.setDateLast( getLastDayOfWeek( date ) );
1049 
1050                 break;
1051 
1052             case CONSTANT_SUBMIT_FILTER_MONTH:
1053                 submitFilter.setDateFirst( getFirstDayOfMonth( date ) );
1054                 submitFilter.setDateLast( getLastDayOfMonth( date ) );
1055 
1056                 break;
1057 
1058             default:
1059                 break;
1060         }
1061     }
1062 
1063     /**
1064      * initialized the submit filter object with the sort specified in parameter
1065      * @param submitFilter the filter to initialized
1066      * @param nIdSort the id of the sort(date response, score)
1067      */
1068     public static void initSubmitFilterBySort( SubmitFilter submitFilter, int nIdSort )
1069     {
1070         switch ( nIdSort )
1071         {
1072             case SubmitFilter.SORT_BY_DATE_RESPONSE_ASC:
1073                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_DATE_RESPONSE_ASC );
1074 
1075                 break;
1076 
1077             case SubmitFilter.SORT_BY_DATE_RESPONSE_DESC:
1078                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_DATE_RESPONSE_DESC );
1079 
1080                 break;
1081 
1082             case SubmitFilter.SORT_BY_SCORE_ASC:
1083                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_SCORE_ASC );
1084                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_DATE_RESPONSE_DESC );
1085 
1086                 break;
1087 
1088             case SubmitFilter.SORT_BY_SCORE_DESC:
1089                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_SCORE_DESC );
1090                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_DATE_RESPONSE_DESC );
1091 
1092                 break;
1093 
1094             case SubmitFilter.SORT_BY_NUMBER_COMMENT_ASC:
1095                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_NUMBER_COMMENT_ASC );
1096                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_DATE_RESPONSE_DESC );
1097 
1098                 break;
1099 
1100             case SubmitFilter.SORT_BY_NUMBER_COMMENT_DESC:
1101                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_NUMBER_COMMENT_DESC );
1102                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_DATE_RESPONSE_DESC );
1103 
1104                 break;
1105 
1106             case SubmitFilter.SORT_MANUALLY:
1107                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_MANUALLY );
1108 
1109                 break;
1110 
1111             case SubmitFilter.SORT_BY_NUMBER_VIEW_ASC:
1112                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_NUMBER_VIEW_ASC );
1113                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_DATE_RESPONSE_DESC );
1114 
1115                 break;
1116 
1117             case SubmitFilter.SORT_BY_NUMBER_VIEW_DESC:
1118                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_NUMBER_VIEW_DESC );
1119                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_DATE_RESPONSE_DESC );
1120 
1121                 break;
1122 
1123             case SubmitFilter.SORT_BY_PINNED_FIRST:
1124                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_PINNED_FIRST );
1125 
1126                 break;
1127 
1128             default:
1129                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_SCORE_DESC );
1130                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_DATE_RESPONSE_DESC );
1131 
1132                 break;
1133         }
1134     }
1135 
1136     /**
1137      * initialized the submit filter object with the sort specified in parameter
1138      * @param submitFilter the filter to initialized
1139      * @param nIdSort the id of the sort(date response, score)
1140      */
1141     public static void initCommentFilterBySort( SubmitFilter submitFilter, int nIdSort )
1142     {
1143         switch ( nIdSort )
1144         {
1145             case SubmitFilter.SORT_BY_DATE_RESPONSE_ASC:
1146                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_DATE_RESPONSE_ASC );
1147 
1148                 break;
1149 
1150             case SubmitFilter.SORT_BY_DATE_RESPONSE_DESC:
1151                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_DATE_RESPONSE_DESC );
1152 
1153                 break;
1154 
1155             case SubmitFilter.SORT_MANUALLY:
1156                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_MANUALLY );
1157 
1158                 break;
1159 
1160             case SubmitFilter.SORT_BY_DATE_MODIFY_ASC:
1161                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_DATE_MODIFY_ASC );
1162 
1163                 break;
1164 
1165             case SubmitFilter.SORT_BY_DATE_MODIFY_DESC:
1166                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_DATE_MODIFY_DESC );
1167 
1168                 break;
1169 
1170             default:
1171                 submitFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_DATE_MODIFY_DESC );
1172 
1173                 break;
1174         }
1175     }
1176 
1177     /**
1178      * Perform the vote on a digg submit
1179      * @param nIdDiggSubmit the id of the digg submit
1180      * @param strLuteceUserKey the key of the lutece user who have vote
1181      * @param nScore the score of the vote
1182      * @param plugin the plugin
1183      */
1184     public static void doVoteDiggSubmit( int nIdDiggSubmit, int nScore, String strLuteceUserKey, Plugin plugin )
1185     {
1186         DiggSubmit diggSubmit = DiggSubmitService.getService(  ).findByPrimaryKey( nIdDiggSubmit, false, plugin );
1187 
1188         if ( diggSubmit != null )
1189         {
1190             diggSubmit.setNumberVote( diggSubmit.getNumberVote(  ) + 1 );
1191             diggSubmit.setNumberScore( diggSubmit.getNumberScore(  ) + nScore );
1192             DiggSubmitService.getService(  ).update( diggSubmit, plugin );
1193 
1194             if ( strLuteceUserKey != null )
1195             {
1196                 Vote vote = new Vote(  );
1197                 vote.setLuteceUserKey( strLuteceUserKey );
1198                 vote.setIdDiggSubmit( nIdDiggSubmit );
1199                 VoteHome.create( vote, plugin );
1200             }
1201         }
1202     }
1203 
1204     /**
1205      * Perform the report on a digg submit
1206      * @param diggSubmit the digg submit
1207      * @param plugin the plugin
1208      */
1209     public static void doReportDiggSubmit( DiggSubmit diggSubmit, Plugin plugin )
1210     {
1211         if ( diggSubmit != null )
1212         {
1213             diggSubmit.setReported( true );
1214             DiggSubmitService.getService(  ).update( diggSubmit, plugin );
1215         }
1216     }
1217 
1218     /**
1219      * Init reference list with the different categories
1220      *
1221      *
1222      * @param listCategories the list of categories
1223      * @return reference list of category
1224      */
1225     public static ReferenceList getRefListCategory( List<Category> listCategories )
1226     {
1227         ReferenceList refListCategories = new ReferenceList(  );
1228 
1229         for ( Category category : listCategories )
1230         {
1231             refListCategories.addItem( category.getIdCategory(  ), category.getTitle(  ) );
1232         }
1233 
1234         return refListCategories;
1235     }
1236 
1237     /**
1238      * Init reference list with the different types
1239      *
1240      *
1241      * @param listTypes the list of types
1242      * @return reference list of type
1243      */
1244     public static ReferenceList getRefListType( List<DiggSubmitType> listTypes )
1245     {
1246         ReferenceList refListTypes = new ReferenceList(  );
1247 
1248         for ( DiggSubmitType type : listTypes )
1249         {
1250             refListTypes.addItem( type.getIdType(  ), type.getName(  ) );
1251         }
1252 
1253         return refListTypes;
1254     }
1255 
1256     /**
1257      * Init reference list with the different categories
1258      *
1259      *
1260      * @param listDiggs the list of categories
1261      * @param bIncludeDefault true if a default item must be insert
1262      *
1263      * @return reference list of category
1264      */
1265     public static ReferenceList getRefListDigg( List<Digg> listDiggs, boolean bIncludeDefault )
1266     {
1267         ReferenceList refListDiggs = new ReferenceList(  );
1268 
1269         if ( bIncludeDefault )
1270         {
1271             refListDiggs.addItem( CONSTANT_ID_NULL, EMPTY_STRING );
1272         }
1273 
1274         for ( Digg digg : listDiggs )
1275         {
1276             refListDiggs.addItem( digg.getIdDigg(  ), digg.getTitle(  ) );
1277         }
1278 
1279         return refListDiggs;
1280     }
1281 
1282     /**
1283      * Init reference list width the different sort
1284      *
1285      * @param locale the locale
1286      * @return reference list of sort
1287      */
1288     public static ReferenceList getRefListDiggSort( Locale locale )
1289     {
1290         return getRefListDiggSort( locale, false );
1291     }
1292 
1293     /**
1294      * Init reference list width the different sort
1295      *
1296      * @param locale the locale
1297      * @param bFront yes if the refList is display in front office
1298      * @return reference list of sort
1299      */
1300     public static ReferenceList getRefListDiggSort( Locale locale, boolean bFront )
1301     {
1302         ReferenceList refListSorter = new ReferenceList(  );
1303 
1304         refListSorter.addItem( CONSTANT_ID_NULL, EMPTY_STRING );
1305         addEmptyItem( refListSorter );
1306         refListSorter.addItem( SubmitFilter.SORT_BY_DATE_RESPONSE_ASC,
1307             I18nService.getLocalizedString( PROPERTY_SORTER_LIST_ITEM_DATE_RESPONSE_ASC, locale ) );
1308         refListSorter.addItem( SubmitFilter.SORT_BY_DATE_RESPONSE_DESC,
1309             I18nService.getLocalizedString( PROPERTY_SORTER_LIST_ITEM_DATE_RESPONSE_DESC, locale ) );
1310         refListSorter.addItem( SubmitFilter.SORT_BY_SCORE_ASC,
1311             I18nService.getLocalizedString( PROPERTY_SORTER_LIST_ITEM_SCORE_ASC, locale ) );
1312         refListSorter.addItem( SubmitFilter.SORT_BY_SCORE_DESC,
1313             I18nService.getLocalizedString( PROPERTY_SORTER_LIST_ITEM_SCORE_DESC, locale ) );
1314         refListSorter.addItem( SubmitFilter.SORT_BY_NUMBER_COMMENT_ASC,
1315             I18nService.getLocalizedString( PROPERTY_SORTER_LIST_ITEM_AMOUNT_COMMENT_ASC, locale ) );
1316         refListSorter.addItem( SubmitFilter.SORT_BY_NUMBER_COMMENT_DESC,
1317             I18nService.getLocalizedString( PROPERTY_SORTER_LIST_ITEM_AMOUNT_COMMENT_DESC, locale ) );
1318 
1319         if ( !bFront )
1320         {
1321             refListSorter.addItem( SubmitFilter.SORT_MANUALLY,
1322                 I18nService.getLocalizedString( PROPERTY_SORTER_LIST_ITEM_MANUAL, locale ) );
1323         }
1324 
1325         refListSorter.addItem( SubmitFilter.SORT_BY_NUMBER_VIEW_ASC,
1326             I18nService.getLocalizedString( PROPERTY_SORTER_LIST_ITEM_VIEW_ASC, locale ) );
1327         refListSorter.addItem( SubmitFilter.SORT_BY_NUMBER_VIEW_DESC,
1328             I18nService.getLocalizedString( PROPERTY_SORTER_LIST_ITEM_VIEW_DESC, locale ) );
1329 
1330         return refListSorter;
1331     }
1332 
1333     /**
1334      * Init reference list width the different sort
1335      *
1336      * @param locale the locale
1337      * @return reference list of sort
1338      */
1339     public static ReferenceList getRefListFilterByPeriod( Locale locale )
1340     {
1341         ReferenceList refListFilterByPeriod = new ReferenceList(  );
1342 
1343         refListFilterByPeriod.addItem( CONSTANT_ID_NULL, I18nService.getLocalizedString( PROPERTY_FILTER_ALL, locale ) );
1344         refListFilterByPeriod.addItem( CONSTANT_SUBMIT_FILTER_TO_DAY,
1345             I18nService.getLocalizedString( PROPERTY_FILTER_TO_DAY, locale ) );
1346         refListFilterByPeriod.addItem( CONSTANT_SUBMIT_FILTER_YESTERDAY,
1347             I18nService.getLocalizedString( PROPERTY_FILTER_YESTERDAY, locale ) );
1348         refListFilterByPeriod.addItem( CONSTANT_SUBMIT_FILTER_WEEK,
1349             I18nService.getLocalizedString( PROPERTY_FILTER_WEEK, locale ) );
1350         refListFilterByPeriod.addItem( CONSTANT_SUBMIT_FILTER_MONTH,
1351             I18nService.getLocalizedString( PROPERTY_FILTER_MONTH, locale ) );
1352 
1353         return refListFilterByPeriod;
1354     }
1355 
1356     /**
1357      * Init reference list width the different sort
1358      *
1359      * @param locale the locale
1360      * @return reference list of sort
1361      */
1362     public static ReferenceList getRefListCommentSort( Locale locale )
1363     {
1364         ReferenceList refListSorter = new ReferenceList(  );
1365 
1366         refListSorter.addItem( CONSTANT_ID_NULL, EMPTY_STRING );
1367         refListSorter.addItem( SubmitFilter.SORT_BY_DATE_MODIFY_ASC,
1368             I18nService.getLocalizedString( PROPERTY_SORTER_LIST_ITEM_DATE_RESPONSE_ASC, locale ) );
1369         refListSorter.addItem( SubmitFilter.SORT_BY_DATE_MODIFY_DESC,
1370             I18nService.getLocalizedString( PROPERTY_SORTER_LIST_ITEM_DATE_RESPONSE_DESC, locale ) );
1371 
1372         return refListSorter;
1373     }
1374 
1375     /**
1376      * Init reference list width the different sort
1377      *
1378      * @param locale the locale
1379      * @return reference list of sort
1380      */
1381     public static ReferenceList getRefListCommentState( Locale locale )
1382     {
1383         ReferenceList refListSorter = new ReferenceList(  );
1384 
1385         refListSorter.addItem( CONSTANT_ID_NULL, EMPTY_STRING );
1386         refListSorter.addItem( CommentSubmit.STATE_ENABLE,
1387             I18nService.getLocalizedString( PROPERTY_COMMENT_STATE_ENABLE, locale ) );
1388         refListSorter.addItem( CommentSubmit.STATE_DISABLE,
1389             I18nService.getLocalizedString( PROPERTY_COMMENT_STATE_DISABLE, locale ) );
1390 
1391         return refListSorter;
1392     }
1393 
1394     /**
1395      * write the http header in the response
1396      * @param request the httpServletRequest
1397      * @param response the http response
1398      * @param strFileName the name of the file who must insert in the response
1399      */
1400     public static void addHeaderResponse( HttpServletRequest request, HttpServletResponse response, String strFileName )
1401     {
1402         response.setHeader( "Content-Disposition", "attachment ;filename=\"" + strFileName + "\";" );
1403         response.setHeader( "Pragma", "public" );
1404         response.setHeader( "Expires", "0" );
1405         response.setHeader( "Cache-Control", "must-revalidate,post-check=0,pre-check=0" );
1406     }
1407 
1408     /**
1409      * Builds a query with filters placed in parameters
1410      * @param strSelect the select of the query
1411      * @param listStrFilter the list of filter to add in the query
1412      * @param strOrder the order by of the query
1413      * @return a query
1414      */
1415     public static String buildRequestWithFilter( String strSelect, List<String> listStrFilter, String strOrder )
1416     {
1417         StringBuffer strBuffer = new StringBuffer(  );
1418         strBuffer.append( strSelect );
1419 
1420         int nCount = 0;
1421 
1422         for ( String strFilter : listStrFilter )
1423         {
1424             if ( ++nCount == 1 )
1425             {
1426                 strBuffer.append( CONSTANT_WHERE );
1427             }
1428 
1429             strBuffer.append( strFilter );
1430 
1431             if ( nCount != listStrFilter.size(  ) )
1432             {
1433                 strBuffer.append( CONSTANT_AND );
1434             }
1435         }
1436 
1437         if ( strOrder != null )
1438         {
1439             strBuffer.append( strOrder );
1440         }
1441 
1442         return strBuffer.toString(  );
1443     }
1444 
1445     /**
1446      * Like {@link List#retainAll(java.util.Collection)}, keeping first list
1447      * order. This method is based on the fact that list1 and list2 have unique
1448      * elements.
1449      *
1450      * @param list1
1451      *            the first list
1452      * @param list2
1453      *            the other list
1454      * @return first list
1455      */
1456     public static List<Integer> retainAllIdsKeepingFirstOrder( List<Integer> list1, List<Integer> list2 )
1457     {
1458         Iterator<Integer> it = list1.iterator(  );
1459 
1460         // makes contains quicker
1461         TreeSet<Integer> ts = new TreeSet<Integer>( list2 );
1462 
1463         while ( it.hasNext(  ) )
1464         {
1465             if ( !ts.contains( it.next(  ) ) )
1466             {
1467                 it.remove(  );
1468             }
1469         }
1470 
1471         return list1;
1472     }
1473 
1474     /**
1475      * move a element in the list
1476      * @param nOldPosistion the old position
1477      * @param nNewPosition the new position
1478      * @param list The list
1479      */
1480     public static void moveElement( int nOldPosistion, int nNewPosition, ArrayList<Integer> list )
1481     {
1482         Integer element = list.get( nOldPosistion - 1 );
1483         list.remove( nOldPosistion - 1 );
1484         list.add( nNewPosition - 1, element );
1485     }
1486 
1487     /**
1488      * replace special characters in the string passed as a parameter
1489      *
1490      * @param strSource
1491      *            the string
1492      * @return substitute special in the string passed as a parameter
1493      */
1494     public static String substituteSpecialCaractersForExport( String strSource )
1495     {
1496         String strResult = EMPTY_STRING;
1497 
1498         if ( strSource != null )
1499         {
1500             strResult = strSource;
1501         }
1502 
1503         strResult = StringUtil.substitute( strResult, CONSTANT_CHARACTER_SIMPLE_QUOTE, CONSTANT_CHARACTER_DOUBLE_QUOTE );
1504         strResult = StringUtil.substitute( strResult, EMPTY_STRING, CONSTANTE_CHARACTER_RETURN );
1505         strResult = StringUtil.substitute( strResult, EMPTY_STRING, CONSTANTE_CHARACTERNEW_LINE );
1506         strResult = StringUtil.substitute( strResult, EMPTY_STRING, "<div[^>]+>" );
1507         strResult = StringUtil.substitute( strResult, EMPTY_STRING, "</div>" );
1508 
1509         return strResult;
1510     }
1511 
1512     /**
1513      * Add an empty element to a reference list
1514      * @param refList The reference list to add the empty element to
1515      */
1516     public static void addEmptyItem( ReferenceList refList )
1517     {
1518         ReferenceItem refEmpty = new ReferenceItem(  );
1519         refEmpty.setCode( EMPTY_STRING + CONSTANT_ID_NULL );
1520         refEmpty.setName( EMPTY_STRING );
1521         refList.add( 0, refEmpty );
1522     }
1523 
1524     /**
1525      * Depopulate the digg into a map of key - value
1526      * @param digg the digg
1527      * @return a map of key - value
1528      */
1529     public static Map<String, Object> depopulate( Digg digg )
1530     {
1531         Map<String, Object> mapAttributes = new HashMap<String, Object>(  );
1532 
1533         for ( java.lang.reflect.Field field : Digg.class.getDeclaredFields(  ) )
1534         {
1535             DiggAttribute attribute = field.getAnnotation( DiggAttribute.class );
1536 
1537             if ( attribute != null )
1538             {
1539                 String strAttributeKey = attribute.value(  );
1540 
1541                 try
1542                 {
1543                     field.setAccessible( true );
1544 
1545                     Object attributeValue = ReflectionUtils.getField( field, digg );
1546                     mapAttributes.put( strAttributeKey, attributeValue );
1547                 }
1548                 catch ( SecurityException e )
1549                 {
1550                     AppLogService.error( e );
1551                 }
1552             }
1553         }
1554 
1555         return mapAttributes;
1556     }
1557 
1558     /**
1559      * create a filter for getting the list of pinned digg submit
1560      * @param filter the init filter
1561      * @return a filter for getting f the list of pinned digg submit
1562      */
1563     public static SubmitFilter createPinnedFilter( SubmitFilter filter )
1564     {
1565         SubmitFilter pinnedFilter = new SubmitFilter(  );
1566         pinnedFilter.setIdDigg( filter.getIdDigg(  ) );
1567         pinnedFilter.setDateFirst( filter.getDateFirst(  ) );
1568         pinnedFilter.setDateLast( filter.getDateLast(  ) );
1569         pinnedFilter.setIdCategory( filter.getIdCategory(  ) );
1570         pinnedFilter.setIdType( filter.getIdType(  ) );
1571         pinnedFilter.setIdReported( filter.getIdReported(  ) );
1572         pinnedFilter.setIdDiggSubmitState( filter.getIdDiggSubmitState(  ) );
1573         pinnedFilter.setIdPinned( SubmitFilter.ID_TRUE );
1574         pinnedFilter.setIdContainsCommentDisable( filter.getIdContainsCommentDisable(  ) );
1575         initSubmitFilterBySort( pinnedFilter, SubmitFilter.SORT_MANUALLY );
1576 
1577         return pinnedFilter;
1578     }
1579 
1580     /**
1581      *
1582      * @param searchFields the search fields
1583      * @return a submit Filter
1584      */
1585     public static SubmitFilter getDiggSubmitFilter( DigglikeAdminSearchFields searchFields )
1586     {
1587         return getDiggSubmitFilter( searchFields, null );
1588     }
1589 
1590     /**
1591      *
1592      * @param searchFields the search fields
1593      * @param nDefaultIdSort the defautlt sort
1594      * @return SubmitFilter
1595      */
1596     public static SubmitFilter getDiggSubmitFilter( DigglikeAdminSearchFields searchFields, Integer nDefaultIdSort )
1597     {
1598         SubmitFilter filter = new SubmitFilter(  );
1599         filter.setIdDigg( searchFields.getIdDigg(  ) );
1600         filter.setIdDiggSubmitState( searchFields.getIdDiggSumitState(  ) );
1601         filter.setIdReported( searchFields.getIdDiggSubmitReport(  ) );
1602         filter.setIdCategory( searchFields.getIdCategory(  ) );
1603         filter.setIdType( searchFields.getIdType(  ) );
1604         filter.setIdContainsCommentDisable( searchFields.getIdDiggSubmitContainsCommentDisable(  ) );
1605         DiggUtils.initSubmitFilterBySort( filter,
1606             ( ( searchFields.getIdDiggSubmitSort(  ) == DiggUtils.CONSTANT_ID_NULL ) && ( nDefaultIdSort != null ) )
1607             ? nDefaultIdSort : searchFields.getIdDiggSubmitSort(  ) );
1608         //add sort by pinned first
1609         DiggUtils.initSubmitFilterBySort( filter, SubmitFilter.SORT_BY_PINNED_FIRST );
1610 
1611         return filter;
1612     }
1613     
1614     
1615     /**
1616      * Add Avatar to model
1617      * @param model
1618      * @param luteceUserInfo
1619      */
1620     public static void addAvatarToModel( Map<String, Object> model,DiggUserInfo luteceUserInfo)
1621     {
1622     	
1623     	if(luteceUserInfo!=null)
1624         {
1625         
1626     		if(!StringUtils.isEmpty(luteceUserInfo.getHomeMail()))
1627     		{
1628     			model.put(MARK_AVATAR, AvatarService.getAvatarUrl(luteceUserInfo.getHomeMail()));
1629     		}
1630     		else if(!StringUtils.isEmpty(luteceUserInfo.getBusinessMail()))
1631     		{
1632     			model.put(MARK_AVATAR, AvatarService.getAvatarUrl(luteceUserInfo.getBusinessMail()));
1633     		}
1634     		
1635         }
1636     	
1637     }
1638     
1639 }