- Added component locks
[public/netxms.git] / src / server / core / epp.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: epp.cpp
20 **
21 **/
22
23 #include "nms_core.h"
24
25
26 //
27 // Event policy rule constructor
28 //
29
30 EPRule::EPRule(DWORD dwId, char *szComment, DWORD dwFlags)
31 {
32 m_dwId = dwId;
33 m_dwFlags = dwFlags;
34 m_dwNumSources = 0;
35 m_pSourceList = NULL;
36 m_dwNumEvents = 0;
37 m_pdwEventList = NULL;
38 m_dwNumActions = 0;
39 m_pdwActionList = NULL;
40 m_pszComment = strdup(szComment);
41 }
42
43
44 //
45 // Event policy rule destructor
46 //
47
48 EPRule::~EPRule()
49 {
50 safe_free(m_pSourceList);
51 safe_free(m_pdwEventList);
52 safe_free(m_pdwActionList);
53 safe_free(m_pszComment);
54 }
55
56
57 //
58 // Load rule from database
59 //
60
61 BOOL EPRule::LoadFromDB(void)
62 {
63 DB_RESULT hResult;
64 char szQuery[256];
65 BOOL bSuccess = TRUE;
66 DWORD i;
67
68 // Load rule's sources
69 sprintf(szQuery, "SELECT source_type,object_id FROM PolicySourceList WHERE rule_id=%ld", m_dwId);
70 hResult = DBSelect(g_hCoreDB, szQuery);
71 if (hResult != NULL)
72 {
73 m_dwNumSources = DBGetNumRows(hResult);
74 m_pSourceList = (EVENT_SOURCE *)malloc(sizeof(EVENT_SOURCE) * m_dwNumSources);
75 for(i = 0; i < m_dwNumSources; i++)
76 {
77 m_pSourceList[i].iType = DBGetFieldLong(hResult, i, 0);
78 m_pSourceList[i].dwObjectId = DBGetFieldULong(hResult, i, 1);
79 }
80 DBFreeResult(hResult);
81 }
82 else
83 {
84 bSuccess = FALSE;
85 }
86
87 // Load rule's events
88 sprintf(szQuery, "SELECT event_id FROM PolicyEventList WHERE rule_id=%ld", m_dwId);
89 hResult = DBSelect(g_hCoreDB, szQuery);
90 if (hResult != NULL)
91 {
92 m_dwNumEvents = DBGetNumRows(hResult);
93 m_pdwEventList = (DWORD *)malloc(sizeof(DWORD) * m_dwNumEvents);
94 for(i = 0; i < m_dwNumEvents; i++)
95 m_pdwEventList[i] = DBGetFieldULong(hResult, i, 0);
96 DBFreeResult(hResult);
97 }
98 else
99 {
100 bSuccess = FALSE;
101 }
102
103 // Load rule's sources
104 sprintf(szQuery, "SELECT action_id FROM PolicyActionList WHERE rule_id=%ld", m_dwId);
105 hResult = DBSelect(g_hCoreDB, szQuery);
106 if (hResult != NULL)
107 {
108 m_dwNumActions = DBGetNumRows(hResult);
109 m_pdwActionList = (DWORD *)malloc(sizeof(DWORD) * m_dwNumActions);
110 for(i = 0; i < m_dwNumActions; i++)
111 m_pdwActionList[i] = DBGetFieldULong(hResult, i, 0);
112 DBFreeResult(hResult);
113 }
114 else
115 {
116 bSuccess = FALSE;
117 }
118
119 return bSuccess;
120 }
121
122
123 //
124 // Event processing policy constructor
125 //
126
127 EventPolicy::EventPolicy()
128 {
129 m_dwNumRules = 0;
130 m_ppRuleList = NULL;
131 }
132
133
134 //
135 // Event processing policy destructor
136 //
137
138 EventPolicy::~EventPolicy()
139 {
140 DWORD i;
141
142 for(i = 0; i < m_dwNumRules; i++)
143 safe_free(m_ppRuleList[i]);
144 safe_free(m_ppRuleList);
145 }
146
147
148 //
149 // Load event processing policy from database
150 //
151
152 BOOL EventPolicy::LoadFromDB(void)
153 {
154 DB_RESULT hResult;
155 BOOL bSuccess = FALSE;
156
157 hResult = DBSelect(g_hCoreDB, "SELECT id,flags,comments FROM EventPolicy ORDER BY id");
158 if (hResult != NULL)
159 {
160 DWORD i;
161
162 bSuccess = TRUE;
163 m_dwNumRules = DBGetNumRows(hResult);
164 m_ppRuleList = (EPRule **)malloc(sizeof(EPRule *) * m_dwNumRules);
165 for(i = 0; i < m_dwNumRules; i++)
166 {
167 m_ppRuleList[i] = new EPRule(DBGetFieldULong(hResult, i, 0),
168 DBGetField(hResult, i, 2),
169 DBGetFieldULong(hResult, i, 1));
170 bSuccess = bSuccess && m_ppRuleList[i]->LoadFromDB();
171 }
172 DBFreeResult(hResult);
173 }
174
175 return bSuccess;
176 }