// 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.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.TreeSet; 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.Person; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.Transformer; /** * Autogenerated EJB manageable session bean class PersonManageableService. */ /** * 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({PersonManageableService.class}) public final class PersonManageableServiceBase implements PersonManageableService { // ------ Session Context Injection ------ @Resource protected SessionContext context; // ------ Persistence Context Injection -------- /** * Inject persistence context howtomodel */ @PersistenceContext(unitName = "howtomodel") protected EntityManager emanager; // ------------ Private Operations ---------- private List findCarByIds(Long[] ids) throws PersonReadManageableException { try { Query query = emanager.createQuery("select entity from Car as entity where entity.id in (:id)"); query.setParameter("id", Arrays.asList(ids)); return query.getResultList(); } catch (Exception ex) { throw new PersonReadManageableException(ex); } } // ------------ CRUD Operations ------------- /** * Create operations * * @return Person * @throws PersonCreateManageableException */ public Person create(String name, Date birthDate, Long id, Long[] cars) throws PersonCreateManageableException { if (name == null) { throw new IllegalArgumentException( "org.andromda.test.howto16.a.crud.PersonManageableService.create(String name, Date birthDate, Long id, Long[] cars) - 'name' can not be null"); } if (birthDate == null) { throw new IllegalArgumentException( "org.andromda.test.howto16.a.crud.PersonManageableService.create(String name, Date birthDate, Long id, Long[] cars) - 'birthDate' can not be null"); } final Person entity = new Person(); entity.setName(name); entity.setBirthDate(birthDate); entity.setId(id); try { final List carsEntities = (cars != null && cars.length > 0) ? this.findCarByIds(cars) : Collections.EMPTY_LIST; if (carsEntities != null) { entity.setCars(new TreeSet(carsEntities)); } emanager.persist(entity); emanager.flush(); return entity; } catch (Exception ex) { throw new PersonCreateManageableException(ex); } } /** * Entity read operation * * @return Person * @throws PersonReadManageableException */ public Person readById(Long id) throws PersonReadManageableException { try{ return (Person)emanager.find(Person.class, id); } catch (Exception ex) { throw new PersonReadManageableException(ex); } } /** * Read operation * * @return List * @throws PersonReadManageableException */ public List read(String name, Date birthDate, Long id, Long[] cars) throws PersonReadManageableException { String logicalOperator = ""; StringBuilder buf = new StringBuilder("from Person as entity"); buf.append(" join entity.cars as Car "); buf.append(" where "); if (name != null) { buf.append(logicalOperator); buf.append("entity.name like :name"); logicalOperator = " and "; } if (birthDate != null) { buf.append(logicalOperator); final Calendar calendar = new GregorianCalendar(); calendar.setTime(birthDate); if (calendar.get(Calendar.HOUR) != 0 || calendar.get(Calendar.MINUTE) != 0 || calendar.get(Calendar.SECOND) != 0 || calendar.get(Calendar.MILLISECOND) != 0) { buf.append("entity.birthDate = :birthDate"); } else { buf.append("entity.birthDate between = :birthDateStart and :birthDateEnd"); } logicalOperator = " and "; } if (id != null) { buf.append(logicalOperator); buf.append("entity.id = :id"); logicalOperator = " and "; } if (cars != null && cars.length > 0) { buf.append(logicalOperator); buf.append("Car IN (:cars)"); logicalOperator = " and "; } try { final Query query = emanager.createQuery(buf.toString()); if (cars != null && cars.length > 0) { query.setParameter("cars", Arrays.asList(cars)); } query.setMaxResults(250); return query.getResultList(); } catch (Exception ex) { throw new PersonReadManageableException(ex); } } /** * Read all operation * * @return List * @throws PersonReadManageableException */ public List readAll() throws PersonReadManageableException { try { Query query = emanager.createQuery("select entity from Person as entity"); query.setMaxResults(250); List entities = query.getResultList(); CollectionUtils.transform(entities, new Transformer() { public Object transform(final Object object) { Person result = (Person)object; if (result.getCars() != null) { Collection carsLabels = Collections.synchronizedCollection(new ArrayList()); for (final Iterator iterator = result.getCars().iterator(); iterator.hasNext();) { Car entity = iterator.next(); synchronized (carsLabels) { carsLabels.add(entity.getSerial()); } } result.setCarsLabels(carsLabels); } return result; } }); return entities; } catch (Exception ex) { throw new PersonReadManageableException(ex); } } /** * Read backing list operation * * @return Map * @throws PersonReadManageableException */ public Map readBackingLists() throws PersonReadManageableException { final Map lists = new HashMap(); try { lists.put("cars", emanager.createQuery("select item.id, item.serial from Car as item order by item.serial").getResultList()); } catch (Exception ex) { throw new PersonReadManageableException(ex); } return lists; } /** * Update Operation * * @return Person * @throws PersonUpdateManageableException */ public Person update(String name, Date birthDate, Long id, Long[] cars) throws PersonUpdateManageableException { if (name == null) { throw new IllegalArgumentException( "org.andromda.test.howto16.a.crud.PersonManageableService.update(String name, Date birthDate, Long id, Long[] cars) - 'name' can not be null"); } if (birthDate == null) { throw new IllegalArgumentException( "org.andromda.test.howto16.a.crud.PersonManageableService.update(String name, Date birthDate, Long id, Long[] cars) - 'birthDate' can not be null"); } final Person entity = (Person)emanager.find(Person.class, id); entity.setName(name); entity.setBirthDate(birthDate); try { final List carsEntities = (cars != null && cars.length > 0) ? this.findCarByIds(cars) : Collections.EMPTY_LIST; if (carsEntities != null) { entity.setCars(new TreeSet(carsEntities)); } emanager.merge(entity); emanager.flush(); return entity; } catch (Exception ex) { throw new PersonUpdateManageableException(ex); } } /** * Delete operation * * @throws PersonDeleteManageableException */ public void delete(Long[] ids) throws PersonDeleteManageableException { if (ids == null) { throw new IllegalArgumentException( "org.andromda.test.howto16.a.crud.PersonManageableService.delete(Long[] ids) - 'ids' can not be null"); } try { final Query queryObject = emanager.createQuery("delete from Person where id in (:ids)"); queryObject.setParameter("ids", Arrays.asList(ids)); queryObject.executeUpdate(); } catch (Exception ex) { throw new PersonDeleteManageableException(ex); } } }