Index: XMLIO_V2/dev/common/src/node/file.cpp
===================================================================
--- XMLIO_V2/dev/common/src/node/file.cpp	(revision 290)
+++ XMLIO_V2/dev/common/src/node/file.cpp	(revision 300)
@@ -7,4 +7,9 @@
 #include "object_factory.hpp"
 #include "object_factory_impl.hpp"
+#include "data_output.hpp"
+#include "context.hpp"
+#include "context_server.hpp"
+#include "nc4_data_output.hpp"
+
 
 namespace xmlioserver {
@@ -16,10 +21,10 @@
       : CObjectTemplate<CFile>(), CFileAttributes()
       , vFieldGroup(), data_out(), enabledFields()
-   { /* Ne rien faire de plus */ }
+   { setVirtualFieldGroup() ;}
 
    CFile::CFile(const StdString & id)
       : CObjectTemplate<CFile>(id), CFileAttributes()
       , vFieldGroup(), data_out(), enabledFields()
-   { /* Ne rien faire de plus */ }
+   { setVirtualFieldGroup() ;}
 
    CFile::~CFile(void)
@@ -62,37 +67,42 @@
       this->enabledFields = this->getAllFields();
 
-      std::cout<<"---> File :"<<this->name.getValue()<<std::endl ;
+      std::vector<boost::shared_ptr<CField> > newEnabledFields;
+      
       for ( it = this->enabledFields.begin() ; it != this->enabledFields.end(); it++ )
       {
          if (!(*it)->enabled.isEmpty()) // Si l'attribut 'enabled' est dÃ©fini ...
          {
-            if (! (*it)->enabled.getValue())
-            { it--; this->enabledFields.erase(it+1); continue; }
+            if (! (*it)->enabled.getValue()) continue;
+//            { it--; this->enabledFields.erase(it+1); continue; }
          }
          else // Si l'attribut 'enabled' n'est pas dÃ©fini ...
          {
-            if (!default_enabled)
-            { it--; this->enabledFields.erase(it+1); continue; }
+            if (!default_enabled) continue ;
+//            { it--; this->enabledFields.erase(it+1); continue; }
          }
 
          if (!(*it)->level.isEmpty()) // Si l'attribut 'level' est dÃ©fini ...
          {
-            if ((*it)->level.getValue() > _outputlevel)
-            { it--; this->enabledFields.erase(it+1); continue; }
+            if ((*it)->level.getValue() > _outputlevel) continue ;
+//            { it--; this->enabledFields.erase(it+1); continue; }
          }
          else // Si l'attribut 'level' n'est pas dÃ©fini ...
          {
-            if (default_level > _outputlevel)
-            { it--; this->enabledFields.erase(it+1); continue; }
-         }
-
+            if (default_level > _outputlevel) continue ;
+//            { it--; this->enabledFields.erase(it+1); continue; }
+         }
+ 
+//         CField* field_tmp=(*it).get() ;
+//         shared_ptr<CField> sptfield=*it ;
+//         field_tmp->refObject.push_back(sptfield) ;
+         newEnabledFields.push_back(*it) ;
          // Le champ est finalement actif, on y ajoute sa propre reference.
          (*it)->refObject.push_back(*it);
          // Le champ est finalement actif, on y ajoute la rÃ©fÃ©rence au champ de base.
-         std::cout<<"    Field Enabled : "<<(*it)->getId()<<std::endl ;
          (*it)->setRelFile(CObjectFactory::GetObject(this));
          (*it)->baseRefObject->refObject.push_back(*it);
          // A faire, ajouter les references intermediaires...
       }
+      enabledFields=newEnabledFields ;
 
       return (this->enabledFields);
@@ -108,37 +118,74 @@
    //----------------------------------------------------------------
 
-   void CFile::setVirtualFieldGroup(const StdString & newVFieldGroupId)
+   void CFile::setVirtualFieldGroup(void)
    {
       this->setVirtualFieldGroup
-         (CObjectFactory::CreateObject<CFieldGroup>(/*newVFieldGroupId*/));
-   }
-
-   //----------------------------------------------------------------
-
-   void CFile::initializeDataOutput(boost::shared_ptr<io::CDataOutput> dout)
-   {
-      this->data_out = dout;
-      this->data_out->writeFile(CObjectFactory::GetObject<CFile>(this));
-      
+         (CObjectFactory::CreateObject<CFieldGroup>());
+   }
+
+   //----------------------------------------------------------------
+
+   void CFile::createHeader(void)
+   {
+    
       std::vector<boost::shared_ptr<CField> >::iterator it, end = this->enabledFields.end();
 
+      AllDomainEmpty=true ;
       for (it = this->enabledFields.begin() ;it != end; it++)
       {
          boost::shared_ptr<CField> field = *it;
-         this->data_out->writeFieldGrid(field);
+         AllDomainEmpty&=field->grid->domain->isEmpty() ;
       }
-         
-      for (it = this->enabledFields.begin() ;it != end; it++)
+      
+      if (!AllDomainEmpty ||  type.getValue()=="one_file")
       {
-         boost::shared_ptr<CField> field = *it;
-         this->data_out->writeField(field);
+         StdString filename = (!name.isEmpty()) ?   name.getValue() : getId();
+         StdOStringStream oss;
+//         if (! output_dir.isEmpty()) oss << output_dir.getValue();
+         oss << filename;
+         if (!name_suffix.isEmpty()) oss << name_suffix.getValue();
+
+         bool multifile=true ;
+         if (!type.isEmpty())
+         {
+           if (type.getValue()=="one_file") multifile=false ;
+           else if (type.getValue()=="multi_file") multifile=true ;
+           else ERROR("void Context::createDataOutput(void)",
+                      "incorrect file <type> attribut : must be <multi_file> or <one_file>, "
+                      <<"having : <"<<type.getValue()<<">") ;
+         } 
+         
+         shared_ptr<CContext> context=CObjectFactory::GetObject<CContext>(CObjectFactory::GetCurrentContextId()) ;
+         CContextServer* server=context->server ;
+
+         if (multifile) 
+         {
+            if (server->intraCommSize > 1) oss << "_" << server->intraCommRank;
+         }
+         oss << ".nc";
+
+         data_out=shared_ptr<io::CDataOutput>(new io::CNc4DataOutput(oss.str(), false,server->intraComm,multifile));
+
+         data_out->writeFile(CObjectFactory::GetObject<CFile>(this));
+         for (it = this->enabledFields.begin() ;it != end; it++)
+         {
+            boost::shared_ptr<CField> field = *it;
+            this->data_out->writeFieldGrid(field);
+         }
+         
+         for (it = this->enabledFields.begin() ;it != end; it++)
+         {
+            boost::shared_ptr<CField> field = *it;
+            this->data_out->writeField(field);
+         }
+         
+         this->data_out->definition_end();
       }
-         
-      this->data_out->definition_end();
    }
 
    void CFile::close(void)
    {
-     this->data_out->closeFile();
+     if (!AllDomainEmpty ||  type.getValue()=="one_file")
+       this->data_out->closeFile();
    }
    //----------------------------------------------------------------
