/*
* 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 public OrderEvent validatesOrderEvent(OrderEvent orderEvent, GigaSpace gigaSpace) {
try {
Thread.sleep(workDuration);
} catch (InterruptedException e) {
}
System.out.println("\nValidator validates [Normal] order: First Name["+orderEvent.getFirstName()+
"] Last Name ["+orderEvent.getLastName()+
"] Price ["+orderEvent.getPrice()+"]");
Account queryTemplate = new Account(orderEvent.getFirstName()/* account first name*/
,orderEvent.getLastName()/* account last name*/
,orderEvent.getPrice());/* account amount*/
SQLQuery<Account> query = new SQLQuery<Account>(queryTemplate,"firstName = ? and lastName = ? and amount > ?");
Account account=(Account)gigaSpace.read((Object)query);
if (account!= null)
{
orderEvent.setStatus(OrderEvent.STATUS_APPROVED);
}
else
{
orderEvent.setStatus(OrderEvent.STATUS_REJECTED);
}
System.out.println("Validator set order status to: ["+orderEvent.getStatus()+"]");
orderEvent.setOrderID(null);
return orderEvent;
}
}