| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * ElementDbAction.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: May 9, 2010
 | 
|---|
| 12 |  *      Author: heber
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | // include config.h
 | 
|---|
| 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 17 | #include <config.h>
 | 
|---|
| 18 | #endif
 | 
|---|
| 19 | 
 | 
|---|
| 20 | // include headers that implement a archive in simple text format
 | 
|---|
| 21 | #include <boost/archive/text_oarchive.hpp>
 | 
|---|
| 22 | #include <boost/archive/text_iarchive.hpp>
 | 
|---|
| 23 | 
 | 
|---|
| 24 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 25 | 
 | 
|---|
| 26 | #include "config.hpp"
 | 
|---|
| 27 | #include "element.hpp"    // we need element because of serialization!
 | 
|---|
| 28 | #include "periodentafel.hpp"
 | 
|---|
| 29 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 30 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| 31 | #include "World.hpp"
 | 
|---|
| 32 | 
 | 
|---|
| 33 | #include <string>
 | 
|---|
| 34 | 
 | 
|---|
| 35 | 
 | 
|---|
| 36 | #include "Actions/CommandAction/ElementDbAction.hpp"
 | 
|---|
| 37 | 
 | 
|---|
| 38 | // and construct the stuff
 | 
|---|
| 39 | #include "ElementDbAction.def"
 | 
|---|
| 40 | #include "Actions/Action_impl_pre.hpp"
 | 
|---|
| 41 | 
 | 
|---|
| 42 | /** =========== define the function ====================== */
 | 
|---|
| 43 | Action::state_ptr CommandElementDbAction::performCall() {
 | 
|---|
| 44 |   std::ostringstream usage;
 | 
|---|
| 45 | 
 | 
|---|
| 46 |   // obtain information
 | 
|---|
| 47 |   getParametersfromValueStorage();
 | 
|---|
| 48 |   periodentafel *periode = World::getInstance().getPeriode();
 | 
|---|
| 49 | 
 | 
|---|
| 50 |   // create undo state
 | 
|---|
| 51 |   std::stringstream undostream;
 | 
|---|
| 52 |   boost::archive::text_oarchive oa(undostream);
 | 
|---|
| 53 |   oa << periode;
 | 
|---|
| 54 |   CommandElementDbState *UndoState =
 | 
|---|
| 55 |       new CommandElementDbState(
 | 
|---|
| 56 |           undostream.str(),
 | 
|---|
| 57 |           params
 | 
|---|
| 58 |           );
 | 
|---|
| 59 | 
 | 
|---|
| 60 |   // get the path
 | 
|---|
| 61 |   // TODO: Make databasepath a std::string
 | 
|---|
| 62 |   config *configuration = World::getInstance().getConfig();
 | 
|---|
| 63 |   strcpy(configuration->databasepath, params.databasepath.branch_path().string().c_str());
 | 
|---|
| 64 | 
 | 
|---|
| 65 |   // load table
 | 
|---|
| 66 |   if (periode->LoadPeriodentafel(configuration->databasepath)) {
 | 
|---|
| 67 |     DoLog(0) && (Log() << Verbose(0) << "Element list loaded successfully." << endl);
 | 
|---|
| 68 |     //periode->Output();
 | 
|---|
| 69 |     return Action::state_ptr(UndoState);
 | 
|---|
| 70 |   } else {
 | 
|---|
| 71 |     DoLog(0) && (Log() << Verbose(0) << "Element list loading failed." << endl);
 | 
|---|
| 72 |     return Action::failure;
 | 
|---|
| 73 |   }
 | 
|---|
| 74 | }
 | 
|---|
| 75 | 
 | 
|---|
| 76 | Action::state_ptr CommandElementDbAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 77 |   CommandElementDbState *state = assert_cast<CommandElementDbState*>(_state.get());
 | 
|---|
| 78 | 
 | 
|---|
| 79 |   periodentafel *periode;
 | 
|---|
| 80 | 
 | 
|---|
| 81 |   std::stringstream undostream(state->undostring);
 | 
|---|
| 82 |   boost::archive::text_iarchive ia(undostream);
 | 
|---|
| 83 |   ia >> periode;
 | 
|---|
| 84 | 
 | 
|---|
| 85 |   delete World::getInstance().getPeriode();
 | 
|---|
| 86 |   World::getInstance().getPeriode() = periode;
 | 
|---|
| 87 | 
 | 
|---|
| 88 |   return Action::state_ptr(_state);
 | 
|---|
| 89 | }
 | 
|---|
| 90 | 
 | 
|---|
| 91 | Action::state_ptr CommandElementDbAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 92 |   CommandElementDbState *state = assert_cast<CommandElementDbState*>(_state.get());
 | 
|---|
| 93 | 
 | 
|---|
| 94 |   // get the path
 | 
|---|
| 95 |   // TODO: Make databasepath a std::string
 | 
|---|
| 96 |   config *configuration = World::getInstance().getConfig();
 | 
|---|
| 97 |   strcpy(configuration->databasepath, state->params.databasepath.branch_path().string().c_str());
 | 
|---|
| 98 | 
 | 
|---|
| 99 |   // load table
 | 
|---|
| 100 |   periodentafel *periode = World::getInstance().getPeriode();
 | 
|---|
| 101 |   if (periode->LoadPeriodentafel(configuration->databasepath)) {
 | 
|---|
| 102 |     DoLog(0) && (Log() << Verbose(0) << "Element list loaded successfully." << endl);
 | 
|---|
| 103 |     //periode->Output();
 | 
|---|
| 104 |     return Action::state_ptr(_state);
 | 
|---|
| 105 |   } else {
 | 
|---|
| 106 |     DoLog(0) && (Log() << Verbose(0) << "Element list loading failed." << endl);
 | 
|---|
| 107 |     return Action::failure;
 | 
|---|
| 108 |   }
 | 
|---|
| 109 | }
 | 
|---|
| 110 | 
 | 
|---|
| 111 | bool CommandElementDbAction::canUndo() {
 | 
|---|
| 112 |   return true;
 | 
|---|
| 113 | }
 | 
|---|
| 114 | 
 | 
|---|
| 115 | bool CommandElementDbAction::shouldUndo() {
 | 
|---|
| 116 |   return true;
 | 
|---|
| 117 | }
 | 
|---|
| 118 | /** =========== end of function ====================== */
 | 
|---|