use of com.salesmanager.shop.model.catalog.product.attribute.PersistableProductAttribute in project shopizer by shopizer-ecommerce.
the class ProductOptionFacadeImpl method saveAttribute.
@Override
public ReadableProductAttributeEntity saveAttribute(Long productId, PersistableProductAttribute attribute, MerchantStore store, Language language) {
Validate.notNull(productId, "Product id cannot be null");
Validate.notNull(attribute, "ProductAttribute cannot be null");
Validate.notNull(attribute.getOption(), "ProductAttribute option cannot be null");
Validate.notNull(attribute.getOptionValue(), "ProductAttribute option value cannot be null");
Validate.notNull(store, "Store cannot be null");
attribute.setProductId(productId);
ProductAttribute attr = new ProductAttribute();
if (attribute.getId() != null && attribute.getId().longValue() > 0) {
attr = productAttributeService.getById(attribute.getId());
if (attr == null) {
throw new ResourceNotFoundException("Product attribute [" + attribute.getId() + "] not found");
}
if (productId != attr.getProduct().getId().longValue()) {
throw new ResourceNotFoundException("Product attribute [" + attribute.getId() + "] not found for product [" + productId + "]");
}
}
attr = persistableProductAttributeMapper.merge(attribute, attr, store, language);
try {
productAttributeService.saveOrUpdate(attr);
} catch (ServiceException e) {
throw new ServiceRuntimeException("Exception while saving ProductAttribute", e);
}
// refresh
attr = productAttributeService.getById(attr.getId());
ReadableProductAttributeEntity readable = readableProductAttributeMapper.convert(attr, store, language);
return readable;
}
use of com.salesmanager.shop.model.catalog.product.attribute.PersistableProductAttribute in project shopizer by shopizer-ecommerce.
the class ProductOptionFacadeImpl method createAttributes.
@Override
public List<CodeEntity> createAttributes(List<PersistableProductAttribute> attributes, Long productId, MerchantStore store) {
Validate.notNull(productId, "Product id must not be null");
Validate.notNull(store, "Merchant cannot be null");
// convert to model
List<ProductAttribute> modelAttributes = attributes.stream().map(attr -> persistableProductAttributeMapper.convert(attr, store, null)).collect(Collectors.toList());
try {
productAttributeService.saveAll(modelAttributes);
// save to a product
Product product = this.product(productId, store);
product.getAttributes().addAll(modelAttributes);
productService.save(product);
} catch (ServiceException e) {
throw new ServiceRuntimeException("Exception while saving product with attributes", e);
}
return modelAttributes.stream().map(e -> codeEntity(e)).collect(Collectors.toList());
}
Aggregations