Search in sources :

Example 1 with ProductModelEao

use of eu.ggnet.dwoss.spec.ee.eao.ProductModelEao in project dwoss by gg-net.

the class ProductProcessorOperation method create.

/**
 * Creates a new ProductModel and Persists it.
 * Multistep Process:
 * <ol>
 * <li>Validate and throw exception if a model with the same name exists</li>
 * <li>Selection of Family and Series
 * <ul>
 * <li>If Family is null and Series is null &rarr; find or create default Series and default Family</li>
 * <li>If only Family is null &rarr; find or create default default Family for Series</li>
 * <li>Else use supplied Family</li>
 * </ul>
 * </li>
 * <li>Persist new Model with Family</li>
 * </ol>
 *
 * @param brand     must not be null
 * @param group     must not be null
 * @param series    if null, default is used
 * @param family    if null, default is used
 * @param modelName the name of the model
 *
 * @return the persisted and detached Model
 */
@Override
public ProductModel create(final TradeName brand, final ProductGroup group, ProductSeries series, ProductFamily family, final String modelName) {
    EntityManager em = specEm;
    // 1. check if there exits a model anytwhere with the same name. -> throw Exception
    ProductModelEao modelEao = new ProductModelEao(em);
    ProductModel model = modelEao.find(modelName);
    if (model != null)
        throw new RuntimeException("There exits a model " + model + " but we want to create it");
    // 2. Select Family and Series
    if (family != null && family.getId() != 0) {
        ProductFamilyEao familyEao = new ProductFamilyEao(em);
        family = familyEao.findById(family.getId());
    } else {
        if (series != null && series.getId() != 0) {
            ProductSeriesEao seriesEao = new ProductSeriesEao(em);
            series = seriesEao.findById(series.getId());
        } else {
            ProductSeriesEao seriesEao = new ProductSeriesEao(em);
            series = seriesEao.find(brand, group, SpecPu.DEFAULT_NAME);
            if (series == null) {
                series = new ProductSeries(brand, group, SpecPu.DEFAULT_NAME);
                em.persist(series);
            }
        }
        for (ProductFamily f : series.getFamilys()) {
            if (f.getName().equals(SpecPu.DEFAULT_NAME)) {
                family = f;
            }
        }
        if (family == null) {
            family = new ProductFamily(SpecPu.DEFAULT_NAME);
            family.setSeries(series);
            em.persist(family);
        }
    }
    // 3. Create Model
    model = new ProductModel(modelName);
    model.setFamily(family);
    em.persist(model);
    return model;
}
Also used : ProductFamily(eu.ggnet.dwoss.spec.ee.entity.ProductFamily) EntityManager(javax.persistence.EntityManager) ProductFamilyEao(eu.ggnet.dwoss.spec.ee.eao.ProductFamilyEao) ProductModelEao(eu.ggnet.dwoss.spec.ee.eao.ProductModelEao) ProductSeriesEao(eu.ggnet.dwoss.spec.ee.eao.ProductSeriesEao) ProductSeries(eu.ggnet.dwoss.spec.ee.entity.ProductSeries) ProductModel(eu.ggnet.dwoss.spec.ee.entity.ProductModel)

Example 2 with ProductModelEao

use of eu.ggnet.dwoss.spec.ee.eao.ProductModelEao in project dwoss by gg-net.

the class ProductModelEaoIT method testFind.

@Test
public void testFind() throws Exception {
    utx.begin();
    em.joinTransaction();
    ProductFamily family = new ProductFamily("Family1");
    ProductSeries series = new ProductSeries(TradeName.SAMSUNG, ProductGroup.MISC, "TestSeries");
    em.persist(series);
    family.setSeries(series);
    em.persist(family);
    ProductModel model = new ProductModel("Model 1");
    model.setFamily(family);
    em.persist(model);
    utx.commit();
    utx.begin();
    em.joinTransaction();
    ProductModelEao productModelEao = new ProductModelEao(em);
    ProductModel productModel = productModelEao.find("Model 1");
    assertNotNull(productModel);
    assertEquals(model, productModel);
    assertNull(productModelEao.find("No Model"));
    utx.commit();
}
Also used : ProductFamily(eu.ggnet.dwoss.spec.ee.entity.ProductFamily) ProductModelEao(eu.ggnet.dwoss.spec.ee.eao.ProductModelEao) ProductSeries(eu.ggnet.dwoss.spec.ee.entity.ProductSeries) ProductModel(eu.ggnet.dwoss.spec.ee.entity.ProductModel) Test(org.junit.Test)

Aggregations

ProductModelEao (eu.ggnet.dwoss.spec.ee.eao.ProductModelEao)2 ProductFamily (eu.ggnet.dwoss.spec.ee.entity.ProductFamily)2 ProductModel (eu.ggnet.dwoss.spec.ee.entity.ProductModel)2 ProductSeries (eu.ggnet.dwoss.spec.ee.entity.ProductSeries)2 ProductFamilyEao (eu.ggnet.dwoss.spec.ee.eao.ProductFamilyEao)1 ProductSeriesEao (eu.ggnet.dwoss.spec.ee.eao.ProductSeriesEao)1 EntityManager (javax.persistence.EntityManager)1 Test (org.junit.Test)1