#pragma once ///////////////////////////////////////////////////////////////////// // Display.h - display NoSqlDb contents // // ver 1.0 // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2017 // ///////////////////////////////////////////////////////////////////// /* * Package Operations: * ------------------- * This package defines a single class, Display that shows contents * of an instance of NoSqlDb. It accepts a reference to a NoSqlDb * database from which it displays records. * * I plan to replace the reference with a pointer so that the database can * be changed without creating a new instance of Display. * * Required Files: * --------------- * Display.h, Display.cpp, * NoSqlDb.h, NoSqlDb.cpp, CppProperties.h, CppProperties.cpp * * Maintenance History: *--------------------- * ver 1.0 : 06 Feb 2017 * - first release */ #include "../NoSqlDb/NoSqlDb.h" #include "../TestTypes/TestTypes.h" #include "../Utilities/Utilities.h" #include #include namespace NoSQLDB { template class Display { public: enum Style { summary, detailed }; Display(NoSqlDb& db) : db_(db) {} void show(Key key, Style style = summary, size_t fieldwidth = 10, std::ostream& out = std::cout); void showAll(Style style = summary, size_t fieldwidth = 10, std::ostream& out = std::cout); void showHeader(Style style = summary, size_t fieldwidth = 10, std::ostream& out = std::cout); void showQuery(Keys keys, Style style = summary, size_t fieldwidth = 10, std::ostream& out = std::cout); private: void showSummary(Key key, size_t fieldwidth, std::ostream& out); void showDetails(Key key, size_t fieldwidth, std::ostream& out); NoSqlDb& db_; }; //----< show single record in one-line format >---------------------- template void Display::showSummary(Key key, size_t fieldwidth, std::ostream& out) { if (!db_.hasKey(key)) return; Element elem = db_.value(key); int save = out.setf(std::ios::left, std::ios::adjustfield); out << " "; out << std::setw(fieldwidth) << key.substr(0, fieldwidth) << " "; out << std::setw(fieldwidth) << static_cast(elem.name).substr(0, fieldwidth) << " "; out << std::setw(fieldwidth) << static_cast(elem.category).substr(0, fieldwidth) << " "; DateTime dt = elem.dateTime; out << std::setw(fieldwidth) << static_cast(dt).substr(0, fieldwidth) << " "; std::string temp = Utilities::Convert::toString(elem.data.getValue()); out << std::setw(fieldwidth) << temp.substr(0, fieldwidth) << " "; if (elem.children.getValue().size() > 0) { for (Key k : elem.children.getValue()) out << std::setw(fieldwidth) << static_cast(k).substr(0, fieldwidth) << " "; } out << "\n"; out.setf(save); } //----< show single record with detailed, multi-line, format >------- template void Display::showDetails(Key key, size_t fieldwidth, std::ostream& out) { if (!db_.hasKey(key)) return; Element elem = db_.value(key); int save = out.setf(std::ios::left, std::ios::adjustfield); out << " "; out << "key : " << key << "\n "; out << "Name : " << static_cast(elem.name) << "\n "; out << "Category : " << static_cast(elem.category) << "\n "; DateTime dt = elem.dateTime; out << "DateTime : " << static_cast(dt) << "\n "; out << "Data : " << elem.data.getValue() << "\n "; out << "Children :\n "; if (elem.children.getValue().size() > 0) { for (Key k : elem.children.getValue()) out << std::setw(fieldwidth) << static_cast(k).substr(0, fieldwidth) << " "; } out << "\n"; out.setf(save); } //----< show single record >----------------------------------------- /* * Style: Display::summary or Display::detailed * fieldwidth: number of characters for each field * out: stream for output - typically std::cout (default) or std::ostringstream */ template void Display::show(Key key, Style style, size_t fieldwidth, std::ostream& out) { if (!db_.hasKey(key)) return; if (style == Display::Style::summary) showSummary(key, fieldwidth, out); else showDetails(key, fieldwidth, out); } //----< show all records >------------------------------------------- /* * Style: Display::summary or Display::detailed * fieldwidth: number of characters for each field * out: stream for output - typically std::cout (default) or std::ostringstream */ template void Display::showAll(Style style, size_t fieldwidth, std::ostream& out) { Keys keys = db_.keys(); if (style == Display::Style::summary) { showHeader(style, fieldwidth, out); for (Key k : keys) showSummary(k, fieldwidth, out); } else { for (Key k : keys) showDetails(k, fieldwidth, out); } } //----< show all records in query >---------------------------------- /* * Style: Display::summary or Display::detailed * fieldwidth: number of characters for each field * out: stream for output - typically std::cout (default) or std::ostringstream */ template void Display::showQuery(Keys keys, Style style, size_t fieldwidth, std::ostream& out) { if (style == Display::Style::summary) { showHeader(style, fieldwidth, out); for (Key k : keys) showSummary(k, fieldwidth, out); } else { for (Key k : keys) showDetails(k, fieldwidth, out); } } //----< shows underlined names of each field for summary output >---- template void Display::showHeader(Style style, size_t fieldwidth, std::ostream& out) { size_t numChildren = 0; Keys keys = db_.keys(); for (Key k : keys) { size_t size = db_.value(k).children.getValue().size(); if (numChildren < size) numChildren = size; } int save = out.setf(std::ios::left, std::ios::adjustfield); out << " "; out << std::setw(fieldwidth) << std::string("Key").substr(0, fieldwidth) << " "; out << std::setw(fieldwidth) << std::string("Name").substr(0, fieldwidth) << " "; out << std::setw(fieldwidth) << std::string("Category").substr(0, fieldwidth) << " "; out << std::setw(fieldwidth) << std::string("TimeDate").substr(0, fieldwidth) << " "; out << std::setw(fieldwidth) << std::string("Data").substr(0, fieldwidth) << " "; if (numChildren > 0) { for (size_t i = 0; i < numChildren; ++i) out << std::setw(fieldwidth) << std::string("Child").substr(0, fieldwidth) << " "; } out << "\n "; for (size_t i = 0; i < numChildren + 5; ++i) out << std::setw(fieldwidth) << std::string("----------").substr(0, fieldwidth) << " "; out << "\n"; out.setf(save); } }