com.algoTrader.entity.security.EquityIndexDaoBase.java Source code

Java tutorial

Introduction

Here is the source code for com.algoTrader.entity.security.EquityIndexDaoBase.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.security;

import com.algoTrader.PrincipalStore;
import com.algoTrader.PropertySearch;
import com.algoTrader.Search;
import com.algoTrader.SearchParameter;
import java.security.Principal;
import java.util.Collection;
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.NonUniqueResultException;
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>EquityIndex</code>.
 * </p>
 *
 * @see EquityIndex
 */
public abstract class EquityIndexDaoBase extends HibernateDaoSupport implements EquityIndexDao {
    /**
     * {@inheritDoc}
     */
    @Override
    public Object get(final int transform, final int id) {
        final Object entity = this.getHibernateTemplate().get(EquityIndexImpl.class, new Integer(id));
        return transformEntity(transform, (EquityIndex) entity);
    }

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

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

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

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings({ "unchecked" })
    public Collection<EquityIndex> loadAll() {
        return (Collection<EquityIndex>) this.loadAll(EquityIndexDao.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(EquityIndexDao.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(EquityIndexImpl.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 EquityIndex create(EquityIndex equityIndex) {
        return (EquityIndex) this.create(EquityIndexDao.TRANSFORM_NONE, equityIndex);
    }

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

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

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

    /**
     * {@inheritDoc}
     */
    @Override
    public EquityIndex create(String isin, String symbol) {
        return (EquityIndex) this.create(EquityIndexDao.TRANSFORM_NONE, isin, symbol);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object create(final int transform, String isin, String symbol) {
        EquityIndex entity = new EquityIndexImpl();
        entity.setIsin(isin);
        entity.setSymbol(symbol);
        return this.create(transform, entity);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public EquityIndex create(String isin, SecurityFamily securityFamily) {
        return (EquityIndex) this.create(EquityIndexDao.TRANSFORM_NONE, isin, securityFamily);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object create(final int transform, String isin, SecurityFamily securityFamily) {
        EquityIndex entity = new EquityIndexImpl();
        entity.setIsin(isin);
        entity.setSecurityFamily(securityFamily);
        return this.create(transform, entity);
    }

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

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

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

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

    /**
     * {@inheritDoc}
     */
    @Override
    public void remove(Collection<EquityIndex> entities) {
        if (entities == null) {
            throw new IllegalArgumentException("EquityIndex.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>EquityIndexDao</code>, please note
     * that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself
     * will be returned.
     *
     * If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed.
     *
     * @param transform one of the constants declared in {@link EquityIndexDao}
     * @param entity an entity that was found
     * @return the transformed entity (i.e. new value object, etc)
     * @see EquityIndexDao#transformEntity(int, EquityIndex)
     */
    public Object transformEntity(final int transform, final EquityIndex entity) {
        Object target = null;
        if (entity != null) {
            switch (transform) {
            case EquityIndexDao.TRANSFORM_NONE: // fall-through
            default:
                target = entity;
            }
        }
        return target;
    }

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

    /**
     * @see EquityIndexDao#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 EquityIndex}
     * 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 EquityIndex) {
                result = input;
            }
            return result;
        }
    };

    /**
     * @param row
     * @return EquityIndex
     */
    protected EquityIndex toEntity(Object[] row) {
        EquityIndex 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 EquityIndex) {
                    target = (EquityIndex) object;
                    break;
                }
            }
        }
        return target;
    }

    /**
     * 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), EquityIndexImpl.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(EquityIndexDao.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), EquityIndexImpl.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<EquityIndex> search(final Search search) {
        return (Set<EquityIndex>) this.search(EquityIndexDao.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);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public EquityIndex searchUniqueIsin(final String isin) {
        final Search search = new Search(
                new SearchParameter[] { new SearchParameter("isin", isin, SearchParameter.EQUAL_COMPARATOR) });

        final Set<EquityIndex> searchResult = this.search(search);
        switch (searchResult.size()) {
        case 0:
            return null;
        case 1:
            return searchResult.iterator().next();
        default:
            throw new NonUniqueResultException(searchResult.size());
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object searchUniqueIsin(final int transform, final String isin) {
        final EquityIndex entity = this.searchUniqueIsin(isin);
        if (entity == null) {
            return null;
        }
        return transformEntity(transform, entity);
    }

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