23 #include <itksys/SystemTools.hxx>
27 return "PersistenceNode";
32 return "PersistenceService";
42 m_PropertyLists.clear();
43 m_FileNamesToModifiedTimes.clear();
48 MITK_DEBUG(
"mitk::PersistenceService") <<
"destructing PersistenceService";
54 std::string file =
"PersistentData.xml";
56 std::string contextDataFile = context->GetDataFile(file);
58 if (!contextDataFile.empty())
60 file = contextDataFile;
76 std::map<std::string, mitk::PropertyList::Pointer>::iterator it = m_PropertyLists.find(
id);
77 if (it == m_PropertyLists.end())
80 m_PropertyLists[id] = propList;
86 propList = (*it).second;
98 const std::map<std::string, BaseProperty::Pointer> *propMap = from->
GetMap();
99 std::map<std::string, BaseProperty::Pointer>::const_iterator propMapIt = propMap->begin();
100 while (propMapIt != propMap->end())
112 std::string theFile = fileName;
116 std::string thePath = itksys::SystemTools::GetFilenamePath(theFile.c_str());
117 if (!thePath.empty() && !itksys::SystemTools::FileExists(thePath.c_str()))
119 if (!itksys::SystemTools::MakeDirectory(thePath.c_str()))
121 MITK_ERROR(
"PersistenceService") <<
"Could not create " << thePath;
126 bool createFile = !itksys::SystemTools::FileExists(theFile.c_str());
127 if (!itksys::SystemTools::Touch(theFile.c_str(), createFile))
129 MITK_ERROR(
"PersistenceService") <<
"Could not create or write to " << theFile;
133 bool xmlFile =
false;
134 if (itksys::SystemTools::GetFilenameLastExtension(theFile.c_str()) ==
".xml")
140 if (xmlFile ==
false)
142 if (itksys::SystemTools::FileExists(theFile.c_str()))
146 load = (m_SceneIO->GetFailedNodes() == 0 || m_SceneIO->GetFailedNodes()->size() == 0) &&
147 (m_SceneIO->GetFailedNodes() == 0 || m_SceneIO->GetFailedProperties()->IsEmpty());
157 if (xmlFile && appendChanges && itksys::SystemTools::FileExists(theFile.c_str()))
159 if (!m_PropertyListsXmlFileReaderAndWriter->ReadLists(theFile, m_PropertyLists))
164 this->RestorePropertyListsFromPersistentDataNodes(tempDs);
166 else if (xmlFile ==
false)
173 save = m_PropertyListsXmlFileReaderAndWriter->WriteLists(theFile, m_PropertyLists);
179 if (m_SceneIO.IsNull())
183 save = m_SceneIO->SaveScene(sceneNodes.GetPointer(), tempDs, theFile);
187 long int currentModifiedTime = itksys::SystemTools::ModifiedTime(theFile.c_str());
188 m_FileNamesToModifiedTimes[theFile] = currentModifiedTime;
199 std::string theFile = fileName;
203 MITK_DEBUG <<
"Load persistence data from file: " << theFile;
205 if (!itksys::SystemTools::FileExists(theFile.c_str()))
208 bool xmlFile =
false;
209 if (itksys::SystemTools::GetFilenameLastExtension(theFile.c_str()) ==
".xml")
212 if (enforceReload ==
false)
214 bool loadTheFile =
true;
215 std::map<std::string, long int>::iterator it = m_FileNamesToModifiedTimes.find(theFile);
217 long int currentModifiedTime = itksys::SystemTools::ModifiedTime(theFile.c_str());
218 if (it != m_FileNamesToModifiedTimes.end())
220 long int knownModifiedTime = (*it).second;
221 if (knownModifiedTime >= currentModifiedTime)
227 m_FileNamesToModifiedTimes[theFile] = currentModifiedTime;
234 load = m_PropertyListsXmlFileReaderAndWriter->ReadLists(theFile, m_PropertyLists);
238 if (m_SceneIO.IsNull())
243 load = (m_SceneIO->GetFailedNodes() == 0 || m_SceneIO->GetFailedNodes()->size() == 0) &&
244 (m_SceneIO->GetFailedNodes() == 0 || m_SceneIO->GetFailedProperties()->IsEmpty());
247 this->RestorePropertyListsFromPersistentDataNodes(ds);
252 MITK_DEBUG(
"mitk::PersistenceService") <<
"loading of scene files failed";
262 m_AutoLoadAndSave = autoLoadAndSave;
264 std::string
id = GetPersistencePropertyListName();
266 propList->Set(
"m_AutoLoadAndSave", m_AutoLoadAndSave);
273 m_PropertyListReplacedObserver.insert(observer);
279 m_PropertyListReplacedObserver.erase(observer);
296 return GetPersistencePropertyName();
303 std::map<std::string, mitk::PropertyList::Pointer>::const_iterator it = m_PropertyLists.begin();
304 while (it != m_PropertyLists.end())
307 const std::string &name = (*it).first;
309 this->ClonePropertyList((*it).second, node->GetPropertyList());
312 MITK_DEBUG <<
"Persistence Property Name: " << GetPersistencePropertyName().c_str();
314 node->SetBoolProperty(GetPersistencePropertyName().c_str(),
true);
317 set->push_back(node);
327 bool oneFound =
false;
329 for (mitk::DataStorage::SetOfObjects::const_iterator sourceIter = allNodes->begin(); sourceIter != allNodes->end();
333 bool isPersistenceNode =
false;
335 node->
GetBoolProperty(GetPersistencePropertyName().c_str(), isPersistenceNode);
337 if (isPersistenceNode)
340 MITK_DEBUG(
"mitk::PersistenceService") <<
"isPersistenceNode was true";
341 std::string name = node->
GetName();
343 bool existed =
false;
348 MITK_DEBUG(
"mitk::PersistenceService") <<
"calling replace observer before replacing values";
349 std::set<PropertyListReplacedObserver *>::iterator it = m_PropertyListReplacedObserver.begin();
350 while (it != m_PropertyListReplacedObserver.end())
352 (*it)->BeforePropertyListReplaced(name, propList);
357 MITK_DEBUG(
"mitk::PersistenceService") <<
"replacing values";
363 MITK_DEBUG(
"mitk::PersistenceService") <<
"calling replace observer before replacing values";
364 std::set<PropertyListReplacedObserver *>::iterator it = m_PropertyListReplacedObserver.begin();
365 while (it != m_PropertyListReplacedObserver.end())
367 (*it)->AfterPropertyListReplaced(name, propList);
380 return m_AutoLoadAndSave;
386 std::map<std::string, mitk::PropertyList::Pointer>::iterator it = m_PropertyLists.find(
id);
387 if (it != m_PropertyLists.end())
389 m_PropertyLists.erase(it);
395 void mitk::PersistenceService::Initialize()
397 if (m_Initialized || m_InInitialized)
399 m_InInitialized =
true;
406 std::string
id = GetPersistencePropertyListName();
408 bool autoLoadAndSave =
true;
409 propList->GetBoolProperty(
"m_AutoLoadAndSave", autoLoadAndSave);
411 if (autoLoadAndSave ==
false)
413 MITK_DEBUG(
"mitk::PersistenceService") <<
"autoloading was not wished. clearing data we got so far.";
414 this->SetAutoLoadAndSave(
false);
418 m_InInitialized =
false;
419 m_Initialized =
true;
424 if (this->GetAutoLoadAndSave())
DataStorage::SetOfObjects::Pointer GetDataNodes(DataStorage *ds=0) override
virtual void Add(mitk::DataNode *node, const mitk::DataStorage::SetOfObjects *parents=nullptr)=0
Adds a DataNode containing a data object to its internal storage.
mitk::PropertyList * GetPropertyList(const mitk::BaseRenderer *renderer=nullptr) const
Get the PropertyList of the renderer. If renderer is NULL, the BaseRenderer-independent PropertyList ...
Data management class that handles 'was created by' relations.
itk::SmartPointer< Self > Pointer
void SetAutoLoadAndSave(bool autoLoadAndSave) override
static us::ModuleContext * GetModuleContext()
void SetProperty(const std::string &propertyKey, BaseProperty *property)
Set a property in the list/map by value.
bool GetName(std::string &nodeName, const mitk::BaseRenderer *renderer=nullptr, const char *propertyKey="name") const
Convenience access method for accessing the name of an object (instance of StringProperty with proper...
bool GetBoolProperty(const char *propertyKey, bool &boolValue, const mitk::BaseRenderer *renderer=nullptr) const
Convenience access method for bool properties (instances of BoolProperty)
void AddPropertyListReplacedObserver(PropertyListReplacedObserver *observer) override
Key-value list holding instances of BaseProperty.
bool Save(const std::string &fileName="", bool appendChanges=false) override
bool Load(const std::string &fileName="", bool enforeReload=true) override
static std::string GetPersistencePropertyName()
itk::SmartPointer< const Self > ConstPointer
void RemovePropertyListReplacedObserver(PropertyListReplacedObserver *observer) override
std::string GetPersistenceNodePropertyName() override
virtual SetOfObjects::ConstPointer GetAll() const =0
returns a set of all data objects that are stored in the data storage
bool RestorePropertyListsFromPersistentDataNodes(const DataStorage *storage) override
static std::string GetPersistencePropertyListName()
bool RemovePropertyList(std::string &id) override
bool GetAutoLoadAndSave() override
mitk::PropertyList::Pointer GetPropertyList(std::string &id, bool *existed=0) override
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
std::string GetDefaultPersistenceFile() override
Class for nodes of the DataTree.
const PropertyMap * GetMap() const
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.