Class: Zimbra::Appointment

Inherits:
Object
  • Object
show all
Defined in:
lib/zimbra/appointment.rb,
lib/zimbra/appointment/alarm.rb,
lib/zimbra/appointment/reply.rb,
lib/zimbra/appointment/invite.rb,
lib/zimbra/appointment/attendee.rb,
lib/zimbra/appointment/recur_rule.rb,
lib/zimbra/appointment/recur_exception.rb

Defined Under Namespace

Classes: Alarm, Attendee, Invite, RecurException, RecurRule, Reply

Constant Summary collapse

ATTRS =
[
  :id, :uid, :date, :revision, :size, :calendar_id, 
  :replies, :invites, :invites_zimbra_attributes, :invites_attributes
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Appointment

Returns a new instance of Appointment.



60
61
62
63
# File 'lib/zimbra/appointment.rb', line 60

def initialize(args = {})
  self.attributes = args
  @loaded_from_search = args[:loaded_from_search] || false
end

Instance Attribute Details

#loaded_from_searchObject (readonly)

Returns the value of attribute loaded_from_search.



58
59
60
# File 'lib/zimbra/appointment.rb', line 58

def loaded_from_search
  @loaded_from_search
end

Class Method Details

.find(appointment_id) ⇒ Object



23
24
25
26
27
# File 'lib/zimbra/appointment.rb', line 23

def find(appointment_id)
  attrs = AppointmentService.find(appointment_id)
  return nil unless attrs
  new_from_zimbra_attributes(attrs)
end

.find_all_by_calendar_id(calendar_id) ⇒ Object



15
16
17
# File 'lib/zimbra/appointment.rb', line 15

def find_all_by_calendar_id(calendar_id)
  AppointmentService.find_all_by_calendar_id(calendar_id).collect { |attrs| new_from_zimbra_attributes(attrs.merge(:loaded_from_search => true)) }
end

.find_all_by_calendar_id_since(calendar_id, since_date) ⇒ Object



19
20
21
# File 'lib/zimbra/appointment.rb', line 19

def find_all_by_calendar_id_since(calendar_id, since_date)
  AppointmentService.find_all_by_calendar_id_since(calendar_id, since_date).collect { |attrs| new_from_zimbra_attributes(attrs.merge(:loaded_from_search => true)) }
end

.new_from_zimbra_attributes(zimbra_attributes) ⇒ Object



29
30
31
# File 'lib/zimbra/appointment.rb', line 29

def new_from_zimbra_attributes(zimbra_attributes)
  new(parse_zimbra_attributes(zimbra_attributes))
end

.parse_zimbra_attributes(zimbra_attributes) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/zimbra/appointment.rb', line 33

def parse_zimbra_attributes(zimbra_attributes)
  zimbra_attributes = Zimbra::Hash.symbolize_keys(zimbra_attributes.dup, true)
  
  return {} unless zimbra_attributes.has_key?(:appt) && zimbra_attributes[:appt].has_key?(:attributes)
  
  {
    :id                        => zimbra_attributes[:appt][:attributes][:id],
    :uid                       => zimbra_attributes[:appt][:attributes][:uid],
    :revision                  => zimbra_attributes[:appt][:attributes][:rev],
    :calendar_id               => zimbra_attributes[:appt][:attributes][:l],
    :size                      => zimbra_attributes[:appt][:attributes][:s],
    :replies                   => zimbra_attributes[:appt][:replies],
    :invites_zimbra_attributes => zimbra_attributes[:appt][:inv],
    :date                      => zimbra_attributes[:appt][:attributes][:d],
    :loaded_from_search        => zimbra_attributes[:loaded_from_search]
  }
end

Instance Method Details

#attributes=(args = {}) ⇒ Object



65
66
67
68
69
# File 'lib/zimbra/appointment.rb', line 65

def attributes=(args = {})
  ATTRS.each do |attr_name|
    self.send(:"#{attr_name}=", args[attr_name]) if args.has_key?(attr_name)
  end
end

#create_xml(document, invite_id = nil) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/zimbra/appointment.rb', line 116

def create_xml(document, invite_id = nil)
  document.add "m" do |mime|
    mime.set_attr "l", calendar_id
    
    invites.each do |invite|
      next unless invite_id.nil? || invite_id == invite.id
      
      mime.add "inv" do |invite_element|
        invite.create_xml(invite_element)
      end
    end
  end
  
  document
end

#date=(val) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/zimbra/appointment.rb', line 108

def date=(val)
  if val.is_a?(Integer)
    @date = parse_date_in_seconds(val)
  else
    @date = val
  end
end

#destroyObject



132
133
134
135
136
# File 'lib/zimbra/appointment.rb', line 132

def destroy
  invites.each do |invite|
    AppointmentService.cancel(self, invite.id)
  end
end

#id_with_invite_idObject



154
155
156
# File 'lib/zimbra/appointment.rb', line 154

def id_with_invite_id
  "#{id}-#{invites.first.id}"
end

#invitesObject



89
90
91
92
# File 'lib/zimbra/appointment.rb', line 89

def invites
  reload if loaded_from_search
  @invites
end

#invites_attributes=(attributes) ⇒ Object



94
95
96
97
98
99
# File 'lib/zimbra/appointment.rb', line 94

def invites_attributes=(attributes)
  return @invites = nil unless attributes
  
  attributes = attributes.is_a?(Array) ? attributes : [ attributes ]
  @invites = attributes.collect { |attrs| Zimbra::Appointment::Invite.new(attrs.merge( { :appointment => self } )) }
end

#invites_zimbra_attributes=(attributes) ⇒ Object



101
102
103
104
105
106
# File 'lib/zimbra/appointment.rb', line 101

def invites_zimbra_attributes=(attributes)
  return @invites = nil unless attributes
  
  attributes = attributes.is_a?(Array) ? attributes : [ attributes ]
  @invites = attributes.collect { |attrs| Zimbra::Appointment::Invite.new_from_zimbra_attributes(attrs.merge( { :appointment => self } )) }
end

#last_instance_timeObject



158
159
160
161
162
# File 'lib/zimbra/appointment.rb', line 158

def last_instance_time
  instance_times = Zimbra::AppointmentService.find_all_instance_times_of_an_appointment(self)
  return nil unless instance_times && instance_times.count > 0
  instance_times.max
end

#new_record?Boolean

Returns:



150
151
152
# File 'lib/zimbra/appointment.rb', line 150

def new_record?
  id.nil?
end

#reloadObject



71
72
73
74
75
# File 'lib/zimbra/appointment.rb', line 71

def reload
  raw_attributes = AppointmentService.find(id)
  self.attributes = Zimbra::Appointment.parse_zimbra_attributes(raw_attributes)
  @loaded_from_search = false
end

#repliesObject



77
78
79
80
# File 'lib/zimbra/appointment.rb', line 77

def replies
  reload if loaded_from_search
  @replies
end

#replies=(replies_attributes) ⇒ Object



82
83
84
85
86
87
# File 'lib/zimbra/appointment.rb', line 82

def replies=(replies_attributes)
  return @replies = [] unless replies_attributes
  
  replies_attributes = replies_attributes[:reply].is_a?(Array) ? replies_attributes[:reply] : [ replies_attributes[:reply] ]
  @replies = replies_attributes.collect { |attrs| Zimbra::Appointment::Reply.new_from_zimbra_attributes(attrs[:attributes]) }
end

#saveObject



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/zimbra/appointment.rb', line 138

def save
  if new_record?
    response = Zimbra::AppointmentService.create(self)
    invites.first.id = response[:invite_id]
    @id = response[:id]
  else
    invites.each do |invite|
      Zimbra::AppointmentService.update(self, invite.id)
    end
  end
end