Solved

How to realize a historisation with ejb3.0?

Posted on 2009-07-06
3
598 Views
Last Modified: 2013-11-24
Hi,

I am currently migrating ejb2.0 beans to ejb3.0. After some problems and a little help from google and this site I managed to migrate a fair amount of our beans, all our simple beans are done.

Now I hang on another problem. The remaining beans use a historisation mechanism to allow a traceable lookup of all actions that occured on a particular bean. To make it easier I will use one of our beans as example.

We have a bean for maintaining changes on orders. For this purpose we have 2 tables, a view and a trigger on our Oracle10g database:

ORDER - Containing latest data of an order.
ORDER_HISTORY - Containing all data of an order that had bean stored prior to the latest data in the ORDER table.
ORDER_COMPLETE - A view displaying all data from ORDER and ORDER_HISTORY
ORDER_BEFORE_INSERT - A trigger that writes old data to ORDER_HISTORY before it is updated with new data in the table ORDER.

When creating or changing an order, we use the default methods of the ejb2.0 bean (ejbCreate and ejbStore). which refer to the ORDER table. When we want to lookup history data, we implemented an additional method which returns all data on an order by refering to ORDER_COMPLETE. The datasets are encapsuled in value objects, where each value object represents a dataset. Since both tables and the view have the same structure, I can use one value object class to transfer data from all threee sources.


In ejb3.0 I created a new object (a pojo) representing a dataset in the table ORDER. Creating and updating works fine. But since the bean is bound to the ORDER table, I can not use it to transfer data from ORDER_HISTORY or ORDER_COMPLETE.

Anybody knows how I can work around this. I don't want to create three pojos for three sources, when the structure is identical. it also brings up more problems, since our GUI is woking on just one object for all three source, It would also mean a heavy change in out GUI sources.

Or has anybody already implemented a historisation mechanism in ejb3.0 and can tell me how to implement it?

Thanks and Regards,
CB

0
Comment
Question by:Crazy_Bytes
  • 3
3 Comments
 
LVL 2

Author Comment

by:Crazy_Bytes
Comment Utility
Hi again,

for convenience I created a little project, demonstrating my problem with an Order entity as example. I tried to upload a complete project, but ee blocked it due to the fact that .java-, .class- and .xml-files are not allowed. So I copied every needed source file and xml files into the code snippet window.

I used
    - JBoss 4.2.3.GA application Server
    - Oracle 10g Database
    - JDK 1.6 / JRE 1.6 runtime
    - eclipse 3.4.1 (Ganymede)

Required libraries are
    - javaee.jar    (only for compiling, available at java.sun.com)
    - jbossall-client-4.2.3.jar (only for running, available at www.jboss.org)

The Tables ORDER_HEAD and ORDER_ITEM should be created during deployment of the beans.
In my scenario I created two more tables (ORDER_HEAD_HISTORY and ORDER_ITEM_HISTORY).
These two tables have the samestructure as the other two, except that the primary keys on the *_HISTOTY tables were extended with the field VALID_FROM.

I created two Views (ORDER_HEAD_FULL and ORDER_ITEM_FULL). Those display the data of both tables (ORDER_HEAD_FULL displays data from ORDER_HEAD and ORDER_HEAD_HISTORY, etc.),

The table ORDER_HEAD and ORDER_ITEM have a trigger which writes the old data into the *_HISTORY tables when ever a dataset is updated or deleted.

Creating and updating an order works fine. But when I try to load the full history of an order, I only receive the latest dataset multiplied with the amount of changes, eg.:
     An order has been created and was modified twice afterwards. I recieve a Collection with three
    entries, but all contain the latest information of the order.

Anybody knows how to fix this problem?

Thanks and regards,
CB


//***********************************************************************

// OrderHead.java (Entity)

//***********************************************************************

package com.crazy_bytes.business.order.persist;
 

import java.io.Serializable;

import java.util.Calendar;

import java.util.Date;
 

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.IdClass;

import javax.persistence.PrePersist;

import javax.persistence.PreRemove;

import javax.persistence.PreUpdate;

import javax.persistence.Table;

import javax.persistence.Temporal;

import javax.persistence.TemporalType;
 

@Entity

@Table(name = "ORDER_HEAD")

@IdClass(OrderHeadPK.class)

@SuppressWarnings("serial")

public class OrderHead implements Serializable {

	/** Represents a company */

	@Column(nullable = false, scale = 2, precision = 0)

	@Id

	private Integer company_id;

	

	/** Represents an office branch */

	@Column(nullable = false, scale = 2, precision = 0)

	@Id

	private Integer branch_id;

	

	/** The id of the order */

	@Column(nullable = false, scale = 8, precision = 0)

	@Id

	private Integer order_id;

	

	/** Represents a customer */

	@Column(nullable = false, scale = 2, precision = 0)

	@Id

	private Integer customer_id;

	

	/** Optional discount for the entire order */

	//@Column(scale = 5, precision = 2)

	// Oracle specific

	@Column(scale = 5, precision = 2, columnDefinition = "number(5, 2)")

	private Float discount;

	

	/** Optional hints on the order */

	@Column(length = 1000)

	private String hints;

	

	/** Optional date to indicate a requested date of delivery */

	@Temporal(TemporalType.DATE)

	private Date date_of_delivery;

	

	/** User who created or manipulated the order */

	@Column(nullable = false, length = 12)

	private String username;

	

	/** Indicates the type of action that was performed on the order */

	@Column(nullable = false, length = 6)

	private String action;

	

	/** Timestamp which indicates from when this entity is valid */

	@Column(nullable = false)

	@Temporal(TemporalType.TIMESTAMP)

	private Date valid_from;
 

	/** Timestamp which indicates till when this entity is valid */

	@Column(nullable = false)

	@Temporal(TemporalType.TIMESTAMP)

	private Date valid_to;

	

	public OrderHead() {}
 

	/**

	 * @return the company_id

	 */

	public Integer getCompany_id() {

		return company_id;

	}
 

	/**

	 * @param company_id the company_id to set

	 */

	public void setCompany_id(Integer company_id) {

		this.company_id = company_id;

	}
 

	/**

	 * @return the branch_id

	 */

	public Integer getBranch_id() {

		return branch_id;

	}
 

	/**

	 * @param branch_id the branch_id to set

	 */

	public void setBranch_id(Integer branch_id) {

		this.branch_id = branch_id;

	}
 

	/**

	 * @return the order_id

	 */

	public Integer getOrder_id() {

		return order_id;

	}
 

	/**

	 * @param order_id the order_id to set

	 */

	public void setOrder_id(Integer order_id) {

		this.order_id = order_id;

	}
 

	/**

	 * @return the customer_id

	 */

	public Integer getCustomer_id() {

		return customer_id;

	}
 

	/**

	 * @param customer_id the customer_id to set

	 */

	public void setCustomer_id(Integer customer_id) {

		this.customer_id = customer_id;

	}
 

	/**

	 * @return the discount

	 */

	public Float getDiscount() {

		discount = discount + 1;

		return discount;

	}
 

	/**

	 * @param discount the discount to set

	 */

	public void setDiscount(Float discount) {

		this.discount = discount;

	}
 

	/**

	 * @return the hints

	 */

	public String getHints() {

		return hints;

	}
 

	/**

	 * @param hints the hints to set

	 */

	public void setHints(String hints) {

		this.hints = hints;

	}
 

	/**

	 * @return the date_of_delivery

	 */

	public Date getDate_of_delivery() {

		return date_of_delivery;

	}
 

	/**

	 * @param date_of_delivery the date_of_delivery to set

	 */

	public void setDate_of_delivery(Date date_of_delivery) {

		this.date_of_delivery = date_of_delivery;

	}
 

	/**

	 * @return the username

	 */

	public String getUsername() {

		return username;

	}
 

	/**

	 * @param username the username to set

	 */

	public void setUsername(String username) {

		this.username = username;

	}
 

	/**

	 * @return the action

	 */

	public String getAction() {

		return action;

	}
 

	/**

	 * @param action the action to set

	 */

	public void setAction(String action) {

		this.action = action;

	}
 

	/**

	 * @return the valid_from

	 */

	public Date getValid_from() {

		return valid_from;

	}
 

	/**

	 * @param valid_from the valid_from to set

	 */

	public void setValid_from(Date valid_from) {

		this.valid_from = valid_from;

	}
 

	/**

	 * @return the valid_to

	 */

	public Date getValid_to() {

		return valid_to;

	}
 

	/**

	 * @param valid_to the valid_to to set

	 */

	public void setValid_to(Date valid_to) {

		this.valid_to = valid_to;

	}
 

	/* (non-Javadoc)

	 * @see java.lang.Object#hashCode()

	 */

	@Override

	public int hashCode() {

		final int prime = 31;

		int result = 1;

		result = prime * result	+ ((branch_id == null) ? 0 : branch_id.hashCode());

		result = prime * result	+ ((company_id == null) ? 0 : company_id.hashCode());

		result = prime * result	+ ((customer_id == null) ? 0 : customer_id.hashCode());

		result = prime * result + ((date_of_delivery == null) ? 0 : date_of_delivery.hashCode());

		result = prime * result + ((discount == null) ? 0 : discount.hashCode());

		result = prime * result + ((hints == null) ? 0 : hints.hashCode());

		result = prime * result + ((order_id == null) ? 0 : order_id.hashCode());

		return result;

	}
 

	/* (non-Javadoc)

	 * @see java.lang.Object#equals(java.lang.Object)

	 */

	@Override

	public boolean equals(Object obj) {

		if (this == obj)

			return true;

		if (obj == null)

			return false;

		if (!(obj instanceof OrderHead))

			return false;

		OrderHead other = (OrderHead) obj;

		if (branch_id == null) {

			if (other.branch_id != null)

				return false;

		} else if (!branch_id.equals(other.branch_id))

			return false;

		if (company_id == null) {

			if (other.company_id != null)

				return false;

		} else if (!company_id.equals(other.company_id))

			return false;

		if (customer_id == null) {

			if (other.customer_id != null)

				return false;

		} else if (!customer_id.equals(other.customer_id))

			return false;

		if (date_of_delivery == null) {

			if (other.date_of_delivery != null)

				return false;

		} else if (!date_of_delivery.equals(other.date_of_delivery))

			return false;

		if (discount == null) {

			if (other.discount != null)

				return false;

		} else if (!discount.equals(other.discount))

			return false;

		if (hints == null) {

			if (other.hints != null)

				return false;

		} else if (!hints.equals(other.hints))

			return false;

		if (order_id == null) {

			if (other.order_id != null)

				return false;

		} else if (!order_id.equals(other.order_id))

			return false;

		return true;

	}
 

	private void setActionCredentials() {

		Calendar calendar = Calendar.getInstance();

		setValid_from(calendar.getTime());

		calendar.set(Calendar.YEAR, 2999);

		calendar.set(Calendar.MONTH, Calendar.DECEMBER);

		calendar.set(Calendar.DAY_OF_MONTH, 31);

		calendar.set(Calendar.HOUR_OF_DAY, 0);

		calendar.set(Calendar.MINUTE, 0);

		calendar.set(Calendar.SECOND, 0);

		calendar.set(Calendar.MILLISECOND, 0);

		setValid_to(calendar.getTime());

		//setUser(ctx.getCallerPrincipal().getName());

	}

	

	@PrePersist

	@SuppressWarnings("unused")

	private void prePersist() {

		setAction("CREATE");

		setActionCredentials();

	}

	

	@PreUpdate

	@SuppressWarnings("unused")

	private void preUpdate() {

		setAction("UPDATE");

		setActionCredentials();

	}
 

	@PreRemove

	@SuppressWarnings("unused")

	private void preRemove() {

		setAction("DELETE");

		setActionCredentials();

	}

}
 

//***********************************************************************

// OrderHeadPK.java (Primary Key of OrderHead)

//***********************************************************************

package com.crazy_bytes.business.order.persist;
 

import java.io.Serializable;
 

@SuppressWarnings("serial")

public final class OrderHeadPK implements Serializable {

	private Integer company_id;

	private Integer branch_id;

	private Integer order_id;

	private Integer customer_id;

	

	public OrderHeadPK() {}

	

	/* (non-Javadoc)

	 * @see java.lang.Object#hashCode()

	 */

	@Override

	public int hashCode() {

		final int prime = 31;

		int result = 1;

		result = prime * result	+ ((branch_id == null) ? 0 : branch_id.hashCode());

		result = prime * result	+ ((company_id == null) ? 0 : company_id.hashCode());

		result = prime * result	+ ((customer_id == null) ? 0 : customer_id.hashCode());

		result = prime * result	+ ((order_id == null) ? 0 : order_id.hashCode());

		return result;

	}

	

	/* (non-Javadoc)

	 * @see java.lang.Object#equals(java.lang.Object)

	 */

	@Override

	public boolean equals(Object obj) {

		if (this == obj)

			return true;

		if (obj == null)

			return false;

		if (!(obj instanceof OrderHeadPK))

			return false;

		OrderHeadPK other = (OrderHeadPK) obj;

		if (branch_id == null) {

			if (other.branch_id != null)

				return false;

		} else if (!branch_id.equals(other.branch_id))

			return false;

		if (company_id == null) {

			if (other.company_id != null)

				return false;

		} else if (!company_id.equals(other.company_id))

			return false;

		if (customer_id == null) {

			if (other.customer_id != null)

				return false;

		} else if (!customer_id.equals(other.customer_id))

			return false;

		if (order_id == null) {

			if (other.order_id != null)

				return false;

		} else if (!order_id.equals(other.order_id))

			return false;

		return true;

	}

}
 

//***********************************************************************

// OrderItem.java (Entity)

//***********************************************************************

package com.crazy_bytes.business.order.persist;
 

import java.io.Serializable;

import java.util.Calendar;

import java.util.Date;
 

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.IdClass;

import javax.persistence.PrePersist;

import javax.persistence.PreRemove;

import javax.persistence.PreUpdate;

import javax.persistence.Table;

import javax.persistence.Temporal;

import javax.persistence.TemporalType;
 

@Entity

@Table(name = "ORDER_ITEM")

@IdClass(OrderItemPK.class)

@SuppressWarnings("serial")

public class OrderItem implements Serializable {

	/** Represents a company */

	@Column(nullable = false, scale = 2, precision = 0)

	@Id

	private Integer company_id;

	

	/** Represents an office branch */

	@Column(nullable = false, scale = 2, precision = 0)

	@Id

	private Integer branch_id;

	

	/** The id of the order */

	@Column(nullable = false, scale = 8, precision = 0)

	@Id

	private Integer order_id;

	

