PageRenderTime 161ms CodeModel.GetById 60ms app.highlight 8ms RepoModel.GetById 53ms app.codeStats 0ms

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

http://github.com/AF83/ucengine
Erlang | 79 lines | 52 code | 10 blank | 17 comment | 0 complexity | 6b63ee946fcf8966173a3305ea7a2ba4 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_role_mnesia).
19
20-behaviour(gen_uce_role).
21
22-export([init/0, drop/0]).
23
24-export([add/2,
25         delete/2,
26         update/2,
27         get/2]).
28
29-include("uce.hrl").
30
31init() ->
32    case mnesia:create_table(uce_role,
33                             [{disc_copies, [node()]},
34                              {type, set},
35                              {attributes, record_info(fields, uce_role)}]) of
36        {atomic, ok} -> ok;
37        {aborted, {already_exists, uce_role}} -> ok
38    end.
39
40add(Domain, #uce_role{id=Id}=Role) ->
41    case mnesia:dirty_write(Role#uce_role{id={Id, Domain}}) of
42        ok ->
43            {ok, created};
44        {aborted, Reason} ->
45            {error, Reason}
46    end.
47
48update(Domain, #uce_role{id=Id}=Role) ->
49    case mnesia:transaction(fun() ->
50                                    mnesia:write(Role#uce_role{id={Id, Domain}})
51                            end) of
52        {atomic, _} ->
53            {ok, updated};
54        {aborted, Reason} ->
55            {error, Reason}
56    end.
57
58delete(Domain, Id) ->
59    case mnesia:transaction(fun() ->
60                                    mnesia:delete({uce_role, {Id, Domain}})
61                            end) of
62        {atomic, _} ->
63            {ok, deleted};
64        {aborted, _} ->
65            throw({error, bad_parameters})
66    end.
67
68get(Domain, Id) ->
69    case mnesia:dirty_read(uce_role, {Id, Domain}) of
70        [#uce_role{id={Id, Domain}} = Record] ->
71            {ok, Record#uce_role{id=Id}};
72        [] ->
73            throw({error, not_found});
74        {aborted, _} ->
75            throw({error, bad_parameters})
76    end.
77
78drop() ->
79    mnesia:clear_table(uce_role).