RegistrationManageableServiceBase.java

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

  7. import java.util.Arrays;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import javax.annotation.Resource;
  12. import javax.ejb.Remote;
  13. import javax.ejb.SessionContext;
  14. import javax.ejb.TransactionAttribute;
  15. import javax.ejb.TransactionAttributeType;
  16. import javax.persistence.EntityManager;
  17. import javax.persistence.PersistenceContext;
  18. import javax.persistence.Query;
  19. import org.andromda.demo.ejb3.registration.Profile;
  20. import org.andromda.demo.ejb3.registration.Registration;
  21. import org.apache.commons.collections.CollectionUtils;
  22. import org.apache.commons.collections.Transformer;

  23. /**
  24.  * Autogenerated EJB manageable session bean class RegistrationManageableService.
  25.  */
  26. /**
  27.  * Do not specify the javax.ejb.${manageable.type} annotation
  28.  * Instead, define the session bean in the ejb-jar.xml descriptor
  29.  * javax.ejb.${manageable.type}
  30.  */
  31. @TransactionAttribute(TransactionAttributeType.REQUIRED)
  32. @Remote({RegistrationManageableService.class})
  33. public final class RegistrationManageableServiceBase
  34.     implements RegistrationManageableService
  35. {
  36.     // ------ Session Context Injection ------

  37.     @Resource
  38.     protected SessionContext context;

  39.     // ------ Persistence Context Injection --------

  40.     /**
  41.      * Inject persistence context demo-ejb3
  42.      */
  43.     @PersistenceContext(unitName = "demo-ejb3")
  44.     protected EntityManager emanager;

  45.     // ------------ Private Operations ----------

  46.     private List<Profile> findProfileByIds(Long[] ids)
  47.         throws RegistrationReadManageableException
  48.     {
  49.         try
  50.         {
  51.             Query query = emanager.createQuery("select entity from Profile as entity where entity.profileId in (:profileId)");
  52.             query.setParameter("profileId", Arrays.asList(ids));
  53.             return query.getResultList();
  54.         }
  55.         catch (Exception ex)
  56.         {
  57.             throw new RegistrationReadManageableException(ex);
  58.         }
  59.     }

  60.     // ------------ CRUD Operations -------------

  61.     /**
  62.      * Create operations
  63.      *
  64.      * @return Registration
  65.      * @throws RegistrationCreateManageableException
  66.      */
  67.     public Registration create(String mobileNumber, String name, String address, String description, Long registrationId, Long profile)
  68.         throws RegistrationCreateManageableException
  69.     {
  70.         if (mobileNumber == null)
  71.         {
  72.             throw new IllegalArgumentException(
  73.                 "org.andromda.demo.ejb3.registration.crud.RegistrationManageableService.create(String mobileNumber, String name, String address, String description, Long registrationId, Long profile) - 'mobileNumber' can not be null");
  74.         }

  75.         if (name == null)
  76.         {
  77.             throw new IllegalArgumentException(
  78.                 "org.andromda.demo.ejb3.registration.crud.RegistrationManageableService.create(String mobileNumber, String name, String address, String description, Long registrationId, Long profile) - 'name' can not be null");
  79.         }

  80.         if (address == null)
  81.         {
  82.             throw new IllegalArgumentException(
  83.                 "org.andromda.demo.ejb3.registration.crud.RegistrationManageableService.create(String mobileNumber, String name, String address, String description, Long registrationId, Long profile) - 'address' can not be null");
  84.         }

  85.         if (profile == null)
  86.         {
  87.             throw new IllegalArgumentException(
  88.                 "org.andromda.demo.ejb3.registration.crud.RegistrationManageableService.create(String mobileNumber, String name, String address, String description, Long registrationId, Long profile) - 'profile' can not be null");
  89.         }

  90.         final Registration entity = new Registration();
  91.         entity.setMobileNumber(mobileNumber);
  92.         entity.setName(name);
  93.         entity.setAddress(address);
  94.         entity.setDescription(description);
  95.         entity.setRegistrationId(registrationId);

  96.         try
  97.         {
  98.             Profile profileEntity = null;
  99.             if (profile != null)
  100.             {
  101.                 profileEntity = (Profile)emanager.find(Profile.class, profile);
  102.             }

  103.             if (profileEntity != null)
  104.             {
  105.                 entity.setProfile(profileEntity);
  106.             }


  107.             emanager.persist(entity);
  108.             emanager.flush();
  109.             return entity;
  110.         }
  111.         catch (Exception ex)
  112.         {
  113.             throw new RegistrationCreateManageableException(ex);
  114.         }
  115.     }

  116.     /**
  117.      * Entity read operation
  118.      *
  119.      * @return Registration
  120.      * @throws RegistrationReadManageableException
  121.      */
  122.     public Registration readById(Long registrationId)
  123.         throws RegistrationReadManageableException
  124.     {
  125.         try{
  126.             return (Registration)emanager.find(Registration.class, registrationId);
  127.         }
  128.         catch (Exception ex)
  129.         {
  130.             throw new RegistrationReadManageableException(ex);
  131.         }
  132.     }

  133.     /**
  134.      * Read operation
  135.      *
  136.      * @return List
  137.      * @throws RegistrationReadManageableException
  138.      */
  139.     public List<Registration> read(String mobileNumber, String name, String address, String description, Long registrationId, Long profile)
  140.         throws RegistrationReadManageableException
  141.     {
  142.         String logicalOperator = "";
  143.         StringBuilder buf = new StringBuilder("from Registration as entity");
  144.         buf.append(" where ");
  145.         if (mobileNumber != null)
  146.         {
  147.             buf.append(logicalOperator);
  148.             buf.append("entity.mobileNumber like :mobileNumber");
  149.             logicalOperator = " and ";
  150.         }
  151.         if (name != null)
  152.         {
  153.             buf.append(logicalOperator);
  154.             buf.append("entity.name like :name");
  155.             logicalOperator = " and ";
  156.         }
  157.         if (address != null)
  158.         {
  159.             buf.append(logicalOperator);
  160.             buf.append("entity.address like :address");
  161.             logicalOperator = " and ";
  162.         }
  163.         if (description != null)
  164.         {
  165.             buf.append(logicalOperator);
  166.             buf.append("entity.description like :description");
  167.             logicalOperator = " and ";
  168.         }
  169.         if (registrationId != null)
  170.         {
  171.             buf.append(logicalOperator);
  172.             buf.append("entity.registrationId = :registrationId");
  173.             logicalOperator = " and ";
  174.         }
  175.         if (profile != null)
  176.         {
  177.             buf.append(logicalOperator);
  178.             buf.append("entity.profile = :profile");
  179.             logicalOperator = " and ";
  180.         }

  181.         try
  182.         {
  183.             final Query query = emanager.createQuery(buf.toString());
  184.             if (profile != null)
  185.             {
  186.                 query.setParameter("profile", profile);
  187.             }
  188.             query.setMaxResults(250);
  189.             return query.getResultList();
  190.         }
  191.         catch (Exception ex)
  192.         {
  193.             throw new RegistrationReadManageableException(ex);
  194.         }
  195.     }

  196.     /**
  197.      * Read all operation
  198.      *
  199.      * @return List
  200.      * @throws RegistrationReadManageableException
  201.      */
  202.     public List<Registration> readAll()
  203.         throws RegistrationReadManageableException
  204.     {
  205.         try
  206.         {
  207.             Query query = emanager.createQuery("select entity from Registration as entity");
  208.             query.setMaxResults(250);
  209.             List<Registration> entities = query.getResultList();
  210.             CollectionUtils.transform(entities, new Transformer()
  211.             {
  212.                 public Object transform(final Object object)
  213.                 {
  214.                     Registration result = (Registration)object;
  215.                     if (result.getProfile() != null)
  216.                     {
  217.                         result.setProfileLabel(result.getProfile().getCode());
  218.                     }
  219.                     return result;
  220.                 }
  221.             });
  222.             return entities;
  223.         }
  224.         catch (Exception ex)
  225.         {
  226.             throw new RegistrationReadManageableException(ex);
  227.         }
  228.     }

  229.     /**
  230.      * Read backing list operation
  231.      *
  232.      * @return Map
  233.      * @throws RegistrationReadManageableException
  234.      */
  235.     public Map readBackingLists()
  236.         throws RegistrationReadManageableException
  237.     {
  238.         final Map lists = new HashMap();

  239.         try
  240.         {
  241.             lists.put("profile", emanager.createQuery("select item.profileId, item.code from Profile as item order by item.code").getResultList());
  242.         }
  243.         catch (Exception ex)
  244.         {
  245.             throw new RegistrationReadManageableException(ex);
  246.         }
  247.         return lists;
  248.     }

  249.     /**
  250.      * Update Operation
  251.      *
  252.      * @return Registration
  253.      * @throws RegistrationUpdateManageableException
  254.      */
  255.     public Registration update(String mobileNumber, String name, String address, String description, Long registrationId, Long profile)
  256.         throws RegistrationUpdateManageableException
  257.     {
  258.         if (mobileNumber == null)
  259.         {
  260.             throw new IllegalArgumentException(
  261.                 "org.andromda.demo.ejb3.registration.crud.RegistrationManageableService.update(String mobileNumber, String name, String address, String description, Long registrationId, Long profile) - 'mobileNumber' can not be null");
  262.         }

  263.         if (name == null)
  264.         {
  265.             throw new IllegalArgumentException(
  266.                 "org.andromda.demo.ejb3.registration.crud.RegistrationManageableService.update(String mobileNumber, String name, String address, String description, Long registrationId, Long profile) - 'name' can not be null");
  267.         }

  268.         if (address == null)
  269.         {
  270.             throw new IllegalArgumentException(
  271.                 "org.andromda.demo.ejb3.registration.crud.RegistrationManageableService.update(String mobileNumber, String name, String address, String description, Long registrationId, Long profile) - 'address' can not be null");
  272.         }

  273.         if (profile == null)
  274.         {
  275.             throw new IllegalArgumentException(
  276.                 "org.andromda.demo.ejb3.registration.crud.RegistrationManageableService.update(String mobileNumber, String name, String address, String description, Long registrationId, Long profile) - 'profile' can not be null");
  277.         }

  278.         final Registration entity = (Registration)emanager.find(Registration.class, registrationId);
  279.         entity.setMobileNumber(mobileNumber);
  280.         entity.setName(name);
  281.         entity.setAddress(address);
  282.         entity.setDescription(description);

  283.         try
  284.         {
  285.             Profile profileEntity = null;
  286.             if (profile != null)
  287.             {
  288.                 profileEntity = (Profile)emanager.find(Profile.class, profile);
  289.             }

  290.             if (profileEntity != null)
  291.             {
  292.                 entity.setProfile(profileEntity);
  293.             }

  294.             emanager.merge(entity);
  295.             emanager.flush();
  296.             return entity;
  297.         }
  298.         catch (Exception ex)
  299.         {
  300.             throw new RegistrationUpdateManageableException(ex);
  301.         }
  302.     }

  303.     /**
  304.      * Delete operation
  305.      *
  306.      * @throws RegistrationDeleteManageableException
  307.      */
  308.     public void delete(Long[] ids)
  309.         throws RegistrationDeleteManageableException
  310.     {
  311.         if (ids == null)
  312.         {
  313.             throw new IllegalArgumentException(
  314.                 "org.andromda.demo.ejb3.registration.crud.RegistrationManageableService.delete(Long[] ids) - 'ids' can not be null");
  315.         }

  316.         try
  317.         {
  318.             final Query queryObject = emanager.createQuery("delete from Registration where registrationId in (:ids)");
  319.             queryObject.setParameter("ids", Arrays.asList(ids));
  320.             queryObject.executeUpdate();
  321.         }
  322.         catch (Exception ex)
  323.         {
  324.             throw new RegistrationDeleteManageableException(ex);
  325.         }
  326.     }

  327. }