PageRenderTime 203ms CodeModel.GetById 91ms app.highlight 12ms RepoModel.GetById 98ms app.codeStats 0ms

/ucengine/src/controllers/search_controller.erl

http://github.com/AF83/ucengine
Erlang | 102 lines | 74 code | 11 blank | 17 comment | 0 complexity | 09a2fe57a8b32f9b323e2b424ecbf8e2 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(search_controller).
 19
 20-export([init/0, search/4]).
 21
 22-include("uce.hrl").
 23-include_lib("yaws/include/yaws_api.hrl").
 24
 25init() ->
 26    [#uce_route{method='GET',
 27                path=["search", '_'],
 28                callback={?MODULE, search,
 29                          [{"uid", required, string},
 30                           {"sid", required, string},
 31                           {"searchTerms", "", string},
 32                           {"startIndex", 0, integer},
 33                           {"startPage", 1, integer},
 34                           {"count", 10, integer},
 35                           {"order", "asc", string}]}}].
 36
 37extract_terms(SearchTerms, [Term|Terms], [Default|Defaults]) ->
 38    {ok, Regexp} = re:compile("(^|.* )" ++ Term ++ ":([^ ]+)(.*)"),
 39    case re:run(SearchTerms, Regexp, [{capture, all, list}]) of
 40        {match, [_, Start, Value, End]} ->
 41            [{Term, Value}] ++ extract_terms(Start ++ End, Terms, Defaults);
 42        nomatch ->
 43            [{Term, Default}] ++ extract_terms(SearchTerms, Terms, Defaults);
 44        Error ->
 45            ?ERROR_MSG("search: ~p", [Error]),
 46            throw({error, bad_parameters})
 47    end;
 48extract_terms(SearchTerms, [], []) ->
 49    [{"keywords", string:tokens(SearchTerms, " ")}].
 50
 51search(Domain, [], [Uid, Sid, SearchTerms, StartIndex, StartPage, Count, Order], Arg) ->
 52    {ok, true} = uce_presence:assert(Domain, Uid, Sid),
 53
 54    [{"type", Type},
 55     {"start", DateStart},
 56     {"end", DateEnd},
 57     {"location", Location},
 58     {"from", From},
 59     {"to", _To},
 60     {"parent", Parent},
 61     {"keywords", Keywords}] =
 62        extract_terms(SearchTerms,
 63                      ["type", "start", "end", "location", "from", "to", "parent"],
 64                      ["", "0", infinity, "", "", "", ""]),
 65
 66    {ok, true} = uce_access:assert(Domain, Uid, Location, "event", "list",
 67                                   [{"from", From}]),
 68
 69    DateEndInt = case DateEnd of
 70                     infinity ->
 71                         infinity;
 72                     A when is_list(A) ->
 73                         list_to_integer(A)
 74                 end,
 75
 76    Start = uce_paginate:index(Count, StartIndex, StartPage),
 77    {ok, NumTotal, Events} = uce_event:search(Domain,
 78                                              Location,
 79                                              Keywords,
 80                                              From,
 81                                              string:tokens(Type, ","),
 82                                              Uid,
 83                                              list_to_integer(DateStart),
 84                                              DateEndInt,
 85                                              Parent,
 86                                              Start,
 87                                              Count,
 88                                              list_to_atom(Order)),
 89
 90    {abs_path, Path} = Arg#arg.req#http_request.path,
 91    Link = lists:concat(["http://", Arg#arg.headers#headers.host, Path]),
 92
 93    Entries = json_helpers:to_json(Domain, Events),
 94    Feed = {struct, [{'link', Link},
 95                     {'totalResults', NumTotal},
 96                     {'startIndex', StartIndex},
 97                     {'itemsPerPage', Count},
 98                     {'Query', {struct, [{role, "request"},
 99                                         {searchTerms, SearchTerms},
100                                         {startPage, StartPage}]}},
101                     {'entries', Entries}]},
102    json_helpers:json(Domain, Feed).