PageRenderTime 77ms CodeModel.GetById 20ms app.highlight 37ms RepoModel.GetById 13ms app.codeStats 0ms

/ucengine/src/backends/db/mnesia/uce_meeting_mnesia.erl

http://github.com/AF83/ucengine
Erlang | 93 lines | 64 code | 12 blank | 17 comment | 0 complexity | 0590708eb119669a70304ce63b32ccad MD5 | raw file
 1%%
 2%%  U.C.Engine - Unified Collaboration Engine
 3%%  Copyright (C) 2011 af83
 4%%
 5%%  This program is free software: you can redistribute it and/or modify
 6%%  it under the terms of the GNU Affero General Public License as published by
 7%%  the Free Software Foundation, either version 3 of the License, or
 8%%  (at your option) any later version.
 9%%
10%%  This program is distributed in the hope that it will be useful,
11%%  but WITHOUT ANY WARRANTY; without even the implied warranty of
12%%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13%%  GNU Affero General Public License for more details.
14%%
15%%  You should have received a copy of the GNU Affero General Public License
16%%  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17%%
18-module(uce_meeting_mnesia).
19
20-behaviour(gen_uce_meeting).
21
22-export([init/0, drop/0]).
23
24-export([add/2,
25         delete/2,
26         get/2,
27         update/2,
28         list/1]).
29
30-include("uce.hrl").
31
32init() ->
33    case mnesia:create_table(uce_meeting,
34                             [{disc_copies, [node()]},
35                              {type, set},
36                              {attributes, record_info(fields, uce_meeting)}]) of
37        {atomic, ok} -> ok;
38        {aborted, {already_exists, uce_meeting}} -> ok
39    end.
40
41add(Domain, #uce_meeting{id=Id} = Meeting) ->
42    case mnesia:dirty_write(Meeting#uce_meeting{id={Id, Domain}}) of
43        {aborted, _} ->
44            throw({error, bad_parameters});
45        ok ->
46            {ok, created}
47    end.
48
49delete(Domain, Id) ->
50    case mnesia:transaction(fun() ->
51                                    mnesia:delete({uce_meeting, {Id, Domain}})
52                            end) of
53        {aborted, _} ->
54            throw({error, bad_parameters});
55        {atomic, ok} ->
56            {ok, deleted}
57    end.
58
59get(Domain, Id) ->
60    case mnesia:dirty_read(uce_meeting, {Id, Domain}) of
61        [Meeting] ->
62            {ok, remove_domain_from_id(Meeting)};
63        [] ->
64            throw({error, not_found});
65        {aborted, _} ->
66            throw({error, bad_parameters})
67    end.
68
69update(Domain, #uce_meeting{id=Id} = Meeting) ->
70    case mnesia:transaction(fun() ->
71                                    mnesia:write(Meeting#uce_meeting{id={Id, Domain}})
72                            end) of
73        {aborted, _} ->
74            throw({error, bad_parameters});
75        {atomic, _} ->
76            {ok, updated}
77    end.
78
79list(Domain) ->
80    case mnesia:dirty_match_object(#uce_meeting{id={'_', Domain},
81                                                roster='_',
82                                                metadata='_'}) of
83        {aborted, _} ->
84            throw({error, bad_parameters});
85        Meetings ->
86            {ok, remove_domain_from_id(Meetings)}
87    end.
88
89drop() ->
90    mnesia:clear_table(uce_meeting).
91
92remove_domain_from_id(Meetings) ->
93    ?REMOVE_ID_FROM_RECORD(Meetings, uce_meeting).