N342 Home Work 5 - Project  Databases/XML/XSL

Modified

Note

The solution is to require IE to check for new versions on every visit to a page. In IE: Tools | Internet Options | Temporary Internet Files | Settings | Every Visit to Page

The following error may occur when accessing XML files downloaded from one computer to another such as the ZIP files for the assignment. Vista (and perhaps XP) blocks access to files, presumably attempting to protect you from using files you have copied to your computer.

Unblock access to individual files by the following:

  1. Open in Windows Explorer the directory containing the file, either the XML file or one referenced by the XML.
  2. Right click on the file and open Properties.
  3. Click Unblock.

The XML page cannot be displayed

Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.


Access is denied. Error processing resource 'file:///W:/N341/Project/TraderSummaryContent.htm'.

 

Unblock access to all files in a directory by the following:

  1. Download Streams, copy Streams.exe to Windows directory if possible.
  2. To unblock all the files in directory c:\temp and any subdirectories enter:

    Streams –d –s c:\temp

 

Overview 

Three key stock trading functions remain: removing cart contents, committing selected stocks to buy or sell, and producing a summary of stock trading performance.

Home Work 4 completed logic for adding stocks to buy (BuyStock.asp) or sell (SellStock.asp) to the shopping cart, a metaphor for the Cart database table.  Logic for viewing the cart (CartSummary.asp) was also implemented.

Cart removal (CartEdit.asp) - Remove unwanted trades from the cart as marked by a trader. When completed redirects to CartSummary.asp to display the shopping cart contents, no need to write a style sheet.

The stocks to remove are input on the URL address line from the browser, designated by the unique TRADEID number. To designate removal of TRADEID 95 and 101, the address line would be:

http://www.ius.edu/username/N342/Project/CartEdit.asp?95=on&101=on

Determining trades to remove is similar to BuyStock.asp and SellStock.asp. The TRADEIDs can be determined with the Request.QueryString object.

 

Committing (CartCommit.asp) - The purpose of this assignment is to implement logic that commits the stock trades contained in the Cart for the current logged in trader. That is, stocks are bought or sold against the trader's portfolio and account balance.

