fixed compiler warnings; minor refactoring
[public/netxms.git] / src / server / core / dctcolumn.cpp
CommitLineData
df94e0ce
VK
1/*
2** NetXMS - Network Management System
9ceab287 3** Copyright (C) 2003-2016 Victor Kirhenshtein
df94e0ce
VK
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: dctcolumn.cpp
20**
21**/
22
23#include "nxcore.h"
24
55bdca5a
VK
25/**
26 * Copy constructor
27 */
df94e0ce
VK
28DCTableColumn::DCTableColumn(const DCTableColumn *src)
29{
30 nx_strncpy(m_name, src->m_name, MAX_COLUMN_NAME);
22aaa779 31 m_flags = src->m_flags;
9ceab287 32 m_snmpOid = (src->m_snmpOid != NULL) ? new SNMP_ObjectId(*src->m_snmpOid) : NULL;
0b77b73e 33 m_displayName = (src->m_displayName != NULL) ? _tcsdup(src->m_displayName) : NULL;
df94e0ce
VK
34}
35
55bdca5a
VK
36/**
37 * Create column object from NXCP message
38 */
b368969c 39DCTableColumn::DCTableColumn(NXCPMessage *msg, UINT32 baseId)
b4369830 40{
b368969c
VK
41 msg->getFieldAsString(baseId, m_name, MAX_COLUMN_NAME);
42 m_flags = msg->getFieldAsUInt16(baseId + 1);
43 m_displayName = msg->getFieldAsString(baseId + 3);
b4369830 44
5c44534b 45 if (msg->isFieldExist(baseId + 2))
b4369830 46 {
967893bb 47 UINT32 oid[256];
b95f153a 48 size_t len = msg->getFieldAsInt32Array(baseId + 2, 256, oid);
b4369830
VK
49 if (len > 0)
50 {
9ceab287 51 m_snmpOid = new SNMP_ObjectId(oid, len);
b4369830
VK
52 }
53 else
54 {
55 m_snmpOid = NULL;
56 }
57 }
58 else
59 {
60 m_snmpOid = NULL;
61 }
62}
63
55bdca5a
VK
64/**
65 * Create column object from database result set
66 * Expected field order is following:
0b77b73e 67 * column_name,flags,snmp_oid,display_name
55bdca5a 68 */
b4369830
VK
69DCTableColumn::DCTableColumn(DB_RESULT hResult, int row)
70{
71 DBGetField(hResult, row, 0, m_name, MAX_COLUMN_NAME);
22aaa779 72 m_flags = (UINT16)DBGetFieldULong(hResult, row, 1);
0b77b73e 73 m_displayName = DBGetField(hResult, row, 3, NULL, 0);
b4369830 74
b4369830
VK
75 TCHAR oid[1024];
76 oid[0] = 0;
55bdca5a 77 DBGetField(hResult, row, 2, oid, 1024);
b4369830
VK
78 StrStrip(oid);
79 if (oid[0] != 0)
80 {
967893bb 81 UINT32 oidBin[256];
e0471fad 82 size_t len = SNMPParseOID(oid, oidBin, 256);
b4369830
VK
83 if (len > 0)
84 {
9ceab287 85 m_snmpOid = new SNMP_ObjectId(oidBin, len);
b4369830
VK
86 }
87 else
88 {
89 m_snmpOid = NULL;
90 }
91 }
92 else
93 {
94 m_snmpOid = NULL;
95 }
96}
97
55bdca5a 98/**
9387bc59
VK
99 * Create from NXMP record
100 */
101DCTableColumn::DCTableColumn(ConfigEntry *e)
102{
103 nx_strncpy(m_name, e->getSubEntryValue(_T("name"), 0, _T("")), MAX_COLUMN_NAME);
31b0f68b 104 m_flags = (UINT16)e->getSubEntryValueAsUInt(_T("flags"));
9387bc59
VK
105 m_displayName = _tcsdup(e->getSubEntryValue(_T("displayName"), 0, _T("")));
106
107 const TCHAR *oid = e->getSubEntryValue(_T("snmpOid"));
108 if ((oid != NULL) && (*oid != 0))
109 {
110 UINT32 oidBin[256];
e0471fad 111 size_t len = SNMPParseOID(oid, oidBin, 256);
9387bc59
VK
112 if (len > 0)
113 {
9ceab287 114 m_snmpOid = new SNMP_ObjectId(oidBin, len);
9387bc59
VK
115 }
116 else
117 {
118 m_snmpOid = NULL;
119 }
120 }
121 else
122 {
123 m_snmpOid = NULL;
124 }
125}
126
127/**
55bdca5a
VK
128 * Destructor
129 */
df94e0ce
VK
130DCTableColumn::~DCTableColumn()
131{
132 delete m_snmpOid;
0b77b73e 133 safe_free(m_displayName);
df94e0ce 134}
6e102d6e
VK
135
136/**
137 * Create NXMP record
138 */
139void DCTableColumn::createNXMPRecord(String &str, int id)
140{
4e0e77e6 141 str.appendFormattedString(_T("\t\t\t\t\t\t<column id=\"%d\">\n")
6e102d6e
VK
142 _T("\t\t\t\t\t\t\t<name>%s</name>\n")
143 _T("\t\t\t\t\t\t\t<displayName>%s</displayName>\n")
144 _T("\t\t\t\t\t\t\t<snmpOid>%s</snmpOid>\n")
145 _T("\t\t\t\t\t\t\t<flags>%d</flags>\n")
146 _T("\t\t\t\t\t\t</column>\n"),
147 id, (const TCHAR *)EscapeStringForXML2(m_name),
148 (const TCHAR *)EscapeStringForXML2(CHECK_NULL_EX(m_displayName)),
9ceab287 149 (m_snmpOid != NULL) ? (const TCHAR *)m_snmpOid->toString() : _T(""), (int)m_flags);
6e102d6e 150}
a31a2d32
VK
151
152/**
153 * Serialize to JSON
154 */
155json_t *DCTableColumn::toJson() const
156{
157 json_t *root = json_object();
158 json_object_set_new(root, "name", json_string_t(m_name));
159 json_object_set_new(root, "displayName", json_string_t(m_displayName));
160 json_object_set_new(root, "snmpOid", (m_snmpOid != NULL) ? json_string_t(m_snmpOid->toString()) : json_null());
161 json_object_set_new(root, "flags", json_integer(m_flags));
162 return root;
163}