PageRenderTime 38ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/ucengine/src/backends/db/mongodb/uce_event_mongodb.erl

http://github.com/AF83/ucengine
Erlang | 156 lines | 110 code | 9 blank | 37 comment | 7 complexity | 8fb1f1537a3906c6e4f52b695583d392 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_mongodb).
  19. -behaviour(gen_uce_event).
  20. -export([add/2,
  21. get/2,
  22. list/8,
  23. index/1]).
  24. -include("uce.hrl").
  25. -include("mongodb.hrl").
  26. %%--------------------------------------------------------------------
  27. %% @spec (Domain::list, #uce_event{}) -> {ok, Id}
  28. %% @doc Insert given record #uce_event{} in uce_event mongodb table
  29. %% @end
  30. %%--------------------------------------------------------------------
  31. add(Domain, #uce_event{id=Id} = Event) ->
  32. mongodb_helpers:ok(emongo:insert_sync(Domain, "uce_event", to_collection(Domain, Event))),
  33. {ok, Id}.
  34. %%--------------------------------------------------------------------
  35. %% @spec (Domain::list, EventId::list) -> {ok, #uce_event{}} | {error, bad_parameters}
  36. %% @doc Retrieve record #uce_event{} for the given id and domain
  37. %% @end
  38. %%--------------------------------------------------------------------
  39. get(Domain, EventId) ->
  40. case emongo:find_one(Domain, "uce_event", [{"id", EventId}, {"domain", Domain}]) of
  41. [Collection] ->
  42. {ok, from_collection(Collection)};
  43. [] ->
  44. throw({error, not_found})
  45. end.
  46. %%--------------------------------------------------------------------
  47. %% @spec (Domain::list, location::list, FromId::list, Type::list, Start::(integer|atom), End::({integer|atom), Parent::list, Order::atom) -> {ok, [#uce_event{}, #uce_event{}, ...] = Events::list}
  48. %% @doc Returns list of record #uce_event which are match with given keys
  49. %% @end
  50. %%--------------------------------------------------------------------
  51. list(Domain, Location, From, Type, Start, End, Parent, Order) ->
  52. SelectLocation = case Location of
  53. "" ->
  54. [];
  55. Meeting ->
  56. [{"meeting", Meeting}]
  57. end,
  58. SelectFrom = case From of
  59. "" ->
  60. [];
  61. Uid ->
  62. [{"from", Uid}]
  63. end,
  64. SelectTypes = if
  65. Type == [] ->
  66. [];
  67. true ->
  68. [{"type", [{in, Type}]}]
  69. end,
  70. SelectParent = if
  71. Parent == "" ->
  72. [];
  73. true ->
  74. [{"parent", Parent}]
  75. end,
  76. SelectTime = if
  77. Start == 0, End == infinity ->
  78. [];
  79. Start /= 0, End == infinity ->
  80. [{"datetime", [{'>=', Start}]}];
  81. Start /= 0, End /= infinity ->
  82. [{"datetime", [{'>=', Start},
  83. {'=<', End}]}];
  84. Start == 0, End /= infinity ->
  85. [{"datetime", [{'=<', End}]}];
  86. true ->
  87. []
  88. end,
  89. Events = lists:map(fun(Collection) ->
  90. from_collection(Collection)
  91. end,
  92. emongo:find_all(Domain, "uce_event",
  93. SelectLocation ++
  94. SelectFrom ++
  95. SelectTypes ++
  96. SelectParent ++
  97. SelectTime,
  98. [{orderby, [{"datetime", Order}]}])),
  99. {ok, Events}.
  100. %%--------------------------------------------------------------------
  101. %% @spec ([{Key::list, Value::list}, {Key::list, Value::list}, ...] = Collection::list) -> #uce_user{} | {error, bad_parameters}
  102. %% @doc Convert collection returned by mongodb to valid record #uce_event{}
  103. %% @end
  104. %%--------------------------------------------------------------------
  105. from_collection(Collection) ->
  106. case utils:get(mongodb_helpers:collection_to_list(Collection),
  107. ["id", "domain", "meeting", "from", "metadata", "datetime", "type", "parent", "to"]) of
  108. [Id, _Domain, Meeting, From, Metadata, Datetime, Type, Parent, To] ->
  109. #uce_event{id=Id,
  110. datetime=Datetime,
  111. from=From,
  112. to=To,
  113. location=Meeting,
  114. type=Type,
  115. parent=Parent,
  116. metadata=Metadata};
  117. _ ->
  118. throw({error, bad_parameters})
  119. end.
  120. -spec to_collection(domain(), #uce_event{}) -> list({string(), any()}).
  121. to_collection(Domain, #uce_event{id=Id,
  122. location=Meeting,
  123. from=From,
  124. to=To,
  125. metadata=Metadata,
  126. datetime=Datetime,
  127. type=Type,
  128. parent=Parent}) ->
  129. [{"domain", Domain},
  130. {"id", Id},
  131. {"meeting", Meeting},
  132. {"from", From},
  133. {"to", To},
  134. {"metadata", mongodb_helpers:to_bson(Metadata)},
  135. {"datetime", Datetime},
  136. {"type", Type},
  137. {"parent", Parent}].
  138. -spec index(domain()) -> ok.
  139. index(Domain) ->
  140. Indexes = [{"domain", 1},
  141. {"meeting", 1},
  142. {"from", 1},
  143. {"parent", 1},
  144. {"datetime", 1},
  145. {"type", 1}],
  146. [emongo:ensure_index(Domain, "uce_event", [Index]) || Index <- Indexes],
  147. ok.