// license-header java merge-point // // Attention: Generated code! Do not modify by hand! // Generated by crud/ManageableServiceBase.vsl in andromda-ejb3-cartridge on 09/18/2014 16:56:11. // package org.andromda.test.howto16.a.crud; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.ejb.Remote; import javax.ejb.SessionContext; import javax.ejb.TransactionAttribute; import javax.ejb.TransactionAttributeType; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import org.andromda.test.howto16.a.Car; import org.andromda.test.howto16.a.CarType; import org.andromda.test.howto16.a.Person; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.Transformer; /** * Autogenerated EJB manageable session bean class CarManageableService. */ /** * Do not specify the javax.ejb.${manageable.type} annotation * Instead, define the session bean in the ejb-jar.xml descriptor * javax.ejb.${manageable.type} */ @TransactionAttribute(TransactionAttributeType.REQUIRED) @Remote({CarManageableService.class}) public final class CarManageableServiceBase implements CarManageableService { // ------ Session Context Injection ------ @Resource protected SessionContext context; // ------ Persistence Context Injection -------- /** * Inject persistence context howtomodel */ @PersistenceContext(unitName = "howtomodel") protected EntityManager emanager; // ------------ Private Operations ---------- private List findPersonByIds(Long[] ids) throws CarReadManageableException { try { Query query = emanager.createQuery("select entity from Person as entity where entity.id in (:id)"); query.setParameter("id", Arrays.asList(ids)); return query.getResultList(); } catch (Exception ex) { throw new CarReadManageableException(ex); } } // ------------ CRUD Operations ------------- /** * Create operations * * @return Car * @throws CarCreateManageableException */ public Car create(String serial, String name, CarType type, Long id, Long owner) throws CarCreateManageableException { if (serial == null) { throw new IllegalArgumentException( "org.andromda.test.howto16.a.crud.CarManageableService.create(String serial, String name, CarType type, Long id, Long owner) - 'serial' can not be null"); } if (name == null) { throw new IllegalArgumentException( "org.andromda.test.howto16.a.crud.CarManageableService.create(String serial, String name, CarType type, Long id, Long owner) - 'name' can not be null"); } if (type == null) { throw new IllegalArgumentException( "org.andromda.test.howto16.a.crud.CarManageableService.create(String serial, String name, CarType type, Long id, Long owner) - 'type' can not be null"); } if (owner == null) { throw new IllegalArgumentException( "org.andromda.test.howto16.a.crud.CarManageableService.create(String serial, String name, CarType type, Long id, Long owner) - 'owner' can not be null"); } final Car entity = new Car(); entity.setSerial(serial); entity.setName(name); entity.setType(type); entity.setId(id); try { Person ownerEntity = null; if (owner != null) { ownerEntity = (Person)emanager.find(Person.class, owner); } if (ownerEntity != null) { entity.setOwner(ownerEntity); } emanager.persist(entity); emanager.flush(); return entity; } catch (Exception ex) { throw new CarCreateManageableException(ex); } } /** * Entity read operation * * @return Car * @throws CarReadManageableException */ public Car readById(Long id) throws CarReadManageableException { try{ return (Car)emanager.find(Car.class, id); } catch (Exception ex) { throw new CarReadManageableException(ex); } } /** * Read operation * * @return List * @throws CarReadManageableException */ public List read(String serial, String name, CarType type, Long id, Long owner) throws CarReadManageableException { String logicalOperator = ""; StringBuilder buf = new StringBuilder("from Car as entity"); buf.append(" where "); if (serial != null) { buf.append(logicalOperator); buf.append("entity.serial like :serial"); logicalOperator = " and "; } if (name != null) { buf.append(logicalOperator); buf.append("entity.name like :name"); logicalOperator = " and "; } if (type != null) { buf.append(logicalOperator); buf.append("entity.type = :type"); logicalOperator = " and "; } if (id != null) { buf.append(logicalOperator); buf.append("entity.id = :id"); logicalOperator = " and "; } if (owner != null) { buf.append(logicalOperator); buf.append("entity.owner = :owner"); logicalOperator = " and "; } try { final Query query = emanager.createQuery(buf.toString()); if (owner != null) { query.setParameter("owner", owner); } query.setMaxResults(250); return query.getResultList(); } catch (Exception ex) { throw new CarReadManageableException(ex); } } /** * Read all operation * * @return List * @throws CarReadManageableException */ public List readAll() throws CarReadManageableException { try { Query query = emanager.createQuery("select entity from Car as entity"); query.setMaxResults(250); List entities = query.getResultList(); CollectionUtils.transform(entities, new Transformer() { public Object transform(final Object object) { Car result = (Car)object; if (result.getOwner() != null) { result.setOwnerLabel(result.getOwner().getName()); } return result; } }); return entities; } catch (Exception ex) { throw new CarReadManageableException(ex); } } /** * Read backing list operation * * @return Map * @throws CarReadManageableException */ public Map readBackingLists() throws CarReadManageableException { final Map lists = new HashMap(); try { lists.put("owner", emanager.createQuery("select item.id, item.name from Person as item order by item.name").getResultList()); } catch (Exception ex) { throw new CarReadManageableException(ex); } return lists; } /** * Update Operation * * @return Car * @throws CarUpdateManageableException */ public Car update(String serial, String name, CarType type, Long id, Long owner) throws CarUpdateManageableException { if (serial == null) { throw new IllegalArgumentException( "org.andromda.test.howto16.a.crud.CarManageableService.update(String serial, String name, CarType type, Long id, Long owner) - 'serial' can not be null"); } if (name == null) { throw new IllegalArgumentException( "org.andromda.test.howto16.a.crud.CarManageableService.update(String serial, String name, CarType type, Long id, Long owner) - 'name' can not be null"); } if (type == null) { throw new IllegalArgumentException( "org.andromda.test.howto16.a.crud.CarManageableService.update(String serial, String name, CarType type, Long id, Long owner) - 'type' can not be null"); } if (owner == null) { throw new IllegalArgumentException( "org.andromda.test.howto16.a.crud.CarManageableService.update(String serial, String name, CarType type, Long id, Long owner) - 'owner' can not be null"); } final Car entity = (Car)emanager.find(Car.class, id); entity.setSerial(serial); entity.setName(name); entity.setType(type); try { Person ownerEntity = null; if (owner != null) { ownerEntity = (Person)emanager.find(Person.class, owner); } if (ownerEntity != null) { entity.setOwner(ownerEntity); } emanager.merge(entity); emanager.flush(); return entity; } catch (Exception ex) { throw new CarUpdateManageableException(ex); } } /** * Delete operation * * @throws CarDeleteManageableException */ public void delete(Long[] ids) throws CarDeleteManageableException { if (ids == null) { throw new IllegalArgumentException( "org.andromda.test.howto16.a.crud.CarManageableService.delete(Long[] ids) - 'ids' can not be null"); } try { final Query queryObject = emanager.createQuery("delete from Car where id in (:ids)"); queryObject.setParameter("ids", Arrays.asList(ids)); queryObject.executeUpdate(); } catch (Exception ex) { throw new CarDeleteManageableException(ex); } } }