@@ -150,5 +197,5 @@
       { // Si la dÃ©finition du fichier intÃ©gre des champs et si le fichier est identifiÃ©.
          node.goToParentElement();
-         this->setVirtualFieldGroup(this->getId());
+//         this->setVirtualFieldGroup(this->getId());
          this->getVirtualFieldGroup()->parse(node, false);
       }
@@ -234,9 +281,125 @@
                << "[ renum = " << renum << "] Bad type !");
       
-      this->setVirtualFieldGroup(this->getId());
+//      this->setVirtualFieldGroup(this->getId());
       if (hasVFG)this->getVirtualFieldGroup()->fromBinary(is);
       
    }
-
+   
+   shared_ptr<CField> CFile::addField(const string& id)
+   {
+     return vFieldGroup->createChild(id) ;
+   }
+
+   shared_ptr<CFieldGroup> CFile::addFieldGroup(const string& id)
+   {
+     return vFieldGroup->createChildGroup(id) ;
+   }
+   
+  
+   void CFile::sendAddField(const string& id)
+   {
+    shared_ptr<CContext> context=CContext::current() ;
+    
+    if (! context->hasServer )
+    {
+       CContextClient* client=context->client ;
+
+       CEventClient event(this->getType(),EVENT_ID_ADD_FIELD) ;   
+       if (client->isServerLeader())
+       {
+         CMessage msg ;
+         msg<<this->getId() ;
+         msg<<id ;
+         event.push(client->getServerLeader(),1,msg) ;
+         client->sendEvent(event) ;
+       }
+       else client->sendEvent(event) ;
+    }
+      
+   }
+   
+   void CFile::sendAddFieldGroup(const string& id)
+   {
+    shared_ptr<CContext> context=CContext::current() ;
+    if (! context->hasServer )
+    {
+       CContextClient* client=context->client ;
+
+       CEventClient event(this->getType(),EVENT_ID_ADD_FIELD_GROUP) ;   
+       if (client->isServerLeader())
+       {
+         CMessage msg ;
+         msg<<this->getId() ;
+         msg<<id ;
+         event.push(client->getServerLeader(),1,msg) ;
+         client->sendEvent(event) ;
+       }
+       else client->sendEvent(event) ;
+    }
+      
+   }
+   
+   void CFile::recvAddField(CEventServer& event)
+   {
+      
+      CBufferIn* buffer=event.subEvents.begin()->buffer;
+      string id;
+      *buffer>>id ;
+      get(id)->recvAddField(*buffer) ;
+   }
+   
+   
+   void CFile::recvAddField(CBufferIn& buffer)
+   {
+      string id ;
+      buffer>>id ;
+      addField(id) ;
+   }
+
+   void CFile::recvAddFieldGroup(CEventServer& event)
+   {
+      
+      CBufferIn* buffer=event.subEvents.begin()->buffer;
+      string id;
+      *buffer>>id ;
+      get(id)->recvAddFieldGroup(*buffer) ;
+   }
+   
+   
+   void CFile::recvAddFieldGroup(CBufferIn& buffer)
+   {
+      string id ;
+      buffer>>id ;
+      addFieldGroup(id) ;
+   }
+   
+
+   bool CFile::dispatchEvent(CEventServer& event)
+   {
+      if (SuperClass::dispatchEvent(event)) return true ;
+      else
+      {
+        switch(event.type)
+        {
+           case EVENT_ID_ADD_FIELD :
+             recvAddField(event) ;
+             return true ;
+             break ;
+         
+           case EVENT_ID_ADD_FIELD_GROUP :
+             recvAddFieldGroup(event) ;
+             return true ;
+             break ;       
+         
+           default :
+              ERROR("bool CFile::dispatchEvent(CEventServer& event)", <<"Unknown Event") ;
+           return false ;
+        }
+      }
+   }
+   
+   
+   
+   
    ///---------------------------------------------------------------
 
