53aeee3f6de6bbad4c8b3db025f454841677471e
[public/netxms.git] / src / server / core / uniroot.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: uniroot.cpp
20 **
21 **/
22
23 #include "nxcore.h"
24
25 /**
26 * Constructor
27 */
28 UniversalRoot::UniversalRoot() : NetObj()
29 {
30 m_guid = uuid::generate();
31 }
32
33 /**
34 * Destructor
35 */
36 UniversalRoot::~UniversalRoot()
37 {
38 }
39
40 /**
41 * Link child objects
42 * This method is expected to be called only at startup, so we don't lock
43 */
44 void UniversalRoot::linkObjects()
45 {
46 NetObj::linkObjects();
47
48 DB_HANDLE hdb = DBConnectionPoolAcquireConnection();
49
50 TCHAR szQuery[256];
51 _sntprintf(szQuery, sizeof(szQuery) / sizeof(TCHAR), _T("SELECT object_id FROM container_members WHERE container_id=%d"), m_id);
52
53 DB_RESULT hResult = DBSelect(hdb, szQuery);
54 if (hResult != NULL)
55 {
56 int count = DBGetNumRows(hResult);
57 for(int i = 0; i < count; i++)
58 {
59 UINT32 dwObjectId = DBGetFieldULong(hResult, i, 0);
60 NetObj *pObject = FindObjectById(dwObjectId);
61 if (pObject != NULL)
62 linkObject(pObject);
63 else
64 nxlog_write(MSG_ROOT_INVALID_CHILD_ID, EVENTLOG_WARNING_TYPE, "ds", dwObjectId, getObjectClassName());
65 }
66 DBFreeResult(hResult);
67 }
68
69 DBConnectionPoolReleaseConnection(hdb);
70 }
71
72 /**
73 * Save object to database
74 */
75 bool UniversalRoot::saveToDatabase(DB_HANDLE hdb)
76 {
77 TCHAR szQuery[1024];
78
79 lockProperties();
80
81 saveCommonProperties(hdb);
82
83 // Update members list
84 _sntprintf(szQuery, sizeof(szQuery) / sizeof(TCHAR), _T("DELETE FROM container_members WHERE container_id=%d"), m_id);
85 DBQuery(hdb, szQuery);
86 lockChildList(FALSE);
87 for(int i = 0; i < m_childList->size(); i++)
88 {
89 _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());
90 DBQuery(hdb, szQuery);
91 }
92 unlockChildList();
93
94 // Save access list
95 saveACLToDB(hdb);
96
97 // Unlock object and clear modification flag
98 unlockProperties();
99 m_isModified = false;
100 return true;
101 }
102
103 /**
104 * Load properties from database
105 */
106 void UniversalRoot::loadFromDatabase(DB_HANDLE hdb)
107 {
108 loadCommonProperties(hdb);
109 loadACLFromDB(hdb);
110 }