/ucengine/src/backends/db/mongodb/uce_file_mongodb.erl

http://github.com/AF83/ucengine · Erlang · 121 lines · 59 code · 10 blank · 52 comment · 2 complexity · 969732c0303c3742f0eb378fca2e00f6 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_file_mongodb).
  19. -behaviour(gen_uce_file).
  20. -export([add/2,
  21. list/3,
  22. all/1,
  23. get/2,
  24. delete/2]).
  25. -include("uce.hrl").
  26. -include("mongodb.hrl").
  27. %%--------------------------------------------------------------------
  28. %% @spec (#uce_file{}) -> {ok, Id}
  29. %% @doc Insert given record #uce_file{} in uce_file mongodb table
  30. %% @end
  31. %%--------------------------------------------------------------------
  32. add(Domain, #uce_file{} = File) ->
  33. emongo:insert_sync(Domain, "uce_file", to_collection(Domain, File)),
  34. {ok, File#uce_file.id}.
  35. %%--------------------------------------------------------------------
  36. %% @spec (Domain, Location::list) -> {ok, [#uce_file{}, #uce_file{}, ..] = Files::list} | {error, bad_parameters}
  37. %% @doc List all record #uce_file for the given pair location(meeting) and domain
  38. %% @end
  39. %%--------------------------------------------------------------------
  40. list(Domain, Location, Order) ->
  41. Files = emongo:find_all(Domain, "uce_file", [{"location", Location},
  42. {"domain", Domain}],
  43. [{orderby, [{"datetime", Order}]}]),
  44. {ok, [from_collection(File) || File <- Files]}.
  45. %%--------------------------------------------------------------------
  46. %% @spec ({Location::list, Domain::list}) -> {ok, [#uce_file{}, #uce_file{}, ..] = Files::list} | {error, bad_parameters}
  47. %% @doc List all record #uce_file for the given domain
  48. %% @end
  49. %%--------------------------------------------------------------------
  50. all(Domain) ->
  51. Files = emongo:find_all(Domain, "uce_file", [{"domain", Domain}]),
  52. {ok, [from_collection(File) || File <- Files]}.
  53. %%--------------------------------------------------------------------
  54. %% @spec (Domain::list, FileId::list) -> {ok, #uce_file{}} | {error, bad_parameters} | {error, not_found}
  55. %% @doc Get #uce_file record for the given id
  56. %% @end
  57. %%--------------------------------------------------------------------
  58. get(Domain, FileId) ->
  59. case emongo:find_one(Domain, "uce_file", [{"id", FileId}]) of
  60. [File] ->
  61. {ok, from_collection(File)};
  62. [] ->
  63. throw({error, not_found})
  64. end.
  65. %%--------------------------------------------------------------------
  66. %% @spec (Domain::list, FileId::list) -> {ok, deleted}
  67. %% @doc Delete #uce_file record for the given id
  68. %% @end
  69. %%--------------------------------------------------------------------
  70. delete(Domain, FileId) ->
  71. mongodb_helpers:ok(emongo:delete_sync(Domain, "uce_file", [{"id", FileId}])),
  72. {ok, deleted}.
  73. %%--------------------------------------------------------------------
  74. %% @spec ([{Key::list, Value::list}, {Key::list, Value::list}, ...] = Collection::list) -> #uce_file{} | {error, bad_parameters}
  75. %% @doc Convert collection returned by mongodb to valid record #uce_file{}
  76. %% @end
  77. %%--------------------------------------------------------------------
  78. from_collection(Collection) ->
  79. case utils:get(mongodb_helpers:collection_to_list(Collection),
  80. ["id", "domain", "location", "name", "datetime", "mime", "uri", "metadata"]) of
  81. [Id, _Domain, Location, Name, Datetime, Mime, Uri, Metadata] ->
  82. #uce_file{id=Id,
  83. name=Name,
  84. location=Location,
  85. uri=Uri,
  86. datetime=Datetime,
  87. mime=Mime,
  88. metadata=Metadata};
  89. _ ->
  90. throw({error, bad_parameters})
  91. end.
  92. %%--------------------------------------------------------------------
  93. %% @spec (#uce_file{}) -> [{Key::list, Value::list}, {Key::list, Value::list}, ...] = Collection::list
  94. %% @doc Convert #uce_file{} record to valid collection
  95. %% @end
  96. %%--------------------------------------------------------------------
  97. to_collection(Domain, #uce_file{id=Id,
  98. name=Name,
  99. location=Location,
  100. uri=Uri,
  101. datetime=Datetime,
  102. mime=Mime,
  103. metadata=Metadata}) ->
  104. [{"id", Id},
  105. {"domain", Domain},
  106. {"location", Location},
  107. {"name", Name},
  108. {"datetime", Datetime},
  109. {"mime", Mime},
  110. {"uri", Uri},
  111. {"metadata", mongodb_helpers:to_bson(Metadata)}].