PageRenderTime 32ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/AF83/ucengine
Erlang | 126 lines | 93 code | 15 blank | 18 comment | 1 complexity | f56eaa86304f3c8acbe2c2658386c801 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_event_mnesia).
  19. -behaviour(gen_uce_event).
  20. -export([init/0, drop/0]).
  21. -export([add/2,
  22. get/2,
  23. list/8]).
  24. -include("uce.hrl").
  25. init() ->
  26. case mnesia:create_table(uce_event,
  27. [{disc_copies, [node()]},
  28. {type, set},
  29. {attributes, record_info(fields, uce_event)}]) of
  30. {atomic, ok} -> ok;
  31. {aborted, {already_exists, uce_event}} -> ok
  32. end.
  33. add(Domain, #uce_event{id=Id} = Event) ->
  34. case mnesia:dirty_write(Event#uce_event{id={Id, Domain}}) of
  35. ok ->
  36. {ok, Id};
  37. {aborted, _} ->
  38. throw({error, bad_parameters})
  39. end.
  40. get(Domain, Id) ->
  41. case mnesia:dirty_read(uce_event, {Id, Domain}) of
  42. {aborted, _} ->
  43. throw({error, bad_parameters});
  44. [] ->
  45. throw({error, not_found});
  46. [Event] ->
  47. {ok, remove_domain_from_id(Event)}
  48. end.
  49. list(Domain, Location, From, [], Start, End, Parent, Order) ->
  50. list(Domain, Location, From, [""], Start, End, Parent, Order);
  51. list(Domain, Location, From, Types, Start, End, Parent, Order) ->
  52. {SelectId, ResultId} = {{'$1', Domain}, {{'$1', Domain}}},
  53. {SelectLocation, ResultLocation} =
  54. case Location of
  55. "" ->
  56. {'$3', '$3'};
  57. _ ->
  58. {Location, Location}
  59. end,
  60. {SelectFrom, ResultFrom} =
  61. case From of
  62. "" ->
  63. {'$4', '$4'};
  64. _ ->
  65. {From, From}
  66. end,
  67. SelectParent =
  68. case Parent of
  69. "" ->
  70. '$7';
  71. _ ->
  72. Parent
  73. end,
  74. Guard = if
  75. Start /= 0, End /= infinity ->
  76. [{'>=', '$2', Start}, {'=<', '$2', End}];
  77. Start /= 0 ->
  78. [{'>=', '$2', Start}];
  79. End /= infinity ->
  80. [{'=<', '$2', End}];
  81. true ->
  82. []
  83. end,
  84. Events =
  85. lists:map(fun(Type) ->
  86. SelectType = if
  87. Type == "" ->
  88. '$6';
  89. true ->
  90. Type
  91. end,
  92. Match = #uce_event{id=SelectId,
  93. datetime='$2',
  94. location=SelectLocation,
  95. from=SelectFrom,
  96. to='$5',
  97. type=SelectType,
  98. parent=SelectParent,
  99. metadata='$8'},
  100. Result = {{'uce_event', ResultId, '$2', ResultLocation,
  101. ResultFrom, '$5', SelectType, SelectParent, '$8'}},
  102. mnesia:dirty_select(uce_event, [{Match, Guard, [Result]}])
  103. end,
  104. Types),
  105. %% XXX: mnesia should be able to sort events
  106. OrderedEvents = event_helpers:sort(lists:flatten(Events), Order),
  107. {ok, remove_domain_from_id(OrderedEvents)}.
  108. drop() ->
  109. mnesia:clear_table(uce_event).
  110. remove_domain_from_id(Events) ->
  111. ?REMOVE_ID_FROM_RECORD(Events, uce_event).