customer.cpp
Go to the documentation of this file.
00001 /*************************************************************************** 00002 file : $URL: https://frepple.svn.sourceforge.net/svnroot/frepple/trunk/src/model/customer.cpp $ 00003 version : $LastChangedRevision: 1505 $ $LastChangedBy: jdetaeye $ 00004 date : $LastChangedDate: 2011-08-26 18:55:08 +0200 (Fri, 26 Aug 2011) $ 00005 ***************************************************************************/ 00006 00007 /*************************************************************************** 00008 * * 00009 * Copyright (C) 2007-2011 by Johan De Taeye, frePPLe bvba * 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 Customer> DECLARE_EXPORT Tree utils::HasName<Customer>::st; 00035 DECLARE_EXPORT const MetaCategory* Customer::metadata; 00036 DECLARE_EXPORT const MetaClass* CustomerDefault::metadata; 00037 00038 00039 int Customer::initialize() 00040 { 00041 // Initialize the metadata 00042 metadata = new MetaCategory("customer", "customers", reader, writer); 00043 00044 // Initialize the Python class 00045 return FreppleCategory<Customer>::initialize(); 00046 } 00047 00048 00049 int CustomerDefault::initialize() 00050 { 00051 // Initialize the metadata 00052 CustomerDefault::metadata = new MetaClass( 00053 "customer", 00054 "customer_default", 00055 Object::createString<CustomerDefault>, true); 00056 00057 // Initialize the Python class 00058 return FreppleClass<CustomerDefault,Customer>::initialize(); 00059 } 00060 00061 00062 DECLARE_EXPORT void Customer::writeElement(XMLOutput* o, const Keyword& tag, mode m) const 00063 { 00064 // Writing a reference 00065 if (m == REFERENCE) 00066 { 00067 o->writeElement(tag, Tags::tag_name, getName()); 00068 return; 00069 } 00070 00071 // Write the complete object 00072 if (m != NOHEADER) o->BeginObject(tag, Tags::tag_name, getName()); 00073 00074 // Write the fields 00075 HasDescription::writeElement(o, tag); 00076 HasHierarchy<Customer>::writeElement(o, tag); 00077 o->EndObject(tag); 00078 } 00079 00080 00081 DECLARE_EXPORT void Customer::beginElement(XMLInput& pIn, const Attribute& pAttr) 00082 { 00083 HasHierarchy<Customer>::beginElement(pIn, pAttr); 00084 } 00085 00086 00087 DECLARE_EXPORT void Customer::endElement(XMLInput& pIn, const Attribute& pAttr, const DataElement& pElement) 00088 { 00089 HasDescription::endElement(pIn, pAttr, pElement); 00090 HasHierarchy<Customer>::endElement(pIn, pAttr, pElement); 00091 } 00092 00093 00094 DECLARE_EXPORT Customer::~Customer() 00095 { 00096 // Remove all references from demands to this customer 00097 for (Demand::iterator i = Demand::begin(); i != Demand::end(); ++i) 00098 if (i->getCustomer() == this) i->setCustomer(NULL); 00099 } 00100 00101 00102 DECLARE_EXPORT PyObject* Customer::getattro(const Attribute& attr) 00103 { 00104 if (attr.isA(Tags::tag_name)) 00105 return PythonObject(getName()); 00106 if (attr.isA(Tags::tag_description)) 00107 return PythonObject(getDescription()); 00108 if (attr.isA(Tags::tag_category)) 00109 return PythonObject(getCategory()); 00110 if (attr.isA(Tags::tag_subcategory)) 00111 return PythonObject(getSubCategory()); 00112 if (attr.isA(Tags::tag_owner)) 00113 return PythonObject(getOwner()); 00114 if (attr.isA(Tags::tag_hidden)) 00115 return PythonObject(getHidden()); 00116 if (attr.isA(Tags::tag_members)) 00117 return new CustomerIterator(this); 00118 return NULL; 00119 } 00120 00121 00122 DECLARE_EXPORT int Customer::setattro(const Attribute& attr, const PythonObject& field) 00123 { 00124 if (attr.isA(Tags::tag_name)) 00125 setName(field.getString()); 00126 else if (attr.isA(Tags::tag_description)) 00127 setDescription(field.getString()); 00128 else if (attr.isA(Tags::tag_category)) 00129 setCategory(field.getString()); 00130 else if (attr.isA(Tags::tag_subcategory)) 00131 setSubCategory(field.getString()); 00132 else if (attr.isA(Tags::tag_owner)) 00133 { 00134 if (!field.check(Customer::metadata)) 00135 { 00136 PyErr_SetString(PythonDataException, "customer owner must be of type customer"); 00137 return -1; 00138 } 00139 Customer* y = static_cast<Customer*>(static_cast<PyObject*>(field)); 00140 setOwner(y); 00141 } 00142 else if (attr.isA(Tags::tag_hidden)) 00143 setHidden(field.getBool()); 00144 else 00145 return -1; 00146 return 0; 00147 } 00148 00149 00150 } // end namespace