PageRenderTime 47ms CodeModel.GetById 42ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/models/m_rsc_export.erl

https://code.google.com/p/zotonic/
Erlang | 74 lines | 37 code | 15 blank | 22 comment | 2 complexity | b98ceffd29906e357c08fb8db33b7f83 MD5 | raw file
 1%% @author Arjan Scherpenisse <arjan@scherpenisse.net>
 2%% @copyright 2010 Arjan Scherpenisse
 3%%
 4%% @doc Export function for resources.
 5
 6%% Licensed under the Apache License, Version 2.0 (the "License");
 7%% you may not use this file except in compliance with the License.
 8%% You may obtain a copy of the License at
 9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17
18-module(m_rsc_export).
19-author("Arjan Scherpenisse <arjan@scherpenisse.net>").
20
21
22-export([full/2,
23         simple/2
24         ]).
25
26
27%% @doc Get the full representation of a resource.
28full(Id, Context) when is_integer(Id) ->
29    case m_rsc:exists(Id, Context) of
30        false -> undefined;
31        true ->
32
33            Rsc = m_rsc:get_raw(Id, Context),
34            %% This should probably be encapsulated in m_edges.
35            Edges0 = z_db:assoc("
36                                select e.predicate_id, p.name as predicate_name, e.object_id, e.seq
37                                from edge e join rsc p on p.id = e.predicate_id
38                                where e.subject_id = $1
39                                order by e.predicate_id, e.seq, e.id", [Id], Context),
40            Edges = [edge_details(E, Context) || E <- Edges0],
41
42            {ok, Category} = z_db:select(category, Id, Context),
43            {ok, Medium} = z_db:select(medium, Id, Context),
44
45            Export = [
46                      %% Essential fields
47                      {id, Id},
48                      {uri, m_rsc:p(Id, uri, Context)},
49                      %% Parts
50                      {rsc, Rsc},
51                      {medium, Medium},
52                      {category, Category},
53                      {edges, Edges}
54                     ],
55
56            %% Filter empty lists
57            lists:filter(fun({_,L}) -> not(L == []) end, Export)
58    end;
59
60full(Id, Context) ->
61    full(m_rsc:rid(Id, Context), Context).
62
63
64%% @doc Given an edge record, add the resource uris for the object and the predicate.
65edge_details(Edge, Context) ->
66    Edge ++ [{predicate_uri, m_rsc:p(proplists:get_value(predicate_id, Edge), uri, Context)},
67             {predicate_title, m_rsc:p(proplists:get_value(predicate_id, Edge), title, Context)},
68             {object_uri, m_rsc:p(proplists:get_value(object_id, Edge), uri, Context)},
69             {object_title, m_rsc:p(proplists:get_value(object_id, Edge), title, Context)}].
70
71
72%% Simple export with limited information (e.g. for atom feeds)
73simple(_, _) ->
74    throw({error, {not_implemented}}).