Search in sources :

Example 1 with PersistableProductOptionValue

use of com.salesmanager.shop.model.catalog.product.attribute.PersistableProductOptionValue in project shopizer by shopizer-ecommerce.

the class ProductOptionFacadeImpl method saveOptionValue.

@Override
public ReadableProductOptionValueEntity saveOptionValue(PersistableProductOptionValue optionValue, MerchantStore store, Language language) {
    Validate.notNull(optionValue, "Option value code must not be null");
    Validate.notNull(store, "Store code must not be null");
    ProductOptionValue value = new ProductOptionValue();
    if (optionValue.getId() != null && optionValue.getId().longValue() > 0) {
        value = productOptionValueService.getById(store, optionValue.getId());
        if (value == null) {
            throw new ResourceNotFoundException("ProductOptionValue [" + optionValue.getId() + "] does not exists for store [" + store.getCode() + "]");
        }
    }
    value = persistableOptionValueMapper.merge(optionValue, value, store, language);
    try {
        productOptionValueService.saveOrUpdate(value);
    } catch (ServiceException e) {
        throw new ServiceRuntimeException("Exception while saving option value", e);
    }
    ProductOptionValue optValue = productOptionValueService.getById(store, value.getId());
    // convert to readable
    ReadableProductOptionValueEntity readableProductOptionValue = new ReadableProductOptionValueEntity();
    readableProductOptionValue = readableOptionValueMapper.merge(optValue, readableProductOptionValue, store, language);
    return readableProductOptionValue;
}
Also used : ProductOptionValue(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue) PersistableProductOptionValue(com.salesmanager.shop.model.catalog.product.attribute.PersistableProductOptionValue) ServiceException(com.salesmanager.core.business.exception.ServiceException) ReadableProductOptionValueEntity(com.salesmanager.shop.model.catalog.product.attribute.api.ReadableProductOptionValueEntity) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 2 with PersistableProductOptionValue

use of com.salesmanager.shop.model.catalog.product.attribute.PersistableProductOptionValue in project shopizer by shopizer-ecommerce.

the class PersistableProductOptionValueMapper method merge.

@Override
public ProductOptionValue merge(PersistableProductOptionValue source, ProductOptionValue destination, MerchantStore store, Language language) {
    if (destination == null) {
        destination = new ProductOptionValue();
    }
    try {
        if (StringUtils.isBlank(source.getCode())) {
            if (!StringUtils.isBlank(destination.getCode())) {
                source.setCode(destination.getCode());
            }
        }
        if (!CollectionUtils.isEmpty(source.getDescriptions())) {
            for (com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValueDescription desc : source.getDescriptions()) {
                ProductOptionValueDescription description = null;
                if (!CollectionUtils.isEmpty(destination.getDescriptions())) {
                    for (ProductOptionValueDescription d : destination.getDescriptions()) {
                        if (!StringUtils.isBlank(desc.getLanguage()) && desc.getLanguage().equals(d.getLanguage().getCode())) {
                            d.setDescription(desc.getDescription());
                            d.setName(desc.getName());
                            d.setTitle(desc.getTitle());
                            if (StringUtils.isBlank(d.getName())) {
                                d.setName(d.getDescription());
                            }
                            description = d;
                            break;
                        }
                    }
                }
                // else {
                if (description == null) {
                    description = description(desc);
                    description.setProductOptionValue(destination);
                    destination.getDescriptions().add(description);
                }
            // description = description(desc);
            // description.setProductOptionValue(destination);
            // }
            // destination.getDescriptions().add(description);
            }
        }
        destination.setCode(source.getCode());
        destination.setMerchantStore(store);
        destination.setProductOptionValueSortOrder(source.getSortOrder());
        return destination;
    } catch (Exception e) {
        throw new ServiceRuntimeException("Error while converting product option", e);
    }
}
Also used : ProductOptionValue(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue) PersistableProductOptionValue(com.salesmanager.shop.model.catalog.product.attribute.PersistableProductOptionValue) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ProductOptionValueDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 3 with PersistableProductOptionValue

use of com.salesmanager.shop.model.catalog.product.attribute.PersistableProductOptionValue in project shopizer by shopizer-ecommerce.

the class ProductManagementAPIIntegrationTest method createOptionValue.

/**
 * Creates a product option value that can be used to create a product
 * attribute when creating a new product
 *
 * @throws Exception
 */
