item.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * Copyright (C) 2007-2013 by Johan De Taeye, frePPLe bvba *
4  * *
5  * This library is free software; you can redistribute it and/or modify it *
6  * under the terms of the GNU Affero General Public License as published *
7  * by the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU Affero General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Affero General Public *
16  * License along with this program. *
17  * If not, see <http://www.gnu.org/licenses/>. *
18  * *
19  ***************************************************************************/
20 
21 #define FREPPLE_CORE
22 #include "frepple/model.h"
23 
24 namespace frepple
25 {
26 
27 template<class Item> DECLARE_EXPORT Tree utils::HasName<Item>::st;
30 
31 
33 {
34  // Initialize the metadata
35  metadata = new MetaCategory("item", "items", reader, writer);
36 
37  // Initialize the Python class
39 }
40 
41 
43 {
44  // Initialize the metadata
45  ItemDefault::metadata = new MetaClass("item", "item_default",
46  Object::createString<ItemDefault>, true);
47 
48  // Initialize the Python class
50 }
51 
52 
54 {
55  // Remove references from the buffers
56  for (Buffer::iterator buf = Buffer::begin(); buf != Buffer::end(); ++buf)
57  if (buf->getItem() == this) buf->setItem(NULL);
58 
59  // Remove references from the demands
60  for (Demand::iterator l = Demand::begin(); l != Demand::end(); ++l)
61  if (l->getItem() == this) l->setItem(NULL);
62 }
63 
64 
66 {
67  // Writing a reference
68  if (m == REFERENCE)
69  {
71  return;
72  }
73 
74  // Write the head
75  if (m != NOHEAD && m != NOHEADTAIL)
77 
78  // Write the fields
79  HasDescription::writeElement(o, tag);
81  o->writeElement(Tags::tag_operation, deliveryOperation);
82  if (getPrice() != 0.0) o->writeElement(Tags::tag_price, getPrice());
83 
84  // Write the tail
85  if (m != NOHEADTAIL && m != NOTAIL) o->EndObject(tag);
86 }
87 
88 
90 {
91  if (pAttr.isA (Tags::tag_operation))
93  else
95 }
96 
97 
98 DECLARE_EXPORT void Item::endElement(XMLInput& pIn, const Attribute& pAttr, const DataElement& pElement)
99 {
100  if (pAttr.isA(Tags::tag_operation))
101  {
102  Operation *o = dynamic_cast<Operation*>(pIn.getPreviousObject());
103  if (o) setOperation(o);
104  else throw LogicException("Incorrect object type during read operation");
105  }
106  else if (pAttr.isA(Tags::tag_price))
107  setPrice(pElement.getDouble());
108  else
109  {
110  HasDescription::endElement(pIn, pAttr, pElement);
111  HasHierarchy<Item>::endElement(pIn, pAttr, pElement);
112  }
113 }
114 
115 
117 {
118  if (attr.isA(Tags::tag_name))
119  return PythonObject(getName());
120  if (attr.isA(Tags::tag_description))
121  return PythonObject(getDescription());
122  if (attr.isA(Tags::tag_category))
123  return PythonObject(getCategory());
124  if (attr.isA(Tags::tag_subcategory))
125  return PythonObject(getSubCategory());
126  if (attr.isA(Tags::tag_price))
127  return PythonObject(getPrice());
128  if (attr.isA(Tags::tag_owner))
129  return PythonObject(getOwner());
130  if (attr.isA(Tags::tag_operation))
131  return PythonObject(getOperation());
132  if (attr.isA(Tags::tag_hidden))
133  return PythonObject(getHidden());
134  if (attr.isA(Tags::tag_members))
135  return new ItemIterator(this);
136  return NULL;
137 }
138 
139 
140 DECLARE_EXPORT int Item::setattro(const Attribute& attr, const PythonObject& field)
141 {
142  if (attr.isA(Tags::tag_name))
143  setName(field.getString());
144  else if (attr.isA(Tags::tag_description))
145  setDescription(field.getString());
146  else if (attr.isA(Tags::tag_category))
147  setCategory(field.getString());
148  else if (attr.isA(Tags::tag_subcategory))
149  setSubCategory(field.getString());
150  else if (attr.isA(Tags::tag_price))
151  setPrice(field.getDouble());
152  else if (attr.isA(Tags::tag_owner))
153  {
154  if (!field.check(Item::metadata))
155  {
156  PyErr_SetString(PythonDataException, "item owner must be of type item");
157  return -1;
158  }
159  Item* y = static_cast<Item*>(static_cast<PyObject*>(field));
160  setOwner(y);
161  }
162  else if (attr.isA(Tags::tag_operation))
163  {
164  if (!field.check(Operation::metadata))
165  {
166  PyErr_SetString(PythonDataException, "item operation must be of type operation");
167  return -1;
168  }
169  Operation* y = static_cast<Operation*>(static_cast<PyObject*>(field));
170  setOperation(y);
171  }
172  else if (attr.isA(Tags::tag_hidden))
173  setHidden(field.getBool());
174  else
175  return -1;
176  return 0;
177 }
178 
179 
180 } // end namespace