/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2016 Frederik Heber. All rights reserved.
 * 
 *
 *   This file is part of MoleCuilder.
 *
 *    MoleCuilder is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    MoleCuilder is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with MoleCuilder.  If not, see .
 */
/*
 * ParseParticleParametersAction.cpp
 *
 *  Created on: Feb 27, 2016
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
// include headers that implement a archive in simple text format
// and before MemDebug due to placement new
#include 
#include 
#include "CodePatterns/MemDebug.hpp"
#include "Actions/PotentialAction/ParseParticleParametersAction.hpp"
#include 
#include 
#include "CodePatterns/Log.hpp"
#include "Potentials/Exceptions.hpp"
#include "Potentials/Particles/ParticleFactory.hpp"
#include "Potentials/Particles/ParticleRegistry.hpp"
#include "Potentials/RegistryDeserializer.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "ParseParticleParametersAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
ActionState::ptr PotentialParseParticleParametersAction::performCall()
{
  if (boost::filesystem::exists(params.particle_file.get())) {
    std::ifstream returnstream(params.particle_file.get().string().c_str());
    if (returnstream.good()) {
      try {
        RegistryDeserializer deserialize(
            returnstream,
            ParticleFactory::getInstance(),
            ParticleRegistry::getInstance(),
            std::string("particle"));
        deserialize();
      } catch (SerializerMissingValueException &e) {
        if (const std::string *key = boost::get_error_info(e))
          STATUS("Missing value when parsing information for particle "+*key+".");
        else
          STATUS("Missing value parsing information for particle with unknown key.");
        return Action::failure;
      } catch (SerializerIllegalKeyException &e) {
        if (const std::string *key = boost::get_error_info(e))
          STATUS("Illegal key parsing information for particle "+*key+".");
        else
          STATUS("Illegal key parsing information for particle with unknown key.");
        return Action::failure;
      }
    } else {
      STATUS("Failed to parse from "+params.particle_file.get().string()+".");
      return Action::failure;
    }
    returnstream.close();
    LOG(0, "STATUS: Parsed a set of particle parameters parsed from "
        << params.particle_file.get().string() << ".");
  } else {
      STATUS("No existing particles file given!");
      return Action::failure;
  }
  return Action::success;
}
ActionState::ptr PotentialParseParticleParametersAction::performUndo(ActionState::ptr _state) {
  STATUS("Undo of PotentialParseParticleParametersAction not implemented.");
  return Action::failure;
}
ActionState::ptr PotentialParseParticleParametersAction::performRedo(ActionState::ptr _state){
  STATUS("Redo of PotentialParseParticleParametersAction not implemented.");
  return Action::failure;
}
bool PotentialParseParticleParametersAction::canUndo() {
  return false;
}
bool PotentialParseParticleParametersAction::shouldUndo() {
  return false;
}
/** =========== end of function ====================== */