#!/usr/bin/perl # 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. use CGI qw/:standard/; use WSO2::WSF; use XML::LibXML; my $q = new CGI; my $WSFC_HOME='/opt/wso2/wsf_c/'; # get the endpoitn from the configuration service sub get_endpoint { $config_endpoint = $q->cookie('endpoint'); $input_xml = < PERL_CLIENT XML my $client = new WSO2::WSF::WSClient( { 'to' => $config_endpoint, 'log_file' => '/tmp/stocktrader.log', 'log_level' => 4, 'wsfc_home' => $WSFC_HOME } ); my $response_msg = $client->request( { 'payload' => $input_xml } ); my $response_str = $response_msg->{str}; my $response_parser = XML::LibXML->new(); $response_xml = $response_parser->parse_string($response_str); my $xpc = XML::LibXML::XPathContext->new($response_xml); $xpc->registerNs('ns'=> 'http://wso2.org/interop/stocktrader/xsd'); $bs = $xpc->findvalue('//ns:BS'); return $bs; } sub convert_date { my($str) = shift; if($str) { $str =~ s/[T:\.]/-/g; @date_arr = split("-", $str); $year = @date_arr[0]; $mon = @date_arr[1]; $date = @date_arr[2]; $hour = @date_arr[3]; $min = @date_arr[4]; $sec = @date_arr[5]; if($hour > 12) { $hour = $hour - 12; $ampm = "PM"; } else { $ampm = "AM"; } return "${mon}/${date}/${year} ${hour}:${min}:${sec} ${ampm}"; } return ""; } # Gets closed orders of current user # @return collection of orders whose status is closed sub get_closed_orders { my($username) = shift; my $get_closed_orders_request = < ${username} XML my $endpoint = get_endpoint(); my $client = new WSO2::WSF::WSClient( { 'to' => $endpoint, 'log_file' => '/tmp/stocktrader.log', 'log_level' => 4, 'wsfc_home' => $WSFC_HOME } ); $response = $client->request( { 'payload' => $get_closed_orders_request } ); my $get_closed_orders_response = $response->{str}; # my $get_closed_orders_response = < # # # 100000023 # sell # completed # 2008-07-22T10:02:42.543 # 2008-07-22T10:02:42.607 # 1 # 96.78 # 15.95 # s:0 # # # #XML my $closed_orders_parser = XML::LibXML->new(); $closed_orders_xml = $closed_orders_parser->parse_string($get_closed_orders_response); return $closed_orders_xml; } sub get_quote { my($quote_id) = shift; my $get_quotes_request = < ${quote_id} XML $endpoint = get_endpoint(); my $client = new WSO2::WSF::WSClient( { 'to' => $endpoint, 'log_file' => '/tmp/stocktrader.log', 'log_level' => 4, 'wsfc_home' => $WSFC_HOME } ); $response = $client->request( { 'payload' => $get_quotes_request } ); my $get_quotes_response = $response->{str}; # my $get_quotes_response = < # # s:0 # S0 Incorporated # 100.00 # 100.00 # 100.00 # 100.00 # 0 # 0 # # #XML # my $parser = XML::LibXML->new(); $quote_xml = $parser->parse_string($get_quotes_response); return $quote_xml; } sub buy { $userID = shift; $symbol = shift; $quantity = shift; $orderProcessingMode = shift; my $buy_request = < ${userID} ${symbol} ${quantity} ${orderProcessingMode} XML my $endpoint = get_endpoint(); my $client = new WSO2::WSF::WSClient( { 'to' => $endpoint, 'log_file' => '/tmp/stocktrader.log', 'log_level' => 4, 'wsfc_home' => $WSFC_HOME } ); $response = $client->request( { 'payload' => $buy_request } ); my $buy_response = $response->{str}; # my $buy_response = < # # 42 # buy # completed # 2008-05-21T16:01:44.157 # 2008-05-21T16:01:44.157 # 200 # 100.00 # 24.50 # s:8 # # #XML my $parser = XML::LibXML->new(); $buy_xml = $parser->parse_string($buy_response); return $buy_xml; } sub sell_enhanced { $userID = shift; $holdingID = shift; $quantity = shift; my $sell_request = < ${userID} ${holdingID} ${quantity} XML my $endpoint = get_endpoint(); my $client = new WSO2::WSF::WSClient( { 'to' => $endpoint, 'log_file' => '/tmp/stocktrader.log', 'log_level' => 4, 'wsfc_home' => $WSFC_HOME } ); $response = $client->request( { 'payload' => $sell_request } ); $sell_response = $response->{str}; # my $sell_response = < # # 42 # sell # completed # 2008-05-21T16:01:44.157 # 2008-05-21T16:01:44.157 # 200 # 100.00 # 24.50 # s:8 # # #XML my $parser = XML::LibXML->new(); $sell_xml = $parser->parse_string($sell_response); return $sell_xml; } my $symbol; my $is_buy; my $is_sell; my $is_reply = 0; my $buy_return_symbol; my $quotes_initial_page = 0; my $brx; # buyReturn my $sell_enhanced; my $xpc_buy; my $xpc_sell; my $xpc_quotes; if ( defined $q->param('GETQUOTE') ) { $symbol = $q->param('SYMBOLS'); $quotes = get_quote($symbol); my $xpc_quotes = XML::LibXML::XPathContext->new($quotes); $xpc_quotes->registerNs('ns'=> 'http://trade.samples.websphere.ibm.com'); } elsif ( defined( $q->param('BUY') ) || defined( $q->param('SELL') ) ) { my $quantity = $q->param('QUANTITY'); # my $userid = get_from_cookie $userid = $q->cookie("username"); $is_buy = 0; $is_sell = 0; if ( defined $q->param('BUY') ) { my $mode = 0; $is_buy = 1; $symbol = $q->param('SYMBOL'); # find out what kind of xml is here and then if the order id is defined # $buy_return = that xml twig $buy_response = buy($userid, $symbol, $quantity, $mode); $xpc_buy = XML::LibXML::XPathContext->new($buy_response); $xpc_buy->registerNs('ns'=> 'http://trade.samples.websphere.ibm.com'); $order_id = $xpc_buy->findvalue('//ns:orderID'); if($order_id) { $is_reply = 1; } } elsif ( defined $q->param('SELL') ) { $is_sell = 1; $holdingid = $q->param('HOLDINGID'); $sell_response = sell_enhanced($userid, $holdingid, $quantity); $xpc_sell = XML::LibXML::XPathContext->new($sell_response); $xpc_sell->registerNs('ns'=> 'http://trade.samples.websphere.ibm.com'); $order_id = $xpc_sell->findvalue('//ns:orderID'); if($order_id) { $is_reply = 1; } } } else { # just wanna visit sire $quotes_initial_page = 1; } my $html_begin = < WSF/PHP StockTrader Welcome
E print $q->header(); print $html_begin; $get_closed_orders_return = get_closed_orders($username); # check whether there's a new status change happened if($get_closed_orders_return) { my $xpc = XML::LibXML::XPathContext->new($get_closed_orders_return); $xpc->registerNs('ns'=> 'http://trade.samples.websphere.ibm.com'); @nodes = $xpc->findnodes('//ns:OrderDataBean'); if(@nodes) { # checking whether a new status change happened in a particular order print("

