View Javadoc
1   /*
2    * Copyright (c) 2002-2018, 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.appointment.service;
35  
36  import java.io.IOException;
37  import java.util.ArrayList;
38  import java.util.Arrays;
39  import java.util.HashMap;
40  import java.util.List;
41  import java.util.Locale;
42  
43  import org.apache.commons.collections.CollectionUtils;
44  import org.apache.commons.lang3.StringUtils;
45  
46  import com.fasterxml.jackson.annotation.JsonInclude.Include;
47  import com.fasterxml.jackson.core.JsonParseException;
48  import com.fasterxml.jackson.core.JsonProcessingException;
49  import com.fasterxml.jackson.databind.JsonMappingException;
50  import com.fasterxml.jackson.databind.ObjectMapper;
51  import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
52  
53  import fr.paris.lutece.plugins.appointment.business.category.Category;
54  import fr.paris.lutece.plugins.appointment.business.display.Display;
55  import fr.paris.lutece.plugins.appointment.business.form.Form;
56  import fr.paris.lutece.plugins.appointment.business.localization.Localization;
57  import fr.paris.lutece.plugins.appointment.business.message.FormMessage;
58  import fr.paris.lutece.plugins.appointment.business.planning.ClosingDay;
59  import fr.paris.lutece.plugins.appointment.business.planning.TimeSlot;
60  import fr.paris.lutece.plugins.appointment.business.planning.WeekDefinition;
61  import fr.paris.lutece.plugins.appointment.business.planning.WorkingDay;
62  import fr.paris.lutece.plugins.appointment.business.rule.FormRule;
63  import fr.paris.lutece.plugins.appointment.business.rule.ReservationRule;
64  import fr.paris.lutece.plugins.appointment.business.slot.Slot;
65  import fr.paris.lutece.plugins.genericattributes.business.Entry;
66  import fr.paris.lutece.plugins.genericattributes.business.EntryHome;
67  import fr.paris.lutece.plugins.genericattributes.business.Field;
68  import fr.paris.lutece.plugins.genericattributes.business.FieldHome;
69  import fr.paris.lutece.plugins.regularexpression.business.RegularExpressionHome;
70  import fr.paris.lutece.plugins.workflow.service.WorkflowTraderService;
71  import fr.paris.lutece.portal.business.regularexpression.RegularExpression;
72  import fr.paris.lutece.portal.service.plugin.Plugin;
73  import fr.paris.lutece.portal.service.plugin.PluginService;
74  import fr.paris.lutece.portal.service.regularexpression.RegularExpressionService;
75  import fr.paris.lutece.portal.service.util.AppLogService;
76  import net.sf.json.JSONArray;
77  import net.sf.json.JSONObject;
78  
79  /**
80   * Import/Export Forms from Json
81   * 
82   * @author Laurent Payen
83   *
84   */
85  public final class FormTraderService
86  {
87  
88      // PROPERTIES
89      private static final String CATEGORY = "category";
90      private static final String CLOSING_DAYS = "closing_days";
91      private static final String DISPLAY = "display";
92      private static final String ENTRIES = "entries";
93      private static final String FIELDS = "fields";
94      private static final String FORM = "form";
95      private static final String FORM_MESSAGE = "form_message";
96      private static final String FORM_RULE = "form_rule";
97      private static final String IMPORT = "Import";
98      private static final String LOCALIZATION = "localization";
99      private static final String RESERVATION_RULES = "reservation_rules";
100     private static final String SLOTS = "slots";
101     private static final String WEEK_DEFINITIONS = "week_definitions";
102     private static final String WORKFLOW = "workflow";
103 
104     /**
105      * The mapper (need to add the javaTime module for Java 8 date compatibility)l
106      */
107     private static ObjectMapper _mapper = new ObjectMapper( ).registerModule( new JavaTimeModule( ) ).setSerializationInclusion( Include.NON_NULL );
108 
109     private static Plugin _plugin = PluginService.getPlugin( AppointmentPlugin.PLUGIN_NAME );
110 
111     /**
112      * Private constructor
113      */
114     private FormTraderService( )
115     {
116     }
117 
118     /**
119      * Import a form in database from a json
120      * 
121      * @param jsonObject
122      *            the json object
123      * @throws JsonParseException
124      * @throws JsonMappingException
125      * @throws IOException
126      */
127     public static void importFormFromJson( JSONObject jsonObject ) throws IOException
128     {
129         Form form = null;
130         Category category = null;
131         int nIdWorkflow = 0;
132         Object objectCategory = jsonObject.get( CATEGORY );
133         Object objectWorkflow = jsonObject.get( WORKFLOW );
134         try
135         {
136             if ( objectCategory != null )
137             {
138                 category = _mapper.readValue( objectCategory.toString( ), Category.class );
139                 if ( category != null )
140                 {
141                     category = CategoryService.saveCategory( category );
142                 }
143             }
144             if ( objectWorkflow != null )
145             {
146                 nIdWorkflow = WorkflowTraderService.importWorkflowFromJson( JSONObject.fromObject( objectWorkflow ) );
147             }
148             Object objectForm = jsonObject.get( FORM );
149             if ( objectForm != null )
150             {
151                 form = _mapper.readValue( objectForm.toString( ), Form.class );
152             }
153             if ( form != null )
154             {
155                 // To avoid multiple forms with same name
156                 if ( CollectionUtils.isNotEmpty( FormService.findFormsByTitle( form.getTitle( ) ) ) )
157                 {
158                     form.setTitle( IMPORT + StringUtils.SPACE + form.getTitle( ) );
159                 }
160                 if ( category != null )
161                 {
162                     form.setIdCategory( category.getIdCategory( ) );
163                 }
164                 if ( nIdWorkflow != 0 )
165                 {
166                     form.setIdWorkflow( nIdWorkflow );
167                 }
168                 form = FormService.saveForm( form );
169                 int nIdForm = form.getIdForm( );
170                 importFormRule( jsonObject, nIdForm );
171                 importDisplay( jsonObject, nIdForm );
172                 importLocalization( jsonObject, nIdForm );
173                 importFormMessage( jsonObject, nIdForm );
174                 importReservationRules( jsonObject, nIdForm );
175                 importClosingDays( jsonObject, nIdForm );
176                 importWeekDefinitions( jsonObject, nIdForm );
177                 importSlots( jsonObject, nIdForm );
178                 importEntries( jsonObject, nIdForm );
179             }
180         }
181         catch( IOException e )
182         {
183             AppLogService.error( "Error during import of the json", e );
184             throw e;
185         }
186     }
187 
188     /**
189      * Export a form in a json object
190      * 
191      * @param nIdForm
192      *            the form id
193      * @return a json object of the form
194      * @throws JsonProcessingException
195      */
196     public static JSONObject exportFormToJson( int nIdForm )
197     {
198         JSONObject jsObj = new JSONObject( );
199         try
200         {
201             Form form = FormService.findFormLightByPrimaryKey( nIdForm );
202             if ( form != null )
203             {
204                 jsObj.put( FORM, _mapper.writeValueAsString( form ) );
205                 Category category = CategoryService.findCategoryById( form.getIdCategory( ) );
206                 if ( category != null )
207                 {
208                     jsObj.put( CATEGORY, _mapper.writeValueAsString( category ) );
209                 }
210                 int nIdWorkflow = form.getIdWorkflow( );
211                 if ( nIdWorkflow > 0 )
212                 {
213                     jsObj.put( WORKFLOW, WorkflowTraderService.exportWorkflowToJson( nIdWorkflow, Locale.getDefault( ) ) );
214                 }
215             }
216             exportFormRule( jsObj, nIdForm );
217             exportDisplay( jsObj, nIdForm );
218             exportLocalization( jsObj, nIdForm );
219             exportFormMessage( jsObj, nIdForm );
220             exportReservationRules( jsObj, nIdForm );
221             exportClosingDays( jsObj, nIdForm );
222             exportWeekDefinitions( jsObj, nIdForm );
223             exportSlots( jsObj, nIdForm );
224             exportEntries( jsObj, nIdForm );
225         }
226         catch( JsonProcessingException e )
227         {
228             AppLogService.error( "Error during export of the form into json object", e );
229         }
230         return jsObj;
231     }
232 
233     /**
234      * Import the form rule part of a form from a json object
235      * 
236      * @param jsonObject
237      *            the json object
238      * @param nIdForm
239      *            the form id
240      * @throws JsonParseException
241      * @throws JsonMappingException
242      * @throws IOException
243      */
244     private static void importFormRule( JSONObject jsonObject, int nIdForm ) throws JsonParseException, JsonMappingException, IOException
245     {
246         FormRule formRule = null;
247         Object objectFormRule = jsonObject.get( FORM_RULE );
248         if ( objectFormRule != null )
249         {
250             formRule = _mapper.readValue( objectFormRule.toString( ), FormRule.class );
251         }
252         if ( formRule != null )
253         {
254             formRule.setIdForm( nIdForm );
255             FormRuleService.saveFormRule( formRule );
256         }
257     }
258 
259     /**
260      * Import the display part of a form from a json object
261      * 
262      * @param jsonObject
263      *            the json object
264      * @param nIdForm
265      *            the form id
266      * @throws JsonParseException
267      * @throws JsonMappingException
268      * @throws IOException
269      */
270     private static void importDisplay( JSONObject jsonObject, int nIdForm ) throws JsonParseException, JsonMappingException, IOException
271     {
272         Display display = null;
273         Object objectDisplay = jsonObject.get( DISPLAY );
274         if ( objectDisplay != null )
275         {
276             display = _mapper.readValue( objectDisplay.toString( ), Display.class );
277         }
278         if ( display != null )
279         {
280             display.setIdForm( nIdForm );
281             DisplayService.saveDisplay( display );
282         }
283     }
284 
285     /**
286      * Import the localization part of a form from a json object
287      * 
288      * @param jsonObject
289      *            the json object
290      * @param nIdForm
291      *            the form id
292      * @throws JsonParseException
293      * @throws JsonMappingException
294      * @throws IOException
295      */
296     private static void importLocalization( JSONObject jsonObject, int nIdForm ) throws JsonParseException, JsonMappingException, IOException
297     {
298         Localization localization = null;
299         Object objectLocalization = jsonObject.get( LOCALIZATION );
300         if ( objectLocalization != null )
301         {
302             localization = _mapper.readValue( objectLocalization.toString( ), Localization.class );
303         }
304         if ( localization != null )
305         {
306             localization.setIdForm( nIdForm );
307             LocalizationService.saveLocalization( localization );
308         }
309     }
310 
311     /**
312      * Import the form message part of a form from a json object
313      * 
314      * @param jsonObject
315      *            the json object
316      * @param nIdForm
317      *            the form id
318      * @throws JsonParseException
319      * @throws JsonMappingException
320      * @throws IOException
321      */
322     private static void importFormMessage( JSONObject jsonObject, int nIdForm ) throws JsonParseException, JsonMappingException, IOException
323     {
324         FormMessage formMessage = null;
325         Object objectFormMessage = jsonObject.get( FORM_MESSAGE );
326         if ( objectFormMessage != null )
327         {
328             formMessage = _mapper.readValue( objectFormMessage.toString( ), FormMessage.class );
329         }
330         if ( formMessage != null )
331         {
332             formMessage.setIdForm( nIdForm );
333             FormMessageService.saveFormMessage( formMessage );
334         }
335     }
336 
337     /**
338      * Import the reservation rule part of a form from a json object
339      * 
340      * @param jsonObject
341      *            the json object
342      * @param nIdForm
343      *            the form id
344      * @throws JsonParseException
345      * @throws JsonMappingException
346      * @throws IOException
347      */
348     private static void importReservationRules( JSONObject jsonObject, int nIdForm ) throws JsonParseException, JsonMappingException, IOException
349     {
350         List<ReservationRule> listReservationRules = new ArrayList<>( );
351         JSONArray jsArrayReservationRules = null;
352         if ( jsonObject.containsKey( RESERVATION_RULES ) )
353         {
354             jsArrayReservationRules = jsonObject.getJSONArray( RESERVATION_RULES );
355         }
356         if ( CollectionUtils.isNotEmpty( jsArrayReservationRules ) )
357         {
358             listReservationRules = Arrays.asList( _mapper.readValue( jsArrayReservationRules.toString( ), ReservationRule [ ].class ) );
359         }
360         for ( ReservationRule reservationRule : listReservationRules )
361         {
362             reservationRule.setIdForm( nIdForm );
363             ReservationRuleService.saveReservationRule( reservationRule );
364         }
365     }
366 
367     /**
368      * Import the closing days of a form from a json object
369      * 
370      * @param jsonObject
371      *            the json object
372      * @param nIdForm
373      *            the form id
374      * @throws JsonParseException
375      * @throws JsonMappingException
376      * @throws IOException
377      */
378     private static void importClosingDays( JSONObject jsonObject, int nIdForm ) throws JsonParseException, JsonMappingException, IOException
379     {
380         List<ClosingDay> listClosingDays = new ArrayList<>( );
381         JSONArray jsArrayClosingDays = null;
382         if ( jsonObject.containsKey( CLOSING_DAYS ) )
383         {
384             jsArrayClosingDays = jsonObject.getJSONArray( CLOSING_DAYS );
385         }
386         if ( CollectionUtils.isNotEmpty( jsArrayClosingDays ) )
387         {
388             listClosingDays = Arrays.asList( _mapper.readValue( jsArrayClosingDays.toString( ), ClosingDay [ ].class ) );
389         }
390         for ( ClosingDay closingDay : listClosingDays )
391         {
392             closingDay.setIdForm( nIdForm );
393             ClosingDayService.saveClosingDay( closingDay );
394         }
395     }
396 
397     /**
398      * Import the week definition of a form from a json object
399      * 
400      * @param jsonObject
401      *            the json object
402      * @param nIdForm
403      *            the form id
404      * @throws JsonParseException
405      * @throws JsonMappingException
406      * @throws IOException
407      */
408     private static void importWeekDefinitions( JSONObject jsonObject, int nIdForm ) throws JsonParseException, JsonMappingException, IOException
409     {
410         List<WeekDefinition> listWeekDefinitions = new ArrayList<>( );
411         HashMap<Integer, Integer> mapIdWeekDefinition = new HashMap<>( );
412         HashMap<Integer, Integer> mapIdWorkingDay = new HashMap<>( );
413         JSONArray jsArrayWeekDefinitions = null;
414         int nOldIdWeekDefinition;
415         int nNewIdWeekDefinition;
416         int nOldIdWorkingDay;
417         int nNewIdWorkingDay;
418         if ( jsonObject.containsKey( WEEK_DEFINITIONS ) )
419         {
420             jsArrayWeekDefinitions = jsonObject.getJSONArray( WEEK_DEFINITIONS );
421         }
422         if ( CollectionUtils.isNotEmpty( jsArrayWeekDefinitions ) )
423         {
424             listWeekDefinitions = Arrays.asList( _mapper.readValue( jsArrayWeekDefinitions.toString( ), WeekDefinition [ ].class ) );
425         }
426         for ( WeekDefinition weekDefinition : listWeekDefinitions )
427         {
428             nOldIdWeekDefinition = weekDefinition.getIdWeekDefinition( );
429             weekDefinition.setIdForm( nIdForm );
430             weekDefinition = WeekDefinitionService.saveWeekDefinition( weekDefinition );
431             mapIdWeekDefinition.put( nOldIdWeekDefinition, weekDefinition.getIdWeekDefinition( ) );
432             for ( WorkingDay workingDay : weekDefinition.getListWorkingDay( ) )
433             {
434                 nOldIdWorkingDay = workingDay.getIdWorkingDay( );
435                 nNewIdWeekDefinition = 0;
436                 if ( mapIdWeekDefinition.containsKey( workingDay.getIdWeekDefinition( ) ) )
437                 {
438                     nNewIdWeekDefinition = mapIdWeekDefinition.get( workingDay.getIdWeekDefinition( ) );
439                 }
440                 workingDay.setIdWeekDefinition( nNewIdWeekDefinition );
441                 workingDay = WorkingDayService.saveWorkingDay( workingDay );
442                 mapIdWorkingDay.put( nOldIdWorkingDay, workingDay.getIdWorkingDay( ) );
443                 for ( TimeSlot timeSlot : workingDay.getListTimeSlot( ) )
444                 {
445                     nNewIdWorkingDay = 0;
446                     if ( mapIdWorkingDay.containsKey( timeSlot.getIdWorkingDay( ) ) )
447                     {
448                         nNewIdWorkingDay = mapIdWorkingDay.get( timeSlot.getIdWorkingDay( ) );
449                     }
450                     timeSlot.setIdWorkingDay( nNewIdWorkingDay );
451                     TimeSlotService.saveTimeSlot( timeSlot );
452                 }
453             }
454         }
455     }
456 
457     /**
458      * import the slots of a form from a json object
459      * 
460      * @param jsonObject
461      *            the json object
462      * @param nIdForm
463      *            the form id
464      * @throws JsonParseException
465      * @throws JsonMappingException
466      * @throws IOException
467      */
468     private static void importSlots( JSONObject jsonObject, int nIdForm ) throws JsonParseException, JsonMappingException, IOException
469     {
470         List<Slot> listSlots = new ArrayList<>( );
471         JSONArray jsArraySlots = null;
472         if ( jsonObject.containsKey( SLOTS ) )
473         {
474             jsArraySlots = jsonObject.getJSONArray( SLOTS );
475         }
476         if ( CollectionUtils.isNotEmpty( jsArraySlots ) )
477         {
478             listSlots = Arrays.asList( _mapper.readValue( jsArraySlots.toString( ), Slot [ ].class ) );
479         }
480         for ( Slot slot : listSlots )
481         {
482             slot.setIdForm( nIdForm );
483             slot.setIdSlot( 0 );
484             SlotService.saveSlot( slot );
485         }
486     }
487 
488     /**
489      * Import the generic attributes of a form from a json object
490      * 
491      * @param jsonObject
492      *            the json object
493      * @param nIdForm
494      *            the form id
495      * @throws JsonParseException
496      * @throws JsonMappingException
497      * @throws IOException
498      */
499     private static void importEntries( JSONObject jsonObject, int nIdForm ) throws JsonParseException, JsonMappingException, IOException
500     {
501         List<Entry> listEntries = new ArrayList<>( );
502         List<Field> listFields = new ArrayList<>( );
503         HashMap<Integer, Integer> mapIdEntry = new HashMap<>( );
504         JSONArray jsArrayEntries = null;
505         JSONArray jsArrayFields = null;
506         int nOldIdEntry;
507         int nNewIdEntry;
508         int nNewIdField;
509         if ( jsonObject.containsKey( ENTRIES ) )
510         {
511             jsArrayEntries = jsonObject.getJSONArray( ENTRIES );
512         }
513         if ( jsonObject.containsKey( FIELDS ) )
514         {
515             jsArrayFields = jsonObject.getJSONArray( FIELDS );
516         }
517         if ( CollectionUtils.isNotEmpty( jsArrayEntries ) )
518         {
519             listEntries = Arrays.asList( _mapper.readValue( jsArrayEntries.toString( ), Entry [ ].class ) );
520         }
521         for ( Entry entry : listEntries )
522         {
523             nOldIdEntry = entry.getIdEntry( );
524             entry.setIdResource( nIdForm );
525             nNewIdEntry = EntryHome.create( entry );
526             mapIdEntry.put( nOldIdEntry, nNewIdEntry );
527         }
528         if ( CollectionUtils.isNotEmpty( jsArrayFields ) )
529         {
530             listFields = Arrays.asList( _mapper.readValue( jsArrayFields.toString( ), Field [ ].class ) );
531         }
532         HashMap<String, RegularExpression> mapRegularExpression = new HashMap<>( );
533         List<RegularExpression> listRegularExpressionsInDB = RegularExpressionService.getInstance( ).getAllRegularExpression( );
534         for ( RegularExpression regularExpression : listRegularExpressionsInDB )
535         {
536             if ( !mapRegularExpression.containsKey( regularExpression.getTitle( ) ) )
537             {
538                 mapRegularExpression.put( regularExpression.getTitle( ), regularExpression );
539             }
540         }
541         for ( Field field : listFields )
542         {
543             nNewIdEntry = 0;
544             if ( field.getParentEntry( ) != null && mapIdEntry.containsKey( field.getParentEntry( ).getIdEntry( ) ) )
545             {
546                 nNewIdEntry = mapIdEntry.get( field.getParentEntry( ).getIdEntry( ) );
547             }
548             field.setParentEntry( EntryHome.findByPrimaryKey( nNewIdEntry ) );
549             nNewIdField = FieldHome.create( field );
550             List<RegularExpression> listRegularExpressionsToImport = field.getRegularExpressionList( );
551             int nIdExpressionToGet;
552             for ( RegularExpression regularExpression : listRegularExpressionsToImport )
553             {
554                 if ( mapRegularExpression.containsKey( regularExpression.getTitle( ) ) )
555                 {
556                     nIdExpressionToGet = mapRegularExpression.get( regularExpression.getTitle( ) ).getIdExpression( );
557                 }
558                 else
559                 {
560                     RegularExpressionHome.create( regularExpression, _plugin );
561                     nIdExpressionToGet = regularExpression.getIdExpression( );
562                 }
563                 FieldHome.createVerifyBy( nNewIdField, nIdExpressionToGet );
564             }
565         }
566     }
567 
568     /**
569      * Export and save in database the form rule of a form from a json object
570      * 
571      * @param jsObj
572      *            the json object
573      * @param nIdForm
574      *            the form id
575      * @throws JsonProcessingException
576      */
577     private static void exportFormRule( JSONObject jsObj, int nIdForm ) throws JsonProcessingException
578     {
579         FormRule formRule = FormRuleService.findFormRuleWithFormId( nIdForm );
580         if ( formRule != null )
581         {
582             jsObj.put( FORM_RULE, _mapper.writeValueAsString( formRule ) );
583         }
584     }
585 
586     /**
587      * Export and save in database the display of a form from a json object
588      * 
589      * @param jsObj
590      *            the json object
591      * @param nIdForm
592      *            the form id
593      * @throws JsonProcessingException
594      */
595     private static void exportDisplay( JSONObject jsObj, int nIdForm ) throws JsonProcessingException
596     {
597         Display display = DisplayService.findDisplayWithFormId( nIdForm );
598         if ( display != null )
599         {
600             jsObj.put( DISPLAY, _mapper.writeValueAsString( display ) );
601         }
602     }
603 
604     /**
605      * Export and save in database the localization of a form from a json object
606      * 
607      * @param jsObj
608      *            the json object
609      * @param nIdForm
610      *            the form id
611      * @throws JsonProcessingException
612      */
613     private static void exportLocalization( JSONObject jsObj, int nIdForm ) throws JsonProcessingException
614     {
615         Localization localization = LocalizationService.findLocalizationWithFormId( nIdForm );
616         if ( localization != null )
617         {
618             jsObj.put( LOCALIZATION, _mapper.writeValueAsString( localization ) );
619         }
620     }
621 
622     /**
623      * Export and save in database the form message of a form from a json object
624      * 
625      * @param jsObj
626      *            the json object
627      * @param nIdForm
628      *            the form id
629      * @throws JsonProcessingException
630      */
631     private static void exportFormMessage( JSONObject jsObj, int nIdForm ) throws JsonProcessingException
632     {
633         FormMessage formMessage = FormMessageService.findFormMessageByIdForm( nIdForm );
634         if ( formMessage != null )
635         {
636             jsObj.put( FORM_MESSAGE, _mapper.writeValueAsString( formMessage ) );
637         }
638     }
639 
640     /**
641      * Export and save in database the reservation rules of a form from a json object
642      * 
643      * @param jsObj
644      *            the json object
645      * @param nIdForm
646      *            the form id
647      * @throws JsonProcessingException
648      */
649     private static void exportReservationRules( JSONObject jsObj, int nIdForm ) throws JsonProcessingException
650     {
651         JSONArray jsReservationRules = new JSONArray( );
652         for ( ReservationRule reservationRule : ReservationRuleService.findListReservationRule( nIdForm ) )
653         {
654             jsReservationRules.add( _mapper.writeValueAsString( reservationRule ) );
655         }
656         if ( CollectionUtils.isNotEmpty( jsReservationRules ) )
657         {
658             jsObj.put( RESERVATION_RULES, jsReservationRules );
659         }
660     }
661 
662     /**
663      * Export and save in database the closing days of a form from a json object
664      * 
665      * @param jsObj
666      *            the json object
667      * @param nIdForm
668      *            the form id
669      * @throws JsonProcessingException
670      */
671     private static void exportClosingDays( JSONObject jsObj, int nIdForm ) throws JsonProcessingException
672     {
673         JSONArray jsClosingDays = new JSONArray( );
674         for ( ClosingDay closingDay : ClosingDayService.findListClosingDay( nIdForm ) )
675         {
676 
677             jsClosingDays.add( _mapper.writeValueAsString( closingDay ) );
678         }
679         if ( CollectionUtils.isNotEmpty( jsClosingDays ) )
680         {
681             jsObj.put( CLOSING_DAYS, jsClosingDays );
682         }
683     }
684 
685     /**
686      * Export and save in database the week definition of a form from a json object
687      * 
688      * @param jsObj
689      *            the json object
690      * @param nIdForm
691      *            the form id
692      * @throws JsonProcessingException
693      */
694     private static void exportWeekDefinitions( JSONObject jsObj, int nIdForm ) throws JsonProcessingException
695     {
696         JSONArray jsWeekDefinitions = new JSONArray( );
697         for ( WeekDefinition weekDefinition : WeekDefinitionService.findListWeekDefinition( nIdForm ) )
698         {
699             jsWeekDefinitions.add( _mapper.writeValueAsString( weekDefinition ) );
700         }
701         if ( CollectionUtils.isNotEmpty( jsWeekDefinitions ) )
702         {
703             jsObj.put( WEEK_DEFINITIONS, jsWeekDefinitions );
704         }
705     }
706 
707     /**
708      * Export and save in database the slots of a form from a json object
709      * 
710      * @param jsObj
711      *            the json object
712      * @param nIdForm
713      *            the form id
714      * @throws JsonProcessingException
715      */
716     private static void exportSlots( JSONObject jsObj, int nIdForm ) throws JsonProcessingException
717     {
718         JSONArray jsSlots = new JSONArray( );
719         for ( Slot slot : SlotService.findListSlot( nIdForm ) )
720         {
721 
722             jsSlots.add( _mapper.writeValueAsString( slot ) );
723         }
724         if ( CollectionUtils.isNotEmpty( jsSlots ) )
725         {
726             jsObj.put( SLOTS, jsSlots );
727         }
728     }
729 
730     /**
731      * Export and save in database the generic attributes of a form from a json object
732      * 
733      * @param jsObj
734      *            the json object
735      * @param nIdForm
736      *            the form id
737      * @throws JsonProcessingException
738      */
739     private static void exportEntries( JSONObject jsObj, int nIdForm ) throws JsonProcessingException
740     {
741         JSONArray jsEntries = new JSONArray( );
742         JSONArray jsFields = new JSONArray( );
743         for ( Entry entry : EntryService.findListEntry( nIdForm ) )
744         {
745             jsEntries.add( _mapper.writeValueAsString( entry ) );
746             for ( Field fieldLight : entry.getFields( ) )
747             {
748                 Field fullField = FieldHome.findByPrimaryKey( fieldLight.getIdField( ) );
749                 jsFields.add( _mapper.writeValueAsString( fullField ) );
750             }
751         }
752         if ( CollectionUtils.isNotEmpty( jsEntries ) )
753         {
754             jsObj.put( ENTRIES, jsEntries );
755         }
756         if ( CollectionUtils.isNotEmpty( jsFields ) )
757         {
758             jsObj.put( FIELDS, jsFields );
759         }
760     }
761 }