PageRenderTime 102ms CodeModel.GetById 60ms app.highlight 9ms RepoModel.GetById 32ms app.codeStats 0ms

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

http://github.com/AF83/ucengine
Erlang | 111 lines | 48 code | 11 blank | 52 comment | 1 complexity | b79e44d3daf7db5cec766e955673da5d 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_mongodb).
 19
 20-behaviour(gen_uce_meeting).
 21
 22-export([add/2,
 23         delete/2,
 24         get/2,
 25         update/2,
 26         list/1]).
 27
 28-include("uce.hrl").
 29-include("mongodb.hrl").
 30
 31%%--------------------------------------------------------------------
 32%% @spec (Domain::list, #uce_meeting{}) -> {ok, created}
 33%% @doc Insert given record #uce_meeting{} in uce_meeting mongodb table
 34%% @end
 35%%--------------------------------------------------------------------
 36add(Domain, #uce_meeting{} = Meeting) ->
 37    mongodb_helpers:ok(emongo:insert_sync(Domain, "uce_meeting", to_collection(Domain, Meeting))),
 38    {ok, created}.
 39
 40%%--------------------------------------------------------------------
 41%% @spec (Domain::list, Name::list) -> {ok, deleted}
 42%% @doc Delete record
 43%% @end
 44%%--------------------------------------------------------------------
 45delete(Domain, Name) ->
 46    mongodb_helpers:ok(emongo:delete_sync(Domain, "uce_meeting", [{"name", Name}, {"domain", Domain}])),
 47    {ok, deleted}.
 48
 49%%--------------------------------------------------------------------
 50%% @spec (Domain::list, Name::list) -> {ok, #uce_meeting{}} | {error, not_found}
 51%% @doc Get record uce_meeting which correspond to the given name and domain
 52%% @end
 53%%--------------------------------------------------------------------
 54get(Domain, Name) ->
 55    case emongo:find_one(Domain, "uce_meeting",
 56                         [{"name", Name}, {"domain", Domain}]) of
 57        [Record] ->
 58            {ok, from_collection(Record)};
 59        [] ->
 60            throw({error, not_found})
 61    end.
 62
 63%%--------------------------------------------------------------------
 64%% @spec (Domain::list, #uce_meeting{}) -> {ok, updated}
 65%% @doc update #uce_meeting record
 66%% @end
 67%%--------------------------------------------------------------------
 68update(Domain, #uce_meeting{id=Name} = Meeting) ->
 69    mongodb_helpers:updated(emongo:update_sync(Domain, "uce_meeting",
 70                                               [{"name", Name}, {"domain", Domain}],
 71                                               to_collection(Domain, Meeting), false)),
 72    {ok, updated}.
 73
 74%%--------------------------------------------------------------------
 75%% @spec (Domain::list) -> {ok, [#uce_meeting{}, #uce_meeting{}, ..] = Meetings::list} | {error, bad_parameters}
 76%% @doc List all record #uce_meeting for the given domain
 77%% @end
 78%%--------------------------------------------------------------------
 79list(Domain) ->
 80    Collections = emongo:find_all(Domain, "uce_meeting", [{"domain", Domain}]),
 81    {ok, [from_collection(Collection) || Collection <- Collections]}.
 82
 83
 84%%--------------------------------------------------------------------
 85%% @spec (#uce_meeting{}) -> [{Key::list, Value::list}, {Key::list, Value::list}, ...] = Collection::list
 86%% @doc Convert #uce_meeting{} record to valid collection
 87%% @end
 88%%--------------------------------------------------------------------
 89to_collection(Domain, #uce_meeting{id=Name,
 90                                   roster=Roster,
 91                                   metadata=Metadata}) ->
 92    [{"name", Name},
 93     {"domain", Domain},
 94     {"roster", {array, Roster}},
 95     {"metadata", mongodb_helpers:to_bson(Metadata)}].
 96
 97%%--------------------------------------------------------------------
 98%% @spec ([{Key::list, Value::list}, {Key::list, Value::list}, ...] = Collection::list) -> #uce_meeting{} | {error, bad_parameters}
 99%% @doc Convert collection returned by mongodb to valid record #uce_meeting{}
100%% @end
101%%--------------------------------------------------------------------
102from_collection(Collection) ->
103    case utils:get(mongodb_helpers:collection_to_list(Collection),
104                   ["name", "domain", "roster", "metadata"]) of
105        [Name, _Domain, {array, Roster}, Metadata] ->
106            #uce_meeting{id=Name,
107                         roster=Roster,
108                         metadata=Metadata};
109        _ ->
110            throw({error, bad_parameters})
111    end.