item.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * Copyright (C) 2007-2012 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 complete item
75  if (m != NOHEADER) o->BeginObject(tag, Tags::tag_name, XMLEscape(getName()));
76 
77  // Write the fields
80  o->writeElement(Tags::tag_operation, deliveryOperation);
81  if (getPrice() != 0.0) o->writeElement(Tags::tag_price, getPrice());
82  o->EndObject(tag);
83 }
84 
85 
87 {
88  if (pAttr.isA (Tags::tag_operation))
90  else
92 }
93 
94 
95 DECLARE_EXPORT void Item::endElement(XMLInput& pIn, const Attribute& pAttr, const DataElement& pElement)
96 {
97  if (pAttr.isA(Tags::tag_operation))
98  {
99  Operation *o = dynamic_cast<Operation*>(pIn.getPreviousObject());
100  if (o) setOperation(o);
101  else throw LogicException("Incorrect object type during read operation");
102  }
103  else if (pAttr.isA(Tags::tag_price))
104  setPrice(pElement.getDouble());
105  else
106  {
107  HasDescription::endElement(pIn, pAttr, pElement);
108  HasHierarchy<Item>::endElement(pIn, pAttr, pElement);
109  }
110 }
111 
112 
114 {
115  if (attr.isA(Tags::tag_name))
116  return PythonObject(getName());
117  if (attr.isA(Tags::tag_description))
118  return PythonObject(getDescription());
119  if (attr.isA(Tags::tag_category))
120  return PythonObject(getCategory());
121  if (attr.isA(Tags::tag_subcategory))
122  return PythonObject(getSubCategory());
123  if (attr.isA(Tags::tag_price))
124  return PythonObject(getPrice());
125  if (attr.isA(Tags::tag_owner))
126  return PythonObject(getOwner());
127  if (attr.isA(Tags::tag_operation))
128  return PythonObject(getOperation());
129  if (attr.isA(Tags::tag_hidden))
130  return PythonObject(getHidden());
131  if (attr.isA(Tags::tag_members))
132  return new ItemIterator(this);
133  return NULL;
134 }
135 
136 
137 DECLARE_EXPORT int Item::setattro(const Attribute& attr, const PythonObject& field)
138 {
139  if (attr.isA(Tags::tag_name))
140  setName(field.getString());
141  else if (attr.isA(Tags::tag_description))
142  setDescription(field.getString());
143  else if (attr.isA(Tags::tag_category))
144  setCategory(field.getString());
145  else if (attr.isA(Tags::tag_subcategory))
146  setSubCategory(field.getString());
147  else if (attr.isA(Tags::tag_price))
148  setPrice(field.getDouble());
149  else if (attr.isA(Tags::tag_owner))
150  {
151  if (!field.check(Item::metadata))
152  {
153  PyErr_SetString(PythonDataException, "item owner must be of type item");
154  return -1;
155  }
156  Item* y = static_cast<Item*>(static_cast<PyObject*>(field));
157  setOwner(y);
158  }
159  else if (attr.isA(Tags::tag_operation))
160  {
161  if (!field.check(Operation::metadata))
162  {
163  PyErr_SetString(PythonDataException, "item operation must be of type operation");
164  return -1;
165  }
166  Operation* y = static_cast<Operation*>(static_cast<PyObject*>(field));
167  setOperation(y);
168  }
169  else if (attr.isA(Tags::tag_hidden))
170  setHidden(field.getBool());
171  else
172  return -1;
173  return 0;
174 }
175 
176 
177 } // end namespace