	/** The position of the item inside the order */

	@Column(nullable = false, scale = 8, precision = 0)

	@Id

	private Integer item_position;

	

	/** The alphanumerical item id */

	@Column(nullable = false, length = 12)

	private String item_id;

	

	/** The alphanumerical item size */

	@Column(length = 4)

	private String item_size;

	

	/** The quantity of the item */

	@Column(nullable = false, scale = 6)

	private Integer quantity;

	

	/** The optional alphanumerical measuremnt, e.g. kg (kilo), km (kilometer), l (liter) */

	@Column(length = 4)

	private String measurement;

	

	/** Optional discount for the item */

	//@Column(scale = 5, precision = 2)

	// Oracle specific

	@Column(scale = 5, precision = 2, columnDefinition = "number(5, 2)")

	private Float discount;

	

	/** User who created or manipulated the item */

	@Column(nullable = false, length = 12)

	private String username;

	

	/** Indicates the type of action that was performed on the item */

	@Column(nullable = false, length = 6)

	private String action;

	

	/** Timestamp which indicates from when this entity is valid */

	@Column(nullable = false)

	@Temporal(TemporalType.TIMESTAMP)

	private Date valid_from;
 

	/** Timestamp which indicates till when this entity is valid */

	@Column(nullable = false)

	@Temporal(TemporalType.TIMESTAMP)

	private Date valid_to;

	

	public OrderItem() {}
 

	/**

	 * @return the company_id

	 */

	public Integer getCompany_id() {

		return company_id;

	}
 

	/**

	 * @param company_id the company_id to set

	 */

	public void setCompany_id(Integer company_id) {

		this.company_id = company_id;

	}
 

	/**

	 * @return the branch_id

	 */

	public Integer getBranch_id() {

		return branch_id;

	}
 

	/**

	 * @param branch_id the branch_id to set

	 */

	public void setBranch_id(Integer branch_id) {

		this.branch_id = branch_id;

	}
 

	/**

	 * @return the order_id

	 */

	public Integer getOrder_id() {

		return order_id;

	}
 

	/**

	 * @param order_id the order_id to set

	 */

	public void setOrder_id(Integer order_id) {

		this.order_id = order_id;

	}
 

	/**

	 * @return the item_position

	 */

	public Integer getItem_position() {

		return item_position;

	}
 

	/**

	 * @param item_position the item_position to set

	 */

	public void setItem_position(Integer item_position) {

		this.item_position = item_position;

	}
 

	/**

	 * @return the item_id

	 */

	public String getItem_id() {

		return item_id;

	}
 

	/**

	 * @param item_id the item_id to set

	 */

	public void setItem_id(String item_id) {

		this.item_id = item_id;

	}
 

	/**

	 * @return the item_size

	 */

	public String getItem_size() {

		return item_size;

	}
 

	/**

	 * @param item_size the item_size to set

	 */

	public void setItem_size(String item_size) {

		this.item_size = item_size;

	}
 

	/**

	 * @return the quantity

	 */

	public Integer getQuantity() {

		return quantity;

	}
 

	/**

	 * @param quantity the quantity to set

	 */

	public void setQuantity(Integer quantity) {

		this.quantity = quantity;

	}
 

	/**

	 * @return the measurement

	 */

	public String getMeasurement() {

		return measurement;

	}
 

	/**

	 * @param measurement the measurement to set

	 */

	public void setMeasurement(String measurement) {

		this.measurement = measurement;

	}
 

	/**

	 * @return the discount

	 */

	public Float getDiscount() {

		return discount;

	}
 

	/**

	 * @param discount the discount to set

	 */

	public void setDiscount(Float discount) {

		this.discount = discount;

	}
 

	/**

	 * @return the username

	 */

	public String getUsername() {

		return username;

	}
 

	/**

	 * @param username the username to set

	 */

	public void setUsername(String username) {

		this.username = username;

	}
 

	/**

	 * @return the action

	 */

	public String getAction() {

		return action;

	}
 

	/**

	 * @param action the action to set

	 */

	public void setAction(String action) {

		this.action = action;

	}
 

	/**

	 * @return the valid_from

	 */

	public Date getValid_from() {

		return valid_from;

	}
 

	/**

	 * @param valid_from the valid_from to set

	 */

	public void setValid_from(Date valid_from) {

		this.valid_from = valid_from;

	}
 

	/**

	 * @return the valid_to

	 */

	public Date getValid_to() {

		return valid_to;

	}
 

	/**

	 * @param valid_to the valid_to to set

	 */

	public void setValid_to(Date valid_to) {

		this.valid_to = valid_to;

	}
 

	/* (non-Javadoc)

	 * @see java.lang.Object#hashCode()

	 */

	@Override

	public int hashCode() {

		final int prime = 31;

		int result = 1;

		result = prime * result	+ ((branch_id == null) ? 0 : branch_id.hashCode());

		result = prime * result	+ ((company_id == null) ? 0 : company_id.hashCode());

		result = prime * result + ((discount == null) ? 0 : discount.hashCode());

		result = prime * result + ((item_id == null) ? 0 : item_id.hashCode());

		result = prime * result	+ ((item_position == null) ? 0 : item_position.hashCode());

		result = prime * result	+ ((item_size == null) ? 0 : item_size.hashCode());

		result = prime * result	+ ((measurement == null) ? 0 : measurement.hashCode());

		result = prime * result	+ ((order_id == null) ? 0 : order_id.hashCode());

		result = prime * result	+ ((quantity == null) ? 0 : quantity.hashCode());

		return result;

	}
 

	/* (non-Javadoc)

	 * @see java.lang.Object#equals(java.lang.Object)

	 */

	@Override

	public boolean equals(Object obj) {

		if (this == obj)

			return true;

		if (obj == null)

			return false;

		if (!(obj instanceof OrderItem))

			return false;

		OrderItem other = (OrderItem) obj;

		if (branch_id == null) {

			if (other.branch_id != null)

				return false;

		} else if (!branch_id.equals(other.branch_id))

			return false;

		if (company_id == null) {

			if (other.company_id != null)

				return false;

		} else if (!company_id.equals(other.company_id))

			return false;

		if (discount == null) {

			if (other.discount != null)

				return false;

		} else if (!discount.equals(other.discount))

			return false;

		if (item_id == null) {

			if (other.item_id != null)

				return false;

		} else if (!item_id.equals(other.item_id))

			return false;

		if (item_position == null) {

			if (other.item_position != null)

				return false;

		} else if (!item_position.equals(other.item_position))

			return false;

		if (item_size == null) {

			if (other.item_size != null)

				return false;

		} else if (!item_size.equals(other.item_size))

			return false;

		if (measurement == null) {

			if (other.measurement != null)

				return false;

		} else if (!measurement.equals(other.measurement))

			return false;

		if (order_id == null) {

			if (other.order_id != null)

				return false;

		} else if (!order_id.equals(other.order_id))

			return false;

		if (quantity == null) {

			if (other.quantity != null)

				return false;

		} else if (!quantity.equals(other.quantity))

			return false;

		return true;

	}

	

	private void setActionCredentials() {

		Calendar calendar = Calendar.getInstance();

		setValid_from(calendar.getTime());

		calendar.set(Calendar.YEAR, 2999);

		calendar.set(Calendar.MONTH, Calendar.DECEMBER);

		calendar.set(Calendar.DAY_OF_MONTH, 31);

		calendar.set(Calendar.HOUR_OF_DAY, 0);

		calendar.set(Calendar.MINUTE, 0);

		calendar.set(Calendar.SECOND, 0);

		calendar.set(Calendar.MILLISECOND, 0);

		setValid_to(calendar.getTime());

		//setUser(ctx.getCallerPrincipal().getName());

	}

	

	@PrePersist

	@SuppressWarnings("unused")

	private void prePersist() {

		setAction("CREATE");

		setActionCredentials();

	}

	

	@PreUpdate

	@SuppressWarnings("unused")

	private void preUpdate() {

		setAction("UPDATE");

		setActionCredentials();

	}
 

	@PreRemove

	@SuppressWarnings("unused")

	private void preRemove() {

		setAction("DELETE");

		setActionCredentials();

	}

}
 

//***********************************************************************

// OrderItemPK.java (Primary Key of OrderItem)

//***********************************************************************

package com.crazy_bytes.business.order.persist;
 

import java.io.Serializable;
 

@SuppressWarnings("serial")

public final class OrderItemPK implements Serializable {

	private Integer company_id;

	private Integer branch_id;

	private Integer order_id;

	private Integer item_position;

	

	public OrderItemPK() {}

	

	/* (non-Javadoc)

	 * @see java.lang.Object#hashCode()

	 */

	@Override

	public int hashCode() {

		final int prime = 31;

		int result = 1;

		result = prime * result	+ ((branch_id == null) ? 0 : branch_id.hashCode());

		result = prime * result	+ ((company_id == null) ? 0 : company_id.hashCode());

		result = prime * result	+ ((item_position == null) ? 0 : item_position.hashCode());

		result = prime * result	+ ((order_id == null) ? 0 : order_id.hashCode());

		return result;

	}

	

	/* (non-Javadoc)

	 * @see java.lang.Object#equals(java.lang.Object)

	 */

	@Override

	public boolean equals(Object obj) {

		if (this == obj)

			return true;

		if (obj == null)

			return false;

		if (!(obj instanceof OrderItemPK))

			return false;

		OrderItemPK other = (OrderItemPK) obj;

		if (branch_id == null) {

			if (other.branch_id != null)

				return false;

		} else if (!branch_id.equals(other.branch_id))

			return false;

		if (company_id == null) {

			if (other.company_id != null)

				return false;

		} else if (!company_id.equals(other.company_id))

			return false;

		if (item_position == null) {

			if (other.item_position != null)

				return false;

		} else if (!item_position.equals(other.item_position))

			return false;

		if (order_id == null) {

			if (other.order_id != null)

				return false;

		} else if (!order_id.equals(other.order_id))

			return false;

		return true;

	}

}
 

//***********************************************************************

// OrderSessionRemote.java (Remote interface of OrderSession)

//***********************************************************************

package com.crazy_bytes.business.order.session;
 

import java.util.Collection;
 

import javax.ejb.Remote;
 

import com.crazy_bytes.business.order.data.Order;
 

@Remote

public interface OrderSessionRemote {

	public Order getOrder(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id);

	public Collection<Order> getOrderHistory(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id);

	public void saveOrder(Order order);

	public void deleteOrder(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id);

}
 

//***********************************************************************

// OrderSession.java (Session bean)

//***********************************************************************

package com.crazy_bytes.business.order.session;
 

import java.util.ArrayList;

import java.util.Collection;

import java.util.Date;
 

import javax.annotation.Resource;

import javax.ejb.SessionContext;

import javax.ejb.Stateless;

import javax.persistence.EntityManager;

import javax.persistence.NoResultException;

import javax.persistence.PersistenceContext;

import javax.persistence.Query;
 

import com.crazy_bytes.business.order.data.Order;

import com.crazy_bytes.business.order.persist.OrderHead;

import com.crazy_bytes.business.order.persist.OrderItem;
 

@Stateless

public class OrderSession implements OrderSessionRemote {

	@PersistenceContext(name = "OrderSession")

	private EntityManager entityManager;

	

	@Resource

	private SessionContext sessionContext;
 

	@Override

	public Order getOrder(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id) {

		Order order = new Order();

		order.setHead(getOrderHead(company_id, branch_id, order_id, customer_id));

		

		try {

			order.getItems().addAll(getOrderItems(company_id, branch_id, order_id, customer_id));

		} catch (NoResultException nre) {

			// It's possible that an order has no items.

		}

		

		return order;

	}

	

	@Override

	public Collection<Order> getOrderHistory(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id) {

		Collection<Order> orderHistory = new ArrayList<Order>();

		Collection<OrderHead> orderHeadHistory = getOrderHeadHistory(company_id, branch_id, order_id, customer_id);

		for(OrderHead orderHead : orderHeadHistory) {

			Order order = new Order();

			order.setHead(orderHead);

			

			try {

				order.getItems().addAll(getOrderItemsHistory(company_id, branch_id, order_id, customer_id, orderHead.getValid_from()));

			} catch (NoResultException nre) {

				// It's possible that an order has no items.

			}

			

			orderHistory.add(order);

		}

		

		return orderHistory;

	}

	

	@Override

	public void saveOrder(Order order) {

		OrderHead newHead = order.getHead();

		newHead.setUsername(getUser());

		Collection<OrderItem> newItems = order.getItems();

		

		try {

			// Try to lookup old order head

			getOrderHead(newHead.getCompany_id(), newHead.getBranch_id(), newHead.getOrder_id(), newHead.getCustomer_id());

			entityManager.merge(newHead);

			

			// To shorten code remove all items an re-persist them

			try {

				Collection<OrderItem> oldItems = getOrderItems(newHead.getCompany_id(), newHead.getBranch_id(), newHead.getOrder_id(), newHead.getCustomer_id());

				for(OrderItem item : oldItems) {

					entityManager.remove(item);

				}

			} catch (NoResultException nre) {

				// It's possible that an order has no items.

			}

			

		} catch (NoResultException nre) {

			// Order is new, persist it.

			entityManager.persist(newHead);

		}

		

		for(OrderItem item : newItems) {

			item.setUsername(getUser());

			entityManager.persist(item);

		}

	}

	

	@Override

	public void deleteOrder(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id) {

		Order order = getOrder(company_id, branch_id, order_id, customer_id);

		Collection<OrderItem> items = order.getItems();

		for(OrderItem item : items) {

			entityManager.remove(item);

		}

		entityManager.remove(order.getHead());

	}

	

	private OrderHead getOrderHead(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id) {

		Query headQuery = entityManager.createQuery("SELECT h FROM OrderHead h " +

													"WHERE company_id = :company_id " +

													"AND branch_id = :branch_id " +

													"AND order_id = :order_id " +

													"AND customer_id = :customer_id");

		headQuery.setParameter("company_id", company_id);

		headQuery.setParameter("branch_id", branch_id);

		headQuery.setParameter("order_id", order_id);

		headQuery.setParameter("customer_id", customer_id);

		

		return (OrderHead) headQuery.getSingleResult();

	}
 

	@SuppressWarnings("unchecked")

	private Collection<OrderItem> getOrderItems(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id) {

		Query itemQuery = entityManager.createQuery("SELECT h FROM OrderItem h " +

													"WHERE company_id = :company_id " +

													"AND branch_id = :branch_id " +

													"AND order_id = :order_id");

		itemQuery.setParameter("company_id", company_id);

		itemQuery.setParameter("branch_id", branch_id);

		itemQuery.setParameter("order_id", order_id);

		

		return (Collection<OrderItem>) itemQuery.getResultList();

	}
 

