fw4spl
fwActivities/src/fwActivities/builder/ActivitySeries.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
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 "fwActivities/builder/ActivitySeries.hpp"
8 
9 #include "fwActivities/builder/registry/macros.hpp"
10 
11 #include <fwData/Composite.hpp>
12 #include <fwData/Vector.hpp>
13 
14 #include <fwDataCamp/getObject.hpp>
15 
16 #include <fwMedData/Equipment.hpp>
17 #include <fwMedData/Patient.hpp>
18 #include <fwMedData/Study.hpp>
19 
20 #include <fwTools/dateAndTime.hpp>
21 #include <fwTools/UUID.hpp>
22 
23 #include <boost/date_time/posix_time/posix_time.hpp>
24 
25 namespace fwActivities
26 {
27 namespace builder
28 {
29 
30 fwActivitiesBuilderRegisterMacro(::fwActivities::builder::ActivitySeries, "::fwActivities::builder::ActivitySeries");
31 
32 //-----------------------------------------------------------------------------
33 
35 {
36 }
37 
38 //-----------------------------------------------------------------------------
39 
41 {
42 }
43 
44 //-----------------------------------------------------------------------------
45 
46 ::fwData::Composite::sptr vectorToComposite(const ::fwData::Vector::csptr& vector,
47  const ::fwActivities::registry::ActivityRequirement& req)
48 {
49  namespace ActReg = ::fwActivities::registry;
50  ::fwData::Composite::sptr composite = ::fwData::Composite::New();
51 
52  OSLM_ASSERT("Each possible items in requirement need to have a matching key", req.keys.size() >= req.maxOccurs );
53 
54  ActReg::ActivityRequirement::KeyType::const_iterator iter = req.keys.begin();
55 
56  for(const ::fwData::Object::sptr& obj : *vector)
57  {
58  const ActReg::ActivityRequirementKey& keyTag = (*iter++);
59  if(keyTag.path.empty())
60  {
61  (*composite)[keyTag.key] = obj;
62  }
63  else
64  {
65  (*composite)[keyTag.key] = ::fwDataCamp::getObject( obj, keyTag.path );
66  }
67  }
68 
69  return composite;
70 }
71 
72 //-----------------------------------------------------------------------------
73 
74 ::fwMedData::ActivitySeries::sptr ActivitySeries::buildData(
75  const ::fwActivities::registry::ActivityInfo& activityInfo,
76  const ::fwData::Vector::csptr& currentSelection ) const
77 {
78  ::fwMedData::ActivitySeries::sptr actSeries = ::fwMedData::ActivitySeries::New();
79 
80  ::fwMedData::Series::sptr series;
81  for(const ::fwData::Object::sptr& obj : *currentSelection)
82  {
83  series = ::fwMedData::Series::dynamicCast(obj);
84  if(series)
85  {
86  break;
87  }
88  }
89 
90  if(series)
91  {
92  actSeries->setPatient( ::fwData::Object::copy(series->getPatient()) );
93  actSeries->setStudy( ::fwData::Object::copy(series->getStudy()) );
94  actSeries->setEquipment( ::fwData::Object::copy(series->getEquipment()) );
95  }
96 
97  actSeries->setModality("OT");
98  actSeries->setInstanceUID("fwActivities." + ::fwTools::UUID::generateUUID() );
99 
100  ::boost::posix_time::ptime now = ::boost::posix_time::second_clock::local_time();
101  actSeries->setDate(::fwTools::getDate(now));
102  actSeries->setTime(::fwTools::getTime(now));
103 
104  actSeries->setActivityConfigId(activityInfo.id);
105  ::fwData::Composite::sptr data = actSeries->getData();
106 
107  namespace ActReg = ::fwActivities::registry;
108 
109  ActReg::ActivityInfo::RequirementsType reqVect = activityInfo.requirements;
110  for(const ActReg::ActivityRequirement& req : reqVect)
111  {
112  ::fwData::Vector::sptr vectorType = this->getType(currentSelection, req.type);
113  // param is optional (minOccurs==0) or required (minOccurs==1), but is single (maxOccurs == 1)
114  if(req.maxOccurs == 1 && req.minOccurs == 1)
115  {
116  OSLM_ASSERT("No param name "<<req.name<<" with type "<<req.type, !vectorType->empty());
117  (*data)[req.name] = (*vectorType)[0];
118  }
119  else
120  {
121  SLM_ASSERT("Unknown specified container: '"+req.container+"'.",
122  req.container.empty() ||
123  req.container == "vector" ||
124  req.container == "composite");
125  if(req.container == "vector")
126  {
127  (*data)[req.name] = vectorType;
128  }
129  else if(req.container == "composite" || req.container.empty())
130  {
131  (*data)[req.name] = vectorToComposite(vectorType, req);
132  }
133  }
134  }
135 
136  return actSeries;
137 }
138 
139 //-----------------------------------------------------------------------------
140 
141 } // namespace builder
142 } // namespace fwActivities
Defines the default builder for the data ActivitySeries.
virtual FWACTIVITIES_API::fwMedData::ActivitySeries::sptr buildData(const ::fwActivities::registry::ActivityInfo &activityInfo, const std::shared_ptr< const ::fwData::Vector > &currentSelection) const override
Build an ActivitySeries with required data present in currentSelection and defined in configuration...
#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
virtual FWACTIVITIES_API ~ActivitySeries()
Destructor. Do nothing.
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
FWACTIVITIES_API ActivitySeries(::fwActivities::IBuilder::Key key)
Constructor. Do nothing.
#define SLM_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:308
static FWDATA_API::fwData::Object::sptr copy(const ::fwData::Object::csptr &source)
return a copy of the source. if source is a null pointer, return a null pointer.
Namespace containing activities data and builder.
FWTOOLS_API std::string getDate(const ::boost::posix_time::ptime &dateAndTime)
Convert a boost time to a string date.
FWTOOLS_API std::string getTime(const ::boost::posix_time::ptime &dateAndTime)
Convert a boost time to a string time.
static FWTOOLS_API UUIDType generateUUID()
Return a new extended UUID;.
Definition: UUID.cpp:114