]> granicus.if.org Git - icinga2/blob - lib/db_ido/servicegroupdbobject.cpp
Rename IDO libraries.
[icinga2] / lib / db_ido / servicegroupdbobject.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/)   *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (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 General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "ido/servicegroupdbobject.h"
21 #include "ido/dbtype.h"
22 #include "ido/dbvalue.h"
23 #include "base/objectlock.h"
24 #include "base/initialize.h"
25 #include <boost/foreach.hpp>
26
27 using namespace icinga;
28
29 REGISTER_DBTYPE(ServiceGroup, "servicegroup", DbObjectTypeServiceGroup, "servicegroup_object_id", ServiceGroupDbObject);
30
31 ServiceGroupDbObject::ServiceGroupDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
32         : DbObject(type, name1, name2)
33 { }
34
35 Dictionary::Ptr ServiceGroupDbObject::GetConfigFields(void) const
36 {
37         Dictionary::Ptr fields = boost::make_shared<Dictionary>();
38         ServiceGroup::Ptr group = static_pointer_cast<ServiceGroup>(GetObject());
39
40         fields->Set("alias", group->GetDisplayName());
41
42         return fields;
43 }
44
45 Dictionary::Ptr ServiceGroupDbObject::GetStatusFields(void) const
46 {
47         return Empty;
48 }
49
50 void ServiceGroupDbObject::OnConfigUpdate(void)
51 {
52         ServiceGroup::Ptr group = static_pointer_cast<ServiceGroup>(GetObject());
53
54         DbQuery query1;
55         query1.Table = DbType::GetByName("ServiceGroup")->GetTable() + "_members";
56         query1.Type = DbQueryDelete;
57         query1.WhereCriteria = boost::make_shared<Dictionary>();
58         query1.WhereCriteria->Set("servicegroup_id", DbValue::FromObjectInsertID(group));
59         OnQuery(query1);
60
61         BOOST_FOREACH(const Service::Ptr& service, group->GetMembers()) {
62                 DbQuery query2;
63                 query2.Table = DbType::GetByName("ServiceGroup")->GetTable() + "_members";
64                 query2.Type = DbQueryInsert;
65                 query2.Fields = boost::make_shared<Dictionary>();
66                 query2.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
67                 query2.Fields->Set("servicegroup_id", DbValue::FromObjectInsertID(group));
68                 query2.Fields->Set("service_object_id", service);
69                 OnQuery(query2);
70         }
71 }