| Quintiq file version 2.0 | 
| #parent: #root | 
| Method MPSyncUnitCalendar ( | 
|   Boolean isoverwritemanualconfig, | 
|   IOUnitCalendars iounitcalendars | 
| ) | 
| { | 
|   Description: | 
|   [* | 
|     Synchronization of IOUnitCalendar from MPSync to UnitCalendar Macroplan | 
|     Synchronization of IOEvent from MPSync to LibCal_Event Macroplan | 
|     Synchronization of IOParticipation from MPSync to LibCal_Participation Macroplan | 
|   *] | 
|   TextBody: | 
|   [* | 
|     // Get Common calendar | 
|     commoncalendar := this.CalendarRegistry().CommonCalendar(); | 
|      | 
|     // Select a set of existing UnitCalendar | 
|     existingUnitCalendar := selectset( this, Unit.UnitCalendar, uc, true ); | 
|      | 
|     // Select a set of existing Event | 
|     existingEvent := selectset( existingUnitCalendar, Elements.Event, e, true ); | 
|     updatesEvent := construct( LibCal_Events ); | 
|      | 
|     // Select a set of existing Participation | 
|     existingParticipation := selectset( existingUnitCalendar, Elements.Participation.astype( LibCal_Subscription ), p, true ); | 
|     updatesParticipation := construct( LibCal_Subscriptions ); | 
|      | 
|     // Select a set of existing LeadingParticipation | 
|     existingLeadingParticipation := selectset( existingUnitCalendar, Elements.Event.LeadingParticipation, lp, true ); | 
|     updatesLeadingParticipation := construct( LibCal_LeadingParticipations ); | 
|      | 
|     // Traverse IOUnitCalendar objects | 
|     traverse( iounitcalendars, Elements, iounitcalendar ) | 
|     { | 
|       // Find existing Unit | 
|       unit := Unit::FindUnitTypeIndex( iounitcalendar.OwnerID() ); | 
|        | 
|       if( not isnull( unit ) ) | 
|       { | 
|         // Create UnitCalendar if unit doesn't have one | 
|         unitCalendar := unit.FindCreateUnitCalendar( iounitcalendar );  | 
|         if( not isnull( unitCalendar ) ) | 
|         { | 
|           unitCalendar.Synchronize( iounitcalendar.Base(),  | 
|                                     iounitcalendar.UpdateInterval(),  | 
|                                     iounitcalendar.Window(),  | 
|                                     iounitcalendar.History(),  | 
|                                     iounitcalendar.IsAlwaysAvailable() ); | 
|         } | 
|                      | 
|         // Event | 
|         traverse( iounitcalendar, IOEvent, ioevent ) | 
|         { | 
|           ioleadparticipation := ioevent.IOLeadingParticipation(); | 
|           if( not isnull( ioleadparticipation ) ) | 
|           { | 
|             event := unitCalendar.SynchronizeEvent( ioevent.EventID(), | 
|                                                     ioevent.CalendarType(), | 
|                                                     ioevent.CalendarID(), | 
|                                                     ioevent.Category(), | 
|                                                     ioevent.Subject(), | 
|                                                     ioevent.Description(), | 
|                                                     ioevent.Type(), | 
|                                                     ioevent.PartialCapacity(), | 
|                                                     ioevent.IsDefault(), | 
|                                                     ioevent.IsRecurring(), | 
|                                                     ioevent.IsAllDay(), | 
|                                                     ioevent.StartTimeOfDay(), | 
|                                                     ioevent.Duration(), | 
|                                                     ioevent.PatternType(), | 
|                                                     ioevent.RecurrenceInterval(), | 
|                                                     ioevent.PatternDaily_IsEveryWeekday(), | 
|                                                     ioevent.PatternWeekly_Weekdays(), | 
|                                                     ioevent.PatternMonthly_IsDay(), | 
|                                                     ioevent.PatternMonthly_Day(), | 
|                                                     ioevent.PatternMonthly_WeekOfMonth(), | 
|                                                     ioevent.PatternMonthly_DayOfWeek(), | 
|                                                     ioevent.PatternYearly_IsDay(), | 
|                                                     ioevent.PatternYearly_Month(), | 
|                                                     ioevent.PatternYearly_Day(), | 
|                                                     ioevent.PatternYearly_WeekOfMonth(), | 
|                                                     ioevent.PatternYearly_DayOfWeek(), | 
|                                                     ioleadparticipation.ParticipationID(), | 
|                                                     ioleadparticipation.StartDate(), | 
|                                                     ioleadparticipation.HasSpecificPeriod(), | 
|                                                     ioleadparticipation.PeriodType(), | 
|                                                     ioleadparticipation.PeriodStartDate(), | 
|                                                     ioleadparticipation.PeriodNrOfOccurences(), | 
|                                                     ioleadparticipation.PeriodStartDate() ); | 
|           updatesEvent.Add( event ); | 
|           updatesLeadingParticipation.Add( event.LeadingParticipation() );                                      | 
|           } | 
|         } | 
|       } | 
|     } | 
|      | 
|     // After every Events are created, traverse Events to created Participations | 
|     // Traverse IOEvent objects | 
|     traverse( iounitcalendars, Elements.IOEvent, ioevent ) | 
|     { | 
|       // Find exisiting Event | 
|       event := LibCal_Event::FindByEventID( ioevent.EventID() ); | 
|       // Participation | 
|       traverse( ioevent, IOParticipation, ioparticipation ) | 
|       { | 
|         // Check preventing error in SynchronizeSubscription with null subscription being created | 
|         if( event.Calendar() <> event.LeadingParticipation().Calendar() ) | 
|         { | 
|             participation := event.Calendar().SynchronizeSubscription( ioparticipation.ParticipationID(), | 
|                                                                        ioparticipation.EventID(), | 
|                                                                        ioparticipation.CalendarType(), | 
|                                                                        ioparticipation.CalendarID(), | 
|                                                                        ioparticipation.EventIsRecurring(), | 
|                                                                        event.IsAllDay(), | 
|                                                                        event.StartTimeOfDay(), | 
|                                                                        event.Duration(), | 
|                                                                        ioparticipation.StartDate(), | 
|                                                                        ioparticipation.HasSpecificPeriod(), | 
|                                                                        ioparticipation.PeriodType(), | 
|                                                                        ioparticipation.PeriodStartDate(), | 
|                                                                        ioparticipation.PeriodNrOfOccurences(), | 
|                                                                        ioparticipation.PeriodEndDate() ); | 
|          updatesParticipation.Add( participation ); | 
|          }                                                                                                    | 
|       } | 
|     } | 
|      | 
|     // Make sure that the declarative logic is propagated before creating occurences. | 
|     Transaction::Transaction().Propagate( attribute( LibCal_Calendar, EndDate ) ); | 
|      | 
|     // Get every Calendars | 
|     synchronizedCalendars := this.CalendarRegistry().Calendar( relget ); | 
|      | 
|     // Update the calendars that have been synchronized. | 
|     traverse( synchronizedCalendars, Elements, calendar ) | 
|     { | 
|       calendar.GenerateOccurrences(); | 
|     } | 
|      | 
|     // Get the set of old LeadingParticipation to be deleted | 
|     tobedeletesetleadingparticipation := existingLeadingParticipation.Difference( updatesLeadingParticipation ); | 
|      | 
|     // Traverse the set of LeadingParticipation to be deleted | 
|     traverse( tobedeletesetleadingparticipation, Elements, ele ) | 
|     { | 
|       ele.Delete(); | 
|     } | 
|      | 
|     // Get the set of old Participation to be deleted | 
|     // Filter out participation to Common Calendar | 
|     tobedeletesetparticipation := existingParticipation.Difference( updatesParticipation ); | 
|      | 
|     // Traverse the set of Participation to be deleted | 
|     traverse( tobedeletesetparticipation, Elements, ele, ele.Event().Calendar() <> commoncalendar ) | 
|     { | 
|       ele.Delete(); | 
|     } | 
|      | 
|     // Get the set of old Event to be deleted | 
|     tobedeletesetevent := existingEvent.Difference( updatesEvent ); | 
|      | 
|     // Traverse the set of Event to be deleted | 
|     traverse( tobedeletesetevent, Elements, ele ) | 
|     { | 
|       ele.Delete(); | 
|     } | 
|   *] | 
| } |