com.algoTrader.entity.marketData.BarDaoBase.java Source code

Java tutorial

Introduction

Here is the source code for com.algoTrader.entity.marketData.BarDaoBase.java

Source

// license-header java merge-point
//
// Attention: Generated code! Do not modify by hand!
// Generated by: hibernate/SpringHibernateDaoBase.vsl in andromda-spring-cartridge.
//
package com.algoTrader.entity.marketData;

import com.algoTrader.PrincipalStore;
import com.algoTrader.PropertySearch;
import com.algoTrader.Search;
import com.algoTrader.entity.security.Security;
import com.algoTrader.entity.security.SecurityDao;
import com.algoTrader.vo.BarVO;
import java.math.BigDecimal;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.andromda.spring.PaginationResult;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * <p>
 * Base Spring DAO Class: is able to create, update, remove, load, and find
 * objects of type <code>Bar</code>.
 * </p>
 *
 * @see Bar
 */
public abstract class BarDaoBase extends HibernateDaoSupport implements BarDao {
    private SecurityDao securityDao;

    /**
     * Sets the reference to <code>securityDao</code>.
     * @param securityDaoIn
     */
    public void setSecurityDao(SecurityDao securityDaoIn) {
        this.securityDao = securityDaoIn;
    }

    /**
     * Gets the reference to <code>securityDao</code>.
     * @return securityDao
     */
    protected SecurityDao getSecurityDao() {
        return this.securityDao;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object get(final int transform, final int id) {
        final Object entity = this.getHibernateTemplate().get(BarImpl.class, new Integer(id));
        return transformEntity(transform, (Bar) entity);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Bar get(int id) {
        return (Bar) this.get(TRANSFORM_NONE, id);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object load(final int transform, final int id) {
        final Object entity = this.getHibernateTemplate().get(BarImpl.class, new Integer(id));
        return transformEntity(transform, (Bar) entity);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Bar load(int id) {
        return (Bar) this.load(TRANSFORM_NONE, id);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings({ "unchecked" })
    public Collection<Bar> loadAll() {
        return (Collection<Bar>) this.loadAll(BarDao.TRANSFORM_NONE);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<?> loadAll(final int transform) {
        return this.loadAll(transform, -1, -1);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<?> loadAll(final int pageNumber, final int pageSize) {
        return this.loadAll(BarDao.TRANSFORM_NONE, pageNumber, pageSize);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize) {
        try {
            final Criteria criteria = this.getSession(false).createCriteria(BarImpl.class);
            if (pageNumber > 0 && pageSize > 0) {
                criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
                criteria.setMaxResults(pageSize);
            }
            final Collection<?> results = criteria.list();
            this.transformEntities(transform, results);
            return results;
        } catch (HibernateException ex) {
            throw super.convertHibernateAccessException(ex);
        }
    }

    /**
     * firstResult = (pageNumber - 1) * pageSize
     * @param pageNumber
     * @param pageSize
     * @return firstResult
     */
    protected int calculateFirstResult(int pageNumber, int pageSize) {
        int firstResult = 0;
        if (pageNumber > 0) {
            firstResult = (pageNumber - 1) * pageSize;
        }
        return firstResult;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Bar create(Bar bar) {
        return (Bar) this.create(BarDao.TRANSFORM_NONE, bar);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object create(final int transform, final Bar bar) {
        if (bar == null) {
            throw new IllegalArgumentException("Bar.create - 'bar' can not be null");
        }
        this.getHibernateTemplate().save(bar);
        return this.transformEntity(transform, bar);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings({ "unchecked" })
    public Collection<Bar> create(final Collection<Bar> entities) {
        return (Collection<Bar>) create(BarDao.TRANSFORM_NONE, entities);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<?> create(final int transform, final Collection<Bar> entities) {
        if (entities == null) {
            throw new IllegalArgumentException("Bar.create - 'entities' can not be null");
        }
        this.getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                for (Bar entity : entities) {
                    create(transform, entity);
                }
                return null;
            }
        });
        return entities;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Bar create(BigDecimal open, BigDecimal high, BigDecimal low, BigDecimal close, BigDecimal adjClose,
            int vol, int openInterest, Date dateTime) {
        return (Bar) this.create(BarDao.TRANSFORM_NONE, open, high, low, close, adjClose, vol, openInterest,
                dateTime);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object create(final int transform, BigDecimal open, BigDecimal high, BigDecimal low, BigDecimal close,
            BigDecimal adjClose, int vol, int openInterest, Date dateTime) {
        Bar entity = new BarImpl();
        entity.setOpen(open);
        entity.setHigh(high);
        entity.setLow(low);
        entity.setClose(close);
        entity.setAdjClose(adjClose);
        entity.setVol(vol);
        entity.setOpenInterest(openInterest);
        entity.setDateTime(dateTime);
        return this.create(transform, entity);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Bar create(BigDecimal close, Date dateTime, BigDecimal high, BigDecimal low, BigDecimal open,
            int openInterest, Security security, int vol) {
        return (Bar) this.create(BarDao.TRANSFORM_NONE, close, dateTime, high, low, open, openInterest, security,
                vol);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object create(final int transform, BigDecimal close, Date dateTime, BigDecimal high, BigDecimal low,
            BigDecimal open, int openInterest, Security security, int vol) {
        Bar entity = new BarImpl();
        entity.setClose(close);
        entity.setDateTime(dateTime);
        entity.setHigh(high);
        entity.setLow(low);
        entity.setOpen(open);
        entity.setOpenInterest(openInterest);
        entity.setSecurity(security);
        entity.setVol(vol);
        return this.create(transform, entity);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void update(Bar bar) {
        if (bar == null) {
            throw new IllegalArgumentException("Bar.update - 'bar' can not be null");
        }
        this.getHibernateTemplate().update(bar);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void update(final Collection<Bar> entities) {
        if (entities == null) {
            throw new IllegalArgumentException("Bar.update - 'entities' can not be null");
        }
        this.getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                for (Bar entity : entities) {
                    update(entity);
                }
                return null;
            }
        });
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void remove(Bar bar) {
        if (bar == null) {
            throw new IllegalArgumentException("Bar.remove - 'bar' can not be null");
        }
        this.getHibernateTemplate().delete(bar);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void remove(int id) {
        Bar entity = this.get(id);
        if (entity != null) {
            this.remove(entity);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void remove(Collection<Bar> entities) {
        if (entities == null) {
            throw new IllegalArgumentException("Bar.remove - 'entities' can not be null");
        }
        this.getHibernateTemplate().deleteAll(entities);
    }

    /**
     * Allows transformation of entities into value objects
     * (or something else for that matter), when the <code>transform</code>
     * flag is set to one of the constants defined in <code>BarDao</code>, please note
     * that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself
     * will be returned.
     * <p>
     * This method will return instances of these types:
     * <ul>
     *   <li>{@link Bar} - {@link #TRANSFORM_NONE}</li>
     *   <li>{@link BarVO} - {@link #TRANSFORM_BARVO}</li>
     * </ul>
     *
     * If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed.
     *
     * @param transform one of the constants declared in {@link BarDao}
     * @param entity an entity that was found
     * @return the transformed entity (i.e. new value object, etc)
     * @see BarDao#transformEntity(int, Bar)
     */
    public Object transformEntity(final int transform, final Bar entity) {
        Object target = null;
        if (entity != null) {
            switch (transform) {
            case TRANSFORM_BARVO:
                target = toBarVO(entity);
                break;
            case BarDao.TRANSFORM_NONE: // fall-through
            default:
                target = entity;
            }
        }
        return target;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void transformEntities(final int transform, final Collection<?> entities) {
        switch (transform) {
        case TRANSFORM_BARVO:
            toBarVOCollection(entities);
            break;
        case BarDao.TRANSFORM_NONE: // fall-through
        default:
            // do nothing;
        }
    }

    /**
     * @see BarDao#toEntities(Collection)
     */
    public void toEntities(final Collection<?> results) {
        if (results != null) {
            CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
        }
    }

    /**
     * This anonymous transformer is designed to transform report query results
     * (which result in an array of entities) to {@link Bar}
     * using the Jakarta Commons-Collections Transformation API.
     */
    private Transformer ENTITYTRANSFORMER = new Transformer() {
        public Object transform(Object input) {
            Object result = null;
            if (input instanceof Object[]) {
                result = toEntity((Object[]) input);
            } else if (input instanceof Bar) {
                result = input;
            }
            return result;
        }
    };

    /**
     * @param row
     * @return Bar
     */
    protected Bar toEntity(Object[] row) {
        Bar target = null;
        if (row != null) {
            final int numberOfObjects = row.length;
            for (int ctr = 0; ctr < numberOfObjects; ctr++) {
                final Object object = row[ctr];
                if (object instanceof Bar) {
                    target = (Bar) object;
                    break;
                }
            }
        }
        return target;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final Collection toBarVOCollection(Collection<?> entities) {
        Collection result = new ArrayList<BarVO>();
        if (entities != null) {
            CollectionUtils.transform(entities, this.BARVO_TRANSFORMER);
            result.addAll((Collection) entities);
        }
        return result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings({ "unchecked" })
    public final BarVO[] toBarVOArray(Collection<?> entities) {
        BarVO[] result = null;
        if (entities != null) {
            // Unchecked transformation: reuses entities Collection
            final Collection collection = new ArrayList(entities);
            this.toBarVOCollection(collection);
            result = (BarVO[]) collection.toArray(new BarVO[collection.size()]);
        }
        return result;
    }

    /**
     * Default implementation for transforming the results of a report query into a value object. This
     * implementation exists for convenience reasons only. It needs only be overridden in the
     * {@link BarDaoImpl} class if you intend to use reporting queries.
     * @param row
     * @return toBarVO(this.toEntity(row))
     * @see BarDao#toBarVO(Bar)
     */
    protected BarVO toBarVO(Object[] row) {
        return this.toBarVO(this.toEntity(row));
    }

    /**
     * This anonymous transformer is designed to transform entities or report query results
     * (which result in an array of objects) to {@link BarVO}
     * using the Jakarta Commons-Collections Transformation API.
     */
    private Transformer BARVO_TRANSFORMER = new Transformer() {
        public Object transform(Object input) {
            Object result = null;
            if (input instanceof Bar) {
                result = toBarVO((Bar) input);
            } else if (input instanceof Object[]) {
                result = toBarVO((Object[]) input);
            }
            return result;
        }
    };

    /**
     * {@inheritDoc}
     */
    @Override
    public final void barVOToEntityCollection(Collection<?> instances) {
        if (instances != null) {
            for (final Iterator<?> iterator = instances.iterator(); iterator.hasNext();) {
                // - remove an objects that are null or not of the correct instance
                if (!(iterator.next() instanceof BarVO)) {
                    iterator.remove();
                }
            }
            CollectionUtils.transform(instances, this.BarVOToEntityTransformer);
        }
    }

    private final Transformer BarVOToEntityTransformer = new Transformer() {
        public Object transform(Object input) {
            return barVOToEntity((BarVO) input);
        }
    };

    /**
     * {@inheritDoc}
     */
    @Override
    public void toBarVO(Bar source, BarVO target) {
        target.setDateTime(source.getDateTime());
        target.setOpen(source.getOpen());
        target.setHigh(source.getHigh());
        target.setLow(source.getLow());
        target.setClose(source.getClose());
        target.setAdjClose(source.getAdjClose());
        target.setVol(source.getVol());
        target.setOpenInterest(source.getOpenInterest());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public BarVO toBarVO(final Bar entity) {
        BarVO target = null;
        if (entity != null) {
            target = new BarVO();
            this.toBarVO(entity, target);
        }
        return target;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void barVOToEntity(BarVO source, Bar target, boolean copyIfNull) {
        if (copyIfNull || source.getOpen() != null) {
            target.setOpen(source.getOpen());
        }
        if (copyIfNull || source.getHigh() != null) {
            target.setHigh(source.getHigh());
        }
        if (copyIfNull || source.getLow() != null) {
            target.setLow(source.getLow());
        }
        if (copyIfNull || source.getClose() != null) {
            target.setClose(source.getClose());
        }
        if (copyIfNull || source.getAdjClose() != null) {
            target.setAdjClose(source.getAdjClose());
        }
        if (copyIfNull || source.getVol() != 0) {
            target.setVol(source.getVol());
        }
        if (copyIfNull || source.getOpenInterest() != 0) {
            target.setOpenInterest(source.getOpenInterest());
        }
        if (copyIfNull || source.getDateTime() != null) {
            target.setDateTime(source.getDateTime());
        }
    }

    /**
     * Gets the current <code>principal</code> if one has been set,
     * otherwise returns <code>null</code>.
     *
     * @return the current principal
     */
    protected Principal getPrincipal() {
        return PrincipalStore.get();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings({ "unchecked" })
    public PaginationResult search(final int transform, final int pageNumber, final int pageSize,
            final Search search) {
        try {
            search.setPageNumber(pageNumber);
            search.setPageSize(pageSize);
            final PropertySearch propertySearch = new PropertySearch(this.getSession(false), BarImpl.class, search);
            final List results = propertySearch.executeAsList();
            this.transformEntities(transform, results);
            return new PaginationResult(results.toArray(new Object[results.size()]),
                    propertySearch.getTotalCount());
        } catch (HibernateException ex) {
            throw super.convertHibernateAccessException(ex);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public PaginationResult search(final int pageNumber, final int pageSize, final Search search) {
        return this.search(BarDao.TRANSFORM_NONE, pageNumber, pageSize, search);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Set<?> search(final int transform, final Search search) {
        try {
            final PropertySearch propertySearch = new PropertySearch(this.getSession(false), BarImpl.class, search);
            final Set<?> results = propertySearch.executeAsSet();
            this.transformEntities(transform, results);
            return results;
        } catch (HibernateException ex) {
            throw super.convertHibernateAccessException(ex);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public Set<Bar> search(final Search search) {
        return (Set<Bar>) this.search(BarDao.TRANSFORM_NONE, search);
    }

    /**
     * Executes and returns the given Hibernate queryObject as a {@link PaginationResult} instance.
     * @param queryObject
     * @param transform
     * @param pageNumber
     * @param pageSize
     * @return PaginationResult
     */
    @SuppressWarnings({ "unchecked" })
    protected PaginationResult getPaginationResult(final Query queryObject, final int transform, int pageNumber,
            int pageSize) {
        try {
            final ScrollableResults scrollableResults = queryObject.scroll();
            scrollableResults.last();
            int totalCount = scrollableResults.getRowNumber();
            totalCount = totalCount >= 0 ? totalCount + 1 : 0;
            if (pageNumber > 0 && pageSize > 0) {
                queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
                queryObject.setMaxResults(pageSize);
            }
            // Unchecked transformation because Set object is reused, cannot be strongly typed.
            Set results = new LinkedHashSet(queryObject.list());
            transformEntities(transform, results);
            return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
        } catch (HibernateException ex) {
            throw super.convertHibernateAccessException(ex);
        }
    }

    // spring-hibernate-dao-base merge-point
}