]> SALOME platform Git repositories - samples/hello.git/blob - src/HELLO/HELLO.cxx
Salome HOME
16c3a293aeabb9a1e0058bcc9dc787a640ec5128
[samples/hello.git] / src / HELLO / HELLO.cxx
1 // Copyright (C) 2007-2016  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 "HELLO.hxx"
24 #include "HELLO_version.h"
25
26 #include <SALOMEconfig.h>
27 #include CORBA_CLIENT_HEADER(SALOMEDS)
28 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
29
30 #include <Utils_ExceptHandlers.hxx>
31
32 #include <string>
33
34 namespace {
35   static std::string studyName( const std::string& name )
36   {
37     std::string fullName = "/Hello/";
38     fullName += name;
39     return fullName;
40   }
41 }
42 /*!
43   \brief Constructor
44
45   Creates an instance of the HELLO component engine
46   
47   \param orb reference to the ORB
48   \param poa reference to the POA
49   \param contId CORBA object ID, pointing to the owner SALOME container
50   \param instanceName SALOME component instance name
51   \param interfaceName SALOME component interface name
52 */
53 HELLO::HELLO( CORBA::ORB_ptr orb,
54               PortableServer::POA_ptr poa,
55               PortableServer::ObjectId* contId, 
56               const char* instanceName, 
57               const char* interfaceName )
58   : Engines_Component_i( orb, poa, contId, instanceName, interfaceName )
59 {
60   _thisObj = this;
61   _id = _poa->activate_object( _thisObj ); // register and activate this servant object
62 }
63
64 /*!
65   \brief Destructor
66
67   Clean up allocated resources
68 */
69 HELLO::~HELLO()
70 {
71   // nothing to do
72 }
73
74 /*!
75   \brief Say hello to \a name
76   \param study SALOME study
77   \param name person's name
78   \return operation status
79 */
80 HELLO_ORB::status HELLO::hello( SALOMEDS::Study_ptr study, const char* name )
81 {
82   // set exception handler to catch unexpected CORBA exceptions
83   Unexpect aCatch(SALOME_SalomeException);
84   
85   // set result status to error initially
86   HELLO_ORB::status result = HELLO_ORB::OP_ERR_UNKNOWN;
87
88   // check if reference to study is valid
89   if ( !CORBA::is_nil( study ) ) {
90     // get full object path
91     std::string fullName = studyName( name );
92     // check if the object with the same name is already registered in the study
93     SALOMEDS::SObject_var sobj = study->FindObjectByPath( fullName.c_str() );
94     if ( !CORBA::is_nil( sobj ) ) {
95       // person is already registered in the study -> ERROR
96       result = HELLO_ORB::OP_ERR_ALREADY_MET;
97     }
98     else {
99       // person is not registered yet -> register
100       SALOMEDS::StudyBuilder_var     studyBuilder   = study->NewBuilder();          // study builder
101       SALOMEDS::UseCaseBuilder_var   useCaseBuilder = study->GetUseCaseBuilder();   // use case builder
102
103       // find HELLO component; create it if not found
104       SALOMEDS::SComponent_var father = study->FindComponent( "HELLO" );
105       if ( CORBA::is_nil( father ) ) {
106         // create component
107         father = studyBuilder->NewComponent( "HELLO" ); 
108         // set name attribute
109         father->SetAttrString( "AttributeName", "Hello" );
110         // set icon attribute
111         father->SetAttrString( "AttributePixMap", "ICON_HELLO" );
112         // register component in the study
113         studyBuilder->DefineComponentInstance( father, HELLO_Gen::_this() );
114         // add component to the use case tree
115         // (to support tree representation customization and drag-n-drop)
116         useCaseBuilder->SetRootCurrent();
117         useCaseBuilder->Append( father ); // component object is added as the top level item
118       }
119
120       // create new sub-object, as a child of the component object
121       sobj = studyBuilder->NewObject( father );
122       sobj->SetAttrString( "AttributeName", name );
123       // add object to the use case tree
124       // (to support tree representation customization and drag-n-drop)
125       useCaseBuilder->AppendTo( father, sobj );
126
127       // cleanup
128       father->UnRegister();
129       sobj->UnRegister();
130
131       // set operation status
132       result = HELLO_ORB::OP_OK;
133     }
134   }
135   
136   // return result of the operation
137   return result;
138 }
139
140 /*!
141   \brief Say goodbye to \a name
142   \param study SALOME study
143   \param name person's name
144   \return operation status
145 */
146 HELLO_ORB::status HELLO::goodbye( SALOMEDS::Study_ptr study, const char* name )
147 {
148   // set exception handler to catch unexpected CORBA exceptions
149   Unexpect aCatch(SALOME_SalomeException);
150   
151   // set result status to error initially
152   HELLO_ORB::status result = HELLO_ORB::OP_ERR_UNKNOWN;
153
154   // check if reference to study is valid
155   if ( !CORBA::is_nil( study ) ) {
156     // get full object path
157     std::string fullName = studyName( name );
158
159     // initially set error status: person is not registered
160     result = HELLO_ORB::OP_ERR_DID_NOT_MEET;
161
162     // check if the object with the same name is registered in the study
163     // find all objects with same name
164     SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();            // study builder
165     SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder(); // use case builder
166     SALOMEDS::SObject_var sobj = study->FindObjectByPath( fullName.c_str() );
167     while ( !CORBA::is_nil( sobj ) ) {
168       std::list<SALOMEDS::SObject_var> toRemove;
169       toRemove.push_back( sobj );
170       SALOMEDS::UseCaseIterator_var useCaseIt = useCaseBuilder->GetUseCaseIterator( sobj ); // use case iterator
171       for ( useCaseIt->Init( true ); useCaseIt->More(); useCaseIt->Next() )
172         toRemove.push_back( useCaseIt->Value() );
173       // perform removing of all found objects (recursively with children)
174       std::list<SALOMEDS::SObject_var>::const_iterator it;
175       for( it = toRemove.begin(); it != toRemove.end(); ++it ) {
176         sobj = *it;
177         // remove object from the study
178         // - normally it's enough to call RemoveObject() method
179         // RemoveObject`WithChildren() also removes all children recursively if there are any
180         // - it's not necessary to remove it from use case builder, it is done automatically
181         studyBuilder->RemoveObjectWithChildren( sobj );
182         
183         // cleanup
184         sobj->UnRegister();
185         
186         // set operation status to OK as at least one object is removed
187         result = HELLO_ORB::OP_OK;
188       }
189       sobj = study->FindObjectByPath( fullName.c_str() );
190     }
191   }
192   
193   // return result of the operation
194   return result;
195 }
196
197 /*!
198   \brief Copy or move objects to the specified position
199   
200   This function is used in the drag-n-drop functionality.
201   
202   \param what objects being copied/moved
203   \param where parent object where objects are copied/moved to
204   \param row position in the parent object's children list at which objects are copied/moved
205   \param isCopy \c true if object are copied or \c false otherwise
206 */
207 void HELLO::copyOrMove( const HELLO_ORB::object_list& what,
208                         SALOMEDS::SObject_ptr where,
209                         CORBA::Long row, CORBA::Boolean isCopy )
210 {
211   if ( CORBA::is_nil( where ) ) return; // bad parent
212
213   SALOMEDS::Study_var study = where->GetStudy();                               // study
214   SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();               // study builder
215   SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();    // use case builder
216   SALOMEDS::SComponent_var father = where->GetFatherComponent();               // father component
217   std::string dataType = father->ComponentDataType();
218   if ( dataType != "HELLO" ) return; // not a HELLO component
219   
220   SALOMEDS::SObject_var objAfter;
221   if ( row >= 0 && useCaseBuilder->HasChildren( where ) ) {
222     // insert at given row -> find insertion position
223     SALOMEDS::UseCaseIterator_var useCaseIt = useCaseBuilder->GetUseCaseIterator( where );
224     int i;
225     for ( i = 0; i < row && useCaseIt->More(); i++, useCaseIt->Next() );
226     if ( i == row && useCaseIt->More() ) {
227       objAfter = useCaseIt->Value();
228     }
229   }
230   
231   for ( int i = 0; i < what.length(); i++ ) {
232     SALOMEDS::SObject_var sobj = what[i];
233     if ( CORBA::is_nil( sobj ) ) continue; // skip bad object
234     if ( isCopy ) {
235       // copying is performed
236       // get name of the object
237       CORBA::String_var name = sobj->GetName();
238       // create new object, as a child of the component object
239       SALOMEDS::SObject_var new_sobj = studyBuilder->NewObject( father );
240       new_sobj->SetAttrString( "AttributeName", name.in() );
241       sobj = new_sobj;
242     }
243     // insert the object or its copy to the use case tree
244     if ( !CORBA::is_nil( objAfter ) )
245       useCaseBuilder->InsertBefore( sobj, objAfter ); // insert at given row
246     else
247       useCaseBuilder->AppendTo( where, sobj );        // append to the end of list
248   }
249 }
250
251 // Version information
252 char* HELLO::getVersion()
253 {
254 #if defined(HELLO_DEVELOPMENT)
255   return CORBA::string_dup(HELLO_VERSION_STR"dev");
256 #else
257   return CORBA::string_dup(HELLO_VERSION_STR);
258 #endif
259 }
260
261 extern "C"
262 {
263   /*!
264     \brief Exportable factory function: create an instance of the HELLO component engine
265     \param orb reference to the ORB
266     \param poa reference to the POA
267     \param contId CORBA object ID, pointing to the owner SALOME container
268     \param instanceName SALOME component instance name
269     \param interfaceName SALOME component interface name
270     \return CORBA object identifier of the registered servant
271   */
272   PortableServer::ObjectId* HELLOEngine_factory( CORBA::ORB_ptr orb,
273                                                  PortableServer::POA_ptr poa, 
274                                                  PortableServer::ObjectId* contId,
275                                                  const char* instanceName, 
276                                                  const char* interfaceName )
277   {
278     HELLO* component = new HELLO( orb, poa, contId, instanceName, interfaceName );
279     return component->getId();
280   }
281 }