customer.cpp
Go to the documentation of this file.
00001 /*************************************************************************** 00002 file : $URL: http://svn.code.sf.net/p/frepple/code/trunk/src/model/customer.cpp $ 00003 version : $LastChangedRevision: 1713 $ $LastChangedBy: jdetaeye $ 00004 date : $LastChangedDate: 2012-07-18 11:46:01 +0200 (Wed, 18 Jul 2012) $ 00005 ***************************************************************************/ 00006 00007 /*************************************************************************** 00008 * * 00009 * Copyright (C) 2007-2012 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 Affero General Public License as published * 00013 * by the Free Software Foundation; either version 3 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 * 00019 * GNU Affero General Public License for more details. * 00020 * * 00021 * You should have received a copy of the GNU Affero General Public * 00022 * License along with this program. * 00023 * If not, see <http://www.gnu.org/licenses/>. * 00024 * * 00025 ***************************************************************************/ 00026 00027 #define FREPPLE_CORE 00028 #include "frepple/model.h" 00029 00030 namespace frepple 00031 { 00032 00033 template<class Customer> DECLARE_EXPORT Tree utils::HasName<Customer>::st; 00034 DECLARE_EXPORT const MetaCategory* Customer::metadata; 00035 DECLARE_EXPORT const MetaClass* CustomerDefault::metadata; 00036 00037 00038 int Customer::initialize() 00039 { 00040 // Initialize the metadata 00041 metadata = new MetaCategory("customer", "customers", reader, writer); 00042 00043 // Initialize the Python class 00044 return FreppleCategory<Customer>::initialize(); 00045 } 00046 00047 00048 int CustomerDefault::initialize() 00049 { 00050 // Initialize the metadata 00051 CustomerDefault::metadata = new MetaClass( 00052 "customer", 00053 "customer_default", 00054 Object::createString<CustomerDefault>, true); 00055 00056 // Initialize the Python class 00057 return FreppleClass<CustomerDefault,Customer>::initialize(); 00058 } 00059 00060 00061 DECLARE_EXPORT void Customer::writeElement(XMLOutput* o, const Keyword& tag, mode m) const 00062 { 00063 // Writing a reference 00064 if (m == REFERENCE) 00065 { 00066 o->writeElement(tag, Tags::tag_name, getName()); 00067 return; 00068 } 00069 00070 // Write the complete object 00071 if (m != NOHEADER) o->BeginObject(tag, Tags::tag_name, XMLEscape(getName())); 00072 00073 // Write the fields 00074 HasDescription::writeElement(o, tag); 00075 HasHierarchy<Customer>::writeElement(o, tag); 00076 o->EndObject(tag); 00077 } 00078 00079 00080 DECLARE_EXPORT void Customer::beginElement(XMLInput& pIn, const Attribute& pAttr) 00081 { 00082 HasHierarchy<Customer>::beginElement(pIn, pAttr); 00083 } 00084 00085 00086 DECLARE_EXPORT void Customer::endElement(XMLInput& pIn, const Attribute& pAttr, const DataElement& pElement) 00087 { 00088 HasDescription::endElement(pIn, pAttr, pElement); 00089 HasHierarchy<Customer>::endElement(pIn, pAttr, pElement); 00090 } 00091 00092 00093 DECLARE_EXPORT Customer::~Customer() 00094 { 00095 // Remove all references from demands to this customer 00096 for (Demand::iterator i = Demand::begin(); i != Demand::end(); ++i) 00097 if (i->getCustomer() == this) i->setCustomer(NULL); 00098 } 00099 00100 00101 DECLARE_EXPORT PyObject* Customer::getattro(const Attribute& attr) 00102 { 00103 if (attr.isA(Tags::tag_name)) 00104 return PythonObject(getName()); 00105 if (attr.isA(Tags::tag_description)) 00106 return PythonObject(getDescription()); 00107 if (attr.isA(Tags::tag_category)) 00108 return PythonObject(getCategory()); 00109 if (attr.isA(Tags::tag_subcategory)) 00110 return PythonObject(getSubCategory()); 00111 if (attr.isA(Tags::tag_owner)) 00112 return PythonObject(getOwner()); 00113 if (attr.isA(Tags::tag_hidden)) 00114 return PythonObject(getHidden()); 00115 if (attr.isA(Tags::tag_members)) 00116 return new CustomerIterator(this); 00117 return NULL; 00118 } 00119 00120 00121 DECLARE_EXPORT int Customer::setattro(const Attribute& attr, const PythonObject& field) 00122 { 00123 if (attr.isA(Tags::tag_name)) 00124 setName(field.getString()); 00125 else if (attr.isA(Tags::tag_description)) 00126 setDescription(field.getString()); 00127 else if (attr.isA(Tags::tag_category)) 00128 setCategory(field.getString()); 00129 else if (attr.isA(Tags::tag_subcategory)) 00130 setSubCategory(field.getString()); 00131 else if (attr.isA(Tags::tag_owner)) 00132 { 00133 if (!field.check(Customer::metadata)) 00134 { 00135 PyErr_SetString(PythonDataException, "customer owner must be of type customer"); 00136 return -1; 00137 } 00138 Customer* y = static_cast<Customer*>(static_cast<PyObject*>(field)); 00139 setOwner(y); 00140 } 00141 else if (attr.isA(Tags::tag_hidden)) 00142 setHidden(field.getBool()); 00143 else 00144 return -1; 00145 return 0; 00146 } 00147 00148 00149 } // end namespace