[152] | 1 | #include "context.hpp" |
---|
| 2 | |
---|
[173] | 3 | #include "tree_manager.hpp" |
---|
| 4 | |
---|
[152] | 5 | #include "attribute_template_impl.hpp" |
---|
| 6 | #include "object_template_impl.hpp" |
---|
| 7 | #include "group_template_impl.hpp" |
---|
| 8 | |
---|
[188] | 9 | #include "calendar_type.hpp" |
---|
| 10 | |
---|
[202] | 11 | #include "data_treatment.hpp" |
---|
| 12 | |
---|
[168] | 13 | namespace xmlioserver { |
---|
| 14 | namespace tree { |
---|
| 15 | |
---|
| 16 | /// ////////////////////// Définitions ////////////////////// /// |
---|
[152] | 17 | |
---|
[168] | 18 | CContext::CContext(void) |
---|
| 19 | : CObjectTemplate<CContext>(), CContextAttributes() |
---|
[188] | 20 | , calendar() |
---|
[168] | 21 | { /* Ne rien faire de plus */ } |
---|
[152] | 22 | |
---|
[168] | 23 | CContext::CContext(const StdString & id) |
---|
| 24 | : CObjectTemplate<CContext>(id), CContextAttributes() |
---|
[188] | 25 | , calendar() |
---|
[168] | 26 | { /* Ne rien faire de plus */ } |
---|
[152] | 27 | |
---|
[168] | 28 | CContext::~CContext(void) |
---|
| 29 | { /* Ne rien faire de plus */ } |
---|
[152] | 30 | |
---|
[168] | 31 | //---------------------------------------------------------------- |
---|
[152] | 32 | |
---|
[168] | 33 | StdString CContext::GetName(void) { return (StdString("context")); } |
---|
| 34 | StdString CContext::GetDefName(void){ return (CContext::GetName()); } |
---|
| 35 | ENodeType CContext::GetType(void) { return (eContext); } |
---|
[152] | 36 | |
---|
[168] | 37 | //---------------------------------------------------------------- |
---|
[152] | 38 | |
---|
[168] | 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 | } |
---|
[188] | 45 | |
---|
| 46 | //---------------------------------------------------------------- |
---|
[202] | 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 | |
---|
[188] | 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 | //---------------------------------------------------------------- |
---|
[152] | 75 | |
---|
[188] | 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 | |
---|
[168] | 98 | //---------------------------------------------------------------- |
---|
[152] | 99 | |
---|
[168] | 100 | void CContext::parse(xml::CXMLNode & node) |
---|
| 101 | { |
---|
| 102 | CContext::SuperClass::parse(node); |
---|
[152] | 103 | |
---|
[168] | 104 | // PARSING POUR GESTION DES ENFANTS |
---|
| 105 | xml::THashAttributes attributes; |
---|
[152] | 106 | |
---|
[168] | 107 | if (node.getElementName().compare(CContext::GetName())) |
---|
| 108 | DEBUG("Le noeud est mal nommé mais sera traité comme un contexte !"); |
---|
[152] | 109 | |
---|
[168] | 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. |
---|
[152] | 117 | |
---|
[168] | 118 | StdString name = node.getElementName(); |
---|
| 119 | attributes.clear(); |
---|
| 120 | attributes = node.getAttributes(); |
---|
[152] | 121 | |
---|
[168] | 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 !"); } |
---|
[152] | 125 | |
---|
[173] | 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); \ |
---|
[168] | 129 | continue; } |
---|
[173] | 130 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 131 | #include "node_type.conf" |
---|
[152] | 132 | |
---|
[168] | 133 | DEBUG(<< "L'élément nommé \'" << name |
---|
| 134 | << "\' dans le contexte \'" << CObjectFactory::GetCurrentContextId() |
---|
| 135 | << "\' ne représente pas une définition !"); |
---|
[152] | 136 | |
---|
[168] | 137 | } while (node.goToNextElement()); |
---|
[152] | 138 | |
---|
[168] | 139 | node.goToParentElement(); // Retour au parent |
---|
[152] | 140 | } |
---|
[168] | 141 | } |
---|
[152] | 142 | |
---|
[168] | 143 | //---------------------------------------------------------------- |
---|
[152] | 144 | |
---|
[168] | 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(); |
---|
[152] | 153 | |
---|
[168] | 154 | out << "<? xml version=\"1.0\" ?>" << std::endl; |
---|
[173] | 155 | out << "<" << xml::CXMLNode::GetRootName() << " >" << std::endl; |
---|
[168] | 156 | |
---|
| 157 | for (; it != end; it++) |
---|
| 158 | { |
---|
[173] | 159 | boost::shared_ptr<CContext> context = *it; |
---|
| 160 | CTreeManager::SetCurrentContextId(context->getId()); |
---|
[168] | 161 | out << *context << std::endl; |
---|
| 162 | } |
---|
| 163 | |
---|
[173] | 164 | out << "</" << xml::CXMLNode::GetRootName() << " >" << std::endl; |
---|
| 165 | CTreeManager::SetCurrentContextId(currentContextId); |
---|
[168] | 166 | } |
---|
[173] | 167 | |
---|
[168] | 168 | //---------------------------------------------------------------- |
---|
[173] | 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 | //---------------------------------------------------------------- |
---|
[152] | 212 | |
---|
[168] | 213 | StdString CContext::toString(void) const |
---|
| 214 | { |
---|
| 215 | StdOStringStream oss; |
---|
[173] | 216 | oss << "<" << CContext::GetName() |
---|
[168] | 217 | << " id=\"" << this->getId() << "\" " |
---|
| 218 | << SuperClassAttribute::toString() << ">" << std::endl; |
---|
| 219 | if (!this->hasChild()) |
---|
[152] | 220 | { |
---|
[168] | 221 | //oss << "<!-- No definition -->" << std::endl; // fait planter l'incrémentation |
---|
[152] | 222 | } |
---|
[168] | 223 | else |
---|
| 224 | { |
---|
[152] | 225 | |
---|
[173] | 226 | #define DECLARE_NODE(Name_, name_) \ |
---|
| 227 | if (CObjectFactory::HasObject<C##Name_##Definition>(C##Name_##Definition::GetDefName())) \ |
---|
[168] | 228 | oss << *CObjectFactory::GetObject<C##Name_##Definition>(C##Name_##Definition::GetDefName()) << std::endl; |
---|
[173] | 229 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 230 | #include "node_type.conf" |
---|
[168] | 231 | |
---|
[152] | 232 | } |
---|
| 233 | |
---|
[173] | 234 | oss << "</" << CContext::GetName() << " >"; |
---|
[168] | 235 | |
---|
| 236 | return (oss.str()); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | //---------------------------------------------------------------- |
---|
| 240 | |
---|
| 241 | void CContext::solveDescInheritance(const CAttributeMap * const UNUSED(parent)) |
---|
| 242 | { |
---|
[173] | 243 | #define DECLARE_NODE(Name_, name_) \ |
---|
| 244 | if (CObjectFactory::HasObject<C##Name_##Definition>(C##Name_##Definition::GetDefName())) \ |
---|
[168] | 245 | CObjectFactory::GetObject<C##Name_##Definition>(C##Name_##Definition::GetDefName())->solveDescInheritance(); |
---|
[173] | 246 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 247 | #include "node_type.conf" |
---|
[168] | 248 | } |
---|
| 249 | |
---|
| 250 | //---------------------------------------------------------------- |
---|
| 251 | |
---|
| 252 | bool CContext::hasChild(void) const |
---|
| 253 | { |
---|
[173] | 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" |
---|
[168] | 259 | false); |
---|
[173] | 260 | } |
---|
[168] | 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++) |
---|
[152] | 273 | { |
---|
[168] | 274 | boost::shared_ptr<CField> field = *it; |
---|
| 275 | field->solveRefInheritance(); |
---|
[152] | 276 | } |
---|
[168] | 277 | } |
---|
[152] | 278 | |
---|
[168] | 279 | ///--------------------------------------------------------------- |
---|
| 280 | |
---|
| 281 | } // namespace tree |
---|
[152] | 282 | } // namespace xmlioserver |
---|