fw4spl
IActivityView.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 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 "fwGui/view/IActivityView.hpp"
8 
9 #include "fwGui/dialog/MessageDialog.hpp"
10 
11 #include <fwActivities/IActivityValidator.hpp>
12 #include <fwActivities/IValidator.hpp>
13 
14 #include <fwCom/Slot.hpp>
15 #include <fwCom/Slot.hxx>
16 #include <fwCom/Slots.hxx>
17 
18 #include <fwData/Composite.hpp>
19 #include <fwData/String.hpp>
20 
21 #include <fwDataCamp/getObject.hpp>
22 
23 #include <fwRuntime/operations.hpp>
24 
25 #include <fwServices/macros.hpp>
26 
27 #include <fwTools/dateAndTime.hpp>
28 #include <fwTools/UUID.hpp>
29 
30 namespace fwGui
31 {
32 namespace view
33 {
34 
35 const ::fwCom::Slots::SlotKeyType IActivityView::s_LAUNCH_ACTIVITY_SLOT = "launchActivity";
36 const ::fwCom::Slots::SlotKeyType IActivityView::s_LAUNCH_ACTIVITY_SERIES_SLOT = "launchActivitySeries";
37 
38 //-----------------------------------------------------------------------------
39 
41 {
42  newSlot(s_LAUNCH_ACTIVITY_SLOT, &IActivityView::launchActivity, this);
43  newSlot(s_LAUNCH_ACTIVITY_SERIES_SLOT, &IActivityView::launchActivitySeries, this);
44 }
45 
46 //-----------------------------------------------------------------------------
47 
49 {
50 }
51 
52 //------------------------------------------------------------------------------
53 
55 {
57 
58  const ConfigType config = this->getConfigTree();
59 
60  m_mainActivityId = config.get<std::string>("mainActivity.<xmlattr>.id", "");
61  SLM_DEBUG_IF("main activity 'id' is not defined", m_mainActivityId.empty());
62 
63  const auto inoutsCfg = config.equal_range("inout");
64  for (auto itCfg = inoutsCfg.first; itCfg != inoutsCfg.second; ++itCfg)
65  {
66  const std::string key = itCfg->second.get<std::string>("<xmlattr>.key");
67  SLM_ASSERT("Missing 'key' tag.", !key.empty());
68 
69  const std::string uid = itCfg->second.get<std::string>("<xmlattr>.uid");
70  SLM_ASSERT("Missing 'uid' tag.", !uid.empty());
71 
72  const std::string strOptional = itCfg->second.get<std::string>("<xmlattr>.optional", "no");
73  const bool optional = (strOptional == "yes");
74 
75  ::fwData::Object::csptr obj = this->getInOut< ::fwData::Object>(key);
76  ParameterType param;
77  param.replace = key;
78  if(optional)
79  {
80  param.by = uid;
81  }
82  else
83  {
84  SLM_ASSERT("Object key '" + key + "'with uid '" + uid + "' does not exists.", obj);
85  param.by = obj->getID();
86  }
87  m_parameters.push_back(param);
88  }
89 
90  ConfigType configParams = config.get_child("parameters");
91 
92  const auto paramsCfg = configParams.equal_range("parameter");
93  for (auto itParams = paramsCfg.first; itParams != paramsCfg.second; ++itParams)
94  {
95  const std::string replace = itParams->second.get<std::string>("<xmlattr>.replace");
96  std::string by = itParams->second.get<std::string>("<xmlattr>.by", "");
97  if(by.empty())
98  {
99  by = itParams->second.get<std::string>("<xmlattr>.uid");
100  }
101  SLM_ASSERT("'parameter' tag must contain valid 'replace' and 'by' attributes.",
102  !replace.empty() && !by.empty());
103  ParameterType param;
104  param.replace = replace;
105  param.by = by;
106  SLM_ASSERT("'camp' paths are not managed in the configuration parameters", !param.isSeshat());
107  m_parameters.push_back(param);
108  }
109 }
110 
111 //------------------------------------------------------------------------------
112 
113 void IActivityView::launchActivitySeries(fwMedData::Series::sptr series)
114 {
115  ::fwMedData::ActivitySeries::sptr activitySeries = ::fwMedData::ActivitySeries::dynamicCast(series);
116  if (activitySeries)
117  {
118  this->launchActivity(activitySeries);
119  }
120 }
121 
122 //------------------------------------------------------------------------------
123 
124 bool IActivityView::validateActivity(fwMedData::ActivitySeries::sptr activitySeries) const
125 {
126  bool isValid = true;
127  // Applies validator on activity series to check the data
129  info = ::fwActivities::registry::Activities::getDefault()->getInfo(activitySeries->getActivityConfigId());
130 
131  // load activity bundle
132  std::shared_ptr< ::fwRuntime::Bundle > bundle = ::fwRuntime::findBundle(info.bundleId, info.bundleVersion);
133  if (!bundle->isStarted())
134  {
135  bundle->start();
136  }
137 
138  for (std::string validatorImpl : info.validatorsImpl)
139  {
141  ::fwActivities::IValidator::sptr validator = ::fwActivities::validator::factory::New(validatorImpl);
142 
143  ::fwActivities::IActivityValidator::sptr activityValidator =
144  ::fwActivities::IActivityValidator::dynamicCast(validator);
145  SLM_ASSERT("Validator '" + validatorImpl + "' instantiation failed", activityValidator);
146 
147  ::fwActivities::IValidator::ValidationType validation = activityValidator->validate(activitySeries);
148  if(!validation.first)
149  {
150  const std::string message = "The activity '" + info.title + "' can not be launched:\n" + validation.second;
152  message,
153  ::fwGui::dialog::IMessageDialog::CRITICAL);
154  isValid = false;
155  break;
156  }
157  }
158 
159  return isValid;
160 }
161 
162 //------------------------------------------------------------------------------
163 
164 fwMedData::ActivitySeries::sptr IActivityView::createMainActivity() const
165 {
168 
169  ::fwMedData::ActivitySeries::sptr actSeries = ::fwMedData::ActivitySeries::New();
170  if (info.requirements.size() > 0)
171  {
172  ::fwData::Composite::sptr data = actSeries->getData();
173  for (::fwActivities::registry::ActivityRequirement req : info.requirements)
174  {
175  if ((req.minOccurs == 0 && req.maxOccurs == 0) || req.create)
176  {
177  (*data)[req.name] = ::fwData::factory::New(req.type);
178  }
179  else
180  {
182  "Main activity",
183  "The main activity " + m_mainActivityId + " can not be launched. \n"
184  "This activity needs parameters that cannot be defined",
185  ::fwGui::dialog::IMessageDialog::INFO);
186  return nullptr;
187  }
188  }
189  }
190 
191  actSeries->setModality("OT");
192  actSeries->setInstanceUID("fwActivities." + ::fwTools::UUID::generateUUID() );
193 
194  const ::boost::posix_time::ptime now = ::boost::posix_time::second_clock::local_time();
195  actSeries->setDate(::fwTools::getDate(now));
196  actSeries->setTime(::fwTools::getTime(now));
197  actSeries->setActivityConfigId(info.id);
198 
199  return actSeries;
200 }
201 
202 //------------------------------------------------------------------------------
203 
204 void IActivityView::translateParameters( ::fwData::Object::sptr sourceObj, const ParametersType& parameters,
205  ReplaceMapType& replaceMap )
206 {
207  for(const ParametersType::value_type& param : parameters)
208  {
209  if(!param.isSeshat())
210  {
211  replaceMap[param.replace] = param.by;
212  }
213  else
214  {
215  std::string parameterToReplace = param.by;
216  if (parameterToReplace.substr(0, 1) == "!")
217  {
218  parameterToReplace.replace(0, 1, "@");
219  }
220 
221  ::fwData::Object::sptr obj = ::fwDataCamp::getObject(sourceObj, parameterToReplace);
222  SLM_ASSERT("Invalid seshat path : '" + param.by + "'", obj);
223 
224  ::fwData::String::sptr stringParameter = ::fwData::String::dynamicCast(obj);
225 
226  std::string parameterValue = obj->getID();
227 
228  if(stringParameter && param.by.substr(0, 1) == "!")
229  {
230  parameterValue = stringParameter->getValue();
231  }
232  replaceMap[param.replace] = parameterValue;
233  }
234  }
235 }
236 
237 //------------------------------------------------------------------------------
238 
239 void IActivityView::translateParameters( const ParametersType& parameters, ReplaceMapType& replaceMap )
240 {
241  for(const ParametersType::value_type& param : parameters)
242  {
243  replaceMap[param.replace] = param.by;
244  }
245 }
246 
247 } // namespace view
248 } // namespace fwGui
virtual FWGUI_API void launchActivitySeries(::fwMedData::Series::sptr series)
Slot: Launch the given activity in a new tab.
std::pair< bool, std::string > ValidationType
Defines validation result of an activity. First element tells if the activity is validated or not by ...
Definition: IValidator.hpp:39
Holds Activities configuration.
Definition: Activities.hpp:175
The namespace fwGui contains the base interface for IHM services.
Definition: SJobBar.hpp:23
#define SLM_DEBUG_IF(message, cond)
Definition: spyLog.hpp:243
unsigned int maxOccurs
minimum number of data required
Definition: Activities.hpp:97
static FWGUI_API IMessageDialog::Buttons showMessageDialog(const std::string &title, const std::string &message,::fwGui::dialog::IMessageDialog::Icons icon=INFO)
static FWACTIVITIES_API Activities::sptr getDefault()
Return the default global instance of Activities.
Definition: Activities.cpp:226
std::string m_mainActivityId
configuration id of the main activity
std::string bundleVersion
Version of the bundle containing the activity.
Definition: Activities.hpp:198
ParametersType m_parameters
parameters given in configuration
virtual FWGUI_API bool validateActivity(::fwMedData::ActivitySeries::sptr activitySeries) const
Check if the activity is valid by calling the activity validator.
std::string bundleId
Identifier of the bundle containing the activity.
Definition: Activities.hpp:197
virtual FWGUI_API::fwMedData::ActivitySeries::sptr createMainActivity() const
Create the activity series given in &#39;mainActivity&#39; configuration.
unsigned int minOccurs
Implementation of data validator.
Definition: Activities.hpp:96
virtual FWGUI_API ~IActivityView()
Destructor. 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
virtual FWGUI_API void configuring() override
Parse the configuration.
FWGUI_API IActivityView()
Constructor. Do nothing.
FWTOOLS_API std::string getDate(const ::boost::posix_time::ptime &dateAndTime)
Convert a boost time to a string date.
std::vector< std::string > validatorsImpl
Validator implementations.
Definition: Activities.hpp:201
FWTOOLS_API std::string getTime(const ::boost::posix_time::ptime &dateAndTime)
Convert a boost time to a string time.
bool create
maximum number of data required
Definition: Activities.hpp:98
virtual FWGUI_API void translateParameters(::fwData::Object::sptr sourceObj, const ParametersType &parameters, ReplaceMapType &replaceMap)
Translate parameters from source object.
virtual FWGUI_API void launchActivity(::fwMedData::ActivitySeries::sptr activitySeries)=0
Slot: Launch the given activity in a new tab.
FWGUI_API void initialize()
Initialize managers.
virtual FWSERVICES_API void info(std::ostream &_sstream)
Write information in a stream.
Definition: IService.cpp:74
static FWTOOLS_API UUIDType generateUUID()
Return a new extended UUID;.
Definition: UUID.cpp:114
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.
Definition: IService.cpp:247