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