#!/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; # check for logged in cookie and redirect to home if its' not set $username = $q->cookie('username'); if(!$username) { print redirect('/cgi-bin/trader_login'); exit(0); } print $q->header(); # 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 UpdateAccountProfile { $userID = shift; $fullName = shift; $email = shift; $address = shift; $creditCard = shift; $password = shift; $request = < ${userID} ${password} ${fullName}
${address}
${email} ${creditCard}
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' => $request } ); $get_closed_orders_response = $response->{str}; # my $get_closed_orders_response = < # # # #XML return $get_closed_orders_response; } sub get_orders { my $userID = shift; $request = < ${userID} 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' => $request } ); my $get_orders_response = $response->{str}; # my $get_orders_response = < # # # 46 # sell # completed # 2008-05-21T16:01:44.157 # 2008-05-21T16:01:44.157 # 200 # 100.00 # 24.50 # s:9 # # # 46 # buy # completed # 2008-05-21T16:01:44.157 # 2008-05-21T16:01:44.157 # 200 # 100.00 # 21.50 # s:9 # # # 42 # buy # completed # 2008-05-21T16:01:44.157 # 2008-05-21T16:01:44.157 # 200 # 100.00 # 24.50 # s:8 # # # 37 # buy # completed # 2008-05-21T16:01:44.157 # 2008-05-21T16:01:44.157 # 200 # 100.00 # 24.50 # s:7 # # # 32 # buy # completed # 2008-05-21T16:01:44.157 # 2008-05-21T16:01:44.157 # 200 # 100.00 # 24.50 # s:6 # # # 26 # buy # completed # 2008-05-21T16:01:44.14 # 2008-05-21T16:01:44.14 # 200 # 100.00 # 24.50 # s:5 # # # #XML my $parser = XML::LibXML->new(); $get_orders_xml = $parser->parse_string($get_orders_response); return $get_orders_xml; } sub get_user_account_summary { $orders_return_xml = shift; my $xpc = XML::LibXML::XPathContext->new($orders_return_xml); $xpc->registerNs('ns'=> 'http://trade.samples.websphere.ibm.com'); @nodes = $xpc->findnodes('//ns:OrderDataBean'); $tax = 0; $buys = 0; $sells = 0; foreach my $bean (@nodes) { my $xpc = XML::LibXML::XPathContext->new($bean); $xpc->registerNs('ns'=> 'http://trade.samples.websphere.ibm.com'); $order_fee = $xpc->findvalue('./ns:orderFee'); $price = $xpc->findvalue('./ns:price'); $order_type = $xpc->findvalue('./ns:orderType'); $quantity = $xpc->findvalue('./ns:quantity'); if ($order_type eq "buy") { $buys = $buys + (($price) * int($quantity)) + ($order_fee); } elsif ($order_type eq "sell") { $sells = $sells + (($price) * int($quantity)) - ($order_fee); } $tax = $tax + $order_fee; } return %account_summary = ('totalBuys' => $buys, 'totalSells' => $sells, 'totalTax' => $tax, 'totalImpact' => $buys + $tax - $sells); } sub get_account_data { my($username) = shift; my $get_account_data_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_account_data_request} ); my $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_account_profile_data { my($username) = shift; $request = < ${username} E 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' => $request } ); $response_str = $response->{str}; # $response_str = < # # uid:0 # xxx # Full Name 0 #
111 First Street, Redmond, WA 98033
# user0@company.com # 469023-0320 #
# #E my $parser = XML::LibXML->new(); $account_data_xml = $parser->parse_string($response_str); return $account_data_xml; } # If the user requested to update his profile information if ($q->param('UPDATEUSERPROFILE')) { $userID = $q->cookie("username"); $password = $q->cookie('PASSWORD'); $fullName = $q->cookie('FULLNAME'); $address = $q->cookie('ADDRESS'); $email = $q->cookie('EMAIL'); $creditCard = $q->cookie('CREDITCARD'); UpdateAccountProfile($userID, $fullName, $email, $address, $creditCard, $password); } $orders_return = get_orders($username); %account_summary = get_user_account_summary($orders_return); $user_account_data = get_account_data($username); $user_account_profile_data = get_account_profile_data($username); #convert the date 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; } $get_closed_orders_return = get_closed_orders($username); my $html_begin =< WSF/PHP StockTrader Welcome
E # call WSClient and get data print $html_begin; 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 (""); } # get order details and display print("
Order IDOrder StatusCreation DateCompletion Date Txn FeeTypeSymbolQuantity
${order_id} ${order_status} ${open_date} ${completion_date} ${order_fee} ${order_type} ${symbol} ${quantity}


"); } } # Display the account summary information of a particular user. if (%account_summary) { $total_buys = $account_summary{"totalBuys"}; $total_sells = $account_summary{"totalSells"}; $total_tax = $account_summary{"totalTax"}; $total_impact = $account_summary{"totalImpact"}; print ("

Account Information

"); print (""); printf("", $total_buys); printf("", $total_sells); printf("", $total_tax); printf("
Subtotal BuysSubtotal SellsSubtotal Fees Net Impact Cash Balance
\$%.2f\$%.2f\$%.2f"); if ($total_impact > 0) { printf("\$%.2f", $total_impact); } elsif ($total_impact < 0) { printf("\$%.2f", $total_impact); } else { printf("(\$%.2f)", $total_impact); } printf("
"); } # Display the orders a particular user is associated with. if ($orders_return) { print("

Total Orders Shown

"); print(""); print(""); $index = 0; my $xpc = XML::LibXML::XPathContext->new($orders_return); $xpc->registerNs('ns'=> 'http://trade.samples.websphere.ibm.com'); @nodes = $xpc->findnodes('//ns:OrderDataBean'); 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'); $calculated_fee = (($price * $quantity) + $order_fee); print (""); print (""); print (""); $index ++; } print ("
Order IDOrder StatusCreation DateCompletion Date Txn FeeTypeSymbolQuantityPrice Total
${order_id} ${order_status} ${open_date} ${completion_date} \$${order_fee} ${order_type}
${quantity} \$${price} \$${calculated_fee}
"); } if($user_account_profile_data) { my $xpc = XML::LibXML::XPathContext->new($user_account_profile_data); $xpc->registerNs('ns'=> 'http://trade.samples.websphere.ibm.com'); $fullName = $xpc->findvalue('//ns:fullName'); $email = $xpc->findvalue('//ns:email'); $address = $xpc->findvalue('//ns:address'); $creditCard = $xpc->findvalue('//ns:creditCard'); $password = $xpc->findvalue('//ns:password'); print ("
Update Account Profile:${username}
"); print (""); print (""); print (""); print (""); print (""); print ("
Full Name: Email Address:
Address: Password:
Credit Card: Confirm Password:
new($user_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 $logout_count = $xpc->findvalue('//ns:logoutCount'); my $profile_id = $xpc->findvalue('//ns:profileID'); my $open_balance = $xpc->findvalue('//ns:openBalance'); my $balance = $xpc->findvalue('//ns:balance'); print(""); print (""); print (""); print (""); print(""); if ($balance > 0) { print(""); } elsif($balance < 0) { $positive_balance = -1 * $balance; print(""); } else { print(""); } print(""); print("
Account ID: ${account_id} Account Created${creation_date}
User ID: ${profile_id} Last Login:${last_login}
Opening Balance: ${open_balance} Total Logins: ${login_count}
Cash Balance:\$ ${balance}\$${positive_balance}\$${balance}Total Logous: ${logout_count}
"); } print("
"); } print <
E