/* ***** BEGIN LICENSE BLOCK *****
 * FW4SPL - Copyright (C) IRCAD, 2009-2013.
 * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
 * published by the Free Software Foundation.
 * ****** END LICENSE BLOCK ****** */

#include <fwTools/UUID.hpp>

#include <fwAtoms/Object.hpp>
#include <fwAtoms/Object.hxx>
#include <fwAtoms/String.hpp>

#include <fwAtomsPatch/helper/Object.hpp>
#include <fwAtomsPatch/helper/functions.hpp>

#include "fwMDSemanticPatch/V1/V2/fwData/Patient.hpp"

namespace fwMDSemanticPatch
{
namespace V1
{
namespace V2
{
namespace fwData
{

Patient::Patient() : ::fwAtomsPatch::ISemanticPatch()
{
    m_originClassname = "::fwData::Patient";
    m_originVersion = "1";
    this->addContext("MedicalData", "V1", "V2");
}

// ----------------------------------------------------------------------------

Patient::~Patient()
{}

// ----------------------------------------------------------------------------

Patient::Patient( const Patient &cpy ) : ::fwAtomsPatch::ISemanticPatch(cpy)
{}

// ----------------------------------------------------------------------------

void Patient::apply(
        const ::fwAtoms::Object::sptr& previous,
        const ::fwAtoms::Object::sptr& current,
        ::fwAtomsPatch::IPatch::NewVersionsType& newVersions)
{
    ISemanticPatch::apply(previous, current, newVersions);
    ::fwAtomsPatch::helper::cleanFields( current );
    ::fwAtomsPatch::helper::Object helper( current );

    ::fwAtoms::String::sptr uid = current->getAttribute< ::fwAtoms::String >("patient_id");
    if( uid->getValue().empty() )
    {
        helper.replaceAttribute("patient_uid", ::fwAtoms::String::New(::fwTools::UUID::generateUUID()));
    }
}

// ----------------------------------------------------------------------------

} // namespace fwData
} // namespace V2
} // namespace V1
} // namespace fwMDSemanticPatch

