Salome HOME
Python containers are no more supported
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Driver_i.cxx
1 // Copyright (C) 2007-2013  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
23 #include "SALOMEDS_Driver_i.hxx"
24 #include "SALOMEDS_TMPFile_i.hxx"
25 #include "utilities.h"
26 #include "SALOMEDS_SObject_i.hxx"
27 #include "SALOMEDS_SComponent_i.hxx"
28 #include "SALOMEDS_Study_i.hxx"
29 #include "SALOMEDS.hxx"
30 #include <stdlib.h>
31
32 SALOMEDS_Driver_i::SALOMEDS_Driver_i(Engines::EngineComponent_ptr theEngine, CORBA::ORB_ptr theORB)
33 {
34   // engine should not be null - component is supposed to be inherited from Engines::EngineComponent
35   _engine = Engines::EngineComponent::_duplicate(theEngine);
36   // driver can be null - if component interface does not inherit SALOMEDS::Driver
37   _driver = SALOMEDS::Driver::_narrow(theEngine);
38   _orb = CORBA::ORB::_duplicate(theORB);
39 }
40
41 SALOMEDS_Driver_i::SALOMEDS_Driver_i(SALOMEDS::Driver_ptr theDriver, CORBA::ORB_ptr theORB)
42 {
43   // driver can be null - if component interface does not inherit SALOMEDS::Driver
44   _driver = SALOMEDS::Driver::_duplicate(theDriver);
45   // engine can be null - since it is narrowed from SALOMEDS::Driver ptr which can be null
46   _engine = Engines::EngineComponent::_narrow(theDriver);
47   _orb = CORBA::ORB::_duplicate(theORB);
48 }
49
50 SALOMEDS_Driver_i::~SALOMEDS_Driver_i()
51 {
52 }
53
54 std::string SALOMEDS_Driver_i::GetIOR()
55 {
56   std::string ior = "";
57   if ( !CORBA::is_nil(_engine) ) {
58     CORBA::String_var cior = _orb->object_to_string(_engine);
59     ior = cior;
60   }
61   return ior;
62 }
63
64 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::Save(const SALOMEDSImpl_SComponent& theComponent,
65                                               const std::string& theURL,
66                                               long& theStreamLength,
67                                               bool isMultiFile)
68 {
69   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
70   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
71
72   SALOMEDS::unlock();
73   SALOMEDS::TMPFile_var aStream;
74
75   if ( !CORBA::is_nil(_driver) )
76     aStream = _driver->Save(sco.in(), url, isMultiFile);
77
78   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
79   theStreamLength = aTMPFile->Size();
80
81   sco->UnRegister();
82   SALOMEDS::lock();
83
84   return aTMPFile;
85 }
86
87 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::SaveASCII(const SALOMEDSImpl_SComponent& theComponent,
88                                                    const std::string& theURL,
89                                                    long& theStreamLength,
90                                                    bool isMultiFile)
91 {
92   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
93   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
94
95   SALOMEDS::unlock();
96   SALOMEDS::TMPFile_var aStream;
97
98   if ( !CORBA::is_nil(_driver) )
99     aStream = _driver->SaveASCII(sco.in(), url, isMultiFile);
100
101   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
102   theStreamLength = aTMPFile->Size();
103
104   sco->UnRegister();
105   SALOMEDS::lock();
106
107   return aTMPFile;
108 }
109
110 bool SALOMEDS_Driver_i::Load(const SALOMEDSImpl_SComponent& theComponent,
111                              const unsigned char* theStream,
112                              const long theStreamLength,
113                              const std::string& theURL,
114                              bool isMultiFile)
115 {
116   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
117   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
118   CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
119
120   SALOMEDS::TMPFile_var aStream;
121   if (theStreamLength > 0)
122     aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
123   else
124     aStream = new SALOMEDS::TMPFile(0);
125
126   SALOMEDS::unlock();
127   bool isOk = false;
128
129   if ( !CORBA::is_nil(_driver) )
130     isOk = _driver->Load(sco.in(), aStream.in(), url, isMultiFile);
131
132   sco->UnRegister();
133   SALOMEDS::lock();
134   return isOk;
135 }
136
137 bool SALOMEDS_Driver_i::LoadASCII(const SALOMEDSImpl_SComponent& theComponent,
138                                   const unsigned char* theStream,
139                                   const long theStreamLength,
140                                   const std::string& theURL,
141                                   bool isMultiFile)
142 {
143   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
144   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
145   CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
146
147   SALOMEDS::TMPFile_var aStream;
148   if(theStreamLength > 0)
149     aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
150   else
151     aStream = new SALOMEDS::TMPFile(0);
152
153   SALOMEDS::unlock();
154   bool isOk = false;
155
156   if ( !CORBA::is_nil(_driver) )
157     isOk = _driver->LoadASCII(sco.in(), aStream.in(), url, isMultiFile);
158
159   sco->UnRegister();
160   SALOMEDS::lock();
161   return isOk;
162 }
163
164 void SALOMEDS_Driver_i::Close(const SALOMEDSImpl_SComponent& theComponent)
165 {
166   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
167
168   SALOMEDS::unlock();
169   if ( !CORBA::is_nil(_driver) )
170     _driver->Close(sco.in());
171   sco->UnRegister();
172   SALOMEDS::lock();
173 }
174
175 std::string SALOMEDS_Driver_i::ComponentDataType()
176 {
177   std::string dtype = "";
178   if ( !CORBA::is_nil(_driver) ) {
179     CORBA::String_var type = _driver->ComponentDataType();
180     dtype = type;
181   }
182   return dtype;
183 }
184
185 std::string SALOMEDS_Driver_i::Version()
186 {
187   return !CORBA::is_nil( _engine ) ? _engine->getVersion() : std::string("");
188 }
189
190 std::string SALOMEDS_Driver_i::IORToLocalPersistentID(const SALOMEDSImpl_SObject& theSObject,
191                                                       const std::string& IORString,
192                                                       bool isMultiFile,
193                                                       bool isASCII)
194 {
195   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theSObject, _orb);
196   SALOMEDS::unlock();
197
198   std::string pstr = "";
199
200   if ( !CORBA::is_nil(_driver) ) {
201     CORBA::String_var pers_string =_driver->IORToLocalPersistentID(so.in(), IORString.c_str(),
202                                                                    isMultiFile, isASCII);
203     pstr = pers_string;
204   }
205
206   so->UnRegister();
207   SALOMEDS::lock();
208
209   return pstr;
210 }
211
212
213 std::string SALOMEDS_Driver_i::LocalPersistentIDToIOR(const SALOMEDSImpl_SObject& theObject,
214                                                       const std::string& aLocalPersistentID,
215                                                       bool isMultiFile,
216                                                       bool isASCII)
217 {
218   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
219   CORBA::String_var pers_string = CORBA::string_dup(aLocalPersistentID.c_str());
220   SALOMEDS::unlock();
221
222   std::string ior = "";
223
224   if ( !CORBA::is_nil(_driver) ) {
225     CORBA::String_var IOR = _driver->LocalPersistentIDToIOR(so.in(), pers_string.in(), isMultiFile, isASCII);
226     ior = IOR;
227   }
228   so->UnRegister();
229   SALOMEDS::lock();
230   return ior;
231 }
232
233 bool SALOMEDS_Driver_i::CanCopy(const SALOMEDSImpl_SObject& theObject)
234 {
235   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
236
237   SALOMEDS::unlock();
238   bool isOk = false;
239
240   if ( !CORBA::is_nil(_driver) )
241     isOk = _driver->CanCopy(so.in());
242
243   so->UnRegister();
244   SALOMEDS::lock();
245
246   return isOk;
247 }
248
249
250 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::CopyFrom(const SALOMEDSImpl_SObject& theObject,
251                                                   int& theObjectID,
252                                                   long& theStreamLength)
253 {
254   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
255
256   SALOMEDS::unlock();
257   CORBA::Long anObjectID;
258   SALOMEDS::TMPFile_var aStream;
259
260   if ( !CORBA::is_nil(_driver) )
261     aStream = _driver->CopyFrom(so.in(), anObjectID);
262
263   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
264   theStreamLength = aTMPFile->Size();
265   theObjectID = anObjectID;
266
267   so->UnRegister();
268   SALOMEDS::lock();
269
270   return aTMPFile;
271 }
272
273 bool SALOMEDS_Driver_i::CanPaste(const std::string& theComponentName, int theObjectID)
274 {
275   SALOMEDS::unlock();
276   bool canPaste = false;
277
278   if ( !CORBA::is_nil(_driver) )
279     canPaste = _driver->CanPaste(theComponentName.c_str(), theObjectID);
280
281   SALOMEDS::lock();
282   return canPaste;
283 }
284
285 std::string SALOMEDS_Driver_i::PasteInto(const unsigned char* theStream,
286                                          const long theStreamLength,
287                                          int theObjectID,
288                                          const SALOMEDSImpl_SObject& theObject)
289 {
290   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
291   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)theStream;
292
293   SALOMEDS::TMPFile_var aStream;
294   if(theStreamLength > 0)
295     aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
296   else
297     aStream = new SALOMEDS::TMPFile(0);
298
299   SALOMEDS::unlock();
300
301   std::string entry = "";
302
303   if ( !CORBA::is_nil(_driver) ) {
304     SALOMEDS::SObject_var ret_so = _driver->PasteInto(aStream.in(), theObjectID, so.in());
305     entry = ret_so->GetID();
306   }
307
308   so->UnRegister();
309   SALOMEDS::lock();
310
311   return entry;
312 }
313
314 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::DumpPython(SALOMEDSImpl_Study* theStudy,
315                                                     bool isPublished,
316                                                     bool isMultiFile,
317                                                     bool& isValidScript,
318                                                     long& theStreamLength)
319 {
320   SALOMEDS_Study_i *  st_servant = SALOMEDS_Study_i::GetStudyServant(theStudy, _orb);//new SALOMEDS_Study_i (theStudy, _orb);
321   SALOMEDS::Study_var st  = SALOMEDS::Study::_narrow(st_servant->_this());
322
323   SALOMEDS::unlock();
324
325   Engines::TMPFile_var aStream;
326   CORBA::Boolean aValidScript = true; // VSR: maybe should be false by default ???
327
328   if ( !CORBA::is_nil( _engine ) )
329     aStream = _engine->DumpPython(st.in(), isPublished, isMultiFile, aValidScript);
330
331   SALOMEDSImpl_TMPFile* aTMPFile = new Engines_TMPFile_i(aStream._retn());
332   theStreamLength = aTMPFile->Size();
333   isValidScript = aValidScript;
334
335   SALOMEDS::lock();
336
337   return aTMPFile;
338 }
339
340 //###############################################################################################################
341 //                                          SALOMEDS_DriverFactory
342 //###############################################################################################################
343
344 SALOMEDS_DriverFactory_i::SALOMEDS_DriverFactory_i(CORBA::ORB_ptr theORB)
345 {
346   _orb = CORBA::ORB::_duplicate(theORB);
347   _name_service = new SALOME_NamingService(_orb);
348 }
349
350
351 SALOMEDS_DriverFactory_i::~SALOMEDS_DriverFactory_i()
352 {
353   delete _name_service;
354 }
355
356 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByType(const std::string& theComponentType)
357 {
358   CORBA::Object_var obj;
359
360   std::string aFactoryType;
361   if (theComponentType == "SUPERV") aFactoryType = "SuperVisionContainer";
362   else aFactoryType = "FactoryServer";
363
364   SALOMEDS::unlock();
365   obj = SALOME_LifeCycleCORBA(_name_service).FindOrLoad_Component(aFactoryType.c_str(), theComponentType.c_str());
366   SALOMEDS::lock();
367
368   if (CORBA::is_nil(obj)) {
369     obj = SALOME_LifeCycleCORBA(_name_service).FindOrLoad_Component("FactoryServer", theComponentType.c_str());
370   }
371
372   if (!CORBA::is_nil(obj)) {
373     Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
374     return new SALOMEDS_Driver_i(anEngine, _orb);
375   }
376
377   return NULL;
378 }
379
380 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByIOR(const std::string& theIOR)
381 {
382   CORBA::Object_var obj;
383   obj = _orb->string_to_object(theIOR.c_str());
384
385   if (!CORBA::is_nil(obj)) {
386     Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
387     return new SALOMEDS_Driver_i(anEngine, _orb);
388   }
389
390   return NULL;
391 }