PageRenderTime 162ms CodeModel.GetById 60ms app.highlight 10ms RepoModel.GetById 70ms app.codeStats 21ms

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

http://github.com/AF83/ucengine
Erlang | 121 lines | 59 code | 10 blank | 52 comment | 2 complexity | 969732c0303c3742f0eb378fca2e00f6 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_file_mongodb).
 19
 20-behaviour(gen_uce_file).
 21
 22-export([add/2,
 23         list/3,
 24         all/1,
 25         get/2,
 26         delete/2]).
 27
 28-include("uce.hrl").
 29-include("mongodb.hrl").
 30
 31%%--------------------------------------------------------------------
 32%% @spec (#uce_file{}) -> {ok, Id}
 33%% @doc Insert given record #uce_file{} in uce_file mongodb table
 34%% @end
 35%%--------------------------------------------------------------------
 36add(Domain, #uce_file{} = File) ->
 37    emongo:insert_sync(Domain, "uce_file", to_collection(Domain, File)),
 38    {ok, File#uce_file.id}.
 39
 40%%--------------------------------------------------------------------
 41%% @spec (Domain, Location::list) -> {ok, [#uce_file{}, #uce_file{}, ..] = Files::list} | {error, bad_parameters}
 42%% @doc List all record #uce_file for the given pair location(meeting) and domain
 43%% @end
 44%%--------------------------------------------------------------------
 45list(Domain, Location, Order) ->
 46    Files = emongo:find_all(Domain, "uce_file", [{"location", Location},
 47                                                 {"domain", Domain}],
 48                            [{orderby, [{"datetime", Order}]}]),
 49    {ok, [from_collection(File) || File <- Files]}.
 50
 51%%--------------------------------------------------------------------
 52%% @spec ({Location::list, Domain::list}) -> {ok, [#uce_file{}, #uce_file{}, ..] = Files::list} | {error, bad_parameters}
 53%% @doc List all record #uce_file for the given domain
 54%% @end
 55%%--------------------------------------------------------------------
 56all(Domain) ->
 57    Files = emongo:find_all(Domain, "uce_file", [{"domain", Domain}]),
 58    {ok, [from_collection(File) || File <- Files]}.
 59
 60%%--------------------------------------------------------------------
 61%% @spec (Domain::list, FileId::list) -> {ok, #uce_file{}} | {error, bad_parameters} | {error, not_found}
 62%% @doc Get #uce_file record for the given id
 63%% @end
 64%%--------------------------------------------------------------------
 65get(Domain, FileId) ->
 66    case emongo:find_one(Domain, "uce_file", [{"id", FileId}]) of
 67        [File] ->
 68            {ok, from_collection(File)};
 69        [] ->
 70            throw({error, not_found})
 71    end.
 72
 73%%--------------------------------------------------------------------
 74%% @spec (Domain::list, FileId::list) -> {ok, deleted}
 75%% @doc Delete #uce_file record for the given id
 76%% @end
 77%%--------------------------------------------------------------------
 78delete(Domain, FileId) ->
 79    mongodb_helpers:ok(emongo:delete_sync(Domain, "uce_file", [{"id", FileId}])),
 80    {ok, deleted}.
 81
 82%%--------------------------------------------------------------------
 83%% @spec ([{Key::list, Value::list}, {Key::list, Value::list}, ...] = Collection::list) -> #uce_file{} | {error, bad_parameters}
 84%% @doc Convert collection returned by mongodb to valid record #uce_file{}
 85%% @end
 86%%--------------------------------------------------------------------
 87from_collection(Collection) ->
 88    case utils:get(mongodb_helpers:collection_to_list(Collection),
 89                   ["id", "domain", "location", "name", "datetime", "mime", "uri", "metadata"]) of
 90        [Id, _Domain, Location, Name, Datetime, Mime, Uri, Metadata] ->
 91            #uce_file{id=Id,
 92                      name=Name,
 93                      location=Location,
 94                      uri=Uri,
 95                      datetime=Datetime,
 96                      mime=Mime,
 97                      metadata=Metadata};
 98        _ ->
 99            throw({error, bad_parameters})
100    end.
101
102%%--------------------------------------------------------------------
103%% @spec (#uce_file{}) -> [{Key::list, Value::list}, {Key::list, Value::list}, ...] = Collection::list
104%% @doc Convert #uce_file{} record to valid collection
105%% @end
106%%--------------------------------------------------------------------
107to_collection(Domain, #uce_file{id=Id,
108                                name=Name,
109                                location=Location,
110                                uri=Uri,
111                                datetime=Datetime,
112                                mime=Mime,
113                                metadata=Metadata}) ->
114    [{"id", Id},
115     {"domain", Domain},
116     {"location", Location},
117     {"name", Name},
118     {"datetime", Datetime},
119     {"mime", Mime},
120     {"uri", Uri},
121     {"metadata", mongodb_helpers:to_bson(Metadata)}].