/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

  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_erlang_search).
  19. -export([add/2, list/11, search_metadata/2]).
  20. -include("uce.hrl").
  21. %% It is not needed to index something here
  22. add(_, _) ->
  23. {ok, created}.
  24. search_value(_, []) ->
  25. true;
  26. search_value(Value, [Word|Words]) ->
  27. case string:str(Value, Word) of
  28. 0 ->
  29. false;
  30. _ ->
  31. search_value(Value, Words)
  32. end.
  33. search_metadata([], _) ->
  34. false;
  35. search_metadata({struct, Values}, Words) ->
  36. search_metadata(Values, Words);
  37. search_metadata({array, Values}, Words) ->
  38. search_metadata(Values, Words);
  39. search_metadata([{_, Value}|Tail], Words) when is_list(Value) ->
  40. case search_value(Value, Words) of
  41. true ->
  42. true;
  43. false ->
  44. search_metadata(Tail, Words)
  45. end;
  46. search_metadata(_, _Words) ->
  47. false.
  48. filter(Events, []) ->
  49. Events;
  50. filter(Events, Words) ->
  51. lists:filter(fun(#uce_event{metadata=Metadata}) ->
  52. search_metadata(Metadata, Words)
  53. end,
  54. Events).
  55. list(Domain, Location, Search, From, Type, DateStart, DateEnd, Parent, Start, Max, Order) ->
  56. {ok, Events} = (db:get(uce_event, Domain)):list(Domain, Location, From, Type, DateStart, DateEnd, Parent, Order),
  57. FilteredEvents = filter(Events, Search),
  58. EventPage = uce_paginate:paginate(FilteredEvents, Start, Max),
  59. {ok, length(FilteredEvents), EventPage}.