MotocycleDaoBase.java

  1. // license-header java merge-point
  2. //
  3. // Attention: Generated code! Do not modify by hand!
  4. // Generated by DaoBase.vsl in andromda-ejb3-cartridge on 08/08/2014 12:21:06.
  5. //
  6. package org.andromda.demo.ejb3.vehicle;

  7. import java.util.ArrayList;
  8. import java.util.Collection;
  9. import java.util.List;
  10. import javax.annotation.Resource;
  11. import javax.ejb.Local;
  12. import javax.ejb.SessionContext;
  13. import javax.ejb.TransactionAttribute;
  14. import javax.ejb.TransactionAttributeType;
  15. import javax.persistence.EntityManager;
  16. import javax.persistence.PersistenceContext;
  17. import javax.persistence.TypedQuery;
  18. import org.hibernate.Session;

  19. /**
  20.  * <p>
  21.  * Base EJB3 DAO Class: is able to create, update, remove, load, and find
  22.  * objects of type <code>Motocycle</code>.
  23.  * </p>
  24.  *
  25.  * @see MotocycleDao
  26.  */
  27. @TransactionAttribute(TransactionAttributeType.REQUIRED)
  28. @Local({MotocycleDao.class})
  29. public abstract class MotocycleDaoBase
  30.     implements MotocycleDao
  31. {
  32.     /** Session Context Injection */
  33.     @Resource
  34.     protected SessionContext context;

  35.     /**
  36.      * Inject persistence context demo-ejb3
  37.      */
  38.     @PersistenceContext(unitName = "demo-ejb3")
  39.     protected EntityManager emanager;

  40.     /**
  41.      * Inject Hibernate Session
  42.      */
  43.     @PersistenceContext(unitName = "demo-ejb3")
  44.     protected Session hibernateSession;

  45.     /**
  46.      * @see MotocycleDao#load
  47.      */
  48.     @Override
  49.     public Object load(final int transform, final String license)
  50.         throws MotocycleDaoException
  51.     {
  52.         if (license == null)
  53.         {
  54.             throw new IllegalArgumentException(
  55.                 "Motocycle.load - 'license' can not be null");
  56.         }
  57.         try
  58.         {
  59.             final Motocycle entity = this.emanager.find(Motocycle.class, license);
  60.             return transformEntity(transform, entity);
  61.         }
  62.         catch (Exception ex)
  63.         {
  64.             throw new MotocycleDaoException(ex);
  65.         }
  66.     }

  67.     /**
  68.      * @see MotocycleDao#load( String)
  69.      */
  70.     @Override
  71.     public Motocycle load( final String license)
  72.         throws MotocycleDaoException
  73.     {
  74.         return (Motocycle)this.load(TRANSFORM_NONE, license);
  75.     }

  76.     /**
  77.      * @see MotocycleDao#loadAll()
  78.      */
  79.     @Override
  80.     @SuppressWarnings({"unchecked"})
  81.     public Collection<Motocycle> loadAll()
  82.         throws MotocycleDaoException
  83.     {
  84.         return this.loadAll(TRANSFORM_NONE);
  85.     }

  86.     /**
  87.      * @see MotocycleDao#loadAll(int)
  88.      */
  89.     @Override
  90.     public Collection loadAll(final int transform)
  91.         throws MotocycleDaoException
  92.     {
  93.         try
  94.         {
  95.             TypedQuery<Motocycle> query = this.emanager.createNamedQuery("Motocycle.findAll", Motocycle.class);
  96.             List<Motocycle> results = query.getResultList();
  97.             this.transformEntities(transform, results);
  98.             return results;
  99.         }
  100.         catch (Exception ex)
  101.         {
  102.             throw new MotocycleDaoException(ex);
  103.         }
  104.     }

  105.     /**
  106.      * Create Motocycle with no VO transformation
  107.      * @see MotocycleDao#create(Motocycle)
  108.      */
  109.     @Override
  110.     public Motocycle create(Motocycle motocycle)
  111.         throws MotocycleDaoException
  112.     {
  113.         return (Motocycle)this.create(TRANSFORM_NONE, motocycle);
  114.     }

  115.     /**
  116.      * Create Motocycle with VO transformation
  117.      * @see MotocycleDao#create(int, Motocycle)
  118.      */
  119.     @Override
  120.     public Object create(final int transform, final Motocycle motocycle)
  121.         throws MotocycleDaoException
  122.     {
  123.         if (motocycle == null)
  124.         {
  125.             throw new IllegalArgumentException(
  126.                 "Motocycle.create - 'motocycle' can not be null");
  127.         }

  128.         try
  129.         {
  130.             this.emanager.persist(motocycle);
  131.             this.emanager.flush();
  132.             return this.transformEntity(transform, motocycle);
  133.         }
  134.         catch (Exception ex)
  135.         {
  136.             throw new MotocycleDaoException(ex);
  137.         }
  138.     }

  139.     /**
  140.      * Create a Collection of Motocycle with no VO transformation
  141.      * @see MotocycleDao#create(Collection)
  142.      */
  143.     @Override
  144.     @SuppressWarnings({"unchecked"})
  145.     public Collection<Motocycle> create(final Collection<Motocycle> entities)
  146.         throws MotocycleDaoException
  147.     {
  148.         return create(TRANSFORM_NONE, entities);
  149.     }

  150.     /**
  151.      * Create a Collection of Motocycle with VO transformation
  152.      * @see MotocycleDao#create(int, Collection)
  153.      */
  154.     @Override
  155.     @SuppressWarnings({"unchecked", "rawtypes"})
  156.     public Collection create(final int transform, final Collection<Motocycle> entities)
  157.         throws MotocycleDaoException
  158.     {
  159.         if (entities == null)
  160.         {
  161.             throw new IllegalArgumentException(
  162.                 "Motocycle.create - 'entities' can not be null");
  163.         }
  164.         Collection results = new ArrayList();
  165.         try
  166.         {
  167.             for (final Motocycle entity : entities)
  168.             {
  169.                 results.add(create(transform, entity));
  170.             }
  171.         }
  172.         catch (Exception ex)
  173.         {
  174.             throw new MotocycleDaoException(ex);
  175.         }
  176.         return results;
  177.     }

  178.     /**
  179.      * Create Entity Motocycle using instance attributes with no VO transformation
  180.      * @see MotocycleDao#create(String, String, String)
  181.      */
  182.     @Override
  183.     public Motocycle create(
  184.         String classCode,
  185.         String make,
  186.         String model)
  187.         throws MotocycleDaoException
  188.     {
  189.         return (Motocycle)this.create(TRANSFORM_NONE, classCode, make, model);
  190.     }

  191.     /**
  192.      * Create Entity Motocycle using instance attributes with VO transformation
  193.      * @see MotocycleDao#create(int, String, String, String)
  194.      * composite=false identifiers=1
  195.      */
  196.     @Override
  197.     public Object create(
  198.         final int transform,
  199.         String classCode,
  200.         String make,
  201.         String model)
  202.         throws MotocycleDaoException
  203.     {
  204.         Motocycle entity = new Motocycle();
  205.         entity.setClassCode(classCode);
  206.         entity.setMake(make);
  207.         entity.setModel(model);
  208.         return this.create(transform, entity);
  209.     }

  210.     /**
  211.      * @see MotocycleDao#update(Motocycle)
  212.      */
  213.     @Override
  214.     public void update(Motocycle motocycle)
  215.         throws MotocycleDaoException
  216.     {
  217.         if (motocycle == null)
  218.         {
  219.             throw new IllegalArgumentException(
  220.                 "Motocycle.update - 'motocycle' can not be null");
  221.         }
  222.         try
  223.         {
  224.             this.emanager.merge(motocycle);
  225.             this.emanager.flush();
  226.         }
  227.         catch (Exception ex)
  228.         {
  229.             throw new MotocycleDaoException(ex);
  230.         }
  231.     }

  232.     /**
  233.      * @see MotocycleDao#update(Collection)
  234.      */
  235.     @Override
  236.     public void update(final Collection<Motocycle> entities)
  237.         throws MotocycleDaoException
  238.     {
  239.         if (entities == null)
  240.         {
  241.             throw new IllegalArgumentException(
  242.                 "Motocycle.update - 'entities' can not be null");
  243.         }
  244.         try
  245.         {
  246.             for (final Motocycle entity : entities)
  247.             {
  248.                 update(entity);
  249.             }
  250.         }
  251.         catch (Exception ex)
  252.         {
  253.             throw new MotocycleDaoException(ex);
  254.         }
  255.     }

  256.     /**
  257.      * @see MotocycleDao#remove(Motocycle)
  258.      */
  259.     @Override
  260.     public void remove(Motocycle motocycle)
  261.         throws MotocycleDaoException
  262.     {
  263.         if (motocycle == null)
  264.         {
  265.             throw new IllegalArgumentException(
  266.                 "Motocycle.remove - 'motocycle' can not be null");
  267.         }
  268.         try
  269.         {
  270.             this.emanager.remove(motocycle);
  271.             this.emanager.flush();
  272.         }
  273.         catch (Exception ex)
  274.         {
  275.             throw new MotocycleDaoException(ex);
  276.         }
  277.     }

  278.     /**
  279.      * @see MotocycleDao#remove(String)
  280.      */
  281.     @Override
  282.     public void remove(String license)
  283.         throws MotocycleDaoException
  284.     {
  285.         if (license == null)
  286.         {
  287.             throw new IllegalArgumentException(
  288.                 "Motocycle.remove - 'license' can not be null");
  289.         }
  290.         try
  291.         {
  292.             final Motocycle entity = (Motocycle)this.load(license);
  293.             if (entity != null)
  294.             {
  295.                 this.remove(entity);
  296.             }
  297.         }
  298.         catch (Exception ex)
  299.         {
  300.             throw new MotocycleDaoException(ex);
  301.         }
  302.     }

  303.     /**
  304.      * @see MotocycleDao#remove(Collection)
  305.      */
  306.     @Override
  307.     public void remove(Collection<Motocycle> entities)
  308.         throws MotocycleDaoException
  309.     {
  310.         if (entities == null)
  311.         {
  312.             throw new IllegalArgumentException(
  313.                 "Motocycle.remove - 'entities' can not be null");
  314.         }
  315.         try
  316.         {
  317.             for (final Motocycle entity : entities)
  318.             {
  319.                 remove(entity);
  320.             }
  321.         }
  322.         catch (Exception ex)
  323.         {
  324.             throw new MotocycleDaoException(ex);
  325.         }
  326.     }

  327.     /**
  328.      * Allows transformation of entities into value objects
  329.      * (or something else for that matter), when the <code>transform</code>
  330.      * flag is set to one of the constants defined in <code>MotocycleDao</code>, please note
  331.      * that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself
  332.      * will be returned.
  333.      *
  334.      * If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed.
  335.      *
  336.      * @param transform one of the constants declared in {@link MotocycleDao}
  337.      * @param entity an entity that was found
  338.      * @return the transformed entity (i.e. new value object, etc)
  339.      * @see #transformEntities(int,Collection)
  340.      */
  341.     protected Object transformEntity(final int transform, final Motocycle entity)
  342.     {
  343.         Object target = null;
  344.         if (entity != null)
  345.         {
  346.             switch (transform)
  347.             {
  348.                 case TRANSFORM_NONE : // fall-through
  349.                 default:
  350.                     target = entity;
  351.             }
  352.         }
  353.         return target;
  354.     }

  355.     /**
  356.      * Transforms a collection of entities using the
  357.      * {@link #transformEntity(int, Motocycle)}
  358.      * method. This method does not instantiate a new collection.
  359.      * <p/>
  360.      * Transforms into the same collection as the argument, but this time containing the transformed entities
  361.      * This method is to be used internally only.
  362.      *
  363.      * @param transform one of the constants declared in <code>MotocycleDao</code>
  364.      * @param entities the collection of entities to transform
  365.      * @see #transformEntity(int, Motocycle)
  366.      */
  367.     protected void transformEntities(final int transform, final Collection<Motocycle> entities)
  368.     {
  369.         switch (transform)
  370.         {
  371.             case TRANSFORM_NONE : // fall-through
  372.                 default:
  373.                 // do nothing;
  374.         }
  375.     }


  376.     // For unit testing outside of container - persistence context not injected
  377.     /**
  378.      * @return the context
  379.      */
  380.     public SessionContext getContext()
  381.     {
  382.         return this.context;
  383.     }

  384.     /**
  385.      * @param contextIn the context to set
  386.      */
  387.     public void setContext(SessionContext contextIn)
  388.     {
  389.         this.context = contextIn;
  390.     }

  391.     /**
  392.      * @return the emanager
  393.      */
  394.     public EntityManager getEmanager()
  395.     {
  396.         return this.emanager;
  397.     }

  398.     /**
  399.      * @param emanagerIn the emanager to set
  400.      */
  401.     public void setEmanager(EntityManager emanagerIn)
  402.     {
  403.         this.emanager = emanagerIn;
  404.     }

  405.     /**
  406.      * @return the hibernateSession
  407.      */
  408.     public Session getHibernateSession()
  409.     {
  410.         return this.hibernateSession;
  411.     }

  412.     /**
  413.      * @param hibernateSessionIn the hibernateSession to set
  414.      */
  415.     public void setHibernateSession(Session hibernateSessionIn)
  416.     {
  417.         this.hibernateSession = hibernateSessionIn;
  418.     }
  419. }