#!/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; $username = $q->cookie('username'); if(!$username) { #print redirect('/cgi-bin/trader_login'); } print $q->header(); 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 ""; } # 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 get_quote { my($quote_id) = shift; my $get_quotes_request = < ${quote_id} 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_quotes_request } ); $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 $quote_parser = XML::LibXML->new(); $quote_xml = $quote_parser->parse_string($get_quotes_response); return $quote_xml; } # Gets holding details of given user # @param userid user id of current user # returns collection of holding if success. NULL otherwise 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:6 # # # 25 # 200 # 100.00 # 2008-05-21T16:01:44.14 # s:5 # # # #XML my $holdings_parser = XML::LibXML->new(); $holdings_xml = $holdings_parser->parse_string($get_holdings_response); return $holdings_xml; } # 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; } my $username = $q->cookie('username'); my $holdings = get_holdings($username); print < WSF/PHP StockTrader Welcome
E $get_closed_orders_return = get_closed_orders($username); if($get_closed_orders_return) { print("

Trade Alert: The following orders have completed.

"); print(""); my $xpc = XML::LibXML::XPathContext->new($get_closed_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'); print (""); } print("
Order IDOrder StatusCreation DateCompletion Date Txn FeeTypeSymbolQuantity
${order_id} ${order_status} ${open_date} ${completion_date} ${order_fee} ${order_type} ${symbol} ${quantity}


"); } $holdings_return = get_holdings($username); if ($holdings_return) { print ("

Portfolio Information

"); print (""); $index = 0; $purchaseBasis = 0; $marketValue = 0; $gain = 0; my $xpc = XML::LibXML::XPathContext->new($holdings_return); $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'); $purchase_date = $xpc->findvalue('./ns:purchaseDate'); $holding_id = $xpc->findvalue('./ns:holdingID'); if(!$quote_info{$quote_id}) { $quotes = get_quote($quote_id); my $xpc2 = XML::LibXML::XPathContext->new($quotes); $xpc2->registerNs('ns'=> 'http://trade.samples.websphere.ibm.com'); $price = $xpc2->findvalue('//ns:price'); $quote_info{$quote_id} = $price; } my $quote_price = $quote_info{$quote_id}; $purchase_basis += ($purchase_price) * ($quantity); $market_value += ($quote_price) * ($quantity); $gain = ($quote_price - $purchase_price) * ($quantity); print(""); } print (""); } elsif ($gain > 0) { print ("\$${gain}"); } else { print ("\$${gain}"); } print ("
Holding IDPurchase DateSymbol QuantityPurchase PriceCurrent Price Purchase BasisMarket ValueGain(Loss) Trade
${holding_id}${purchase_date}
${quantity}\$${purchase_price} ${quote_price}${purchase_basis} ${market_value} "); if ($gain > 0) { print ("${gain}"); } elsif ($gain < 0) { print ("${gain}"); } else { print ("${gain}"); } print(""); print("
"); print(""); print(""); print(""); print(""); print("
Totals\$${purchase_basis} \$${market_value}"); $gain = $market_value - $purchase_basis; if ($gain < 0) { print ("\$${gain}
"); } print <
E