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