Author: shankar Date: Fri Feb 6 04:06:18 2009 New Revision: 741394 URL: http://svn.apache.org/viewvc?rev=741394&view=rev Log: request processor
Added: incubator/stonehenge/trunk/stocktrader/php/trader_client/request_processor.php Added: incubator/stonehenge/trunk/stocktrader/php/trader_client/request_processor.php URL: http://svn.apache.org/viewvc/incubator/stonehenge/trunk/stocktrader/php/trader_client/request_processor.php?rev=741394&view=auto ============================================================================== --- incubator/stonehenge/trunk/stocktrader/php/trader_client/request_processor.php (added) +++ incubator/stonehenge/trunk/stocktrader/php/trader_client/request_processor.php Fri Feb 6 04:06:18 2009 @@ -0,0 +1,1001 @@ +<?php +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +define ("STATUS_SUCCESS", 1); +define ("STATUS_FAILURE", 0); +define ("TRUE", 1); +define ("FALSE", 0); +define ("ORDER_TYPE_BUY", "buy"); +define ("ORDER_TYPE_SELL", "sell"); +define ("COOKIE_USERNAME", "username"); +define ("COOKIE_ENDPOINT", "endpoint"); +define ("DEFAULT_ENDPOINT", "http://localhost:80/TradeServiceWcf/TradeServiceWcf.svc"); +define ("CLIENT_NAME", "PHP_CLIENT"); + +/*this will set the default end point if end point is NOT already set.*/ +SetDefaultEndpoint(); + +/** + * This class includes the summery of acctivities of the user + */ + +class userAccountSummary +{ + public $totalBuys; //double + public $totalSells; //double + public $totalTax; //double + public $totalImpact; //double +} + +/** + * This class encapsulates login data of a user + */ + +class login +{ + public $userID; // string + public $password; // string +} + +/** + * This class describes the response of login, if success gives + * the account details of the user + */ + +class loginResponse +{ + public $loginReturn; // AccountDataBean +} + +/** + * This class encapsulates user account details + */ + +class AccountDataBean +{ + public $accountID; // int + public $loginCount; // int + public $logoutCount; // int + public $lastLogin; // dateTime + public $creationDate; // dateTime + public $balance; // decimal + public $openBalance; // decimal + public $profileID; // string +} + +/** + * This class will contain details needed to get the orders of a user + */ + +class getOrders +{ + public $userID; // string +} + +/** + * This class encapsulates the response of a getOrders request + */ + +class getOrdersResponse +{ + public $getOrdersReturn; // ArrayOfOrderDataBean +} + +/** + * This class encapsulates the order details of a user + */ + +class ArrayOfOrderDataBean +{ + public $OrderDataBean; // array[0, unbounded] of OrderDataBean +} + +/** + * This class encapsulates the details of an order + */ + +class OrderDataBean +{ + public $orderID; // int + public $orderType; // string + public $orderStatus; // string + public $openDate; // dateTime + public $completionDate; // dateTime + public $quantity; // double + public $price; // decimal + public $orderFee; // decimal + public $symbol; // string +} + +/** + * This class encapsulates the details needed to get account data of a + * particular user + */ + +class getAccountData +{ + public $userID; // string +} + +/** + * This class encapsulates the response of getAccountData request + */ + +class getAccountDataResponse +{ + public $getAccountDataReturn; // AccountDataBean +} + +/** + * This class encapsulates details needed to get account profile information + */ + +class getAccountProfileData +{ + public $userID; // string +} + +/** + * This class encapsulates the response of getAccountProfileData request + */ + +class getAccountProfileDataResponse +{ + public $getAccountProfileDataReturn; // AccountProfileDataBean +} + +/** + * Encapsulates details of an account profile + */ + +class AccountProfileDataBean +{ + public $userID; // string + public $password; // string + public $fullName; // string + public $address; // string + public $email; // string + public $creditCard; // string +} + +/** + * Contain information needed to update account profile + */ + +class updateAccountProfile +{ + public $profileData; // AccountProfileDataBean +} + +/** + * Contains response of updated account profile + */ + +class updateAccountProfileResponse { + public $updateAccountProfileReturn; // AccountProfileDataBean +} + +/** + * Information needed to logout a user + */ + +class logout +{ + public $userID; // string +} + +/** + * Response of logout + */ + +class logoutResponse +{ +} + +/** + * Details needed to do "buy" transaction + */ + +class buy +{ + public $userID; // string + public $symbol; // string + public $quantity; // double + public $orderProcessingMode; // int +} + +/** + * Response of "buy" transaction + */ + +class buyResponse +{ + public $buyReturn; // OrderDataBean +} + +/** + * Details needed to do "sell" transaction + */ + +class sell +{ + public $userID; // string + public $holdingID; // int + public $orderProcessingMode; // int +} + +/** + * Response of "sell" transaction + */ + +class sellResponse +{ + public $sellReturn; // OrderDataBean +} + +/** + * Details needed to get holdings of a user + */ + +class getHoldings +{ + public $userID; // string +} + +/** + * Response of getHolding request + */ + +class getHoldingsResponse +{ + public $getHoldingsReturn; // ArrayOfHoldingDataBean +} + +/** + * Details of holdings of a user + */ + +class ArrayOfHoldingDataBean +{ + public $HoldingDataBean; // array[0, unbounded] of HoldingDataBean +} + +/** + * Details of a holding + */ + +class HoldingDataBean +{ + public $holdingID; // int + public $quantity; // double + public $purchasePrice; // decimal + public $purchaseDate; // dateTime + public $quoteID; // string +} + +/** + * Information needed to register a user to the system + */ + +class register +{ + public $userID; // string + public $password; // string + public $fullname; // string + public $address; // string + public $email; // string + public $creditcard; // string + public $openBalance; // decimal +} + +/** + * Account details of registered user + */ + +class registerResponse +{ + public $registerReturn; // AccountDataBean +} + +/** + * Details needed to get closed orders of a user + */ + +class getClosedOrders +{ + public $userID; // string +} + +/** + * Response of getClosedOrder request + */ + +class getClosedOrdersResponse +{ + public $getClosedOrdersReturn; // ArrayOfOrderDataBean +} + +/** + * Details needed to get market summery + */ + +class getMarketSummary +{ +} + +/** + * Response of getMarketSummary request + */ + +class getMarketSummaryResponse +{ + public $getMarketSummaryReturn; // MarketSummaryDataBeanWS +} + +/** + * Details about market summery + */ + +class MarketSummaryDataBeanWS +{ + public $TSIA; // decimal + public $openTSIA; // decimal + public $volume; // double + public $topGainers; // ArrayOfQuoteDataBean + public $topLosers; // ArrayOfQuoteDataBean + public $summaryDate; // dateTime +} + +/** + * Details of Quote request + */ + +class ArrayOfQuoteDataBean +{ + public $QuoteDataBean; // array[0, unbounded] of QuoteDataBean +} + +/** + * Details of a quote + */ + +class QuoteDataBean +{ + public $symbol; // string + public $companyName; // string + public $price; // decimal + public $open; // decimal + public $low; // decimal + public $high; // decimal + public $change; // double + public $volume; // double +} + +/** + * Details needed to get quote information of a symbol + */ + +class getQuote +{ + public $symbol; // string +} + +/** + * Response of getQuote request + */ + +class getQuoteResponse +{ + public $getQuoteReturn; // QuoteDataBean +} + +/** + * Details needed to get a particular holding of a user + */ + +class getHolding +{ + public $userID; // string + public $holdingID; // int +} + +/** + * Response of getHolding request + */ + +class getHoldingResponse +{ + public $getHoldingReturn; // HoldingDataBean +} + +/** + * Details needed to get top orders of a user + */ + +class getTopOrders +{ + public $userID; // string +} + +/** + * Response of getTopOrders request + */ + +class getTopOrdersResponse +{ + public $getTopOrdersReturn; // ArrayOfOrderDataBean +} + +/** + * Details needed to sell a holding of a user + */ + +class sellEnhanced +{ + public $userID; // string + public $holdingID; // int + public $quantity; // double +} + +/** + * Response of sellEnhanced request + */ + +class sellEnhancedResponse +{ + public $sellEnhancedReturn; // OrderDataBean +} + +/** + * Details needed to check whether service is online + */ + +class isOnline +{ +} + +/** + * Response of service's online status + */ + +class isOnlineResponse +{ +} + +/** + * Summery of holdings of a particular user + */ + +class holdingInformation +{ + public $totalHoldings; + public $noOfHoldings; +} + +/* + * Request send to get configurations parameter needed by client + */ +class ClientConfigRequest +{ + /** + * @var string + */ + public $Client; +} + +/* + * contains information needed by client + */ +class ClientConfigResponse +{ + /** + * @var anyURI + */ + public $BS; +} + +/* + * Get the endpoint of business service + */ +function GetBSEndPoint() +{ + + // define the class map + $class_map = array( + "ClientConfigRequest" => "ClientConfigRequest", + "ClientConfigResponse" => "ClientConfigResponse"); + + // create client in WSDL mode + $client = new WSClient(array ("wsdl" =>"wsdl/config_svc.wsdl", + "classmap" => $class_map, + "to" => GetEndpoint())); + + // get proxy object reference form client + $proxy = $client->getProxy(); + $input = new ClientConfigRequest(); + $input->Client = CLIENT_NAME; + + $response = $proxy->ClientConfigRequest($input); + if($response) + { + $endPoint = $response->BS; + } + + return $endPoint; +} + +/** + * This method registes a new user in the system + * @param userID id of the user + * @param password password of the user + * @param fullname full name of the user + * @param address address of the user + * @param email email address of the user + * @param creditcard credit card number of the user + * @param openBalance initial balance of the user + * @return account details of the registerd user on success. NULL otherwise. + */ + +function RegisterUser($userID, $password, $fullname, + $address, $email, $creditcard, $openBalance) +{ + $proxy = GetProxy(); + $input = new register(); + $input->userID = $userID; + $input->password = $password; + $input->fullname = $fullname; + $input->address = $address; + $input->email = $email; + $input->creditcard = $creditcard; + $input->openBalance = $openBalance; + + $response = $proxy->register($input); + return $response; +} + +/** + * Updates account profile information + * @param userID id of the user + * @param fullname full name of the user + * @param email email address of the user + * @param address address of the user + * @param creditcard credit card number of the user + * @param password password of the user + * @return account details of the modified user on success. NULL otherwise. + */ + +function UpdateAccountProfile($userID, $fullName, $email, + $address, $creditCard, $password) +{ + $proxy = GetProxy(); + $input = new updateAccountProfile(); + $input->profileData = new AccountProfileDataBean(); + $input->profileData->userID = $userID; + $input->profileData->password = $password; + $input->profileData->fullName = $fullName; + $input->profileData->address = $address; + $input->profileData->email = $email; + $input->profileData->creditCard = $creditCard; + $response = $proxy->updateAccountProfile($input); + return $response; +} + +/** + * Given the order details of a user, this method calculates summery of + * activities of the user. This includes total money spent on buying stocks + * total money earned from selling them etc. + * @param ordersReturn collection of orders of a user + * @return summery of user's activities + */ + +function GetUserAccountSummary($ordersReturn) +{ + $index = 0; + while($ordersReturn->OrderDataBean[$index]) + { + if ($ordersReturn->OrderDataBean[$index]->orderType == ORDER_TYPE_BUY) + { + $buys = $buys + (($ordersReturn->OrderDataBean[$index]->price) * + ($ordersReturn->OrderDataBean[$index]->quantity)) + + ($ordersReturn->OrderDataBean[$index]->orderFee); + } + else if ($ordersReturn->OrderDataBean[$index]->orderType == ORDER_TYPE_SELL) + { + $sells = $sells + (($ordersReturn->OrderDataBean[$index]->price) * + ($ordersReturn->OrderDataBean[$index]->quantity)) - + ($ordersReturn->OrderDataBean[$index]->orderFee); + } + $tax = $tax + $ordersReturn->OrderDataBean[$index]->orderFee; + $index ++; + } + $accountSummary = new userAccountSummary(); + $accountSummary->totalBuys = $buys; + $accountSummary->totalSells = $sells; + $accountSummary->totalTax = $tax; + $accountSummary->totalImpact = $buys + $tax - $sells; + return $accountSummary; +} + +/** + * Given the holdings of a user, this methods calculate total market value of + * holding and number of holdings + * @param holdings collection of holdings of a user + * @return summery of holding details + */ + +function GetHoldingInformation($holdings) +{ + $holdingsReturn = $holdings->getHoldingsReturn; + $index = 0; + $totalHoldings= 0; + $marketValue = 0; + + while($holdingsReturn->HoldingDataBean[$index]) + { + $bean = $holdingsReturn->HoldingDataBean[$index]; + if (!$quoteInfo[$bean->quoteID]) + { + $quotes = GetQuote($bean->quoteID); + if ($quotes) + $quotesReturn = $quotes->getQuoteReturn; + $quoteInfo[$bean->quoteID] = $quotesReturn->price; + } + $marketValue = $marketValue + ($quoteInfo[$bean->quoteID]) * ($bean->quantity); + + $totalHoldings = $totalHoldings + $holdingsReturn->HoldingDataBean[$index]->quantity * + $holdingsReturn->HoldingDataBean[$index]->purchasePrice; + $index ++; + } + $holdingInfo = new holdingInformation(); + $holdingInfo->totalHoldings = $marketValue; + $holdingInfo->noOfHoldings = $index; + + return $holdingInfo; +} + +/** + * Writes user id to cookie + * @param username user id of the current user + */ + +function WriteCookie($username) +{ + setcookie(COOKIE_USERNAME, $username, time()+3600); +} + +/** + * Deletes user id from cookie + * @param username user id of current user + */ +function DeleteCookie($username) +{ + setcookie(COOKIE_USERNAME, $username, time()-3600); + if (isset($_COOKIE[COOKIE_USERNAME])) + unset($_COOKIE[COOKIE_USERNAME]); +} + +/** + * When user logout, user id will be deleted from the cookie + * @param username user id of current user + */ + +function LogoutUser($username) +{ + $proxy = GetProxy(); + $input = new logout(); + $input->userID = $username; + $response = $proxy->logout($input); + DeleteCookie($username); +} + +/** + * Gets user id from cookie + */ + +function GetUserFromCookie() +{ + return ($_COOKIE[COOKIE_USERNAME]); +} + +/** + * Checks whether user is logged in. If so, user id cookie would have + * been already set. Checking whether cookie is set equals to check + * whether user is logged in + */ + +function IsLoggedIn() +{ + return isset($_COOKIE[COOKIE_USERNAME]); +} + +/** + * Gets user id of current user + */ + +function GetUser() +{ + return ($_COOKIE[COOKIE_USERNAME]); +} + +/** + * Store business service's endpoint in cookie + * @param endPoint end point address of the business service + */ + +function WriteEndpoint($endPoint) +{ + setcookie(COOKIE_ENDPOINT, $endPoint, time()+3600); +} + +/** + * Sets default end point + */ + +function SetDefaultEndpoint() +{ + if(GetEndpoint() == "") + WriteEndpoint(DEFAULT_ENDPOINT); +} + +/** + * Gets stored end point address of business service + */ + +function GetEndpoint() +{ + return ($_COOKIE[COOKIE_ENDPOINT]); +} + +/** + * Gets proxy object to make communication with business service + */ + +function GetProxy() +{ + /*define the class map */ + + $class_map = array( + "anyType" => "anyType", "login" => "login", + "loginResponse" => "loginResponse", "AccountDataBean" => + "AccountDataBean", "getOrders" => "getOrders", "getOrdersResponse" => + "getOrdersResponse", "ArrayOfOrderDataBean" => "ArrayOfOrderDataBean", + "OrderDataBean" => "OrderDataBean", "getAccountData" => "getAccountData", + "getAccountDataResponse" => "getAccountDataResponse", + "getAccountProfileData" => "getAccountProfileData", + "getAccountProfileDataResponse" => "getAccountProfileDataResponse", + "AccountProfileDataBean" => "AccountProfileDataBean", + "updateAccountProfile" => "updateAccountProfile", + "updateAccountProfileResponse" => "updateAccountProfileResponse", + "logout" => "logout", "logoutResponse" => "logoutResponse", "buy" + => "buy", "buyResponse" => "buyResponse", "sell" => "sell", + "sellResponse" => "sellResponse", "getHoldings" => "getHoldings", + "getHoldingsResponse" => "getHoldingsResponse", "ArrayOfHoldingDataBean" + => "ArrayOfHoldingDataBean", "HoldingDataBean" => "HoldingDataBean", + "register" => "register", "registerResponse" => "registerResponse", + "getClosedOrders" => "getClosedOrders", "getClosedOrdersResponse" => + "getClosedOrdersResponse", "getMarketSummary" => "getMarketSummary", + "getMarketSummaryResponse" => "getMarketSummaryResponse", + "MarketSummaryDataBeanWS" => "MarketSummaryDataBeanWS", + "ArrayOfQuoteDataBean" => "ArrayOfQuoteDataBean", "QuoteDataBean" => + "QuoteDataBean", "getQuote" => "getQuote", "getQuoteResponse" => + "getQuoteResponse", "getHolding" => "getHolding", "getHoldingResponse" + => "getHoldingResponse", "getTopOrders" => "getTopOrders", + "getTopOrdersResponse" => "getTopOrdersResponse", "sellEnhanced" => + "sellEnhanced", "sellEnhancedResponse" => "sellEnhancedResponse", + "isOnline" => "isOnline", "isOnlineResponse" => "isOnlineResponse"); + + $client = new WSClient(array ("wsdl" =>"wsdl/TradeServiceWcf.svc.wsdl", + "classmap" => $class_map, + "to" => GetBSEndPoint())); + //We can set this port through tcpmon + + $proxy = $client->getProxy(); + return $proxy; +} + +/** + * Sends login request to verify whether current user is authorized + * @param userid user id of current user + * @param password password given by current user + * @return profile id of the user if login is success. NULL otherwise + */ + +function Login($userid, $password) +{ + $proxy = GetProxy(); + $input = new login(); + $input->userID = $userid; + $input->password = $password; + $response = $proxy->login($input); + + return $response->loginReturn->profileID; +} + +/** + * Gets orders of current user + * @param userid user id of current user + * @return collection of orders of the user + */ + +function GetOrders($userid) +{ + $proxy = GetProxy(); + $input = new getOrders(); + $input->userID = $userid; + $response = $proxy->getOrders($input); + return $response; +} + +/** + * Gets market summary + * @return market summery + */ + +function GetMarketSummary() +{ + $proxy = GetProxy(); + $input = new getMarketSummary(); + $response = $proxy->getMarketSummary($input); + return $response; +} + +/** + * Gets account details of current user + * @param userid user id of current user + * @return account details if success. NULL otherwise + */ + +function GetAccountData($userid) +{ + $proxy = GetProxy(); + $input = new getAccountData(); + $input->userID = $userid; + $response = $proxy->getAccountData($input); + return $response; +} + +/** + * Gets account profile information of current user + * @param userid user id of current user + * @return account profile data of current user if success. NULL otherwise + */ + +function GetAccountProfileData($userid) +{ + $proxy = GetProxy(); + $input = new getAccountProfileData(); + $input->userID = $userid; + $response = $proxy->getAccountProfileData($input); + return $response; +} + +/** + * Gets holding details of given user + * @param userid user id of current user + * returns collection of holding if success. NULL otherwise + */ + +function GetHoldings($userid) +{ + $proxy = GetProxy(); + $input = new getHoldings(); + $input->userID = $userid; + $response = $proxy->getHoldings($input); + return $response; +} + +/** + * Gets quote of given symbol + * @param symbol id of the stock + * @return details of the symbol when success. NULL otherwise + */ + +function GetQuote($symbol) +{ + $proxy = GetProxy(); + $input = new getQuote(); + $input->symbol = $symbol; + $response = $proxy->getQuote($input); + return $response; +} + + +/** + * Sells given holding or part of it of given user + * @param userID user id of current user + * @param holdingID holding id of the holding + * @param quantity number of stocks to be sold + * @return details of sell order if success. NULL otherwise + */ + +function SellEnhanced($userID, $holdingID, $quantity) +{ + $proxy = GetProxy(); + $input = new sellEnhanced(); + $input->userID = $userID; + $input->holdingID = $holdingID; + $input->quantity = $quantity; + $response = $proxy->sellEnhanced($input); + return $response; +} + +/** + * Buys number of stocks of given symbol + * @param userID user id of current user + * @param symbol symbol of needed stock + * @quantity number of stocks needed + * @mode mode of buying + * @return details of buy order if success. NULL otherwise + */ + +function Buy($userID, $symbol, $quantity, $mode) +{ + $proxy = GetProxy(); + $input = new buy(); + $input->symbol = $symbol; + $input->userID = $userID; + $input->quantity = $quantity; + $input->orderProcessingMode = $mode; + $response = $proxy->buy($input); + return $response; +} + +/** + * Gets closed orders of current user + * @return collection of orders whose status is closed + */ + +function GetClosedOrders() +{ + $proxy = GetProxy(); + $input = new getClosedOrders(); + $input->userID = GetUserFromCookie(); + if($input->userID) + { + $response = $proxy->getClosedOrders($input); + $getClosedOrdersReturn = $response->getClosedOrdersReturn; + } + return $getClosedOrdersReturn; +} + +/** + * Gets closed orders of current user if there are any. Then prints it + */ + +function checkForClosedOrders() +{ + $proxy = GetProxy(); + $input = new getClosedOrders(); + $input->userID = GetUserFromCookie(); + if($input->userID) + { + $response = $proxy->getClosedOrders($input); + $getClosedOrdersReturn = $response->getClosedOrdersReturn; + + $index = 0; + while ($getClosedOrdersReturn->OrderDataBean[$index]) + { + print("THIS IS THE ID OF THE JUST CLOSED ORDER:"); + print($getClosedOrdersReturn->OrderDataBean[$index]->orderID); + print("\n"); + $index ++; + } + } +} + +?>