yanweiyuan3
2023-08-09 588bc7829387dfc761cc25f06f77d4c81818bd10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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();
    }
  *]
}