2
* Copyright (C) 2014 Canonical Ltd
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License version 3 as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by: Pawel Stolowski <pawel.stolowski@canonical.com>
19
#include <unity/scopes/internal/DepartmentImpl.h>
20
#include <unity/UnityExceptions.h>
21
#include <unity/scopes/internal/QueryImpl.h>
32
DepartmentImpl::DepartmentImpl(Query const& query, std::string const& label)
38
DepartmentImpl::DepartmentImpl(std::string const& department_id, Query const& query, std::string const& label)
42
query_.set_department_id(department_id);
45
DepartmentImpl::DepartmentImpl(std::string const& department_id, Query const& query, std::string const& label, DepartmentList const& subdepartments)
48
departments_(subdepartments)
50
query_.set_department_id(department_id);
53
void DepartmentImpl::set_subdepartments(DepartmentList const& departments)
55
departments_ = departments;
58
std::string DepartmentImpl::id() const
60
return query_.department_id();
63
std::string DepartmentImpl::label() const
68
Query DepartmentImpl::query() const
73
DepartmentList DepartmentImpl::subdepartments() const
78
VariantMap DepartmentImpl::serialize() const
82
vm["query"] = query_.serialize();
84
// sub-departments are optional
85
if (departments_.size())
87
VariantArray subdeparr;
88
for (auto const& dep: departments_)
90
subdeparr.push_back(Variant(dep.serialize()));
93
vm["departments"] = Variant(subdeparr);
98
Department DepartmentImpl::create(VariantMap const& var)
100
auto it = var.find("label");
103
throw unity::InvalidArgumentException("DepartmentImpl::create(): missing 'label'");
105
auto label = it->second.get_string();
106
it = var.find("query");
109
throw unity::InvalidArgumentException("DepartmentImpl::create(): missing 'query'");
111
auto query = QueryImpl::create(it->second.get_dict());
113
Department department(query, label);
115
it = var.find("departments");
118
DepartmentList subdeps;
119
for (auto const dep: it->second.get_array())
121
subdeps.push_back(create(dep.get_dict()));
123
department.set_subdepartments(subdeps);
128
void DepartmentImpl::validate_departments(DepartmentList const& departments, std::unordered_set<std::string>& lookup)
130
for (auto const& dep: departments)
132
if (lookup.find(dep.id()) != lookup.end())
134
std::stringstream str;
135
str << "DepartmentImpl::validate_departments(): Duplicate department: '" << dep.id() << "'";
136
throw unity::LogicException(str.str());
138
lookup.insert(dep.id());
139
validate_departments(dep.subdepartments(), lookup);
143
void DepartmentImpl::validate_departments(DepartmentList const& departments, std::string const ¤t_department_id)
145
if (departments.size() == 0)
147
throw unity::LogicException("DepartmentImpl::validate_departments(): empty departments list");
150
// don't allow duplicated department ids. ensure at current_department_id matches one of the departments (if non-empty).
151
std::unordered_set<std::string> lookup;
152
validate_departments(departments, lookup);
153
if (!current_department_id.empty())
155
if (lookup.find(current_department_id) == lookup.end())
157
std::stringstream str;
158
str << "DepartmentImpl::validate_departments(): current department '" << current_department_id << "' doesn't match any of the departments";
159
throw unity::LogicException(str.str());
164
} // namespace internal
166
} // namespace scopes