public class ContextHelper 
{
	private static GenericXmlApplicationContext ctx = null;
	
	public static GenericXmlApplicationContext getApplicationContext()
	{
		if( ctx == null)
		{
			ctx = new GenericXmlApplicationContext();
			ctx.load("classpath:app-context.xml");
			ctx.refresh();
		}
		
		return ctx;
	}
	
} public class DatabaseHelper
{
	
    public static IService getDatabase()
	{

    	IService db = (IService) ContextHelper.getApplicationContext().getBean( "service" );
    	if (db == null)
    	{
    		System.out.println("Service is null");
    	}
		return db;

	}
	
} public interface IAddressDao extends IDao<IAddress>
{	
	public List<IAddress> findAll();

	public IAddress findById(int id);
	
} public interface IAuthorDao extends IDao<IAuthor>
{
	public List<IAuthor> findAll();

	public IAuthor findById(int id);

	public List<IAuthor> findBySoundexLname(String keyword);
	
} public interface ICcXactsDao extends IDao<ICcXacts> 
{

	int getLastCcXactsId();

	public ICcXacts findById(Integer id);
} public interface ICountryDao extends IDao<ICountry>
{

	public ICountry findById(int id);

	public ICountry getByName(String country);
	
	public void createTable();

} public interface ICustomerDao extends IDao<ICustomer> 
{

	public ICustomer findById(Integer id);

	public ICustomer getUserBy(String username, String password);

	public List<ICustomer> findByAddress(IAddress addrId);

	

} public interface IItemDao extends IDao<IItem>
{
	public List<IItem> findAll();
	public IItem findById(int id);
	public List<IItem> getPromotional();
	public List<IItem> getNewProducts(String category);
	public List<Object[]> getBestSellers(String category);
	public IItem getRandomItem();
	public IItem getObject();
	public List<IItem> findAllByAuthor(IAuthor author);
	public List<IItem> findAllByTitle(String keyword);
	public List<IItem> findAllBySubject(String keyword);
} public interface IOrderLineDao extends IDao<IOrderLine> 
{

	List<IOrderLine> findAllByOrder(IOrders orders);
	IOrderLine findById(Integer id);

} public interface IOrdersDao extends IDao<IOrders> 
{
	public IOrders getMostRecentOrder(ICustomer customer);

	public IOrders findById(Integer cxOId);
} public interface IShoppingCartDao extends IDao<IShoppingCart> 
{
//	public Integer createEmptyCart();

	public IShoppingCart findById(Integer shoppingId);

	public List<IShoppingCartLine> findAllBySC(IShoppingCart shoppingCart);
} public interface IShoppingCartLineDao extends IDao<IShoppingCartLine> 
{

	public IShoppingCartLine getBySCandItem(Integer shoppingId, int itemId);

	public void delete(IShoppingCartLine bySCandItem);

	public boolean isCartEmpty(int scId);

	public List<Object[]> findBySCId(Integer shoppingId);

} public interface IAddress
{

	public Integer getAddrId();

	public void setAddrId(Integer addrId);

	public  ICountry getCountry();

	public void setCountry(ICountry country);

	public String getAddrStreet1();

	public void setAddrStreet1(String addrStreet1);

	public String getAddrStreet2();

	public void setAddrStreet2(String addrStreet2);

	public String getAddrCity();

	public void setAddrCity(String addrCity);

	public String getAddrState();

	public void setAddrState(String addrState);

	public String getAddrZip();

	public void setAddrZip(String addrZip);

	public Set<ICustomer> getCustomers();

} public interface IAuthor
{

	public Integer getAId();

	public void setAId(Integer AId);

	public String getAFname();

	public void setAFname(String AFname);

	public String getALname();

	public void setALname(String ALname);

	public String getAMname();

	public void setAMname(String AMname);

	public Date getADob();

	public void setADob(Date ADob);

	public String getABio();

	public void setABio(String ABio);

} public interface ICountry
{

	public Integer getCoId();

	public void setCoId(Integer coId);

	public String getCoName();

	public void setCoName(String coName);

	public Double getCoExchange();

	public void setCoExchange(Double coExchange);

	public String getCoCurrency();

	public void setCoCurrency(String coCurrency);

} public interface ICustomer
{

	public Integer getCId();

	public void setCId(Integer CId);

	public IAddress getAddress();

	public void setAddress(IAddress address);

	public String getCUname();

	public void setCUname(String CUname);

	public String getCPasswd();

	public void setCPasswd(String CPasswd);

	public String getCFname();

	public void setCFname(String CFname);

	public String getCLname();

	public void setCLname(String CLname);

	public String getCPhone();

	public void setCData(String CData);

	public String getCData();

	public void setCBirthdate(Date CBirthdate);

	public Date getCBirthdate();

	public void setCYtdPmt(Double c_YTD_PMT);

	public Double getCYtdPmt();

	public void setCBalance(Double c_BALANCE);

	public Double getCBalance();

	public void setCDiscount(double c_DISCOUNT);

	public Double getCDiscount();

	public void setCExpiration(Date CExpiration);

	public Date getCExpiration();

	public void setCLogin(Date CLogin);

	public Date getCLogin();

	public void setCLastVisit(Date CLastVisit);

	public Date getCLastVisit();

	public void setCSince(Date CSince);

	public Date getCSince();

	public void setCEmail(String CEmail);

	public String getCEmail();

	public void setCPhone(String CPhone);
} public interface IItem
{

	public Integer getIId();

	public void setIDimension(String IDimension);

	public String getIDimension();

	public void setIBacking(String IBacking);

	public String getIBacking();

	public void setIPage(String IPage);

	public String getIPage();

	public void setIIsbn(String IIsbn);

	public String getIIsbn();

	public void setIStock(Integer IStock);

	public Integer getIStock();

	public void setIAvail(Date IAvail);

	public Date getIAvail();

	public void setICost(Double i_COST);

	public Double getICost();

	public void setISrp(Double i_SRP);

	public Double getISrp();

	public void setIImage(String IImage);

	public String getIImage();

	public void setIThumbnail(String IThumbnail);

	public String getIThumbnail();

	public void setIRelated5(Integer IRelated5);

	public Integer getIRelated5();

	public void setIRelated4(Integer IRelated4);

	public Integer getIRelated4();

	public void setIRelated3(Integer IRelated3);

	public Integer getIRelated3();

	public void setIRelated2(Integer IRelated2);

	public Integer getIRelated2();

	public void setIRelated1(Integer IRelated1);

	public Integer getIRelated1();

	public void setIDesc(String IDesc);

	public String getIDesc();

	public void setISubject(String ISubject);

	public String getISubject();

	public void setIPublisher(String IPublisher);

	public String getIPublisher();

	public void setIPubDate(Date IPubDate);

	public Date getIPubDate();

	public void setITitle(String ITitle);

	public String getITitle();

	public void setAuthor(IAuthor author);

	public IAuthor getAuthor();

	public void setIId(Integer IId);
	
	public double getIRandom();
	
	public void setIRandom(double num);
} public interface IService 
{
	
    public List getNewProducts(String category);

	public IShoppingCart doCart(IShoppingCart sc, Integer itemId,
            List<Integer> ids, List<Integer> quantities);

	public BuyConfirmResult doBuyConfirm(Integer shoppingId, Integer customerId,
            String ccType, long ccNumber, String ccName, Date ccExpiry,
            String shipping, String street1, String street2, String city,
            String state, String zip, String country);

	public BuyConfirmResult doBuyConfirm(Integer shoppingId, Integer customerId,
            String ccType, Long ccNumber, String ccName, Date ccExpiry,
            String shipping);

	public List<IItem> searchByAuthor(String keyword);

	public List<IItem> searchByTitle(String keyword);

	public List<IItem> searchBySubject(String keyword);

	public List<Object[]> getBestSellers(String category);

	public IShoppingCart createEmptyCart();

	public IShoppingCart findShoppingCartById(Integer shoppingId);

	public List<IItem> getPromotional();

	boolean countryExist(String country);

	public ICustomer getUserBy(String uname, String passwd);

	public ICustomer getCustomerObject();

	public IAddress getAddressObject();

	public ICountry getCountryByName(String country);

	public void saveAddress(IAddress address);

	public void saveCustomer(ICustomer customer);

	public ICustomer findCustomerById(Integer customerId);

	public IOrders getMostRecentOrder(ICustomer customer);

	public List<IOrderLine> findAllOrderLineByOrder(IOrders order);

	public IItem findItemById(Integer itemId);

	List findAllShoppingCartLinesBySC(IShoppingCart shoppingCart);

	ICcXacts findCcXactsById(Integer ccXactId);

	IOrderLine findOrderLineById(Integer olId);

	IAuthor findAuthorById(Integer cxAuthId);

	IOrders findOrdersById(Integer cxOId);

	List<ICustomer> findCustomerByAddress(IAddress address);

	ICountry getCountryById(Integer coId);

	IAddress findAddressById(Integer addrId);

	public void saveItem(IItem item);
	
} @Service
//@Component
public class MongoService extends AService
{
	public MongoService()
	{	
		System.out.println("Creating MongoService");
//		addressDao = ContextHelper.getApplicationContext().getBean( AddressDaoImpl.class );
	}
	
	
	@Override
	protected IOrders saveOrder(IAddress address, ICustomer customer,
	        String shipping, IShoppingCart sc, double discount)
	{
		Calendar cal = Calendar.getInstance();
		Random rand = new Random();

		Orders order = (Orders) ordersDao.getObject();
		order.setCustomer( customer );
		order.setOTax( 8.25 );
		order.setODate( new Date() );
		order.setAddressByOBillAddrId( customer.getAddress() );
		order.setAddressByOShipAddrId( address );

		cal.add( Calendar.DATE, rand.nextInt( 7 ) );
		order.setOShipDate( cal.getTime() );
		order.setOShipType( shipping );
		order.setOStatus( "PENDING" );
		order.setOTotal( calculateTotal( sc ) );

		order.setOSubTotal( calculateTotal( sc ) );
		
		Set<IShoppingCartLine> res = sc.getShoppingCartLines();
		
		if( res != null && !res.isEmpty() )
		{
    		for ( IShoppingCartLine scl :  res)
    		{
    			IOrderLine ol = orderLineDao.getObject();
    			ol.setItem( scl.getItem() );
    			ol.setOlComment( getRandomString( 20, 100 ) );
    			ol.setOlDiscount( discount );
    			ol.setOlQty( scl.getSclQty() );
    			ol.setOrders( order );
    			
    			orderLineDao.shrani( ol );
    			
    			HashSet<IOrderLine> set = new HashSet<IOrderLine>();
				set.add(ol);
				set.addAll(order.getOrderLines());
    			
    			order.setOrderLines( set );
    			//ordersDao.shrani(order);
    			
    			Item item = (Item) itemDao.findById( scl.getItem().getIId() );

    			Integer olQty = item.getOlQty();
    			item.setOlQty( (olQty == null ? 0 : olQty ) + 1 );
    			
    			itemDao.shrani( item );
    
    		}
		}

		ordersDao.shrani( order );

		return order;
	}
	
}
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import eu.cloudscale.showcase.db.common.ContextHelper;
import eu.cloudscale.showcase.db.common.DatabaseHelper;
import eu.cloudscale.showcase.db.dao.IAddressDao;
import eu.cloudscale.showcase.db.dao.IAuthorDao;
import eu.cloudscale.showcase.db.dao.ICcXactsDao;
import eu.cloudscale.showcase.db.dao.ICountryDao;
import eu.cloudscale.showcase.db.dao.ICustomerDao;
import eu.cloudscale.showcase.db.dao.IItemDao;
import eu.cloudscale.showcase.db.dao.IOrderLineDao;
import eu.cloudscale.showcase.db.dao.IOrdersDao;
import eu.cloudscale.showcase.db.dao.IShoppingCartDao;
import eu.cloudscale.showcase.db.dao.IShoppingCartLineDao;
import eu.cloudscale.showcase.db.model.IAddress;
import eu.cloudscale.showcase.db.model.IAuthor;
import eu.cloudscale.showcase.db.model.ICountry;
import eu.cloudscale.showcase.db.model.ICustomer;
import eu.cloudscale.showcase.db.model.IItem;
import eu.cloudscale.showcase.db.services.IService;
import eu.cloudscale.showcase.db.services.MongoService;
/*******************************************************************************
*  Copyright (c) 2015 XLAB d.o.o.
*  All rights reserved. This program and the accompanying materials
*  are made available under the terms of the Eclipse Public License v1.0
*  which accompanies this distribution, and is available at
*  http://www.eclipse.org/legal/epl-v10.html
*  
*  @author XLAB d.o.o.
*******************************************************************************/
package eu.cloudscale.showcase.generate;



@Transactional
public abstract class AGenerate implements IGenerate
{
	@Autowired(required=true)
	protected SessionFactory sessionFactory;
	
	@Autowired(required=true)
    protected IAddressDao addressDao;
	
	@Autowired(required=true)
	protected IAuthorDao authorDao;
	
	@Autowired
	protected ICcXactsDao ccXactsDao;