#!/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 $WSFC_HOME='/opt/wso2/wsf_c/'; my $q = new CGI; # 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; } # get market summary sub get_market_summary { my $get_market_summary_request = < 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 } ); my $response = $client->request( { 'payload' => $get_market_summary_request } ); my $get_market_summary_response = $response->{str}; # my $get_market_summary_response = < # # 100.000000 # 100.000000 # 0 # # # s:100 # S100 Incorporated # 100.00 # 100.00 # 100.00 # 100.00 # 0 # 0 # # # s:101 # S101 Incorporated # 100.00 # 100.00 # 100.00 # 100.00 # 0 # 0 # # # s:102 # S102 Incorporated # 100.00 # 100.00 # 100.00 # 100.00 # 0 # 0 # # # s:103 # S103 Incorporated # 100.00 # 100.00 # 100.00 # 100.00 # 0 # 0 # # # s:104 # S104 Incorporated # 100.00 # 100.00 # 100.00 # 100.00 # 0 # 0 # # # # # s:100 # S100 Incorporated # 100.00 # 100.00 # 100.00 # 100.00 # 0 # 0 # # # s:101 # S101 Incorporated # 100.00 # 100.00 # 100.00 # 100.00 # 0 # 0 # # # s:102 # S102 Incorporated # 100.00 # 100.00 # 100.00 # 100.00 # 0 # 0 # # # s:103 # S103 Incorporated # 100.00 # 100.00 # 100.00 # 100.00 # 0 # 0 # # # s:104 # S104 Incorporated # 100.00 # 100.00 # 100.00 # 100.00 # 0 # 0 # # # 2008-07-18T16:06:36.328125+05:30 # # #XML my $parser = XML::LibXML->new(); $market_summary_xml = $parser->parse_string($get_market_summary_response); return $market_summary_xml; } sub get_account_data { my($username) = shift; my $get_account_data_request = < ${username} 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_account_data_request} ); $get_account_data_response = $response->{str}; # my $get_account_data_response = < # # 1 # 5 # 0 # 2008-07-18T16:06:35.42 # 2008-05-21T16:01:44.063 # 100000.00 # 200000.00 # uid:0 # # #XML my $parser = XML::LibXML->new(); $account_data_xml = $parser->parse_string($get_account_data_response); return $account_data_xml; } sub get_holdings { my($username) = shift; my $get_holdings_request = < ${username} $endpoint, 'log_file' => '/tmp/stocktrader.log', 'log_level' => 4, 'wsfc_home' => $WSFC_HOME } ); $response = $client->request( { 'payload' => $get_holdings_request} ); my $get_holdings_response = $response->{str}; # my $get_holdings_response = < # # # 46 # 200 # 100.00 # 2008-05-21T16:01:44.157 # s:9 # # # 41 # 200 # 100.00 # 2008-05-21T16:01:44.157 # s:8 # # # 37 # 200 # 100.00 # 2008-05-21T16:01:44.157 # s:7 # # # 32 # 200 # 100.00 # 2008-05-21T16:01:44.14 # s:6 # # # 25 # 200 # 100.00 # 2008-05-21T16:01:44.14 # s:5 # # # #XML my $parser = XML::LibXML->new(); $holdings_xml = $parser->parse_string($get_holdings_response); return $holdings_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 get_holding_information { my($holdings) = shift; my $index = 0; my $total_holdings = 0; my $market_value = 0; my @sub_nodes; my %quote_info; my $xpc = XML::LibXML::XPathContext->new($holdings); $xpc->registerNs('ns'=> 'http://trade.samples.websphere.ibm.com'); @nodes = $xpc->findnodes('//ns:HoldingDataBean'); foreach my $bean (@nodes) { my $xpc = XML::LibXML::XPathContext->new($bean); $xpc->registerNs('ns'=> 'http://trade.samples.websphere.ibm.com'); $quote_id = $xpc->findvalue('./ns:quoteID'); $quantity = $xpc->findvalue('./ns:quantity'); $purchase_price = $xpc->findvalue('./ns:purchasePrice'); if(!$quote_info{$quote_id}) { $quotes = get_quote($quote_id); my $xpc_inner = XML::LibXML::XPathContext->new($quotes); $xpc_inner->registerNs('ns'=> 'http://trade.samples.websphere.ibm.com'); $price = $xpc_inner->findvalue('//ns:price'); $quote_info{$quote_id} = $price; } $quote_price = $quote_info{$quote_id}; $market_value += $quote_price + $quantity; $total_holdings += $quantity * $purchase_price; $index ++; } %holding_info = ('totalHoldings' => $market_value, 'noOfHoldings' => $index); return %holding_info; } 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 ""; } my $market_summary_xml = get_market_summary(); my $xpc = XML::LibXML::XPathContext->new($market_summary_xml); $xpc->registerNs('ns'=> 'http://trade.samples.websphere.ibm.com'); my $tsia = $xpc->findvalue('//ns:TSIA'); print $q->header(); $open_tsia = $xpc->findvalue('//ns:openTSIA'); my $gain = $tsia - $open_tsia; $volume = $xpc->findvalue('//ns:getMarketSummaryReturn/ns:volume'); my @top_gainers = $xpc->findnodes('//ns:topGainers/ns:QuoteDataBean'); my @top_losers = $xpc->findnodes('//ns:topLosers/ns:QuoteDataBean'); my $username = $q->cookie('username'); $account_xml = get_account_data($username); my $xpc = XML::LibXML::XPathContext->new($account_xml); $xpc->registerNs('ns'=> 'http://trade.samples.websphere.ibm.com'); my @account_data_arr = $xpc->findnodes('//ns:getAccountDataReturn'); my $account_data = @account_data_arr[0]; my $holdings = get_holdings($username); my %holding_info = get_holding_information($holdings); my $no_of_holdings = $holding_info{"noOfHoldings"}; my $total_holdings = $holding_info{"totalHoldings"}; my $html_begin =< WSF/PHP StockTrader Welcome