	@SuppressWarnings("unchecked")

	private Collection<OrderHead> getOrderHeadHistory(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id) {

		Query headQuery = entityManager.createNativeQuery("SELECT * FROM ORDER_HEAD_FULL " +

														  "WHERE company_id = :company_id " +

														  "AND branch_id = :branch_id " +

														  "AND order_id = :order_id " +

														  "ORDER BY valid_from DESC");

		headQuery.setParameter("company_id", company_id);

		headQuery.setParameter("branch_id", branch_id);

		headQuery.setParameter("order_id", order_id);

		

		return (Collection<OrderHead>) headQuery.getResultList();

	}

	

	@SuppressWarnings("unchecked")

	private Collection<OrderItem> getOrderItemsHistory(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id, Date valid_on) {

		Query itemQuery = entityManager.createNativeQuery("SELECT * FROM ORDER_HEAD_FULL " +

														  "WHERE company_id = :company_id " +

														  "AND branch_id = :branch_id " +

														  "AND order_id = :order_id " +

														  "AND valid_from <= :valid_on " +

														  "AND valid_to >= :valid_on " +

														  "ORDER BY order_position ASC");

		itemQuery.setParameter("company_id", company_id);

		itemQuery.setParameter("branch_id", branch_id);

		itemQuery.setParameter("order_id", order_id);

		itemQuery.setParameter("valid_on", valid_on);

			

		return (Collection<OrderItem>) itemQuery.getResultList();

	}

	

	private String getUser() {

		return sessionContext.getCallerPrincipal().getName();

	}

}
 

//***********************************************************************

// TestOrderBean.java (Test-class)

//***********************************************************************

package com.crazy_bytes.test.order;
 

import java.util.Properties;
 

import javax.ejb.EJBException;

import javax.naming.Context;

import javax.naming.InitialContext;
 

import com.crazy_bytes.business.order.data.Order;

import com.crazy_bytes.business.order.persist.OrderHead;

import com.crazy_bytes.business.order.persist.OrderItem;

import com.crazy_bytes.business.order.session.OrderSessionRemote;
 

public class TestOrderBean {

	public static final String SERVER = "localhost";

	public static final String USER = "Test";

	public static final String PWD = "Test";

	public static final String SECURITY_CONFIG = "myrealm";

	public static final String LOCAL_INITIAL_CONTEXT_FACTORY = "org.jnp.interfaces.NamingContextFactory";

	public static final String INITIAL_CONTEXT_FACTORY = "org.jboss.security.jndi.LoginInitialContextFactory";

	public static final String PROVIDER_URL = "jnp://" + SERVER + ":1099";

	

	public static void main(String[] args) {

		try {

			System.setProperty("java.security.auth.login.config","http://" + SERVER + ":7001/auth.conf");

			Properties p = new Properties();

			p.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);

			p.put(Context.PROVIDER_URL, PROVIDER_URL);

			p.put(Context.SECURITY_PRINCIPAL, USER);

			p.put(Context.SECURITY_CREDENTIALS, PWD);

			p.put(Context.SECURITY_PROTOCOL, SECURITY_CONFIG);

			

			System.out.print("Connecting to server \"" + SERVER + "\"...");

			InitialContext ctx = new InitialContext(p);

			System.out.println("OK!");

			

			System.out.print("Obtaining remote interface...");

			OrderSessionRemote rmi = (OrderSessionRemote) ctx.lookup("OrderSession/remote");

			System.out.println("OK!");

			

			System.out.print("Obtaining order...");

			try {

				rmi.getOrder(1, 1, 1, 1);

				System.out.println("OK!");

			} catch (EJBException e) {

				if(e.getMessage().startsWith("javax.persistence.NoResultException")) {

					System.out.println("NOT FOUND!");

				} else {

					throw e;

				}

			}

			

			Order newOrder = new Order();

			OrderHead newHead = TestOrderBean.createOrderHead(1, 1, 1, 1);

			newHead.setHints("My first order.");

			newOrder.setHead(newHead);

			

			OrderItem item = createOrderItem(newOrder);

			item.setItem_id("M-RX-7FD");

			item.setMeasurement("pcs.");

			item.setQuantity(1);

			newOrder.getItems().add(item);

			

			System.out.print("Saving order...");

			rmi.saveOrder(newOrder);

			System.out.println("OK!");
 

			System.out.print("Obtaining order...");

			try {

				rmi.getOrder(1, 1, 1, 1);

				System.out.println("OK!");

			} catch (EJBException e) {

				if(e.getMessage().startsWith("javax.persistence.NoResultException")) {

					System.out.println("NOT FOUND!");

				} else {

					throw e;

				}

			}

			

			System.out.print("Deleting order...");

			rmi.deleteOrder(newHead.getCompany_id(), newHead.getBranch_id(), newHead.getOrder_id(), newHead.getCustomer_id());

			System.out.println("OK!");
 

			System.out.print("Obtaining order...");

			try {

				rmi.getOrder(1, 1, 1, 1);

				System.out.println("OK!");

			} catch (EJBException e) {

				if(e.getMessage().startsWith("javax.persistence.NoResultException")) {

					System.out.println("NOT FOUND!");

				} else {

					throw e;

				}

			}

			

		} catch (Exception e) {

			System.out.println("FAILED!");

			e.printStackTrace();

		}

		

		System.out.println("Quitting program.");

		System.exit(0);

	}

	

	public static OrderHead createOrderHead(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id) {

		OrderHead head = new OrderHead();

		head.setCompany_id(1);

		head.setBranch_id(1);

		head.setOrder_id(1);

		head.setCustomer_id(1);

		return head;

	}

	

	public static OrderItem createOrderItem(Order order) {

		OrderItem item = new OrderItem();

		item.setCompany_id(order.getHead().getCompany_id());

		item.setBranch_id(order.getHead().getBranch_id());

		item.setOrder_id(order.getHead().getOrder_id());

		item.setItem_position(order.getItems().size() + 1);

		return item;

	}

}
 

//***********************************************************************

// META-INF/persistence.xml (Persistence config)

//***********************************************************************

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="1.0"

	xmlns="http://java.sun.com/xml/ns/persistence" 

	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

	

	<persistence-unit name="OrderSession">

		<provider>org.hibernate.ejb.HibernatePersistence</provider>

		<jta-data-source>java:/DB1TX</jta-data-source>

		

		<class>

			com.crazy_bytes.business.order.persist.OrderHead

		</class>

		

		<class>

			com.crazy_bytes.business.order.persist.OrderItem

		</class>

		

		<properties>

            <!--Use the hbm2ddl feature -->

			<property name="hibernate.hbm2ddl.auto" value="create"/>

		</properties>

	</persistence-unit>

	

</persistence>
 

//***********************************************************************

// META-INF/orm.xml (Object relational mapping config)

//***********************************************************************

<?xml version="1.0" encoding="UTF-8"?>

<entity-mappings 

	xmlns="http://java.sun.com/xml/ns/persistence/orm" 

	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" 

	version="1.0">

	

</entity-mappings>

Open in new window

0
 
LVL 2

Accepted Solution

by:
Crazy_Bytes earned 0 total points
Comment Utility
I solved the problem by myself.

 I attached the solution as .zip file. Just remove the .txt extension on every file. I attached the full source as cose snippet in case you don't trust the zip file.

Attention!
Be advised that this solution is optimized for an Oracle database (Oracle 10g in my case,
but I also tested it with Oracle 8g, 9i and 11g). It is possible that due to Oracle specific
functions (Triggers and Views), this code might be not working for other databases like
mySQL, PostgreSQL, etc.

I used a JBoss 4.2.3.GA application server, but since the code is pure java and java ee, it
should run on any server which understands ejb3

I don't claim that this code is perfect, but it works for me. If someone knows a better
or more portable solution, feel free to send me an example.

I hope it helps people who are experience this problem in the future.

Best regards,
CB


//***********************************************************************

// Order.java (Wrapper class for order data)

//***********************************************************************

package com.crazy_bytes.business.order.data;
 

import java.io.Serializable;

import java.util.ArrayList;

import java.util.Collection;
 

import com.crazy_bytes.business.order.persist.OrderHead;

import com.crazy_bytes.business.order.persist.OrderHeadHistory;

import com.crazy_bytes.business.order.persist.OrderItem;

import com.crazy_bytes.business.order.persist.OrderItemHistory;
 

/**

 * Wrapper class for order data.

 */

@SuppressWarnings("serial")

public class Order implements Serializable {

	/** Order head data */

	private OrderHeadProxy head;

	

	/** Collection of order items */

	private Collection<OrderItemProxy> items = new ArrayList<OrderItemProxy>();

	

	public Order(OrderHead head, Collection<OrderItem> items) {

		this.head = new OrderHeadProxy(head);

		for(OrderItem item : items) {

			this.items.add(new OrderItemProxy(item));

		}

	}
 

	public Order(OrderHeadHistory head, Collection<OrderItemHistory> items) {

		this.head = new OrderHeadProxy(head);

		for(OrderItemHistory item : items) {

			this.items.add(new OrderItemProxy(item));

		}

	}
 

	/**

	 * @return the head

	 */

	public OrderHeadProxy getHead() {

		return head;

	}
 

	/**

	 * @return the items

	 */

	public Collection<OrderItemProxy> getItems() {

		return items;

	}
 

	/* (non-Javadoc)

	 * @see java.lang.Object#hashCode()

	 */

	@Override

	public int hashCode() {

		final int prime = 31;

		int result = 1;

		result = prime * result + ((head == null) ? 0 : head.hashCode());

		result = prime * result + ((items == null) ? 0 : items.hashCode());

		return result;

	}
 

	/* (non-Javadoc)

	 * @see java.lang.Object#equals(java.lang.Object)

	 */

	@Override

	public boolean equals(Object obj) {

		if (this == obj)

			return true;

		if (obj == null)

			return false;

		if (!(obj instanceof Order))

			return false;

		Order other = (Order) obj;

		if (head == null) {

			if (other.head != null)

				return false;

		} else if (!head.equals(other.head))

			return false;

		if (items == null) {

			if (other.items != null)

				return false;

		} else if (!items.equals(other.items))

			return false;

		return true;

	}

}
 

//***********************************************************************

// OrderHeadProxy.java (Wrapper class for order head data)

//***********************************************************************

package com.crazy_bytes.business.order.data;
 

import java.io.Serializable;

import java.util.Date;
 

import com.crazy_bytes.business.order.persist.OrderHead;

import com.crazy_bytes.business.order.persist.OrderHeadHistory;
 

/** Wrapper class for the entities {@link OrderHead} and {@link OrderHeadHistory}. */

@SuppressWarnings("serial")

public class OrderHeadProxy implements Serializable {

	/** Represents a company */

	private Integer company_id;

	

	/** Represents an office branch */

	private Integer branch_id;

	

	/** The id of the order */

	private Integer order_id;

	

	/** Represents a customer */

	private Integer customer_id;

	

	/** Optional discount for the entire order */

	private Float discount;

	

	/** Optional hints on the order */

	private String hints;

	

	/** Optional date to indicate a requested date of delivery */

	private Date date_of_delivery;

	

	/** User who created or manipulated the order */

	private String username;

	

	/** Indicates the type of action that was performed on the order */

	private String action;

	

	/** Timestamp which indicates from when this entity is valid */

	private Date valid_from;
 

	/** Timestamp which indicates till when this entity is valid */

	private Date valid_to;

	

	/** Indicates if the contained data is history data. */

	private boolean isHistoryData = false;

	

	public OrderHeadProxy() {}

	

	public OrderHeadProxy(OrderHead head) {

		fillData(head);

	}

	

	public OrderHeadProxy(OrderHeadHistory head) {

		fillData(head);

	}
 

	/**

	 * @return the company_id

	 */

	public Integer getCompany_id() {

		return company_id;

	}
 

	/**

	 * @param company_id the company_id to set

	 */

	public void setCompany_id(Integer company_id) {

		this.company_id = company_id;

	}
 

	/**

	 * @return the branch_id

	 */

	public Integer getBranch_id() {

		return branch_id;

	}
 

	/**

	 * @param branch_id the branch_id to set

	 */

	public void setBranch_id(Integer branch_id) {

		this.branch_id = branch_id;

	}
 

	/**

	 * @return the order_id

	 */

	public Integer getOrder_id() {

		return order_id;

	}
 

	/**

	 * @param order_id the order_id to set

	 */

	public void setOrder_id(Integer order_id) {

		this.order_id = order_id;

	}
 

	/**

	 * @return the customer_id

	 */

	public Integer getCustomer_id() {

		return customer_id;

	}
 

	/**

	 * @param customer_id the customer_id to set

	 */

	public void setCustomer_id(Integer customer_id) {

		this.customer_id = customer_id;

	}
 

	/**

	 * @return the discount

	 */

	public Float getDiscount() {

		return discount;

	}
 

	/**

	 * @param discount the discount to set

	 */

	public void setDiscount(Float discount) {

		this.discount = discount;

	}
 

	/**

	 * @return the hints

	 */

	public String getHints() {

		return hints;

	}
 

	/**

	 * @param hints the hints to set

	 */

	public void setHints(String hints) {

		this.hints = hints;

	}
 

	/**

	 * @return the date_of_delivery

	 */

	public Date getDate_of_delivery() {

		return date_of_delivery;

	}
 

	/**

	 * @param date_of_delivery the date_of_delivery to set

	 */

	public void setDate_of_delivery(Date date_of_delivery) {

		this.date_of_delivery = date_of_delivery;

	}
 

	/**

	 * @return the username

	 */

	public String getUsername() {

		return username;

	}
 

	/**

	 * @param username the username to set

	 */

	public void setUsername(String username) {

		this.username = username;

	}
 

	/**

	 * @return the action

	 */

	public String getAction() {

		return action;

	}
 

	/**

	 * @param action the action to set

	 */

	public void setAction(String action) {

		this.action = action;

	}
 

	/**

	 * @return the valid_from

	 */

	public Date getValid_from() {

		return valid_from;

	}
 

	/**

	 * @param valid_from the valid_from to set

	 */

	public void setValid_from(Date valid_from) {

		this.valid_from = valid_from;

	}
 

	/**

	 * @return the valid_to

	 */

	public Date getValid_to() {

		return valid_to;

	}
 

	/**

	 * @param valid_to the valid_to to set

	 */

	public void setValid_to(Date valid_to) {

		this.valid_to = valid_to;

	}

	

