flex遠隔オブジェクト呼び出し、flexとspringの統合、改ページ


ここで4つの例を説明します.
1、flexの遠隔呼び出し方式(javaのBlazeck方式)を使って、一つの検索テーブルの
2,flexとspringの結合を使って、遠隔の対象を採用してテーブルの検索をします.
3,flexとspringの結合を使って、改ページクエリのリモートオブジェクト呼び出しをします.
4,flexとspringの結合を使って、改ページクエリのリモートオブジェクト呼び出しをして、表ヘッドの方式に対して
まずweb.xmlファイルを配置し、webプロジェクトを行いながら、spring容器とflexのservletを起動することができます.

<context-param>
  	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  	<listener>
	    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
  	<!-- Http Flex Session attribute and binding listener support -->
	<listener>
		<listener-class>flex.messaging.HttpFlexSession</listener-class>
	</listener>

	<!-- MessageBroker Servlet -->
	<servlet>
		<servlet-name>MessageBrokerServlet</servlet-name>
		<servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
		<init-param>
			<param-name>services.configuration.file</param-name>
			<param-value>/WEB-INF/flex/services-config.xml</param-value>
		</init-param>
		<init-param>
			<param-name>flex.write.path</param-name>
			<param-value>/WEB-INF/flex</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>MessageBrokerServlet</servlet-name>
		<url-pattern>/messagebroker/*</url-pattern>
	</servlet-mapping>
flex serviceで使用するオブジェクトを追加します.spring容器で作成したオブジェクトを使用できます.
flexプロファイルservices-config.xmlに追加します.

<factories>
       <factory id="spring" class="com.fruitking.report.flex.util.FlexSpringFactory" />
    </factories>
このクラスのソースコードは以下の通りです.

package com.fruitking.report.flex.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import flex.messaging.FactoryInstance;
import flex.messaging.FlexFactory;
import flex.messaging.config.ConfigMap;
import flex.messaging.services.ServiceException;

public class FlexSpringFactory implements FlexFactory {
	private static final String SOURCE = "source";
	public void initialize(String id, ConfigMap configMap) {
	}

	public FactoryInstance createFactoryInstance(String id, ConfigMap properties) {
		SpringFactoryInstance instance = new SpringFactoryInstance(this,id,properties);
		instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId()));
		return instance;
	} // end method createFactoryInstance()

	public Object lookup(FactoryInstance inst) {
		SpringFactoryInstance factoryInstance = (SpringFactoryInstance) inst;
		return factoryInstance.lookup();
	 }

	static class SpringFactoryInstance extends FactoryInstance {
		SpringFactoryInstance(FlexSpringFactory factory, String id,ConfigMap properties) {
			super(factory, id, properties);
		}

	public String toString() {
			return "SpringFactory instance for id=" + getId() + " source="
					+ getSource() + " scope=" + getScope();
		}

	public Object lookup(){
	   ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext.getServletConfig().getServletContext());
	   String beanName = getSource();
	     try{
	       return appContext.getBean(beanName);
	    }
	  catch (NoSuchBeanDefinitionException nexc){
		ServiceException e = new ServiceException();
		String msg = "Spring service named '" + beanName + "' does not exist.";
		e.setMessage(msg);
		e.setRootCause(nexc);
		e.setDetails(msg);
		e.setCode("Server.Processing");
		throw e;
	  }
	  catch (BeansException bexc){
		ServiceException e = new ServiceException();
		String msg = "Unable to create Spring service named '" + beanName + "' ";
		e.setMessage(msg);
		e.setRootCause(bexc);
		e.setDetails(msg);
		e.setCode("Server.Processing");
		throw e;
	   }
	  }
	}
}
スプリング容器の配置ファイル

<bean id="springIOCProductService" class="com.fruitking.report.service.impl.SpringProductServiceImpl">
	</bean>
ここでflexのリモートオブジェクトから呼び出したプロファイルを再設定します.
remoting-config.xml
最初の配置はflexが自分の配置の実用化クラスを使うことです.
第二はスプリングと結合して、スプリング容器を使って作成した実例類のオブジェクトです.

	<destination id="flexProductService">
        <properties>
            <source>com.fruitking.report.service.impl.FlexProductServiceImpl</source>
        </properties>
    </destination>
    
    <destination id="springProductService">
    	<properties>
    		<factory>spring</factory>
    		<source>springIOCProductService</source>
    		<scope>session</scope>
    	</properties>
    </destination>
flex方式の実用化のクラス

public class FlexProductServiceImpl {
	
	public List<Product> getProductList(){
		List<Product> productList = new ArrayList<Product>();
		try{
			Product product = null;
			for(int i=0;i<10;i++){
				product = new Product();
				product.setProductId(""+i);
				product.setProductName(ContentUtil.getProductName(i));
				product.setProductArea(ContentUtil.getProductArea(i));
				product.setTotalSaleNumber(ContentUtil.getProductTotalSale(i));
				product.setTotalBackNumber(ContentUtil.getProductTotalBack(i));
				BigDecimal totalBackNumber = BigDecimal.valueOf(product.getTotalBackNumber());
				BigDecimal totalSaleNumber = BigDecimal.valueOf(product.getTotalSaleNumber());
				BigDecimal percent = totalBackNumber.divide(totalSaleNumber,4,BigDecimal.ROUND_HALF_EVEN);
				percent = percent.divide(BigDecimal.valueOf(0.01d),2,BigDecimal.ROUND_HALF_EVEN);
				product.setTotalBackPercent(percent.toString()+"%");
				productList.add(product);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return productList;
	}

}
springの実用化の種類

public class SpringProductServiceImpl {
	
	private int pageSize = 10;
	private int pageTotal = 56;
	
	public List<Product> getProductList(){
		List<Product> productList = new ArrayList<Product>();
		Product product = null;
		for(int i=0;i<10;i++){
			product = new Product();
			product.setProductId(""+i);
			product.setProductName(ContentUtil.getProductName(i));
			product.setProductArea(ContentUtil.getProductArea(i));
			product.setTotalSaleNumber(ContentUtil.getProductTotalSale(i));
			product.setTotalBackNumber(ContentUtil.getProductTotalBack(i));
			BigDecimal totalBackNumber = BigDecimal.valueOf(product.getTotalBackNumber());
			BigDecimal totalSaleNumber = BigDecimal.valueOf(product.getTotalSaleNumber());
			BigDecimal percent = totalBackNumber.divide(totalSaleNumber,4,BigDecimal.ROUND_HALF_EVEN);
			percent = percent.divide(BigDecimal.valueOf(0.01d),2,BigDecimal.ROUND_HALF_EVEN);
			product.setTotalBackPercent(percent.toString()+"%");
			productList.add(product);
		}
		return productList;
	}
	
	public FlexPageObject getProductPageList(int pageNumber){
		if(pageNumber<=0){
			pageNumber = 1;
		}
		if(pageNumber>pageTotal){
			pageNumber = pageTotal;
		}
		int productCode = pageSize*(pageNumber-1);
		productCode++;
		FlexPageObject flexPageObject = new FlexPageObject();
		List<Product> productList = new ArrayList<Product>();
		Product product = null;
		for(int i=0;i<pageSize;i++){
			product = new Product();
			product.setProductId(""+productCode);
			product.setProductName(ContentUtil.getProductName(i));
			product.setProductArea(ContentUtil.getProductArea(i));
			product.setTotalSaleNumber(ContentUtil.getProductTotalSale(i));
			product.setTotalBackNumber(ContentUtil.getProductTotalBack(i));
			BigDecimal totalBackNumber = BigDecimal.valueOf(product.getTotalBackNumber());
			BigDecimal totalSaleNumber = BigDecimal.valueOf(product.getTotalSaleNumber());
			BigDecimal percent = totalBackNumber.divide(totalSaleNumber,4,BigDecimal.ROUND_HALF_EVEN);
			percent = percent.divide(BigDecimal.valueOf(0.01d),2,BigDecimal.ROUND_HALF_EVEN);
			product.setTotalBackPercent(percent.toString()+"%");
			productList.add(product);
			productCode++;
		}
		flexPageObject.setObjectList(productList);
		flexPageObject.setPageNumber(pageNumber);
		flexPageObject.setPageSize(pageSize);
		flexPageObject.setPageTotal(pageTotal);
		return flexPageObject;
	}
	
	public FlexPageObject getMutilProductPageList(int pageNumber){
		if(pageNumber<=0){
			pageNumber = 1;
		}
		if(pageNumber>pageTotal){
			pageNumber = pageTotal;
		}
		int productCode = pageSize*(pageNumber-1);
		productCode++;
		FlexPageObject flexPageObject = new FlexPageObject();
		List<Product> productList = new ArrayList<Product>();
		Product product = null;
		for(int i=0;i<pageSize;i++){
			product = new Product();
			product.setProductId(""+productCode);
			product.setProductName(ContentUtil.getProductName(i));
			product.setProductArea(ContentUtil.getProductArea(i));
			product.setTotalSaleNumber(ContentUtil.getProductTotalSale(i));
			product.setTotalBackNumber(ContentUtil.getProductTotalBack(i));
			BigDecimal backNumber = BigDecimal.valueOf(product.getTotalBackNumber());
			BigDecimal saleNumber = BigDecimal.valueOf(product.getTotalSaleNumber());
			BigDecimal percent = backNumber.divide(saleNumber,4,BigDecimal.ROUND_HALF_EVEN);
			percent = percent.divide(BigDecimal.valueOf(0.01d),2,BigDecimal.ROUND_HALF_EVEN);
			product.setTotalBackPercent(percent.toString()+"%");
			product.setWeekSaleNumber(ContentUtil.getProductWeekSale(i));
			product.setWeekBackNumber(ContentUtil.getProductWeekBack(i));
			backNumber = BigDecimal.valueOf(product.getWeekBackNumber());
			saleNumber = BigDecimal.valueOf(product.getWeekSaleNumber());
			percent = backNumber.divide(saleNumber,4,BigDecimal.ROUND_HALF_EVEN);
			percent = percent.divide(BigDecimal.valueOf(0.01d),2,BigDecimal.ROUND_HALF_EVEN);
			product.setWeekBackPercent(percent.toString()+"%");
			product.setYesterdaySaleNumber(ContentUtil.getProductYesterdaySale(i));
			product.setYesterdayBackNumber(ContentUtil.getProductYesterdayBack(i));
			productList.add(product);
			productCode++;
		}
		flexPageObject.setObjectList(productList);
		flexPageObject.setPageNumber(pageNumber);
		flexPageObject.setPageSize(pageSize);
		flexPageObject.setPageTotal(pageTotal);
		return flexPageObject;
	}

}
オブジェクトを組み立てる

public class Product {
	
	private String productId;//    
    private String productName;//    
    private String productArea;//     
    private String countWeek;//    
    private int totalSaleNumber;//      
    private int totalBackNumber;//      
    private String totalBackPercent;//    
    private int weekSaleNumber;//     
    private int weekBackNumber;//     
    private String weekBackPercent;//     
    private int yesterdaySaleNumber;//     
    private int yesterdayBackNumber;//     
}
改ページオブジェクト

public class FlexPageObject {
	
	private int pageNumber;//   
	private int pageSize;//   
	private int pageTotal;//   
	private List<?> objectList;//      
}
最初のflexで呼び出したテーブルのmxmlコード

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	<mx:Script>
	<![CDATA[
		import mx.rpc.events.ResultEvent;
		import mx.collections.ArrayCollection;

        [Bindable]
        public var productWeekDataSource:ArrayCollection = new ArrayCollection(
            [{label:"2010 5 31 -6 6 ", data:1}, 
              {label:"2010 6 7 -6 13 ", data:2}, 
              {label:"2010 6 14 -6 20 ", data:3}]);
 		[Bindable]
        public var productAreaDataSource:ArrayCollection = new ArrayCollection(
         	[{label:"  ", data:1},
         	{label:"  ", data:2},
         	{label:"  ", data:3},
         	{label:"  ", data:4},
         	{label:"  ", data:5},
         	{label:"  ", data:6},
         	{label:"  ", data:7},
         	{label:"  ", data:8},
         	{label:"  ", data:9}]);
       [Bindable]
       public var productSaleDataSource:ArrayCollection = new ArrayCollection(
            [{label:"   ", data:1},
             {label:"   ", data:2}]);
       private var rerultProductList:ArrayCollection;
       public function handleQueryData(event:ResultEvent):void{
       		rerultProductList = ArrayCollection(event.result);
       		productList.dataProvider = rerultProductList;
       }
       public function clearQueryData():void{
       		productList.dataProvider = null;
       }
	]]>
	</mx:Script>
	<mx:RemoteObject id="productDataSource" destination="flexProductService" result="handleQueryData(event)">
		
	</mx:RemoteObject>
	<mx:Panel x="10" y="10" width="969" height="508" layout="absolute" borderColor="#EFCBCB" fontSize="12">
		<mx:ComboBox x="48" y="10" width="193" id="marketweek" dataProvider="{productWeekDataSource}"></mx:ComboBox>
		<mx:ComboBox x="282" y="10" width="128" id="marketarea" dataProvider="{productAreaDataSource}"></mx:ComboBox>
		<mx:ComboBox x="446" y="10" width="131" id="marketproduct" dataProvider="{productSaleDataSource}"></mx:ComboBox>
		<mx:Button x="624" y="11" label="  " fontSize="12" click="productDataSource.getProductList()"/>
		<mx:DataGrid x="19" y="59" width="920" height="334" id="productList">
			<mx:columns>
				<mx:DataGridColumn headerText="    " dataField="productId"/>
				<mx:DataGridColumn headerText="    " dataField="productName"/>
				<mx:DataGridColumn headerText="    " dataField="productArea"/>
				<mx:DataGridColumn headerText="   " dataField="totalSaleNumber"/>
				<mx:DataGridColumn headerText="   " dataField="totalBackNumber"/>
				<mx:DataGridColumn headerText="   " dataField="totalBackPercent"/>
			</mx:columns>
		</mx:DataGrid>
		<mx:Button x="712" y="11" label="  " click="clearQueryData()"/>
	</mx:Panel>
	
</mx:Application>