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