	/** Populates the proxy with data from the {@link OrderHead} entity. */ 

	public void fillData(OrderHead head) {

		this.isHistoryData = false;

		this.company_id = head.getCompany_id();

		this.branch_id = head.getBranch_id();

		this.order_id = head.getOrder_id();

		this.customer_id = head.getCustomer_id();

		this.discount = head.getDiscount();

		this.hints = head.getHints();

		this.date_of_delivery = head.getDate_of_delivery();

		this.username = head.getUsername();

		this.action = head.getAction();

		this.valid_from = head.getValid_from();

		this.valid_to = head.getValid_to();

	}

	

	/** Populates the proxy with data from the {@link OrderHeadHistory} entity. */ 

	public void fillData(OrderHeadHistory head) {

		this.isHistoryData = true;

		this.company_id = head.getCompany_id();

		this.branch_id = head.getBranch_id();

		this.order_id = head.getOrder_id();

		this.customer_id = head.getCustomer_id();

		this.discount = head.getDiscount();

		this.hints = head.getHints();

		this.date_of_delivery = head.getDate_of_delivery();

		this.username = head.getUsername();

		this.action = head.getAction();

		this.valid_from = head.getValid_from();

		this.valid_to = head.getValid_to();

	}
 

	/* (non-Javadoc)

	 * @see java.lang.Object#hashCode()

	 */

	@Override

	public int hashCode() {

		final int prime = 31;

		int result = 1;

		result = prime * result

				+ ((branch_id == null) ? 0 : branch_id.hashCode());

		result = prime * result

				+ ((company_id == null) ? 0 : company_id.hashCode());

		result = prime * result

				+ ((customer_id == null) ? 0 : customer_id.hashCode());

		result = prime

				* result

				+ ((date_of_delivery == null) ? 0 : date_of_delivery.hashCode());

		result = prime * result

				+ ((discount == null) ? 0 : discount.hashCode());

		result = prime * result + ((hints == null) ? 0 : hints.hashCode());

		result = prime * result

				+ ((order_id == null) ? 0 : order_id.hashCode());

		return result;

	}
 

	/* (non-Javadoc)

	 * @see java.lang.Object#equals(java.lang.Object)

	 */

	@Override

	public boolean equals(Object obj) {

		if (this == obj)

			return true;

		if (obj == null)

			return false;

		if (!(obj instanceof OrderHeadProxy))

			return false;

		OrderHeadProxy other = (OrderHeadProxy) obj;

		if (branch_id == null) {

			if (other.branch_id != null)

				return false;

		} else if (!branch_id.equals(other.branch_id))

			return false;

		if (company_id == null) {

			if (other.company_id != null)

				return false;

		} else if (!company_id.equals(other.company_id))

			return false;

		if (customer_id == null) {

			if (other.customer_id != null)

				return false;

		} else if (!customer_id.equals(other.customer_id))

			return false;

		if (date_of_delivery == null) {

			if (other.date_of_delivery != null)

				return false;

		} else if (!date_of_delivery.equals(other.date_of_delivery))

			return false;

		if (discount == null) {

			if (other.discount != null)

				return false;

		} else if (!discount.equals(other.discount))

			return false;

		if (hints == null) {

			if (other.hints != null)

				return false;

		} else if (!hints.equals(other.hints))

			return false;

		if (order_id == null) {

			if (other.order_id != null)

				return false;

		} else if (!order_id.equals(other.order_id))

			return false;

		return true;

	}

	

	/**

	 * Converts a proxy into an {@link OrderHead} entity, throws ClassCastExceptions when data

	 * is history data.

	 */

	public OrderHead toEntity() {

		if(isHistoryData) {

			throw new ClassCastException("Can not convert history data to entity OrderHead!");

		}

		OrderHead head = new OrderHead();

		head.setCompany_id(this.company_id);

		head.setBranch_id(this.branch_id);

		head.setOrder_id(this.order_id);

		head.setCustomer_id(this.customer_id);

		head.setDiscount(this.discount);

		head.setHints(this.hints);

		head.setDate_of_delivery(this.date_of_delivery);

		head.setUsername(this.username);

		head.setAction(this.action);

		head.setValid_from(this.valid_from);

		head.setValid_to(this.valid_to);

		return head;

	}

}
 

//***********************************************************************

// OrderItemProxy.java (Wrapper class for order item data)

//***********************************************************************

package com.crazy_bytes.business.order.data;
 

import java.io.Serializable;

import java.util.Date;
 

import com.crazy_bytes.business.order.persist.OrderItem;

import com.crazy_bytes.business.order.persist.OrderItemHistory;
 

/** Wrapper class for the entities {@link OrderItem} and {@link OrderItemHistory}. */

@SuppressWarnings("serial")

public class OrderItemProxy implements Serializable {

	/** Represents a company */

	private Integer company_id;

	

	/** Represents an office branch */

	private Integer branch_id;

	

	/** The id of the order */

	private Integer order_id;

	

	/** The position of the item inside the order */

	private Integer item_position;

	

	/** The alphanumerical item id */

	private String item_id;

	

	/** The alphanumerical item size */

	private String item_size;

	

	/** The quantity of the item */

	private Integer quantity;

	

	/** The optional alphanumerical measuremnt, e.g. kg (kilo), km (kilometer), l (liter) */

	private String measurement;

	

	/** Optional discount for the item */

	private Float discount;

	

	/** User who created or manipulated the item */

	private String username;

	

	/** Indicates the type of action that was performed on the item */

	private String action;

	

	/** Timestamp which indicates from when this entity is valid */

	private Date valid_from;
 

	/** Timestamp which indicates till when this entity is valid */

	private Date valid_to;

	

	/** Indicates if the contained data is history data. */

	private boolean isHistoryData = false;

	

	public OrderItemProxy() {};

	

	public OrderItemProxy(OrderItem item) {

		fillData(item);

	}

	

	public OrderItemProxy(OrderItemHistory item) {

		fillData(item);

	}
 

	/**

	 * @return the company_id

	 */

	public Integer getCompany_id() {

		return company_id;

	}
 

	/**

	 * @param company_id the company_id to set

	 */

	public void setCompany_id(Integer company_id) {

		this.company_id = company_id;

	}
 

	/**

	 * @return the branch_id

	 */

	public Integer getBranch_id() {

		return branch_id;

	}
 

	/**

	 * @param branch_id the branch_id to set

	 */

	public void setBranch_id(Integer branch_id) {

		this.branch_id = branch_id;

	}
 

	/**

	 * @return the order_id

	 */

	public Integer getOrder_id() {

		return order_id;

	}
 

	/**

	 * @param order_id the order_id to set

	 */

	public void setOrder_id(Integer order_id) {

		this.order_id = order_id;

	}
 

	/**

	 * @return the item_position

	 */

	public Integer getItem_position() {

		return item_position;

	}
 

	/**

	 * @param item_position the item_position to set

	 */

	public void setItem_position(Integer item_position) {

		this.item_position = item_position;

	}
 

	/**

	 * @return the item_id

	 */

	public String getItem_id() {

		return item_id;

	}
 

	/**

	 * @param item_id the item_id to set

	 */

	public void setItem_id(String item_id) {

		this.item_id = item_id;

	}
 

	/**

	 * @return the item_size

	 */

	public String getItem_size() {

		return item_size;

	}
 

	/**

	 * @param item_size the item_size to set

	 */

	public void setItem_size(String item_size) {

		this.item_size = item_size;

	}
 

	/**

	 * @return the quantity

	 */

	public Integer getQuantity() {

		return quantity;

	}
 

	/**

	 * @param quantity the quantity to set

	 */

	public void setQuantity(Integer quantity) {

		this.quantity = quantity;

	}
 

	/**

	 * @return the measurement

	 */

	public String getMeasurement() {

		return measurement;

	}
 

	/**

	 * @param measurement the measurement to set

	 */

	public void setMeasurement(String measurement) {

		this.measurement = measurement;

	}
 

	/**

	 * @return the discount

	 */

	public Float getDiscount() {

		return discount;

	}
 

	/**

	 * @param discount the discount to set

	 */

	public void setDiscount(Float discount) {

		this.discount = discount;

	}
 

	/**

	 * @return the username

	 */

	public String getUsername() {

		return username;

	}
 

	/**

	 * @param username the username to set

	 */

	public void setUsername(String username) {

		this.username = username;

	}
 

	/**

	 * @return the action

	 */

	public String getAction() {

		return action;

	}
 

	/**

	 * @param action the action to set

	 */

	public void setAction(String action) {

		this.action = action;

	}
 

	/**

	 * @return the valid_from

	 */

	public Date getValid_from() {

		return valid_from;

	}
 

	/**

	 * @param valid_from the valid_from to set

	 */

	public void setValid_from(Date valid_from) {

		this.valid_from = valid_from;

	}
 

	/**

	 * @return the valid_to

	 */

	public Date getValid_to() {

		return valid_to;

	}
 

	/**

	 * @param valid_to the valid_to to set

	 */

	public void setValid_to(Date valid_to) {

		this.valid_to = valid_to;

	}

	

	/** Populates the proxy with data from the {@link OrderItem} entity. */ 

	public void fillData(OrderItem item) {

		this.isHistoryData = false;

		this.company_id = item.getCompany_id();

		this.branch_id = item.getBranch_id();

		this.order_id = item.getOrder_id();

		this.item_position = item.getItem_position();

		this.item_id = item.getItem_id();

		this.item_size = item.getItem_size();

		this.quantity = item.getQuantity();

		this.company_id = item.getCompany_id();

		this.measurement = item.getMeasurement();

		this.discount = item.getDiscount();

		this.username = item.getUsername();

		this.action = item.getAction();

		this.valid_from = item.getValid_from();

		this.valid_to = item.getValid_to();

	}

	

	/** Populates the proxy with data from the {@link OrderItemHistory} entity. */ 

	public void fillData(OrderItemHistory item) {

		this.isHistoryData = true;

		this.company_id = item.getCompany_id();

		this.branch_id = item.getBranch_id();

		this.order_id = item.getOrder_id();

		this.item_position = item.getItem_position();

		this.item_id = item.getItem_id();

		this.item_size = item.getItem_size();

		this.quantity = item.getQuantity();

		this.company_id = item.getCompany_id();

		this.measurement = item.getMeasurement();

		this.discount = item.getDiscount();

		this.username = item.getUsername();

		this.action = item.getAction();

		this.valid_from = item.getValid_from();

		this.valid_to = item.getValid_to();

	}
 

	/* (non-Javadoc)

	 * @see java.lang.Object#hashCode()

	 */

	@Override

	public int hashCode() {

		final int prime = 31;

		int result = 1;

		result = prime * result

				+ ((branch_id == null) ? 0 : branch_id.hashCode());

		result = prime * result

				+ ((company_id == null) ? 0 : company_id.hashCode());

		result = prime * result

				+ ((discount == null) ? 0 : discount.hashCode());

		result = prime * result + ((item_id == null) ? 0 : item_id.hashCode());

		result = prime * result

				+ ((item_position == null) ? 0 : item_position.hashCode());

		result = prime * result

				+ ((item_size == null) ? 0 : item_size.hashCode());

		result = prime * result

				+ ((measurement == null) ? 0 : measurement.hashCode());

		result = prime * result

				+ ((order_id == null) ? 0 : order_id.hashCode());

		result = prime * result

				+ ((quantity == null) ? 0 : quantity.hashCode());

		return result;

	}
 

	/* (non-Javadoc)

	 * @see java.lang.Object#equals(java.lang.Object)

	 */

	@Override

	public boolean equals(Object obj) {

		if (this == obj)

			return true;

		if (obj == null)

			return false;

		if (!(obj instanceof OrderItemProxy))

			return false;

		OrderItemProxy other = (OrderItemProxy) obj;

		if (branch_id == null) {

			if (other.branch_id != null)

				return false;

		} else if (!branch_id.equals(other.branch_id))

			return false;

		if (company_id == null) {

			if (other.company_id != null)

				return false;

		} else if (!company_id.equals(other.company_id))

			return false;

		if (discount == null) {

			if (other.discount != null)

				return false;

		} else if (!discount.equals(other.discount))

			return false;

		if (item_id == null) {

			if (other.item_id != null)

				return false;

		} else if (!item_id.equals(other.item_id))

			return false;

		if (item_position == null) {

			if (other.item_position != null)

				return false;

		} else if (!item_position.equals(other.item_position))

			return false;

		if (item_size == null) {

			if (other.item_size != null)

				return false;

		} else if (!item_size.equals(other.item_size))

			return false;

		if (measurement == null) {

			if (other.measurement != null)

				return false;

		} else if (!measurement.equals(other.measurement))

			return false;

		if (order_id == null) {

			if (other.order_id != null)

				return false;

		} else if (!order_id.equals(other.order_id))

			return false;

		if (quantity == null) {

			if (other.quantity != null)

				return false;

		} else if (!quantity.equals(other.quantity))

			return false;

		return true;

	}

	

	/**

	 * Converts a proxy into an {@link OrderItem} entity, throws ClassCastExceptions when data

	 * is history data.

	 */

	public OrderItem toEntity() {

		if(isHistoryData) {

			throw new ClassCastException("Can not convert history data to entity OrderItem!");

		}

		OrderItem item = new OrderItem();

		item.setCompany_id(this.company_id);

		item.setBranch_id(this.branch_id);

		item.setOrder_id(this.order_id);

		item.setItem_position(this.item_position);

		item.setItem_id(this.item_id);

		item.setItem_size(this.item_size);

		item.setQuantity(this.quantity);

		item.setCompany_id(this.company_id);

		item.setMeasurement(this.measurement);

		item.setDiscount(this.discount);

		item.setUsername(this.username);

		item.setAction(this.action);

		item.setValid_from(this.valid_from);

		item.setValid_to(this.valid_to);

		return item;

	}

}
 

//***********************************************************************

// OrderHead.java (Entity)

//***********************************************************************

package com.crazy_bytes.business.order.persist;
 

import java.io.Serializable;

import java.util.Calendar;

import java.util.Date;
 

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.IdClass;

import javax.persistence.PrePersist;

import javax.persistence.PreRemove;

import javax.persistence.PreUpdate;

import javax.persistence.Table;

import javax.persistence.Temporal;

import javax.persistence.TemporalType;
 

@Entity

@Table(name = "ORDER_HEAD")

@IdClass(OrderHeadPK.class)

@SuppressWarnings("serial")

public class OrderHead implements Serializable {

	/** Represents a company */

	@Column(nullable = false, scale = 2, precision = 0)

	@Id

	private Integer company_id;

	

	/** Represents an office branch */