Home

${username}

E print $html_begin; if($account_data) { my $xpc = XML::LibXML::XPathContext->new($account_data); $xpc->registerNs('ns'=> 'http://trade.samples.websphere.ibm.com'); my $creation_date = convert_date($xpc->findvalue('//ns:creationDate')); my $last_login = convert_date($xpc->findvalue('//ns:lastLogin')); my $account_id = $xpc->findvalue('//ns:accountID'); my $login_count = $xpc->findvalue('//ns:loginCount'); my $xml = < XML print $xml; } print < E if(%holding_info && $account_data) { my $xpc = XML::LibXML::XPathContext->new($account_data); $xpc->registerNs('ns'=> 'http://trade.samples.websphere.ibm.com'); my $balance = $xpc->findvalue('//ns:balance'); my $open_balance = $xpc->findvalue('//ns:openBalance'); my $total_cash = $total_holdings + $balance; my $gain = (($total_holdings + $balance) - $open_balance); print (""); print (""); print (""); print (""); print (""); print (""); print (""); print (""); print(""); } print < E print <

Market Summary

E $time = ($sec,$min,$hour,$mday,$mon,$year,$wday, $yday,$isdst)=localtime(time); printf "%4d-%02d-%02d %02d:%02d:%02d\n", $year+1900,$mon+1,$mday,$hour,$min,$sec; print <

User Statistics
Account ID: ${account_id}
Account Created: ${creation_date}
Total Logins: ${login_count}
Session Created: ${last_login}
Summary
Cash Balance: ${balance}
Number of Holdings: ${no_of_holdings}
Total of Holdings:"); printf("\$ %.2f",$total_holdings); print ("
Sum of Cash and Holdings:"); printf("\$ %.2f",($total_cash)); print ("
Opening Balance:"); printf ("\$ %.2f", $open_balance); print ("
Current Gain/(Loss): "); if ($gain < 0) { printf("\$ (%.2f)", ((-1) * $gain)); } elsif ($gain >= 0) { printf("\$ %.2f", $gain); } $gain_percent = ($gain/$open_balance) * 100; print ("
%Gain/(Loss):"); if ($gain_percent > 0) { print ("${gain_percent}%"); } elsif ($gain_percent < 0) { print ("${gain_percent}%"); } else { print ("${gain_percent}%"); } print ("
E if (%holding_info && $account_data) { print (""); print (""); print(""); print ("
Trade Stock Index (TSIA)"); print($tsia); print(" "); print(" 0) { print (" class=\"price-gain\">\$"); } elsif ($gain < 0) { print (" class=\"price-loss\">\$"); } else { print(">"); } printf("%.2f", $gain); print ("
Trading Volume"); print ($volume); print ("
Top Gainers"); print (""); print(""); print(""); foreach my $account_data (@top_gainers) { my $xpc = XML::LibXML::XPathContext->new($account_data); $xpc->registerNs('ns'=> 'http://trade.samples.websphere.ibm.com'); my $symbol = $xpc->findvalue('./ns:symbol'); my $change = $xpc->findvalue('./ns:change'); my $price = $xpc->findvalue('./ns:price'); print (""); printf ("", $price); print (""); print (""); } print ("
SymbolPriceChange
\$%.2f 0) { print ("class=\"price-gain\">\$"); } elsif (($change) < 0) { print ("class=\"price-loss\">\$"); } else { print(">\$"); } printf ("%.2f", $change); print ("
Top Losers "); $index = 0; foreach my $account_data (@top_losers) { my $xpc = XML::LibXML::XPathContext->new($account_data); $xpc->registerNs('ns'=> 'http://trade.samples.websphere.ibm.com'); my $symbol = $xpc->findvalue('./ns:symbol'); my $change = $xpc->findvalue('./ns:change'); my $price = $xpc->findvalue('./ns:price'); print (""); printf ("", $price); print (""); print (""); } print("
SymbolPriceChange
\$%.2f 0) { print (" class=\"price-gain\">\$"); } elsif (($change) < 0) { print (" class=\"price-loss\">\$"); } else { print(">\$"); } printf ("%.2f", $change); print ("
"); print("
"); print("
"); print("
"); } my $html_end = <
E print $html_end;