/ucengine/src/backends/search/erlang/uce_event_erlang_search.erl
http://github.com/AF83/ucengine · Erlang · 67 lines · 41 code · 8 blank · 18 comment · 0 complexity · 4cc85ff4e98ce27d4dfeda829d02faa6 MD5 · raw file
- %%
- %% U.C.Engine - Unified Collaboration Engine
- %% Copyright (C) 2011 af83
- %%
- %% This program is free software: you can redistribute it and/or modify
- %% it under the terms of the GNU Affero General Public License as published by
- %% the Free Software Foundation, either version 3 of the License, or
- %% (at your option) any later version.
- %%
- %% This program is distributed in the hope that it will be useful,
- %% but WITHOUT ANY WARRANTY; without even the implied warranty of
- %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- %% GNU Affero General Public License for more details.
- %%
- %% You should have received a copy of the GNU Affero General Public License
- %% along with this program. If not, see <http://www.gnu.org/licenses/>.
- %%
- -module(uce_event_erlang_search).
- -export([add/2, list/11, search_metadata/2]).
- -include("uce.hrl").
- %% It is not needed to index something here
- add(_, _) ->
- {ok, created}.
- search_value(_, []) ->
- true;
- search_value(Value, [Word|Words]) ->
- case string:str(Value, Word) of
- 0 ->
- false;
- _ ->
- search_value(Value, Words)
- end.
- search_metadata([], _) ->
- false;
- search_metadata({struct, Values}, Words) ->
- search_metadata(Values, Words);
- search_metadata({array, Values}, Words) ->
- search_metadata(Values, Words);
- search_metadata([{_, Value}|Tail], Words) when is_list(Value) ->
- case search_value(Value, Words) of
- true ->
- true;
- false ->
- search_metadata(Tail, Words)
- end;
- search_metadata(_, _Words) ->
- false.
- filter(Events, []) ->
- Events;
- filter(Events, Words) ->
- lists:filter(fun(#uce_event{metadata=Metadata}) ->
- search_metadata(Metadata, Words)
- end,
- Events).
- list(Domain, Location, Search, From, Type, DateStart, DateEnd, Parent, Start, Max, Order) ->
- {ok, Events} = (db:get(uce_event, Domain)):list(Domain, Location, From, Type, DateStart, DateEnd, Parent, Order),
- FilteredEvents = filter(Events, Search),
- EventPage = uce_paginate:paginate(FilteredEvents, Start, Max),
- {ok, length(FilteredEvents), EventPage}.