	@Column(nullable = false, scale = 2, precision = 0)

	@Id

	private Integer branch_id;

	

	/** The id of the order */

	@Column(nullable = false, scale = 8, precision = 0)

	@Id

	private Integer order_id;

	

	/** Represents a customer */

	@Column(nullable = false, scale = 2, precision = 0)

	@Id

	private Integer customer_id;

	

	/** Optional discount for the entire order */

	//@Column(scale = 5, precision = 2)

	// Oracle specific

	@Column(scale = 5, precision = 2, columnDefinition = "number(5, 2)")

	private Float discount;

	

	/** Optional hints on the order */

	@Column(length = 1000)

	private String hints;

	

	/** Optional date to indicate a requested date of delivery */

	@Temporal(TemporalType.DATE)

	private Date date_of_delivery;

	

	/** User who created or manipulated the order */

	@Column(nullable = false, length = 12)

	private String username;

	

	/** Indicates the type of action that was performed on the order */

	@Column(nullable = false, length = 6)

	private String action;

	

	/** Timestamp which indicates from when this entity is valid */

	@Column(nullable = false)

	@Temporal(TemporalType.TIMESTAMP)

	private Date valid_from;
 

	/** Timestamp which indicates till when this entity is valid */

	@Column(nullable = false)

	@Temporal(TemporalType.TIMESTAMP)

	private Date valid_to;

	

	public OrderHead() {}
 

	/**

	 * @return the company_id

	 */

	public Integer getCompany_id() {

		return company_id;

	}
 

	/**

	 * @param company_id the company_id to set

	 */

	public void setCompany_id(Integer company_id) {

		this.company_id = company_id;

	}
 

	/**

	 * @return the branch_id

	 */

	public Integer getBranch_id() {

		return branch_id;

	}
 

	/**

	 * @param branch_id the branch_id to set

	 */

	public void setBranch_id(Integer branch_id) {

		this.branch_id = branch_id;

	}
 

	/**

	 * @return the order_id

	 */

	public Integer getOrder_id() {

		return order_id;

	}
 

	/**

	 * @param order_id the order_id to set

	 */

	public void setOrder_id(Integer order_id) {

		this.order_id = order_id;

	}
 

	/**

	 * @return the customer_id

	 */

	public Integer getCustomer_id() {

		return customer_id;

	}
 

	/**

	 * @param customer_id the customer_id to set

	 */

	public void setCustomer_id(Integer customer_id) {

		this.customer_id = customer_id;

	}
 

	/**

	 * @return the discount

	 */

	public Float getDiscount() {

		return discount;

	}
 

	/**

	 * @param discount the discount to set

	 */

	public void setDiscount(Float discount) {

		this.discount = discount;

	}
 

	/**

	 * @return the hints

	 */

	public String getHints() {

		return hints;

	}
 

	/**

	 * @param hints the hints to set

	 */

	public void setHints(String hints) {

		this.hints = hints;

	}
 

	/**

	 * @return the date_of_delivery

	 */

	public Date getDate_of_delivery() {

		return date_of_delivery;

	}
 

	/**

	 * @param date_of_delivery the date_of_delivery to set

	 */

	public void setDate_of_delivery(Date date_of_delivery) {

		this.date_of_delivery = date_of_delivery;

	}
 

	/**

	 * @return the username

	 */

	public String getUsername() {

		return username;

	}
 

	/**

	 * @param username the username to set

	 */

	public void setUsername(String username) {

		this.username = username;

	}
 

	/**

	 * @return the action

	 */

	public String getAction() {

		return action;

	}
 

	/**

	 * @param action the action to set

	 */

	public void setAction(String action) {

		this.action = action;

	}
 

	/**

	 * @return the valid_from

	 */

	public Date getValid_from() {

		return valid_from;

	}
 

	/**

	 * @param valid_from the valid_from to set

	 */

	public void setValid_from(Date valid_from) {

		this.valid_from = valid_from;

	}
 

	/**

	 * @return the valid_to

	 */

	public Date getValid_to() {

		return valid_to;

	}
 

	/**

	 * @param valid_to the valid_to to set

	 */

	public void setValid_to(Date valid_to) {

		this.valid_to = valid_to;

	}
 

	/* (non-Javadoc)

	 * @see java.lang.Object#hashCode()

	 */

	@Override

	public int hashCode() {

		final int prime = 31;

		int result = 1;

		result = prime * result	+ ((branch_id == null) ? 0 : branch_id.hashCode());

		result = prime * result	+ ((company_id == null) ? 0 : company_id.hashCode());

		result = prime * result	+ ((customer_id == null) ? 0 : customer_id.hashCode());

		result = prime * result + ((date_of_delivery == null) ? 0 : date_of_delivery.hashCode());

		result = prime * result + ((discount == null) ? 0 : discount.hashCode());

		result = prime * result + ((hints == null) ? 0 : hints.hashCode());

		result = prime * result + ((order_id == null) ? 0 : order_id.hashCode());

		return result;

	}
 

	/* (non-Javadoc)

	 * @see java.lang.Object#equals(java.lang.Object)

	 */

	@Override

	public boolean equals(Object obj) {

		if (this == obj)

			return true;

		if (obj == null)

			return false;

		if (!(obj instanceof OrderHead))

			return false;

		OrderHead other = (OrderHead) obj;

		if (branch_id == null) {

			if (other.branch_id != null)

				return false;

		} else if (!branch_id.equals(other.branch_id))

			return false;

		if (company_id == null) {

			if (other.company_id != null)

				return false;

		} else if (!company_id.equals(other.company_id))

			return false;

		if (customer_id == null) {

			if (other.customer_id != null)

				return false;

		} else if (!customer_id.equals(other.customer_id))

			return false;

		if (date_of_delivery == null) {

			if (other.date_of_delivery != null)

				return false;

		} else if (!date_of_delivery.equals(other.date_of_delivery))

			return false;

		if (discount == null) {

			if (other.discount != null)

				return false;

		} else if (!discount.equals(other.discount))

			return false;

		if (hints == null) {

			if (other.hints != null)

				return false;

		} else if (!hints.equals(other.hints))

			return false;

		if (order_id == null) {

			if (other.order_id != null)

				return false;

		} else if (!order_id.equals(other.order_id))

			return false;

		return true;

	}
 

	private void setActionCredentials() {

		Calendar calendar = Calendar.getInstance();

		setValid_from(calendar.getTime());

		calendar.set(Calendar.YEAR, 2999);

		calendar.set(Calendar.MONTH, Calendar.DECEMBER);

		calendar.set(Calendar.DAY_OF_MONTH, 31);

		calendar.set(Calendar.HOUR_OF_DAY, 0);

		calendar.set(Calendar.MINUTE, 0);

		calendar.set(Calendar.SECOND, 0);

		calendar.set(Calendar.MILLISECOND, 0);

		setValid_to(calendar.getTime());

		//setUser(ctx.getCallerPrincipal().getName());

	}

	

	@PrePersist

	@SuppressWarnings("unused")

	private void prePersist() {

		setAction("CREATE");

		setActionCredentials();

	}

	

	@PreUpdate

	@SuppressWarnings("unused")

	private void preUpdate() {

		setAction("UPDATE");

		setActionCredentials();

	}
 

	@PreRemove

	@SuppressWarnings("unused")

	private void preRemove() {

		setAction("DELETE");

		setActionCredentials();

	}

}
 

//***********************************************************************

// OrderHeadHistory.java (Entity)

//***********************************************************************

package com.crazy_bytes.business.order.persist;
 

import java.io.Serializable;

import java.util.Date;
 

import javax.ejb.CreateException;

import javax.ejb.FinderException;

import javax.ejb.RemoveException;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.IdClass;

import javax.persistence.NamedNativeQuery;

import javax.persistence.PrePersist;

import javax.persistence.PreRemove;

import javax.persistence.PreUpdate;

import javax.persistence.Table;

import javax.persistence.Temporal;

import javax.persistence.TemporalType;
 

@NamedNativeQuery(name = "OrderHeadHistory.historyData",

		query = "SELECT COMPANY_ID, BRANCH_ID, ORDER_ID, CUSTOMER_ID, " +

				"DISCOUNT, HINTS, DATE_OF_DELIVERY, USERNAME, ACTION, " +

				"VALID_FROM, VALID_TO " +

				"FROM ORDER_HEAD_FULL " +

				"WHERE COMPANY_ID = :company_id " +

				"AND BRANCH_ID = :branch_id " +

				"AND ORDER_ID = :order_id " +

				"AND CUSTOMER_ID = :customer_id " +

				"ORDER BY VALID_FROM DESC",

		resultClass = OrderHeadHistory.class)
 

@Entity

@Table(name = "ORDER_HEAD_HISTORY")

@IdClass(OrderHeadHistoryPK.class)

@SuppressWarnings("serial")

public class OrderHeadHistory implements Serializable {

	/** Represents a company */

	@Column(nullable = false, scale = 2, precision = 0)

	@Id

	private Integer company_id;

	

	/** Represents an office branch */

	@Column(nullable = false, scale = 2, precision = 0)

	@Id

	private Integer branch_id;

	

	/** The id of the order */

	@Column(nullable = false, scale = 8, precision = 0)

	@Id

	private Integer order_id;

	

	/** Represents a customer */

	@Column(nullable = false, scale = 2, precision = 0)

	@Id

	private Integer customer_id;

	

	/** Optional discount for the entire order */

	//@Column(scale = 5, precision = 2)

	// Oracle specific

	@Column(scale = 5, precision = 2, columnDefinition = "number(5, 2)")

	private Float discount;

	

	/** Optional hints on the order */

	@Column(length = 1000)

	private String hints;

	

	/** Optional date to indicate a requested date of delivery */

	@Temporal(TemporalType.DATE)

	private Date date_of_delivery;

	

	/** User who created or manipulated the order */

	@Column(nullable = false, length = 12)

	private String username;

	

	/** Indicates the type of action that was performed on the order */

	@Column(nullable = false, length = 6)

	private String action;

	

	/** Timestamp which indicates from when this entity is valid */

	@Column(nullable = false)

	@Id

	@Temporal(TemporalType.TIMESTAMP)

	private Date valid_from;
 

	/** Timestamp which indicates till when this entity is valid */

	@Column(nullable = false)

	@Temporal(TemporalType.TIMESTAMP)

	private Date valid_to;

	

	public OrderHeadHistory() {}
 

	/**

	 * @return the company_id

	 */

	public Integer getCompany_id() {

		return company_id;

	}
 

	/**

	 * @param company_id the company_id to set

	 */

	public void setCompany_id(Integer company_id) {

		this.company_id = company_id;

	}
 

	/**

	 * @return the branch_id

	 */

	public Integer getBranch_id() {

		return branch_id;

	}
 

	/**

	 * @param branch_id the branch_id to set

	 */

	public void setBranch_id(Integer branch_id) {

		this.branch_id = branch_id;

	}
 

	/**

	 * @return the order_id

	 */

	public Integer getOrder_id() {

		return order_id;

	}
 

	/**

	 * @param order_id the order_id to set

	 */

	public void setOrder_id(Integer order_id) {

		this.order_id = order_id;

	}
 

	/**

	 * @return the customer_id

	 */

	public Integer getCustomer_id() {

		return customer_id;

	}
 

	/**

	 * @param customer_id the customer_id to set

	 */

	public void setCustomer_id(Integer customer_id) {

		this.customer_id = customer_id;

	}
 

	/**

	 * @return the discount

	 */

	public Float getDiscount() {

		return discount;

	}
 

	/**

	 * @param discount the discount to set

	 */

	public void setDiscount(Float discount) {

		this.discount = discount;

	}
 

	/**

	 * @return the hints

	 */

	public String getHints() {

		return hints;

	}
 

	/**

	 * @param hints the hints to set

	 */

	public void setHints(String hints) {

		this.hints = hints;

	}
 

	/**

	 * @return the date_of_delivery

	 */

	public Date getDate_of_delivery() {

		return date_of_delivery;

	}
 

	/**

	 * @param date_of_delivery the date_of_delivery to set

	 */

	public void setDate_of_delivery(Date date_of_delivery) {

		this.date_of_delivery = date_of_delivery;

	}
 

	/**

	 * @return the username

	 */

	public String getUsername() {

		return username;

	}
 

	/**

	 * @param username the username to set

	 */

	public void setUsername(String username) {

		this.username = username;

	}
 

	/**

	 * @return the action

	 */

	public String getAction() {

		return action;

	}
 

	/**

	 * @param action the action to set

	 */

	public void setAction(String action) {

		this.action = action;

	}
 

	/**

	 * @return the valid_from

	 */

	public Date getValid_from() {

		return valid_from;

	}
 

	/**

	 * @param valid_from the valid_from to set

	 */

	public void setValid_from(Date valid_from) {

		this.valid_from = valid_from;

	}
 

	/**

	 * @return the valid_to

	 */

	public Date getValid_to() {

		return valid_to;

	}
 

	/**

	 * @param valid_to the valid_to to set

	 */

	public void setValid_to(Date valid_to) {

		this.valid_to = valid_to;

	}
 

	/* (non-Javadoc)

	 * @see java.lang.Object#hashCode()

	 */

	@Override

	public int hashCode() {

		final int prime = 31;

		int result = 1;

		result = prime * result	+ ((branch_id == null) ? 0 : branch_id.hashCode());

		result = prime * result	+ ((company_id == null) ? 0 : company_id.hashCode());

		result = prime * result	+ ((customer_id == null) ? 0 : customer_id.hashCode());

		result = prime * result + ((date_of_delivery == null) ? 0 : date_of_delivery.hashCode());

		result = prime * result + ((discount == null) ? 0 : discount.hashCode());

		result = prime * result + ((hints == null) ? 0 : hints.hashCode());

		result = prime * result + ((order_id == null) ? 0 : order_id.hashCode());

		return result;

	}
 

	/* (non-Javadoc)

	 * @see java.lang.Object#equals(java.lang.Object)

	 */

	@Override

	public boolean equals(Object obj) {

		if (this == obj)

			return true;

		if (obj == null)

			return false;

		if (!(obj instanceof OrderHeadHistory))

			return false;

		OrderHeadHistory other = (OrderHeadHistory) obj;

		if (branch_id == null) {

			if (other.branch_id != null)

				return false;

		} else if (!branch_id.equals(other.branch_id))

			return false;

		if (company_id == null) {

			if (other.company_id != null)

				return false;

		} else if (!company_id.equals(other.company_id))

			return false;

		if (customer_id == null) {

			if (other.customer_id != null)

				return false;

		} else if (!customer_id.equals(other.customer_id))

			return false;

		if (date_of_delivery == null) {

			if (other.date_of_delivery != null)

				return false;

		} else if (!date_of_delivery.equals(other.date_of_delivery))

			return false;

		if (discount == null) {

			if (other.discount != null)

				return false;

		} else if (!discount.equals(other.discount))

			return false;

		if (hints == null) {

			if (other.hints != null)

				return false;

		} else if (!hints.equals(other.hints))

			return false;

		if (order_id == null) {

			if (other.order_id != null)

				return false;

		} else if (!order_id.equals(other.order_id))

			return false;

		return true;

	}
 

