/**
   * Creates a new product.
   *
   * @param destCatalog catalog.
   * @param taxCode tax code.
   * @return created product.
   */
  protected Product createSimpleProduct(final Catalog destCatalog, final TaxCode taxCode) {
    final Product product = new ProductImpl();
    product.initialize();
    product.setLastModifiedDate(new Date());
    product.setStartDate(new Date());
    product.setCode(Utils.uniqueCode("product"));

    ProductLocaleDependantFieldsImpl f = createDependentField();
    f.setLocale(Locale.ENGLISH);
    product.addOrUpdateLocaleDependantFields(f);

    ProductType productType = new ProductTypeImpl();
    productType.setName(Utils.uniqueCode("productName"));
    productType.initialize();
    productType.setCatalog(destCatalog);
    productType.setTemplate("template1");
    productType.setTaxCode(taxCode);

    productType = persist(productType);

    productType = getPersistenceEngine().load(ProductTypeImpl.class, productType.getUidPk());
    product.setProductType(productType);

    return product;
  }
  /**
   * Creates a new category type.
   *
   * @return The new category type.
   */
  public CategoryType createCategoryType(final Catalog catalog) {
    final CategoryType catType = new CategoryTypeImpl();

    catType.setCatalog(catalog);
    catType.setName(Utils.uniqueCode("cat_name"));
    catType.setTemplate("templ");
    catType.setGuid(Utils.uniqueCode("guid"));

    doInTransaction(
        new TransactionCallback<CategoryType>() {
          @Override
          public CategoryType doInTransaction(final TransactionStatus arg0) {
            EntityManager entityManager =
                ((JpaSessionImpl) getPersistenceEngine().getPersistenceSession())
                    .getEntityManager();
            if (!entityManager.contains(catType.getCatalog())) {
              catType.setCatalog(
                  entityManager.find(CatalogImpl.class, catType.getCatalog().getUidPk()));
            }
            getPersistenceEngine().save(catType);
            return null;
          }
        });

    return catType;
  }
  /**
   * Creates and persists catalog.
   *
   * @param code catalog code as String.
   * @return persisted catalog.
   */
  protected Catalog createPersistedCatalog(final String code) {
    final Catalog catalog = new CatalogImpl();
    catalog.setMaster(true);
    catalog.setCode(Utils.uniqueCode("catalog"));
    catalog.addSupportedLocale(Locale.JAPANESE);
    catalog.setDefaultLocale(Locale.getDefault());
    //		catalog.setDefaultCurrency(Currency.getInstance(Locale.CANADA));
    catalog.setName(catalog.getCode());

    // We don't cascade persist from product onto catalog, so make sure it exists
    getTxTemplate()
        .setPropagationBehaviorName(
            DefaultTransactionDefinition.PREFIX_PROPAGATION + Propagation.REQUIRES_NEW.name());
    getTxTemplate()
        .execute(
            new TransactionCallback<Catalog>() {
              @Override
              public Catalog doInTransaction(final TransactionStatus arg0) {
                getPersistenceEngine().save(catalog);
                return catalog;
              }
            });

    return catalog;
  }
  /**
   * Creates simple store.
   *
   * @param country The store country.
   * @param name The store name.
   * @param storeType The StoreType value.
   * @param subCountry The province or state.
   * @param url The store URL.
   * @param timeZone The store time zone.
   * @param code The unique store code.
   * @param emailSenderName The email sender name.
   * @param emailSenderAddress The email sender address.
   * @param storeAdminEmailAddress The admin email address.
   * @param catalog The store default catalog.
   * @param defaultLocale The default locale.
   * @param defaultCurrency The default currency of the store.
   * @return The newly persisted store.
   */
  public Store createStore(
      final String country,
      final String name,
      final StoreType storeType,
      final String subCountry,
      final String url,
      final TimeZone timeZone,
      final String code,
      final String emailSenderName,
      final String emailSenderAddress,
      final String storeAdminEmailAddress,
      final Catalog catalog,
      final Locale defaultLocale,
      final Currency defaultCurrency) {

    final Store store = getBeanFactory().getBean(ContextIdNames.STORE);
    store.setCountry(country);
    store.setName(name);
    store.setStoreType(storeType);
    store.setSubCountry(subCountry);
    store.setUrl(url);
    store.setTimeZone(timeZone);
    store.setCode(Utils.uniqueCode(code));
    store.setEmailSenderName(emailSenderName);
    store.setEmailSenderAddress(emailSenderAddress);
    store.setStoreAdminEmailAddress(storeAdminEmailAddress);
    store.setCatalog(catalog);
    store.setDefaultLocale(defaultLocale);
    store.setDefaultCurrency(defaultCurrency);

    return store;
  }
  /**
   * Creates a catalog using the given values.
   *
   * @param name The catalog name.
   * @param defaultLocale The catalog locale.
   * @param defaultCurrency The catalog default currency.
   * @return The newly persisted catalog.
   */
  public Catalog createCatalog(
      final String name, final Locale defaultLocale, final Currency defaultCurrency) {
    final Catalog catalog = new CatalogImpl();
    catalog.setCode(Utils.uniqueCode("catalog"));
    catalog.setDefaultLocale(defaultLocale);
    catalog.setName(name);

    getTxTemplate()
        .execute(
            new TransactionCallback<Catalog>() {
              @Override
              public Catalog doInTransaction(final TransactionStatus arg0) {
                getPersistenceEngine().save(catalog);
                return catalog;
              }
            });

    return catalog;
  }
  /**
   * Creates a new category.
   *
   * @return The newly created category.
   */
  public Category createCategory(final Catalog catalog, final CategoryType categoryType) {
    final Category category = new CategoryImpl();
    category.initialize();
    category.setStartDate(new Date());
    category.setCategoryType(categoryType);
    category.setCode(Utils.uniqueCode("cat_code"));
    category.setCatalog(catalog);

    doInTransaction(
        new TransactionCallback<Category>() {

          @Override
          public Category doInTransaction(final TransactionStatus arg0) {
            getPersistenceEngine().save(category);
            return null;
          }
        });

    return category;
  }
 /**
  * Creates tax code.
  *
  * @return tax code.
  */
 protected TaxCode createPersistedTaxCode() {
   return createPersistedTaxCode(Utils.uniqueCode("tax"));
 }