fw4spl
PatchingManager.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include <fwAtoms/Object.hpp>
8 
9 #include "fwAtomsPatch/exceptions/ImpossibleConversion.hpp"
10 #include "fwAtomsPatch/exceptions/MissingInformation.hpp"
11 #include "fwAtomsPatch/infos/log.hpp"
12 #include "fwAtomsPatch/patcher/IPatcher.hpp"
13 #include "fwAtomsPatch/PatchingManager.hpp"
14 #include "fwAtomsPatch/VersionsGraph.hpp"
15 #include "fwAtomsPatch/VersionsManager.hpp"
16 
17 namespace fwAtomsPatch
18 {
19 
20 PatchingManager::PatchingManager(::fwAtoms::Object::sptr object)
21  : m_object(object)
22 {
23 }
24 
25 // ----------------------------------------------------------------------------
26 
28 {
29 }
30 
31 // ----------------------------------------------------------------------------
32 
33 ::fwAtoms::Object::sptr PatchingManager::transformTo(const std::string& newVersion)
34 {
35  const std::string& context = m_object->getMetaInfo("context");
36  const std::string& currentVersion = m_object->getMetaInfo("version_name");
37 
38  if(currentVersion == newVersion)
39  {
40  OSLM_WARN("Trying to patch an object with the same version (" << currentVersion << ").");
41  return m_object;
42  }
43 
44  FW_RAISE_EXCEPTION_IF(
45  ::fwAtomsPatch::exceptions::MissingInformation("Context information is missing."),
46  context.empty());
47 
48  FW_RAISE_EXCEPTION_IF(
49  ::fwAtomsPatch::exceptions::MissingInformation("Version information is missing."),
50  currentVersion.empty());
51 
52 
53  ::fwAtomsPatch::VersionsGraph::sptr versionsGraph;
54  versionsGraph = ::fwAtomsPatch::VersionsManager::getDefault()->getGraph(context);
55 
56  FW_RAISE_EXCEPTION_IF( ::fwAtomsPatch::exceptions::ImpossibleConversion(
57  "There is no way to go from version '" + currentVersion + "' to version '" +
58  newVersion + "' for context '" + context +"'."), !versionsGraph);
59 
60 
61  ::fwAtomsPatch::VersionsGraph::VersionSeriesType series
62  = versionsGraph->shortestPath(currentVersion, newVersion);
63 
64  FW_RAISE_EXCEPTION_IF( ::fwAtomsPatch::exceptions::ImpossibleConversion(
65  "There is no way to go from version '" + currentVersion + "' to version '" +
66  newVersion + "' for context '" + context +"'."),
67  series.empty());
68 
69  ::fwAtomsPatch::VersionsGraph::NodeIDType currentVersionNode = versionsGraph->getNode(currentVersion);
70 
71  ::fwAtomsPatch::patcher::IPatcher::sptr patcher;
72  std::string currentName, targetName;
73 
74  SLM_TRACE("[PATCHING] Starting...");
75 
76  for(::fwAtomsPatch::VersionsGraph::VersionSeriesType::value_type elt : series)
77  {
78  ::fwAtomsPatch::VersionsGraph::NodeIDType targetVersionNode = elt;
79 
80  //Retrieve versions names
81  currentName = versionsGraph->getNode(currentVersionNode).getVersionName();
82  targetName = versionsGraph->getNode(targetVersionNode).getVersionName();
83 
84  //Retrieve link
85  const ::fwAtomsPatch::LinkDescriptor& link
86  = versionsGraph->getEdge(currentVersionNode, targetVersionNode);
87 
88  //Retrieve patcher
89  patcher = ::fwAtomsPatch::patcher::factory::New(link.getPatcher());
90  OSLM_ASSERT("There is no patcher called \"" << link.getPatcher() << "\".", patcher);
91 
92  OSLM_TRACE("[PATCHING] '" << currentName << "' -> '" << targetName << "'.");
93  fwAtomsPatchInfoLogMacro("Begin patcher '" + link.getPatcher() + "'");
94 
95  //Patching
96  m_object = patcher->transformObject(m_object, context, currentName, targetName);
97  fwAtomsPatchInfoLogMacro("End patcher '" + link.getPatcher() + "'");
98 
99  currentVersionNode = targetVersionNode;
100  }
101 
102  m_object->setMetaInfo("version_name", newVersion);
103 
104  return m_object;
105 }
106 
107 } //namespace fwAtomsPatch
std::shared_ptr< ::fwAtoms::Object > m_object
Object that we want to patch.
FWATOMSPATCH_API::fwAtoms::Object::sptr transformTo(const std::string &newVersion)
Transforms the object to the given version.
#define OSLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:310
Reports an missing information required for data object patching.
#define OSLM_TRACE(message)
Definition: spyLog.hpp:230
Contains base functionalities used to transform objects from a version to another.
Definition: Abstract.hpp:16
#define OSLM_WARN(message)
Definition: spyLog.hpp:263
FWATOMSPATCH_API ~PatchingManager()
Default destructor.
static std::shared_ptr< VersionsManager > getDefault()
Returns the default instance of VersionsManager.
#define SLM_TRACE(message)
Definition: spyLog.hpp:228
FWATOMSPATCH_API PatchingManager(std::shared_ptr< ::fwAtoms::Object >object)
Default constructor.
Reports an impossible conversion between data objects.