	@PrePersist

	@SuppressWarnings("unused")

	private void prePersist() throws CreateException {

		throw new CreateException("History data can not be persisted!");

	}

	

	@PreUpdate

	@SuppressWarnings("unused")

	private void preUpdate() throws FinderException {

		throw new FinderException("History data can not be updated!");

	}
 

	@PreRemove

	@SuppressWarnings("unused")

	private void preRemove() throws RemoveException {

		throw new RemoveException("History data can not be removed!");

	}

}
 

//***********************************************************************

// OrderHeadHistoryPK.java (Primary key class for entity

// OrderHeadHistory)

//***********************************************************************

/**

 * 

 */

package com.crazy_bytes.business.order.persist;
 

import java.io.Serializable;

import java.util.Date;
 

@SuppressWarnings("serial")

public final class OrderHeadHistoryPK implements Serializable {

	private Integer company_id;

	private Integer branch_id;

	private Integer order_id;

	private Integer customer_id;

	private Date valid_from;

	

	public OrderHeadHistoryPK() {}

	

	/* (non-Javadoc)

	 * @see java.lang.Object#hashCode()

	 */

	@Override

	public int hashCode() {

		final int prime = 31;

		int result = 1;

		result = prime * result	+ ((branch_id == null) ? 0 : branch_id.hashCode());

		result = prime * result	+ ((company_id == null) ? 0 : company_id.hashCode());

		result = prime * result	+ ((customer_id == null) ? 0 : customer_id.hashCode());

		result = prime * result	+ ((order_id == null) ? 0 : order_id.hashCode());

		result = prime * result	+ ((valid_from == null) ? 0 : valid_from.hashCode());

		return result;

	}

	

	/* (non-Javadoc)

	 * @see java.lang.Object#equals(java.lang.Object)

	 */

	@Override

	public boolean equals(Object obj) {

		if (this == obj)

			return true;

		if (obj == null)

			return false;

		if (!(obj instanceof OrderHeadHistoryPK))

			return false;

		OrderHeadHistoryPK other = (OrderHeadHistoryPK) obj;

		if (branch_id == null) {

			if (other.branch_id != null)

				return false;

		} else if (!branch_id.equals(other.branch_id))

			return false;

		if (company_id == null) {

			if (other.company_id != null)

				return false;

		} else if (!company_id.equals(other.company_id))

			return false;

		if (customer_id == null) {

			if (other.customer_id != null)

				return false;

		} else if (!customer_id.equals(other.customer_id))

			return false;

		if (order_id == null) {

			if (other.order_id != null)

				return false;

		} else if (!order_id.equals(other.order_id))

			return false;

		if (valid_from == null) {

			if (other.valid_from != null)

				return false;

		} else if (!valid_from.equals(other.valid_from))

			return false;

		return true;

	}

}
 

//***********************************************************************

// OrderHeadPK.java (Primary key class for entity OrderHead)

//***********************************************************************

/**

 * 

 */

package com.crazy_bytes.business.order.persist;
 

import java.io.Serializable;
 

@SuppressWarnings("serial")

public final class OrderHeadPK implements Serializable {

	private Integer company_id;

	private Integer branch_id;

	private Integer order_id;

	private Integer customer_id;

	

	public OrderHeadPK() {}

	

	/* (non-Javadoc)

	 * @see java.lang.Object#hashCode()

	 */

	@Override

	public int hashCode() {

		final int prime = 31;

		int result = 1;

		result = prime * result	+ ((branch_id == null) ? 0 : branch_id.hashCode());

		result = prime * result	+ ((company_id == null) ? 0 : company_id.hashCode());

		result = prime * result	+ ((customer_id == null) ? 0 : customer_id.hashCode());

		result = prime * result	+ ((order_id == null) ? 0 : order_id.hashCode());

		return result;

	}

	

	/* (non-Javadoc)

	 * @see java.lang.Object#equals(java.lang.Object)

	 */

	@Override

	public boolean equals(Object obj) {

		if (this == obj)

			return true;

		if (obj == null)

			return false;

		if (!(obj instanceof OrderHeadPK))

			return false;

		OrderHeadPK other = (OrderHeadPK) obj;

		if (branch_id == null) {

			if (other.branch_id != null)

				return false;

		} else if (!branch_id.equals(other.branch_id))

			return false;

		if (company_id == null) {

			if (other.company_id != null)

				return false;

		} else if (!company_id.equals(other.company_id))

			return false;

		if (customer_id == null) {

			if (other.customer_id != null)

				return false;

		} else if (!customer_id.equals(other.customer_id))

			return false;

		if (order_id == null) {

			if (other.order_id != null)

				return false;

		} else if (!order_id.equals(other.order_id))

			return false;

		return true;

	}

}
 

//***********************************************************************

// OrderItem.java (Entity)

//***********************************************************************

package com.crazy_bytes.business.order.persist;
 

import java.io.Serializable;

import java.util.Calendar;

import java.util.Date;
 

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.IdClass;

import javax.persistence.PrePersist;

import javax.persistence.PreRemove;

import javax.persistence.PreUpdate;

import javax.persistence.Table;

import javax.persistence.Temporal;

import javax.persistence.TemporalType;
 

@Entity

@Table(name = "ORDER_ITEM")

@IdClass(OrderItemPK.class)

@SuppressWarnings("serial")

public class OrderItem implements Serializable {

	/** Represents a company */

	@Column(nullable = false, scale = 2, precision = 0)

	@Id

	private Integer company_id;

	

	/** Represents an office branch */

	@Column(nullable = false, scale = 2, precision = 0)

	@Id

	private Integer branch_id;

	

	/** The id of the order */

	@Column(nullable = false, scale = 8, precision = 0)

	@Id

	private Integer order_id;

	

	/** The position of the item inside the order */

	@Column(nullable = false, scale = 8, precision = 0)

	@Id

	private Integer item_position;

	

	/** The alphanumerical item id */

	@Column(nullable = false, length = 12)

	private String item_id;

	

	/** The alphanumerical item size */

	@Column(length = 4)

	private String item_size;

	

	/** The quantity of the item */

	@Column(nullable = false, scale = 6)

	private Integer quantity;

	

	/** The optional alphanumerical measuremnt, e.g. kg (kilo), km (kilometer), l (liter) */

	@Column(length = 4)

	private String measurement;

	

	/** Optional discount for the item */

	//@Column(scale = 5, precision = 2)

	// Oracle specific

	@Column(scale = 5, precision = 2, columnDefinition = "number(5, 2)")

	private Float discount;

	

	/** User who created or manipulated the item */

	@Column(nullable = false, length = 12)

	private String username;

	

	/** Indicates the type of action that was performed on the item */

	@Column(nullable = false, length = 6)

	private String action;

	

	/** Timestamp which indicates from when this entity is valid */

	@Column(nullable = false)

	@Temporal(TemporalType.TIMESTAMP)

	private Date valid_from;
 

	/** Timestamp which indicates till when this entity is valid */

	@Column(nullable = false)

	@Temporal(TemporalType.TIMESTAMP)

	private Date valid_to;

	

	public OrderItem() {}
 

	/**

	 * @return the company_id

	 */

	public Integer getCompany_id() {

		return company_id;

	}
 

	/**

	 * @param company_id the company_id to set

	 */

	public void setCompany_id(Integer company_id) {

		this.company_id = company_id;

	}
 

	/**

	 * @return the branch_id

	 */

	public Integer getBranch_id() {

		return branch_id;

	}
 

	/**

	 * @param branch_id the branch_id to set

	 */

	public void setBranch_id(Integer branch_id) {

		this.branch_id = branch_id;

	}
 

	/**

	 * @return the order_id

	 */

	public Integer getOrder_id() {

		return order_id;

	}
 

	/**

	 * @param order_id the order_id to set

	 */

	public void setOrder_id(Integer order_id) {

		this.order_id = order_id;

	}
 

	/**

	 * @return the item_position

	 */

	public Integer getItem_position() {

		return item_position;

	}
 

	/**

	 * @param item_position the item_position to set

	 */

	public void setItem_position(Integer item_position) {

		this.item_position = item_position;

	}
 

	/**

	 * @return the item_id

	 */

	public String getItem_id() {

		return item_id;

	}
 

	/**

	 * @param item_id the item_id to set

	 */

	public void setItem_id(String item_id) {

		this.item_id = item_id;

	}
 

	/**

	 * @return the item_size

	 */

	public String getItem_size() {

		return item_size;

	}
 

	/**

	 * @param item_size the item_size to set

	 */

	public void setItem_size(String item_size) {

		this.item_size = item_size;

	}
 

	/**

	 * @return the quantity

	 */

	public Integer getQuantity() {

		return quantity;

	}
 

	/**

	 * @param quantity the quantity to set

	 */

	public void setQuantity(Integer quantity) {

		this.quantity = quantity;

	}
 

	/**

	 * @return the measurement

	 */

	public String getMeasurement() {

		return measurement;

	}
 

	/**

	 * @param measurement the measurement to set

	 */

	public void setMeasurement(String measurement) {

		this.measurement = measurement;

	}
 

	/**

	 * @return the discount

	 */

	public Float getDiscount() {

		return discount;

	}
 

	/**

	 * @param discount the discount to set

	 */

	public void setDiscount(Float discount) {

		this.discount = discount;

	}
 

	/**

	 * @return the username

	 */

	public String getUsername() {

		return username;

	}
 

	/**

	 * @param username the username to set

	 */

	public void setUsername(String username) {

		this.username = username;

	}
 

	/**

	 * @return the action

	 */

	public String getAction() {

		return action;

	}
 

	/**

	 * @param action the action to set

	 */

	public void setAction(String action) {

		this.action = action;

	}
 

	/**

	 * @return the valid_from

	 */

	public Date getValid_from() {

		return valid_from;

	}
 

	/**

	 * @param valid_from the valid_from to set

	 */

	public void setValid_from(Date valid_from) {

		this.valid_from = valid_from;

	}
 

	/**

	 * @return the valid_to

	 */

	public Date getValid_to() {

		return valid_to;

	}
 

	/**

	 * @param valid_to the valid_to to set

	 */

	public void setValid_to(Date valid_to) {

		this.valid_to = valid_to;

	}
 

	/* (non-Javadoc)

	 * @see java.lang.Object#hashCode()

	 */

	@Override

	public int hashCode() {

		final int prime = 31;

		int result = 1;

		result = prime * result	+ ((branch_id == null) ? 0 : branch_id.hashCode());

		result = prime * result	+ ((company_id == null) ? 0 : company_id.hashCode());

		result = prime * result + ((discount == null) ? 0 : discount.hashCode());

		result = prime * result + ((item_id == null) ? 0 : item_id.hashCode());

		result = prime * result	+ ((item_position == null) ? 0 : item_position.hashCode());

		result = prime * result	+ ((item_size == null) ? 0 : item_size.hashCode());

		result = prime * result	+ ((measurement == null) ? 0 : measurement.hashCode());

		result = prime * result	+ ((order_id == null) ? 0 : order_id.hashCode());

		result = prime * result	+ ((quantity == null) ? 0 : quantity.hashCode());

		return result;

	}
 

	/* (non-Javadoc)

	 * @see java.lang.Object#equals(java.lang.Object)

	 */

	@Override

	public boolean equals(Object obj) {

		if (this == obj)

			return true;

		if (obj == null)

			return false;

		if (!(obj instanceof OrderItem))

			return false;

		OrderItem other = (OrderItem) obj;

		if (branch_id == null) {

			if (other.branch_id != null)

				return false;

		} else if (!branch_id.equals(other.branch_id))

			return false;

		if (company_id == null) {

			if (other.company_id != null)

				return false;

		} else if (!company_id.equals(other.company_id))

			return false;

		if (discount == null) {

			if (other.discount != null)

				return false;

		} else if (!discount.equals(other.discount))

			return false;

		if (item_id == null) {

			if (other.item_id != null)

				return false;

		} else if (!item_id.equals(other.item_id))

			return false;

		if (item_position == null) {

			if (other.item_position != null)

				return false;

		} else if (!item_position.equals(other.item_position))

			return false;

		if (item_size == null) {

			if (other.item_size != null)

				return false;

		} else if (!item_size.equals(other.item_size))

			return false;

		if (measurement == null) {

			if (other.measurement != null)

				return false;

		} else if (!measurement.equals(other.measurement))

			return false;

		if (order_id == null) {

			if (other.order_id != null)

				return false;

		} else if (!order_id.equals(other.order_id))

			return false;

		if (quantity == null) {

			if (other.quantity != null)

				return false;

		} else if (!quantity.equals(other.quantity))

			return false;

		return true;

	}

	

	private void setActionCredentials() {

		Calendar calendar = Calendar.getInstance();

		setValid_from(calendar.getTime());

		calendar.set(Calendar.YEAR, 2999);

		calendar.set(Calendar.MONTH, Calendar.DECEMBER);

		calendar.set(Calendar.DAY_OF_MONTH, 31);

		calendar.set(Calendar.HOUR_OF_DAY, 0);

		calendar.set(Calendar.MINUTE, 0);

		calendar.set(Calendar.SECOND, 0);

		calendar.set(Calendar.MILLISECOND, 0);

		setValid_to(calendar.getTime());

		//setUser(ctx.getCallerPrincipal().getName());

	}

	

	@PrePersist

	@SuppressWarnings("unused")

	private void prePersist() {

		setAction("CREATE");

		setActionCredentials();

	}

	

	@PreUpdate

	@SuppressWarnings("unused")

	private void preUpdate() {

		setAction("UPDATE");

		setActionCredentials();

	}
 

	@PreRemove

	@SuppressWarnings("unused")

	private void preRemove() {

		setAction("DELETE");

		setActionCredentials();

	}

}
 

//***********************************************************************

// OrderHeadHistory.java (Entity)

//***********************************************************************

package com.crazy_bytes.business.order.persist;
 

import java.io.Serializable;

import java.util.Date;
 

import javax.ejb.CreateException;

import javax.ejb.FinderException;

import javax.ejb.RemoveException;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.IdClass;

