#if defined(WIN32) || defined (_WINDOWS) #pragma warning(disable: 4786) #endif #include #include "osoa/sca/sca.h" #include "uBikeService.h" #include "uBikeServiceImpl.h" #define DBSTRLEN 50 /* DATA DEFINITIONS */ typedef struct itemdesc { char name[DBSTRLEN]; float baseprice; char SKU[DBSTRLEN]; char type[DBSTRLEN]; } ITEMDESC; #define NUM_ITEMS 13 ITEMDESC items[] = { { "Carver 800", 800.00f, "C800", "R" }, { "Flyer 1200", 1200.00f, "F1200", "R" }, { "Infinity 1600", 1750.00f, "I1600", "R" }, { "Americana 2500", 2000.00f, "A2500", "R" }, { "Cutter 5200", 3000.00f, "C5200", "R" }, { "Led Sled 900", 600.00f, "LS900", "M" }, { "Steeler 1100", 900.00f, "S1100", "M" }, { "RockJumper 4500", 1400.00f, "RJ4500", "M" }, { "M4 Sport 6000", 1800.00f, "M4S6000", "M" }, { "StumpLeaper Pro 8800", 2400.00f, "SLP8800", "M" }, { "Pro Level Components", 1200.00f, "P-COMP", "C" }, { "Sport Level Components", 800.00f, "S-COMP", "C" }, { "Economy Level Components", 400.00f, "E-COMP", "C" }, }; #define BIKEDBSIZE 100 char colors[][20] = { { "YELLOW" }, { "ORANGE" }, { "RED" }, { "GREEN" }, { "BLUE" }, { "WHITE" }, { "BLACK" }, { "SILVER" }, { "NONE" } }; typedef struct datestruct { char month[2]; char sep1; char day[2]; char sep2; char year[4]; } ORDERDATE; static struct tm gs_today; using namespace std; using namespace commonj::sdo; using namespace osoa::sca; /** * uBikeServiceImpl component implementation */ namespace services { namespace uBike { /** * DataObject holding the in-memory database */ DataObjectPtr database; /** * GetRandomNumber * * Takes a low number and a high number then tries to pick a random * number within that range. Rand() is not an exact science, and * therefore requires a bit of work to manage a proper number. **/ int uBikeServiceImpl::GetRandomNumber(int low, int high) { int number = 0; int modifier = 0; /* Do our best to make a random number that is within our range */ number = (int) ( low + rand() * (high / (double)(RAND_MAX)) ); /* Modify number to get it within our range */ if (number < low) modifier = (high-low); else if (number > high) modifier = -(high - low); else return (number); while ((number < low) || (number > high)) { number += modifier; } return number; } /** * Constructor: populate in-memory database */ uBikeServiceImpl::uBikeServiceImpl() { int i = 0; int rc = 0; int bikeindex = 0; int color = 0; int size = 0; int ordereddaysago = 0; int orderday = 0; int ordermonth = 0; int orderyear = 0; float price = 0.0f; char instock[DBSTRLEN]; char serialno[DBSTRLEN]; char orderdate[DBSTRLEN]; int daysinmonth[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; /* We don't play the leapyear game */ DataFactoryPtr factory = DataFactory::getDataFactory(); XSDHelperPtr xsh = HelperProvider::getXSDHelper(factory); xsh->defineFile("uBike.xsd"); database = factory->create("tuxedo", "BikeInventory"); DataObjectList& dl = database->getList("BIKES"); DataObjectPtr bike; /* Loop 100 times to create 100 bike inventory */ for (i = 0; i < BIKEDBSIZE; i++) { /* Calculate Serial Number */ sprintf(serialno, "14789%7.7d", i + 1); /* Pick a bikeindex number from 0-9, determines which bike, SKU, desc, type, and base price */ bikeindex = GetRandomNumber(0, 9); if (items[bikeindex].type[0] == 'R') { /* Road Bike Size */ size = GetRandomNumber(46, 60); /* Make sure size is even */ size = ((size %2 == 0) ? size : ++size); } else { /* MTB Size */ size = GetRandomNumber(14, 21); } /* Pick a random color 0-7 */ color = GetRandomNumber(0, 7); price = items[bikeindex].baseprice; /* * Just pick a number from 1-28 then subtract it from the current date. * Account for 28 days only to make life easier. * * Dates within 7 days are not instock: INSTOCK='N' * Older dates are in stock: INSTOCK='Y' * * Everything needs to be current from today's date or * The exercise/legacy app has legacy data too. It has * to seem like a current app for everybody whether they * run this in our TCTT tour, or if they download it * in three months. */ if (i == 0) ordereddaysago = 8; /* Fix the date for it to be INSTOCK for search.ud32 */ else ordereddaysago = GetRandomNumber(0, 28); if (ordereddaysago >= 7) strcpy(instock, "Y"); else strcpy(instock, "N"); /* Set day based on number of days ago it was ordered */ orderday = gs_today.tm_mday - ordereddaysago; /* * Make sure we stay within the calendar and * adjust the month properly if we go back too far */ if (orderday < 1) { orderday += 28; /* This should make it positive, and within the proper date criteria */ ordermonth = gs_today.tm_mon; /* Since the month is 0 based we don't subtract cuz we r 1 based */ /* * Now make sure that we didn't go back a year * and adjust accordingly. This is important * especially since this lab will be done around * Jan/Feb... */ if (ordermonth < 1) { ordermonth = 12; orderyear = 2000 + gs_today.tm_year - 1; } else { orderyear = 2000 + gs_today.tm_year; } } else { ordermonth = gs_today.tm_mon + 1; /* Since 0 based, we add one for our actual month # */ orderyear = 2000 + gs_today.tm_year; } /* FINALLY! Set the date within a string */ sprintf(orderdate, "%2.2d/%2.2d/%4.4d", ordermonth, orderday, orderyear); bike = factory->create("tuxedo", "Bike"); bike->setCString("SERIALNO", serialno); bike->setCString("SKU", items[bikeindex].SKU); bike->setCString("NAME", items[bikeindex].name); bike->setCString("TYPE", items[bikeindex].type); bike->setCString("COLOR", colors[color]); bike->setInteger("SIZE", size); bike->setFloat("PRICE", price); bike->setCString("INSTOCK", instock); bike->setCString("ORDERDATE", orderdate); dl.append(bike); } } DataObjectPtr uBikeServiceImpl::searchBike(const string COLOR) { ComponentContext theContext = ComponentContext::getCurrent(); DataFactoryPtr factory = theContext.getDataFactory(); DataObjectPtr inventory = factory->create("tuxedo", "BikeInventory"); DataObjectList& bikesList = inventory->getList("BIKES"); DataObjectPtr bike; DataObjectList& dl = database->getList("BIKES"); for (unsigned int i = 0; i < dl.size(); i++) { string color = dl[i]->getCString("COLOR"); if (color.compare(COLOR) == 0) { bike = factory->create("tuxedo", "Bike"); bike->setCString("SERIALNO", dl[i]->getCString("SERIALNO")); bike->setCString("SKU", dl[i]->getCString("SKU")); bike->setCString("NAME", dl[i]->getCString("NAME")); bike->setCString("TYPE", dl[i]->getCString("TYPE")); bike->setCString("COLOR", dl[i]->getCString("COLOR")); bike->setInteger("SIZE", dl[i]->getInteger("SIZE")); bike->setFloat("PRICE", dl[i]->getFloat("PRICE")); bike->setCString("INSTOCK", dl[i]->getCString("INSTOCK")); bike->setCString("ORDERDATE", dl[i]->getCString("ORDERDATE")); bikesList.append(bike); } } if (inventory->getList("BIKES").size() == 0) inventory->setCString("STATUS", "No inventory matching search items found"); return inventory; } } // End uBike } // End services