The logic for committing the trades can be stated as follows:

  1. First  - Verify that the Session("trader") variable is defined, redirect to Login.xml if not defined. Recall that you can edit SessionTrader.asp to initialize the variable.

     

  2. Before Selling - Verify that the number of stocks owned are at least equal to the number in the cart to be sold. You can make the simplifying assumption that this must be true for each individual sale. The assumption is reasonable even though a trader can buy and sell the same stock at the same time.

    The necessary records are produced by a SELECT using an inner join of the Cart and Portfolio tables on SYMBOL and ID selecting records where ID is the trader and BUY is false. The INNER JOIN includes the following:

    FROM CART INNER JOIN PORTFOLIO ON CART.SYMBOL = PORTFOLIO.SYMBOL AND CART.ID = PORTFOLIO.ID

    This is sufficient because the SellList.asp module listed stocks to sell from the trader's portfolio, there is no stock to sell in the cart that is not in the portfolio.

    Iterate through the record set produced by the SELECT verifying a sufficient number of stocks. Redirect to page "userStockShortage.xml" when stocks owned is less than the number of stocks to sell. Do not alter any database tables if there are insufficient stocks owned.

     

    Note that accessing the recordset can be simplified by naming fields. For example, naming CART.SHARES field as SELL:

    rs=conn.Execute("SELECT CART.SHARES AS SELL, PORTFOLIO.SHARES AS OWN  

    allows recordset operations to be written as:

    if( rs("SELL") > rs("OWN") ) ...

     

  3. Before Buying - Verify a sufficient balance for the total stock purchase after stocks in the cart are sold.

    Redirect to page "OverDraft.xml" when insufficient funds are available. Do not alter any database tables if there are insufficient funds.

     

  4. Committing a buy (after verifying above there are sufficient funds from the balance in the Trader table).

    There are three cases to handle:

    1. Stocks not in the Portfolio table must be inserted from the Cart into the trader's Portfolio.
    2. Stocks already in the Portfolio must have the number in the Cart added.
    3. The trade must be inserted into the permanent record of trades, the Trades database table. Examine the table for the necessary fields. Note that TRADEID and TIMEDATE are automatic fields and are automatically inserted (i.e. INSERT INTO TRADES (ID, SYMBOL, PRICE, SHARES, BUY) VALUES ...).

    Note that multiple recordsets can be defined simultaneously. One can then write statements such as:

    var rc = conn.Execute("SELECT * FROM CART WHERE ID = '"+trader+"';");
    while( !rc.EOF ) {
        var rp = conn.Execute("SELECT * FROM PORTFOLIO WHERE SYMBOL = '"+rc("SYMBOL")+"' AND ID='"+trader+"'");
        rc.MoveNext();
    }

    This example selects all records for a trader from the CART table (rc recordset) and, using the current CART record, the PORTFOLIO record with the matching SYMBOL (for that trader).

     

  5. Committing a sell (after verifying above there are sufficient stocks in the Portfolio table)

     

  6. Account balance - The balance is updated to reflect the stocks bought and sold less the 5% commission on the value of each stock trade.

     

  7. Cart - After all trades have been committed the cart is emptied of the trader's selections.

     

  8. Completed - Redirect to page "Portfolio.asp" showing the trader the results.

 

Testing requires placing several buy/sell stock transactions in the CART table for a registered and logged in trader. Then execute:

http://www.ius.edu/username/N342/Project/CartCommit.asp

 

Performance (TradingPerformance.asp) - The trading performance summary consists of a balance for all stock trades and the total of commissions paid.

The logic is:

  1. Start with a BALANCE=0.
  2. Select all records from the TRADES database table for the trader.
  3. For each trader record:
    1. Compute the value of each trade as PRICE*SHARES; add to the BALANCE for the stocks sold or subtract from the BALANCE for the stocks bought.
    2. Sum the COMMISSION as 5% of the value of each trade.
  4. Subtract the COMMISSION from the BALANCE.
  5. Place the BALANCE and COMMISSION in an XML tree.
  6. Display BALANCE and COMMISSION by transforming the XML with XSL.

The key difference from other project modules is that the XML tree is not generated from a database but is constructed dynamically by placing computed BALANCE and COMMISSION values in the XML tree. The XML tree can be created from text by:

var xmlData = Server.CreateObject("Microsoft.XMLDOM");
xmlData.async = false;
xmlData.loadXML("<PERFORMANCE><BALANCE/><COMMISSION/></PERFORMANCE>");
The elements <BALANCE> and <COMMISSION> are to be assigned a text value; a value of 123.45 is assigned as the text of the BALANCE node by:
  var root = xmlData.documentElement;
  var node = root.selectSingleNode("BALANCE"); 
  node.text = 123.45;
The XSL transformation file is "TradingPerformanceContent.htm", downloaded for HW5. 
For more on tree manipulations, see class notes.

 

Testing requires placing several buy/sell stock transactions in the TRADES table for a registered and logged in trader. Then execute:

http://www.ius.edu/username/N342/Project/TradingPerformance.asp

 

Global.asa

The file, Global.Asa, is part of HW5 files and should be copied into the Project directory. The Global.Asa file should automatically be executed when a session starts and ends; the purpose is usually to perform session initializations on start, logout traders and delete the cart when the session times out.

However, at the current time, the IUS server is not properly executing the Global.Asa file. Please ignore for this assignment but it would need to be resolved for a production system. The default sessions timeout limit and the one used at IUS is set at 20 minutes.

On opening an ASP script on a virtual directory for the first time, the server first reads the file Global.Asa from the root directory of the same virtual directory of the script. The Global.Asa file would normally contain session timeout limits, initialization, etc. The example below contains sets the important Session.Timeout to 1 minute. If the client associated with the session has no activity in 1 minute that causes a call to the Session_OnEnd subroutine in the Global.Asa script by the server, the server undefines the session variables and effectively forgets about the client. The Session.Timeout default is 20 minutes.

For the project, use the following Global.asa (copied with the Home Work 5 files). Its purpose is, on an inactivity timeout, to remove any stocks from the cart for the trader and to logout the trader.

If timeouts occur too soon, change the Session.Timeout = 1 to a larger value.

<Script LANGUAGE = VBScript RUNAT = Server> 

Sub Session_OnStart 
  Response.Expires = 1 ' Page cached 1 minute
  Session.Timeout = 1 ' End session after 1 minutes 
 End Sub 

Sub Session_OnEnd
  if(Session("trader")<>"") then
    set conn = Server.CreateObject("ADODB.Connection") 
    conn.Mode = 3
    conn.Open ("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("Project.mdb")  )
    conn.Execute("UPDATE TRADER SET LOGGED = 0 WHERE ID='"&Session("trader")&"'")
    conn.Execute("DELETE FROM CART WHERE ID='"&Session("trader")&"'")
    conn.Close()
  end if
End Sub 
</Script>

 

Assignment

Complete:

  1. Remove unwanted trades from cart (CartEdit.asp).
  2. Commit stock trades from cart (CartCommit.asp).
  3. Calculate the performance as the balance and commission of trades (TradingPerformance.asp)

Turn in - Due before class on the date listed in the syllabus.

  1. OnCourse Drop Box
    1. Compress the Project folder containing the homework files.
      • In Windows Explorer, right-click on the Project folder.
      • Click Send to then click Compressed (zipped) Folder.
      • Project.zip will be created.
    2. Upload Project.zip to your CSCI N342 OnCourse Drop Box using the Display Name of HW5.
  2. IUS server
    1. Copy to W: drive or FTP to webftp.ius.edu
      • In browser address enter: ftp://webftp.ius.edu
      • Login using your IUS ADS\username and password.
      • Copy the contents of the Project directory onto ftp page.
    2. Test in IE.
  3. Email notification to rwisman@ius.edu with subject: YOUR NAME - N342 HW5 - username