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