/*
 * Copyright 2008 GigaSpaces Technologies LTD. All rights reserved.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY AND 
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. GIGASPACES WILL NOT 
 * BE LIABLE FOR ANY DAMAGE OR LOSS IN CONNECTION WITH THE SOFTWARE.
 */

package com.gigaspaces.examples.tutorials.queries.validator;

import org.openspaces.core.GigaSpace;
import org.openspaces.events.adapter.SpaceDataEvent;

import com.gigaspaces.examples.tutorials.queries.common.Account;
import com.gigaspaces.examples.tutorials.queries.common.OrderEvent;

import com.j_spaces.core.client.SQLQuery;

/**
 * Simple bean used to validate the "Normal" orderEvent objects.
 * Showing use of template parameterized query.
 */
public class NormalOrderEventValidator {
	
	private long workDuration = 100;
    
	/**
     * Sets the simulated work duration (in milliseconds). Default to 100.
     */
    public void setWorkDuration(long workDuration) {
        this.workDuration = workDuration;
    }

    /**
     * Validates the given OrderEvent object and returns the validated OrderEvent with
     * status field set to Approved/Rejected according to the validation.
     * Can be invoked using OpenSpaces Events when a matching event
     * occurs.
     * The order is approved if an account for the user is found holding enough money 
     * to buy the order (compared to orderEvent.price attribute).
     */
    @SpaceDataEvent	//	This annotation marks the method as the event listener.
    public OrderEvent validatesOrderEvent(OrderEvent orderEvent, GigaSpace gigaSpace) {
            	    	
    	// sleep to simulate some work
        try {
            Thread.sleep(workDuration);
        } catch (InterruptedException e) {
            // do nothing
        }
     
        System.out.println("\nValidator validates [Normal] order: First Name["+orderEvent.getFirstName()+
        															"] Last Name ["+orderEvent.getLastName()+
        															"] Price ["+orderEvent.getPrice()+"]");
        
        // Querying using template query
        // =============================
        // Create the template for the query
        Account queryTemplate = new Account(orderEvent.getFirstName()/* account first name*/ 
        									,orderEvent.getLastName()/* account last name*/ 
        									,orderEvent.getPrice());/* account amount*/
        
        // Create the query using the values from the template, each ? sign is replaced with the corresponding value 
        // The query is actually "firstName=(queryTemplate.firstName) and lastName=(queryTemplate.lastName) and amount>(queryTemplate.amount)" 
        SQLQuery<Account> query = new SQLQuery<Account>(queryTemplate,"firstName = ? and lastName = ? and amount > ?");
		
        // REMARK: Instead of using the constructor the query can also be 
        // set using the setTemplate and setQuery methods:
        // ===============================================
        // query.setTemplate(queryTemplate);
        // query.setQuery("firstName = ? and lastName = ? and amount > ?");
        
        // Read from the space the account matching the query
        Account account=(Account)gigaSpace.read((Object)query);

        // Set the order status according to the query result
        if (account!= null)
		{
			orderEvent.setStatus(OrderEvent.STATUS_APPROVED);
		}
		else 
		{
			orderEvent.setStatus(OrderEvent.STATUS_REJECTED);
		}       
        
		System.out.println("Validator set order status to: ["+orderEvent.getStatus()+"]");
		
        //  orderID is declared as primary key and as auto-generated. 
    	//	It must be null before writing an operation.
    	orderEvent.setOrderID(null);
    	
        return orderEvent;
    }
}
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence