1 #ifndef _ots_ConfigurationView_h_
2 #define _ots_ConfigurationView_h_
4 #include "otsdaq-core/ConfigurationDataFormats/ViewColumnInfo.h"
5 #include "otsdaq-core/MessageFacility/MessageFacility.h"
6 #include "otsdaq-core/Macros/CoutMacros.h"
7 #include "otsdaq-core/Macros/StringMacros.h"
8 #include "otsdaq-core/ConfigurationDataFormats/ConfigurationVersion.h"
25 static const unsigned int INVALID;
26 typedef std::vector<std::vector<std::string> > DataView;
27 typedef DataView::iterator iterator;
28 typedef DataView::const_iterator const_iterator;
41 unsigned int findRow(
unsigned int col,
const T& value,
42 unsigned int offsetRow=0)
const
44 std::istringstream s(value);
45 return findRow(col,s.str(),offsetRow);
47 unsigned int findRow (
unsigned int col,
const std::string& value,
unsigned int offsetRow=0)
const;
50 unsigned int findRowInGroup(
unsigned int col,
const T& value,
51 const std::string &groupId,
const std::string &childLinkIndex,
unsigned int offsetRow=0)
const
53 std::istringstream s(value);
54 return findRowInGroup(col,s.str(),groupId,childLinkIndex,offsetRow);
56 unsigned int findRowInGroup (
unsigned int col,
const std::string &value,
const std::string &groupId,
const std::string &childLinkIndex,
unsigned int offsetRow=0)
const;
57 unsigned int findCol (
const std::string &name)
const;
58 unsigned int findColByType (
const std::string &type,
int startingCol = 0)
const;
61 const std::string& getUniqueStorageIdentifier (
void)
const;
62 const std::string& getTableName (
void)
const;
64 const std::string& getComment (
void)
const;
65 const std::string& getAuthor (
void)
const;
66 const time_t& getCreationTime (
void)
const;
67 const time_t& getLastAccessTime (
void)
const;
68 const bool& getLooseColumnMatching (
void)
const;
69 const unsigned int getDataColumnSize (
void)
const;
70 const unsigned int& getSourceColumnMismatch (
void)
const;
71 const unsigned int& getSourceColumnMissing (
void)
const;
72 const std::set<std::string>& getSourceColumnNames (
void)
const;
73 std::set<std::string> getColumnNames (
void)
const;
74 std::set<std::string> getColumnStorageNames (
void)
const;
75 std::vector<std::string> getDefaultRowValues (
void)
const;
77 unsigned int getNumberOfRows (
void)
const;
78 unsigned int getNumberOfColumns (
void)
const;
79 const unsigned int getColUID (
void)
const;
80 const unsigned int getColStatus (
void)
const;
81 const unsigned int getColPriority (
void)
const;
87 bool isEntryInGroupCol (
const unsigned int& row,
const unsigned int& groupCol,
const std::string& groupNeedle, std::set<std::string>* groupIDList = 0)
const;
90 std::set<std::string> getSetOfGroupIDs (
const std::string& childLinkIndex,
unsigned int row = -1)
const;
91 bool isEntryInGroup (
const unsigned int& row,
const std::string& childLinkIndex,
const std::string& groupNeedle)
const;
92 const bool getChildLink (
const unsigned int& col,
bool& isGroup, std::pair<unsigned int /*link col*/, unsigned int /*link id col*/>& linkPair)
const;
93 const unsigned int getColLinkGroupID (
const std::string& childLinkIndex)
const;
94 void addRowToGroup (
const unsigned int& row,
const unsigned int& col,
const std::string& groupID);
95 bool removeRowFromGroup (
const unsigned int& row,
const unsigned int& col,
const std::string& groupID,
bool deleteRowIfNoGroupLeft=
false);
102 void getValue(T& value,
unsigned int row,
unsigned int col,
bool doConvertEnvironmentVariables=
true)
const
104 if(!(col < columnsInfo_.size() && row < getNumberOfRows()))
106 __SS__ <<
"Invalid row col requested" << std::endl;
107 __COUT_ERR__ <<
"\n" << ss.str();
108 throw std::runtime_error(ss.str());
111 value = validateValueForColumn<T>(theDataView_[row][col],col,doConvertEnvironmentVariables);
115 void getValue(std::string& value,
unsigned int row,
unsigned int col,
bool doConvertEnvironmentVariables=
true)
const;
124 T validateValueForColumn(
const std::string& value,
unsigned int col,
125 bool doConvertEnvironmentVariables=
true)
const
127 if(col >= columnsInfo_.size())
129 __SS__ <<
"Invalid col " << col <<
" requested." << std::endl;
137 if(columnsInfo_[col].getDataType() == ViewColumnInfo::DATATYPE_NUMBER)
139 std::string data = doConvertEnvironmentVariables?StringMacros::convertEnvironmentVariables(value):
142 if(StringMacros::getNumber(data,retValue))
146 __SS__ << (data +
" is not a number!") << __E__;
150 else if(columnsInfo_[col].getType() == ViewColumnInfo::TYPE_FIXED_CHOICE_DATA &&
151 columnsInfo_[col].getDataType() == ViewColumnInfo::DATATYPE_STRING &&
152 (
typeid(int) ==
typeid(retValue) ||
153 typeid(
unsigned int) ==
typeid(retValue)))
160 if(value == ViewColumnInfo::DATATYPE_STRING_DEFAULT)
164 std::vector<std::string> choices = columnsInfo_[col].getDataChoices();
170 bool skipOne = (choices.size() &&
171 choices[0].find(
"arbitraryBool=") == 0);
173 for(retValue=1 + (skipOne?1:0);retValue-1<(T)choices.size();++retValue)
174 if(value == choices[retValue-1])
175 return retValue - (skipOne?1:0);
177 __SS__ <<
"\tInvalid value for column data type: " << columnsInfo_[col].getDataType()
178 <<
" in configuration " << tableName_
179 <<
" at column=" << columnsInfo_[col].getName()
180 <<
" for getValue with type '" << StringMacros::demangleTypeName(
typeid(retValue).name())
182 <<
"Attempting to get index of '" << value
183 <<
" in fixed choice array, but was not found in array. "
184 <<
"Here are the valid choices:\n";
185 ss <<
"\t" << ViewColumnInfo::DATATYPE_STRING_DEFAULT <<
"\n";
186 for(
const auto &choice:choices)
187 ss <<
"\t" << choice <<
"\n";
188 __COUT__ <<
"\n" << ss.str();
189 throw std::runtime_error(ss.str());
194 else if(columnsInfo_[col].getDataType() == ViewColumnInfo::DATATYPE_STRING &&
195 typeid(bool) ==
typeid(retValue))
197 if(columnsInfo_[col].getType() == ViewColumnInfo::TYPE_ON_OFF)
198 retValue = (value == ViewColumnInfo::TYPE_VALUE_ON) ?
true:
false;
199 else if(columnsInfo_[col].getType() == ViewColumnInfo::TYPE_TRUE_FALSE)
200 retValue = (value == ViewColumnInfo::TYPE_VALUE_TRUE) ?
true:
false;
201 else if(columnsInfo_[col].getType() == ViewColumnInfo::TYPE_YES_NO)
202 retValue = (value == ViewColumnInfo::TYPE_VALUE_YES) ?
true:
false;
203 else if(value.length() && value[0] ==
'1')
205 else if(value.length() && value[0] ==
'0')
209 __SS__ <<
"Invalid boolean value encountered: " << value << __E__;
215 else if(columnsInfo_[col].getDataType() == ViewColumnInfo::DATATYPE_STRING &&
216 typeid(std::string) !=
typeid(retValue))
218 return StringMacros::validateValueForDefaultStringDataType<T>(value,doConvertEnvironmentVariables);
222 __SS__ <<
"Impossible Error." << __E__;
225 catch(
const std::runtime_error& e)
227 __SS__ <<
"\tUnrecognized column data type: " << columnsInfo_[col].getDataType()
228 <<
" and column type: " << columnsInfo_[col].getType()
229 <<
", in configuration " << tableName_
230 <<
" at column=" << columnsInfo_[col].getName()
231 <<
" for getValue with type '" << StringMacros::demangleTypeName(
typeid(retValue).name())
234 if(columnsInfo_[col].getType() == ViewColumnInfo::TYPE_FIXED_CHOICE_DATA)
235 ss <<
"For column type " << ViewColumnInfo::TYPE_FIXED_CHOICE_DATA
236 <<
" the only valid numeric types are 'int' and 'unsigned int.'" << __E__;
238 ss << e.what() << __E__;
244 std::string validateValueForColumn(
const std::string& value,
unsigned int col,
bool convertEnvironmentVariables=
true)
const;
246 std::string getValueAsString(
unsigned int row,
unsigned int col,
bool convertEnvironmentVariables=
true)
const;
247 std::string getEscapedValueAsString(
unsigned int row,
unsigned int col,
bool convertEnvironmentVariables=
true)
const;
248 bool isURIEncodedCommentTheSame(
const std::string &comment)
const;
250 const DataView& getDataView (
void)
const;
251 const std::vector<ViewColumnInfo>& getColumnsInfo (
void)
const;
252 std::vector<ViewColumnInfo>* getColumnsInfoP(
void);
257 void setUniqueStorageIdentifier (
const std::string &storageUID );
258 void setTableName (
const std::string &name );
259 void setComment (
const std::string &comment );
260 void setURIEncodedComment (
const std::string &uriComment );
261 void setAuthor (
const std::string &author );
262 void setCreationTime (time_t t );
263 void setLastAccessTime (time_t t = time(0) );
264 void setLooseColumnMatching (
bool setValue );
268 void setVersion (
const T &version)
277 void setValue(
const T &value,
unsigned int row,
unsigned int col)
279 if(!(col < columnsInfo_.size() && row < getNumberOfRows()))
281 __SS__ <<
"Invalid row (" << row <<
") col (" << col <<
") requested!" << std::endl;
282 throw std::runtime_error(ss.str());
285 if(columnsInfo_[col].getDataType() == ViewColumnInfo::DATATYPE_NUMBER)
287 std::stringstream ss;
289 theDataView_[row][col] = ss.str();
291 else if(columnsInfo_[col].getDataType() == ViewColumnInfo::DATATYPE_TIME &&
292 typeid(time_t) ==
typeid(value))
295 std::stringstream ss;
297 theDataView_[row][col] = ss.str();
301 __SS__ <<
"\tUnrecognized view data type: " << columnsInfo_[col].getDataType()
302 <<
" in configuration " << tableName_
303 <<
" at column=" << columnsInfo_[col].getName()
304 <<
" for setValue with type '" << StringMacros::demangleTypeName(
typeid(value).name())
306 throw std::runtime_error(ss.str());
309 void setValue (
const std::string &value,
unsigned int row,
unsigned int col);
310 void setValue (
const char *value,
unsigned int row,
unsigned int col);
313 void setValueAsString (
const std::string &value,
unsigned int row,
unsigned int col);
316 void resizeDataView (
unsigned int nRows,
unsigned int nCols);
317 int addRow (
const std::string &author =
"",
bool incrementUniqueData =
false, std::string baseNameAutoUID =
"");
318 void deleteRow (
int r);
323 iterator begin (
void) {
return theDataView_.begin();}
324 iterator end (
void) {
return theDataView_.end();}
325 const_iterator begin (
void)
const {
return theDataView_.begin();}
326 const_iterator end (
void)
const {
return theDataView_.end();}
328 void print (std::ostream &out = std::cout)
const;
329 void printJSON (std::ostream &out = std::cout)
const;
330 int fillFromJSON (
const std::string &json);
331 int fillFromCSV (
const std::string &data,
const int &dataOffset = 0,
const std::string &author =
"")
throw(std::runtime_error);
332 bool setURIEncodedValue (
const std::string &value,
const unsigned int &row,
const unsigned int &col,
const std::string &author =
"");
336 const unsigned int getOrInitColUID (
void);
337 const unsigned int getOrInitColStatus (
void);
338 const unsigned int getOrInitColPriority (
void);
342 std::string uniqueStorageIdentifier_;
343 std::string tableName_ ;
345 std::string comment_ ;
346 std::string author_ ;
347 time_t creationTime_ ;
348 time_t lastAccessTime_ ;
349 unsigned int colUID_, colStatus_, colPriority_;
350 std::map<std::string, unsigned int> colLinkGroupIDs_;
352 bool fillWithLooseColumnMatching_;
353 unsigned int sourceColumnMismatchCount_, sourceColumnMissingCount_;
354 std::set<std::string> sourceColumnNames_;
356 std::vector<ViewColumnInfo> columnsInfo_ ;
357 DataView theDataView_ ;