item.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002   file : $URL: https://frepple.svn.sourceforge.net/svnroot/frepple/tags/0.8.0/src/model/item.cpp $
00003   version : $LastChangedRevision: 1108 $  $LastChangedBy: jdetaeye $
00004   date : $LastChangedDate: 2009-12-06 18:54:18 +0100 (Sun, 06 Dec 2009) $
00005  ***************************************************************************/
00006 
00007 /***************************************************************************
00008  *                                                                         *
00009  * Copyright (C) 2007 by Johan De Taeye                                    *
00010  *                                                                         *
00011  * This library is free software; you can redistribute it and/or modify it *
00012  * under the terms of the GNU Lesser General Public License as published   *
00013  * by the Free Software Foundation; either version 2.1 of the License, or  *
00014  * (at your option) any later version.                                     *
00015  *                                                                         *
00016  * This library is distributed in the hope that it will be useful,         *
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
00019  * General Public License for more details.                                *
00020  *                                                                         *
00021  * You should have received a copy of the GNU Lesser General Public        *
00022  * License along with this library; if not, write to the Free Software     *
00023  * Foundation Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 *
00024  * USA                                                                     *
00025  *                                                                         *
00026  ***************************************************************************/
00027 
00028 #define FREPPLE_CORE
00029 #include "frepple/model.h"
00030 
00031 namespace frepple
00032 {
00033 
00034 template<class Item> DECLARE_EXPORT Tree utils::HasName<Item>::st;
00035 DECLARE_EXPORT const MetaCategory* Item::metadata;
00036 DECLARE_EXPORT const MetaClass* ItemDefault::metadata;
00037 
00038 
00039 int Item::initialize()
00040 {
00041   // Initialize the metadata
00042   metadata = new MetaCategory("item", "items", reader, writer);
00043 
00044   // Initialize the Python class
00045   return FreppleCategory<Item>::initialize();
00046 }
00047 
00048 
00049 int ItemDefault::initialize()
00050 {
00051   // Initialize the metadata
00052   ItemDefault::metadata = new MetaClass("item", "item_default",
00053     Object::createString<ItemDefault>, true);
00054 
00055   // Initialize the Python class
00056   return FreppleClass<ItemDefault,Item>::initialize();
00057 }
00058 
00059 
00060 DECLARE_EXPORT Item::~Item()
00061 {
00062   // Remove references from the buffers
00063   for (Buffer::iterator buf = Buffer::begin(); buf != Buffer::end(); ++buf)
00064     if (buf->getItem() == this) buf->setItem(NULL);
00065 
00066   // Remove references from the demands
00067   for (Demand::iterator l = Demand::begin(); l != Demand::end(); ++l)
00068     if (l->getItem() == this) l->setItem(NULL);
00069 }
00070 
00071 
00072 DECLARE_EXPORT void Item::writeElement(XMLOutput *o, const Keyword& tag, mode m) const
00073 {
00074   // Writing a reference
00075   if (m == REFERENCE)
00076   {
00077     o->writeElement(tag, Tags::tag_name, getName());
00078     return;
00079   }
00080 
00081   // Write the complete item
00082   if (m != NOHEADER) o->BeginObject(tag, Tags::tag_name, getName());
00083 
00084   // Write the fields
00085   HasDescription::writeElement(o, tag);
00086   HasHierarchy<Item>::writeElement(o, tag);
00087   o->writeElement(Tags::tag_operation, deliveryOperation);
00088   if (getPrice() != 0.0) o->writeElement(Tags::tag_price, getPrice());
00089   o->EndObject(tag);
00090 }
00091 
00092 
00093 DECLARE_EXPORT void Item::beginElement(XMLInput& pIn, const Attribute& pAttr)
00094 {
00095   if (pAttr.isA (Tags::tag_operation))
00096     pIn.readto( Operation::reader(Operation::metadata,pIn.getAttributes()) );
00097   else
00098     HasHierarchy<Item>::beginElement(pIn, pAttr);
00099 }
00100 
00101 
00102 DECLARE_EXPORT void Item::endElement(XMLInput& pIn, const Attribute& pAttr, const DataElement& pElement)
00103 {
00104   if (pAttr.isA(Tags::tag_operation))
00105   {
00106     Operation *o = dynamic_cast<Operation*>(pIn.getPreviousObject());
00107     if (o) setOperation(o);
00108     else throw LogicException("Incorrect object type during read operation");
00109   }
00110   else if (pAttr.isA(Tags::tag_price))
00111     setPrice(pElement.getDouble());
00112   else
00113   {
00114     HasDescription::endElement(pIn, pAttr, pElement);
00115     HasHierarchy<Item>::endElement(pIn, pAttr, pElement);
00116   }
00117 }
00118 
00119 
00120 DECLARE_EXPORT PyObject* Item::getattro(const Attribute& attr)
00121 {
00122   if (attr.isA(Tags::tag_name))
00123     return PythonObject(getName());
00124   if (attr.isA(Tags::tag_description))
00125     return PythonObject(getDescription());
00126   if (attr.isA(Tags::tag_category))
00127     return PythonObject(getCategory());
00128   if (attr.isA(Tags::tag_subcategory))
00129     return PythonObject(getSubCategory());
00130   if (attr.isA(Tags::tag_price))
00131     return PythonObject(getPrice());
00132   if (attr.isA(Tags::tag_owner))
00133     return PythonObject(getOwner());
00134   if (attr.isA(Tags::tag_operation))
00135     return PythonObject(getOperation());
00136   if (attr.isA(Tags::tag_hidden))
00137     return PythonObject(getHidden());
00138   return NULL;
00139 }
00140 
00141 
00142 DECLARE_EXPORT int Item::setattro(const Attribute& attr, const PythonObject& field)
00143 {
00144   if (attr.isA(Tags::tag_name))
00145     setName(field.getString());
00146   else if (attr.isA(Tags::tag_description))
00147     setDescription(field.getString());
00148   else if (attr.isA(Tags::tag_category))
00149     setCategory(field.getString());
00150   else if (attr.isA(Tags::tag_subcategory))
00151     setSubCategory(field.getString());
00152   else if (attr.isA(Tags::tag_price))
00153     setPrice(field.getDouble());
00154   else if (attr.isA(Tags::tag_owner))
00155   {
00156     if (!field.check(Item::metadata))
00157     {
00158       PyErr_SetString(PythonDataException, "item owner must be of type item");
00159       return -1;
00160     }
00161     Item* y = static_cast<Item*>(static_cast<PyObject*>(field));
00162     setOwner(y);
00163   }
00164   else if (attr.isA(Tags::tag_operation))
00165   {
00166     if (!field.check(Operation::metadata))
00167     {
00168       PyErr_SetString(PythonDataException, "item operation must be of type operation");
00169       return -1;
00170     }
00171     Operation* y = static_cast<Operation*>(static_cast<PyObject*>(field));
00172     setOperation(y);
00173   }
00174   else if (attr.isA(Tags::tag_hidden))
00175     setHidden(field.getBool());
00176   else
00177     return -1;
00178   return 0;
00179 }
00180 
00181 
00182 } // end namespace