a3ed1a5fcbb874e57170bdf4cc452b2db5e73ca5
[public/netxms.git] / src / server / core / uniroot.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003, 2004 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 ** $module: uniroot.cpp
20 **
21 **/
22
23 #include "nxcore.h"
24
25
26 //
27 // Constructor
28 //
29
30 UniversalRoot::UniversalRoot()
31 :NetObj()
32 {
33 }
34
35
36 //
37 // Destructor
38 //
39
40 UniversalRoot::~UniversalRoot()
41 {
42 }
43
44
45 //
46 // Link child objects
47 // This method is expected to be called only at startup, so we don't lock
48 //
49
50 void UniversalRoot::LinkChildObjects(void)
51 {
52 DWORD i, dwNumChilds, dwObjectId;
53 NetObj *pObject;
54 char szQuery[256];
55 DB_RESULT hResult;
56
57 // Load child list and link objects
58 sprintf(szQuery, "SELECT object_id FROM container_members WHERE container_id=%d", m_dwId);
59 hResult = DBSelect(g_hCoreDB, szQuery);
60 if (hResult != NULL)
61 {
62 dwNumChilds = DBGetNumRows(hResult);
63 for(i = 0; i < dwNumChilds; i++)
64 {
65 dwObjectId = DBGetFieldULong(hResult, i, 0);
66 pObject = FindObjectById(dwObjectId);
67 if (pObject != NULL)
68 LinkObject(pObject);
69 else
70 nxlog_write(MSG_ROOT_INVALID_CHILD_ID, EVENTLOG_WARNING_TYPE, "ds",
71 dwObjectId, g_szClassName[Type()]);
72 }
73 DBFreeResult(hResult);
74 }
75 }
76
77
78 //
79 // Save object to database
80 //
81
82 BOOL UniversalRoot::SaveToDB(DB_HANDLE hdb)
83 {
84 char szQuery[1024];
85 DWORD i;
86
87 LockData();
88
89 SaveCommonProperties(hdb);
90
91 // Update members list
92 sprintf(szQuery, "DELETE FROM container_members WHERE container_id=%d", m_dwId);
93 DBQuery(hdb, szQuery);
94 LockChildList(FALSE);
95 for(i = 0; i < m_dwChildCount; i++)
96 {
97 sprintf(szQuery, "INSERT INTO container_members (container_id,object_id) VALUES (%d,%d)", m_dwId, m_pChildList[i]->Id());
98 DBQuery(hdb, szQuery);
99 }
100 UnlockChildList();
101
102 // Save access list
103 SaveACLToDB(hdb);
104
105 // Unlock object and clear modification flag
106 UnlockData();
107 m_bIsModified = FALSE;
108 return TRUE;
109 }
110
111
112 //
113 // Load properties from database
114 //
115
116 void UniversalRoot::LoadFromDB(void)
117 {
118 LoadCommonProperties();
119 LoadACLFromDB();
120 }