Salome HOME
sources v1.2
[modules/yacs.git] / src / ModuleCatalog / SALOME_ModuleCatalog_Acomponent_impl.cxx
1 //  SALOME ModuleCatalog : implementation of ModuleCatalog server which parsers xml description of modules
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SALOME_ModuleCatalog_Acomponent_impl.cxx
25 //  Author : Estelle Deville
26 //  Module : SALOME
27 //  $Header$
28
29 using namespace std;
30 #include "SALOME_ModuleCatalog_Acomponent_impl.hxx"
31
32 //----------------------------------------------------------------------
33 // Function : SALOME_ModuleCatalog_AcomponentImpl
34 // Purpose  : Constructor
35 //            Affect the component name, type,icone,  a bool to define 
36 //            if it's multistudy or not.
37 //            Affect too the constraint and the interfaces of the component
38 //            and the pathes prefixes for all computers
39 //----------------------------------------------------------------------  
40 SALOME_ModuleCatalog_AcomponentImpl::SALOME_ModuleCatalog_AcomponentImpl(
41                    const char* name,
42                    const char* constraint,  
43                    SALOME_ModuleCatalog::ComponentType componenttype,
44                    CORBA::Boolean componentmultistudy,
45                    const char* icone,
46                    SALOME_ModuleCatalog::ListOfDefInterface list_interfaces,
47                    ListOfPathPrefix pathes)
48 {
49   MESSAGE("Component creation")
50   // Affect component name
51   _component_name = new char[strlen(name)+1];
52  strcpy(_component_name, name);
53
54  // Affect constraint
55  _constraint =new char[strlen(constraint)+1];
56  strcpy(_constraint, constraint);
57
58  // Affect component type
59  _componenttype = componenttype;
60  
61  // Affect component multistudy
62  _componentmultistudy = componentmultistudy;
63
64  // Affect icone
65  _icone =new char[strlen(icone)+1];
66  strcpy(_icone, icone);
67
68  //Affect interfaces
69  _list_interfaces.length(list_interfaces.length());
70  _list_interfaces = list_interfaces;
71
72  // affect path prefixes
73  _pathes.resize(pathes.size());
74  _pathes = pathes ;
75 }
76
77 //----------------------------------------------------------------------
78 // Function : ~SALOME_ModuleCatalog_AcomponentImpl
79 // Purpose  : Destructor 
80 //----------------------------------------------------------------------
81 SALOME_ModuleCatalog_AcomponentImpl::~SALOME_ModuleCatalog_AcomponentImpl()
82 {
83   MESSAGE("Component destruction")
84   // empty memory
85   delete [] _component_name;
86   delete [] _constraint;
87 }
88
89 //----------------------------------------------------------------------
90 // Function : GetInterfaceList
91 // Purpose  : get a list of the interfaces name of a component
92 //----------------------------------------------------------------------
93 SALOME_ModuleCatalog::ListOfInterfaces* 
94 SALOME_ModuleCatalog_AcomponentImpl::GetInterfaceList() 
95 {
96   SALOME_ModuleCatalog::ListOfInterfaces_var _list = new SALOME_ModuleCatalog::ListOfInterfaces;
97
98   // All the interfaces are defined in _list_interfaces affected at the
99   // component creation
100   unsigned int _length_interfaces =_list_interfaces.length();
101
102   _list->length(_length_interfaces);
103
104   // Parse all the interfaces to get their name
105   for (unsigned int ind = 0; ind < _length_interfaces; ind++)
106     {
107       _list[ind] = CORBA::string_dup(_list_interfaces[ind].interfacename);
108       MESSAGE("The component " << _component_name << " contains " << _list[ind] << " as interface") 
109     }
110   
111   return _list._retn();
112 }
113
114 //----------------------------------------------------------------------
115 // Function : GetInterface
116 // Purpose  : get one interface of a component
117 //----------------------------------------------------------------------
118 SALOME_ModuleCatalog::DefinitionInterface*
119 SALOME_ModuleCatalog_AcomponentImpl::GetInterface(const char* interfacename)
120                                      throw(SALOME_ModuleCatalog::NotFound)
121 {
122   SALOME_ModuleCatalog::DefinitionInterface_var _interface = new SALOME_ModuleCatalog::DefinitionInterface;
123   SALOME_ModuleCatalog::Service_var _service = new SALOME_ModuleCatalog::Service;
124   // Variables initialisation
125   _interface->interfaceservicelist.length(0);
126   int _length = 0;
127   bool _find = false ;
128   
129   // looking for the specified interface
130   for (unsigned int ind = 0; ind < _list_interfaces.length(); ind++)
131     {
132       if (strcmp(interfacename, _list_interfaces[ind].interfacename) == 0)
133         {
134           // wanted interface
135           _find = true ;
136
137           // Affect the name of the interface
138           _interface->interfacename = interfacename;
139
140           // Affect each service of the wanted interface
141           for (unsigned int ind1 = 0; ind1 <  _list_interfaces[ind].interfaceservicelist.length() ; ind1++)
142             {
143               _interface->interfaceservicelist.length(_length+1);
144               _service = _duplicate_service(_list_interfaces[ind].interfaceservicelist[ind1]);
145               _interface->interfaceservicelist[_length] = _service;     
146              
147               _length++;
148             }
149
150         }
151     }
152   
153   if (!_find)
154     {
155       // The interface was not found, the exception should be thrown
156       char * message = new char[100];
157       strcpy(message, "The interface ");
158       strcat(message,interfacename);
159       strcat(message, " of the component ");
160       strcat(message,_component_name); 
161       strcat(message, " was not found") ;
162       MESSAGE("The interface " << interfacename << " of the component " << _component_name << " was not found")
163         throw SALOME_ModuleCatalog::NotFound(message);
164       delete [] message;
165     }
166
167   return _interface._retn();
168 }
169
170
171
172 //----------------------------------------------------------------------
173 // Function : GetServiceList
174 // Purpose  : get a list of the services name of an interface 
175 //            of a component
176 //----------------------------------------------------------------------
177 SALOME_ModuleCatalog::ListOfServices* 
178 SALOME_ModuleCatalog_AcomponentImpl::GetServiceList(const char* interfacename)
179                                      throw(SALOME_ModuleCatalog::NotFound)
180 {
181   SALOME_ModuleCatalog::ListOfServices_var _list = new SALOME_ModuleCatalog::ListOfServices;
182
183   // Variables initialisation
184   _list->length(0); 
185   bool _find = false ;
186
187   // looking for the specified interface
188   for (unsigned int ind = 0; ind < _list_interfaces.length(); ind++)
189     {
190       if (strcmp(interfacename, _list_interfaces[ind].interfacename) == 0)
191         {
192           _find = true ;
193           // wanted interface
194           // Get the list of services name for this interface
195           unsigned int _length_services = _list_interfaces[ind].interfaceservicelist.length();
196           _list->length(_length_services);
197           for (unsigned int ind1 = 0; ind1 < _length_services ; ind1++)
198             {
199               _list[ind1] = CORBA::string_dup(_list_interfaces[ind].interfaceservicelist[ind1].ServiceName);
200               MESSAGE("The interface " << interfacename << " of the component " << _component_name << " contains " << _list[ind1] << " as a service") 
201             }
202         }
203     }
204
205  if (!_find)
206     {
207       // The interface was not found, the exception should be thrown
208       char * message = new char[100];
209       strcpy(message, "The interface ");
210       strcat(message,interfacename);
211       strcat(message, " of the component ");
212       strcat(message,_component_name); 
213       strcat(message, " was not found") ;
214       MESSAGE("The interface " << interfacename << " of the component " << _component_name << " was not found")
215         throw SALOME_ModuleCatalog::NotFound(message);
216       delete [] message;
217     }
218
219   return _list._retn();
220 }
221
222     
223 //----------------------------------------------------------------------
224 // Function : GetService
225 // Purpose  : get one service of an interface of a component
226 //----------------------------------------------------------------------
227 SALOME_ModuleCatalog::Service* 
228 SALOME_ModuleCatalog_AcomponentImpl::GetService(const char* interfacename, 
229                                                 const char* servicename) 
230                                      throw(SALOME_ModuleCatalog::NotFound)
231 {
232   SALOME_ModuleCatalog::Service_var _service = new SALOME_ModuleCatalog::Service;
233   // Varaibles initialisation
234   bool _find = false ;
235   
236   // looking for the specified interface
237   for (unsigned int ind = 0; ind < _list_interfaces.length(); ind++)
238     {
239       if (strcmp(interfacename, _list_interfaces[ind].interfacename) == 0)
240         {
241           // wanted interface
242           // looking for the specified service
243           for (unsigned int ind1 = 0; ind1 <  _list_interfaces[ind].interfaceservicelist.length() ; ind1++)
244             {
245               if (strcmp(servicename, _list_interfaces[ind].interfaceservicelist[ind1].ServiceName) == 0)
246               {
247                 // Wanted Service
248                 // Affect the service to be returned
249                 _find = true ;
250                 _service = _duplicate_service(_list_interfaces[ind].interfaceservicelist[ind1]);
251               }
252             }
253
254         }
255     }
256   
257   if (!_find)
258     {
259       // The service was not found, the exception should be thrown
260       char * message = new char[100];
261       strcpy(message, "The service ");
262       strcat(message, servicename);
263       strcat(message," of the interface ");
264       strcat(message,interfacename);
265       strcat(message, " of the component ");
266       strcat(message,_component_name); 
267       strcat(message, " was not found") ;
268       MESSAGE("The service " << servicename << " of the interface " << interfacename << " of the component " << _component_name << " was not found")
269         throw SALOME_ModuleCatalog::NotFound(message);
270       delete [] message;
271     }
272
273   return _service._retn();
274 }
275
276 //----------------------------------------------------------------------
277 // Function : GetDefaultService
278 // Purpose  : get the default service of the interface
279 //----------------------------------------------------------------------
280 SALOME_ModuleCatalog::Service* 
281 SALOME_ModuleCatalog_AcomponentImpl::GetDefaultService(const char* interfacename) 
282                                      throw(SALOME_ModuleCatalog::NotFound)
283 {
284   SALOME_ModuleCatalog::Service_var _service = new SALOME_ModuleCatalog::Service;
285
286   // Variables initialisation
287   bool _find = false ;
288
289   // looking for the specified interface
290   for (unsigned int ind = 0; ind < _list_interfaces.length(); ind++)
291     {
292       if (strcmp(interfacename, _list_interfaces[ind].interfacename) == 0)
293         {
294           // wanted interface
295           // looking for the defautl service of the wanted interface
296           for (unsigned int ind1 = 0; ind1 <  _list_interfaces[ind].interfaceservicelist.length() ; ind1++)
297             {
298               if (_list_interfaces[ind].interfaceservicelist[ind1].Servicebydefault)
299               {
300                 // Default Service
301                 // affect the service to be returned
302                 _find = true ;
303                 _service = _duplicate_service(_list_interfaces[ind].interfaceservicelist[ind1]);
304               }
305             }
306
307         }
308     }
309
310   if (!_find)
311     {
312       // The service was not found, the exception should be thrown
313       char * message = new char[100];
314       strcpy(message, "The default service of the interface ");
315       strcat(message,interfacename);
316       strcat(message, " of the component ");
317       strcat(message,_component_name); 
318       strcat(message, " was not found") ;
319       MESSAGE("The default service of the interface " << interfacename << " of the component " << _component_name << " was not found")
320         throw SALOME_ModuleCatalog::NotFound(message);
321       delete [] message;
322     }
323
324   return _service._retn();
325 }
326
327 //----------------------------------------------------------------------
328 // Function : GetPathPrefix
329 // Purpose  : get the PathPrefix of a computer
330 //----------------------------------------------------------------------
331 char* 
332 SALOME_ModuleCatalog_AcomponentImpl::GetPathPrefix(const char* machinename) 
333                                      throw(SALOME_ModuleCatalog::NotFound)
334 {
335   MESSAGE("Begin of GetPathPrefix")
336   // Variables initialisation
337   char* _path = NULL;
338   bool _find = false ;
339
340   // Parse all the path prefixes
341   // looking for the wanted computer
342   for (unsigned int ind = 0 ; ind < _pathes.size() ; ind++)
343     {
344       for (unsigned int ind1 = 0 ; ind1 < _pathes[ind].ListOfComputer.size() ; ind1++)    
345         {
346           if (strcmp(machinename, _pathes[ind].ListOfComputer[ind1].c_str()) == 0)
347             {
348               _find = true ;
349               // Wanted computer
350               // affect the path to be returned
351                 const char* _temp = _pathes[ind].path.c_str() ;
352                   _path = new char[strlen(_temp)+1];
353               strcpy(_path,_temp);
354             }
355         }
356     }
357
358   if (!_find)
359     {
360       // The computer was not found, the exception should be thrown
361       char * message = new char[100];
362       strcpy(message, "The computer ");
363       strcat(message,machinename); 
364       strcat(message, " was not found in the catalog associated to the component ") ;
365       strcat(message,_component_name);      
366       MESSAGE("The computer " << machinename << " was not found in the catalog associated to the component " << _component_name)
367         throw SALOME_ModuleCatalog::NotFound(message);
368       delete [] message;
369     }
370
371   return _path;
372 }
373
374 //----------------------------------------------------------------------
375 // Function : constraint
376 // Purpose  : obtain the constraint affected to a component
377 //----------------------------------------------------------------------
378 char* SALOME_ModuleCatalog_AcomponentImpl::constraint() 
379 {
380   return CORBA::string_dup(_constraint);
381 }
382
383 //----------------------------------------------------------------------
384 // Function : componentname
385 // Purpose  : obtain the componentname
386 //----------------------------------------------------------------------
387 char* SALOME_ModuleCatalog_AcomponentImpl::componentname()
388 {
389   return CORBA::string_dup(_component_name);
390 }
391
392 //----------------------------------------------------------------------
393 // Function : multistudy
394 // Purpose  : define if a component can be multistudy or not
395 //----------------------------------------------------------------------
396 CORBA::Boolean SALOME_ModuleCatalog_AcomponentImpl::multistudy()
397 {
398   return _componentmultistudy ;
399 }
400
401 //----------------------------------------------------------------------
402 // Function : component_type
403 // Purpose  : define the type of the component
404 //----------------------------------------------------------------------
405 SALOME_ModuleCatalog::ComponentType 
406 SALOME_ModuleCatalog_AcomponentImpl::component_type() 
407 {
408   return _componenttype;
409 }
410
411 //----------------------------------------------------------------------
412 // Function : icone
413 // Purpose  : obtain the icone affected to a component (for IAPP)
414 //----------------------------------------------------------------------
415 char* SALOME_ModuleCatalog_AcomponentImpl::component_icone() 
416 {
417   return CORBA::string_dup(_icone);
418 }
419
420 //----------------------------------------------------------------------
421 // Function :  _duplicate_service
422 // Purpose  : duplicate a service
423 //----------------------------------------------------------------------
424 SALOME_ModuleCatalog::Service_var 
425 SALOME_ModuleCatalog_AcomponentImpl::_duplicate_service(SALOME_ModuleCatalog::Service service)
426 {
427   SALOME_ModuleCatalog::Service_var _service = new SALOME_ModuleCatalog::Service;
428
429   // service name
430   _service->ServiceName = CORBA::string_dup(service.ServiceName);
431   // service by default
432   _service->Servicebydefault = service.Servicebydefault;
433
434   // in Parameters service
435   unsigned int _length_in_param = service.ServiceinParameter.length();
436   for(unsigned int ind = 0; ind < _length_in_param; ind++)
437     {
438       _service->ServiceinParameter.length(_length_in_param);
439       // in Parameter type
440       _service->ServiceinParameter[ind].Parametertype = CORBA::string_dup(service.ServiceinParameter[ind].Parametertype);
441       // in Parameter name
442        _service->ServiceinParameter[ind].Parametername = CORBA::string_dup(service.ServiceinParameter[ind].Parametername);
443     }
444
445    // out Parameters service
446   unsigned int _length_out_param = service.ServiceoutParameter.length();
447   for(unsigned int ind = 0; ind < _length_out_param; ind++)
448     {
449       _service->ServiceoutParameter.length(_length_out_param);
450       // out Parameter type
451       _service->ServiceoutParameter[ind].Parametertype = CORBA::string_dup(service.ServiceoutParameter[ind].Parametertype);
452       // out Parameter name
453        _service->ServiceoutParameter[ind].Parametername = CORBA::string_dup(service.ServiceoutParameter[ind].Parametername);
454     }    
455
456   return _service;
457 }