import javax.persistence.NamedNativeQuery;

import javax.persistence.PrePersist;

import javax.persistence.PreRemove;

import javax.persistence.PreUpdate;

import javax.persistence.Table;

import javax.persistence.Temporal;

import javax.persistence.TemporalType;
 

@NamedNativeQuery(name = "OrderItemHistory.historyData",

		query = "SELECT COMPANY_ID, BRANCH_ID, ORDER_ID, ITEM_POSITION, " +

				"ITEM_ID, ITEM_SIZE, QUANTITY, MEASUREMENT , DISCOUNT " +

				"USERNAME, ACTION, VALID_FROM, VALID_TO " +

				"FROM ORDER_ITEM_FULL " +

				"WHERE COMPANY_ID = :company_id " +

				"AND BRANCH_ID = :branch_id " +

				"AND ORDER_ID = :order_id " +

				"AND VALID_FROM <= :valid_on " +

				"AND VALID_TO >= :valid_on " +

				"ORDER BY ITEM_POSITION",

		resultClass = OrderItemHistory.class)
 

@Entity

@Table(name = "ORDER_ITEM_HISTORY")

@IdClass(OrderItemPK.class)

@SuppressWarnings("serial")

public class OrderItemHistory implements Serializable {

	/** Represents a company */

	@Column(nullable = false, scale = 2, precision = 0)

	@Id

	private Integer company_id;

	

	/** Represents an office branch */

	@Column(nullable = false, scale = 2, precision = 0)

	@Id

	private Integer branch_id;

	

	/** The id of the order */

	@Column(nullable = false, scale = 8, precision = 0)

	@Id

	private Integer order_id;

	

	/** The position of the item inside the order */

	@Column(nullable = false, scale = 8, precision = 0)

	@Id

	private Integer item_position;

	

	/** The alphanumerical item id */

	@Column(nullable = false, length = 12)

	private String item_id;

	

	/** The alphanumerical item size */

	@Column(length = 4)

	private String item_size;

	

	/** The quantity of the item */

	@Column(nullable = false, scale = 6)

	private Integer quantity;

	

	/** The optional alphanumerical measuremnt, e.g. kg (kilo), km (kilometer), l (liter) */

	@Column(length = 4)

	private String measurement;

	

	/** Optional discount for the item */

	//@Column(scale = 5, precision = 2)

	// Oracle specific

	@Column(scale = 5, precision = 2, columnDefinition = "number(5, 2)")

	private Float discount;

	

	/** User who created or manipulated the item */

	@Column(nullable = false, length = 12)

	private String username;

	

	/** Indicates the type of action that was performed on the item */

	@Column(nullable = false, length = 6)

	private String action;

	

	/** Timestamp which indicates from when this entity is valid */

	@Column(nullable = false)

	@Id

	@Temporal(TemporalType.TIMESTAMP)

	private Date valid_from;
 

	/** Timestamp which indicates till when this entity is valid */

	@Column(nullable = false)

	@Temporal(TemporalType.TIMESTAMP)

	private Date valid_to;

	

	public OrderItemHistory() {}
 

	/**

	 * @return the company_id

	 */

	public Integer getCompany_id() {

		return company_id;

	}
 

	/**

	 * @param company_id the company_id to set

	 */

	public void setCompany_id(Integer company_id) {

		this.company_id = company_id;

	}
 

	/**

	 * @return the branch_id

	 */

	public Integer getBranch_id() {

		return branch_id;

	}
 

	/**

	 * @param branch_id the branch_id to set

	 */

	public void setBranch_id(Integer branch_id) {

		this.branch_id = branch_id;

	}
 

	/**

	 * @return the order_id

	 */

	public Integer getOrder_id() {

		return order_id;

	}
 

	/**

	 * @param order_id the order_id to set

	 */

	public void setOrder_id(Integer order_id) {

		this.order_id = order_id;

	}
 

	/**

	 * @return the item_position

	 */

	public Integer getItem_position() {

		return item_position;

	}
 

	/**

	 * @param item_position the item_position to set

	 */

	public void setItem_position(Integer item_position) {

		this.item_position = item_position;

	}
 

	/**

	 * @return the item_id

	 */

	public String getItem_id() {

		return item_id;

	}
 

	/**

	 * @param item_id the item_id to set

	 */

	public void setItem_id(String item_id) {

		this.item_id = item_id;

	}
 

	/**

	 * @return the item_size

	 */

	public String getItem_size() {

		return item_size;

	}
 

	/**

	 * @param item_size the item_size to set

	 */

	public void setItem_size(String item_size) {

		this.item_size = item_size;

	}
 

	/**

	 * @return the quantity

	 */

	public Integer getQuantity() {

		return quantity;

	}
 

	/**

	 * @param quantity the quantity to set

	 */

	public void setQuantity(Integer quantity) {

		this.quantity = quantity;

	}
 

	/**

	 * @return the measurement

	 */

	public String getMeasurement() {

		return measurement;

	}
 

	/**

	 * @param measurement the measurement to set

	 */

	public void setMeasurement(String measurement) {

		this.measurement = measurement;

	}
 

	/**

	 * @return the discount

	 */

	public Float getDiscount() {

		return discount;

	}
 

	/**

	 * @param discount the discount to set

	 */

	public void setDiscount(Float discount) {

		this.discount = discount;

	}
 

	/**

	 * @return the username

	 */

	public String getUsername() {

		return username;

	}
 

	/**

	 * @param username the username to set

	 */

	public void setUsername(String username) {

		this.username = username;

	}
 

	/**

	 * @return the action

	 */

	public String getAction() {

		return action;

	}
 

	/**

	 * @param action the action to set

	 */

	public void setAction(String action) {

		this.action = action;

	}
 

	/**

	 * @return the valid_from

	 */

	public Date getValid_from() {

		return valid_from;

	}
 

	/**

	 * @param valid_from the valid_from to set

	 */

	public void setValid_from(Date valid_from) {

		this.valid_from = valid_from;

	}
 

	/**

	 * @return the valid_to

	 */

	public Date getValid_to() {

		return valid_to;

	}
 

	/**

	 * @param valid_to the valid_to to set

	 */

	public void setValid_to(Date valid_to) {

		this.valid_to = valid_to;

	}
 

	/* (non-Javadoc)

	 * @see java.lang.Object#hashCode()

	 */

	@Override

	public int hashCode() {

		final int prime = 31;

		int result = 1;

		result = prime * result	+ ((branch_id == null) ? 0 : branch_id.hashCode());

		result = prime * result	+ ((company_id == null) ? 0 : company_id.hashCode());

		result = prime * result + ((discount == null) ? 0 : discount.hashCode());

		result = prime * result + ((item_id == null) ? 0 : item_id.hashCode());

		result = prime * result	+ ((item_position == null) ? 0 : item_position.hashCode());

		result = prime * result	+ ((item_size == null) ? 0 : item_size.hashCode());

		result = prime * result	+ ((measurement == null) ? 0 : measurement.hashCode());

		result = prime * result	+ ((order_id == null) ? 0 : order_id.hashCode());

		result = prime * result	+ ((quantity == null) ? 0 : quantity.hashCode());

		return result;

	}
 

	/* (non-Javadoc)

	 * @see java.lang.Object#equals(java.lang.Object)

	 */

	@Override

	public boolean equals(Object obj) {

		if (this == obj)

			return true;

		if (obj == null)

			return false;

		if (!(obj instanceof OrderItemHistory))

			return false;

		OrderItemHistory other = (OrderItemHistory) obj;

		if (branch_id == null) {

			if (other.branch_id != null)

				return false;

		} else if (!branch_id.equals(other.branch_id))

			return false;

		if (company_id == null) {

			if (other.company_id != null)

				return false;

		} else if (!company_id.equals(other.company_id))

			return false;

		if (discount == null) {

			if (other.discount != null)

				return false;

		} else if (!discount.equals(other.discount))

			return false;

		if (item_id == null) {

			if (other.item_id != null)

				return false;

		} else if (!item_id.equals(other.item_id))

			return false;

		if (item_position == null) {

			if (other.item_position != null)

				return false;

		} else if (!item_position.equals(other.item_position))

			return false;

		if (item_size == null) {

			if (other.item_size != null)

				return false;

		} else if (!item_size.equals(other.item_size))

			return false;

		if (measurement == null) {

			if (other.measurement != null)

				return false;

		} else if (!measurement.equals(other.measurement))

			return false;

		if (order_id == null) {

			if (other.order_id != null)

				return false;

		} else if (!order_id.equals(other.order_id))

			return false;

		if (quantity == null) {

			if (other.quantity != null)

				return false;

		} else if (!quantity.equals(other.quantity))

			return false;

		return true;

	}

	

	@PrePersist

	@SuppressWarnings("unused")

	private void prePersist() throws CreateException {

		throw new CreateException("History data can not be persisted!");

	}

	

	@PreUpdate

	@SuppressWarnings("unused")

	private void preUpdate() throws FinderException {

		throw new FinderException("History data can not be updated!");

	}
 

	@PreRemove

	@SuppressWarnings("unused")

	private void preRemove() throws RemoveException {

		throw new RemoveException("History data can not be removed!");

	}

}
 

//***********************************************************************

// OrderItemHistoryPK.java (Primaray key class for entity

// OrderItemHistory)

//***********************************************************************

/**

 * 

 */

package com.crazy_bytes.business.order.persist;
 

import java.io.Serializable;

import java.util.Date;
 

@SuppressWarnings("serial")

public final class OrderItemHistoryPK implements Serializable {

	private Integer company_id;

	private Integer branch_id;

	private Integer order_id;

	private Integer item_position;

	private Date valid_from;

	

	public OrderItemHistoryPK() {}

	

	/* (non-Javadoc)

	 * @see java.lang.Object#hashCode()

	 */

	@Override

	public int hashCode() {

		final int prime = 31;

		int result = 1;

		result = prime * result	+ ((branch_id == null) ? 0 : branch_id.hashCode());

		result = prime * result	+ ((company_id == null) ? 0 : company_id.hashCode());

		result = prime * result	+ ((item_position == null) ? 0 : item_position.hashCode());

		result = prime * result	+ ((order_id == null) ? 0 : order_id.hashCode());

		result = prime * result	+ ((valid_from == null) ? 0 : valid_from.hashCode());

		return result;

	}

	

	/* (non-Javadoc)

	 * @see java.lang.Object#equals(java.lang.Object)

	 */

	@Override

	public boolean equals(Object obj) {

		if (this == obj)

			return true;

		if (obj == null)

			return false;

		if (!(obj instanceof OrderItemHistoryPK))

			return false;

		OrderItemHistoryPK other = (OrderItemHistoryPK) obj;

		if (branch_id == null) {

			if (other.branch_id != null)

				return false;

		} else if (!branch_id.equals(other.branch_id))

			return false;

		if (company_id == null) {

			if (other.company_id != null)

				return false;

		} else if (!company_id.equals(other.company_id))

			return false;

		if (item_position == null) {

			if (other.item_position != null)

				return false;

		} else if (!item_position.equals(other.item_position))

			return false;

		if (order_id == null) {

			if (other.order_id != null)

				return false;

		} else if (!order_id.equals(other.order_id))

			return false;

		if (valid_from == null) {

			if (other.valid_from != null)

				return false;

		} else if (!valid_from.equals(other.valid_from))

			return false;

		return true;

	}

}
 

//***********************************************************************

// OrderItemPK.java (Primary key class for entity OrderItem)

//***********************************************************************

/**

 * 

 */

package com.crazy_bytes.business.order.persist;
 

import java.io.Serializable;
 

@SuppressWarnings("serial")

public final class OrderItemPK implements Serializable {

	private Integer company_id;

	private Integer branch_id;

	private Integer order_id;

	private Integer item_position;

	

	public OrderItemPK() {}

	

	/* (non-Javadoc)

	 * @see java.lang.Object#hashCode()

	 */

	@Override

	public int hashCode() {

		final int prime = 31;

		int result = 1;

		result = prime * result	+ ((branch_id == null) ? 0 : branch_id.hashCode());

		result = prime * result	+ ((company_id == null) ? 0 : company_id.hashCode());

		result = prime * result	+ ((item_position == null) ? 0 : item_position.hashCode());

		result = prime * result	+ ((order_id == null) ? 0 : order_id.hashCode());

		return result;

	}

	

	/* (non-Javadoc)

	 * @see java.lang.Object#equals(java.lang.Object)

	 */

	@Override

	public boolean equals(Object obj) {

		if (this == obj)

			return true;

		if (obj == null)

			return false;

		if (!(obj instanceof OrderItemPK))

			return false;

		OrderItemPK other = (OrderItemPK) obj;

		if (branch_id == null) {

			if (other.branch_id != null)

				return false;

		} else if (!branch_id.equals(other.branch_id))

			return false;

		if (company_id == null) {

			if (other.company_id != null)

				return false;

		} else if (!company_id.equals(other.company_id))

			return false;

		if (item_position == null) {

			if (other.item_position != null)

				return false;

		} else if (!item_position.equals(other.item_position))

			return false;

		if (order_id == null) {

			if (other.order_id != null)

				return false;

		} else if (!order_id.equals(other.order_id))

			return false;

		return true;

	}

}
 

//***********************************************************************

// OrderSession.java (Stateless session bean)

//***********************************************************************

package com.crazy_bytes.business.order.session;
 

import java.util.ArrayList;

import java.util.Collection;

import java.util.Date;
 

import javax.annotation.Resource;

import javax.ejb.SessionContext;

import javax.ejb.Stateless;

import javax.persistence.EntityManager;

import javax.persistence.NoResultException;

import javax.persistence.PersistenceContext;

import javax.persistence.Query;
 

import com.crazy_bytes.business.order.data.Order;

import com.crazy_bytes.business.order.data.OrderItemProxy;

import com.crazy_bytes.business.order.persist.OrderHead;

import com.crazy_bytes.business.order.persist.OrderHeadHistory;

import com.crazy_bytes.business.order.persist.OrderItem;

import com.crazy_bytes.business.order.persist.OrderItemHistory;
 

@Stateless

public class OrderSession implements OrderSessionRemote {

	@PersistenceContext(name = "OrderSession")

	private EntityManager entityManager;

	

	@Resource

	private SessionContext sessionContext;
 

	@Override

	public Order getOrder(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id) {

		OrderHead head = getOrderHead(company_id, branch_id, order_id, customer_id);

		Collection<OrderItem> items = new ArrayList<OrderItem>();

		try {

			items = getOrderItems(head.getCompany_id(), head.getBranch_id(), head.getOrder_id(), head.getCustomer_id());

		} catch (NoResultException nre) {

			// It's possible that an order has no items.

		}

		

		return new Order(head, items);

	}

	