@Test
@Ignore
public void createOptionValue() throws Exception {
    final ProductOptionValueDescription description = new ProductOptionValueDescription();
    description.setLanguage("en");
    description.setName("Red");
    final List<ProductOptionValueDescription> descriptions = new ArrayList<>();
    descriptions.add(description);
    final PersistableProductOptionValue optionValue = new PersistableProductOptionValue();
    optionValue.setOrder(1);
    optionValue.setCode("colorred");
    optionValue.setDescriptions(descriptions);
    final ObjectWriter writer = new ObjectMapper().writer().withDefaultPrettyPrinter();
    final String json = writer.writeValueAsString(optionValue);
    System.out.println(json);
    /**
     * { "descriptions" : [ { "name" : "Red", "description" : null,
     * "friendlyUrl" : null, "keyWords" : null, "highlights" : null,
     * "metaDescription" : null, "title" : null, "language" : "en", "id" : 0
     * } ], "order" : 1, "code" : "color-red", "id" : 0 }
     */
    restTemplate = new RestTemplate();
    final HttpEntity<String> entity = new HttpEntity<>(json, getHeader());
    final ResponseEntity response = restTemplate.postForEntity("http://localhost:8080/sm-shop/services/private/DEFAULT/product/optionValue", entity, PersistableProductOptionValue.class);
    final PersistableProductOptionValue opt = (PersistableProductOptionValue) response.getBody();
    System.out.println("New optionValue ID : " + opt.getId());
}
Also used : PersistableProductOptionValue(com.salesmanager.shop.model.catalog.product.attribute.PersistableProductOptionValue) ResponseEntity(org.springframework.http.ResponseEntity) HttpEntity(org.springframework.http.HttpEntity) ArrayList(java.util.ArrayList) RestTemplate(org.springframework.web.client.RestTemplate) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ProductOptionValueDescription(com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValueDescription) Ignore(org.junit.Ignore) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with PersistableProductOptionValue

use of com.salesmanager.shop.model.catalog.product.attribute.PersistableProductOptionValue in project shopizer by shopizer-ecommerce.

the class ProductV2ManagementAPIIntegrationTest method createProductWithCategory.

@Test
public void createProductWithCategory() throws Exception {
    /**
     * Create a category for product association
     */
    final PersistableCategory newCategory = new PersistableCategory();
    newCategory.setCode("test-catv2");
    newCategory.setSortOrder(1);
    newCategory.setVisible(true);
    newCategory.setDepth(4);
    final Category parent = new Category();
    newCategory.setParent(parent);
    final CategoryDescription description = new CategoryDescription();
    description.setLanguage("en");
    description.setName("test-catv2");
    description.setFriendlyUrl("test-catv2");
    description.setTitle("test-catv2");
    final List<CategoryDescription> descriptions = new ArrayList<>();
    descriptions.add(description);
    newCategory.setDescriptions(descriptions);
    final HttpEntity<PersistableCategory> categoryEntity = new HttpEntity<>(newCategory, getHeader());
    final ResponseEntity<PersistableCategory> categoryResponse = testRestTemplate.postForEntity("/api/v1/private/category?store=" + Constants.DEFAULT_STORE, categoryEntity, PersistableCategory.class);
    final PersistableCategory cat = categoryResponse.getBody();
    assertTrue(categoryResponse.getStatusCode() == CREATED);
    assertNotNull(cat.getId());
    final PersistableProduct product = new PersistableProduct();
    final ArrayList<Category> categories = new ArrayList<>();
    categories.add(cat);
    product.setCategories(categories);
    ProductSpecification specifications = new ProductSpecification();
    specifications.setManufacturer(com.salesmanager.core.model.catalog.product.manufacturer.Manufacturer.DEFAULT_MANUFACTURER);
    product.setProductSpecifications(specifications);
    product.setPrice(BigDecimal.TEN);
    product.setSku("123");
    final HttpEntity<PersistableProduct> entity = new HttpEntity<>(product, getHeader());
    final ResponseEntity<PersistableProduct> response = testRestTemplate.postForEntity("/api/v2/private/product/definition?store=" + Constants.DEFAULT_STORE, entity, PersistableProduct.class);
    assertTrue(response.getStatusCode() == CREATED);
    // create options
    PersistableProductOption color = new PersistableProductOption();
    color.setCode("color");
    ProductOptionDescription colorEn = new ProductOptionDescription();
    colorEn.setName("Color");
    colorEn.setLanguage("en");
    color.getDescriptions().add(colorEn);
    final HttpEntity<PersistableProductOption> colorEntity = new HttpEntity<>(color, getHeader());
    final ResponseEntity<PersistableProductOption> colorResponse = testRestTemplate.postForEntity("/api/v1/private/product/option?store=" + Constants.DEFAULT_STORE, colorEntity, PersistableProductOption.class);
    assertTrue(colorResponse.getStatusCode() == CREATED);
    System.out.println(colorResponse.getBody().getId());
    PersistableProductOption size = new PersistableProductOption();
    size.setCode("size");
    ProductOptionDescription sizeEn = new ProductOptionDescription();
    sizeEn.setName("Size");
    sizeEn.setLanguage("en");
    size.getDescriptions().add(sizeEn);
    final HttpEntity<PersistableProductOption> sizeEntity = new HttpEntity<>(size, getHeader());
    final ResponseEntity<PersistableProductOption> sizeResponse = testRestTemplate.postForEntity("/api/v1/private/product/option?store=" + Constants.DEFAULT_STORE, sizeEntity, PersistableProductOption.class);
    assertTrue(sizeResponse.getStatusCode() == CREATED);
    System.out.println(colorResponse.getBody().getId());
    // opions values
    PersistableProductOptionValue white = new PersistableProductOptionValue();
    white.setCode("white");
    ProductOptionValueDescription whiteEn = new ProductOptionValueDescription();
    whiteEn.setName("White");
    whiteEn.setLanguage("en");
    white.getDescriptions().add(whiteEn);
    final HttpEntity<PersistableProductOptionValue> whiteEntity = new HttpEntity<>(white, getHeader());
    final ResponseEntity<PersistableProductOptionValue> whiteResponse = testRestTemplate.postForEntity("/api/v1/private/product/option?store=" + Constants.DEFAULT_STORE, whiteEntity, PersistableProductOptionValue.class);
    assertTrue(whiteResponse.getStatusCode() == CREATED);
    System.out.println(whiteResponse.getBody().getId());
    PersistableProductOptionValue medium = new PersistableProductOptionValue();
    medium.setCode("medium");
    ProductOptionValueDescription mediumEn = new ProductOptionValueDescription();
    mediumEn.setName("Medium");
    mediumEn.setLanguage("en");
    white.getDescriptions().add(mediumEn);
    // create variants - todo
    PersistableProductVariation whiteVariant = new PersistableProductVariation();
    // - todo
    PersistableProductVariation mediumVariant = new PersistableProductVariation();
// toto
// create instances
}
Also used : PersistableCategory(com.salesmanager.shop.model.catalog.category.PersistableCategory) PersistableProductOptionValue(com.salesmanager.shop.model.catalog.product.attribute.PersistableProductOptionValue) PersistableCategory(com.salesmanager.shop.model.catalog.category.PersistableCategory) Category(com.salesmanager.shop.model.catalog.category.Category) HttpEntity(org.springframework.http.HttpEntity) ArrayList(java.util.ArrayList) PersistableProductVariation(com.salesmanager.shop.model.catalog.product.variation.PersistableProductVariation) ProductSpecification(com.salesmanager.shop.model.catalog.product.ProductSpecification) ProductOptionDescription(com.salesmanager.shop.model.catalog.product.attribute.ProductOptionDescription) PersistableProduct(com.salesmanager.shop.model.catalog.product.PersistableProduct) PersistableProductOption(com.salesmanager.shop.model.catalog.product.attribute.PersistableProductOption) CategoryDescription(com.salesmanager.shop.model.catalog.category.CategoryDescription) ProductOptionValueDescription(com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValueDescription) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with PersistableProductOptionValue

