Coverage Report - fr.paris.lutece.plugins.form.utils.FormUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
FormUtils
0 %
0/599
0 %
0/288
4,077
 
 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.form.utils;
 35  
 
 36  
 import java.awt.Color;
 37  
 import java.sql.Timestamp;
 38  
 import java.text.DateFormat;
 39  
 import java.util.ArrayList;
 40  
 import java.util.Calendar;
 41  
 import java.util.Collection;
 42  
 import java.util.Date;
 43  
 import java.util.GregorianCalendar;
 44  
 import java.util.HashMap;
 45  
 import java.util.LinkedHashMap;
 46  
 import java.util.List;
 47  
 import java.util.Locale;
 48  
 import java.util.Map;
 49  
 
 50  
 import javax.servlet.http.HttpServletRequest;
 51  
 import javax.servlet.http.HttpServletResponse;
 52  
 import javax.servlet.http.HttpSession;
 53  
 
 54  
 import org.apache.commons.codec.binary.Base64;
 55  
 import org.apache.commons.lang.StringUtils;
 56  
 import org.apache.commons.lang3.math.NumberUtils;
 57  
 import org.jfree.chart.ChartFactory;
 58  
 import org.jfree.chart.JFreeChart;
 59  
 import org.jfree.chart.plot.XYPlot;
 60  
 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
 61  
 import org.jfree.data.time.Day;
 62  
 import org.jfree.data.time.Month;
 63  
 import org.jfree.data.time.TimeSeries;
 64  
 import org.jfree.data.time.TimeSeriesCollection;
 65  
 import org.jfree.data.time.Week;
 66  
 import org.jfree.data.xy.XYDataset;
 67  
 
 68  
 import fr.paris.lutece.plugins.form.business.Category;
 69  
 import fr.paris.lutece.plugins.form.business.ConditionalEntriesFieldMapper;
 70  
 import fr.paris.lutece.plugins.form.business.Form;
 71  
 import fr.paris.lutece.plugins.form.business.FormFilter;
 72  
 import fr.paris.lutece.plugins.form.business.FormHome;
 73  
 import fr.paris.lutece.plugins.form.business.FormSubmit;
 74  
 import fr.paris.lutece.plugins.form.business.StatisticFormSubmit;
 75  
 import fr.paris.lutece.plugins.form.business.iteration.IterationGroup;
 76  
 import fr.paris.lutece.plugins.form.service.FormPlugin;
 77  
 import fr.paris.lutece.plugins.form.service.draft.FormDraftBackupService;
 78  
 import fr.paris.lutece.plugins.form.service.entrytype.EntryTypeMyLuteceUser;
 79  
 import fr.paris.lutece.plugins.form.service.parameter.FormParameterService;
 80  
 import fr.paris.lutece.plugins.form.web.http.GroupHttpServletRequestWrapper;
 81  
 import fr.paris.lutece.plugins.genericattributes.business.Entry;
 82  
 import fr.paris.lutece.plugins.genericattributes.business.EntryFilter;
 83  
 import fr.paris.lutece.plugins.genericattributes.business.EntryHome;
 84  
 import fr.paris.lutece.plugins.genericattributes.business.EntryType;
 85  
 import fr.paris.lutece.plugins.genericattributes.business.EntryTypeHome;
 86  
 import fr.paris.lutece.plugins.genericattributes.business.Field;
 87  
 import fr.paris.lutece.plugins.genericattributes.business.FieldHome;
 88  
 import fr.paris.lutece.plugins.genericattributes.business.GenericAttributeError;
 89  
 import fr.paris.lutece.plugins.genericattributes.business.Response;
 90  
 import fr.paris.lutece.plugins.genericattributes.service.entrytype.AbstractEntryTypeArray;
 91  
 import fr.paris.lutece.plugins.genericattributes.service.entrytype.AbstractEntryTypeGroup;
 92  
 import fr.paris.lutece.plugins.genericattributes.service.entrytype.AbstractEntryTypeUpload;
 93  
 import fr.paris.lutece.plugins.genericattributes.service.entrytype.EntryTypeServiceManager;
 94  
 import fr.paris.lutece.plugins.genericattributes.service.entrytype.IEntryTypeService;
 95  
 import fr.paris.lutece.portal.business.file.File;
 96  
 import fr.paris.lutece.portal.business.file.FileHome;
 97  
 import fr.paris.lutece.portal.business.mailinglist.Recipient;
 98  
 import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
 99  
 import fr.paris.lutece.portal.business.physicalfile.PhysicalFileHome;
 100  
 import fr.paris.lutece.portal.business.user.AdminUser;
 101  
 import fr.paris.lutece.portal.service.captcha.CaptchaSecurityService;
 102  
 import fr.paris.lutece.portal.service.content.XPageAppService;
 103  
 import fr.paris.lutece.portal.service.i18n.I18nService;
 104  
 import fr.paris.lutece.portal.service.mail.MailService;
 105  
 import fr.paris.lutece.portal.service.mailinglist.AdminMailingListService;
 106  
 import fr.paris.lutece.portal.service.plugin.Plugin;
 107  
 import fr.paris.lutece.portal.service.plugin.PluginService;
 108  
 import fr.paris.lutece.portal.service.security.LuteceUser;
 109  
 import fr.paris.lutece.portal.service.security.SecurityService;
 110  
 import fr.paris.lutece.portal.service.security.UserNotSignedException;
 111  
 import fr.paris.lutece.portal.service.template.AppTemplateService;
 112  
 import fr.paris.lutece.portal.service.util.AppLogService;
 113  
 import fr.paris.lutece.portal.service.util.AppPathService;
 114  
 import fr.paris.lutece.portal.service.util.AppPropertiesService;
 115  
 import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
 116  
 import fr.paris.lutece.util.ReferenceList;
 117  
 import fr.paris.lutece.util.filesystem.FileSystemUtil;
 118  
 import fr.paris.lutece.util.html.HtmlTemplate;
 119  
 import fr.paris.lutece.util.url.UrlItem;
 120  
 import fr.paris.lutece.util.xml.XmlUtil;
 121  
 
 122  
 /**
 123  
  * Utility class for plugin Form
 124  
  */
 125  
 public final class FormUtils
 126  
 {
 127  
     // Bean
 128  
     public static final String BEAN_ENTRY_TYPE_SERVICE = "form.entryTypeService";
 129  
     public static final String BEAN_FORM_RESPONSE_SERVICE = "form.responseService";
 130  
     public static final String BEAN_EXPORT_DAEMON_TYPE_FACTORY = "form.exportTypeFactory";
 131  
 
 132  
     // Session
 133  
     private static final String SESSION_FORM_LIST_SUBMITTED_RESPONSES = "form_list_submitted_responses";
 134  
     public static final String SESSION_VALIDATE_REQUIREMENT = "session_validate_requirement";
 135  
 
 136  
     // Parameters
 137  
     private static final String PARAMETER_ID_ENTRY_TYPE = "id_type";
 138  
 
 139  
     // Marks
 140  
     private static final String MARK_LOCALE = "locale";
 141  
     private static final String MARK_ENTRY = "entry";
 142  
     private static final String MARK_FIELD = "field";
 143  
     private static final String MARK_JCAPTCHA = "jcaptcha";
 144  
     private static final String MARK_STR_ENTRY = "str_entry";
 145  
     private static final String MARK_CATEGORY_LIST = "category_list";
 146  
     private static final String MARK_LIST_RESPONSES = "list_responses";
 147  
     private static final String MARK_VALIDATE_REQUIREMENT = "validate_requirement";
 148  
     private static final String MARK_DRAFT_SUPPORTED = "draft_supported";
 149  
     private static final String MARK_USER = "user";
 150  
     private static final String MARK_UPLOAD_HANDLER = "uploadHandler";
 151  
     private static final String MARK_WEBAPP_URL = "webapp_url";
 152  
     private static final String MARK_ENTRY_ITERATION_NUMBER = "entry_iteration_number";
 153  
     private static final String MARK_ENTRY_ITERATION_LIMIT_REACHED = "entry_iteration_limit_reached";
 154  
     private static final String MARK_FORM_ERRORS = "form_errors";
 155  
 
 156  
     // Name of the JCaptcha plugin
 157  
     private static final String JCAPTCHA_PLUGIN = "jcaptcha";
 158  
 
 159  
     // Xml Tags
 160  
     private static final String TAG_FORM = "form";
 161  
     private static final String TAG_FORM_TITLE = "form-title";
 162  
     private static final String TAG_FORM_SUBMIT = "submit";
 163  
     private static final String TAG_FORM_SUBMITS = "submits";
 164  
     private static final String TAG_FORM_SUBMIT_ID = "submit-id";
 165  
     private static final String TAG_FORM_SUBMIT_DATE = "submit-date";
 166  
     private static final String TAG_FORM_SUBMIT_IP = "submit-ip";
 167  
     private static final String TAG_QUESTIONS = "questions";
 168  
     private static final String TAG_QUESTION = "question";
 169  
     private static final String TAG_QUESTION_TITLE = "question-title";
 170  
     private static final String TAG_QUESTION_ID = "question-id";
 171  
     private static final String TAG_QUESTION_ITERATION_NUMBER = "iteration-number";
 172  
     private static final String TAG_RESPONSES = "responses";
 173  
     private static final String TAG_RESPONSE = "response";
 174  
     private static final String TAG_FORM_ENTRIES = "form-entries";
 175  
     private static final String TAG_FORM_ENTRY = "form-entry";
 176  
     private static final String TAG_FORM_ENTRY_ID = "form-entry-id";
 177  
     private static final String TAG_FORM_ENTRY_TITLE = "form-entry-title";
 178  
     private static final String TAG_FORM_ENTRY_ITERATION_NUMBER = "form-entry-iteration-number";
 179  
 
 180  
     // Templates
 181  
     private static final String TEMPLATE_DIV_CONDITIONAL_ENTRY = "skin/plugins/form/html_code_div_conditional_entry.html";
 182  
     private static final String TEMPLATE_HTML_CODE_FORM = "skin/plugins/form/html_code_form.html";
 183  
     private static final String TEMPLATE_NOTIFICATION_MAIL_END_DISPONIBILITY = "admin/plugins/form/notification_mail_end_disponibility.html";
 184  
     private static final String TEMPLATE_NOTIFICATION_MAIL_FORM_SUBMIT = "admin/plugins/form/notification_mail_form_submit.html";
 185  
 
 186  
     // Properties
 187  
     private static final String PROPERTY_NOTIFICATION_MAIL_END_DISPONIBILITY_SUBJECT = "form.notificationMailEndDisponibility.subject";
 188  
     private static final String PROPERTY_NOTIFICATION_MAIL_END_DISPONIBILITY_SENDER_NAME = "form.notificationMailEndDisponibility.senderName";
 189  
     private static final String PROPERTY_NOTIFICATION_MAIL_FORM_SUBMIT_SUBJECT = "form.notificationMailFormSubmit.subject";
 190  
     private static final String PROPERTY_NOTIFICATION_MAIL_FORM_SUBMIT_SENDER_NAME = "form.notificationMailFormSubmit.senderName";
 191  
     private static final String PROPERTY_CHOOSE_CATEGORY = "form.form.choose.category";
 192  
     private static final String PROPERTY_LUTECE_ADMIN_PROD_URL = "lutece.admin.prod.url";
 193  
     private static final String PROPERTY_LUTECE_BASE_URL = "lutece.base.url";
 194  
     private static final String PROPERTY_LUTECE_PROD_URL = "lutece.prod.url";
 195  
     public static final String CONSTANT_MYLUTECE_ATTRIBUTE_I18N_PREFIX = "form.entrytype.myluteceuserattribute.attribute.";
 196  
     public static final String PROPERTY_MY_LUTECE_ATTRIBUTES_LIST = "entrytype.myluteceuserattribute.attributes.list";
 197  
 
 198  
     // Parameters
 199  
     private static final String MARK_UPLOAD_FRONT_OFFICE_PICTURE_SRC = "frontOfficePictureSource";
 200  
 
 201  
     // Constants
 202  
     public static final String CONSTANT_UNDERSCORE = "_";
 203  
     public static final String CONSTANT_COMMA = ",";
 204  
     public static final String CONSTANT_GROUP_BY_DAY = "0";
 205  
     public static final String CONSTANT_GROUP_BY_WEEK = "1";
 206  
     public static final String CONSTANT_GROUP_BY_MONTH = "2";
 207  
 
 208  
     /**
 209  
      * FormUtils
 210  
      *
 211  
      */
 212  
     private FormUtils( )
 213  0
     {
 214  0
     }
 215  
 
 216  
     /**
 217  
      * Send a mail of end of availability to the mailing list associate with the form
 218  
      * 
 219  
      * @param form
 220  
      *            the form
 221  
      * @param locale
 222  
      *            the locale
 223  
      */
 224  
     public static void sendNotificationMailEndDisponibility( Form form, Locale locale )
 225  
     {
 226  
         try
 227  
         {
 228  0
             String strSubject = I18nService.getLocalizedString( PROPERTY_NOTIFICATION_MAIL_END_DISPONIBILITY_SUBJECT, locale );
 229  0
             String strSenderName = I18nService.getLocalizedString( PROPERTY_NOTIFICATION_MAIL_END_DISPONIBILITY_SENDER_NAME, locale );
 230  0
             String strSenderEmail = MailService.getNoReplyEmail( );
 231  
 
 232  0
             Collection<Recipient> listRecipients = AdminMailingListService.getRecipients( form.getIdMailingList( ) );
 233  0
             Map<String, Object> model = new HashMap<String, Object>( );
 234  0
             model.put( FormConstants.MARK_FORM, form );
 235  
 
 236  0
             HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_NOTIFICATION_MAIL_END_DISPONIBILITY, locale, model );
 237  
 
 238  
             // Send Mail
 239  0
             for ( Recipient recipient : listRecipients )
 240  
             {
 241  
                 // Build the mail message
 242  0
                 MailService.sendMailHtml( recipient.getEmail( ), strSenderName, strSenderEmail, strSubject, t.getHtml( ) );
 243  0
             }
 244  
         }
 245  0
         catch( Exception e )
 246  
         {
 247  0
             AppLogService.error( "Error during Notify end disponibilty of form : " + e.getMessage( ) );
 248  0
         }
 249  0
     }
 250  
 
 251  
     /**
 252  
      * SendMail to the mailing list associate to the form a mail of new form submit. It will also display the answers submitted by the user.
 253  
      * 
 254  
      * @param formSubmit
 255  
      *            the submit form
 256  
      * @param locale
 257  
      *            {@link Locale}
 258  
      */
 259  
     public static void sendNotificationMailFormSubmit( FormSubmit formSubmit, Locale locale )
 260  
     {
 261  0
         Collection<Recipient> listRecipients = AdminMailingListService.getRecipients( formSubmit.getForm( ).getIdMailingList( ) );
 262  0
         Map<String, Object> model = new HashMap<String, Object>( );
 263  0
         model.put( FormConstants.MARK_FORM, formSubmit.getForm( ) );
 264  0
         model.put( FormConstants.MARK_FORM_SUBMIT, formSubmit );
 265  0
         sendNotificationMailFormSubmit( model, listRecipients, locale );
 266  0
     }
 267  
 
 268  
     /**
 269  
      * Send the mail
 270  
      * 
 271  
      * @param model
 272  
      *            the model of the template
 273  
      * @param listRecipients
 274  
      *            the list of recipients
 275  
      * @param locale
 276  
      *            {@link Locale}
 277  
      */
 278  
     private static void sendNotificationMailFormSubmit( Map<String, Object> model, Collection<Recipient> listRecipients, Locale locale )
 279  
     {
 280  
         try
 281  
         {
 282  0
             String strSubject = I18nService.getLocalizedString( PROPERTY_NOTIFICATION_MAIL_FORM_SUBMIT_SUBJECT, locale );
 283  0
             String strSenderName = I18nService.getLocalizedString( PROPERTY_NOTIFICATION_MAIL_FORM_SUBMIT_SENDER_NAME, locale );
 284  0
             String strSenderEmail = MailService.getNoReplyEmail( );
 285  
 
 286  0
             HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_NOTIFICATION_MAIL_FORM_SUBMIT, locale, model );
 287  
 
 288  
             // Send Mail
 289  0
             for ( Recipient recipient : listRecipients )
 290  
             {
 291  
                 // Build the mail message
 292  0
                 MailService.sendMailHtml( recipient.getEmail( ), strSenderName, strSenderEmail, strSubject, t.getHtml( ) );
 293  0
             }
 294  
         }
 295  0
         catch( Exception e )
 296  
         {
 297  0
             AppLogService.error( "Error during Notify a new form submit" + e.getMessage( ) );
 298  0
         }
 299  0
     }
 300  
 
 301  
     /**
 302  
      * Return a timestamp Object which correspond with the string specified in parameter.
 303  
      * 
 304  
      * @param date
 305  
      *            the date who must convert
 306  
      * @param locale
 307  
      *            the locale
 308  
      * @return a timestamp Object which correspond with the string specified in parameter.
 309  
      */
 310  
     public static Timestamp getDateLastMinute( Date date, Locale locale )
 311  
     {
 312  0
         if ( date == null )
 313  
         {
 314  0
             return null;
 315  
         }
 316  
 
 317  0
         Calendar caldate = new GregorianCalendar( );
 318  0
         caldate.setTime( date );
 319  0
         caldate.set( Calendar.MILLISECOND, 0 );
 320  0
         caldate.set( Calendar.SECOND, 0 );
 321  0
         caldate.set( Calendar.HOUR_OF_DAY, caldate.getActualMaximum( Calendar.HOUR_OF_DAY ) );
 322  0
         caldate.set( Calendar.MINUTE, caldate.getActualMaximum( Calendar.MINUTE ) );
 323  
 
 324  0
         Timestamp timeStamp = new Timestamp( caldate.getTimeInMillis( ) );
 325  
 
 326  0
         return timeStamp;
 327  
     }
 328  
 
 329  
     /**
 330  
      * Return a timestamp Object which correspond with the string specified in parameter.
 331  
      * 
 332  
      * @param date
 333  
      *            the date who must convert
 334  
      * @param locale
 335  
      *            the locale
 336  
      * @return a timestamp Object which correspond with the string specified in parameter.
 337  
      */
 338  
     public static Timestamp getDateFirstMinute( Date date, Locale locale )
 339  
     {
 340  0
         if ( date == null )
 341  
         {
 342  0
             return null;
 343  
         }
 344  
 
 345  0
         Calendar caldate = new GregorianCalendar( );
 346  0
         caldate.setTime( date );
 347  0
         caldate.set( Calendar.MILLISECOND, 0 );
 348  0
         caldate.set( Calendar.SECOND, 0 );
 349  0
         caldate.set( Calendar.HOUR_OF_DAY, caldate.getActualMinimum( Calendar.HOUR_OF_DAY ) );
 350  0
         caldate.set( Calendar.MINUTE, caldate.getActualMinimum( Calendar.MINUTE ) );
 351  
 
 352  0
         Timestamp timeStamp = new Timestamp( caldate.getTimeInMillis( ) );
 353  
 
 354  0
         return timeStamp;
 355  
     }
 356  
 
 357  
     /**
 358  
      * Return the day of the timestamp in parameter
 359  
      * 
 360  
      * @param timestamp
 361  
      *            date
 362  
      * @return the day of the timestamp in parameter
 363  
      */
 364  
     public static int getDay( Timestamp timestamp )
 365  
     {
 366  0
         Calendar caldate = new GregorianCalendar( );
 367  0
         caldate.setTime( timestamp );
 368  
 
 369  0
         return caldate.get( Calendar.DAY_OF_MONTH );
 370  
     }
 371  
 
 372  
     /**
 373  
      * Return the week of the timestamp in parameter
 374  
      * 
 375  
      * @param timestamp
 376  
      *            date
 377  
      * @return the week of the timestamp in parameter
 378  
      */
 379  
     public static int getWeek( Timestamp timestamp )
 380  
     {
 381  0
         Calendar caldate = new GregorianCalendar( );
 382  0
         caldate.setTime( timestamp );
 383  
 
 384  0
         return caldate.get( Calendar.WEEK_OF_YEAR );
 385  
     }
 386  
 
 387  
     /**
 388  
      * Return the month of the timestamp in parameter
 389  
      * 
 390  
      * @param timestamp
 391  
      *            date
 392  
      * @return the month of the timestamp in parameter
 393  
      */
 394  
     public static int getMonth( Timestamp timestamp )
 395  
     {
 396  0
         Calendar caldate = new GregorianCalendar( );
 397  0
         caldate.setTime( timestamp );
 398  
 
 399  0
         return caldate.get( Calendar.MONTH );
 400  
     }
 401  
 
 402  
     /**
 403  
      * Return the year of the timestamp in parameter
 404  
      * 
 405  
      * @param timestamp
 406  
      *            date
 407  
      * @return the year of the timestamp in parameter
 408  
      */
 409  
     public static int getYear( Timestamp timestamp )
 410  
     {
 411  0
         Calendar caldate = new GregorianCalendar( );
 412  0
         caldate.setTime( timestamp );
 413  
 
 414  0
         return caldate.get( Calendar.YEAR );
 415  
     }
 416  
 
 417  
     /**
 418  
      * Return a timestamp Object which correspond to the timestamp in parameter add with a number of times unit (day,week,month)specify in strTimesUnit .
 419  
      * 
 420  
      * @param timestamp
 421  
      *            date
 422  
      * @param strTimesUnit
 423  
      *            (day,week,month)
 424  
      * @param nDecal
 425  
      *            the number of timesUnit
 426  
      * @return a timestamp Object which correspond with the string specified in parameter add with a number of times unit (day,week,month)specify in
 427  
      *         strTimesUnit.
 428  
      */
 429  
     public static Timestamp addStatisticInterval( Timestamp timestamp, String strTimesUnit, int nDecal )
 430  
     {
 431  0
         int nTimesUnit = Calendar.DAY_OF_MONTH;
 432  
 
 433  0
         if ( strTimesUnit.equals( CONSTANT_GROUP_BY_WEEK ) )
 434  
         {
 435  0
             nTimesUnit = Calendar.WEEK_OF_MONTH;
 436  
         }
 437  
         else
 438  0
             if ( strTimesUnit.equals( CONSTANT_GROUP_BY_MONTH ) )
 439  
             {
 440  0
                 nTimesUnit = Calendar.MONTH;
 441  
             }
 442  
 
 443  0
         Calendar caldate = new GregorianCalendar( );
 444  0
         caldate.setTime( timestamp );
 445  0
         caldate.set( Calendar.MILLISECOND, 0 );
 446  0
         caldate.set( Calendar.SECOND, 0 );
 447  0
         caldate.set( Calendar.HOUR_OF_DAY, caldate.getActualMaximum( Calendar.HOUR_OF_DAY ) );
 448  0
         caldate.set( Calendar.MINUTE, caldate.getActualMaximum( Calendar.MINUTE ) );
 449  0
         caldate.add( nTimesUnit, nDecal );
 450  
 
 451  0
         Timestamp timeStamp1 = new Timestamp( caldate.getTimeInMillis( ) );
 452  
 
 453  0
         return timeStamp1;
 454  
     }
 455  
 
 456  
     /**
 457  
      * Compare two timestamps and return true if they have the same times unit(Day,week,month)
 458  
      * 
 459  
      * @param timestamp1
 460  
      *            timestamp1
 461  
      * @param timestamp2
 462  
      *            timestamp2
 463  
      * @param strTimesUnit
 464  
      *            (day,week,month)
 465  
      * @return Compare two timestamp and return true if they have the same times unit(Day,week,month)
 466  
      */
 467  
     public static boolean sameDate( Timestamp timestamp1, Timestamp timestamp2, String strTimesUnit )
 468  
     {
 469  0
         Calendar caldate1 = new GregorianCalendar( );
 470  0
         caldate1.setTime( timestamp1 );
 471  
 
 472  0
         Calendar caldate2 = new GregorianCalendar( );
 473  0
         caldate2.setTime( timestamp2 );
 474  
 
 475  0
         if ( strTimesUnit.equals( CONSTANT_GROUP_BY_DAY ) && ( caldate1.get( Calendar.YEAR ) == caldate2.get( Calendar.YEAR ) )
 476  0
                 && ( caldate1.get( Calendar.DAY_OF_YEAR ) == caldate2.get( Calendar.DAY_OF_YEAR ) ) )
 477  
         {
 478  0
             return true;
 479  
         }
 480  
         else
 481  0
             if ( strTimesUnit.equals( CONSTANT_GROUP_BY_WEEK ) && ( caldate1.get( Calendar.YEAR ) == caldate2.get( Calendar.YEAR ) )
 482  0
                     && ( caldate1.get( Calendar.WEEK_OF_YEAR ) == caldate2.get( Calendar.WEEK_OF_YEAR ) ) )
 483  
             {
 484  0
                 return true;
 485  
             }
 486  
             else
 487  0
                 if ( strTimesUnit.equals( CONSTANT_GROUP_BY_MONTH ) && ( caldate1.get( Calendar.YEAR ) == caldate2.get( Calendar.YEAR ) )
 488  0
                         && ( caldate1.get( Calendar.MONTH ) == caldate2.get( Calendar.MONTH ) ) )
 489  
                 {
 490  0
                     return true;
 491  
                 }
 492  
 
 493  0
         return false;
 494  
     }
 495  
 
 496  
     /**
 497  
      * Converts a java.sql.Timestamp date in a String date in a "jj/mm/aaaa" format
 498  
      * 
 499  
      * @param date
 500  
      *            java.sql.Timestamp date to convert
 501  
      * @param locale
 502  
      *            the locale
 503  
      * @return strDate The String date in the short locale format or the empty String if the date is null
 504  
      * @deprecated Deprecated
 505  
      */
 506  
     public static String getDateString( Timestamp date, Locale locale )
 507  
     {
 508  0
         DateFormat dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, locale );
 509  
 
 510  0
         return dateFormat.format( date );
 511  
     }
 512  
 
 513  
     /**
 514  
      * Return current Timestamp
 515  
      * 
 516  
      * @return return current Timestamp
 517  
      */
 518  
     public static Timestamp getCurrentTimestamp( )
 519  
     {
 520  0
         return new Timestamp( GregorianCalendar.getInstance( ).getTimeInMillis( ) );
 521  
     }
 522  
 
 523  
     /**
 524  
      * Return current date without hours, minutes and milliseconds
 525  
      * 
 526  
      * @return return current date
 527  
      */
 528  
     public static Date getCurrentDate( )
 529  
     {
 530  0
         Calendar cal1 = Calendar.getInstance( );
 531  0
         cal1.setTime( new Date( ) );
 532  0
         cal1.set( Calendar.HOUR_OF_DAY, 0 );
 533  0
         cal1.set( Calendar.MINUTE, 0 );
 534  0
         cal1.set( Calendar.SECOND, 0 );
 535  0
         cal1.set( Calendar.MILLISECOND, 0 );
 536  
 
 537  0
         return cal1.getTime( );
 538  
     }
 539  
 
 540  
     /**
 541  
      * Return an instance of IEntry function of type entry
 542  
      * 
 543  
      * @param request
 544  
      *            the request
 545  
      * @param plugin
 546  
      *            the plugin
 547  
      * @return an instance of IEntry function of type entry
 548  
      */
 549  
     public static Entry createEntryByType( HttpServletRequest request, Plugin plugin )
 550  
     {
 551  0
         String strIdType = request.getParameter( PARAMETER_ID_ENTRY_TYPE );
 552  0
         int nIdType = -1;
 553  0
         Entry entry = null;
 554  
         EntryType entryType;
 555  
 
 556  0
         if ( ( strIdType != null ) && !strIdType.equals( StringUtils.EMPTY ) )
 557  
         {
 558  
             try
 559  
             {
 560  0
                 nIdType = Integer.parseInt( strIdType );
 561  
             }
 562  0
             catch( NumberFormatException ne )
 563  
             {
 564  0
                 AppLogService.error( ne );
 565  
 
 566  0
                 return null;
 567  0
             }
 568  
         }
 569  
 
 570  0
         if ( nIdType == -1 )
 571  
         {
 572  0
             return null;
 573  
         }
 574  
 
 575  0
         entryType = EntryTypeHome.findByPrimaryKey( nIdType );
 576  
 
 577  0
         entry = new Entry( );
 578  0
         entry.setEntryType( entryType );
 579  
 
 580  0
         return entry;
 581  
     }
 582  
 
 583  
     /**
 584  
      * Return the index in the list of the field whose key is specified in parameter
 585  
      * 
 586  
      * @param nIdField
 587  
      *            the key of the field
 588  
      * @param listField
 589  
      *            the list of field
 590  
      * @return the index in the list of the field whose key is specified in parameter
 591  
      */
 592  
     public static int getIndexFieldInTheFieldList( int nIdField, List<Field> listField )
 593  
     {
 594  0
         int nIndex = 0;
 595  
 
 596  0
         for ( Field field : listField )
 597  
         {
 598  0
             if ( field.getIdField( ) == nIdField )
 599  
             {
 600  0
                 return nIndex;
 601  
             }
 602  
 
 603  0
             nIndex++;
 604  0
         }
 605  
 
 606  0
         return nIndex;
 607  
     }
 608  
 
 609  
     /**
 610  
      * Return the HTML code of the form
 611  
      * 
 612  
      * @param form
 613  
      *            the form which HTML code must be return
 614  
      * @param strUrlAction
 615  
      *            the URL who must be call after the form submit
 616  
      * @param locale
 617  
      *            the locale
 618  
      * @param bDisplayFront
 619  
      *            True if the entry will be displayed in Front Office, false if it will be displayed in Back Office.
 620  
      * @param request
 621  
      *            HttpServletRequest
 622  
      * @return the HTML code of the form
 623  
      */
 624  
     public static String getHtmlForm( Form form, String strUrlAction, Locale locale, boolean bDisplayFront, HttpServletRequest request )
 625  
     {
 626  
         List<Entry> listEntryFirstLevel;
 627  0
         Map<String, Object> model = new HashMap<String, Object>( );
 628  
         HtmlTemplate template;
 629  
         EntryFilter filter;
 630  0
         StringBuffer strBuffer = new StringBuffer( );
 631  0
         filter = new EntryFilter( );
 632  0
         filter.setIdResource( form.getIdForm( ) );
 633  0
         filter.setResourceType( Form.RESOURCE_TYPE );
 634  0
         filter.setEntryParentNull( EntryFilter.FILTER_TRUE );
 635  0
         filter.setFieldDependNull( EntryFilter.FILTER_TRUE );
 636  0
         listEntryFirstLevel = EntryHome.getEntryList( filter );
 637  0
         HttpSession session = request.getSession( );
 638  
 
 639  0
         ArrayList<Category> listCats = new ArrayList<Category>( );
 640  0
         Category category = new Category( );
 641  
 
 642  0
         category.setIdCategory( -2 );
 643  0
         category.setTitle( I18nService.getLocalizedString( PROPERTY_CHOOSE_CATEGORY, locale ) );
 644  
 
 645  0
         if ( form.getCategory( ) != null )
 646  
         {
 647  0
             listCats.add( category );
 648  
         }
 649  
 
 650  0
         ReferenceList refCategoryList = getRefListCategory( listCats );
 651  
 
 652  0
         for ( Entry entry : listEntryFirstLevel )
 653  
         {
 654  0
             FormUtils.getHtmlEntry( entry.getIdEntry( ), strBuffer, locale, bDisplayFront, request );
 655  0
         }
 656  
 
 657  0
         if ( form.isActiveCaptcha( ) && PluginService.isPluginEnable( JCAPTCHA_PLUGIN ) )
 658  
         {
 659  0
             CaptchaSecurityService captchaSecurityService = new CaptchaSecurityService( );
 660  0
             model.put( MARK_JCAPTCHA, captchaSecurityService.getHtmlCode( ) );
 661  
         }
 662  
 
 663  0
         model.put( MARK_CATEGORY_LIST, refCategoryList );
 664  0
         model.put( FormConstants.MARK_FORM, form );
 665  0
         model.put( FormConstants.MARK_URL_ACTION, strUrlAction );
 666  0
         model.put( MARK_STR_ENTRY, strBuffer.toString( ) );
 667  0
         model.put( MARK_LOCALE, locale );
 668  
 
 669  0
         Object objectSessionValidateRequirement = session.getAttribute( SESSION_VALIDATE_REQUIREMENT );
 670  0
         if ( objectSessionValidateRequirement != null )
 671  
         {
 672  0
             boolean bValidateRequirement = (Boolean) objectSessionValidateRequirement;
 673  0
             model.put( MARK_VALIDATE_REQUIREMENT, bValidateRequirement );
 674  
         }
 675  
 
 676  
         // Retrieve the user from the request
 677  0
         LuteceUser user = retrieveLuteceUserFromRequest( request );
 678  0
         model.put( MARK_USER, user );
 679  
 
 680  
         // Check if draft is supported or not
 681  0
         model.put( MARK_DRAFT_SUPPORTED, FormDraftBackupService.isDraftSupported( ) );
 682  
 
 683  
         // Add information about Form picture in front-office
 684  0
         int nIdPictureFile = form.getIdPictureFile( );
 685  0
         if ( nIdPictureFile != NumberUtils.INTEGER_ZERO )
 686  
         {
 687  0
             model.put( MARK_UPLOAD_FRONT_OFFICE_PICTURE_SRC, getFrontOfficePictureSource( nIdPictureFile ) );
 688  
         }
 689  
 
 690  
         // Check if there are responses in the session. If so, then there are errors
 691  0
         model.put( MARK_FORM_ERRORS, FormUtils.getFormErrors( session ) );
 692  
 
 693  0
         template = AppTemplateService.getTemplate( TEMPLATE_HTML_CODE_FORM, locale, model );
 694  
 
 695  0
         return template.getHtml( );
 696  
     }
 697  
 
 698  
     /**
 699  
      * Initialize a reference list with the different categories
 700  
      * 
 701  
      * @param listCategories
 702  
      *            the list of categories
 703  
      * @return reference list of category
 704  
      */
 705  
     public static ReferenceList getRefListCategory( List<Category> listCategories )
 706  
     {
 707  0
         ReferenceList refListCategories = new ReferenceList( );
 708  
 
 709  0
         for ( Category category : listCategories )
 710  
         {
 711  0
             refListCategories.addItem( category.getIdCategory( ), category.getTitle( ) );
 712  0
         }
 713  
 
 714  0
         return refListCategories;
 715  
     }
 716  
 
 717  
     /**
 718  
      * Insert in the string buffer the content of the HTML code of the entry
 719  
      * 
 720  
      * @param nIdEntry
 721  
      *            the key of the entry which HTML code must be insert in the stringBuffer
 722  
      * @param stringBuffer
 723  
      *            the buffer which contains the HTML code
 724  
      * @param locale
 725  
      *            the locale
 726  
      * @param bDisplayFront
 727  
      *            True if the entry will be displayed in Front Office, false if it will be displayed in Back Office.
 728  
      * @param request
 729  
      *            HttpServletRequest
 730  
      */
 731  
     public static void getHtmlEntry( int nIdEntry, StringBuffer stringBuffer, Locale locale, boolean bDisplayFront, HttpServletRequest request )
 732  
     {
 733  0
         getHtmlEntry( nIdEntry, stringBuffer, locale, bDisplayFront, request, FormConstants.DEFAULT_ITERATION_NUMBER );
 734  0
     }
 735  
 
 736  
     /**
 737  
      * Insert in the string buffer the content of the HTML code of the entry
 738  
      * 
 739  
      * @param nIdEntry
 740  
      *            the key of the entry which HTML code must be insert in the stringBuffer
 741  
      * @param stringBuffer
 742  
      *            the buffer which contains the HTML code
 743  
      * @param locale
 744  
      *            the locale
 745  
      * @param bDisplayFront
 746  
      *            True if the entry will be displayed in Front Office, false if it will be displayed in Back Office.
 747  
      * @param request
 748  
      *            HttpServletRequest
 749  
      * @param nIterationNumber
 750  
      *            the current iteration number
 751  
      */
 752  
     public static void getHtmlEntry( int nIdEntry, StringBuffer stringBuffer, Locale locale, boolean bDisplayFront, HttpServletRequest request,
 753  
             int nIterationNumber )
 754  
     {
 755  0
         Map<String, Object> model = new HashMap<String, Object>( );
 756  0
         List<ConditionalEntriesFieldMapper> lstConditionalFieldMapper = null;
 757  
         HtmlTemplate template;
 758  0
         Entry entry = EntryHome.findByPrimaryKey( nIdEntry );
 759  
 
 760  0
         if ( entry.isRoleAssociated( ) )
 761  
         {
 762  0
             entry.setFields( FormUtils.getAuthorizedFieldsByRole( request, entry.getFields( ) ) );
 763  
         }
 764  
 
 765  0
         if ( entry.getEntryType( ).getGroup( ) )
 766  
         {
 767  
             // Create the Html for the entry of type Group
 768  0
             EntryTypeGroupUtils.getHtmlGroupEntry( request, entry, model, bDisplayFront );
 769  
         }
 770  
         else
 771  
         {
 772  0
             if ( entry.getNumberConditionalQuestion( ) != 0 )
 773  
             {
 774  0
                 for ( Field field : entry.getFields( ) )
 775  
                 {
 776  0
                     field.setConditionalQuestions( FieldHome.findByPrimaryKey( field.getIdField( ) ).getConditionalQuestions( ) );
 777  0
                 }
 778  
             }
 779  
         }
 780  
 
 781  0
         if ( entry.getNumberConditionalQuestion( ) != 0 )
 782  
         {
 783  0
             lstConditionalFieldMapper = new ArrayList<ConditionalEntriesFieldMapper>( );
 784  
 
 785  0
             for ( Field field : entry.getFields( ) )
 786  
             {
 787  0
                 if ( field.getConditionalQuestions( ).size( ) != 0 )
 788  
                 {
 789  0
                     StringBuffer strGroupStringBuffer = new StringBuffer( );
 790  
 
 791  0
                     for ( Entry entryConditional : field.getConditionalQuestions( ) )
 792  
                     {
 793  0
                         getHtmlEntry( entryConditional.getIdEntry( ), strGroupStringBuffer, locale, bDisplayFront, request, nIterationNumber );
 794  
 
 795  0
                     }
 796  0
                     model.put( FormConstants.MARK_STR_LIST_CHILDREN, strGroupStringBuffer.toString( ) );
 797  0
                     model.put( MARK_FIELD, field );
 798  0
                     model.put( MARK_ENTRY_ITERATION_NUMBER, nIterationNumber );
 799  0
                     template = AppTemplateService.getTemplate( TEMPLATE_DIV_CONDITIONAL_ENTRY, locale, model );
 800  0
                     lstConditionalFieldMapper.add( new ConditionalEntriesFieldMapper( field.getIdField( ), template.getHtml( ) ) );
 801  
                 }
 802  0
             }
 803  0
             model.put( FormConstants.MARK_LIST_MAP_CHILDREN, lstConditionalFieldMapper );
 804  
         }
 805  
 
 806  0
         model.put( MARK_ENTRY, entry );
 807  0
         model.put( MARK_ENTRY_ITERATION_NUMBER, nIterationNumber );
 808  0
         model.put( MARK_LOCALE, locale );
 809  
 
 810  
         // Retrieve the user from the request
 811  0
         LuteceUser user = retrieveLuteceUserFromRequest( request );
 812  0
         model.put( MARK_USER, user );
 813  
 
 814  0
         Map<Integer, List<Response>> listSubmittedResponses = getResponses( request.getSession( ) );
 815  0
         List<Response> listResponses = new ArrayList<>( );
 816  
 
 817  0
         if ( listSubmittedResponses != null && !EntryTypeGroupUtils.entryBelongIterableGroup( entry ) )
 818  
         {
 819  0
             listResponses = listSubmittedResponses.get( entry.getIdEntry( ) );
 820  
         }
 821  
         else
 822  
         {
 823  0
             String strEntryParameter = request.getParameter( FormConstants.PREFIX_ATTRIBUTE + entry.getIdEntry( ) );
 824  
 
 825  0
             if ( StringUtils.isNotBlank( strEntryParameter ) )
 826  
             {
 827  0
                 EntryTypeServiceManager.getEntryTypeService( entry ).getResponseData( entry, request, listResponses, locale );
 828  
             }
 829  
 
 830  
             // Check if the current entry has a parent and if the parent is of entry type group or not
 831  0
             if ( ( entry != null && entry.getParent( ) != null ) || entry.getFieldDepend( ) != null )
 832  
             {
 833  
                 // Manage the response for an entry belong to an iterable group
 834  0
                 EntryTypeGroupUtils.manageIterableGroupResponse( request, entry, listResponses, nIterationNumber );
 835  
 
 836  
                 // Populate the IterationGroup map in the session
 837  0
                 if ( EntryTypeGroupUtils.entryBelongIterableGroup( entry ) )
 838  
                 {
 839  0
                     EntryTypeGroupUtils.populateIterationGroup( request, entry, nIterationNumber, listResponses );
 840  
                 }
 841  
             }
 842  
         }
 843  
 
 844  
         // The list of response to the model
 845  0
         if ( listResponses != null && !listResponses.isEmpty( ) )
 846  
         {
 847  0
             model.put( MARK_LIST_RESPONSES, listResponses );
 848  
         }
 849  
 
 850  0
         IEntryTypeService entryTypeService = EntryTypeServiceManager.getEntryTypeService( entry );
 851  
 
 852  0
         if ( entryTypeService instanceof AbstractEntryTypeUpload )
 853  
         {
 854  0
             model.put( MARK_UPLOAD_HANDLER, ( (AbstractEntryTypeUpload) entryTypeService ).getAsynchronousUploadHandler( ) );
 855  
         }
 856  
 
 857  0
         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
 858  
 
 859  
         // Check if the maximum number of iteration has been reached for the entry of type group
 860  0
         if ( entryTypeService instanceof AbstractEntryTypeGroup && new IterationGroup( entry ).getNbMaxIteration( ) != FormConstants.DEFAULT_ITERATION_NUMBER )
 861  
         {
 862  0
             Boolean isLimitReached = Boolean.TRUE;
 863  0
             IterationGroup iterationGroup = EntryTypeGroupUtils.retrieveIterationGroup( request, nIdEntry );
 864  0
             if ( iterationGroup != null )
 865  
             {
 866  0
                 isLimitReached = iterationGroup.isIterationLimitReached( );
 867  
             }
 868  
 
 869  0
             model.put( MARK_ENTRY_ITERATION_LIMIT_REACHED, isLimitReached );
 870  
         }
 871  
 
 872  0
         template = AppTemplateService.getTemplate( entryTypeService.getTemplateHtmlForm( entry, bDisplayFront ), locale, model );
 873  0
         stringBuffer.append( template.getHtml( ) );
 874  0
     }
 875  
 
 876  
     /**
 877  
      * Retrieve the LuteceUser associated to the request
 878  
      * 
 879  
      * @param request
 880  
      *            The request to retrieve the LuteceUser from
 881  
      * @return the LuteceUser of the request
 882  
      */
 883  
     private static LuteceUser retrieveLuteceUserFromRequest( HttpServletRequest request )
 884  
     {
 885  0
         LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
 886  
 
 887  0
         if ( user == null && SecurityService.isAuthenticationEnable( ) && SecurityService.getInstance( ).isExternalAuthentication( ) )
 888  
         {
 889  
             try
 890  
             {
 891  0
                 user = SecurityService.getInstance( ).getRemoteUser( request );
 892  
             }
 893  0
             catch( UserNotSignedException e )
 894  
             {
 895  
                 // Nothing to do : lutece user is not mandatory
 896  0
             }
 897  
         }
 898  
 
 899  0
         return user;
 900  
     }
 901  
 
 902  
     /**
 903  
      * Perform in the object formSubmit the responses associates with a entry specify in parameter.<br />
 904  
      * Return null if there is no error in the response else return a FormError Object
 905  
      * 
 906  
      * @param request
 907  
      *            the request
 908  
      * @param nIdEntry
 909  
      *            the key of the entry
 910  
      * @param plugin
 911  
      *            the plugin
 912  
      * @param formSubmit
 913  
      *            Form Submit Object
 914  
      * @param bResponseNull
 915  
      *            true if the response create must be null
 916  
      * @param bReturnErrors
 917  
      *            true if errors must be returned
 918  
      * @param locale
 919  
      *            the locale
 920  
      * @return null if there is no error in the response else return a FormError Object
 921  
      */
 922  
     public static List<GenericAttributeError> getResponseEntry( HttpServletRequest request, int nIdEntry, Plugin plugin, FormSubmit formSubmit,
 923  
             boolean bResponseNull, boolean bReturnErrors, Locale locale )
 924  
     {
 925  0
         return getResponseEntry( request, nIdEntry, plugin, formSubmit, bResponseNull, bReturnErrors, locale, FormConstants.DEFAULT_ITERATION_NUMBER );
 926  
     }
 927  
 
 928  
     /**
 929  
      * Perform in the object formSubmit the responses associates with a entry specify in parameter.<br />
 930  
      * Return null if there is no error in the response else return a FormError Object
 931  
      * 
 932  
      * @param request
 933  
      *            the request
 934  
      * @param nIdEntry
 935  
      *            the key of the entry
 936  
      * @param plugin
 937  
      *            the plugin
 938  
      * @param formSubmit
 939  
      *            Form Submit Object
 940  
      * @param bResponseNull
 941  
      *            true if the response create must be null
 942  
      * @param bReturnErrors
 943  
      *            true if errors must be returned
 944  
      * @param locale
 945  
      *            the locale
 946  
      * @param nIterationNumber
 947  
      *            the current iteration number
 948  
      * @return null if there is no error in the response else return a FormError Object
 949  
      */
 950  
     public static List<GenericAttributeError> getResponseEntry( HttpServletRequest request, int nIdEntry, Plugin plugin, FormSubmit formSubmit,
 951  
             boolean bResponseNull, boolean bReturnErrors, Locale locale, int nIterationNumber )
 952  
     {
 953  0
         List<GenericAttributeError> listFormErrors = new ArrayList<GenericAttributeError>( );
 954  0
         List<Response> listResponse = new ArrayList<Response>( );
 955  0
         Entry entry = EntryHome.findByPrimaryKey( nIdEntry );
 956  
 
 957  0
         List<Field> listField = new ArrayList<Field>( );
 958  
 
 959  0
         for ( Field field : entry.getFields( ) )
 960  
         {
 961  0
             field = FieldHome.findByPrimaryKey( field.getIdField( ) );
 962  0
             listField.add( field );
 963  0
         }
 964  
 
 965  0
         entry.setFields( listField );
 966  
 
 967  0
         if ( entry.getEntryType( ).getGroup( ) )
 968  
         {
 969  
             // Get all the responses for the entry of type group
 970  0
             EntryTypeGroupUtils.getResponseGroupEntry( request, listFormErrors, entry, plugin, formSubmit, bReturnErrors, locale );
 971  
         }
 972  
         else
 973  0
             if ( !entry.getEntryType( ).getComment( ) )
 974  
             {
 975  0
                 GenericAttributeError formError = null;
 976  
 
 977  0
                 if ( !bResponseNull )
 978  
                 {
 979  
                     // Set the iteration number in the request to retrieve it from the EntryTypeService
 980  0
                     request.setAttribute( FormConstants.ATTRIBUTE_RESPONSE_ITERATION_NUMBER, nIterationNumber );
 981  
 
 982  
                     // Manage the case of an entry of type array
 983  0
                     if ( EntryTypeServiceManager.getEntryTypeService( entry ) instanceof AbstractEntryTypeArray
 984  
                             && request instanceof GroupHttpServletRequestWrapper )
 985  
                     {
 986  0
                         StringBuilder strIterationParameterName = new StringBuilder( ( (GroupHttpServletRequestWrapper) request ).getIterationParameterName( ) );
 987  0
                         strIterationParameterName.append( FormConstants.PREFIX_ATTRIBUTE );
 988  0
                         strIterationParameterName.append( entry.getIdEntry( ) );
 989  0
                         strIterationParameterName.append( FormUtils.CONSTANT_UNDERSCORE );
 990  
 
 991  0
                         ( (GroupHttpServletRequestWrapper) request ).setIterationParameterName( strIterationParameterName.toString( ) );
 992  
                     }
 993  
 
 994  
                     // Manage errors
 995  0
                     formError = EntryTypeServiceManager.getEntryTypeService( entry ).getResponseData( entry, request, listResponse, locale );
 996  
 
 997  
                     // Remove the attribute when it has been used
 998  0
                     request.removeAttribute( FormConstants.ATTRIBUTE_RESPONSE_ITERATION_NUMBER );
 999  
 
 1000  0
                     if ( formError != null )
 1001  
                     {
 1002  
                         // If the entry belong to an iterable entry group we must recreate the name of the parameters for the current iteration
 1003  0
                         if ( EntryTypeGroupUtils.entryBelongIterableGroup( entry ) )
 1004  
                         {
 1005  0
                             formError.setUrl( EntryTypeGroupUtils.getIterableEntryChildUrl( entry,
 1006  0
                                     request.getAttribute( FormConstants.ATTRIBUTE_ITERATION_NUMBER ) ) );
 1007  
                         }
 1008  
                         else
 1009  
                         {
 1010  0
                             formError.setUrl( getEntryUrl( entry ) );
 1011  
                         }
 1012  
                     }
 1013  
                 }
 1014  
                 else
 1015  
                 {
 1016  0
                     Response response = new Response( );
 1017  0
                     response.setEntry( entry );
 1018  0
                     response.setIterationNumber( nIterationNumber );
 1019  0
                     listResponse.add( response );
 1020  
                 }
 1021  
 
 1022  0
                 if ( bReturnErrors && ( formError != null ) )
 1023  
                 {
 1024  0
                     entry.setError( formError );
 1025  0
                     listFormErrors.add( formError );
 1026  
                 }
 1027  
 
 1028  0
                 if ( request.getSession( ) != null )
 1029  
                 {
 1030  0
                     Map<Integer, List<Response>> listSubmittedResponses = getResponses( request.getSession( ) );
 1031  
 
 1032  0
                     if ( listSubmittedResponses != null )
 1033  
                     {
 1034  0
                         listSubmittedResponses.put( entry.getIdEntry( ), listResponse );
 1035  0
                         restoreResponses( request.getSession( ), listSubmittedResponses );
 1036  
                     }
 1037  
                 }
 1038  
 
 1039  
                 // If the entry belong to an iterable group we will populate the iteration map in the session
 1040  0
                 if ( EntryTypeGroupUtils.entryBelongIterableGroup( entry ) )
 1041  
                 {
 1042  
                     // Populate the IterationGroup map in the session
 1043  0
                     EntryTypeGroupUtils.populateIterationGroup( request, entry, nIterationNumber, listResponse );
 1044  
                 }
 1045  
 
 1046  0
                 formSubmit.getListResponse( ).addAll( listResponse );
 1047  
 
 1048  0
                 if ( entry.getNumberConditionalQuestion( ) != 0 )
 1049  
                 {
 1050  0
                     for ( Field field : entry.getFields( ) )
 1051  
                     {
 1052  0
                         boolean bIsFieldInResponseList = isFieldInTheResponseList( field.getIdField( ), listResponse );
 1053  
 
 1054  0
                         for ( Entry conditionalEntry : field.getConditionalQuestions( ) )
 1055  
                         {
 1056  0
                             listFormErrors.addAll( getResponseEntry( request, conditionalEntry.getIdEntry( ), plugin, formSubmit, !bIsFieldInResponseList,
 1057  
                                     bReturnErrors, locale, nIterationNumber ) );
 1058  0
                         }
 1059  0
                     }
 1060  
                 }
 1061  
             }
 1062  
 
 1063  0
         return listFormErrors;
 1064  
     }
 1065  
 
 1066  
     /**
 1067  
      * Get the url to modify an entry of the form in front office
 1068  
      * 
 1069  
      * @param entry
 1070  
      *            the entry
 1071  
      * @return The url to modify the entry in front office
 1072  
      */
 1073  
     public static String getEntryUrl( Entry entry )
 1074  
     {
 1075  0
         UrlItem url = new UrlItem( AppPathService.getPortalUrl( ) );
 1076  0
         url.addParameter( XPageAppService.PARAM_XPAGE_APP, FormPlugin.PLUGIN_NAME );
 1077  
 
 1078  0
         if ( ( entry != null ) && ( entry.getIdResource( ) > 0 ) )
 1079  
         {
 1080  0
             url.addParameter( FormConstants.PARAMETER_ID_FORM, entry.getIdResource( ) );
 1081  0
             url.setAnchor( FormConstants.PREFIX_ATTRIBUTE + entry.getIdEntry( ) );
 1082  
         }
 1083  
 
 1084  0
         return url.getUrl( );
 1085  
     }
 1086  
 
 1087  
     /**
 1088  
      * Return true if the field which key is specified in parameter is in the response list
 1089  
      * 
 1090  
      * @param nIdField
 1091  
      *            the id of the field who is search
 1092  
      * @param listResponse
 1093  
      *            the list of object Response
 1094  
      * @return true if the field which key is specified in parameter is in the response list
 1095  
      */
 1096  
     public static Boolean isFieldInTheResponseList( int nIdField, List<Response> listResponse )
 1097  
     {
 1098  0
         for ( Response response : listResponse )
 1099  
         {
 1100  0
             if ( ( response.getField( ) != null ) && ( response.getField( ).getIdField( ) == nIdField ) )
 1101  
             {
 1102  0
                 return true;
 1103  
             }
 1104  0
         }
 1105  
 
 1106  0
         return false;
 1107  
     }
 1108  
 
 1109  
     /**
 1110  
      * Return the XML of form's response
 1111  
      * 
 1112  
      * @param request
 1113  
      *            the request
 1114  
      * @param form
 1115  
      *            the form
 1116  
      * @param listFormSubmit
 1117  
      *            the form submit list
 1118  
      * @param locale
 1119  
      *            the locale
 1120  
      * @param plugin
 1121  
      *            the plugin
 1122  
      * @return the XML of the response of a form
 1123  
      */
 1124  
     public static String getXmlResponses( HttpServletRequest request, Form form, List<FormSubmit> listFormSubmit, Locale locale, Plugin plugin )
 1125  
     {
 1126  
         // this map stores field in order to not request db multiple time for same field
 1127  0
         StringBuffer buffer = new StringBuffer( );
 1128  0
         XmlUtil.beginElement( buffer, TAG_FORM );
 1129  0
         XmlUtil.addElementHtml( buffer, TAG_FORM_TITLE, form.getTitle( ) );
 1130  
 
 1131  
         // Build entries list XML
 1132  0
         XmlUtil.beginElement( buffer, TAG_FORM_ENTRIES );
 1133  0
         Map<Integer, Integer> mapIdEntryIterationNumber = new LinkedHashMap<>( );
 1134  
 
 1135  0
         for ( Entry entry : getAllQuestionList( form.getIdForm( ), plugin ) )
 1136  
         {
 1137  0
             XmlUtil.beginElement( buffer, TAG_FORM_ENTRY );
 1138  0
             XmlUtil.addElement( buffer, TAG_FORM_ENTRY_ID, entry.getIdEntry( ) );
 1139  
 
 1140  
             // Add an iteration-number tag to the document
 1141  0
             int nIterationNumber = FormConstants.DEFAULT_ITERATION_NUMBER;
 1142  0
             if ( EntryTypeGroupUtils.entryBelongIterableGroup( entry ) )
 1143  
             {
 1144  0
                 int nIdEntry = entry.getIdEntry( );
 1145  0
                 if ( !mapIdEntryIterationNumber.containsKey( nIdEntry ) )
 1146  
                 {
 1147  0
                     mapIdEntryIterationNumber.put( nIdEntry, NumberUtils.INTEGER_ONE );
 1148  
                 }
 1149  
 
 1150  0
                 int nCurrentEntryIterationNumber = mapIdEntryIterationNumber.get( nIdEntry );
 1151  0
                 nIterationNumber = nCurrentEntryIterationNumber;
 1152  0
                 mapIdEntryIterationNumber.put( nIdEntry, nCurrentEntryIterationNumber + NumberUtils.INTEGER_ONE );
 1153  
             }
 1154  0
             XmlUtil.addElement( buffer, TAG_FORM_ENTRY_ITERATION_NUMBER, nIterationNumber );
 1155  
 
 1156  0
             XmlUtil.addElementHtml( buffer, TAG_FORM_ENTRY_TITLE, entry.getTitle( ) );
 1157  0
             XmlUtil.endElement( buffer, TAG_FORM_ENTRY );
 1158  0
         }
 1159  
 
 1160  0
         XmlUtil.endElement( buffer, TAG_FORM_ENTRIES );
 1161  
 
 1162  
         // Build Form submits list XML
 1163  0
         XmlUtil.beginElement( buffer, TAG_FORM_SUBMITS );
 1164  
 
 1165  0
         for ( FormSubmit formSubmit : listFormSubmit )
 1166  
         {
 1167  0
             getXmlResponse( request, buffer, formSubmit, locale, plugin );
 1168  0
         }
 1169  
 
 1170  0
         XmlUtil.endElement( buffer, TAG_FORM_SUBMITS );
 1171  0
         XmlUtil.endElement( buffer, TAG_FORM );
 1172  
 
 1173  0
         return buffer.toString( );
 1174  
     }
 1175  
 
 1176  
     /**
 1177  
      * Get the XML responses for a single form submit
 1178  
      * 
 1179  
      * @param request
 1180  
      *            the HTTP request
 1181  
      * @param form
 1182  
      *            the form
 1183  
      * @param formSubmit
 1184  
      *            the form submit
 1185  
      * @param locale
 1186  
      *            the locale
 1187  
      * @param plugin
 1188  
      *            the plugin
 1189  
      * @return the XML
 1190  
      */
 1191  
     public static String getXmlResponses( HttpServletRequest request, Form form, FormSubmit formSubmit, Locale locale, Plugin plugin )
 1192  
     {
 1193  
         // this map stores field in order to not request db multiple time for same field
 1194  0
         StringBuffer buffer = new StringBuffer( );
 1195  0
         XmlUtil.beginElement( buffer, TAG_FORM );
 1196  0
         XmlUtil.addElementHtml( buffer, TAG_FORM_TITLE, form.getTitle( ) );
 1197  
 
 1198  
         // Build entries list XML
 1199  0
         XmlUtil.beginElement( buffer, TAG_FORM_ENTRIES );
 1200  
 
 1201  0
         for ( Entry entry : getAllQuestionList( form.getIdForm( ), plugin ) )
 1202  
         {
 1203  0
             XmlUtil.beginElement( buffer, TAG_FORM_ENTRY );
 1204  0
             XmlUtil.addElement( buffer, TAG_FORM_ENTRY_ID, entry.getIdEntry( ) );
 1205  0
             XmlUtil.addElementHtml( buffer, TAG_FORM_ENTRY_TITLE, entry.getTitle( ) );
 1206  0
             XmlUtil.endElement( buffer, TAG_FORM_ENTRY );
 1207  0
         }
 1208  
 
 1209  0
         XmlUtil.endElement( buffer, TAG_FORM_ENTRIES );
 1210  
 
 1211  
         // Build Form submits list XML
 1212  0
         XmlUtil.beginElement( buffer, TAG_FORM_SUBMITS );
 1213  
 
 1214  0
         getXmlResponse( request, buffer, formSubmit, locale, plugin );
 1215  
 
 1216  0
         XmlUtil.endElement( buffer, TAG_FORM_SUBMITS );
 1217  0
         XmlUtil.endElement( buffer, TAG_FORM );
 1218  
 
 1219  0
         return buffer.toString( );
 1220  
     }
 1221  
 
 1222  
     /**
 1223  
      * Get the XML responses for the given form submit
 1224  
      * 
 1225  
      * @param request
 1226  
      *            the HTTP request
 1227  
      * @param buffer
 1228  
      *            the buffer
 1229  
      * @param formSubmit
 1230  
      *            the form submit
 1231  
      * @param locale
 1232  
      *            the locale
 1233  
      * @param plugin
 1234  
      *            the plugin
 1235  
      */
 1236  
     private static void getXmlResponse( HttpServletRequest request, StringBuffer buffer, FormSubmit formSubmit, Locale locale, Plugin plugin )
 1237  
     {
 1238  0
         XmlUtil.beginElement( buffer, TAG_FORM_SUBMIT );
 1239  0
         XmlUtil.addElement( buffer, TAG_FORM_SUBMIT_ID, formSubmit.getIdFormSubmit( ) );
 1240  
 
 1241  0
         String strDate = ( locale != null ) ? getDateString( formSubmit.getDateResponse( ), locale ) : StringUtils.EMPTY;
 1242  0
         XmlUtil.addElement( buffer, TAG_FORM_SUBMIT_DATE, strDate );
 1243  
 
 1244  0
         if ( formSubmit.getIp( ) != null )
 1245  
         {
 1246  0
             XmlUtil.addElement( buffer, TAG_FORM_SUBMIT_IP, formSubmit.getIp( ) );
 1247  
         }
 1248  
         else
 1249  
         {
 1250  0
             XmlUtil.addElement( buffer, TAG_FORM_SUBMIT_IP, StringUtils.EMPTY );
 1251  
         }
 1252  
 
 1253  0
         Response responseStore = null;
 1254  0
         XmlUtil.beginElement( buffer, TAG_QUESTIONS );
 1255  
 
 1256  0
         if ( ( formSubmit.getListResponse( ) != null ) && !formSubmit.getListResponse( ).isEmpty( ) )
 1257  
         {
 1258  0
             for ( Response response : formSubmit.getListResponse( ) )
 1259  
             {
 1260  0
                 if ( response.getField( ) != null )
 1261  
                 {
 1262  0
                     Field field = FieldHome.findByPrimaryKey( response.getField( ).getIdField( ) );
 1263  0
                     response.setField( field );
 1264  
                 }
 1265  
 
 1266  0
                 if ( responseStore != null
 1267  0
                         && ( response.getEntry( ).getIdEntry( ) != responseStore.getEntry( ).getIdEntry( ) || response.getIterationNumber( ) != responseStore
 1268  0
                                 .getIterationNumber( ) ) )
 1269  
                 {
 1270  0
                     XmlUtil.endElement( buffer, TAG_RESPONSES );
 1271  0
                     XmlUtil.endElement( buffer, TAG_QUESTION );
 1272  
                 }
 1273  
 
 1274  0
                 if ( responseStore == null
 1275  0
                         || ( response.getEntry( ).getIdEntry( ) != responseStore.getEntry( ).getIdEntry( ) || response.getIterationNumber( ) != responseStore
 1276  0
                                 .getIterationNumber( ) ) )
 1277  
                 {
 1278  0
                     XmlUtil.beginElement( buffer, TAG_QUESTION );
 1279  0
                     XmlUtil.addElementHtml( buffer, TAG_QUESTION_TITLE, response.getEntry( ).getTitle( ) );
 1280  0
                     XmlUtil.addElement( buffer, TAG_QUESTION_ID, response.getEntry( ).getIdEntry( ) );
 1281  0
                     XmlUtil.addElement( buffer, TAG_QUESTION_ITERATION_NUMBER, response.getIterationNumber( ) );
 1282  0
                     XmlUtil.beginElement( buffer, TAG_RESPONSES );
 1283  
                 }
 1284  
 
 1285  0
                 if ( StringUtils.isNotBlank( response.getResponseValue( ) ) || ( response.getFile( ) != null ) )
 1286  
                 {
 1287  0
                     XmlUtil.addElementHtml( buffer, TAG_RESPONSE, EntryTypeServiceManager.getEntryTypeService( response.getEntry( ) )
 1288  0
                             .getResponseValueForExport( response.getEntry( ), request, response, locale ) );
 1289  
                 }
 1290  
                 else
 1291  
                 {
 1292  0
                     XmlUtil.addElement( buffer, TAG_RESPONSE, StringUtils.EMPTY );
 1293  
                 }
 1294  
 
 1295  0
                 responseStore = response;
 1296  0
             }
 1297  
 
 1298  0
             XmlUtil.endElement( buffer, TAG_RESPONSES );
 1299  0
             XmlUtil.endElement( buffer, TAG_QUESTION );
 1300  
         }
 1301  
 
 1302  0
         XmlUtil.endElement( buffer, TAG_QUESTIONS );
 1303  0
         XmlUtil.endElement( buffer, TAG_FORM_SUBMIT );
 1304  0
     }
 1305  
 
 1306  
     /**
 1307  
      * Write the HTTP header in the response
 1308  
      * 
 1309  
      * @param request
 1310  
      *            the httpServletRequest
 1311  
      * @param response
 1312  
      *            the HTTP response
 1313  
      * @param strFileName
 1314  
      *            the name of the file who must insert in the response
 1315  
      * @param strFileExtension
 1316  
      *            the file extension
 1317  
      */
 1318  
     public static void addHeaderResponse( HttpServletRequest request, HttpServletResponse response, String strFileName, String strFileExtension )
 1319  
     {
 1320  0
         response.setHeader( "Content-Disposition", "attachment ;filename=\"" + strFileName + "\"" );
 1321  
 
 1322  0
         if ( strFileExtension.equals( "csv" ) )
 1323  
         {
 1324  0
             response.setCharacterEncoding( FormParameterService.getService( ).getExportCSVEncoding( ) );
 1325  0
             response.setContentType( "application/csv" );
 1326  
         }
 1327  
         else
 1328  
         {
 1329  0
             response.setCharacterEncoding( FormParameterService.getService( ).getExportXMLEncoding( ) );
 1330  
 
 1331  0
             String strMimeType = FileSystemUtil.getMIMEType( strFileName );
 1332  
 
 1333  0
             if ( strMimeType != null )
 1334  
             {
 1335  0
                 response.setContentType( strMimeType );
 1336  
             }
 1337  
             else
 1338  
             {
 1339  0
                 response.setContentType( "application/octet-stream" );
 1340  
             }
 1341  
         }
 1342  
 
 1343  0
         response.setHeader( "Pragma", "public" );
 1344  0
         response.setHeader( "Expires", "0" );
 1345  0
         response.setHeader( "Cache-Control", "must-revalidate,post-check=0,pre-check=0" );
 1346  0
     }
 1347  
 
 1348  
     /**
 1349  
      * Create a JFreeChart Graph function of the statistic form submit
 1350  
      * 
 1351  
      * @param listStatistic
 1352  
      *            the list of statistic of form submit
 1353  
      * @param strLabelX
 1354  
      *            the label of axis x
 1355  
      * @param strLableY
 1356  
      *            the label of axis x
 1357  
      * @param strTimesUnit
 1358  
      *            the times unit of axis x(Day,Week,Month)
 1359  
      * @return a JFreeChart Graph function of the statistic form submit
 1360  
      */
 1361  
     public static JFreeChart createXYGraph( List<StatisticFormSubmit> listStatistic, String strLabelX, String strLableY, String strTimesUnit )
 1362  
     {
 1363  0
         XYDataset xyDataset = createDataset( listStatistic, strTimesUnit );
 1364  0
         JFreeChart jfreechart = ChartFactory.createTimeSeriesChart( StringUtils.EMPTY, strLabelX, strLableY, xyDataset, false, false, false );
 1365  0
         jfreechart.setBackgroundPaint( Color.white );
 1366  
 
 1367  0
         XYPlot xyplot = jfreechart.getXYPlot( );
 1368  
 
 1369  0
         xyplot.setBackgroundPaint( Color.white );
 1370  0
         xyplot.setBackgroundPaint( Color.lightGray );
 1371  0
         xyplot.setDomainGridlinePaint( Color.white );
 1372  0
         xyplot.setRangeGridlinePaint( Color.white );
 1373  
 
 1374  0
         XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) xyplot.getRenderer( );
 1375  0
         renderer.setBaseShapesVisible( true );
 1376  0
         renderer.setSeriesFillPaint( 0, Color.RED );
 1377  0
         renderer.setUseFillPaint( true );
 1378  
 
 1379  0
         return jfreechart;
 1380  
     }
 1381  
 
 1382  
     /**
 1383  
      * Create graph dataset function of the statistic form submit
 1384  
      * 
 1385  
      * @param listStatistic
 1386  
      *            the list of statistic of form submit
 1387  
      * @param strTimesUnit
 1388  
      *            the times unit of axis x(Day,Week,Month)
 1389  
      * @return create graph dataset function of the statistic form submit
 1390  
      */
 1391  
     private static XYDataset createDataset( List<StatisticFormSubmit> listStatistic, String strTimesUnit )
 1392  
     {
 1393  0
         TimeSeries series = null;
 1394  
 
 1395  0
         if ( strTimesUnit.equals( CONSTANT_GROUP_BY_DAY ) )
 1396  
         {
 1397  0
             series = new TimeSeries( StringUtils.EMPTY, Day.class );
 1398  
 
 1399  0
             for ( StatisticFormSubmit statistic : listStatistic )
 1400  
             {
 1401  0
                 series.add( new Day( statistic.getStatisticDate( ) ), statistic.getNumberResponse( ) );
 1402  0
             }
 1403  
         }
 1404  
         else
 1405  0
             if ( strTimesUnit.equals( CONSTANT_GROUP_BY_WEEK ) )
 1406  
             {
 1407  0
                 series = new TimeSeries( StringUtils.EMPTY, Week.class );
 1408  
 
 1409  0
                 for ( StatisticFormSubmit statistic : listStatistic )
 1410  
                 {
 1411  0
                     series.add( new Week( statistic.getStatisticDate( ) ), statistic.getNumberResponse( ) );
 1412  0
                 }
 1413  
             }
 1414  
 
 1415  
             else
 1416  0
                 if ( strTimesUnit.equals( CONSTANT_GROUP_BY_MONTH ) )
 1417  
                 {
 1418  0
                     series = new TimeSeries( StringUtils.EMPTY, Month.class );
 1419  
 
 1420  0
                     for ( StatisticFormSubmit statistic : listStatistic )
 1421  
                     {
 1422  0
                         series.add( new Month( statistic.getStatisticDate( ) ), statistic.getNumberResponse( ) );
 1423  0
                     }
 1424  
                 }
 1425  
 
 1426  0
         TimeSeriesCollection dataset = new TimeSeriesCollection( );
 1427  0
         dataset.addSeries( series );
 1428  
 
 1429  0
         return dataset;
 1430  
     }
 1431  
 
 1432  
     /**
 1433  
      * Load the data of all form that the user is authorized to see depends workgroups user
 1434  
      * 
 1435  
      * @param plugin
 1436  
      *            the plugin
 1437  
      * @param user
 1438  
      *            the current user
 1439  
      * @return a reference list of form
 1440  
      */
 1441  
     public static ReferenceList getFormList( Plugin plugin, AdminUser user )
 1442  
     {
 1443  0
         List<Form> listForms = FormHome.getFormList( new FormFilter( ), plugin );
 1444  0
         listForms = (List<Form>) AdminWorkgroupService.getAuthorizedCollection( listForms, user );
 1445  
 
 1446  0
         ReferenceList refListForms = new ReferenceList( );
 1447  
 
 1448  0
         for ( Form form : listForms )
 1449  
         {
 1450  0
             refListForms.addItem( form.getIdForm( ), form.getTitle( ) );
 1451  0
         }
 1452  
 
 1453  0
         return refListForms;
 1454  
     }
 1455  
 
 1456  
     /**
 1457  
      * Get the ReferenceList associated to all questions
 1458  
      * 
 1459  
      * @param nIdForm
 1460  
      *            the id form
 1461  
      * @param plugin
 1462  
      *            the {@link Plugin}
 1463  
      * @return a {@link ReferenceList}
 1464  
      */
 1465  
     public static ReferenceList getRefListAllQuestions( int nIdForm, Plugin plugin )
 1466  
     {
 1467  0
         ReferenceList refListQuestions = new ReferenceList( );
 1468  
 
 1469  0
         for ( Entry entry : getAllQuestionList( nIdForm, plugin ) )
 1470  
         {
 1471  0
             if ( entry.getTitle( ) != null )
 1472  
             {
 1473  0
                 refListQuestions.addItem( entry.getIdEntry( ), entry.getTitle( ) );
 1474  
             }
 1475  
             else
 1476  
             {
 1477  0
                 refListQuestions.addItem( entry.getIdEntry( ), entry.getComment( ) );
 1478  
             }
 1479  0
         }
 1480  
 
 1481  0
         return refListQuestions;
 1482  
     }
 1483  
 
 1484  
     /**
 1485  
      * Return the questions list
 1486  
      * 
 1487  
      * @param nIdForm
 1488  
      *            the form id
 1489  
      * @param plugin
 1490  
      *            the plugin
 1491  
      * @return the questions list
 1492  
      */
 1493  
     public static List<Entry> getAllQuestionList( int nIdForm, Plugin plugin )
 1494  
     {
 1495  0
         List<Entry> listEntry = new ArrayList<Entry>( );
 1496  0
         EntryFilter filter = new EntryFilter( );
 1497  0
         filter.setIdResource( nIdForm );
 1498  0
         filter.setEntryParentNull( EntryFilter.FILTER_TRUE );
 1499  0
         filter.setIdIsComment( EntryFilter.FILTER_FALSE );
 1500  0
         filter.setResourceType( Form.RESOURCE_TYPE );
 1501  0
         filter.setFieldDependNull( EntryFilter.FILTER_TRUE );
 1502  
 
 1503  0
         for ( Entry entryFirstLevel : EntryHome.getEntryList( filter ) )
 1504  
         {
 1505  0
             if ( entryFirstLevel.getEntryType( ).getGroup( ) )
 1506  
             {
 1507  0
                 filter = new EntryFilter( );
 1508  0
                 filter.setIdResource( nIdForm );
 1509  0
                 filter.setIdEntryParent( entryFirstLevel.getIdEntry( ) );
 1510  0
                 filter.setIdIsComment( EntryFilter.FILTER_FALSE );
 1511  
 
 1512  0
                 entryFirstLevel.setFields( FieldHome.getFieldListByIdEntry( entryFirstLevel.getIdEntry( ) ) );
 1513  
 
 1514  
                 // Manage the case of an iteration
 1515  0
                 int nIterationEntryMax = new IterationGroup( entryFirstLevel ).getNbMaxIteration( );
 1516  0
                 if ( nIterationEntryMax != FormConstants.DEFAULT_ITERATION_NUMBER )
 1517  
                 {
 1518  0
                     for ( int nCurrentIterationNumber = NumberUtils.INTEGER_ONE; nCurrentIterationNumber <= nIterationEntryMax; nCurrentIterationNumber++ )
 1519  
                     {
 1520  0
                         for ( Entry entryChild : EntryHome.getEntryList( filter ) )
 1521  
                         {
 1522  0
                             listEntry.add( entryChild );
 1523  0
                             addConditionnalsEntry( entryChild, listEntry, plugin );
 1524  0
                         }
 1525  
 
 1526  
                     }
 1527  
                 }
 1528  
                 else
 1529  
                 {
 1530  0
                     for ( Entry entryChild : EntryHome.getEntryList( filter ) )
 1531  
                     {
 1532  0
                         listEntry.add( entryChild );
 1533  0
                         addConditionnalsEntry( entryChild, listEntry, plugin );
 1534  0
                     }
 1535  
                 }
 1536  0
             }
 1537  
             else
 1538  
             {
 1539  0
                 listEntry.add( entryFirstLevel );
 1540  0
                 addConditionnalsEntry( entryFirstLevel, listEntry, plugin );
 1541  
             }
 1542  0
         }
 1543  
 
 1544  0
         return listEntry;
 1545  
     }
 1546  
 
 1547  
     /**
 1548  
      * Add children question of the root entryParent node
 1549  
      * 
 1550  
      * @param entryParent
 1551  
      *            the parent entry
 1552  
      * @param listEntry
 1553  
      *            the entry list
 1554  
      * @param plugin
 1555  
      *            the plugin
 1556  
      */
 1557  
     private static void addConditionnalsEntry( Entry entryParent, List<Entry> listEntry, Plugin plugin )
 1558  
     {
 1559  0
         Entry parent = EntryHome.findByPrimaryKey( entryParent.getIdEntry( ) );
 1560  
 
 1561  0
         for ( Field field : parent.getFields( ) )
 1562  
         {
 1563  0
             field = FieldHome.findByPrimaryKey( field.getIdField( ) );
 1564  
 
 1565  0
             if ( field.getConditionalQuestions( ) != null )
 1566  
             {
 1567  0
                 for ( Entry entryConditionnal : field.getConditionalQuestions( ) )
 1568  
                 {
 1569  0
                     listEntry.add( entryConditionnal );
 1570  0
                     addConditionnalsEntry( entryConditionnal, listEntry, plugin );
 1571  0
                 }
 1572  
             }
 1573  0
         }
 1574  0
     }
 1575  
 
 1576  
     /**
 1577  
      * Return all entries of form
 1578  
      * 
 1579  
      * @param nIdForm
 1580  
      *            the form id
 1581  
      * @param plugin
 1582  
      *            the plugin
 1583  
      * @return the all entries of form
 1584  
      */
 1585  
     public static List<Entry> getEntriesList( int nIdForm, Plugin plugin )
 1586  
     {
 1587  0
         List<Entry> listEntry = new ArrayList<Entry>( );
 1588  0
         EntryFilter filter = new EntryFilter( );
 1589  0
         filter.setIdResource( nIdForm );
 1590  0
         filter.setResourceType( Form.RESOURCE_TYPE );
 1591  0
         filter.setEntryParentNull( EntryFilter.FILTER_TRUE );
 1592  0
         filter.setFieldDependNull( EntryFilter.FILTER_TRUE );
 1593  
 
 1594  0
         for ( Entry entryFirstLevel : EntryHome.getEntryList( filter ) )
 1595  
         {
 1596  0
             if ( entryFirstLevel.getEntryType( ).getGroup( ) )
 1597  
             {
 1598  0
                 filter = new EntryFilter( );
 1599  0
                 filter.setIdResource( nIdForm );
 1600  0
                 filter.setIdEntryParent( entryFirstLevel.getIdEntry( ) );
 1601  
 
 1602  0
                 List<Entry> listEntryChild = new ArrayList<Entry>( );
 1603  
 
 1604  0
                 for ( Entry entryChild : EntryHome.getEntryList( filter ) )
 1605  
                 {
 1606  0
                     listEntryChild.add( entryChild );
 1607  0
                     addConditionnalsEntry( entryChild, listEntryChild, plugin );
 1608  0
                 }
 1609  
 
 1610  0
                 entryFirstLevel.setChildren( listEntryChild );
 1611  0
                 listEntry.add( entryFirstLevel );
 1612  0
             }
 1613  
             else
 1614  
             {
 1615  0
                 listEntry.add( entryFirstLevel );
 1616  0
                 addConditionnalsEntry( entryFirstLevel, listEntry, plugin );
 1617  
             }
 1618  0
         }
 1619  
 
 1620  0
         return listEntry;
 1621  
     }
 1622  
 
 1623  
     /**
 1624  
      * Builds a query with filters placed in parameters
 1625  
      * 
 1626  
      * @param strSelect
 1627  
      *            the select of the query
 1628  
      * @param listStrFilter
 1629  
      *            the list of filter to add in the query
 1630  
      * @param listStrGroupBy
 1631  
      *            the list of group by to add in the query
 1632  
      * @param strOrder
 1633  
      *            the order by of the query
 1634  
      * @return a query
 1635  
      */
 1636  
     public static String buildRequestWithFilter( String strSelect, List<String> listStrFilter, List<String> listStrGroupBy, String strOrder )
 1637  
     {
 1638  0
         StringBuffer strBuffer = new StringBuffer( );
 1639  0
         strBuffer.append( strSelect );
 1640  
 
 1641  0
         int nCount = 0;
 1642  
 
 1643  0
         for ( String strFilter : listStrFilter )
 1644  
         {
 1645  0
             if ( ++nCount == 1 )
 1646  
             {
 1647  0
                 strBuffer.append( FormConstants.CONSTANT_WHERE );
 1648  
             }
 1649  
 
 1650  0
             strBuffer.append( strFilter );
 1651  
 
 1652  0
             if ( nCount != listStrFilter.size( ) )
 1653  
             {
 1654  0
                 strBuffer.append( FormConstants.CONSTANT_AND );
 1655  
             }
 1656  0
         }
 1657  
 
 1658  0
         if ( listStrGroupBy != null )
 1659  
         {
 1660  0
             for ( String strGroupBy : listStrGroupBy )
 1661  
             {
 1662  0
                 strBuffer.append( strGroupBy );
 1663  0
             }
 1664  
         }
 1665  
 
 1666  0
         if ( strOrder != null )
 1667  
         {
 1668  0
             strBuffer.append( strOrder );
 1669  
         }
 1670  
 
 1671  0
         return strBuffer.toString( );
 1672  
     }
 1673  
 
 1674  
     /**
 1675  
      * Get entry type mylutece user
 1676  
      * 
 1677  
      * @param plugin
 1678  
      *            Plugin
 1679  
      * @return entry type
 1680  
      */
 1681  
     public static EntryType getEntryTypeMyLuteceUser( Plugin plugin )
 1682  
     {
 1683  0
         for ( EntryType entryType : EntryTypeHome.getList( FormPlugin.PLUGIN_NAME ) )
 1684  
         {
 1685  0
             if ( StringUtils.equals( entryType.getBeanName( ), EntryTypeMyLuteceUser.BEAN_NAME ) )
 1686  
             {
 1687  0
                 return entryType;
 1688  
             }
 1689  0
         }
 1690  
 
 1691  0
         return null;
 1692  
     }
 1693  
 
 1694  
     /**
 1695  
      * Activate MyLutece authentication for the form
 1696  
      * 
 1697  
      * @param form
 1698  
      *            form
 1699  
      * @param plugin
 1700  
      *            Plugin
 1701  
      * @param locale
 1702  
      *            Locale
 1703  
      * @param request
 1704  
      *            HttpServletRequest
 1705  
      */
 1706  
     public static void activateMyLuteceAuthentification( Form form, Plugin plugin, Locale locale, HttpServletRequest request )
 1707  
     {
 1708  0
         EntryType entryType = FormUtils.getEntryTypeMyLuteceUser( plugin );
 1709  0
         Entry entry = null;
 1710  
 
 1711  0
         entry = new Entry( );
 1712  0
         entry.setEntryType( entryType );
 1713  
 
 1714  0
         EntryTypeServiceManager.getEntryTypeService( entry ).getRequestData( entry, request, locale );
 1715  0
         entry.setIdResource( form.getIdForm( ) );
 1716  0
         entry.setResourceType( Form.RESOURCE_TYPE );
 1717  0
         entry.setIdEntry( EntryHome.create( entry ) );
 1718  
 
 1719  0
         if ( entry.getFields( ) != null )
 1720  
         {
 1721  0
             for ( Field field : entry.getFields( ) )
 1722  
             {
 1723  0
                 field.setParentEntry( entry );
 1724  0
                 FieldHome.create( field );
 1725  0
             }
 1726  
         }
 1727  0
     }
 1728  
 
 1729  
     /**
 1730  
      * Deactivate MyLutece authentication for the form
 1731  
      * 
 1732  
      * @param form
 1733  
      *            Form
 1734  
      * @param plugin
 1735  
      *            Plugin
 1736  
      */
 1737  
     public static void deactivateMyLuteceAuthentification( Form form, Plugin plugin )
 1738  
     {
 1739  0
         EntryFilter entryFilter = new EntryFilter( );
 1740  0
         entryFilter.setIdResource( form.getIdForm( ) );
 1741  0
         entryFilter.setResourceType( Form.RESOURCE_TYPE );
 1742  
 
 1743  0
         List<Entry> listEntries = EntryHome.getEntryList( entryFilter );
 1744  
 
 1745  0
         for ( Entry entry : listEntries )
 1746  
         {
 1747  0
             IEntryTypeService entryTypeService = EntryTypeServiceManager.getEntryTypeService( entry );
 1748  
 
 1749  0
             if ( entryTypeService instanceof fr.paris.lutece.plugins.form.service.entrytype.EntryTypeMyLuteceUser )
 1750  
             {
 1751  0
                 EntryHome.remove( entry.getIdEntry( ) );
 1752  
 
 1753  0
                 break;
 1754  
             }
 1755  0
         }
 1756  0
     }
 1757  
 
 1758  
     /**
 1759  
      * Restores submitted responses
 1760  
      * 
 1761  
      * @param session
 1762  
      *            the session
 1763  
      * @param mapResponses
 1764  
      *            response list, key is entry id
 1765  
      */
 1766  
     public static void restoreResponses( HttpSession session, Map<Integer, List<Response>> mapResponses )
 1767  
     {
 1768  0
         session.setAttribute( SESSION_FORM_LIST_SUBMITTED_RESPONSES, mapResponses );
 1769  0
     }
 1770  
 
 1771  
     /**
 1772  
      * Removes submitted responses
 1773  
      * 
 1774  
      * @param session
 1775  
      *            the session
 1776  
      */
 1777  
     public static void removeResponses( HttpSession session )
 1778  
     {
 1779  0
         session.removeAttribute( SESSION_FORM_LIST_SUBMITTED_RESPONSES );
 1780  0
     }
 1781  
 
 1782  
     /**
 1783  
      * Gets the responses bound to the session
 1784  
      * 
 1785  
      * @param session
 1786  
      *            the session
 1787  
      * @return the responses if any, <code>null</code> otherwise.
 1788  
      */
 1789  
     public static Map<Integer, List<Response>> getResponses( HttpSession session )
 1790  
     {
 1791  0
         return (Map<Integer, List<Response>>) session.getAttribute( SESSION_FORM_LIST_SUBMITTED_RESPONSES );
 1792  
     }
 1793  
 
 1794  
     /**
 1795  
      * Restores form errors
 1796  
      * 
 1797  
      * @param session
 1798  
      *            the session
 1799  
      * @param listFormErrors
 1800  
      *            the form errosr
 1801  
      */
 1802  
     public static void restoreFormErrors( HttpSession session, List<GenericAttributeError> listFormErrors )
 1803  
     {
 1804  0
         session.setAttribute( FormConstants.SESSION_FORM_ERRORS, listFormErrors );
 1805  0
     }
 1806  
 
 1807  
     /**
 1808  
      * Removes submitted responses
 1809  
      * 
 1810  
      * @param session
 1811  
      *            the session
 1812  
      */
 1813  
     public static void removeFormErrors( HttpSession session )
 1814  
     {
 1815  0
         session.removeAttribute( FormConstants.SESSION_FORM_ERRORS );
 1816  0
     }
 1817  
 
 1818  
     /**
 1819  
      * Gets the form errors bound to the session
 1820  
      * 
 1821  
      * @param session
 1822  
      *            the session
 1823  
      * @return the form errors
 1824  
      */
 1825  
     public static List<GenericAttributeError> getFormErrors( HttpSession session )
 1826  
     {
 1827  0
         return (List<GenericAttributeError>) session.getAttribute( FormConstants.SESSION_FORM_ERRORS );
 1828  
     }
 1829  
 
 1830  
     /**
 1831  
      * Gets the form plugin
 1832  
      * 
 1833  
      * @return the plugin
 1834  
      */
 1835  
     public static Plugin getPlugin( )
 1836  
     {
 1837  0
         return PluginService.getPlugin( FormPlugin.PLUGIN_NAME );
 1838  
     }
 1839  
 
 1840  
     /**
 1841  
      * Returns a copy of the string , with leading and trailing whitespace omitted.
 1842  
      * 
 1843  
      * @param strParameter
 1844  
      *            the string parameter to convert
 1845  
      * @return null if the strParameter is null other return with leading and trailing whitespace omitted.
 1846  
      */
 1847  
     public static String trim( String strParameter )
 1848  
     {
 1849  0
         if ( strParameter != null )
 1850  
         {
 1851  0
             return strParameter.trim( );
 1852  
         }
 1853  
 
 1854  0
         return strParameter;
 1855  
     }
 1856  
 
 1857  
     /**
 1858  
      * Get the base URL
 1859  
      * 
 1860  
      * @param request
 1861  
      *            the HTTP request
 1862  
      * @return the base URL
 1863  
      */
 1864  
     public static String getAdminBaseUrl( HttpServletRequest request )
 1865  
     {
 1866  0
         String strBaseUrl = StringUtils.EMPTY;
 1867  
 
 1868  0
         if ( request != null )
 1869  
         {
 1870  0
             strBaseUrl = AppPathService.getBaseUrl( request );
 1871  
         }
 1872  
         else
 1873  
         {
 1874  0
             strBaseUrl = AppPropertiesService.getProperty( PROPERTY_LUTECE_ADMIN_PROD_URL );
 1875  
 
 1876  0
             if ( StringUtils.isBlank( strBaseUrl ) )
 1877  
             {
 1878  0
                 strBaseUrl = AppPropertiesService.getProperty( PROPERTY_LUTECE_BASE_URL );
 1879  
 
 1880  0
                 if ( StringUtils.isBlank( strBaseUrl ) )
 1881  
                 {
 1882  0
                     strBaseUrl = AppPropertiesService.getProperty( PROPERTY_LUTECE_PROD_URL );
 1883  
                 }
 1884  
             }
 1885  
         }
 1886  
 
 1887  0
         if ( StringUtils.isNotBlank( strBaseUrl ) )
 1888  
         {
 1889  0
             if ( !strBaseUrl.endsWith( FormConstants.SLASH ) )
 1890  
             {
 1891  0
                 return strBaseUrl + FormConstants.SLASH;
 1892  
             }
 1893  
         }
 1894  
 
 1895  0
         return strBaseUrl;
 1896  
     }
 1897  
 
 1898  
     /**
 1899  
      * Filter a list of field for a given user
 1900  
      *
 1901  
      * @param listField
 1902  
      *            a list of field
 1903  
      * @param request
 1904  
      *            The http request
 1905  
      * @return a field list
 1906  
      */
 1907  
     public static List<Field> getAuthorizedFieldsByRole( HttpServletRequest request, List<Field> listField )
 1908  
     {
 1909  0
         List<Field> listFieldAuthorized = new ArrayList<Field>( );
 1910  
 
 1911  0
         for ( Field field : listField )
 1912  
         {
 1913  
             // filter by workgroup
 1914  0
             if ( !SecurityService.isAuthenticationEnable( ) || field.getRoleKey( ) == null || field.getRoleKey( ).equals( Form.ROLE_NONE )
 1915  0
                     || SecurityService.getInstance( ).isUserInRole( request, field.getRoleKey( ) ) )
 1916  
             {
 1917  0
                 listFieldAuthorized.add( field );
 1918  
             }
 1919  0
         }
 1920  
 
 1921  0
         return listFieldAuthorized;
 1922  
     }
 1923  
 
 1924  
     /**
 1925  
      * Return the source content of an img html tag for the front office picture or null if an information is missing
 1926  
      * 
 1927  
      * @param nIdPictureFile
 1928  
      *            The id of the picture to retrieve the information from
 1929  
      * @return the source data of the picture or null if information are missing
 1930  
      */
 1931  
     private static String getFrontOfficePictureSource( int nIdPictureFile )
 1932  
     {
 1933  0
         File filePicture = FileHome.findByPrimaryKey( nIdPictureFile );
 1934  0
         if ( filePicture != null )
 1935  
         {
 1936  0
             PhysicalFile physicalFileLazyLoading = filePicture.getPhysicalFile( );
 1937  0
             if ( physicalFileLazyLoading != null )
 1938  
             {
 1939  0
                 PhysicalFile physicalFile = PhysicalFileHome.findByPrimaryKey( physicalFileLazyLoading.getIdPhysicalFile( ) );
 1940  0
                 if ( physicalFile != null && physicalFile.getValue( ) != null )
 1941  
                 {
 1942  0
                     byte [ ] bEncodedPhysicalFile = new Base64( ).encode( physicalFile.getValue( ) );
 1943  0
                     String strFrontOfficePicture = new String( bEncodedPhysicalFile );
 1944  0
                     String strMimeType = filePicture.getMimeType( );
 1945  
 
 1946  0
                     if ( StringUtils.isNotBlank( strMimeType ) && StringUtils.isNotBlank( strFrontOfficePicture ) )
 1947  
                     {
 1948  0
                         StringBuilder sbFrontOfficePictureSource = new StringBuilder( );
 1949  0
                         sbFrontOfficePictureSource.append( "data:" );
 1950  0
                         sbFrontOfficePictureSource.append( strMimeType );
 1951  0
                         sbFrontOfficePictureSource.append( ";base64," );
 1952  0
                         sbFrontOfficePictureSource.append( strFrontOfficePicture );
 1953  
 
 1954  0
                         return sbFrontOfficePictureSource.toString( );
 1955  
                     }
 1956  
                 }
 1957  
             }
 1958  
         }
 1959  
 
 1960  0
         return null;
 1961  
     }
 1962  
 }