PageRenderTime 39ms CodeModel.GetById 31ms RepoModel.GetById 0ms 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. -behaviour(gen_uce_role).
  20. -export([init/0, drop/0]).
  21. -export([add/2,
  22. delete/2,
  23. update/2,
  24. get/2]).
  25. -include("uce.hrl").
  26. init() ->
  27. case mnesia:create_table(uce_role,
  28. [{disc_copies, [node()]},
  29. {type, set},
  30. {attributes, record_info(fields, uce_role)}]) of
  31. {atomic, ok} -> ok;
  32. {aborted, {already_exists, uce_role}} -> ok
  33. end.
  34. add(Domain, #uce_role{id=Id}=Role) ->
  35. case mnesia:dirty_write(Role#uce_role{id={Id, Domain}}) of
  36. ok ->
  37. {ok, created};
  38. {aborted, Reason} ->
  39. {error, Reason}
  40. end.
  41. update(Domain, #uce_role{id=Id}=Role) ->
  42. case mnesia:transaction(fun() ->
  43. mnesia:write(Role#uce_role{id={Id, Domain}})
  44. end) of
  45. {atomic, _} ->
  46. {ok, updated};
  47. {aborted, Reason} ->
  48. {error, Reason}
  49. end.
  50. delete(Domain, Id) ->
  51. case mnesia:transaction(fun() ->
  52. mnesia:delete({uce_role, {Id, Domain}})
  53. end) of
  54. {atomic, _} ->
  55. {ok, deleted};
  56. {aborted, _} ->
  57. throw({error, bad_parameters})
  58. end.
  59. get(Domain, Id) ->
  60. case mnesia:dirty_read(uce_role, {Id, Domain}) of
  61. [#uce_role{id={Id, Domain}} = Record] ->
  62. {ok, Record#uce_role{id=Id}};
  63. [] ->
  64. throw({error, not_found});
  65. {aborted, _} ->
  66. throw({error, bad_parameters})
  67. end.
  68. drop() ->
  69. mnesia:clear_table(uce_role).