19 #include <itksys/SystemTools.hxx> 23 return "PersistenceNode";
28 return "PersistenceService";
38 m_PropertyLists.clear();
39 m_FileNamesToModifiedTimes.clear();
44 MITK_DEBUG(
"mitk::PersistenceService") <<
"destructing PersistenceService";
50 std::string file =
"PersistentData.xml";
52 std::string contextDataFile = context->GetDataFile(file);
54 if (!contextDataFile.empty())
56 file = contextDataFile;
72 auto it = m_PropertyLists.find(
id);
73 if (it == m_PropertyLists.end())
76 m_PropertyLists[id] = propList;
82 propList = (*it).second;
94 const std::map<std::string, BaseProperty::Pointer> *propMap = from->
GetMap();
95 auto propMapIt = propMap->begin();
96 while (propMapIt != propMap->end())
108 std::string theFile = fileName;
112 std::string thePath = itksys::SystemTools::GetFilenamePath(theFile.c_str());
113 if (!thePath.empty() && !itksys::SystemTools::FileExists(thePath.c_str()))
115 if (!itksys::SystemTools::MakeDirectory(thePath.c_str()))
117 MITK_ERROR(
"PersistenceService") <<
"Could not create " << thePath;
122 bool createFile = !itksys::SystemTools::FileExists(theFile.c_str());
123 if (!itksys::SystemTools::Touch(theFile.c_str(), createFile))
125 MITK_ERROR(
"PersistenceService") <<
"Could not create or write to " << theFile;
129 bool xmlFile =
false;
130 if (itksys::SystemTools::GetFilenameLastExtension(theFile.c_str()) ==
".xml")
136 if (xmlFile ==
false)
138 if (itksys::SystemTools::FileExists(theFile.c_str()))
142 load = (m_SceneIO->GetFailedNodes() ==
nullptr || m_SceneIO->GetFailedNodes()->size() == 0) &&
143 (m_SceneIO->GetFailedNodes() ==
nullptr || m_SceneIO->GetFailedProperties()->IsEmpty());
153 if (xmlFile && appendChanges && itksys::SystemTools::FileExists(theFile.c_str()))
155 if (!m_PropertyListsXmlFileReaderAndWriter->ReadLists(theFile, m_PropertyLists))
162 else if (xmlFile ==
false)
169 save = m_PropertyListsXmlFileReaderAndWriter->WriteLists(theFile, m_PropertyLists);
174 DataStorage::SetOfObjects::Pointer sceneNodes = this->
GetDataNodes(tempDs);
175 if (m_SceneIO.IsNull())
179 save = m_SceneIO->SaveScene(sceneNodes.GetPointer(), tempDs, theFile);
183 long int currentModifiedTime = itksys::SystemTools::ModifiedTime(theFile.c_str());
184 m_FileNamesToModifiedTimes[theFile] = currentModifiedTime;
195 std::string theFile = fileName;
199 MITK_DEBUG <<
"Load persistence data from file: " << theFile;
201 if (!itksys::SystemTools::FileExists(theFile.c_str()))
204 bool xmlFile =
false;
205 if (itksys::SystemTools::GetFilenameLastExtension(theFile.c_str()) ==
".xml")
208 if (enforceReload ==
false)
210 bool loadTheFile =
true;
211 auto it = m_FileNamesToModifiedTimes.find(theFile);
213 long int currentModifiedTime = itksys::SystemTools::ModifiedTime(theFile.c_str());
214 if (it != m_FileNamesToModifiedTimes.end())
216 long int knownModifiedTime = (*it).second;
217 if (knownModifiedTime >= currentModifiedTime)
223 m_FileNamesToModifiedTimes[theFile] = currentModifiedTime;
230 load = m_PropertyListsXmlFileReaderAndWriter->ReadLists(theFile, m_PropertyLists);
234 if (m_SceneIO.IsNull())
239 load = (m_SceneIO->GetFailedNodes() ==
nullptr || m_SceneIO->GetFailedNodes()->size() == 0) &&
240 (m_SceneIO->GetFailedNodes() ==
nullptr || m_SceneIO->GetFailedProperties()->IsEmpty());
248 MITK_DEBUG(
"mitk::PersistenceService") <<
"loading of scene files failed";
258 m_AutoLoadAndSave = autoLoadAndSave;
262 propList->Set(
"m_AutoLoadAndSave", m_AutoLoadAndSave);
269 m_PropertyListReplacedObserver.insert(observer);
275 m_PropertyListReplacedObserver.erase(observer);
297 DataStorage::SetOfObjects::Pointer
set = DataStorage::SetOfObjects::New();
299 std::map<std::string, mitk::PropertyList::Pointer>::const_iterator it = m_PropertyLists.begin();
300 while (it != m_PropertyLists.end())
303 const std::string &name = (*it).first;
305 this->ClonePropertyList((*it).second, node->GetPropertyList());
313 set->push_back(node);
323 bool oneFound =
false;
324 DataStorage::SetOfObjects::ConstPointer allNodes = storage->
GetAll();
325 for (
auto sourceIter = allNodes->begin(); sourceIter != allNodes->end();
329 bool isPersistenceNode =
false;
333 if (isPersistenceNode)
336 MITK_DEBUG(
"mitk::PersistenceService") <<
"isPersistenceNode was true";
337 std::string name = node->
GetName();
339 bool existed =
false;
344 MITK_DEBUG(
"mitk::PersistenceService") <<
"calling replace observer before replacing values";
345 auto it = m_PropertyListReplacedObserver.begin();
346 while (it != m_PropertyListReplacedObserver.end())
348 (*it)->BeforePropertyListReplaced(name, propList);
353 MITK_DEBUG(
"mitk::PersistenceService") <<
"replacing values";
359 MITK_DEBUG(
"mitk::PersistenceService") <<
"calling replace observer before replacing values";
360 auto it = m_PropertyListReplacedObserver.begin();
361 while (it != m_PropertyListReplacedObserver.end())
363 (*it)->AfterPropertyListReplaced(name, propList);
376 return m_AutoLoadAndSave;
382 auto it = m_PropertyLists.find(
id);
383 if (it != m_PropertyLists.end())
385 m_PropertyLists.erase(it);
391 void mitk::PersistenceService::Initialize()
393 if (m_Initialized || m_InInitialized)
395 m_InInitialized =
true;
404 bool autoLoadAndSave =
true;
405 propList->GetBoolProperty(
"m_AutoLoadAndSave", autoLoadAndSave);
407 if (autoLoadAndSave ==
false)
409 MITK_DEBUG(
"mitk::PersistenceService") <<
"autoloading was not wished. clearing data we got so far.";
414 m_InInitialized =
false;
415 m_Initialized =
true;
Data management class that handles 'was created by' relations.
void SetAutoLoadAndSave(bool autoLoadAndSave) override
static us::ModuleContext * GetModuleContext()
~PersistenceService() override
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()
bool GetBoolProperty(const char *propertyKey, bool &boolValue, const mitk::BaseRenderer *renderer=nullptr) const
Convenience access method for bool properties (instances of BoolProperty)
virtual void Add(DataNode *node, const DataStorage::SetOfObjects *parents=nullptr)=0
Adds a DataNode containing a data object to its internal storage.
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
void SetProperty(const std::string &propertyKey, BaseProperty *property, const std::string &contextName="", bool fallBackOnDefaultContext=false) override
Add new or change existent property.
static std::string GetPersistencePropertyListName()
bool RemovePropertyList(std::string &id) override
bool GetAutoLoadAndSave() override
mitk::PropertyList * GetPropertyList(const mitk::BaseRenderer *renderer=nullptr) const
Get the PropertyList of the renderer. If renderer is nullptr, the BaseRenderer-independent PropertyLi...
DataStorage::SetOfObjects::Pointer GetDataNodes(DataStorage *ds=nullptr) override
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
mitk::PropertyList::Pointer GetPropertyList(std::string &id, bool *existed=nullptr) override
std::string GetDefaultPersistenceFile() override
Class for nodes of the DataTree.
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...
const PropertyMap * GetMap() const