Trade Alert: The following orders have completed.

"); print(""); foreach my $bean (@nodes) { my $xpc = XML::LibXML::XPathContext->new($bean); $xpc->registerNs('ns'=> 'http://trade.samples.websphere.ibm.com'); $open_date = convert_date($xpc->findvalue('./ns:openDate')); $completion_date = convert_date($xpc->findvalue('./ns:completionDate')); $order_status = $xpc->findvalue('./ns:orderStatus'); $order_id = $xpc->findvalue('./ns:orderID'); $order_fee = $xpc->findvalue('./ns:orderFee'); $order_type = $xpc->findvalue('./ns:orderType'); $symbol = $xpc->findvalue('./ns:symbol'); $quantity = $xpc->findvalue('./ns:quantity'); print (""); } print("
Order IDOrder StatusCreation DateCompletion Date Txn FeeTypeSymbolQuantity
${order_id} ${order_status} ${open_date} ${completion_date} ${order_fee} ${order_type} ${symbol} ${quantity}


"); } } # if isReply if ($is_reply) { my $order_id; if($is_buy) { $order_id = $xpc_buy->findvalue('//ns:orderID'); $quantity = $xpc_buy->findvalue('//ns:quantity'); } elsif($is_sell) { $order_id = $xpc_sell->findvalue('//ns:orderID'); $quantity = $xpc_sell->findvalue('//ns:quantity'); } # Check whether the user has requested to buy or sell some quote.*/ print ("

