Leosac  0.7.0
OpenSourceAccessControl
Schedule.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 
20 #include "tools/Schedule.hpp"
21 #include "AssertCast.hpp"
23 #include "tools/log.hpp"
24 
25 using namespace Leosac::Tools;
26 
27 Schedule::Schedule(const std::string &sched_name)
28  : name_(sched_name)
29  , odb_version_(0)
30 {
31 }
32 
33 bool Schedule::is_in_schedule(const std::chrono::system_clock::time_point &tp) const
34 {
35  for (const auto &tf : timeframes_)
36  {
37  if (tf.is_in_timeframe(tp))
38  return true;
39  }
40  return false;
41 }
42 
44 {
45  timeframes_.push_back(tf);
46 }
47 
48 const std::string &Schedule::name() const
49 {
50  return name_;
51 }
52 
53 const std::string &Schedule::description() const
54 {
55  return description_;
56 }
57 
59 {
60  return id_;
61 }
62 
63 std::vector<SingleTimeFrame> Schedule::timeframes() const
64 {
65  return timeframes_;
66 }
67 
68 size_t Schedule::odb_version() const
69 {
70  return odb_version_;
71 }
72 
73 void Schedule::name(const std::string &name)
74 {
76  name_ = name;
77 }
78 
79 void Schedule::description(const std::string &desc)
80 {
81  description_ = desc;
82 }
83 
85 {
86  timeframes_.clear();
87 }
88 
90 {
91  ASSERT_LOG(map, "Cannot add a null mapping.");
92  map->schedule_ = assert_cast<SchedulePtr>(shared_from_this());
93  mapping_.push_back(map);
94 }
95 
97 {
98  mapping_.clear();
99 }
100 
101 std::vector<ScheduleMappingPtr> Schedule::mapping() const
102 {
103  return mapping_;
104 }
105 
107 {
108  validate_name(sched.name());
109 }
110 
111 void ScheduleValidator::validate_name(const std::string &name)
112 {
113  if (name.size() < 3 || name.size() > 50)
114  {
115  throw ModelException("data/attributes/name", "Length must be >=3 and <=50.");
116  }
117  for (const auto &c : name)
118  {
119  if (!isalnum(c) && (c != '_' && c != '-' && c != '.'))
120  {
121  throw ModelException(
122  "data/attributes/name",
123  BUILD_STR("Usage of unauthorized character: " << c));
124  }
125  }
126 }
An exception class for general API error.
virtual const std::string & name() const =0
Retrieve the name of the schedule.
bool is_in_schedule(const std::chrono::system_clock::time_point &tp) const override
Check whether or not the given time point can be found in the schedule.
Definition: Schedule.cpp:33
This struct abstracts what we call a single time frame.
std::vector< Tools::ScheduleMappingPtr > mapping_
Definition: Schedule.hpp:91
std::shared_ptr< ScheduleMapping > ScheduleMappingPtr
Definition: ToolsFwd.hpp:41
std::vector< ScheduleMappingPtr > mapping() const override
Definition: Schedule.cpp:101
void add_timeframe(const SingleTimeFrame &tf) override
Add the given timeframe to this schedule;.
Definition: Schedule.cpp:43
void clear_timeframes() override
Remove all the timeframes from this schedule.
Definition: Schedule.cpp:84
void clear_mapping() override
Definition: Schedule.cpp:96
unsigned long ScheduleId
Definition: ToolsFwd.hpp:33
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:221
std::vector< SingleTimeFrame > timeframes_
Definition: Schedule.hpp:83
Schedule(const std::string &sched_name="")
Definition: Schedule.cpp:27
std::shared_ptr< Schedule > SchedulePtr
Definition: ToolsFwd.hpp:32
const std::string & name() const override
Retrieve the name of the schedule.
Definition: Schedule.cpp:48
static void validate_name(const std::string &name)
Definition: Schedule.cpp:111
static void validate(const ISchedule &sched)
Definition: Schedule.cpp:106
std::vector< SingleTimeFrame > timeframes() const override
Retrieves the list of timesframes that compose this schedule.
Definition: Schedule.cpp:63
#define BUILD_STR(param)
Internal macro.
Definition: log.hpp:66
const std::string & description() const override
Definition: Schedule.cpp:53
ScheduleId id() const override
Definition: Schedule.cpp:58
size_t odb_version() const override
Definition: Schedule.cpp:68
std::enable_if_t< is_shared_ptr_v< Out >, Out > assert_cast(const std::shared_ptr< In > &in)
Definition: AssertCast.hpp:58
void add_mapping(const Tools::ScheduleMappingPtr &map) override
Definition: Schedule.cpp:89
std::string description_
Definition: Schedule.hpp:88