1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // SalomeApp_Engine_i : implementation of SalomeApp_Engine.idl
23 // File : SalomeApp_Engine_i.cxx
24 // Author : Alexander SLADKOV
28 #include "SalomeApp_Engine_i.hxx"
30 #include "SALOMEDS_Tool.hxx"
36 SalomeApp_Engine_i* SalomeApp_Engine_i::myInstance = NULL;
41 SalomeApp_Engine_i::SalomeApp_Engine_i()
49 SalomeApp_Engine_i::~SalomeApp_Engine_i()
53 SALOMEDS::TMPFile* SalomeApp_Engine_i::Save (SALOMEDS::SComponent_ptr theComponent,
57 SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
59 cout << "SalomeApp_Engine_i::Save() isMultiFile = " << isMultiFile << endl;
60 if (CORBA::is_nil(theComponent) || CORBA::is_nil(theComponent->GetStudy()))
61 return aStreamFile._retn();
63 const int studyId = theComponent->GetStudy()->StudyId();
64 cout << "SalomeApp_Engine_i::Save() - studyId = " << studyId << endl;
66 // Get a temporary directory to store a file
67 //std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
69 if (myMap.count(studyId)) {
70 cout << "SalomeApp_Engine_i::Save() - myMap.count(studyId)" << endl;
71 MapOfListOfFiles mapOfListOfFiles = myMap[studyId];
72 std::string componentName (theComponent->ComponentDataType());
73 cout << "SalomeApp_Engine_i::Save() - componentName = " << componentName << endl;
74 ListOfFiles listOfFiles = mapOfListOfFiles[componentName];
76 // listOfFiles must contain temporary directory name in its first item
77 // and names of files (relatively the temporary directory) in the others
78 const int n = listOfFiles.size() - 1;
80 if (n > 0) { // there are some files, containing persistent data of the component
81 std::string aTmpDir = listOfFiles[0];
82 cout << "SalomeApp_Engine_i::Save() - aTmpDir = " << aTmpDir << endl;
84 // Create a list to store names of created files
85 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
87 for (int i = 0; i < n; i++)
88 aSeq[i] = CORBA::string_dup(listOfFiles[i + 1].c_str());
90 // Convert a file to the byte stream
91 aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.c_str(), aSeq.in(), isMultiFile);
93 // Remove the files and tmp directory, created by the component storage procedure
94 if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
98 return aStreamFile._retn();
101 CORBA::Boolean SalomeApp_Engine_i::Load (SALOMEDS::SComponent_ptr theComponent,
102 const SALOMEDS::TMPFile& theFile,
106 cout << "SalomeApp_Engine_i::Load() isMultiFile = " << isMultiFile << endl;
107 if (CORBA::is_nil(theComponent) || CORBA::is_nil(theComponent->GetStudy()))
110 const int studyId = theComponent->GetStudy()->StudyId();
112 // Create a temporary directory for the component's data files
113 std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
115 // Convert the byte stream theStream to a files and place them in the tmp directory.
116 // The files and temporary directory must be deleted by the component loading procedure.
117 SALOMEDS::ListOfFileNames_var aSeq =
118 SALOMEDS_Tool::PutStreamToFiles(theFile, aTmpDir.c_str(), isMultiFile);
120 // Store list of file names to be used by the component loading procedure
121 const int n = aSeq->length() + 1;
122 ListOfFiles listOfFiles (n);
123 listOfFiles[0] = aTmpDir;
124 for (int i = 1; i < n; i++)
125 listOfFiles[i] = std::string(aSeq[i - 1]);
127 //MapOfListOfFiles mapOfListOfFiles;
128 //if (myMap.count(studyId))
129 // mapOfListOfFiles = myMap[studyId];
130 //std::string componentName (theComponent->ComponentDataType());
131 //mapOfListOfFiles[componentName] = listOfFiles;
132 //myMap[studyId] = mapOfListOfFiles;
134 SetListOfFiles(listOfFiles, studyId, theComponent->ComponentDataType());
139 SalomeApp_Engine_i::ListOfFiles SalomeApp_Engine_i::GetListOfFiles (const int theStudyId,
140 const char* theComponentName)
142 ListOfFiles aListOfFiles;
144 if (myMap.count(theStudyId))
146 MapOfListOfFiles mapOfListOfFiles = myMap[theStudyId];
147 std::string componentName (theComponentName);
148 if (mapOfListOfFiles.count(componentName))
149 aListOfFiles = mapOfListOfFiles[componentName];
155 void SalomeApp_Engine_i::SetListOfFiles (const ListOfFiles theListOfFiles,
156 const int theStudyId,
157 const char* theComponentName)
159 //if (!myMap.count(theStudyId)) {
160 // MapOfListOfFiles mapOfListOfFiles;
161 // myMap[theStudyId] = mapOfListOfFiles;
164 MapOfListOfFiles& mapOfListOfFiles = myMap[theStudyId];
165 std::string componentName (theComponentName);
166 mapOfListOfFiles[componentName] = theListOfFiles;
170 \return shared instance of engine
172 SalomeApp_Engine_i* SalomeApp_Engine_i::GetInstance()