Leosac  0.8.0
Open Source Access Control
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 }
Leosac::Tools::Schedule::odb_version_
size_t odb_version_
Definition: Schedule.hpp:94
Leosac::Tools::Schedule::Schedule
Schedule(const std::string &sched_name="")
Definition: Schedule.cpp:27
Leosac::Tools::Schedule::timeframes_
std::vector< SingleTimeFrame > timeframes_
Definition: Schedule.hpp:83
Leosac::Tools::Schedule::name_
std::string name_
Definition: Schedule.hpp:86
BUILD_STR
#define BUILD_STR(param)
Internal macro.
Definition: log.hpp:63
Leosac::Tools::Schedule::timeframes
std::vector< SingleTimeFrame > timeframes() const override
Retrieves the list of timesframes that compose this schedule.
Definition: Schedule.cpp:63
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::Tools::Schedule::id
ScheduleId id() const override
Definition: Schedule.cpp:58
Leosac::Tools::Schedule::mapping_
std::vector< Tools::ScheduleMappingPtr > mapping_
Definition: Schedule.hpp:91
Leosac::Tools::ScheduleMappingPtr
std::shared_ptr< ScheduleMapping > ScheduleMappingPtr
Definition: ToolsFwd.hpp:41
Leosac::Tools::ISchedule::name
virtual const std::string & name() const =0
Retrieve the name of the schedule.
Leosac::Tools::Schedule::add_timeframe
void add_timeframe(const SingleTimeFrame &tf) override
Add the given timeframe to this schedule;.
Definition: Schedule.cpp:43
Leosac::Tools::Schedule::description
const std::string & description() const override
Definition: Schedule.cpp:53
Leosac::Tools::ScheduleId
unsigned long ScheduleId
Definition: ToolsFwd.hpp:33
ModelException.hpp
Leosac::Tools::Schedule::clear_mapping
void clear_mapping() override
Definition: Schedule.cpp:96
ModelException
An exception class for general API error.
Definition: ModelException.hpp:33
Leosac::Tools::Schedule::name
const std::string & name() const override
Retrieve the name of the schedule.
Definition: Schedule.cpp:48
Leosac::Tools::ScheduleValidator::validate_name
static void validate_name(const std::string &name)
Definition: Schedule.cpp:111
Leosac::Tools::Schedule::clear_timeframes
void clear_timeframes() override
Remove all the timeframes from this schedule.
Definition: Schedule.cpp:84
Leosac::Tools::Schedule::id_
ScheduleId id_
Definition: Schedule.hpp:80
Leosac::Tools::Schedule::description_
std::string description_
Definition: Schedule.hpp:88
log.hpp
Leosac::Tools::ISchedule
Definition: ISchedule.hpp:38
Leosac::Tools::SingleTimeFrame
This struct abstracts what we call a single time frame.
Definition: SingleTimeFrame.hpp:38
Schedule.hpp
Leosac::Tools::Schedule::odb_version
size_t odb_version() const override
Definition: Schedule.cpp:68
Leosac::Tools::Schedule::mapping
std::vector< ScheduleMappingPtr > mapping() const override
Definition: Schedule.cpp:101
Leosac::Tools
Definition: DatabaseLogSink.hpp:27
Leosac::Tools::Schedule::is_in_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
Leosac::Tools::Schedule::add_mapping
void add_mapping(const Tools::ScheduleMappingPtr &map) override
Definition: Schedule.cpp:89
AssertCast.hpp
Leosac::Tools::ScheduleValidator::validate
static void validate(const ISchedule &sched)
Definition: Schedule.cpp:106