source: XMLIO_V2/dev/common/src/xmlio/node/context.cpp @ 219

Last change on this file since 219 was 219, checked in by hozdoba, 13 years ago

Préparation nouvelle arborescence

File size: 10.1 KB
Line 
1#include "context.hpp"
2
3#include "tree_manager.hpp"
4
5#include "attribute_template_impl.hpp"
6#include "object_template_impl.hpp"
7#include "group_template_impl.hpp"
8
9#include "calendar_type.hpp"
10
11#include "data_treatment.hpp"
12
13namespace xmlioserver {
14namespace tree {
15   
16   /// ////////////////////// Définitions ////////////////////// ///
17
18   CContext::CContext(void)
19      : CObjectTemplate<CContext>(), CContextAttributes()
20      , calendar()
21   { /* Ne rien faire de plus */ }
22
23   CContext::CContext(const StdString & id)
24      : CObjectTemplate<CContext>(id), CContextAttributes()
25      , calendar()
26   { /* Ne rien faire de plus */ }
27
28   CContext::~CContext(void)
29   { /* Ne rien faire de plus */ }
30
31   //----------------------------------------------------------------
32
33   StdString CContext::GetName(void)   { return (StdString("context")); }
34   StdString CContext::GetDefName(void){ return (CContext::GetName()); }
35   ENodeType CContext::GetType(void)   { return (eContext); }
36
37   //----------------------------------------------------------------
38
39   boost::shared_ptr<CContextGroup> CContext::GetContextGroup(void)
40   { 
41      static boost::shared_ptr<CContextGroup> group_context
42                          (new CContextGroup(xml::CXMLNode::GetRootName()));
43      return (group_context); 
44   }
45   
46   //----------------------------------------------------------------
47   void CContext::setDataTreatment(boost::shared_ptr<data::CDataTreatment> ndatat)
48   {
49      this->datat = ndatat;
50   }
51
52   //----------------------------------------------------------------
53   boost::shared_ptr<data::CDataTreatment> CContext::getDataTreatment(void) const
54   {
55      return (this->datat);
56   }
57
58   //----------------------------------------------------------------
59
60   boost::shared_ptr<date::CCalendar> CContext::getCalendar(void) const
61   {
62      return (this->calendar);
63   }
64   
65   //----------------------------------------------------------------
66   
67   void CContext::setCalendar(boost::shared_ptr<date::CCalendar> newCalendar)
68   {
69      this->calendar = newCalendar;
70      calendar_type.setValue(this->calendar->getId());
71      start_date.setValue(this->calendar->getInitDate().toString());
72   }
73   
74   //----------------------------------------------------------------
75
76   void CContext::solveCalendar(void)
77   {
78      if (this->calendar.get() != NULL) return;
79      if (calendar_type.isEmpty() || start_date.isEmpty())
80         ERROR(" CContext::solveCalendar(void)",
81               << "[ context id = " << this->getId() << " ] "
82               << "Impossible de définir un calendrier (un attribut est manquant).");
83
84#define DECLARE_CALENDAR(MType  , mtype)                        \
85   if (calendar_type.getValue().compare(#mtype) == 0)           \
86   {                                                            \
87      this->calendar =  boost::shared_ptr<date::CCalendar>      \
88         (new date::C##MType##Calendar(start_date.getValue())); \
89      return;                                                   \
90   }
91#include "calendar_type.conf"
92
93      ERROR("CContext::solveCalendar(void)",
94            << "[ calendar_type = " << calendar_type.getValue() << " ] "
95            << "Le calendrier n'est pas définie dans le code !");
96   }
97   
98   //----------------------------------------------------------------
99
100   void CContext::parse(xml::CXMLNode & node)
101   {
102      CContext::SuperClass::parse(node);
103
104      // PARSING POUR GESTION DES ENFANTS
105      xml::THashAttributes attributes;
106
107      if (node.getElementName().compare(CContext::GetName()))
108         DEBUG("Le noeud est mal nommé mais sera traité comme un contexte !");
109
110      if (!(node.goToChildElement()))
111      {
112         DEBUG("Le context ne contient pas d'enfant !");
113      }
114      else
115      {
116         do { // Parcours des contextes pour traitement.
117
118            StdString name = node.getElementName();
119            attributes.clear();
120            attributes = node.getAttributes();
121
122            if (attributes.end() != attributes.find("id"))
123            { DEBUG(<< "Le noeud de définition possÚde un identifiant,"
124                    << " ce dernier ne sera pas pris en compte lors du traitement !"); }
125
126#define DECLARE_NODE(Name_, name_)    \
127   if (name.compare(C##Name_##Definition::GetDefName()) == 0) \
128   { CObjectFactory::CreateObject<C##Name_##Definition>(C##Name_##Definition::GetDefName()) -> parse(node); \
129   continue; }
130#define DECLARE_NODE_PAR(Name_, name_)
131#include "node_type.conf"
132
133            DEBUG(<< "L'élément nommé \'"     << name
134                  << "\' dans le contexte \'" << CObjectFactory::GetCurrentContextId()
135                  << "\' ne représente pas une définition !");
136
137         } while (node.goToNextElement());
138
139         node.goToParentElement(); // Retour au parent
140      }
141   }
142
143   //----------------------------------------------------------------
144
145   void CContext::ShowTree(StdOStream & out)
146   {
147      StdString currentContextId =
148         CObjectFactory::GetCurrentContextId();
149      std::vector<boost::shared_ptr<CContext> > def_vector =
150         CContext::GetContextGroup()->getChildList();
151      std::vector<boost::shared_ptr<CContext> >::iterator
152         it = def_vector.begin(), end = def_vector.end();
153
154      out << "<? xml version=\"1.0\" ?>" << std::endl;
155      out << "<"  << xml::CXMLNode::GetRootName() << " >" << std::endl;
156     
157      for (; it != end; it++)
158      {
159         boost::shared_ptr<CContext> context = *it;         
160         CTreeManager::SetCurrentContextId(context->getId());         
161         out << *context << std::endl;
162      }
163     
164      out << "</" << xml::CXMLNode::GetRootName() << " >" << std::endl;
165      CTreeManager::SetCurrentContextId(currentContextId); 
166   }
167   
168   //----------------------------------------------------------------
169   
170   void CContext::toBinary(StdOStream & os) const
171   {
172      SuperClass::toBinary(os);
173       
174#define DECLARE_NODE(Name_, name_)                                         \
175   {                                                                       \
176      ENodeType renum = C##Name_##Definition::GetType();                   \
177      bool val = CObjectFactory::HasObject<C##Name_##Definition>           \
178                     (C##Name_##Definition::GetDefName());                 \
179      os.write (reinterpret_cast<const char*>(&renum), sizeof(ENodeType)); \
180      os.write (reinterpret_cast<const char*>(&val), sizeof(bool));        \
181      if (val) CObjectFactory::GetObject<C##Name_##Definition>             \
182                     (C##Name_##Definition::GetDefName())->toBinary(os);   \
183   }   
184#define DECLARE_NODE_PAR(Name_, name_)
185#include "node_type.conf"
186   }
187   
188   //----------------------------------------------------------------
189   
190   void CContext::fromBinary(StdIStream & is)
191   {
192      SuperClass::fromBinary(is);
193#define DECLARE_NODE(Name_, name_)                                         \
194   {                                                                       \
195      bool val = false;                                                    \
196      ENodeType renum = Unknown;                                           \
197      is.read (reinterpret_cast<char*>(&renum), sizeof(ENodeType));        \
198      is.read (reinterpret_cast<char*>(&val), sizeof(bool));               \
199      if (renum != C##Name_##Definition::GetType())                        \
200         ERROR("CContext::fromBinary(StdIStream & is)",                    \
201               << "[ renum = " << renum << "] Bad type !");                \
202      if (val) CObjectFactory::CreateObject<C##Name_##Definition>          \
203                  (C##Name_##Definition::GetDefName()) -> fromBinary(is);  \
204   }   
205#define DECLARE_NODE_PAR(Name_, name_)
206#include "node_type.conf"
207     
208   }
209   
210   
211   //----------------------------------------------------------------
212
213   StdString CContext::toString(void) const
214   {
215      StdOStringStream oss;
216      oss << "<" << CContext::GetName()
217          << " id=\"" << this->getId() << "\" "
218          << SuperClassAttribute::toString() << ">" << std::endl;
219      if (!this->hasChild())
220      {
221         //oss << "<!-- No definition -->" << std::endl; // fait planter l'incrémentation
222      }
223      else
224      {
225
226#define DECLARE_NODE(Name_, name_)    \
227   if (CObjectFactory::HasObject<C##Name_##Definition>(C##Name_##Definition::GetDefName())) \
228   oss << *CObjectFactory::GetObject<C##Name_##Definition>(C##Name_##Definition::GetDefName()) << std::endl;
229#define DECLARE_NODE_PAR(Name_, name_)
230#include "node_type.conf"
231
232      }
233
234      oss << "</" << CContext::GetName() << " >";
235
236      return (oss.str());
237   }
238
239   //----------------------------------------------------------------
240
241   void CContext::solveDescInheritance(const CAttributeMap * const UNUSED(parent))
242   {
243#define DECLARE_NODE(Name_, name_)    \
244   if (CObjectFactory::HasObject<C##Name_##Definition>(C##Name_##Definition::GetDefName())) \
245   CObjectFactory::GetObject<C##Name_##Definition>(C##Name_##Definition::GetDefName())->solveDescInheritance();
246#define DECLARE_NODE_PAR(Name_, name_)
247#include "node_type.conf"
248   }
249
250   //----------------------------------------------------------------
251
252   bool CContext::hasChild(void) const
253   {
254      return (
255#define DECLARE_NODE(Name_, name_)    \
256   CObjectFactory::HasObject<C##Name_##Definition>  (C##Name_##Definition::GetDefName())   ||
257#define DECLARE_NODE_PAR(Name_, name_)
258#include "node_type.conf"
259      false);
260}
261
262   //----------------------------------------------------------------
263
264   void CContext::solveFieldRefInheritance(void)
265   {
266      if (!this->hasId()) return;
267      std::vector<boost::shared_ptr<CField> > allField
268               = CObjectTemplate<CField>::GetAllVectobject(this->getId());
269      std::vector<boost::shared_ptr<CField> >::iterator
270         it = allField.begin(), end = allField.end();
271           
272      for (; it != end; it++)
273      {
274         boost::shared_ptr<CField> field = *it;
275         field->solveRefInheritance();
276      }
277   }
278
279   //----------------------------------------------------------------
280
281   void CContext::CleanTree(void)
282   {
283#define DECLARE_NODE(Name_, name_) C##Name_##Group::ClearAllAttributes();
284#define DECLARE_NODE_PAR(Name_, name_)
285#include "node_type.conf"
286   }
287   ///---------------------------------------------------------------
288
289} // namespace tree
290} // namespace xmlioserver
Note: See TracBrowser for help on using the repository browser.