Wt::Dbo::ptr<User> Session::user(const Wt::Auth::User& authUser) { Wt::Dbo::ptr<AuthInfo> authInfo = users_->find(authUser); Wt::Dbo::ptr<User> user = authInfo->user(); if (!user) { user = addNew<User>(); authInfo.modify()->setUser(user); } return user; }
virtual void handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) { int n; if (const std::string *queries = request.getParameter("queries")) { n = atoi(queries->c_str()); if (n < 1) n = 1; else if (n > 500) n = 500; } else { n = 1; } response.setMimeType("application/json"); response.addHeader("Server", "Wt"); DbStruct* db = dbStruct_.get(); if (!db) { db = new DbStruct(); dbStruct_.reset(db); } std::vector<Wt::Dbo::ptr<World> > results; for (int i = 0; i < n; ++i) { bool success = false; while (!success) { try { Wt::Dbo::Transaction transaction(db->session); Wt::Dbo::ptr<World> world = db->session.load<World>(db->rand()); world.modify()->randomNumber = db->rand(); transaction.commit(); results.push_back(world); success = true; } catch (Wt::Dbo::Exception& e) { // Retry } } } Wt::Dbo::JsonSerializer writer(response.out()); writer.serialize(results); }
void ChamadaForm::_save(){ Wt::Dbo::Transaction transaction(_dbSession); if(_aula){ std::cout << "Aula set: updating..." << std::endl; for(auto i : sortList){ Wt::Dbo::ptr<SiconfModel::Frequencia> frequencia = _dbSession.find<SiconfModel::Frequencia>("where frequencias_aula_id = ? and frequencias_aluno_id = ?").bind(_aula.id()).bind(i.second.id()); if(frequencia){ frequencia.modify()->definirPresenca(i.first->isChecked()); }else{ std::cout << "ChamadaForm::Save() ........... Frequencia not found" << std::endl; } } }else{ std::cout << "Aula not set: creating..." << std::endl; _aula = _dbSession.add(new SiconfModel::Aula(_disciplina)); for(auto i : sortList){ _dbSession.add(new SiconfModel::Frequencia(_aula, i.second, i.first->isChecked())); } } transaction.commit(); }
/* **************************************************************************** * Menu Man Session * Check Database for existing records and delete them * Read in XML file and populate Database * <menuman name="" path="" type="" parent="" grandparent="" language="" content="" description="" keywords="" title="" copyright="" rating=""></menuman> */ bool MenuManSession::ImportXML() { Wt::log("start") << " *** MenuManSession::ImportXML() *** "; std::string fullFilePath = appPath_ + "db/menuman-db-import.xml"; // FIXME: Multilanguage support, _cn.xml for chinese, make a loop /* */ std::vector<std::string> myDir; CrystalBall::GetFilesInDirectory(myDir, appPath_ + "db"); for (std::vector<std::string>::iterator it = myDir.begin() ; it != myDir.end(); ++it) { Wt::log("notice") << "File = " << *it; fullFilePath = *it; if (CrystalBall::IsFile(fullFilePath)) { Wt::log("info") << "MenuManSession::ImportXML: " << fullFilePath; } else { Wt::log("error") << "-> Missing XML Configuration File MenuManSession::ImportXML: " << fullFilePath; return false; } try { // Open XML File const char *filePath = fullFilePath.c_str(); rapidxml::file<> xmlFile(filePath); rapidxml::xml_document<> xmlDoc; xmlDoc.parse<0>(xmlFile.data()); // Find our root node /* * <?xml version="1.0" encoding="UTF-8" ?> <menuman name="Test" path="test" content="This is a test"></menuman> */ rapidxml::xml_node<> * root_node = xmlDoc.first_node("menusman"); rapidxml::xml_attribute<> *nodeAttrib; for (rapidxml::xml_node<> * domain_node = root_node->first_node("menuman"); domain_node; domain_node = domain_node->next_sibling("menuman")) { Wt::log("progress") << "MenuManSession::ImportXML: Start Loop = " << domain_node->name(); // Start a Transaction Wt::Dbo::Transaction t(*this); // Create a new Video Instance Wt::Dbo::ptr<MenuMan> thisMenu = add(new MenuMan()); // Set object to Modify MenuMan *menuDb = thisMenu.modify(); // name of Menu Item that will be shown in Menu nodeAttrib = domain_node->first_attribute("name"); if (!nodeAttrib) { Wt::log("error") << "MenuManSession::ImportXML: Missing XML Element: name = " << domain_node->name(); return false; } menuDb->name = Wt::WString::fromUTF8(nodeAttrib->value()); Wt::log("progress") << "MenuManSession::ImportXML: name = " << nodeAttrib->value(); // path of Menu Item that will be shown in address bar nodeAttrib = domain_node->first_attribute("path"); if (!nodeAttrib) { Wt::log("error") << "MenuManSession::ImportXML: Missing XML Element: path = " << domain_node->name(); return false; } menuDb->path = nodeAttrib->value(); std::string myPath = nodeAttrib->value(); Wt::log("progress") << "MenuManSession::ImportXML: path = " << nodeAttrib->value(); // type nodeAttrib = domain_node->first_attribute("type"); if (!nodeAttrib) { Wt::log("error") << "MenuManSession::ImportXML: Missing XML Element: type = " << domain_node->name(); return false; } std::string menuType = nodeAttrib->value(); menuDb->type = nodeAttrib->value(); Wt::log("progress") << "MenuManSession::ImportXML: type = " << nodeAttrib->value(); // parent nodeAttrib = domain_node->first_attribute("parent"); if (!nodeAttrib) { Wt::log("error") << "MenuManSession::ImportXML: Missing XML Element: parent = " << domain_node->name(); return false; } menuDb->parent = nodeAttrib->value(); Wt::log("progress") << "MenuManSession::ImportXML: parent = " << nodeAttrib->value(); // grandparent nodeAttrib = domain_node->first_attribute("grandparent"); if (!nodeAttrib) { Wt::log("error") << "MenuManSession::ImportXML: Missing XML Element: grandparent = " << domain_node->name(); return false; } menuDb->grandparent = nodeAttrib->value(); Wt::log("progress") << "MenuManSession::ImportXML: grandparent = " << nodeAttrib->value(); // language of Menu Item that will be shown in address bar: en, cn, ru... nodeAttrib = domain_node->first_attribute("language"); if (!nodeAttrib) { Wt::log("error") << "MenuManSession::ImportXML: Missing XML Element: language = " << domain_node->name(); return false; } menuDb->language = nodeAttrib->value(); std::string myLanguage = nodeAttrib->value(); Wt::log("progress") << "MenuManSession::ImportXML: language = " << nodeAttrib->value(); // content of page for menu item, XHTML for WidgetFunction // Read from file if (menuType == "" || menuType == "submenu") { /* if (myPath == "/") { myPath = "/home"; } // FIXME: home is hard corded std::string fileSufix = ""; if (myLanguage != "en") // FIXME: Is en the default language? { fileSufix = "_" + myLanguage; } if (CrystalBall::IsFile(appPath_ + "xml" + myPath + fileSufix + ".xml")) { Wt::log("info") << "MenuManSession::ImportXML: " << fullFilePath; std::string templateName = myPath; size_t found = templateName.find_last_of("/\\"); if (found != std::string::npos) { templateName = templateName.substr(found + 1); } Wt::WApplication::instance()->setLocale(myLanguage); Wt::WApplication::instance()->messageResourceBundle().use(appPath_ + "xml" + myPath, false); Wt::WString myContent = Wt::WString::tr(templateName + "-template"); menuDb->content = Wt::WString::fromUTF8(myContent.toUTF8()); } */ menuDb->content = CrystalBall::GetTemplate(appPath_, myPath, myLanguage); // Wt::log("progress") << "MenuManSession::ImportXML: content"; } // end if (menuType == "" || menuType == "submenu") // description nodeAttrib = domain_node->first_attribute("description"); if (!nodeAttrib) { Wt::log("error") << "MenuManSession::ImportXML: Missing XML Element: description = " << domain_node->name(); return false; } menuDb->description = Wt::WString::fromUTF8(nodeAttrib->value()); Wt::log("progress") << "MenuManSession::ImportXML: description"; // keywords nodeAttrib = domain_node->first_attribute("keywords"); if (!nodeAttrib) { Wt::log("error") << "MenuManSession::ImportXML: Missing XML Element: keywords = " << domain_node->name(); return false; } menuDb->keywords = Wt::WString::fromUTF8(nodeAttrib->value()); Wt::log("progress") << "MenuManSession::ImportXML: keywords"; // title nodeAttrib = domain_node->first_attribute("title"); if (!nodeAttrib) { Wt::log("error") << "MenuManSession::ImportXML: Missing XML Element: title = " << domain_node->name(); return false; } menuDb->title = Wt::WString::fromUTF8(nodeAttrib->value()); Wt::log("progress") << "MenuManSession::ImportXML: title"; // copyright nodeAttrib = domain_node->first_attribute("copyright"); if (!nodeAttrib) { Wt::log("error") << "MenuManSession::ImportXML: Missing XML Element: copyright = " << domain_node->name(); return false; } menuDb->copyright = Wt::WString::fromUTF8(nodeAttrib->value()); Wt::log("progress") << "MenuManSession::ImportXML: copyright"; // rating nodeAttrib = domain_node->first_attribute("rating"); if (!nodeAttrib) { Wt::log("error") << "MenuManSession::ImportXML: Missing XML Element: rating = " << domain_node->name(); return false; } menuDb->rating = nodeAttrib->value(); Wt::log("progress") << "MenuManSession::ImportXML: rating = " << nodeAttrib->value(); // Commit Transaction t.commit(); } // end for } catch (std::exception& e) { std::cerr << e.what() << std::endl; std::cerr << "MenuManSession::ImportXML(): Failed writting to menuman database"; Wt::log("error") << "-> MenuManSession::ImportXML() Failed writting to menuman database: " << e.what(); return false; } } Wt::WApplication::instance()->setLocale(lang_); Wt::log("end") << "MenuManSession::ImportXML()"; return true; } // end void MenuManSession::MenuManSession::ImportXML
void DboInstaller::InsertRows() { Wt::Dbo::Transaction transaction(DboSession); //Authors and Modules InsertAuthorModules(); //Configurations InsertConfigurations(); //Localization InsertLanguages(); //Pages O.LandingHomePage = DboSession.add(new Page(1, O.NavigationModule)); O.LandingHomePage.modify()->Title = "Home"; O.LandingHomePage.modify()->DefaultInternalPath = "home"; O.SitemapPage = DboSession.add(new Page(2, O.NavigationModule)); O.SitemapPage.modify()->Title = "Site map"; O.SitemapPage.modify()->DefaultInternalPath = "sitemap"; //Access Paths //English language O.DefaultLanguageAccessPath = DboSession.add(new AccessPath()); O.DefaultLanguageAccessPath.modify()->HostName = ""; O.DefaultLanguageAccessPath.modify()->InternalPath = "en"; O.DefaultLanguageAccessPath.modify()->LanguagePtr = O.EnglishLanguagePtr; //Mobile version O.MobileVersionAccessPath = DboSession.add(new AccessPath()); O.MobileVersionAccessPath.modify()->HostName = ""; O.MobileVersionAccessPath.modify()->InternalPath = "mobile"; //Home page O.HomePageAccessPath = DboSession.add(new AccessPath()); O.HomePageAccessPath.modify()->HostName = ""; O.HomePageAccessPath.modify()->InternalPath = "home"; O.HomePageAccessPath.modify()->PagePtr = O.LandingHomePage; //Sitemap page Wt::Dbo::ptr<AccessPath> SitemapPageAccessPath = DboSession.add(new AccessPath()); SitemapPageAccessPath.modify()->HostName = ""; SitemapPageAccessPath.modify()->InternalPath = "sitemap"; SitemapPageAccessPath.modify()->PagePtr = O.SitemapPage; //Default style O.DefaultStyle = DboSession.add(new Style("Default", O.SaifAuthor)); O.DefaultStyle.modify()->Description = "Default style"; O.DefaultStyle.modify()->CompatibilityVersionSeries = 1; O.DefaultStyle.modify()->CompatibilityVersionMajor = 0; transaction.commit(); //Set IDs after they have been assigned in the database Wt::Dbo::Transaction transaction2(DboSession); O.DefaultAccessPathVal.modify()->Value = O.DefaultLanguageAccessPath.id(); O.HomePageAccessPathIdVal.modify()->Value = O.HomePageAccessPath.id(); O.MobileAccessPathIdVal.modify()->Value = O.MobileVersionAccessPath.id(); O.DefaultStyleNameVal.modify()->Value = O.DefaultStyle.id().Name; O.DefaultStyleNameVal.modify()->DefaultValue = O.DefaultStyle.id().Name; O.DefaultStyleAuthorVal.modify()->Value = O.DefaultStyle.id().AuthorPtr.id(); O.DefaultStyleAuthorVal.modify()->DefaultValue = O.DefaultStyle.id().AuthorPtr.id(); transaction2.commit(); }
void UserDetailsModel::save(const Wt::Auth::User& authUser) { Wt::Dbo::ptr<User> user = session_.user(authUser); user.modify()->favouritePet = valueText(FavouritePetField).toUTF8(); }