LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
PropertySetFormatter.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 
26 
38 #ifndef __GNUC__
39 # define __attribute__(x) /*NOTHING*/
40 #endif
41 static char const* SVNid __attribute__((unused)) = "$Id$";
42 
44 
45 #include <sstream>
46 #include <stdexcept>
47 #include <string>
48 #include <vector>
49 
50 #include <boost/serialization/nvp.hpp>
51 #include <boost/serialization/shared_ptr.hpp>
52 #include <boost/serialization/vector.hpp>
53 
55 #include "lsst/daf/base/DateTime.h"
61 #include <lsst/pex/exceptions.h>
62 #include <lsst/pex/logging/Trace.h>
63 #include <lsst/pex/policy/Policy.h>
64 
65 
66 #define EXEC_TRACE 20
67 static void execTrace(std::string s, int level = EXEC_TRACE) {
68  lsst::pex::logging::Trace("daf.persistence.PropertySetFormatter", level, s);
69 }
70 
71 namespace dafBase = lsst::daf::base;
72 namespace dafPersist = lsst::daf::persistence;
73 namespace pexPolicy = lsst::pex::policy;
74 
75 using boost::serialization::make_nvp;
76 
82  typeid(dafBase::PropertySet),
83  createInstance);
84 
89  pexPolicy::Policy::Ptr policy) :
90  dafPersist::Formatter(typeid(*this)), _policy(policy) {
91 }
92 
96 }
97 
99  dafBase::Persistable const* persistable,
100  dafPersist::Storage::Ptr storage,
101  dafBase::PropertySet::Ptr additionalData) {
102  execTrace("PropertySetFormatter write start");
103  dafBase::PropertySet const* ps =
104  dynamic_cast<dafBase::PropertySet const*>(persistable);
105  if (ps == 0) {
106  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Persisting non-PropertySet");
107  }
108  if (typeid(*storage) == typeid(dafPersist::BoostStorage)) {
109  execTrace("PropertySetFormatter write BoostStorage");
110  dafPersist::BoostStorage* boost =
111  dynamic_cast<dafPersist::BoostStorage*>(storage.get());
112  boost->getOArchive() & *ps;
113  execTrace("PropertySetFormatter write end");
114  return;
115  }
116  else if (typeid(*storage) == typeid(dafPersist::XmlStorage)) {
117  execTrace("PropertySetFormatter write XmlStorage");
119  dynamic_cast<dafPersist::XmlStorage*>(storage.get());
120  xml->getOArchive() & make_nvp("propertySet", *ps);
121  execTrace("PropertySetFormatter write end");
122  return;
123  }
124  else if (typeid(*storage) == typeid(dafPersist::DbStorage)) {
125  execTrace("PropertySetFormatter write DbStorage");
127  dynamic_cast<dafPersist::DbStorage*>(storage.get());
128 
129  std::string itemName = additionalData->getAsString("itemName");
130  std::string tableName = itemName;
131  pexPolicy::Policy::Ptr itemPolicy;
132  if (_policy && _policy->exists(itemName)) {
133  itemPolicy = _policy->getPolicy(itemName);
134  if (itemPolicy->exists("TableName")) {
135  tableName = itemPolicy->getString("TableName");
136  }
137  }
138  db->setTableForInsert(tableName);
139 
140  std::vector<std::string> list;
141  if (itemPolicy && itemPolicy->exists("KeyList")) {
142  pexPolicy::Policy::StringArray const& array(
143  itemPolicy->getStringArray("KeyList"));
144  for (pexPolicy::Policy::StringArray::const_iterator it =
145  array.begin(); it != array.end(); ++it) {
146  list.push_back(*it);
147  }
148  }
149  else {
150  list = ps->paramNames(false);
151  }
152 
153  for (std::vector<std::string>::const_iterator it = list.begin();
154  it != list.end(); ++it) {
155  std::string::size_type split = it->find('=');
156  std::string colName;
157  std::string key;
158  if (split == std::string::npos) {
159  colName = key = *it;
160  }
161  else {
162  colName = it->substr(0, split);
163  key = it->substr(split + 1);
164  }
165 
166  if (!ps->exists(key)) {
167  db->setColumnToNull(colName);
168  continue;
169  }
170 
171  std::type_info const& type(ps->typeOf(key));
172 
173  if (type == typeid(bool)) {
174  db->setColumn<bool>(colName, ps->get<bool>(key));
175  }
176  else if (type == typeid(char)) {
177  db->setColumn<char>(colName, ps->get<char>(key));
178  }
179  else if (type == typeid(short)) {
180  db->setColumn<short>(colName, ps->get<short>(key));
181  }
182  else if (type == typeid(int)) {
183  db->setColumn<int>(colName, ps->get<int>(key));
184  }
185  else if (type == typeid(long)) {
186  db->setColumn<long>(colName, ps->get<long>(key));
187  }
188  else if (type == typeid(long long)) {
189  db->setColumn<long long>(colName, ps->get<long long>(key));
190  }
191  else if (type == typeid(float)) {
192  db->setColumn<float>(colName, ps->get<float>(key));
193  }
194  else if (type == typeid(double)) {
195  db->setColumn<double>(colName, ps->get<double>(key));
196  }
197  else if (type == typeid(std::string)) {
198  db->setColumn<std::string>(colName, ps->get<std::string>(key));
199  }
200  else if (type == typeid(dafBase::DateTime)) {
202  colName, ps->get<dafBase::DateTime>(key));
203  }
204  else {
205  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
206  std::string("Unknown type ") + type.name() +
207  " in PropertySetFormatter write");
208  }
209  }
210  db->insertRow();
211  execTrace("PropertySetFormatter write end");
212  return;
213  }
214 
215  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized Storage for PropertySet");
216 }
217 
219  dafPersist::Storage::Ptr storage, dafBase::PropertySet::Ptr additionalData) {
220  execTrace("PropertySetFormatter read start");
222  if (typeid(*storage) == typeid(dafPersist::BoostStorage)) {
223  execTrace("PropertySetFormatter read BoostStorage");
224  dafPersist::BoostStorage* boost =
225  dynamic_cast<dafPersist::BoostStorage*>(storage.get());
226  boost->getIArchive() & *ps;
227  execTrace("PropertySetFormatter read end");
228  return ps;
229  }
230  else if (typeid(*storage) == typeid(dafPersist::XmlStorage)) {
231  execTrace("PropertySetFormatter read XmlStorage");
233  dynamic_cast<dafPersist::XmlStorage*>(storage.get());
234  xml->getIArchive() & make_nvp("propertySet", *ps);
235  execTrace("PropertySetFormatter read end");
236  return ps;
237  }
238  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized Storage for PropertySet");
239 }
240 
242  dafPersist::Storage::Ptr storage,
243  dafBase::PropertySet::Ptr additionalData) {
244  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unexpected call to update for PropertySet");
245 }
246 
252  pexPolicy::Policy::Ptr policy) {
254 }
std::vector< std::string > paramNames(bool topLevelOnly=true) const
Definition: PropertySet.cc:142
Class for XML file storage.
Definition: XmlStorage.h:57
Class for handling dates/times, including MJD, UTC, and TAI.
Definition: DateTime.h:58
boost::shared_ptr< Formatter > Ptr
Definition: Formatter.h:81
std::vector< std::string > StringArray
Definition: Policy.h:181
Interface for DbStorage class.
void setColumn(std::string const &columnName, T const &value)
Definition: DbStorage.cc:152
PropertySetFormatter(pexPolicy::Policy::Ptr policy)
virtual boost::archive::text_iarchive & getIArchive(void)
boost::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:90
definition of the Trace messaging facilities
boost::shared_ptr< Policy > Ptr
Definition: Policy.h:172
limited backward compatibility to the DC2 run-time trace facilities
Definition: Trace.h:93
Construct a static instance of this helper class to register a Formatter subclass in the FormatterReg...
Definition: Formatter.h:138
virtual void update(dafBase::Persistable *persistable, dafPersist::Storage::Ptr storage, dafBase::PropertySet::Ptr additionalData)
virtual boost::archive::xml_iarchive & getIArchive(void)
Definition: XmlStorage.cc:114
Class for database storage.
Definition: DbStorage.h:63
lsst::daf::base::PropertySet PropertySet
Definition: Wcs.cc:58
Interface for XmlStorage class.
#define EXEC_TRACE
Interface for PropertySetFormatter class.
T get(std::string const &name) const
Definition: PropertySet.cc:246
Abstract base class for all formatters.
Definition: Formatter.h:79
Formatter for persistence of PropertySet instances.
virtual void insertRow(void)
Definition: DbStorage.cc:166
Interface for DateTime class.
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
virtual boost::archive::text_oarchive & getOArchive(void)
Auxiliary global template function for Formatter subclasses.
virtual boost::archive::xml_oarchive & getOArchive(void)
Definition: XmlStorage.cc:107
std::type_info const & typeOf(std::string const &name) const
Definition: PropertySet.cc:227
boost::shared_ptr< Storage > Ptr
Definition: Storage.h:62
Class for storing generic metadata.
Definition: PropertySet.h:82
Class for boost::serialization storage.
Definition: BoostStorage.h:58
Interface for PropertySet class.
Base class for all persistable classes.
Definition: Persistable.h:74
#define __attribute__(x)
virtual dafBase::Persistable * read(dafPersist::Storage::Ptr storage, dafBase::PropertySet::Ptr additionalData)
Interface for LogicalLocation class.
virtual void setTableForInsert(std::string const &tableName)
Definition: DbStorage.cc:143
virtual void write(dafBase::Persistable const *persistable, dafPersist::Storage::Ptr storage, dafBase::PropertySet::Ptr additionalData)
static Formatter::Ptr createInstance(pexPolicy::Policy::Ptr policy)
bool exists(std::string const &name) const
Definition: PropertySet.cc:190
virtual void setColumnToNull(std::string const &columnName)
Definition: DbStorage.cc:159
Include files required for standard LSST Exception handling.
Interface for BoostStorage class.