New Order

"); print ("

Order ${order_id} to ${quantity} shares of s:0 has been submitted for processing.

"); print ("

Order Details:

"); print (""); if ($is_buy) { $date_time = convert_date($xpc_buy->findvalue('//ns:openDate')); $order_status = $xpc_buy->findvalue('//ns:orderStatus'); $order_fee = $xpc_buy->findvalue('//ns:orderFee'); $order_type = $xpc_buy->findvalue('//ns:orderType'); $symbol = $xpc_buy->findvalue('//ns:symbol'); $quantity = $xpc_buy->findvalue('//ns:quantity'); print (""); } elsif ($is_sell) { $date_time = convert_date($xpc_sell->findvalue('//ns:openDate')); $order_status = $xpc_sell->findvalue('//ns:orderStatus'); $order_fee = $xpc_sell->findvalue('//ns:orderFee'); $order_type = $xpc_sell->findvalue('//ns:orderType'); $symbol = $xpc_sell->findvalue('//ns:symbol'); $quantity = $xpc_sell->findvalue('//ns:quantity'); print (""); } print ("
Order IDOrder StatusCreation Date Completion DateTxn FeeTypeSymbol Quantity
${order_id}${order_status}${date_time} Pending\$${order_fee}${order_type}${symbol} ${quantity}
${order_id}${order_status}${date_time} Pending\$${order_fee}${order_type}${symbol} ${quantity}
"); } elsif ($q->param('GETQUOTE')) { print("

Stock Quotes

"); print(""); print("
"); print(""); $symbol = $xpc_quotes->findvalue($symbol); if ($quotesReturn->symbol) { $company_name = $xpc_quotes->findvalue("//ns:companyName"); $volume = $xpc_quotes->findvalue("//ns:volume"); $low = $xpc_quotes->findvalue("//ns:low"); $high = $xpc_quotes->findvalue("//ns:high"); $open = $xpc_quotes->findvalue("//ns:open"); $price = $xpc_quotes->findvalue("//ns:price"); $change = $xpc_quotes->findvalue("//ns:change"); print (""); } print("
SymbolCompanyVolumePrice Range Open PriceCurrent PriceGain(Loss)Trade
${symbol}${company_name} ${volume} \$${low}-\$${high}\$${open} \$${price}"); if ($change > 0) { print ("\$${change}"); } elsif ($change < 0) { print ("\$${change}"); } else { print ("\$${change}"); } print("
"); } elsif($quotes_initial_page) { print("

Stock Quotes

"); print(""); print("
"); print(""); $symbolCount = 0; while($symbolCount < 5) { $new_symbol = "s:${symbolCount}"; $new_quotes = get_quote($new_symbol); my $xpc = XML::LibXML::XPathContext->new($new_quotes); $xpc->registerNs('ns'=> 'http://trade.samples.websphere.ibm.com'); if ($new_quotes) { $company_name = $xpc->findvalue("//ns:companyName"); $volume = $xpc->findvalue("//ns:volume"); $low = $xpc->findvalue("//ns:low"); $high = $xpc->findvalue("//ns:high"); $open = $xpc->findvalue("//ns:open"); $price = $xpc->findvalue("//ns:price"); $change = $xpc->findvalue("//ns:change"); $symbol = $xpc->findvalue("//ns:symbol"); if ($symbol) { print (""); } } $symbolCount ++; } print("
SymbolCompanyVolumePrice Range Open PriceCurrent PriceGain(Loss)Trade
${symbol}${company_name} ${volume}\$${low}-\$${high} \$${open} \$${price}"); if ($change > 0) { print ("\$${change}"); } elsif ($change < 0) { print ("\$${change}"); } else { print ("\$${change}"); } print("
"); } else { print("

Stock Quotes

"); print(""); print("
"); } my $html_end =<
E print $html_end;