All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.akquinet.jbosscc.needle.db.testdata.AbstractTestdataBuilder Maven / Gradle / Ivy

package de.akquinet.jbosscc.needle.db.testdata;

import javax.persistence.EntityManager;

import de.akquinet.jbosscc.needle.db.transaction.TransactionHelper;

/**
 * An abstract implementation of {@link TestdataBuilder}.
 *
 * 
 *
 * Implementation example:
 *
 * public class PersonTestDataBuilder extends AbstractTestdataBuilder {
 *
 *  ...
 *
 * 	public PersonTestDataBuilder() {
 * 	 super();
 * 	}
 *
 * 	public PersonTestDataBuilder(EntityManager entityManager) {
 * 	 super(entityManager);
 * 	}
 *
 * 	public PersonTestDataBuilder withName(String name){
 * 	 this.withName = name;
 * 	 return this;
 * 	}
 *
 * 	public PersonTestDataBuilder withAddress(Address address){
 * 	 this.withName = name;
 * 	 return this;
 * 	}
 *
 * 	public Person build() {
 * 	 Person person = new Person();
 * 	 ...
 * 	 return person;
 * 	}
 *
 *
 * Usage example:
 *
 * Person transientPerson = new PersonTestDataBuilder(em).build();
 * Person persistedPerson = new PersonTestDataBuilder(em).buildAndSave();
 * new PersonTestDataBuilder(em).withAddress(address).buildAndSave();
 *
 *
 *
 * 
* * @param * The type of the object to build. */ public abstract class AbstractTestdataBuilder implements TestdataBuilder { private static int count = 0; private EntityManager entityManager; private TransactionHelper transactionHelper; /** * Creates a new {@link TestdataBuilder} with persistence. * * @param entityManager {@link EntityManager} to be used by this {@link TestdataBuilder} */ public AbstractTestdataBuilder(final EntityManager entityManager) { this.entityManager = entityManager; this.transactionHelper = new TransactionHelper(entityManager); } /** * Creates a new {@link TestdataBuilder} without persistence. */ public AbstractTestdataBuilder() { } /** * Returns the EntityManager or null. * * @return {@link EntityManager} or null */ protected final EntityManager getEntityManager() { return entityManager; } /** * Returns whether the {@link TestdataBuilder} is constructed with an * {@link EntityManager} * * @return true if {@link EntityManager} is available, else false */ protected final boolean hasEntityManager() { return entityManager != null; } /** * Ensure the {@link TestdataBuilder} is constructed with an * {@link EntityManager} * * @throws IllegalStateException * if the {@link TestdataBuilder} is constructed without an * {@link EntityManager} */ protected final void ensureEntityManager() { if (entityManager == null) { throw new IllegalStateException("cannot persist w/o entity manager!"); } } /** * {@inheritDoc} Executed within a new transaction. * * @throws IllegalStateException * if the {@link TestdataBuilder} is constructed without an * {@link EntityManager} */ @Override public final T buildAndSave() { ensureEntityManager(); try { return transactionHelper.saveObject(build()); } catch (Exception e) { throw new RuntimeException(e); } } /** * Returns an integer value from a static counter. * * @return value of the static counter. */ protected final int getId() { return count++; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy