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