00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #define FREPPLE_CORE
00028 #include "frepple/model.h"
00029
00030 namespace frepple
00031 {
00032
00033 template<class Item> DECLARE_EXPORT Tree utils::HasName<Item>::st;
00034
00035
00036 DECLARE_EXPORT Item::~Item()
00037 {
00038
00039 for (Buffer::iterator buf = Buffer::begin(); buf != Buffer::end(); ++buf)
00040 if (buf->getItem() == this) buf->setItem(NULL);
00041
00042
00043 for (Demand::iterator l = Demand::begin(); l != Demand::end(); ++l)
00044 if (l->getItem() == this) l->setItem(NULL);
00045 }
00046
00047
00048 DECLARE_EXPORT void Item::writeElement(XMLOutput *o, const Keyword& tag, mode m) const
00049 {
00050
00051 if (m == REFERENCE)
00052 {
00053 o->writeElement(tag, Tags::tag_name, getName());
00054 return;
00055 }
00056
00057
00058 if (m != NOHEADER) o->BeginObject(tag, Tags::tag_name, getName());
00059
00060
00061 HasDescription::writeElement(o, tag);
00062 HasHierarchy<Item>::writeElement(o, tag);
00063 o->writeElement(Tags::tag_operation, deliveryOperation);
00064 if (getPrice() != 0.0) o->writeElement(Tags::tag_price, getPrice());
00065 o->EndObject(tag);
00066 }
00067
00068
00069 DECLARE_EXPORT void Item::beginElement(XMLInput& pIn, const Attribute& pAttr)
00070 {
00071 if (pAttr.isA (Tags::tag_operation))
00072 pIn.readto( Operation::reader(Operation::metadata,pIn.getAttributes()) );
00073 else
00074 HasHierarchy<Item>::beginElement(pIn, pAttr);
00075 }
00076
00077
00078 DECLARE_EXPORT void Item::endElement(XMLInput& pIn, const Attribute& pAttr, const DataElement& pElement)
00079 {
00080 if (pAttr.isA(Tags::tag_operation))
00081 {
00082 Operation *o = dynamic_cast<Operation*>(pIn.getPreviousObject());
00083 if (o) setOperation(o);
00084 else throw LogicException("Incorrect object type during read operation");
00085 }
00086 else if (pAttr.isA(Tags::tag_price))
00087 setPrice(pElement.getDouble());
00088 else
00089 {
00090 HasDescription::endElement(pIn, pAttr, pElement);
00091 HasHierarchy<Item>::endElement(pIn, pAttr, pElement);
00092 }
00093 }
00094
00095
00096 DECLARE_EXPORT PyObject* PythonItem::getattro(const Attribute& attr)
00097 {
00098 if (!obj) return Py_BuildValue("");
00099 if (attr.isA(Tags::tag_name))
00100 return PythonObject(obj->getName());
00101 if (attr.isA(Tags::tag_description))
00102 return PythonObject(obj->getDescription());
00103 if (attr.isA(Tags::tag_category))
00104 return PythonObject(obj->getCategory());
00105 if (attr.isA(Tags::tag_subcategory))
00106 return PythonObject(obj->getSubCategory());
00107 if (attr.isA(Tags::tag_price))
00108 return PythonObject(obj->getPrice());
00109 if (attr.isA(Tags::tag_owner))
00110 return PythonObject(obj->getOwner());
00111 if (attr.isA(Tags::tag_operation))
00112 return PythonObject(obj->getOperation());
00113 if (attr.isA(Tags::tag_hidden))
00114 return PythonObject(obj->getHidden());
00115 return NULL;
00116 }
00117
00118
00119 DECLARE_EXPORT int PythonItem::setattro(const Attribute& attr, const PythonObject& field)
00120 {
00121 if (attr.isA(Tags::tag_name))
00122 obj->setName(field.getString());
00123 else if (attr.isA(Tags::tag_description))
00124 obj->setDescription(field.getString());
00125 else if (attr.isA(Tags::tag_category))
00126 obj->setCategory(field.getString());
00127 else if (attr.isA(Tags::tag_subcategory))
00128 obj->setSubCategory(field.getString());
00129 else if (attr.isA(Tags::tag_price))
00130 obj->setPrice(field.getDouble());
00131 else if (attr.isA(Tags::tag_owner))
00132 {
00133 if (!field.check(PythonItem::getType()))
00134 {
00135 PyErr_SetString(PythonDataException, "item owner must be of type item");
00136 return -1;
00137 }
00138 Item* y = static_cast<PythonItem*>(static_cast<PyObject*>(field))->obj;
00139 obj->setOwner(y);
00140 }
00141 else if (attr.isA(Tags::tag_operation))
00142 {
00143 if (!field.check(PythonOperation::getType()))
00144 {
00145 PyErr_SetString(PythonDataException, "item operation must be of type operation");
00146 return -1;
00147 }
00148 Operation* y = static_cast<PythonOperation*>(static_cast<PyObject*>(field))->obj;
00149 obj->setOperation(y);
00150 }
00151 else if (attr.isA(Tags::tag_hidden))
00152 obj->setHidden(field.getBool());
00153 else
00154 return -1;
00155 return 0;
00156 }
00157
00158
00159 DECLARE_EXPORT PyObject* PythonItemDefault::getattro(const Attribute& attr)
00160 {
00161 return PythonItem(obj).getattro(attr);
00162 }
00163
00164
00165 DECLARE_EXPORT int PythonItemDefault::setattro(const Attribute& attr, const PythonObject& field)
00166 {
00167 return PythonItem(obj).setattro(attr, field);
00168 }
00169
00170 }