New C++ function: gnucomo_database::new_notification()
[gnucomo.git] / src / lib / database.cpp
1
2 /**************************************************************************
3 **  (c) Copyright 2002, Andromeda Technology & Automation
4 ** This is free software; you can redistribute it and/or modify it under the
5 ** terms of the GNU General Public License, see the file COPYING.
6 ***************************************************************************
7 ** MODULE INFORMATION *
8 ***********************
9 **      FILE NAME      : database.cpp
10 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
11 **      VERSION NUMBER : $Revision: 1.4 $
12 **
13 **  DESCRIPTION      :  Implementation of the gnucomo database classes
14 **
15 **  EXPORTED OBJECTS : 
16 **  LOCAL    OBJECTS : 
17 **  MODULES  USED    :
18 ***************************************************************************
19 **  ADMINISTRATIVE INFORMATION *
20 ********************************
21 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
22 **      CREATION DATE   : Sep 10, 2002
23 **      LAST UPDATE     : Jan 17, 2003
24 **      MODIFICATIONS   : 
25 **************************************************************************/
26
27 /*****************************
28    $Log: database.cpp,v $
29    Revision 1.4  2003-01-18 08:52:32  arjen
30    New C++ function: gnucomo_database::new_notification()
31
32    Revision 1.3  2002/11/09 08:04:27  arjen
33    Added a reference to the GPL
34
35    Revision 1.2  2002/11/04 10:13:36  arjen
36    Use proper namespace for iostream classes
37
38    Revision 1.1  2002/10/05 10:25:49  arjen
39    Creation of gcm_input and a first approach to a web interface
40
41 *****************************/
42
43 static const char *RCSID = "$Id: database.cpp,v 1.4 2003-01-18 08:52:32 arjen Exp $";
44
45 #include <AXE/date.h>
46
47 #include "database.h"
48
49 extern bool verbose;   /*  Defined in the main application */
50
51 /*=========================================================================
52 **  NAME           : gnucomo_database
53 **  SYNOPSIS       : gnucomo_database(gnucomo_config &c);
54 **  PARAMETERS     : 
55 **  RETURN VALUE   : Database constructor. Establishes a connection with
56 **                   the database server.
57 **
58 **  DESCRIPTION    : 
59 **
60 **  VARS USED      :
61 **  VARS CHANGED   :
62 **  FUNCTIONS USED :
63 **  SEE ALSO       :
64 **  LAST MODIFIED  : Sep 26, 2002
65 **=========================================================================
66 */
67
68 gnucomo_database::gnucomo_database(gnucomo_config *c)
69 {
70    cfg = c;
71
72    if (verbose)
73    {
74       std::cout <<  "Database connection string = " << cfg->Database() << "\n";
75    }
76
77    db = new PgDatabase(cfg->Database());
78
79    if (db->ConnectionBad())
80    {
81       std::cerr << "Can not connect to database: " << db->ErrorMessage();
82    }
83 }
84
85 /*=========================================================================
86 **  NAME           : find_host
87 **  SYNOPSIS       : String gnucomo_database::find_host(String hostname);
88 **  PARAMETERS     : 
89 **  RETURN VALUE   : Find a hostname in the 'object' table of the gnucomo database
90 **                   and return its object id.
91 **                   Return an empty string as objectid if the hostname is
92 **                   not found.
93 **
94 **  DESCRIPTION    : 
95 **
96 **  VARS USED      :
97 **  VARS CHANGED   :
98 **  FUNCTIONS USED :
99 **  SEE ALSO       :
100 **  LAST MODIFIED  : Sep 16, 2002
101 **=========================================================================
102 */
103
104 String gnucomo_database::find_host(const String hostname)
105 {
106    String objectid("");
107    String check_host("select objectid from object where ");
108
109    check_host += "objectname = '";
110    check_host += hostname;
111    check_host += "'";
112
113    if (Query(check_host) > 0)
114    {
115       objectid = String(db->GetValue(0, "objectid"));
116    }
117
118    return objectid;
119 }
120
121 void gnucomo_database::new_notification(String objectid, String issue, String remark)
122 {
123    String qry;
124    UTC    now = Now();
125
126    String insertion;
127    String notif_id;
128
129    String issueid("");
130
131    std::cout << "Creating notification for " << issue << "\n";
132
133    qry = "select type_of_issueid, suggested_priority from type_of_issue where name='";
134    qry += issue + "'";
135    if (Query(qry) == 1)
136    {
137       issueid = Field(0, "type_of_issueid");
138       insertion = "insert into notification (objectid, type_of_issueid, timestamp, ";
139       insertion += "   statuscode, priority) values ('";
140       insertion += objectid + "', '";
141       insertion += issueid + "', '" + now.format("%Y-%m-%d %T") + "', 'new', '";
142       insertion += Field(0, "suggested_priority") + "')";
143
144       std::cout << insertion << "\n";
145
146       qry = "select notificationid from notification where objectid='";
147       qry += objectid + "' and type_of_issueid = '";
148       qry += issueid + "' order by notificationid";
149
150       Query(insertion);
151       int tuples = Query(qry);
152
153       if (tuples > 0)
154       {
155          notif_id = Field(tuples - 1, "notificationid");
156
157          insertion = "insert into action_user (actionid, username, notificationid,";
158          insertion += "    timestamp, statuscode, remarks) values ('1', 'gnucomo', '";
159          insertion += notif_id + "', '" + now.format("%Y-%m-%d %T") + "', 'new', '";
160          insertion += remark + "')";
161
162          std::cout << insertion << "\n";
163          Query(insertion);
164       }
165       else
166       {
167          std::cerr << "Error inserting notification.\n";
168          exit(1);
169       }
170    }
171 }
172