diff --git a/SAS/OTDB/include/OTDB/OTDBtree.h b/SAS/OTDB/include/OTDB/OTDBtree.h index daab88d651aa8975155db7d011de7d52cb529996..3dd85864cb124f8fae8de7348e63ae9193113025 100644 --- a/SAS/OTDB/include/OTDB/OTDBtree.h +++ b/SAS/OTDB/include/OTDB/OTDBtree.h @@ -50,16 +50,18 @@ class OTDBconnection; // The the last few fields will be empty for PIC trees. class OTDBtree { public: - OTDBtree() : momID(0), itsTreeID(0) {}; + OTDBtree() : itsTreeID(0), itsMomID(0) {}; ~OTDBtree() {}; treeIDType treeID() const { return (itsTreeID); } - treeIDType momID; + treeIDType momID() const { return (itsMomID); } classifType classification; // development / test / operational string creator; ptime creationDate; treeType type; // hardware / VItemplate / VHtree treeState state; // idle / configure / ... / active / ... + string description; // free text + // -- VIC only -- treeIDType originalTree; string campaign; @@ -74,10 +76,11 @@ public: private: //# Prevent changing the database keys - OTDBtree(treeIDType aTreeID) : momID(0), itsTreeID(aTreeID) {}; + OTDBtree(treeIDType aTreeID) : itsTreeID(aTreeID), itsMomID(0) {}; OTDBtree(const pqxx::result::tuple& row); treeIDType itsTreeID; + treeIDType itsMomID; }; //# diff --git a/SAS/OTDB/include/OTDB/TreeMaintenance.h b/SAS/OTDB/include/OTDB/TreeMaintenance.h index 2ff8a49f383b6fc15878157572441bcaba092952..1d0a1710fd9a1f95f94f7b92232495c3b2c53f2a 100644 --- a/SAS/OTDB/include/OTDB/TreeMaintenance.h +++ b/SAS/OTDB/include/OTDB/TreeMaintenance.h @@ -81,9 +81,6 @@ public: // Save new node or update the limits and description fields of the node. bool saveComponentNode (VICnodeDef& aNode); - // Save the parameter definition - bool saveComponentParam (OTDBparam& aParam); - //# --- VIC maintenance : Templates --- // From a component tree a template tree can be constructed. In a template @@ -110,6 +107,9 @@ public: OTDBparam getParam (treeIDType aTreeID, nodeIDType aParamID); + // Save the parameter definition + bool saveParam (OTDBparam& aParam); + // Get a number of levels of children. vector<OTDBnode> getItemList (treeIDType aTreeID, nodeIDType topNode, @@ -183,6 +183,15 @@ public: bool setTreeState(treeIDType aTreeID, treeState aState); + // Update the description of a tree. + bool setDescription(treeIDType aTreeID, + string aDescription); + + // Set the scheduling times of the tree + bool setSchedule(treeIDType aTreeID, + const ptime& aStartTime, + const ptime& aStopTime); + // Whenever an error occurs in one the OTDB functions the message can // be retrieved with this function. inline string errorMsg() const; diff --git a/SAS/OTDB/sql/create_OTDB.sql b/SAS/OTDB/sql/create_OTDB.sql index fa0ba5e2b8b4d45a7ea4bf9b636e682b7f4521c9..73e0c4abe5b0fbe43c10256b7ec503fb2b4cdf80 100644 --- a/SAS/OTDB/sql/create_OTDB.sql +++ b/SAS/OTDB/sql/create_OTDB.sql @@ -9,7 +9,6 @@ \i create_log_system.sql \i security_func.sql -\i classify_func.sql \i misc_func.sql -- State history @@ -21,7 +20,6 @@ \i getTreeList_func.sql \i getTreeInfo_func.sql \i newTree_func.sql -\i setMomInfo_func.sql -- PICtree \i create_PIC_tables.sql @@ -57,6 +55,7 @@ \i getVHitemList_func.sql \i exportTree_func.sql \i searchVHinPeriod_func.sql +\i setSchedule_func.sql -- multi treetype \i getTopNode_func.sql @@ -66,5 +65,7 @@ \i copyTree_func.sql \i deleteTree_func.sql \i addKVT_func.sql - +\i classify_func.sql +\i setMomInfo_func.sql +\i setDescription_func.sql diff --git a/SAS/OTDB/sql/create_tree_table.sql b/SAS/OTDB/sql/create_tree_table.sql index 9b51cddbafa9f97eab4949a637cc617bcf43a136..b064ab28a49c08407ccdb2357dd9948cca8f78db 100644 --- a/SAS/OTDB/sql/create_tree_table.sql +++ b/SAS/OTDB/sql/create_tree_table.sql @@ -56,6 +56,7 @@ CREATE TABLE OTDBtree ( starttime TIMESTAMP(0), stoptime TIMESTAMP(0), owner INT4 REFERENCES operator(ID), + description TEXT, -- contraints CONSTRAINT tree_uniq UNIQUE (treeID) diff --git a/SAS/OTDB/sql/create_types.sql b/SAS/OTDB/sql/create_types.sql index dbd3ac3e6a5c3c4619215e9bd8a90e3f63cb4bdc..1f3d1be103d4309bca3859e1fdd7bfd889fcf5fe 100644 --- a/SAS/OTDB/sql/create_types.sql +++ b/SAS/OTDB/sql/create_types.sql @@ -44,7 +44,8 @@ CREATE TYPE treeInfo AS ( originalTree INT4, -- OTDBtree.treeID%TYPE, campaign VARCHAR(30), -- campaign.name%TYPE, starttime timestamp(0), - stoptime timestamp(0) + stoptime timestamp(0), + description TEXT ); CREATE TYPE stateInfo AS ( diff --git a/SAS/OTDB/sql/getTreeInfo_func.sql b/SAS/OTDB/sql/getTreeInfo_func.sql index fff0fc6ed1ad2d77c0288f3acde1ff1ca9312c8d..51a11fb305666bb142b1625a64a6483359889617 100644 --- a/SAS/OTDB/sql/getTreeInfo_func.sql +++ b/SAS/OTDB/sql/getTreeInfo_func.sql @@ -54,7 +54,8 @@ CREATE OR REPLACE FUNCTION getTreeInfo(INT4, BOOLEAN) t.originID, c.name, t.starttime, - t.stoptime + t.stoptime, + t.description INTO vRecord FROM OTDBtree t INNER JOIN OTDBuser u ON t.creator = u.userid @@ -75,7 +76,8 @@ CREATE OR REPLACE FUNCTION getTreeInfo(INT4, BOOLEAN) t.originID, c.name, t.starttime, - t.stoptime + t.stoptime, + t.description INTO vRecord FROM OTDBtree t INNER JOIN OTDBuser u ON t.creator = u.userid diff --git a/SAS/OTDB/sql/getTreeList_func.sql b/SAS/OTDB/sql/getTreeList_func.sql index 5d579dc3aac3e2d110d1f0b7456df95cfd83eec9..d9c5a156e0fdbc6b48f9850c7200c4eea28e65a9 100644 --- a/SAS/OTDB/sql/getTreeList_func.sql +++ b/SAS/OTDB/sql/getTreeList_func.sql @@ -73,7 +73,8 @@ CREATE OR REPLACE FUNCTION getTreeList(INT2, INT2) t.originID, c.name, t.starttime, - t.stoptime + t.stoptime, + t.description FROM OTDBtree t INNER JOIN OTDBuser u ON t.creator = u.userid INNER JOIN campaign c ON c.ID = t.campaign diff --git a/SAS/OTDB/sql/setDescription_func.sql b/SAS/OTDB/sql/setDescription_func.sql new file mode 100644 index 0000000000000000000000000000000000000000..f4fd039d51309512f0da2a9f3d87d6e3c8da2d7c --- /dev/null +++ b/SAS/OTDB/sql/setDescription_func.sql @@ -0,0 +1,59 @@ +-- +-- setDescription.sql: function for changing the description of a tree +-- +-- Copyright (C) 2006 +-- ASTRON (Netherlands Foundation for Research in Astronomy) +-- P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, seg@astron.nl +-- +-- This program is free software; you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation; either version 2 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program; if not, write to the Free Software +-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +-- +-- $Id$ +-- + +-- +-- setDescription (authToken, treeID, description) +-- +-- Authorisation: yes +-- +-- Tables: otdbtree update +-- +-- Types: none +-- +CREATE OR REPLACE FUNCTION setDescription(INT4, INT4, TEXT) + RETURNS BOOLEAN AS ' + DECLARE + vFunction INT2 := 1; + vIsAuth BOOLEAN; + vAuthToken ALIAS FOR $1; + vCampaignID campaign.id%TYPE; + + BEGIN + -- check authorisation(authToken, treeID, func, none) + vIsAuth := FALSE; + SELECT isAuthorized(vAuthToken, $2, vFunction, 0) + INTO vIsAuth; + IF NOT vIsAuth THEN + RAISE EXCEPTION \'Not authorized.\'; + RETURN FALSE; + END IF; + + -- update the tree + UPDATE OTDBtree + SET description = $3 + WHERE treeID = $2; + + RETURN TRUE; + END; +' LANGUAGE plpgsql; diff --git a/SAS/OTDB/sql/setSchedule_func.sql b/SAS/OTDB/sql/setSchedule_func.sql new file mode 100644 index 0000000000000000000000000000000000000000..f84563d78645e6f75414e8c30b865214372a7a84 --- /dev/null +++ b/SAS/OTDB/sql/setSchedule_func.sql @@ -0,0 +1,79 @@ +-- +-- SetSchedule.sql: function for changing the scheduling times of a tree. +-- +-- Copyright (C) 2006 +-- ASTRON (Netherlands Foundation for Research in Astronomy) +-- P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, seg@astron.nl +-- +-- This program is free software; you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation; either version 2 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program; if not, write to the Free Software +-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +-- +-- $Id$ +-- + +-- +-- setSchedule (authToken, treeID, aStartTime, aStopTime) +-- +-- Authorisation: yes +-- +-- Tables: otdbtree update +-- +-- Types: none +-- +CREATE OR REPLACE FUNCTION setSchedule(INT4, INT4, TIMESTAMP, TIMESTAMP) + RETURNS BOOLEAN AS ' + DECLARE + vFunction INT2 := 1; + TSactive CONSTANT INT2 := 600; + TThierarchy CONSTANT INT2 := 30; + vIsAuth BOOLEAN; + vAuthToken ALIAS FOR $1; + vCampaignID campaign.id%TYPE; + vTreeType OTDBtree.treetype%TYPE; + vState OTDBtree.state%TYPE; + + BEGIN + -- check authorisation(authToken, treeID, func, none) + vIsAuth := FALSE; + SELECT isAuthorized(vAuthToken, $2, vFunction, 0) + INTO vIsAuth; + IF NOT vIsAuth THEN + RAISE EXCEPTION \'Not authorized.\'; + RETURN FALSE; + END IF; + + -- Only non-active VH trees can be scheduled. + SELECT treetype, state + INTO vTreeType, vState + FROM OTDBtree + WHERE treeID = $2; + + IF vTreeType <> TThierarchy THEN + RAISE EXCEPTION \'Only VH trees can be scheduled.\'; + END IF; + + IF vState = TSactive THEN + RAISE EXCEPTION \'Tree may not be active\'; + END IF; + + -- Finally update tree + UPDATE OTDBtree + SET starttime = $3, + stoptime = $4 + WHERE treeID = $2; + + RETURN TRUE; + END; +' LANGUAGE plpgsql; + diff --git a/SAS/OTDB/src/OTDBtree.cc b/SAS/OTDB/src/OTDBtree.cc index 4c2063e48e42c5007fe0b7c649b7dd348dd3485c..bf4fa0fddcfb58997fa26ca261aeed6b69bd4d27 100644 --- a/SAS/OTDB/src/OTDBtree.cc +++ b/SAS/OTDB/src/OTDBtree.cc @@ -38,7 +38,7 @@ OTDBtree::OTDBtree(const result::tuple& row) // construct OTDBtree class with right ID // Note: names refer to SQL OTDBtree type row["treeid"].to(itsTreeID); - row["momid"].to(momID); + row["momid"].to(itsMomID); // fill in rest of the fields row["classification"].to(classification); @@ -62,6 +62,7 @@ OTDBtree::OTDBtree(const result::tuple& row) if (stop.length() > 0) { stoptime = time_from_string(stop); } + row["description"].to(description); } // @@ -70,17 +71,18 @@ OTDBtree::OTDBtree(const result::tuple& row) // Show Tree charateristics. ostream& OTDBtree::print (ostream& os) const { - os << "treeID : " << itsTreeID << endl; - os << "MomID : " << momID << endl; + os << "treeID : " << itsTreeID << endl; + os << "MomID : " << itsMomID << endl; os << "classification: " << classification << endl; - os << "creator : " << creator << endl; - os << "creationdate : " << creationDate << endl; - os << "tree type : " << type << endl; - os << "state : " << state << endl; - os << "original tree : " << originalTree << endl; - os << "campaign : " << campaign << endl; - os << "starttime : " << starttime << endl; - os << "stoptime : " << stoptime << endl; + os << "creator : " << creator << endl; + os << "creationdate : " << creationDate << endl; + os << "tree type : " << type << endl; + os << "state : " << state << endl; + os << "original tree : " << originalTree << endl; + os << "campaign : " << campaign << endl; + os << "starttime : " << starttime << endl; + os << "stoptime : " << stoptime << endl; + os << "description : " << description << endl; return (os); } diff --git a/SAS/OTDB/src/TreeMaintenance.cc b/SAS/OTDB/src/TreeMaintenance.cc index 7e8f07a9d5fc325b92aacca238e3c64530ab33df..e628b8a2464437e9e76fc89ba4a41cd9212deb04 100644 --- a/SAS/OTDB/src/TreeMaintenance.cc +++ b/SAS/OTDB/src/TreeMaintenance.cc @@ -25,6 +25,7 @@ //# Includes #include <Common/LofarLogger.h> +#include <Common/lofar_datetime.h> #include <fstream> #include <OTDB/TreeMaintenance.h> #include <OTDB/OTDBnode.h> @@ -1064,5 +1065,92 @@ bool TreeMaintenance::setTreeState(treeIDType aTreeID, } +// +// setDescription(treeID, description): bool +// +// Set the description of the tree. +bool TreeMaintenance::setDescription(treeIDType aTreeID, + string aDescription) +{ + // Check connection + if (!itsConn->connect()) { + itsError = itsConn->errorMsg(); + return (false); + } + + work xAction(*(itsConn->getConn()), "setDescription"); + try { + // construct a query that calls a stored procedure. + result res = xAction.exec( + formatString("SELECT setDescription(%d,%d,'%s'::text)", + itsConn->getAuthToken(), + aTreeID, + aDescription.c_str())); + + // Analyse result. + bool succes; + res[0]["setDescription"].to(succes); + if (!succes) { + itsError = "Unable to save the description"; + return (false); + } + + xAction.commit(); + return (true); + } + catch (Exception& ex) { + itsError = string("Exception during setDescription:") + ex.what(); + LOG_FATAL(itsError); + return (false); + } + + return (false); +} + +// +// setSchedule(treeID, startTime, stopTime): bool +// +// Set the Executiontime of a tree +bool TreeMaintenance::setSchedule(treeIDType aTreeID, + const ptime& aStartTime, + const ptime& aStopTime) +{ + // Check connection + if (!itsConn->connect()) { + itsError = itsConn->errorMsg(); + return (false); + } + + work xAction(*(itsConn->getConn()), "setSchedule"); + try { + // construct a query that calls a stored procedure. + result res = xAction.exec( + formatString("SELECT setSchedule(%d,%d,'%s','%s')", + itsConn->getAuthToken(), + aTreeID, + to_simple_string(aStartTime).c_str(), + to_simple_string(aStopTime).c_str())); + + // Analyse result. + bool succes; + res[0]["setSchedule"].to(succes); + if (!succes) { + itsError = "Unable to set the schedule times:"; + return (false); + } + + xAction.commit(); + return (true); + } + catch (Exception& ex) { + itsError = string("Exception during setSchedule:") + ex.what(); + LOG_FATAL(itsError); + return (false); + } + + return (false); +} + + } // namespace OTDB } // namespace LOFAR diff --git a/SAS/OTDB/src/loadCompFile.cc b/SAS/OTDB/src/loadCompFile.cc index ad400f2e0e6779b5618edf350387a7ab53fd6054..2ea15943939d060cee773751b2199aa1a22fa13c 100644 --- a/SAS/OTDB/src/loadCompFile.cc +++ b/SAS/OTDB/src/loadCompFile.cc @@ -40,9 +40,9 @@ namespace LOFAR { namespace OTDB { // -// saveComponentParam(OTDBparam) : bool +// saveParam(OTDBparam) : bool // -bool TreeMaintenance::saveComponentParam(OTDBparam& aParam) +bool TreeMaintenance::saveParam(OTDBparam& aParam) { // Check connection if (!itsConn->connect()) { @@ -346,7 +346,7 @@ nodeIDType TreeMaintenance::loadComponentFile (const string& filename) baseParam.runtimeMod = false; baseParam.limits = "1+"; baseParam.description = "Number of instances"; - saveComponentParam (baseParam); + saveParam (baseParam); } // -- USES -- else if (!args[0].compare("uses")) { @@ -385,7 +385,7 @@ nodeIDType TreeMaintenance::loadComponentFile (const string& filename) AttachedChild.runtimeMod = false; AttachedChild.limits = args[4]; AttachedChild.description = ""; - saveComponentParam (AttachedChild); + saveParam (AttachedChild); } // -- PAR -- else if (!args[0].compare("par")) { @@ -415,7 +415,7 @@ nodeIDType TreeMaintenance::loadComponentFile (const string& filename) AttachedChild.runtimeMod = (args[2].find("O",0)) ? true : false; AttachedChild.limits = args[7]; AttachedChild.description = args[9]; - saveComponentParam (AttachedChild); + saveParam (AttachedChild); // TODO: args[8] constraint, args[6] valmoment } // -- UNKNOWN -- diff --git a/SAS/OTDB/test/tVHvalue.cc b/SAS/OTDB/test/tVHvalue.cc index 8c58e0819d154b2e3372ef2905f2f9d334115092..3ba859049407d10c2754189793dd618267e8b5c6 100644 --- a/SAS/OTDB/test/tVHvalue.cc +++ b/SAS/OTDB/test/tVHvalue.cc @@ -33,6 +33,7 @@ #include <OTDB/OTDBtypes.h> #include <OTDB/OTDBnode.h> #include <OTDB/TreeTypeConv.h> +#include <OTDB/TreeStateConv.h> #include <OTDB/ClassifConv.h> using namespace LOFAR; @@ -139,6 +140,7 @@ int main (int argc, char* argv[]) { // Use converters in this testprogram TreeTypeConv TTconv(&conn); + TreeStateConv TSconv(&conn); ClassifConv CTconv(&conn); try { @@ -158,6 +160,29 @@ int main (int argc, char* argv[]) { OTDBtree treeInfo = conn.getTreeInfo(treeID); LOG_INFO_STR(treeInfo); + LOG_INFO("Trying to set the schedule time to 2006-01-20 12:00:00.000"); + LOG_INFO("THIS SHOULD FAIL BECAUSE STATUS IS ACTIVE"); + TreeMaintenance tm(&conn); + try { + tm.setSchedule (treeID, time_from_string("2006-01-20 12:00:00.000"), + time_from_string("2006-01-20 14:53:12.000")); + ASSERTSTR(false, "THIS SHOULD HAVE FAILED"); + } + catch (std::exception& ex) { + LOG_INFO_STR("Caught exception, protection works!"); + } + + LOG_INFO_STR ("Setting tree to scheduled"); + tm.setTreeState(treeID, TSconv.get("scheduled")); + treeInfo = conn.getTreeInfo(treeID); + LOG_INFO_STR(treeInfo); + + LOG_INFO("Retrying to set the schedule time to 2006-01-20 12:00:00"); + tm.setSchedule (treeID, time_from_string("2006-01-20 12:00:00.000"), + time_from_string("2006-01-20 14:53:12.000")); + treeInfo = conn.getTreeInfo(treeID); + LOG_INFO_STR(treeInfo); + LOG_INFO("Trying to construct a TreeValue object"); TreeValue tv(&conn, treeID); @@ -190,7 +215,6 @@ int main (int argc, char* argv[]) { LOG_INFO("Could NOT add the vector of OTDBvalue classes"); } - TreeMaintenance tm(&conn); OTDBnode topNode = tm.getTopNode(treeID); for (int i = 1; i < 4; ++i) { LOG_INFO_STR("searchInPeriod(" << topNode.nodeID() << "," << i diff --git a/SAS/OTDB/test/tVICcomp.cc b/SAS/OTDB/test/tVICcomp.cc index 1cc41c6c98de409d0c403e27fa069c0e3bc59628..b0f3c3a0181dbee68d4eb15f4f0d4234d289cc8f 100644 --- a/SAS/OTDB/test/tVICcomp.cc +++ b/SAS/OTDB/test/tVICcomp.cc @@ -249,7 +249,7 @@ int main (int argc, char* argv[]) { LOG_INFO("Modifying parameter '%nrInstances' to 1..5"); testPar.limits = "1..5"; - ASSERTSTR (tm.saveComponentParam(testPar), "update of parameter failed"); + ASSERTSTR (tm.saveParam(testPar), "update of parameter failed"); testPar = tm.getParam(0, pref); LOG_INFO_STR(testPar); diff --git a/SAS/OTDB/test/tVTtree.cc b/SAS/OTDB/test/tVTtree.cc index f73d6723b474ba73bac449b1320f566e0b2bc0f5..6696b28ebce29506a2a13e0100d73524936b6ea8 100644 --- a/SAS/OTDB/test/tVTtree.cc +++ b/SAS/OTDB/test/tVTtree.cc @@ -148,6 +148,11 @@ int main (int argc, char* argv[]) { LOG_INFO_STR ("Using tree " << VTtreeID << " for the tests"); OTDBtree treeInfo = conn.getTreeInfo(VTtreeID); LOG_INFO_STR(treeInfo); + + LOG_INFO ("Changing the description to 'test_tree'"); + tm.setDescription(VTtreeID, "test_tree"); + treeInfo = conn.getTreeInfo(VTtreeID); + LOG_INFO_STR(treeInfo); LOG_INFO("Trying to get the topnode of the tree"); OTDBnode topNode = tm.getTopNode(VTtreeID);