00001 #ifndef SCT_CONFIGURATION_DALUTILS_H
00002 #define SCT_CONFIGURATION_DALUTILS_H
00003
00004 #include <vector>
00005 #include <config/Configuration.h>
00006
00007 namespace SCTDAL {
00008 class Barrel;
00009 class Row;
00010 class GeographyMUR;
00011
00012 class Partition;
00013 class Crate;
00014 class Rod;
00015 class RodMUR;
00016 class Channel;
00017
00018 class RodRModule;
00019 class SCT_Module;
00020
00021 class PowerCard;
00022 class PowerCrate;
00023 class PowerChannel;
00024 class PowerParam;
00025 }
00026
00027 class Log;
00028
00029 class ConfigurationDALImpl;
00030
00031 namespace ConfDALUtils {
00032 template <typename T> void printVector(std::vector<const T*> objects) {
00033 typedef std::vector<const T*> TVector;
00034
00035 if(objects.size() == 0) {
00036 std::cerr << "Printing empty vector\n";
00037 }
00038 for(typename TVector::iterator i = objects.begin(); i != objects.end(); i++) {
00039 (*i)->print(2, true, std::cerr);
00040 }
00041 }
00042
00043 class Builder {
00044 ::Configuration &m_conf;
00045 ConfigurationDALImpl &m_impl;
00046 Log &log;
00047 public:
00048 Builder(::Configuration &conf, ConfigurationDALImpl &impl, Log &log) : m_conf(conf), m_impl(impl), log(log) {}
00049
00050
00051 SCTDAL::Partition *getOrCreatePartition(unsigned int partition);
00052 SCTDAL::Crate *getOrCreateCrate(unsigned int partition, unsigned int crate);
00053 SCTDAL::Rod *getOrCreateRod(unsigned int partition, unsigned int crate, unsigned int rod);
00054
00055
00056 SCTDAL::Barrel *getOrCreateBarrel(unsigned int barrel);
00057 SCTDAL::Row *getOrCreateBarrelRow(unsigned int barrel, unsigned int row);
00058
00059
00060 SCTDAL::PowerCrate *getOrCreatePowerCrate(unsigned int crate);
00061 SCTDAL::PowerChannel *getOrCreatePowerChannel(unsigned int crate, unsigned int channel);
00062
00063
00064 SCTDAL::Channel *getOrCreateChannelInRodMur(SCTDAL::RodMUR *murObj, int position);
00065 SCTDAL::PowerParam *getOrCreatePowerParamInChannel(SCTDAL::PowerChannel *chanObj, std::string state, std::string name);
00066 };
00067
00068 class LookUp {
00069 ::Configuration &m_conf;
00070 ConfigurationDALImpl &m_impl;
00071 Log &log;
00072 public:
00073 LookUp(::Configuration &conf, ConfigurationDALImpl &impl, Log &log) : m_conf(conf), m_impl(impl), log(log) {}
00074
00075 const SCTDAL::Partition *getPartition(unsigned int partition);
00076 const SCTDAL::Crate *getCrate(unsigned int partition, unsigned int crate);
00077 const SCTDAL::Rod *getRod(unsigned int partition, unsigned int crate, unsigned int rod);
00078 const SCTDAL::Barrel *getBarrel(unsigned int barrel);
00079 const SCTDAL::Row *getBarrelRow(unsigned int barrel, unsigned int row);
00080 const SCTDAL::RodMUR *getMur(unsigned int MUR);
00081 const SCTDAL::GeographyMUR *getGeogMur(unsigned int MUR);
00082 const SCTDAL::SCT_Module *getModule(const std::string sn);
00083
00084 const SCTDAL::PowerCrate *getPowerCrate(unsigned int crate);
00085 const SCTDAL::PowerChannel *getPowerChannel(unsigned int crate, unsigned int channel);
00086 const SCTDAL::PowerChannel *getPowerChannelByMUR(unsigned int MUR, unsigned int number);
00087
00088 const SCTDAL::RodMUR *getMurInRod(const SCTDAL::Rod *rodObj, int position);
00089 const SCTDAL::Channel *getChannelInRodMur(const SCTDAL::RodMUR *murObj, int position);
00090 const SCTDAL::RodRModule *getRModuleInRodMur(const SCTDAL::RodMUR *murObj, int position);
00091 const SCTDAL::PowerParam *getPowerParamInChannel(const SCTDAL::PowerChannel *chanObj, std::string state, std::string name);
00092 const SCTDAL::PowerCard *getPowerCardInCrate(const SCTDAL::PowerCrate *crate, unsigned int card, std::string type);
00093
00094 template<class T, class V> bool getSingleBackReference(const T &object, std::string relationship, const V *&relatedObj);
00095
00096 template<class T> const T *queryViaRelationship(std::string relName, std::string attrName, std::string attrValue);
00097 };
00098
00099 template<class T, class V> bool LookUp::getSingleBackReference(const T &object, std::string relationship, const V *&relatedObj) {
00100 std::vector<const V *> possibleObjs;
00101
00102 m_conf.referenced_by(object, relationship, possibleObjs);
00103
00104 if(possibleObjs.size() == 1) {
00105 relatedObj = possibleObjs.front();
00106 if(relatedObj == 0) {
00107 return false;
00108 } else {
00109 return true;
00110 }
00111 } else {
00112 return false;
00113 }
00114 }
00115
00116 template<class T> const T *LookUp::queryViaRelationship(std::string relName, std::string attrName, std::string attrValue) {
00117 std::string query("(this (\"" + relName + "\" some (\"" + attrName + "\" " + attrValue + " =)))");
00118
00119 std::cerr << "executing query: " << query << std::endl;
00120
00121 std::vector<const T *> possibles;
00122 m_conf.template get<T>(possibles, false, true, query);
00123
00124 const T *queryObj = 0;
00125 if(possibles.size() == 1) {
00126 queryObj = possibles.front();
00127 std::cerr << "Found 1 object:\n";
00128
00129 } else {
00130 std::cerr << "Found other objects\n";
00131 printVector(possibles);
00132 }
00133
00134 return queryObj;
00135 }
00136 }
00137
00138 #endif