Class: NCal2GCal::NotesGoogleSync

Inherits:
Object
  • Object
show all
Defined in:
lib/ncal2gcal/sync.rb

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ NotesGoogleSync

Returns a new instance of NotesGoogleSync.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ncal2gcal/sync.rb', line 17

def initialize(params)
  @notes_calendar = NCal2GCal::LotusNotesCalendar.new(params)
  @google_calendar = NCal2GCal::GoogleCalendar.new(params)
  
  @sync_time = nil
  if params[:days]
     @min_sync_time = DateTime.now-params[:days] #*86400
  end
  if params[:days_max]
     @max_sync_time = DateTime.now+params[:days_max] #*86400
  else 
     @max_sync_time = DateTime.now+400 #*86400
  end
  @max_time = DateTime.parse("2038-01-18")
  # do not sync the description unless the users wants to
  @sync_desc = params[:sync_desc] # || true
  @sync_alarm = params[:sync_alarm]
  @sync_names = params[:sync_names]
  
  init_logger
end

Instance Method Details

#add_event(notes_event, start_time, end_time, key) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/ncal2gcal/sync.rb', line 174

def add_event(notes_event, start_time, end_time, key)
  print "I"
  google_event=init_google_event(notes_event, start_time, end_time)
  ret = google_event.save
  $logger.fatal "insert: cannot save gcal event" unless ret
  raise "cannot save gcal event" unless ret
  @counter.inserts +=1
  sync_entry = NCal2GCal::SyncEntry.new
  sync_entry.lotus_notes_uid = key #notes_event.uid
  sync_entry.sync_time = @sync_time
  sync_entry.lotus_notes_last_modified = notes_event.last_modified
  sync_entry.gcal_id = google_event.id
  sync_entry.sync_action = 'I' # insert
  sync_entry.save
end

#del_eventsObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ncal2gcal/sync.rb', line 110

def del_events
    NCal2GCal::SyncEntry.all(:sync_time.lt => @sync_time).each do |sync_entry|
      @counter.deletes += 1
      if @google_calendar.del_event(sync_entry.gcal_id)
        print "D"
        sync_entry.destroy
      else 
        sync_entry.sync_time = @sync_time
        sync_entry.sync_action = 'E'
        sync_entry.save
        print "E"
      end
    end
end

#get_sync_entry_by_notes_uid(uid) ⇒ Object



160
161
162
163
# File 'lib/ncal2gcal/sync.rb', line 160

def get_sync_entry_by_notes_uid(uid)
  e1 = NCal2GCal::SyncEntry.first(:lotus_notes_uid => uid)
  return e1
end

#init_google_event(notes_event, start_time, end_time) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/ncal2gcal/sync.rb', line 124

def init_google_event(notes_event,start_time,end_time)
  event = @google_calendar.new_event
  google_event= set_google_event_attrs(notes_event, event,start_time,end_time )
  google_event.start_time = start_time 
  google_event.end_time = end_time 
  return google_event
end

#init_loggerObject



38
39
40
41
42
43
44
45
46
# File 'lib/ncal2gcal/sync.rb', line 38

def init_logger
  FileUtils::mkdir_p('ncal2gcal')
  $logger = Log4r::Logger.new("sync_logger")      
  Log4r::FileOutputter.new('logfile', 
                     :filename=>"#{Dir.pwd}/ncal2gcal/ncal2gcal.log", 
                     :trunc=>false,
                     :level=>Log4r::WARN)
  $logger.add('logfile')
end

#insert_update(sync_entry, notes_event, start_time, end_time, key) ⇒ Object



164
165
166
167
168
169
170
171
172
# File 'lib/ncal2gcal/sync.rb', line 164

def insert_update(sync_entry,notes_event, start_time, end_time, key)
  gcal_event = @google_calendar.find_event(sync_entry.gcal_id)
  if gcal_event == []
    $logger.warn "Event not found for update"
    add_event(notes_event,start_time, end_time,key)
  else 
    update_event(sync_entry, notes_event, gcal_event, start_time, end_time)
  end
