00001
00002
00003
00004
00005
00006
00007
00008 #include "Product.h"
00009 #include "Database.h"
00010
00011 #define SQL_CATALOG_PROXY "SELECT pid FROM catalogue ORDER BY pid, category, name"
00012 #define SQL_PRODUCT_PROXY "SELECT * FROM products WHERE pid = "
00013
00014
00015 Product::Product() : ManagedObject("products")
00016 {
00017 }
00018
00019 Product::Product(Row & aRow) : ManagedObject("products", aRow)
00020 {
00021
00022 }
00023
00024 Product::~Product()
00025 {
00026
00027 }
00028
00029 Product *Product::factory(string aName, int aCid, float aPrice, string aDescr,
00030 int aQty, bool isDel)
00031 {
00032 Product *newProduct = new Product();
00033 newProduct->setIntForKey(KEY_PRD_PID, 0);
00034 newProduct->setIntForKey(KEY_PRD_CID, aCid);
00035 newProduct->setValueForKey(KEY_PRD_NAME, aName);
00036 newProduct->setValueForKey(KEY_PRD_DESCR, aDescr);
00037 newProduct->setFloatForKey(KEY_PRD_PRICE, aPrice);
00038 newProduct->setIntForKey(KEY_PRD_AVAILABILITY, aQty);
00039 newProduct->setBoolForKey(KEY_PRD_DELETED, isDel);
00040
00041 return newProduct;
00042 }
00043
00044 Product * Product::productByID(int aPid)
00045 {
00046 Database &db = Database::Instance();
00047
00048 Connection *conn = db.getConnection();
00049 Query q = conn->query();
00050 q << SQL_PRODUCT_PROXY << aPid;
00051 StoreQueryResult res = q.store();
00052 if (!res.empty()) {
00053 StoreQueryResult::const_iterator it = res.begin();
00054 Row row = *it;
00055
00056 return new Product(row);
00057 }
00058
00059 return NULL;
00060 }
00061
00062 string Product::primaryKey()
00063 {
00064 return KEY_PRD_PID;
00065 }
00066
00067 vector<ProductProxy *> &Product::catalog()
00068 {
00069 Database& db = Database::Instance();
00070 vector<ProductProxy *> *catalog = NULL;
00071
00072 try {
00073 Connection *conn = db.getConnection();
00074 Query q = conn->query(SQL_CATALOG_PROXY);
00075 StoreQueryResult res = q.store();
00076
00077 if (!res.empty()) {
00078 catalog = new vector<ProductProxy *>;
00079 catalog->reserve(res.num_rows());
00080
00081 for (size_t i = 0; i < res.num_rows(); ++i) {
00082 catalog->push_back(new ProductProxy((int) res[i][0]));
00083 }
00084 }
00085 }
00086 catch (std::exception & e) {
00087
00088 }
00089
00090 return *catalog;
00091 }
00092
00093 void Product::showCompatibleProducts(int aPid)
00094 {
00095 Database& db = Database::Instance();
00096
00097 Connection *conn = db.getConnection();
00098 Query q = conn->query();
00099 q << "CALL offers_by_product(" << aPid << ")";
00100 StoreQueryResult res = q.store();
00101 cout << "\n\nCONFIGURATION DETAIL\n====================\n";
00102 Database::printResult(res);
00103
00104 cout << "\nConfiguration includes the following products:\n"
00105 "=============================================\n";
00106 for (int i = 1; q.more_results(); ++i) {
00107 res = q.store_next();
00108 Database::printResult(res);
00109 cout << endl;
00110 }
00111 }
00112
00113 auto_ptr<Category> Product::getCategory()
00114 {
00115 return auto_ptr<Category>(Category::categoryByID(intForKey(KEY_PRD_CID)));
00116 }
00117
00118 ostream& operator<<(ostream& aStream, Product & p) {
00119 return aStream << "PRODUCT DETAIL\n" <<
00120 "Category : " << *(p.getCategory()) << endl <<
00121 "Name : " << p.valueForKey(KEY_PRD_NAME) << endl <<
00122 "Description : " << p.valueForKey(KEY_PRD_DESCR) << endl <<
00123 "Price : " << p.valueForKey(KEY_PRD_PRICE) << endl <<
00124 "Availability: " << p.valueForKey(KEY_PRD_AVAILABILITY) << endl;
00125 }
00126
00127
00128
00129
00130 ProductProxy::ProductProxy(int aPid)
00131 {
00132 pid = aPid;
00133 _theProduct = NULL;
00134 }
00135
00136 ProductProxy::~ProductProxy()
00137 {
00138 if (_theProduct)
00139 delete _theProduct;
00140 }
00141
00142 Product *ProductProxy::getProduct()
00143 {
00144 if (!_theProduct) {
00145 Database &db = Database::Instance();
00146 Query q = db.getConnection()->query();
00147 q << SQL_PRODUCT_PROXY << pid;
00148 StoreQueryResult res = q.store();
00149
00150
00151 assert(!res.empty());
00152
00153 StoreQueryResult::const_iterator it = res.begin();
00154 Row row = *it;
00155
00156 _theProduct = new Product(row);
00157 }
00158
00159 return _theProduct;
00160 }
00161
00162 float ProductProxy::getPrice()
00163 {
00164 return getProduct()->floatForKey(KEY_PRD_PRICE);
00165 }
00166
00167 string ProductProxy::getName()
00168 {
00169 return getProduct()->valueForKey(KEY_PRD_NAME);
00170 }
00171
00172 string ProductProxy::getDescr()
00173 {
00174 return getProduct()->valueForKey(KEY_PRD_DESCR);
00175 }
00176
00177 ostream& operator<<(ostream& aStream, ProductProxy & pp) {
00178 Product *p = pp.getProduct();
00179 return aStream << *p << endl;
00180 }