6871faeaa34b997471815ba6ef6509da4276ff3b
[public/netxms.git] / src / server / core / ap_config.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2010 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: ap_config.cpp
20 **
21 **/
22
23 #include "nxcore.h"
24
25
26 //
27 // Agent policy default constructor
28 //
29
30 AgentPolicyConfig::AgentPolicyConfig()
31 : AgentPolicy(AGENT_POLICY_CONFIG)
32 {
33 m_fileContent = NULL;
34 }
35
36
37 //
38 // Constructor for user-initiated object creation
39 //
40
41 AgentPolicyConfig::AgentPolicyConfig(const TCHAR *name)
42 : AgentPolicy(name, AGENT_POLICY_CONFIG)
43 {
44 m_fileContent = NULL;
45 }
46
47
48 //
49 // Destructor
50 //
51
52 AgentPolicyConfig::~AgentPolicyConfig()
53 {
54 safe_free(m_fileContent);
55 }
56
57
58 //
59 // Save to database
60 //
61
62 BOOL AgentPolicyConfig::saveToDatabase(DB_HANDLE hdb)
63 {
64 lockProperties();
65
66 BOOL success = savePolicyCommonProperties(hdb);
67 if (success)
68 {
69 String data = DBPrepareString(hdb, m_fileContent);
70 size_t len = data.length() + 256;
71 TCHAR *query = (TCHAR *)malloc(len * sizeof(TCHAR));
72
73 _sntprintf(query, len, _T("SELECT policy_id FROM ap_config_files WHERE policy_id=%d"), m_id);
74 DB_RESULT hResult = DBSelect(hdb, query);
75 if (hResult != NULL)
76 {
77 BOOL isNew = (DBGetNumRows(hResult) == 0);
78 DBFreeResult(hResult);
79
80 if (isNew)
81 _sntprintf(query, len, _T("INSERT INTO ap_config_files (policy_id,file_content) VALUES (%d,%s)"),
82 m_id, (const TCHAR *)data);
83 else
84 _sntprintf(query, len, _T("UPDATE ap_config_files SET file_content=%s WHERE policy_id=%d"),
85 (const TCHAR *)data, m_id);
86 success = DBQuery(hdb, query);
87 }
88 free(query);
89 }
90
91 // Clear modifications flag and unlock object
92 if (success)
93 m_isModified = false;
94 unlockProperties();
95
96 return success;
97 }
98
99 /**
100 * Delete from database
101 */
102 bool AgentPolicyConfig::deleteFromDatabase(DB_HANDLE hdb)
103 {
104 bool success = AgentPolicy::deleteFromDatabase(hdb);
105 if (success)
106 {
107 success = executeQueryOnObject(hdb, _T("DELETE FROM ap_config_files WHERE policy_id=?"));
108 }
109 return success;
110 }
111
112 /**
113 * Load from database
114 */
115 bool AgentPolicyConfig::loadFromDatabase(DB_HANDLE hdb, UINT32 dwId)
116 {
117 bool success = false;
118
119 if (AgentPolicy::loadFromDatabase(hdb, dwId))
120 {
121 TCHAR query[256];
122
123 _sntprintf(query, 256, _T("SELECT file_content FROM ap_config_files WHERE policy_id=%d"), dwId);
124 DB_RESULT hResult = DBSelect(hdb, query);
125 if (hResult != NULL)
126 {
127 if (DBGetNumRows(hResult) > 0)
128 {
129 m_fileContent = DBGetField(hResult, 0, 0, NULL, 0);
130 success = true;
131 }
132 DBFreeResult(hResult);
133 }
134 }
135 return success;
136 }
137
138 /**
139 * Create NXCP message with policy data
140 */
141 void AgentPolicyConfig::fillMessageInternal(NXCPMessage *msg)
142 {
143 AgentPolicy::fillMessageInternal(msg);
144 msg->setField(VID_CONFIG_FILE_DATA, CHECK_NULL_EX(m_fileContent));
145 }
146
147 /**
148 * Modify policy from message
149 */
150 UINT32 AgentPolicyConfig::modifyFromMessageInternal(NXCPMessage *pRequest)
151 {
152 if (pRequest->isFieldExist(VID_CONFIG_FILE_DATA))
153 {
154 safe_free(m_fileContent);
155 m_fileContent = pRequest->getFieldAsString(VID_CONFIG_FILE_DATA);
156 }
157
158 return AgentPolicy::modifyFromMessageInternal(pRequest);
159 }
160
161 /**
162 * Create deployment message
163 */
164 bool AgentPolicyConfig::createDeploymentMessage(NXCPMessage *msg)
165 {
166 if (!AgentPolicy::createDeploymentMessage(msg))
167 return false;
168
169 if (m_fileContent == NULL)
170 return false; // Policy cannot be deployed
171
172 #ifdef UNICODE
173 char *fd = MBStringFromWideString(m_fileContent);
174 msg->setField(VID_CONFIG_FILE_DATA, (BYTE *)fd, (UINT32)strlen(fd));
175 free(fd);
176 #else
177 msg->setField(VID_CONFIG_FILE_DATA, (BYTE *)m_fileContent, (UINT32)strlen(m_fileContent));
178 #endif
179 return true;
180 }
181
182
183 //
184 // Create uninstall message
185 //
186
187 bool AgentPolicyConfig::createUninstallMessage(NXCPMessage *msg)
188 {
189 return AgentPolicy::createUninstallMessage(msg);
190 }