use of com.salesmanager.shop.model.catalog.product.attribute.PersistableProductOptionValue in project shopizer by shopizer-ecommerce.

the class ShopProductRESTController method createProductOptionValue.

@RequestMapping(value = "/private/{store}/product/optionValue", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public PersistableProductOptionValue createProductOptionValue(@PathVariable final String store, @Valid @RequestBody PersistableProductOptionValue optionValue, HttpServletRequest request, HttpServletResponse response) throws Exception {
    try {
        MerchantStore merchantStore = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
        if (merchantStore != null) {
            if (!merchantStore.getCode().equals(store)) {
                merchantStore = null;
            }
        }
        if (merchantStore == null) {
            merchantStore = merchantStoreService.getByCode(store);
        }
        if (merchantStore == null) {
            LOGGER.error("Merchant store is null for code " + store);
            response.sendError(503, "Merchant store is null for code " + store);
            return null;
        }
        PersistableProductOptionValuePopulator populator = new PersistableProductOptionValuePopulator();
        populator.setLanguageService(languageService);
        com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue optValue = new com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue();
        populator.populate(optionValue, optValue, merchantStore, merchantStore.getDefaultLanguage());
        productOptionValueService.save(optValue);
        optionValue.setId(optValue.getId());
        return optionValue;
    } catch (Exception e) {
        LOGGER.error("Error while saving product option value", e);
        try {
            response.sendError(503, "Error while saving product option value" + e.getMessage());
        } catch (Exception ignore) {
        }
        return null;
    }
}
Also used : PersistableProductOptionValue(com.salesmanager.shop.model.catalog.product.attribute.PersistableProductOptionValue) PersistableProductOptionValuePopulator(com.salesmanager.shop.populator.catalog.PersistableProductOptionValuePopulator) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

PersistableProductOptionValue (com.salesmanager.shop.model.catalog.product.attribute.PersistableProductOptionValue)5 ProductOptionValue (com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue)2 ProductOptionValueDescription (com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValueDescription)2 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 HttpEntity (org.springframework.http.HttpEntity)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 ServiceException (com.salesmanager.core.business.exception.ServiceException)1 ProductOptionValueDescription (com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription)1 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)1 Category (com.salesmanager.shop.model.catalog.category.Category)1 CategoryDescription (com.salesmanager.shop.model.catalog.category.CategoryDescription)1 PersistableCategory (com.salesmanager.shop.model.catalog.category.PersistableCategory)1 PersistableProduct (com.salesmanager.shop.model.catalog.product.PersistableProduct)1 ProductSpecification (com.salesmanager.shop.model.catalog.product.ProductSpecification)1 PersistableProductOption (com.salesmanager.shop.model.catalog.product.attribute.PersistableProductOption)1 ProductOptionDescription (com.salesmanager.shop.model.catalog.product.attribute.ProductOptionDescription)1