Monitor the Stock Price

From Documentation
Revision as of 06:44, 19 November 2010 by Henrichen (talk | contribs) (Created page with '{{ZKSpreadsheetEssentialsPageHeader}} __TOC__ Here is a ''Stock Price'' monitoring system that uses the ZK Spreadsheet's onCellChange event and data push mechanism. ==Purpose=…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Here is a Stock Price monitoring system that uses the ZK Spreadsheet's onCellChange event and data push mechanism.

Purpose

Automatically place buy or sell' command whenever the price of a selected stock reach some preset point.

Template Excel File with Proper Name Expressions

Here is an Excel template file with monitorSheet and dataSheet. monitorSheet shows the selected stocks that refer to the dataSheet with formulas. dataSheet is a sheet that list all available stocks.

File:Stock-template.png

How ZK Spreadsheet Do the Job

Assume a stock price service will keep on pumping in updated stock price into the dataSheet on a separate thread. Whenever a new price is updated into dataSheet, ZK Spreadsheet will trigger the cell change event on monitorSheet that refer to the dataSheet. The onCellChange event listener registered on the ZK Spreadsheet will be called and check the prices . It compairs new price with preset selling price and buying price then notify buying or selling via another Web service.

Stock-event.png

ZUML Example

<spreadsheet apply="demo.stock.StockComposer"
	src="/stock.xls"
	maxrows="200" 
	maxcolumns="40" 
	vflex="1" 
	width="100%">
</spreadsheet>

Stock Composer

This is the controller that handle the onCellChange event.

package demo.stock;
public class StockComposer extends GenericForwardComposer {

}

Result

File:Bsheet-result.png

When Value of Java Bean Changes

Will ZK Spreadsheet update the cells if the value of the Java bean changes? Well, if you notify it.

	bsheet.getBook().notifyChange(new String[] {"dataBean"});

When you notify the ZK Spreadsheet work book model that beans with the specified names have changed, it will collect which cells are affected (i.e. those dependent cells with the specified bean names), and publish data change event accordingly. The ZK Spreadsheet UI or any other components that have subscribed to the work book's event queue and are interested in the event will then retrieve the new cells' data from the beans and update themselves.

Integrate ZK Spreadsheet with Spring

As we have mentioned in #How ZK Spreadsheet Resolve my Java Beans, ZK Spreadsheet follows ZK's EL expression variable resolving mechanism. When trying to resolve a variable, it will finally ask variable resolvers declared in the ZUML page to retrieve the bean of the named variable. So simply declare the variable-resolver with org.zkoss.zkplus.spring.DelegatingVariableResolver on top of your ZUML page, you are now data-binding your ZK Spreadsheet with your Spring-Managed beans directly. Following we demonstrate the example that use the same bsheet.xls template file as in #Template Excel File with Proper Name Expressions but data-binding to Spring-Managed bean.

ZUML Example

In ZUML page, we declare the variable resolver for retrieving Spring-Managed beans so ZK Spreadsheet can find them and show the evaluated value in the spreadsheet's cells.

<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<spreadsheet id="bsheet"
    src="/bsheet.xls"
    maxrows="200"
    maxcolumns="40"
    vflex="1"
    width="100%">
</spreadsheet>

Spring Configuration File

Define bean definitions in applicationContext.xml file, and put it into your WEB-INF directory.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
 
<beans>
  <bean id="dataBean" class="demo.springbean.DataBeanImpl"/>
</beans>

Spring Bean Class

Define DataBean interface and its implementation. Mainly getters and setters for various attributes for a balance sheet.

DataBean.java

package demo.springbean;
public interface DataBean {
	public double getLiquidAssets();

	public void setLiquidAssets(double liquidAssets);

	//...other getters and setters
}

DataBeanImpl.java

package demo.springbean;
public class DataBeanImpl implements DataBean {
	private double liquidAssets;
	private double fundInvestment;
	private double fixedAssets;
	private double intangibleAsset; 
	private double otherAssets;
	private double currentLiabilities;
	private double longTermLiabilities;
	private double otherLiabilities;
	private double capitalStock;
	private double capitalSurplus;
	private double retainedEarnings;
	private double otherEquity;
	private double treasuryStock;
	
	public double getLiquidAssets() {
		return liquidAssets;
	}

	public void setLiquidAssets(double liquidAssets) {
		this.liquidAssets = liquidAssets;
	}

	//...other getters and setters implementation
}

Version History

Last Update : 2010/11/19


Version Date Content
     


All source code listed in this book is at Github.


Last Update : 2010/11/19

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.