summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/common/object.lua
blob: 54821684e22f06ba348794b4e58775be9322c30b (plain)
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
-- Gemeinschaft 5 module: object class
-- (c) AMOOMA GmbH 2013
-- 

module(...,package.seeall)

Object = {}

-- create object object ;)
function Object.new(self, arg)
  arg = arg or {}
  object = arg.object or {}
  setmetatable(object, self);
  self.__index = self;
  self.class = 'object';
  self.log = arg.log;
  self.database = arg.database;
  return object;
end

-- find object
function Object.find(self, attributes)
  if not attributes.class then
    return nil;
  end

  local object = nil;

  require 'common.str';
  local class = common.str.downcase(attributes.class);

  if class == 'user' then
    require 'dialplan.user';
    if tonumber(attributes.id) then
      object = dialplan.user.User:new{ log = self.log, database = self.database }:find_by_id(attributes.id);
    elseif not common.str.blank(attributes.uuid) then
      object = dialplan.user.User:new{ log = self.log, database = self.database }:find_by_uuid(attributes.uuid);
    end

    if object then
      object.user_groups = object:list_groups();
    end
  elseif class == 'tenant' then
    require 'dialplan.tenant';
    if tonumber(attributes.id) then
      object = dialplan.tenant.Tenant:new{ log = self.log, database = self.database }:find_by_id(attributes.id);
    elseif not common.str.blank(attributes.uuid) then
      object = dialplan.tenant.Tenant:new{ log = self.log, database = self.database }:find_by_uuid(attributes.uuid);
    end
  elseif class == 'sipaccount' then
    require 'common.sip_account';
    if not common.str.blank(attributes.auth_name) then
      object = common.sip_account.SipAccount:new{ log = self.log, database = self.database }:find_by_auth_name(attributes.auth_name, attributes.domain);
    elseif tonumber(attributes.id) then
      object = common.sip_account.SipAccount:new{ log = self.log, database = self.database }:find_by_id(attributes.id);
    elseif not common.str.blank(attributes.uuid) then
      object = common.sip_account.SipAccount:new{ log = self.log, database = self.database }:find_by_uuid(attributes.uuid);
    end

    if object then
      object.owner = self:find{class = object.record.sip_accountable_type, id = tonumber(object.record.sip_accountable_id)};
    end
  elseif class == 'huntgroup' then
    require 'dialplan.hunt_group';

    if tonumber(attributes.id) then
      object = dialplan.hunt_group.HuntGroup:new{ log = self.log, database = self.database }:find_by_id(attributes.id);
    elseif not common.str.blank(attributes.uuid) then
      object = dialplan.hunt_group.HuntGroup:new{ log = self.log, database = self.database }:find_by_uuid(attributes.uuid);
    end

    if object then
      object.owner = self:find{class = 'tenant', id = tonumber(object.record.tenant_id)};
    end
  elseif class == 'automaticcalldistributor' then
    require 'dialplan.acd';

    if tonumber(attributes.id) then
      object = dialplan.acd.AutomaticCallDistributor:new{ log = self.log, database = self.database, domain = self.domain }:find_by_id(attributes.id);
    elseif not common.str.blank(attributes.uuid) then
      object = dialplan.acd.AutomaticCallDistributor:new{ log = self.log, database = self.database, domain = self.domain }:find_by_uuid(attributes.uuid);
    end

    if object then
      object.owner = self:find{class = object.record.automatic_call_distributorable_type, id = tonumber(object.record.automatic_call_distributorable_id)};
    end
  elseif class == 'faxaccount' then
    require 'dialplan.fax';
    if tonumber(attributes.id) then
      object = dialplan.fax.Fax:new{ log = self.log, database = self.database }:find_by_id(attributes.id);
    elseif not common.str.blank(attributes.uuid) then
      object = dialplan.fax.Fax:new{ log = self.log, database = self.database }:find_by_uuid(attributes.uuid);
    end

    if object then
      object.owner = self:find{class = object.record.fax_accountable_type, id = tonumber(object.record.fax_accountable_id)};
    end
  elseif class == 'conference' then
    require 'common.conference';

    if tonumber(attributes.id) then
      object = common.conference.Conference:new{ log = self.log, database = self.database }:find_by_id(attributes.id);
    elseif not common.str.blank(attributes.uuid) then
      object = common.conference.Conference:new{ log = self.log, database = self.database }:find_by_uuid(attributes.uuid);
    end

    if object then
      object.owner = self:find{class = object.record.conferenceable_type, id = tonumber(object.record.conferenceable_id)};
    end
  end

  if object then
    require 'common.group';
    object.groups, object.group_ids = common.group.Group:new{ log = self.log, database = self.database }:name_id_by_member(object.id, object.class);
  end

  return object;
end

-- local class from module
function Object.load_one(self, module_path)
  result, module_data = pcall(require, module_path);
  if not result or not module_data then
    return nil, module_data;
  end
  for object_name in pairs(module_data) do
    if tostring(object_name[1]) ~= '_' then
      return module_data[object_name];
    end
  end
end