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