0982eee6888284a4de303f07ab8eda12765a4d4f
[public/netxms.git] / src / server / core / bizsvcroot.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2016 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (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
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: bizsvcroot.cpp
20 **
21 **/
22
23 #include "nxcore.h"
24
25 /**
26 * Business service tree root class default constructor
27 */
28 BusinessServiceRoot::BusinessServiceRoot() : ServiceContainer()
29 {
30 m_id = BUILTIN_OID_BUSINESSSERVICEROOT;
31 _tcscpy(m_name, _T("Business Services"));
32 m_guid = uuid::generate();
33 m_status = STATUS_NORMAL;
34 }
35
36 /**
37 * Business service root class destructor
38 */
39 BusinessServiceRoot::~BusinessServiceRoot()
40 {
41 }
42
43 /**
44 * Save object to database
45 */
46 bool BusinessServiceRoot::saveToDatabase(DB_HANDLE hdb)
47 {
48 TCHAR szQuery[1024];
49
50 lockProperties();
51
52 saveCommonProperties(hdb);
53
54 // Update members list
55 _sntprintf(szQuery, sizeof(szQuery) / sizeof(TCHAR), _T("DELETE FROM container_members WHERE container_id=%d"), m_id);
56 DBQuery(hdb, szQuery);
57 lockChildList(false);
58 for(int i = 0; i < m_childList->size(); i++)
59 {
60 _sntprintf(szQuery, sizeof(szQuery) / sizeof(TCHAR), _T("INSERT INTO container_members (container_id,object_id) VALUES (%d,%d)"), m_id, m_childList->get(i)->getId());
61 DBQuery(hdb, szQuery);
62 }
63 unlockChildList();
64
65 // Save access list
66 saveACLToDB(hdb);
67
68 // Unlock object and clear modification flag
69 unlockProperties();
70 m_isModified = false;
71 return true;
72 }
73
74 /**
75 * Load properties from database
76 */
77 void BusinessServiceRoot::loadFromDatabase(DB_HANDLE hdb)
78 {
79 loadCommonProperties(hdb);
80 loadACLFromDB(hdb);
81 initUptimeStats();
82 }
83
84 /**
85 * Link child objects
86 * This method is expected to be called only at startup, so we don't lock
87 */
88 void BusinessServiceRoot::linkObjects()
89 {
90 ServiceContainer::linkObjects();
91
92 DB_HANDLE hdb = DBConnectionPoolAcquireConnection();
93
94 TCHAR szQuery[256];
95 _sntprintf(szQuery, sizeof(szQuery) / sizeof(TCHAR), _T("SELECT object_id FROM container_members WHERE container_id=%d"), m_id);
96
97 DB_RESULT hResult = DBSelect(hdb, szQuery);
98 if (hResult != NULL)
99 {
100 int count = DBGetNumRows(hResult);
101 for(int i = 0; i < count; i++)
102 {
103 UINT32 dwObjectId = DBGetFieldULong(hResult, i, 0);
104 NetObj *pObject = FindObjectById(dwObjectId);
105 if (pObject != NULL)
106 linkObject(pObject);
107 else
108 nxlog_write(MSG_ROOT_INVALID_CHILD_ID, EVENTLOG_WARNING_TYPE, "ds", dwObjectId, getObjectClassName());
109 }
110 DBFreeResult(hResult);
111 }
112 DBConnectionPoolReleaseConnection(hdb);
113 }