Leosac  0.8.0
Open Source Access Control
ScheduleSerializer.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
21 #include "tools/JSONUtils.hpp"
24 #include "tools/log.hpp"
25 #include <boost/algorithm/string.hpp>
26 
27 using namespace Leosac;
28 using namespace Leosac::Tools;
29 
31  const SecurityContext &)
32 {
33  int tf_pos = 0;
34  json timeframes = json::array();
35  for (const auto &tf : in.timeframes())
36  {
37  // Note that a SingleTimeFrame can only be enabled on 1 day.
38  std::string start_time =
39  std::to_string(tf.start_hour) + ':' + std::to_string(tf.start_min);
40  std::string end_time =
41  std::to_string(tf.end_hour) + ':' + std::to_string(tf.end_min);
42  timeframes.push_back({{"id", tf_pos},
43  {"start-time", start_time},
44  {"end-time", end_time},
45  {"day", tf.day}});
46  tf_pos++;
47  }
48 
49  json json_mappings = json::array();
50  for (const auto &mapping : in.mapping())
51  {
52  json json_mapping = {{"id", mapping->id()}, {"type", "schedule-mapping"}};
53  json_mappings.push_back(json_mapping);
54  }
55 
56  json serialized = {{"id", in.id()},
57  {"type", "schedule"},
58  {"attributes",
59  {{"version", in.odb_version()},
60  {"name", in.name()},
61  {"description", in.description()},
62  {"timeframes", timeframes}}},
63  {"relationships", {{"mapping", {{"data", json_mappings}}}}}};
64 
65  return serialized;
66 }
67 
71 static std::pair<int, int> parse_time(const std::string &tm)
72 {
73  std::vector<std::string> splitted;
74  boost::algorithm::split(splitted, tm, boost::algorithm::is_any_of(":"),
75  boost::algorithm::token_compress_on);
76  if (splitted.size() != 2)
77  throw LEOSACException(
78  BUILD_STR("Timeframe start or end time is invalid: " << tm));
79 
80  return std::make_pair(std::stoi(splitted[0]), std::stoi(splitted[1]));
81 };
82 
84  const SecurityContext &)
85 {
86  using namespace Leosac::JSONUtil;
87 
88  out.name(extract_with_default(in, "name", out.name()));
89  out.description(extract_with_default(in, "description", out.description()));
90 
91  out.clear_timeframes();
92  for (const auto &tf_json : in.at("timeframes"))
93  {
94  std::pair<int, int> start_time = parse_time(tf_json.at("start-time"));
95  std::pair<int, int> end_time = parse_time(tf_json.at("end-time"));
96 
97  SingleTimeFrame tf(tf_json.at("day"), start_time.first, start_time.second,
98  end_time.first, end_time.second);
99  out.add_timeframe(tf);
100  }
101 }
102 
104  const SecurityContext &sc)
105 {
106  return ScheduleJSONSerializer::serialize(in, sc).dump(4);
107 }
108 
110  const std::string &in,
111  const SecurityContext &sc)
112 {
114 }
Leosac::Tools::ISchedule::timeframes
virtual std::vector< SingleTimeFrame > timeframes() const =0
Retrieves the list of timesframes that compose this schedule.
Leosac::JSONUtil
Add a few useful extraction functions.
Definition: JSONUtils.cpp:27
Leosac::JSONUtil::extract_with_default
std::chrono::system_clock::time_point extract_with_default(const nlohmann::json &obj, const std::string &key, const std::chrono::system_clock::time_point &tp)
Extract an ISO 8601 datetime string from a json object.
Definition: JSONUtils.cpp:45
BUILD_STR
#define BUILD_STR(param)
Internal macro.
Definition: log.hpp:63
Leosac::Tools::ISchedule::clear_timeframes
virtual void clear_timeframes()=0
Remove all the timeframes from this schedule.
Leosac::Tools::ISchedule::id
virtual ScheduleId id() const =0
Leosac::Tools::json
nlohmann::json json
Definition: ScheduleMappingSerializer.hpp:31
Leosac::Tools::ISchedule::name
virtual const std::string & name() const =0
Retrieve the name of the schedule.
Leosac::Tools::ISchedule::odb_version
virtual size_t odb_version() const =0
Leosac::Tools::ScheduleJSONSerializer::serialize
static json serialize(const Tools::ISchedule &in, const SecurityContext &sc)
Definition: ScheduleSerializer.cpp:30
Leosac::Tools::ISchedule::add_timeframe
virtual void add_timeframe(const SingleTimeFrame &tf)=0
Add the given timeframe to this schedule;.
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Tools::ScheduleJSONStringSerializer::serialize
static std::string serialize(const Tools::ISchedule &in, const SecurityContext &sc)
Definition: ScheduleSerializer.cpp:103
ScheduleSerializer.hpp
Leosac::Tools::ScheduleJSONSerializer::unserialize
static void unserialize(Tools::ISchedule &out, const json &in, const SecurityContext &sc)
Definition: ScheduleSerializer.cpp:83
LEOSACException
A base class for Leosac specific exception.
Definition: leosacexception.hpp:40
JSONUtils.hpp
Leosac::Tools::ISchedule::mapping
virtual std::vector< Tools::ScheduleMappingPtr > mapping() const =0
log.hpp
Leosac::Tools::ScheduleJSONStringSerializer::unserialize
static void unserialize(Tools::ISchedule &out, const std::string &in, const SecurityContext &sc)
Definition: ScheduleSerializer.cpp:109
Leosac::Tools::ISchedule
Definition: ISchedule.hpp:38
Leosac::Tools::SingleTimeFrame
This struct abstracts what we call a single time frame.
Definition: SingleTimeFrame.hpp:38
Leosac::Tools::ISchedule::description
virtual const std::string & description() const =0
ScheduleMapping.hpp
Leosac::Tools
Definition: DatabaseLogSink.hpp:27
Leosac::SecurityContext
A SecurityContext is used to query permission while doing an operation.
Definition: SecurityContext.hpp:40
SingleTimeFrame.hpp