Salome HOME
1. SALOME::GenericObj : Destroy() -> UnRegister()
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Driver_i.cxx
1 //  Copyright (C) 2007-2010  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 #include "SALOMEDS_Driver_i.hxx"
23 #include "SALOMEDS_TMPFile_i.hxx"
24 #include "utilities.h"
25 #include "SALOMEDS_SObject_i.hxx"
26 #include "SALOMEDS_SComponent_i.hxx"
27 #include "SALOMEDS_Study_i.hxx"
28 #include "SALOMEDS.hxx"
29 #include <stdlib.h>
30
31 SALOMEDS_Driver_i::~SALOMEDS_Driver_i()
32 {
33 }
34
35 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::Save(const SALOMEDSImpl_SComponent& theComponent,
36                                       const std::string& theURL,
37                                       long& theStreamLength,
38                                       bool isMultiFile)
39 {  
40   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
41   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
42
43   SALOMEDS::unlock();
44   SALOMEDS::TMPFile_var aStream = _driver->Save(sco.in(), url, isMultiFile);
45   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
46   theStreamLength = aTMPFile->Size();
47   SALOMEDS::lock();
48
49   return aTMPFile;
50 }
51
52 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::SaveASCII(const SALOMEDSImpl_SComponent& theComponent,
53                                                    const std::string& theURL,
54                                                    long& theStreamLength,
55                                                    bool isMultiFile)
56 {
57   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
58   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
59
60   SALOMEDS::unlock();
61   SALOMEDS::TMPFile_var aStream = _driver->SaveASCII(sco.in(), url, isMultiFile);
62   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
63   theStreamLength = aTMPFile->Size();
64   SALOMEDS::lock();
65
66   return aTMPFile;
67 }
68   
69 bool SALOMEDS_Driver_i::Load(const SALOMEDSImpl_SComponent& theComponent,
70                              const unsigned char* theStream,
71                              const long theStreamLength,
72                              const std::string& theURL,
73                              bool isMultiFile)
74 {
75   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
76   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
77   CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
78
79   SALOMEDS::TMPFile_var aStream;
80   if (theStreamLength > 0) 
81     aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);  
82   else 
83     aStream = new SALOMEDS::TMPFile(0);
84
85   SALOMEDS::unlock();
86   bool isOk = _driver->Load(sco.in(), aStream.in(), url, isMultiFile);
87   SALOMEDS::lock();
88
89   return isOk;
90 }
91
92 bool SALOMEDS_Driver_i::LoadASCII(const SALOMEDSImpl_SComponent& theComponent,
93                                   const unsigned char* theStream,
94                                   const long theStreamLength,
95                                   const std::string& theURL,
96                                   bool isMultiFile)
97 {
98   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
99   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
100   CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
101
102   SALOMEDS::TMPFile_var aStream;
103   if(theStreamLength > 0) 
104     aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);  
105   else 
106     aStream = new SALOMEDS::TMPFile(0);
107
108   SALOMEDS::unlock();
109   bool isOk = _driver->LoadASCII(sco.in(), aStream.in(), url, isMultiFile);
110   SALOMEDS::lock();
111
112   return isOk;
113 }
114
115 void SALOMEDS_Driver_i::Close(const SALOMEDSImpl_SComponent& theComponent)
116 {
117   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
118
119   SALOMEDS::unlock();
120   _driver->Close(sco.in());
121   SALOMEDS::lock();
122 }
123
124
125
126 std::string SALOMEDS_Driver_i::IORToLocalPersistentID(const SALOMEDSImpl_SObject& theSObject,
127                                                  const std::string& IORString,
128                                                  bool isMultiFile,
129                                                  bool isASCII)
130 {
131   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theSObject, _orb);
132   CORBA::String_var ior = CORBA::string_dup(IORString.c_str());
133
134   SALOMEDS::unlock();
135   CORBA::String_var pers_string =_driver->IORToLocalPersistentID(so.in(), ior.in(), isMultiFile, isASCII);
136   SALOMEDS::lock();
137
138   return std::string(pers_string);
139 }
140
141
142 std::string SALOMEDS_Driver_i::LocalPersistentIDToIOR(const SALOMEDSImpl_SObject& theObject,
143                                                  const std::string& aLocalPersistentID,
144                                                  bool isMultiFile,
145                                                  bool isASCII)
146 {
147   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
148   CORBA::String_var pers_string = CORBA::string_dup(aLocalPersistentID.c_str());
149   SALOMEDS::unlock();
150   CORBA::String_var IOR = _driver->LocalPersistentIDToIOR(so.in(), pers_string.in(), isMultiFile, isASCII);
151   SALOMEDS::lock();
152   return std::string(IOR);
153 }
154
155 bool SALOMEDS_Driver_i::CanCopy(const SALOMEDSImpl_SObject& theObject)
156 {
157   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
158
159   SALOMEDS::unlock();
160   bool isOk = _driver->CanCopy(so.in());
161   so->UnRegister();
162   SALOMEDS::lock();
163
164   return isOk;
165 }
166
167
168 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::CopyFrom(const SALOMEDSImpl_SObject& theObject, 
169                                                   int& theObjectID,
170                                                   long& theStreamLength)
171 {
172   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
173
174   SALOMEDS::unlock();
175   CORBA::Long anObjectID;
176   SALOMEDS::TMPFile_var aStream = _driver->CopyFrom(so.in(), anObjectID);
177   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
178   theStreamLength = aTMPFile->Size();
179   theObjectID = anObjectID;
180   SALOMEDS::lock();
181
182   return aTMPFile;
183 }
184
185 bool SALOMEDS_Driver_i::CanPaste(const std::string& theComponentName, int theObjectID)
186 {
187   SALOMEDS::unlock();
188   bool canPaste = _driver->CanPaste(theComponentName.c_str(), theObjectID);
189   SALOMEDS::lock();
190   return canPaste;
191 }
192
193 std::string SALOMEDS_Driver_i::PasteInto(const unsigned char* theStream,
194                                     const long theStreamLength,
195                                     int theObjectID,
196                                     const SALOMEDSImpl_SObject& theObject)
197 {
198   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
199   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)theStream;
200
201   SALOMEDS::TMPFile_var aStream;
202   if(theStreamLength > 0) 
203     aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);  
204   else 
205     aStream = new SALOMEDS::TMPFile(0);
206
207   SALOMEDS::unlock();
208   SALOMEDS::SObject_var ret_so = _driver->PasteInto(aStream.in(), theObjectID, so.in());
209   SALOMEDS::lock();
210
211   return std::string(ret_so->GetID());
212 }
213
214 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::DumpPython(SALOMEDSImpl_Study* theStudy, 
215                                                     bool isPublished, 
216                                                     bool& isValidScript,
217                                                     long& theStreamLength)
218 {
219   SALOMEDS_Study_i *  st_servant = SALOMEDS_Study_i::GetStudyServant(theStudy, _orb);//new SALOMEDS_Study_i (theStudy, _orb);
220   SALOMEDS::Study_var st  = SALOMEDS::Study::_narrow(st_servant->_this());
221   Engines::EngineComponent_ptr aComponent = Engines::EngineComponent::_narrow(_driver);
222
223   SALOMEDS::unlock();
224   CORBA::Boolean aValidScript, aPublished = isPublished;
225   Engines::TMPFile_var aStream = aComponent->DumpPython(st.in(), aPublished, aValidScript);
226   SALOMEDSImpl_TMPFile* aTMPFile = new Engines_TMPFile_i(aStream._retn());
227   theStreamLength = aTMPFile->Size();
228   isValidScript = aValidScript;
229   SALOMEDS::lock();
230
231   return aTMPFile;
232 }
233
234 //###############################################################################################################
235 //                                          SALOMEDS_DriverFactory
236 //###############################################################################################################
237
238 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByType(const std::string& theComponentType)
239 {
240   CORBA::Object_var obj;
241
242   std::string aFactoryType;
243   if (theComponentType == "SUPERV") aFactoryType = "SuperVisionContainer";
244   else aFactoryType = "FactoryServer";
245
246   SALOMEDS::unlock();
247   obj = SALOME_LifeCycleCORBA(_name_service).FindOrLoad_Component(aFactoryType.c_str(), theComponentType.c_str());
248   SALOMEDS::lock();
249
250   if (CORBA::is_nil(obj)) {
251     obj = SALOME_LifeCycleCORBA(_name_service).FindOrLoad_Component("FactoryServerPy", theComponentType.c_str());
252   }
253
254   if (!CORBA::is_nil(obj)) {
255     SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(obj);
256     return new SALOMEDS_Driver_i(aDriver, _orb);
257   }
258
259   return NULL;
260 }
261
262 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByIOR(const std::string& theIOR)
263 {
264   CORBA::Object_var obj;
265   obj = _orb->string_to_object(theIOR.c_str());
266  
267   if (!CORBA::is_nil(obj)) {
268     SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(obj);
269     return new SALOMEDS_Driver_i(aDriver, _orb);
270   }
271
272   return NULL;
273 }