end

#set_google_event_attrs(notes_event, google_event, start_time = nil, end_time = nil) ⇒ Object



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
# File 'lib/ncal2gcal/sync.rb', line 131

def set_google_event_attrs(notes_event, google_event,start_time=nil,end_time=nil)
  google_event.title = notes_event.subject.asciify if notes_event.subject
  if start_time
    google_event.start_time = start_time     
  else
    google_event.start_time = notes_event.start_time 
  end
  if end_time
    google_event.end_time = end_time 
  else
    google_event.end_time = notes_event.end_time 
  end
  google_event.where = notes_event.where.asciify if notes_event.where
  google_event.all_day = notes_event.all_day?
  
  if (@sync_desc || @sync_names)
    content = ''
    content += notes_event.formatted_names.asciify if (@sync_names and notes_event.all_names.size > 0)
    content += notes_event.content.asciify if @sync_desc      
    google_event.content = content 
    #puts content
  end
  
  if @sync_alarm and notes_event.alarm
    google_event.reminder = {:method =>'alert', :minutes => notes_event.alarm_offset.to_i.to_s }
  end
  
  return google_event
end

#sync_event(notes_event, start_time, end_time, key) ⇒ Object



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
# File 'lib/ncal2gcal/sync.rb', line 68

def sync_event(notes_event, start_time, end_time, key)
    sdt = DateTime.parse(start_time)
    return unless sdt < @max_time # workaround 

    if end_time
      edt = DateTime.parse(end_time)
      return unless edt < @max_time # workaround 
    end
    
    #puts DateTime.parse(notes_event.end_time)
    
    if (@min_sync_time and end_time and 
       @min_sync_time > DateTime.parse(end_time)) or
      (@max_sync_time and start_time and
        @max_sync_time < DateTime.parse(start_time))
    then
      @counter.ignored +=1
    else
      #p key
      sync_entry = NCal2GCal::SyncEntry.first(:lotus_notes_uid => key) 
      if sync_entry  
      then
        #puts DateTime.parse(notes_event.end_time)
        #p sync_entry.lotus_notes_last_modified.to_s
        #p notes_event.last_modified
        if sync_entry.lotus_notes_last_modified < notes_event.last_modified
        then
          #!!insert_update(sync_entry,notes_event)
          insert_update(sync_entry,notes_event, start_time, end_time, key)
        else
          print "."
          @counter.ignored +=1
          sync_entry.sync_time = @sync_time
          sync_entry.sync_action = 'N' # none
          sync_entry.save
        end
      else
        add_event(notes_event, start_time, end_time, key)
      end
    end
end

#sync_eventsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ncal2gcal/sync.rb', line 47

def sync_events
  @counter = Counter.new
  @sync_time = DateTime.now
  sleep(1)
  
  @notes_calendar.events.each do |notes_event|
    @counter.selects += 1
    if notes_event.repeats?
      notes_event.repeats.each do |r|
        sync_event(notes_event, r.start_time, r.end_time, notes_event.uid+'_'+r.start_time)
      end
    else
      #p notes_event.repeats
      sync_event(notes_event, notes_event.start_time, notes_event.end_time, notes_event.uid)
    end
  end
  del_events()
  @counter.end
  return @counter
end

#update_event(sync_entry, notes_event, gcal_event, start_time, end_time) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/ncal2gcal/sync.rb', line 189

def update_event(sync_entry, notes_event, gcal_event, start_time, end_time)
  print "U"
  @counter.updates +=1
  set_google_event_attrs(notes_event, gcal_event, start_time, end_time)
  ret = gcal_event.save
  $logger.fatal "update: cannot save gcal event" unless ret
  raise "cannot save gcal event" unless ret
  sync_entry.sync_time = @sync_time
  sync_entry.gcal_id = gcal_event.id
  sync_entry.lotus_notes_last_modified = notes_event.last_modified
  sync_entry.sync_action = 'U' # none
  sync_entry.save
end