CarAccessoryTypeDaoBase.java

  1. // license-header java merge-point
  2. //
  3. // Attention: Generated code! Do not modify by hand! hibernate4=true hibernateVersion=4.3.6.Final
  4. // Generated by hibernate/SpringHibernateDaoBase.vsl in andromda-spring-cartridge on 08/18/2014 15:29:43-0400. Do not modify by hand!.
  5. //
  6. package org.andromda.samples.carrental.inventory;

  7. import java.security.Principal;
  8. import java.util.ArrayList;
  9. import java.util.Collection;
  10. import java.util.List;
  11. import javax.annotation.Resource;
  12. import org.andromda.samples.carrental.PrincipalStore;
  13. import org.andromda.samples.carrental.PropertySearch;
  14. import org.andromda.samples.carrental.Search;
  15. import org.andromda.spring.PaginationResult;
  16. import org.apache.commons.collections.CollectionUtils;
  17. import org.apache.commons.collections.Transformer;
  18. import org.apache.commons.logging.Log;
  19. import org.apache.commons.logging.LogFactory;
  20. import org.hibernate.Criteria;
  21. import org.hibernate.Query;
  22. import org.hibernate.ScrollableResults;
  23. import org.hibernate.Session;
  24. import org.hibernate.SessionFactory;
  25. import org.springframework.stereotype.Repository;

  26. /**
  27.  * <p>
  28.  * Base Spring DAO Class: is able to create, update, remove, load, and find
  29.  * objects of type <code>CarAccessoryType</code>.
  30.  * </p>
  31.  *
  32.  * @see CarAccessoryType
  33.  */
  34. @Repository
  35. public abstract class CarAccessoryTypeDaoBase
  36.     implements CarAccessoryTypeDao
  37. {
  38.     /**
  39.      * For backwards compatibility with HibernateDao method
  40.      * @deprecated Use LogFactory.getLog on each subclass, for the correct class name
  41.      * Apache commons logging logger used by all subclasses
  42.      */
  43.     @Deprecated
  44.     protected Log logger = LogFactory.getLog(CarAccessoryTypeDaoBase.class);

  45.     @Resource
  46.     private SessionFactory sessionFactory;
  47.     /**
  48.      * @param sessionFactoryIn
  49.      */
  50.     public void setSessionFactory(SessionFactory sessionFactoryIn) {
  51.         this.sessionFactory = sessionFactoryIn;
  52.     }
  53.     /**
  54.      * @return SessionFactory
  55.      */
  56.     protected SessionFactory getSessionFactory() {
  57.         return this.sessionFactory;
  58.     }
  59.     /**
  60.      * @return currentSession
  61.      */
  62.     protected Session getSession() {
  63.         return this.sessionFactory.getCurrentSession();
  64.     }
  65.     /**
  66.      * For backwards compatibility with HibernateDao method
  67.      * @deprecated Use this.sessionFactory.getCurrentSession() instead
  68.      * @param ignore boolean - always create new session if needed
  69.      * @return currentSession
  70.      */
  71.     @Deprecated
  72.     protected Session getSession(boolean ignore) {
  73.         return this.getSession();
  74.     }

  75.     /**
  76.      * {@inheritDoc}
  77.      */
  78.     @Override
  79.     public Object get(final int transform, final Long id)
  80.     {
  81.         if (id == null)
  82.         {
  83.             throw new IllegalArgumentException(
  84.                 "CarAccessoryType.get - 'id' can not be null");
  85.         }
  86.         final Object entity = this.sessionFactory.getCurrentSession().get(CarAccessoryTypeImpl.class, id);
  87.         return transformEntity(transform, (CarAccessoryType)entity);
  88.     }
  89.     /**
  90.      * {@inheritDoc}
  91.      */
  92.     @Override
  93.     public CarAccessoryType get(Long id)
  94.     {
  95.         return (CarAccessoryType)this.get(TRANSFORM_NONE, id);
  96.     }

  97.     /**
  98.      * {@inheritDoc}
  99.      */
  100.     @Override
  101.     public Object load(final int transform, final Long id)
  102.     {
  103.         if (id == null)
  104.         {
  105.             throw new IllegalArgumentException(
  106.                 "CarAccessoryType.load - 'id' can not be null");
  107.         }
  108.         final Object entity = this.sessionFactory.getCurrentSession().get(CarAccessoryTypeImpl.class, id);
  109.         return transformEntity(transform, (CarAccessoryType)entity);
  110.     }

  111.     /**
  112.      * {@inheritDoc}
  113.      */
  114.     @Override
  115.     public CarAccessoryType load(Long id)
  116.     {
  117.         return (CarAccessoryType)this.load(TRANSFORM_NONE, id);
  118.     }

  119.     /**
  120.      * {@inheritDoc}
  121.      */
  122.     @Override
  123.     @SuppressWarnings({"unchecked"})
  124.     public Collection<CarAccessoryType> loadAll()
  125.     {
  126.         return (Collection<CarAccessoryType>) this.loadAll(CarAccessoryTypeDao.TRANSFORM_NONE);
  127.     }

  128.     /**
  129.      * {@inheritDoc}
  130.      */
  131.     @Override
  132.     public Collection<?> loadAll(final int transform)
  133.     {
  134.         return this.loadAll(transform, -1, -1);
  135.     }

  136.     /**
  137.      * {@inheritDoc}
  138.      */
  139.     @Override
  140.     public Collection<?> loadAll(final int pageNumber, final int pageSize)
  141.     {
  142.         return this.loadAll(CarAccessoryTypeDao.TRANSFORM_NONE, pageNumber, pageSize);
  143.     }

  144.     /**
  145.      * {@inheritDoc}
  146.      */
  147.     @Override
  148.     public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize)
  149.     {
  150.         final Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(CarAccessoryTypeImpl.class);
  151.         if (pageNumber > 0 && pageSize > 0)
  152.         {
  153.             criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
  154.             criteria.setMaxResults(pageSize);
  155.         }
  156.         final Collection<?> results = criteria.list();
  157.         this.transformEntities(transform, results);
  158.         return results;
  159.     }

  160.     /**
  161.      * firstResult = (pageNumber - 1) * pageSize
  162.      * @param pageNumber
  163.      * @param pageSize
  164.      * @return firstResult
  165.      */
  166.     protected int calculateFirstResult(int pageNumber, int pageSize)
  167.     {
  168.         int firstResult = 0;
  169.         if (pageNumber > 0)
  170.         {
  171.             firstResult = (pageNumber - 1) * pageSize;
  172.         }
  173.         return firstResult;
  174.     }

  175.     /**
  176.      * prePersist event - This method is called before creating the entity
  177.      */
  178.     protected void _prePersist(final CarAccessoryType carAccessoryType)
  179.     {
  180.     }
  181.      
  182.     /**
  183.      * postPersist event - This method is called after creating the entity
  184.      */
  185.     protected void _postPersist(final CarAccessoryType carAccessoryType)
  186.     {
  187.     }
  188.      
  189.     /**
  190.      * preUpdate event - This method is called before updating the entity
  191.      */
  192.     protected void _preUpdate(final CarAccessoryType carAccessoryType)
  193.     {
  194.     }
  195.      
  196.     /**
  197.      * postUpdate event - This method is called after updating the entity
  198.      */
  199.     protected void _postUpdate(final CarAccessoryType carAccessoryType)
  200.     {
  201.     }
  202.      
  203.     /**
  204.      * preRemove event - This method is called before deleting the entity
  205.      */
  206.     protected void _preRemove(final CarAccessoryType carAccessoryType)
  207.     {
  208.     }
  209.      
  210.     /**
  211.      * postRemove event - This method is called after deleting the entity
  212.      */
  213.     protected void _postRemove(final CarAccessoryType carAccessoryType)
  214.     {
  215.     }
  216.      
  217.     /**
  218.      * {@inheritDoc}
  219.      */
  220.     @Override
  221.     public CarAccessoryType create(CarAccessoryType carAccessoryType)
  222.     {
  223.         return (CarAccessoryType)this.create(CarAccessoryTypeDao.TRANSFORM_NONE, carAccessoryType);
  224.     }

  225.     /**
  226.      * {@inheritDoc}
  227.      */
  228.     @Override
  229.     public Object create(final int transform, final CarAccessoryType carAccessoryType)
  230.     {
  231.         if (carAccessoryType == null)
  232.         {
  233.             throw new IllegalArgumentException(
  234.                 "CarAccessoryType.create - 'carAccessoryType' can not be null");
  235.         }

  236.         //prePersist event
  237.         _prePersist(carAccessoryType);

  238.         this.sessionFactory.getCurrentSession().save(carAccessoryType);

  239.         //postPersist event
  240.         _postPersist(carAccessoryType);

  241.         return this.transformEntity(transform, carAccessoryType);
  242.     }

  243.     /**
  244.      * {@inheritDoc}
  245.      */
  246.     @Override
  247.     @SuppressWarnings({"unchecked"})
  248.     public Collection<CarAccessoryType> create(final Collection<CarAccessoryType> entities)
  249.     {
  250.         return (Collection<CarAccessoryType>) create(CarAccessoryTypeDao.TRANSFORM_NONE, entities);
  251.     }

  252.     /**
  253.      * {@inheritDoc}
  254.      */
  255.     @Override
  256.     public Collection<?> create(final int transform, final Collection<CarAccessoryType> entities)
  257.     {
  258.         if (entities == null)
  259.         {
  260.             throw new IllegalArgumentException(
  261.                 "CarAccessoryType.create - 'entities' can not be null");
  262.         }
  263.         Collection<Object> transformed = new ArrayList<Object>();
  264.         for (CarAccessoryType entity : entities)
  265.         {
  266.             transformed.add(create(transform, entity));
  267.         }
  268.         return transformed;
  269.     }

  270.     /**
  271.      * {@inheritDoc}
  272.      */
  273.     @Override
  274.     public CarAccessoryType create(
  275.         String manufacter,
  276.         String identifier,
  277.         String orderNo)
  278.     {
  279.         return (CarAccessoryType)this.create(CarAccessoryTypeDao.TRANSFORM_NONE, manufacter, identifier, orderNo);
  280.     }

  281.     /**
  282.      * {@inheritDoc}
  283.      */
  284.     @Override
  285.     public Object create(
  286.         final int transform,
  287.         String manufacter,
  288.         String identifier,
  289.         String orderNo)
  290.     {
  291.         CarAccessoryType entity = new CarAccessoryTypeImpl();
  292.         entity.setManufacter(manufacter);
  293.         entity.setIdentifier(identifier);
  294.         entity.setOrderNo(orderNo);
  295.         return this.create(transform, entity);
  296.     }

  297.     /**
  298.      * {@inheritDoc}
  299.      */
  300.     @Override
  301.     public void update(CarAccessoryType carAccessoryType)
  302.     {
  303.         if (carAccessoryType == null)
  304.         {
  305.             throw new IllegalArgumentException(
  306.                 "CarAccessoryType.update - 'carAccessoryType' can not be null");
  307.         }
  308.        
  309.         //preUpdate event
  310.         _preUpdate(carAccessoryType);

  311.         this.sessionFactory.getCurrentSession().update(carAccessoryType);

  312.         //postUpdate event
  313.         _postUpdate(carAccessoryType);
  314.     }

  315.     /**
  316.      * {@inheritDoc}
  317.      */
  318.     @Override
  319.     public void update(final Collection<CarAccessoryType> entities)
  320.     {
  321.         if (entities == null)
  322.         {
  323.             throw new IllegalArgumentException(
  324.                 "CarAccessoryType.update - 'entities' can not be null");
  325.         }
  326.         for (CarAccessoryType entity : entities)
  327.         {
  328.             update(entity);
  329.         }
  330.     }

  331.     /**
  332.      * {@inheritDoc}
  333.      */
  334.     @Override
  335.     public void remove(CarAccessoryType carAccessoryType)
  336.     {
  337.         if (carAccessoryType == null)
  338.         {
  339.             throw new IllegalArgumentException(
  340.                 "CarAccessoryType.remove - 'carAccessoryType' can not be null");
  341.         }
  342.         //preRemove event
  343.         _preRemove(carAccessoryType);
  344.        
  345.         this.sessionFactory.getCurrentSession().delete(carAccessoryType);

  346.         //postRemove event
  347.         _postRemove(carAccessoryType);
  348.     }

  349.     /**
  350.      * {@inheritDoc}
  351.      */
  352.     @Override
  353.     public void remove(Long id)
  354.     {
  355.         if (id == null)
  356.         {
  357.             throw new IllegalArgumentException(
  358.                 "CarAccessoryType.remove - 'id' can not be null");
  359.         }
  360.         CarAccessoryType entity = this.get(id);
  361.         if (entity != null)
  362.         {
  363.             this.remove(entity);
  364.         }
  365.     }

  366.     /**
  367.      * {@inheritDoc}
  368.      */
  369.     @Override
  370.     public void remove(Collection<CarAccessoryType> entities)
  371.     {
  372.         if (entities == null)
  373.         {
  374.             throw new IllegalArgumentException(
  375.                 "CarAccessoryType.remove - 'entities' can not be null");
  376.         }
  377.         for (CarAccessoryType entity : entities)
  378.         {
  379.             //preRemove event
  380.             _preRemove(entity);

  381.             this.sessionFactory.getCurrentSession().delete(entity);

  382.             //postRemove event
  383.             _postRemove(entity);
  384.         }
  385.     }
  386.     /**
  387.      * Allows transformation of entities into value objects
  388.      * (or something else for that matter), when the <code>transform</code>
  389.      * flag is set to one of the constants defined in <code>CarAccessoryTypeDao</code>, please note
  390.      * that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself
  391.      * will be returned.
  392.      *
  393.      * If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed.
  394.      *
  395.      * @param transform one of the constants declared in {@link CarAccessoryTypeDao}
  396.      * @param entity an entity that was found
  397.      * @return the transformed entity (i.e. new value object, etc)
  398.      * @see CarAccessoryTypeDao#transformEntity(int, CarAccessoryType)
  399.      */
  400.     @Override
  401.     public Object transformEntity(final int transform, final CarAccessoryType entity)
  402.     {
  403.         Object target = null;
  404.         if (entity != null)
  405.         {
  406.             switch (transform)
  407.             {
  408.                 case CarAccessoryTypeDao.TRANSFORM_NONE : // fall-through
  409.                 default:
  410.                     target = entity;
  411.             }
  412.         }
  413.         return target;
  414.     }

  415.     /**
  416.      * {@inheritDoc}
  417.      */
  418.     @Override
  419.     public void transformEntities(final int transform, final Collection<?> entities)
  420.     {
  421.         switch (transform)
  422.         {
  423.             case CarAccessoryTypeDao.TRANSFORM_NONE : // fall-through
  424.                 default:
  425.                 // do nothing;
  426.         }
  427.     }

  428.     /**
  429.      * @see CarAccessoryTypeDao#toEntities(Collection)
  430.      */
  431.     @Override
  432.     public void toEntities(final Collection<?> results)
  433.     {
  434.         if (results != null)
  435.         {
  436.             CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
  437.         }
  438.     }

  439.     /**
  440.      * This anonymous transformer is designed to transform report query results
  441.      * (which result in an array of entities) to {@link CarAccessoryType}
  442.      * using the Jakarta Commons-Collections Transformation API.
  443.      */
  444.     private Transformer ENTITYTRANSFORMER =
  445.         new Transformer()
  446.         {
  447.             @Override
  448.             public Object transform(Object input)
  449.             {
  450.                 Object result = null;
  451.                 if (input instanceof Object[])
  452.                 {
  453.                     final Object[] rows = (Object[])input;
  454.                     result = toEntity(rows);
  455.                 }
  456.                 else if (input instanceof CarAccessoryType)
  457.                 {
  458.                     result = input;
  459.                 }
  460.                 return result;
  461.             }
  462.         };

  463.     /**
  464.      * @param row
  465.      * @return CarAccessoryType
  466.      */
  467.     protected CarAccessoryType toEntity(Object[] row)
  468.     {
  469.         CarAccessoryType target = null;
  470.         if (row != null)
  471.         {
  472.             final int numberOfObjects = row.length;
  473.             for (int ctr = 0; ctr < numberOfObjects; ctr++)
  474.             {
  475.                 final Object object = row[ctr];
  476.                 if (object instanceof CarAccessoryType)
  477.                 {
  478.                     target = (CarAccessoryType)object;
  479.                     break;
  480.                 }
  481.             }
  482.         }
  483.         return target;
  484.     }

  485.     /**
  486.      * Gets the current <code>principal</code> if one has been set,
  487.      * otherwise returns <code>null</code>.
  488.      *
  489.      * @return the current principal
  490.      */
  491.     protected Principal getPrincipal()
  492.     {
  493.         return PrincipalStore.get();
  494.     }

  495.     /**
  496.      * {@inheritDoc}
  497.      */
  498.     @Override
  499.     @SuppressWarnings({ "unchecked" })
  500.     public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search)
  501.     {
  502.         search.setPageNumber(pageNumber);
  503.         search.setPageSize(pageSize);
  504.         final PropertySearch propertySearch = new PropertySearch(
  505.             this.sessionFactory.getCurrentSession(), CarAccessoryTypeImpl.class, search);
  506.         final List results = propertySearch.executeAsList();
  507.         this.transformEntities(transform, results);
  508.         return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount());
  509.     }

  510.     /**
  511.      * {@inheritDoc}
  512.      */
  513.     @Override
  514.     public PaginationResult search(final int pageNumber, final int pageSize, final Search search)
  515.     {
  516.         return this.search(CarAccessoryTypeDao.TRANSFORM_NONE, pageNumber, pageSize, search);
  517.     }

  518.     /**
  519.      * {@inheritDoc}
  520.      */
  521.     @Override
  522.     public Collection<?> search(final int transform, final Search search)
  523.     {
  524.         final PropertySearch propertySearch = new PropertySearch(
  525.             this.sessionFactory.getCurrentSession(), CarAccessoryTypeImpl.class, search);
  526.         final Collection<?> results = propertySearch.executeAsList();
  527.         this.transformEntities(transform, results);
  528.         return results;
  529.     }

  530.     /**
  531.      * {@inheritDoc}
  532.      */
  533.     @Override
  534.     @SuppressWarnings("unchecked")
  535.     public Collection<CarAccessoryType> search(final Search search)
  536.     {
  537.         return (Collection<CarAccessoryType>) this.search(CarAccessoryTypeDao.TRANSFORM_NONE, search);
  538.     }

  539.     /**
  540.      * Executes and returns the given Hibernate queryObject as a {@link PaginationResult} instance.
  541.      * @param queryObject
  542.      * @param transform
  543.      * @param pageNumber
  544.      * @param pageSize
  545.      * @return PaginationResult
  546.      */
  547.     @SuppressWarnings({ "unchecked" })
  548.     protected PaginationResult getPaginationResult(
  549.         final Query queryObject,
  550.         final int transform, int pageNumber, int pageSize)
  551.     {
  552.         final ScrollableResults scrollableResults = queryObject.scroll();
  553.         scrollableResults.last();
  554.         int totalCount = scrollableResults.getRowNumber();
  555.         totalCount = totalCount >= 0 ? totalCount + 1 : 0;
  556.         if (pageNumber > 0 && pageSize > 0)
  557.         {
  558.             queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
  559.             queryObject.setMaxResults(pageSize);
  560.         }
  561.         // Unchecked transformation because Set object is reused, cannot be strongly typed.
  562.         @SuppressWarnings("rawtypes")
  563.         Collection results = new ArrayList(queryObject.list());
  564.         transformEntities(transform, results);
  565.         return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
  566.     }


  567.     /**
  568.      * Create or Update the <code>carAccessoryType</code> instance in the persistent store.
  569.      * @param carAccessoryType
  570.      */
  571.     @Override
  572.     public CarAccessoryType createOrUpdate(CarAccessoryType carAccessoryType)
  573.     {
  574.         if(carAccessoryType.getId() == null)
  575.         {
  576.             return (CarAccessoryType)this.create(TRANSFORM_NONE,carAccessoryType);
  577.         }
  578.         else
  579.         {
  580.             this.update(carAccessoryType);
  581.             return carAccessoryType;
  582.         }
  583.     }
  584.    
  585.     // spring-hibernate-dao-base merge-point
  586. }