fw4spl
SPacsConfigurationInitializer.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 "ioPacs/SPacsConfigurationInitializer.hpp"
8 
9 #include <fwData/Composite.hpp>
10 #include <fwData/String.hpp>
11 
12 #include <fwPreferences/helper.hpp>
13 
14 #include <fwServices/macros.hpp>
15 #include <fwServices/registry/ObjectService.hpp>
16 
17 #include <boost/foreach.hpp>
18 
19 #include <utility>
20 
21 namespace ioPacs
22 {
23 
26 
27 static const ::fwServices::IService::KeyType s_CONFIG_INOUT = "config";
28 
29 //------------------------------------------------------------------------------
30 
32 {
33 }
34 //------------------------------------------------------------------------------
35 
37 {
38 }
39 
40 //------------------------------------------------------------------------------
41 
42 void SPacsConfigurationInitializer::info(std::ostream& _sstream )
43 {
44  _sstream << "SPacsConfigurationInitializer::info";
45 }
46 
47 //------------------------------------------------------------------------------
48 
50 {
52  this->updating();
53 }
54 
55 //------------------------------------------------------------------------------
56 
58 {
60  this->updating();
61 }
62 
63 //------------------------------------------------------------------------------
64 
65 template<typename T, typename CAST_T = T>
67 {
68  //------------------------------------------------------------------------------
69 
70  void operator()(::fwData::Composite::sptr config, std::string const& confName, T& attribute)
71  {
72  if(config)
73  {
74  ::fwData::Composite::iterator it = config->find(confName);
75  if(it != config->end())
76  {
77  ::fwData::String::sptr obj = ::fwData::String::dynamicCast(it->second);
78  attribute = T(::boost::lexical_cast<CAST_T>(obj->value()));
79  }
80  }
81  }
82 };
83 
84 //------------------------------------------------------------------------------
85 
87 {
89 
90  ::fwRuntime::ConfigurationElement::sptr config = m_configuration->findConfigurationElement("config");
91  SLM_ASSERT("The service ::ioPacs::SPacsConfigurationInitializer must have a \"config\" element.",
92  config);
93 
94  bool success;
95 
97  ::boost::tie(success, m_localApplicationTitle) = config->getSafeAttributeValue("localApplicationTitle");
98  SLM_ASSERT("It should be a \"localApplicationTitle\" tag in the "
99  "::ioPacs::SPacsConfigurationInitializer config element.", success);
100 
102  ::boost::tie(success, m_pacsHostName) = config->getSafeAttributeValue("pacsHostName");
103  SLM_ASSERT("It should be a \"pacsHostName\" tag in the "
104  "::ioPacs::SPacsConfigurationInitializer config element.", success);
105 
107  ::boost::tie(success, m_pacsApplicationTitle) = config->getSafeAttributeValue("pacsApplicationTitle");
108  SLM_ASSERT("It should be a \"pacsApplicationTitle\" tag in the "
109  "::ioPacs::SPacsConfigurationInitializer config element.", success);
110 
112  std::string pacsApplicationPort;
113  ::boost::tie(success, pacsApplicationPort) = config->getSafeAttributeValue("pacsApplicationPort");
114  SLM_ASSERT("It should be a \"pacsApplicationPort\" tag in the "
115  "::ioPacs::SPacsConfigurationInitializer config element.", success);
116  m_pacsApplicationPort = ::boost::lexical_cast< unsigned short >(pacsApplicationPort.c_str());
117 
119  ::boost::tie(success, m_moveApplicationTitle) = config->getSafeAttributeValue("moveApplicationTitle");
120  SLM_ASSERT("It should be a \"moveApplicationTitle\" tag in the "
121  "::ioPacs::SPacsConfigurationInitializer config element.", success);
122 
124  std::string moveApplicationPort;
125  ::boost::tie(success, moveApplicationPort) = config->getSafeAttributeValue("moveApplicationPort");
126  SLM_ASSERT("It should be a \"moveApplicationPort\" tag in the "
127  "::ioPacs::SPacsConfigurationInitializer config element.", success);
128  m_moveApplicationPort = ::boost::lexical_cast< unsigned short >(moveApplicationPort.c_str());
129 
131  std::string retrieveMethod;
132  ::boost::tie(success, retrieveMethod) = config->getSafeAttributeValue("retrieveMethod");
133  SLM_ASSERT("It should be a \"retrieveMethod\" tag in the "
134  "::ioPacs::SPacsConfigurationInitializer config element.", success);
135  m_retrieveMethod = (retrieveMethod == "GET") ?
136  (::fwPacsIO::data::PacsConfiguration::GET_RETRIEVE_METHOD):
137  (::fwPacsIO::data::PacsConfiguration::MOVE_RETRIEVE_METHOD);
138 
140  m_preferenceKey = config->getAttributeValue("preferenceKey");
141 
142  ::fwPacsIO::data::PacsConfiguration::sptr pacsConfiguration = this->getInOut< ::fwPacsIO::data::PacsConfiguration >(
143  s_CONFIG_INOUT);
144  if (!pacsConfiguration)
145  {
146  FW_DEPRECATED_KEY(s_CONFIG_INOUT, "inout", "fw4spl_18.0");
147  pacsConfiguration = this->getObject< ::fwPacsIO::data::PacsConfiguration >();
148  }
149 
150  // Set information from xml and update PacsConfiguration
151  if(!m_preferenceKey.empty())
152  {
153  ::fwData::Composite::sptr prefs = ::fwPreferences::getPreferences();
154  if(prefs)
155  {
156  ::fwData::Composite::sptr config = ::fwData::Composite::dynamicCast((*prefs)[m_preferenceKey]);
157 
158  SetFromConfig< std::string > setFromConfig;
159  SetFromConfig< unsigned short > setFromConfigShort;
161 
162  setFromConfig(config, "LocalApplicationTitle", m_localApplicationTitle);
163  setFromConfig(config, "PacsHostName", m_pacsHostName);
164  setFromConfig(config, "PacsApplicationTitle", m_pacsApplicationTitle);
165  setFromConfigShort(config, "PacsApplicationPort", m_pacsApplicationPort);
166  setFromConfig(config, "MoveApplicationTitle", m_moveApplicationTitle);
167  setFromConfigShort(config, "MoveApplicationPort", m_moveApplicationPort);
168  setFromConfigEnum(config, "RetrieveMethod", m_retrieveMethod);
169  }
170  }
171 
172  pacsConfiguration->setLocalApplicationTitle(m_localApplicationTitle);
173  pacsConfiguration->setPacsHostName(m_pacsHostName);
174  pacsConfiguration->setPacsApplicationTitle(m_pacsApplicationTitle);
175  pacsConfiguration->setPacsApplicationPort(m_pacsApplicationPort);
176  pacsConfiguration->setMoveApplicationTitle(m_moveApplicationTitle);
177  pacsConfiguration->setMoveApplicationPort(m_moveApplicationPort);
178  pacsConfiguration->setRetrieveMethod(m_retrieveMethod);
179 }
180 
181 //------------------------------------------------------------------------------
182 
184 {
185  ::fwPacsIO::data::PacsConfiguration::sptr pacsConfiguration = this->getInOut< ::fwPacsIO::data::PacsConfiguration >(
186  s_CONFIG_INOUT);
187  if (!pacsConfiguration)
188  {
189  FW_DEPRECATED_KEY(s_CONFIG_INOUT, "inout", "fw4spl_18.0");
190  pacsConfiguration = this->getObject< ::fwPacsIO::data::PacsConfiguration >();
191  }
192 
193  // Check if the user has changed the Pacs configuration and update the local var
194  if(pacsConfiguration->getLocalApplicationTitle() != m_localApplicationTitle
195  || pacsConfiguration->getPacsHostName() != m_pacsHostName
196  || pacsConfiguration->getPacsApplicationTitle() != m_pacsApplicationTitle
197  || pacsConfiguration->getPacsApplicationPort() != m_pacsApplicationPort
198  || pacsConfiguration->getMoveApplicationTitle() != m_moveApplicationTitle
199  || pacsConfiguration->getMoveApplicationPort() != m_moveApplicationPort
200  || pacsConfiguration->getRetrieveMethod() != m_retrieveMethod
201  )
202  {
203  m_localApplicationTitle = pacsConfiguration->getLocalApplicationTitle();
204  m_pacsHostName = pacsConfiguration->getPacsHostName();
205  m_pacsApplicationTitle = pacsConfiguration->getPacsApplicationTitle();
206  m_pacsApplicationPort = pacsConfiguration->getPacsApplicationPort();
207  m_moveApplicationTitle = pacsConfiguration->getMoveApplicationTitle();
208  m_moveApplicationPort = pacsConfiguration->getMoveApplicationPort();
209  m_retrieveMethod = pacsConfiguration->getRetrieveMethod();
210  }
211 
212  // If a preference key is set, save the local var to the preferences
213  if(!m_preferenceKey.empty())
214  {
215  ::fwData::Composite::sptr prefs = ::fwPreferences::getPreferences();
216  if(prefs && (prefs->find(m_preferenceKey) == prefs->end() || !(*prefs)[m_preferenceKey]))
217  {
218  (*prefs)[m_preferenceKey] = ::fwData::Composite::New();
219  }
220  if(prefs)
221  {
222  ::fwData::Composite::sptr config = ::fwData::Composite::dynamicCast((*prefs)[m_preferenceKey]);
223  (*config)["LocalApplicationTitle"] = ::fwData::String::New(m_localApplicationTitle);
224  (*config)["PacsHostName" ] = ::fwData::String::New(m_pacsHostName);
225  (*config)["PacsApplicationTitle" ] = ::fwData::String::New(m_pacsApplicationTitle);
226  (*config)["PacsApplicationPort" ] =
227  ::fwData::String::New(::boost::lexical_cast<std::string>(m_pacsApplicationPort));
228  (*config)["MoveApplicationTitle" ] = ::fwData::String::New(m_moveApplicationTitle);
229  (*config)["MoveApplicationPort" ] =
230  ::fwData::String::New(::boost::lexical_cast<std::string>(m_moveApplicationPort));
231  (*config)["RetrieveMethod" ] =
232  ::fwData::String::New(::boost::lexical_cast<std::string>(m_retrieveMethod));
233  }
234  ::fwPreferences::savePreferences();
235  }
236 }
237 
238 } // namespace ioPacs
#define FW_DEPRECATED_KEY(newKey, access, version)
Use this macro when deprecating a service key to warn the developer.
Definition: spyLog.hpp:366
std::string m_preferenceKey
The key to save/load the preferences.
ioPacs contains services use to deal with PACS using DCMTK library.
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
IOPACS_API SPacsConfigurationInitializer() noexcept
Constructor.
This class defines a Pacs configuration.
unsigned short m_moveApplicationPort
Move application port.
::fwPacsIO::data::PacsConfiguration::RETRIEVE_METHOD m_retrieveMethod
Request mode.
virtual IOPACS_API void configuring() override
Configuring method. This method is used to configure the service.
This service is used to initialize a Pacs Configuration data.
std::string m_moveApplicationTitle
Move application title.
This interface defines control service API. Does nothing particularly, can be considered as a default...
Definition: IController.hpp:23
std::string m_pacsApplicationTitle
Pacs application title.
#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
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
Definition: IService.hpp:670
IOPACS_API void info(std::ostream &_sstream) override
Override.
virtual IOPACS_API ~SPacsConfigurationInitializer() noexcept
Destructor.
std::string m_localApplicationTitle
Local application title.
virtual IOPACS_API void starting() override
Override.
virtual IOPACS_API void stopping() override
Override.
IOPACS_API void updating() override
Override.