Salome HOME
SWP12966 - MutiFile is not saved correctly
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_StudyManager.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //  File   : SALOMEDSClient_StudyManager.cxx
21 //  Author : Sergey RUIN
22 //  Module : SALOME
23
24
25 #include "SALOMEDS_StudyManager.hxx"
26
27 #include "SALOMEDS.hxx"
28 #include "SALOMEDS_Study.hxx"
29 #include "SALOMEDS_SObject.hxx"
30
31 #include "SALOMEDS_Driver_i.hxx"
32
33 #include "SALOMEDSImpl_Study.hxx"
34
35 #include "Utils_ORB_INIT.hxx" 
36 #include "Utils_SINGLETON.hxx" 
37
38 #ifdef WIN32
39 #include <process.h>
40 #else
41 #include <sys/types.h>
42 #include <unistd.h>
43 #endif
44
45 #include <string>
46 #include <TCollection_AsciiString.hxx> 
47 #include <TColStd_HSequenceOfTransient.hxx>
48
49 #include "OpUtil.hxx"
50
51 using namespace std;
52
53 SALOMEDS_Driver_i* GetDriver(const Handle(SALOMEDSImpl_SObject)& theObject, CORBA::ORB_ptr orb);
54
55 SALOMEDS_StudyManager::SALOMEDS_StudyManager(SALOMEDS::StudyManager_ptr theManager)
56 {
57
58 #ifdef WIN32
59   long pid =  (long)_getpid();
60 #else
61   long pid =  (long)getpid();
62 #endif  
63
64   long addr = theManager->GetLocalImpl(GetHostname().c_str(), pid, _isLocal);
65   if(_isLocal) {
66     _local_impl = ((SALOMEDSImpl_StudyManager*)(addr));
67     _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
68   }
69   else {
70     _local_impl = NULL;
71     _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
72   }
73
74   init_orb();
75 }
76
77 SALOMEDS_StudyManager::SALOMEDS_StudyManager()
78 {
79   init_orb();
80
81   SALOME_NamingService namingService(_orb);
82   CORBA::Object_var obj = namingService.Resolve( "/myStudyManager" );
83   SALOMEDS::StudyManager_var theManager = SALOMEDS::StudyManager::_narrow( obj );
84   ASSERT( !CORBA::is_nil(theManager) );
85
86 #ifdef WIN32
87   long pid =  (long)_getpid();
88 #else
89   long pid =  (long)getpid();
90 #endif  
91
92   long addr = theManager->GetLocalImpl(GetHostname().c_str(), pid, _isLocal);
93   if(_isLocal) {
94     _local_impl = ((SALOMEDSImpl_StudyManager*)(addr));
95     _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
96   }
97   else {
98     _local_impl = NULL;
99     _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
100   }
101 }
102
103 SALOMEDS_StudyManager::~SALOMEDS_StudyManager()
104 {
105 }
106
107 _PTR(Study) SALOMEDS_StudyManager::NewStudy(const std::string& study_name)
108 {
109   //SRN: Pure CORBA NewStudy as it does more initialization than the local one   
110   SALOMEDSClient_Study* aStudy = NULL;
111
112   SALOMEDS::Study_var aStudy_impl = _corba_impl->NewStudy((char*)study_name.c_str());
113   if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy);
114   aStudy = new SALOMEDS_Study(aStudy_impl);
115
116   return _PTR(Study)(aStudy);
117 }
118
119 _PTR(Study) SALOMEDS_StudyManager::Open(const std::string& theStudyUrl)
120 {
121   //SRN: Pure CORBA Open as it does more initialization than the local one   
122   SALOMEDSClient_Study* aStudy = NULL;
123
124   SALOMEDS::Study_var aStudy_impl = _corba_impl->Open((char*)theStudyUrl.c_str());
125   if(CORBA::is_nil(aStudy_impl)) return  _PTR(Study)(aStudy);
126     
127   aStudy = new SALOMEDS_Study(aStudy_impl.in());
128
129   return _PTR(Study)(aStudy);
130 }
131  
132 void SALOMEDS_StudyManager::Close(const _PTR(Study)& theStudy)
133 {
134   //SRN: Pure CORBA close as it does more cleaning than the local one
135   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
136   _corba_impl->Close(aStudy);
137 }
138  
139 bool SALOMEDS_StudyManager::Save(const _PTR(Study)& theStudy, bool theMultiFile)
140 {
141   //SRN: Pure CORBA save as the save operation require CORBA in any case 
142   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
143   return _corba_impl->Save(aStudy, theMultiFile);
144 }
145  
146 bool SALOMEDS_StudyManager::SaveASCII(const _PTR(Study)& theStudy, bool theMultiFile)
147 {
148   //SRN: Pure CORBA save as the save operation require CORBA in any case 
149   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
150   return _corba_impl->SaveASCII(aStudy, theMultiFile);
151 }
152  
153 bool SALOMEDS_StudyManager::SaveAs(const std::string& theUrl,  const _PTR(Study)& theStudy, bool theMultiFile)
154 {
155   //SRN: Pure CORBA save as the save operation require CORBA in any case 
156   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
157   return _corba_impl->SaveAs((char*)theUrl.c_str(), aStudy, theMultiFile);
158 }
159  
160 bool SALOMEDS_StudyManager::SaveAsASCII(const std::string& theUrl,  const _PTR(Study)& theStudy, bool theMultiFile)
161 {
162   //SRN: Pure CORBA save as the save operation require CORBA in any case 
163   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
164   return _corba_impl->SaveAsASCII((char*)theUrl.c_str(), aStudy, theMultiFile);
165 }
166
167 std::vector<std::string> SALOMEDS_StudyManager::GetOpenStudies()
168 {
169   std::vector<std::string> aVector;
170   int aLength, i;
171
172   if (_isLocal) {
173     SALOMEDS::Locker lock;
174
175     Handle(TColStd_HSequenceOfTransient) aSeq = _local_impl->GetOpenStudies();
176     aLength = aSeq->Length();
177     for(i = 1; i <= aLength; i++) 
178       aVector.push_back(Handle(SALOMEDSImpl_Study)::DownCast(aSeq->Value(i))->Name().ToCString());
179   }
180   else {
181     SALOMEDS::ListOfOpenStudies_var aSeq = _corba_impl->GetOpenStudies();
182     aLength = aSeq->length();
183     for(i = 0; i < aLength; i++) 
184       aVector.push_back(aSeq[i].in());
185   }
186   return aVector;
187 }
188
189 _PTR(Study) SALOMEDS_StudyManager::GetStudyByName(const std::string& theStudyName) 
190 {
191   SALOMEDSClient_Study* aStudy = NULL;
192   if (_isLocal) {
193     SALOMEDS::Locker lock;
194
195     Handle(SALOMEDSImpl_Study) aStudy_impl = _local_impl->GetStudyByName((char*)theStudyName.c_str());
196     if(aStudy_impl.IsNull()) return _PTR(Study)(aStudy);
197     aStudy = new SALOMEDS_Study(aStudy_impl);
198   }
199   else  {
200     SALOMEDS::Study_var aStudy_impl = _corba_impl->GetStudyByName((char*)theStudyName.c_str());
201     if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy);
202     aStudy = new SALOMEDS_Study(aStudy_impl);
203   }
204   return _PTR(Study)(aStudy);
205 }
206
207 _PTR(Study) SALOMEDS_StudyManager::GetStudyByID(int theStudyID) 
208 {
209   SALOMEDSClient_Study* aStudy = NULL;
210   if (_isLocal) {
211     SALOMEDS::Locker lock;
212
213     Handle(SALOMEDSImpl_Study) aStudy_impl = _local_impl->GetStudyByID(theStudyID);
214     if(aStudy_impl.IsNull()) return _PTR(Study)(aStudy);
215     aStudy = new SALOMEDS_Study(aStudy_impl);
216   }
217   else { 
218     SALOMEDS::Study_var aStudy_impl = _corba_impl->GetStudyByID(theStudyID);
219     if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy);
220     aStudy = new SALOMEDS_Study(aStudy_impl);
221   }
222   return _PTR(Study)(aStudy);
223 }
224
225 bool SALOMEDS_StudyManager::CanCopy(const _PTR(SObject)& theSO)
226 {
227   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
228   bool ret;
229
230   if (_isLocal) {
231     SALOMEDS::Locker lock;
232
233     Handle(SALOMEDSImpl_SObject) aSO_impl = aSO->GetLocalImpl();
234     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
235     ret = _local_impl->CanCopy(aSO_impl, aDriver);
236     delete aDriver;
237   }
238   else {
239     ret = _corba_impl->CanCopy(aSO->GetCORBAImpl());
240   }
241
242   return ret;
243 }
244
245 bool SALOMEDS_StudyManager::Copy(const _PTR(SObject)& theSO)
246 {
247   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
248   bool ret;
249   if (_isLocal) {
250     SALOMEDS::Locker lock;
251
252     Handle(SALOMEDSImpl_SObject) aSO_impl = aSO->GetLocalImpl();
253     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
254     ret = _local_impl->Copy(aSO_impl, aDriver);
255     delete aDriver;
256   }
257   else {
258     ret = _corba_impl->Copy(aSO->GetCORBAImpl());
259   }
260   return ret;
261 }
262
263 bool SALOMEDS_StudyManager::CanPaste(const _PTR(SObject)& theSO)
264 {
265   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
266   bool ret;
267
268   if (_isLocal) {
269     SALOMEDS::Locker lock;
270
271     Handle(SALOMEDSImpl_SObject) aSO_impl = aSO->GetLocalImpl();
272     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
273     ret = _local_impl->CanPaste(aSO_impl, aDriver);
274     delete aDriver;
275   }
276   else {
277     ret = _corba_impl->CanPaste(aSO->GetCORBAImpl());
278   }
279
280   return ret;
281 }
282
283 _PTR(SObject) SALOMEDS_StudyManager::Paste(const _PTR(SObject)& theSO)
284 {
285   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
286   SALOMEDSClient_SObject* aResult = NULL;
287
288   if (_isLocal) {
289     SALOMEDS::Locker lock;
290
291     Handle(SALOMEDSImpl_SObject) aSO_impl = aSO->GetLocalImpl();
292     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
293     Handle(SALOMEDSImpl_SObject) aNewSO = _local_impl->Paste(aSO_impl, aDriver);
294     delete aDriver;
295     if(aNewSO.IsNull()) return _PTR(SObject)(aResult);
296     aResult = new SALOMEDS_SObject(aNewSO);
297   }
298   else {
299     SALOMEDS::SObject_ptr aNewSO = _corba_impl->Paste(aSO->GetCORBAImpl());
300     if(CORBA::is_nil(aNewSO)) return _PTR(SObject)(aResult);
301     aResult = new SALOMEDS_SObject(aNewSO);
302   }
303
304   return _PTR(SObject)(aResult);
305 }
306
307
308 void SALOMEDS_StudyManager::init_orb()
309 {
310   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
311   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
312   _orb = init(0 , 0 );
313 }
314
315 SALOMEDS_Driver_i* GetDriver(const Handle(SALOMEDSImpl_SObject)& theObject, CORBA::ORB_ptr orb)
316 {
317   SALOMEDS_Driver_i* driver = NULL;
318   
319   Handle(SALOMEDSImpl_SComponent) aSCO = theObject->GetFatherComponent();
320   if(!aSCO.IsNull()) {
321     TCollection_AsciiString IOREngine = aSCO->GetIOR();
322     if(!IOREngine.IsEmpty()) {
323       CORBA::Object_var obj = orb->string_to_object(IOREngine.ToCString());
324       SALOMEDS::Driver_var Engine = SALOMEDS::Driver::_narrow(obj) ;
325       driver = new SALOMEDS_Driver_i(Engine, orb);
326     }
327   }
328
329   return driver;
330 }