/ucengine/src/backends/db/mnesia/uce_file_mnesia.erl
Erlang | 108 lines | 78 code | 13 blank | 17 comment | 3 complexity | dd09f6f2e8198c51ca90eddce9821432 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 19-module(uce_file_mnesia). 20 21-include_lib("stdlib/include/qlc.hrl"). 22-include("uce.hrl"). 23 24-behaviour(gen_uce_file). 25 26-export([init/0, drop/0]). 27 28-export([add/2, list/3, get/2, all/1, delete/2]). 29 30init() -> 31 case mnesia:create_table(uce_file, 32 [{disc_copies, [node()]}, 33 {type, set}, 34 {attributes, record_info(fields, uce_file)}]) of 35 {atomic, ok} -> ok; 36 {aborted, {already_exists, uce_file}} -> ok 37 end. 38 39add(Domain, #uce_file{id=Id} = File) -> 40 case mnesia:dirty_write(File#uce_file{id={Id, Domain}}) of 41 ok -> 42 {ok, Id}; 43 {aborted, _} -> 44 throw({error, bad_parameters}) 45 end. 46 47list(Domain, Meeting, Order) -> 48 FunOrder = case Order of 49 asc -> 50 fun(A, B) -> 51 B#uce_file.datetime > A#uce_file.datetime 52 end; 53 desc -> 54 fun(A, B) -> 55 B#uce_file.datetime < A#uce_file.datetime 56 end 57 end, 58 Transaction = fun() -> 59 Query = qlc:q([File || File <- mnesia:table(uce_file), File#uce_file.location == Meeting, element(2, File#uce_file.id) == Domain]), 60 qlc:eval(qlc:sort(Query, [{order, FunOrder}])) 61 end, 62 case mnesia:transaction(Transaction) of 63 {atomic, Files} -> 64 {ok, remove_domain_from_id(Files)}; 65 {aborded, Error} -> 66 ?ERROR_MSG("ERROR ~p~n", [Error]), 67 throw({error, Error}) 68 end. 69 70all(Domain) -> 71 case mnesia:dirty_match_object(#uce_file{id={'_', Domain}, 72 name='_', 73 location='_', 74 uri='_', 75 datetime='_', 76 mime='_', 77 metadata='_'}) of 78 Files when is_list(Files) -> 79 {ok, remove_domain_from_id(Files)}; 80 {aborted, _} -> 81 throw({error, bad_parameters}) 82 end. 83 84get(Domain, Id) -> 85 case mnesia:dirty_read(uce_file, {Id, Domain}) of 86 [File] -> 87 {ok, remove_domain_from_id(File)}; 88 [] -> 89 throw({error, not_found}); 90 {aborted, _} -> 91 throw({error, bad_parameters}) 92 end. 93 94delete(Domain, Id) -> 95 case mnesia:transaction(fun() -> 96 mnesia:delete({uce_file, {Id, Domain}}) 97 end) of 98 {atomic, ok} -> 99 {ok, deleted}; 100 {aborted, _} -> 101 throw({error, bad_paramaters}) 102 end. 103 104drop() -> 105 mnesia:clear_table(uce_file). 106 107remove_domain_from_id(Files) -> 108 ?REMOVE_ID_FROM_RECORD(Files, uce_file).