	@Override

	public Collection<Order> getOrderHistory(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id) {

		Collection<Order> orderHistory = new ArrayList<Order>();

		Collection<OrderHeadHistory> orderHeadHistory = getOrderHeadHistory(company_id, branch_id, order_id, customer_id);

		for(OrderHeadHistory orderHead : orderHeadHistory) {

			Collection<OrderItemHistory> orderItems = new ArrayList<OrderItemHistory>();

			try {

				orderItems = getOrderItemsHistory(orderHead.getCompany_id(), orderHead.getBranch_id(), orderHead.getOrder_id(), orderHead.getCustomer_id(), orderHead.getValid_from());

			} catch (NoResultException nre) {

				// It's possible that an order has no items.

			}

			

			orderHistory.add(new Order(orderHead, orderItems));

		}

		

		return orderHistory;

	}

	

	@Override

	public void saveOrder(Order order) {

		OrderHead newHead = order.getHead().toEntity();

		newHead.setUsername(getUser());

		Collection<OrderItem> newItems = new ArrayList<OrderItem>();

		for(OrderItemProxy item: order.getItems()) {

			newItems.add(item.toEntity());

		}

		

		try {

			// Try to lookup old order head

			getOrderHead(newHead.getCompany_id(), newHead.getBranch_id(), newHead.getOrder_id(), newHead.getCustomer_id());

			entityManager.merge(newHead);

			

			// To shorten code remove all items an re-persist them

			try {

				Collection<OrderItem> oldItems = getOrderItems(newHead.getCompany_id(), newHead.getBranch_id(), newHead.getOrder_id(), newHead.getCustomer_id());

				for(OrderItem item : oldItems) {

					entityManager.remove(item);

				}

			} catch (NoResultException nre) {

				// It's possible that an order has no items.

			}

			

		} catch (NoResultException nre) {

			// Order is new, persist it.

			entityManager.persist(newHead);

		}

		

		for(OrderItem item : newItems) {

			item.setUsername(getUser());

			entityManager.persist(item);

		}

	}

	

	@Override

	public void deleteOrder(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id) {

		OrderHead head = getOrderHead(company_id, branch_id, order_id, customer_id);

		Collection<OrderItem> items = getOrderItems(company_id, branch_id, order_id, customer_id);

		for(OrderItem item : items) {

			entityManager.remove(item);

		}

		entityManager.remove(head);

	}

	

	private OrderHead getOrderHead(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id) {

		Query headQuery = entityManager.createQuery("SELECT h FROM OrderHead h " +

													"WHERE company_id = :company_id " +

													"AND branch_id = :branch_id " +

													"AND order_id = :order_id " +

													"AND customer_id = :customer_id");

		headQuery.setParameter("company_id", company_id);

		headQuery.setParameter("branch_id", branch_id);

		headQuery.setParameter("order_id", order_id);

		headQuery.setParameter("customer_id", customer_id);

		

		return (OrderHead) headQuery.getSingleResult();

	}
 

	@SuppressWarnings("unchecked")

	private Collection<OrderItem> getOrderItems(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id) {

		Query itemQuery = entityManager.createQuery("SELECT h FROM OrderItem h " +

													"WHERE company_id = :company_id " +

													"AND branch_id = :branch_id " +

													"AND order_id = :order_id");

		itemQuery.setParameter("company_id", company_id);

		itemQuery.setParameter("branch_id", branch_id);

		itemQuery.setParameter("order_id", order_id);

		

		return (Collection<OrderItem>) itemQuery.getResultList();

	}
 

	@SuppressWarnings("unchecked")

	private Collection<OrderHeadHistory> getOrderHeadHistory(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id) {

		Query headQuery = entityManager.createNamedQuery("OrderHeadHistory.historyData");

		headQuery.setParameter("company_id", company_id);

		headQuery.setParameter("branch_id", branch_id);

		headQuery.setParameter("order_id", order_id);

		headQuery.setParameter("customer_id", customer_id);

		

		return (Collection<OrderHeadHistory>) headQuery.getResultList();

	}

	

	@SuppressWarnings("unchecked")

	private Collection<OrderItemHistory> getOrderItemsHistory(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id, Date valid_on) {

		Query itemQuery = entityManager.createNamedQuery("OrderItemHistory.historyData");

		itemQuery.setParameter("company_id", company_id);

		itemQuery.setParameter("branch_id", branch_id);

		itemQuery.setParameter("order_id", order_id);

		itemQuery.setParameter("valid_on", valid_on);

			

		return (Collection<OrderItemHistory>) itemQuery.getResultList();

	}

	

	private String getUser() {

		return sessionContext.getCallerPrincipal().getName();

	}

}
 

//***********************************************************************

// OrderSessionRemote.java (Remote interface of the session bean

// OrderSession)

//***********************************************************************

package com.crazy_bytes.business.order.session;
 

import java.util.Collection;
 

import javax.ejb.Remote;
 

import com.crazy_bytes.business.order.data.Order;
 

@Remote

public interface OrderSessionRemote {

	public Order getOrder(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id);

	public Collection<Order> getOrderHistory(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id);

	public void saveOrder(Order order);

	public void deleteOrder(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id);

}
 

//***********************************************************************

// TestOrderBean.java (Local test class that simulate some

// client activity)

//***********************************************************************

package com.crazy_bytes.test.order;
 

import java.util.ArrayList;

import java.util.Collection;

import java.util.Properties;
 

import javax.ejb.EJBException;

import javax.naming.Context;

import javax.naming.InitialContext;
 

import com.crazy_bytes.business.order.data.Order;

import com.crazy_bytes.business.order.persist.OrderHead;

import com.crazy_bytes.business.order.persist.OrderItem;

import com.crazy_bytes.business.order.session.OrderSessionRemote;
 

public class TestOrderBean {

	public static final String SERVER = "localhost";

	public static final String USER = "Test";

	public static final String PWD = "Test";

	public static final String SECURITY_CONFIG = "myrealm";

	public static final String LOCAL_INITIAL_CONTEXT_FACTORY = "org.jnp.interfaces.NamingContextFactory";

	public static final String INITIAL_CONTEXT_FACTORY = "org.jboss.security.jndi.LoginInitialContextFactory";

	public static final String PROVIDER_URL = "jnp://" + SERVER + ":1099";

	

	public static void main(String[] args) {

		try {

			System.setProperty("java.security.auth.login.config","http://" + SERVER + ":7001/auth.conf");

			Properties p = new Properties();

			p.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);

			p.put(Context.PROVIDER_URL, PROVIDER_URL);

			p.put(Context.SECURITY_PRINCIPAL, USER);

			p.put(Context.SECURITY_CREDENTIALS, PWD);

			p.put(Context.SECURITY_PROTOCOL, SECURITY_CONFIG);

			

			System.out.print("Connecting to server \"" + SERVER + "\"...");

			InitialContext ctx = new InitialContext(p);

			System.out.println("OK!");

			

			System.out.print("Obtaining remote interface...");

			OrderSessionRemote rmi = (OrderSessionRemote) ctx.lookup("OrderSession/remote");

			System.out.println("OK!");

			

			System.out.print("Obtaining order...");

			try {

				rmi.getOrder(1, 1, 1, 1);

				System.out.println("OK!");

			} catch (EJBException e) {

				if(e.getMessage().startsWith("javax.persistence.NoResultException")) {

					System.out.println("NOT FOUND!");

				} else {

					throw e;

				}

			}

			

			OrderHead newHead = TestOrderBean.createOrderHead(1, 1, 1, 1);

			newHead.setHints("My first order.");

			

			Collection<OrderItem> items = new ArrayList<OrderItem>();

			OrderItem item = createOrderItem(newHead);

			item.setItem_position(items.size() + 1);

			item.setItem_id("M-RX-7FD");

			item.setMeasurement("pcs.");

			item.setQuantity(1);

			items.add(item);

			Order newOrder = new Order(newHead, items);

			

			System.out.print("Saving order...");

			rmi.saveOrder(newOrder);

			System.out.println("OK!");
 

			System.out.print("Obtaining order...");

			try {

				rmi.getOrder(1, 1, 1, 1);

				System.out.println("OK!");

			} catch (EJBException e) {

				if(e.getMessage().startsWith("javax.persistence.NoResultException")) {

					System.out.println("NOT FOUND!");

				} else {

					throw e;

				}

			}

			

			System.out.print("Deleting order...");

			rmi.deleteOrder(newHead.getCompany_id(), newHead.getBranch_id(), newHead.getOrder_id(), newHead.getCustomer_id());

			System.out.println("OK!");
 

			System.out.print("Obtaining order...");

			try {

				rmi.getOrder(1, 1, 1, 1);

				System.out.println("OK!");

			} catch (EJBException e) {

				if(e.getMessage().startsWith("javax.persistence.NoResultException")) {

					System.out.println("NOT FOUND!");

				} else {

					throw e;

				}

			}

			

		} catch (Exception e) {

			System.out.println("FAILED!");

			e.printStackTrace();

		}

		

		System.out.println("Quitting program.");

		System.exit(0);

	}

	

	public static OrderHead createOrderHead(Integer company_id, Integer branch_id, Integer order_id, Integer customer_id) {

		OrderHead head = new OrderHead();

		head.setCompany_id(1);

		head.setBranch_id(1);

		head.setOrder_id(1);

		head.setCustomer_id(1);

		return head;

	}

	

	public static OrderItem createOrderItem(OrderHead head) {

		OrderItem item = new OrderItem();

		item.setCompany_id(head.getCompany_id());

		item.setBranch_id(head.getBranch_id());

		item.setOrder_id(head.getOrder_id());

		return item;

	}

}

Open in new window

OrderBean.zip
0
 
LVL 2

Author Comment

by:Crazy_Bytes
Comment Utility
For the sake of completeness I post the the Triggers and View statements for the Oracle database and the xml-config file for the OrderBean (which are already included in the zip-file).

The four sql statements should be executed after the Order bean was deployed the first time, since the  required tables will be created by the entity beans during deployment.

Best reagrds,
CB
.
/***************************************************************************

*** Begin of the Oracle sql statements.

***************************************************************************/

-- Trigger for ORDER_HEAD.

-- Fires each time a row is updated or deleted.

CREATE OR REPLACE TRIGGER CB.ORDER_HEAD_BEFOREROW

    BEFORE UPDATE OR DELETE ON CB.ORDER_HEAD

    FOR EACH ROW

DECLARE

    my_exception EXCEPTION;
 

BEGIN

    IF DELETING

    OR UPDATING

    THEN

        INSERT INTO

            CB.ORDER_HEAD_HISTORY

        VALUES (

            :OLD.COMPANY_ID,

            :OLD.BRANCH_ID,

            :OLD.ORDER_ID,

            :OLD.CUSTOMER_ID,

            :OLD.DISCOUNT,

            :OLD.HINTS,

            :OLD.DATE_OF_DELIVERY,

            :OLD.USERNAME,

            :OLD.ACTION,

            :OLD.VALID_FROM,

            SYSDATE

        );

                       

    END IF;
 

EXCEPTION

    WHEN my_exception THEN

         RAISE_APPLICATION_ERROR(-20350, LOCALTIMESTAMP);

END ORDER_HEAD_BEFOREROW;
 

-- Trigger for ORDER_ITEM.

-- Fires each time a row is updated or deleted.

CREATE OR REPLACE TRIGGER CB.ORDER_ITEM_BEFOREROW

    BEFORE UPDATE OR DELETE ON CB.ORDER_ITEM

    FOR EACH ROW

DECLARE

    my_exception EXCEPTION;
 

BEGIN

    IF DELETING

    OR UPDATING

    THEN

        INSERT INTO

            CB.ORDER_ITEM_HISTORY

        VALUES (

            :OLD.COMPANY_ID,

            :OLD.BRANCH_ID,

            :OLD.ORDER_ID,

            :OLD.ITEM_POSITION,

            :OLD.ITEM_ID,

            :OLD.ITEM_SIZE,

            :OLD.QUANTITY,

            :OLD.MEASUREMENT,

            :OLD.DISCOUNT,

            :OLD.USERNAME,

            :OLD.ACTION,

            :OLD.VALID_FROM,

            SYSDATE

        );

                       

    END IF;
 

EXCEPTION

    WHEN my_exception THEN

         RAISE_APPLICATION_ERROR(-20350, LOCALTIMESTAMP);

END ORDER_ITEM_BEFOREROW;
 

-- View that cumulates the data from ORDER_HEAD and

-- ORDER_HEAD_HISTORY.

CREATE OR REPLACE FORCE VIEW CB.ORDER_HEAD_FULL

AS

SELECT * FROM ORDER_HEAD

UNION

SELECT * FROM ORDER_HEAD_HISTORY;
 

-- View that cumulates the data from ORDER_ITEM and

-- ORDER_ITEM_HISTORY.

CREATE OR REPLACE FORCE VIEW CB.ORDER_ITEM_FULL

AS

SELECT * FROM ORDER_ITEM

UNION

SELECT * FROM ORDER_ITEM_HISTORY;
 

****************************************************************************

*** Begin of the xml-config files

****************************************************************************
 

****************************************************************************

*** META-INF/orm.xml (Config for object relational mapping)

****************************************************************************

<?xml version="1.0" encoding="UTF-8"?>

<entity-mappings 

	xmlns="http://java.sun.com/xml/ns/persistence/orm" 

	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" 

	version="1.0">

	

</entity-mappings>
 

****************************************************************************

*** META-INF/persistence.xml (Config for entity persistence)

****************************************************************************

<persistence version="1.0"

	xmlns="http://java.sun.com/xml/ns/persistence" 

	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

	

	<persistence-unit name="OrderSession">

		<provider>org.hibernate.ejb.HibernatePersistence</provider>

		<!-- DB1TX is the name of the adat source inside the

		     application server -->

		<jta-data-source>java:/DB1TX</jta-data-source>

		

		<class>

			com.crazy_bytes.business.order.persist.OrderHead

		</class>

		<class>

			com.crazy_bytes.business.order.persist.OrderItem

		</class>

		<class>

			com.crazy_bytes.business.order.persist.OrderHeadHistory

		</class>

		<class>

			com.crazy_bytes.business.order.persist.OrderItemHistory

		</class>

		

		<properties>

            <!-- Use the hbm2ddl feature to create the required

                 tables if they do not exist -->

			<property name="hibernate.hbm2ddl.auto" value="create"/>

		</properties>

	</persistence-unit>

	

</persistence>

Open in new window

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now