Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
|#755|+Anshul|Server side changes for update of product_group and inc…
Browse files Browse the repository at this point in the history
…luding productGroupId in product upload
  • Loading branch information
shibha committed Jun 6, 2013
1 parent 2310f33 commit e1df780
Show file tree
Hide file tree
Showing 18 changed files with 145 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public class Product extends BaseModel implements Importable {
@ImportField(mandatory = true, type = "String", name = "Product Category", nested = "code")
private ProductCategory category;

@ImportField(type = "String", name = "Product Group", nested = "code")
private ProductGroup productGroup;

@ImportField(type = "String", name = "Dosage Units", nested = "code")
private DosageUnit dosageUnit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public ProductGroupRepository(ProductGroupMapper mapper) {
this.mapper = mapper;
}

public void save(ProductGroup productGroup) {
public void insert(ProductGroup productGroup) {
try {
mapper.insert(productGroup);
} catch (DuplicateKeyException duplicateKeyException) {
Expand All @@ -45,4 +45,8 @@ public void save(ProductGroup productGroup) {
public ProductGroup getByCode(String code) {
return mapper.getByCode(code);
}

public void update(ProductGroup productGroup) {
mapper.update(productGroup);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import org.openlmis.core.domain.DosageUnit;
import org.openlmis.core.domain.Product;
import org.openlmis.core.domain.ProductForm;
import org.openlmis.core.domain.ProductGroup;
import org.openlmis.core.exception.DataException;
import org.openlmis.core.repository.mapper.ProductGroupMapper;
import org.openlmis.core.repository.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
Expand All @@ -23,15 +25,19 @@ public class ProductRepository {

ProductMapper mapper;

ProductGroupMapper productGroupMapper;

@Autowired
public ProductRepository(ProductMapper mapper) {
public ProductRepository(ProductMapper mapper, ProductGroupMapper productGroupMapper) {
this.mapper = mapper;
this.productGroupMapper = productGroupMapper;
}

public void insert(Product product) {
try {
validateAndSetDosageUnit(product);
validateAndSetProductForm(product);
validateAndSetProductGroup(product);
mapper.insert(product);
} catch (DuplicateKeyException duplicateKeyException) {
throw new DataException("Duplicate Product Code found");
Expand All @@ -45,6 +51,19 @@ public void insert(Product product) {
}
}

private void validateAndSetProductGroup(Product product) {
ProductGroup group = product.getProductGroup();
if (group == null) return;

String productGroupCode = group.getCode();
if (productGroupCode == null || productGroupCode.isEmpty()) return;

ProductGroup productGroup = productGroupMapper.getByCode(productGroupCode);
if (productGroup == null) throw new DataException("Invalid reference data 'Product Group'");

group.setId(productGroup.getId());
}

public Long getIdByCode(String code) {
Long productCode = mapper.getIdByCode(code);

Expand Down Expand Up @@ -88,10 +107,12 @@ public Product getByCode(String code) {
public void update(Product product) {
mapper.update(product);
}
public Long getDosageUnitIdForCode(String code){

public Long getDosageUnitIdForCode(String code) {
return mapper.getDosageUnitIdForCode(code);
}
public Long getProductFormIdForCode(String code){

public Long getProductFormIdForCode(String code) {
return mapper.getProductFormIdForCode(code);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@
package org.openlmis.core.repository.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.openlmis.core.domain.ProductGroup;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductGroupMapper {

@Insert("INSERT INTO product_groups(code, name) VALUES (#{code}, #{name})")
@Insert("INSERT INTO product_groups(code, name, modifiedDate, modifiedBy) VALUES (#{code}, #{name}, #{modifiedDate}, #{modifiedBy})")
@Options(useGeneratedKeys = true)
public void insert(ProductGroup productGroup);

@Select("Select * from product_groups where code=#{code}")
@Select("SELECT * FROM product_groups WHERE code=#{code}")
ProductGroup getByCode(String code);

@Update("UPDATE product_groups SET code = #{code}, name = #{name}, modifiedDate = #{modifiedDate}, modifiedBy = #{modifiedBy} WHERE id = #{id}")
void update(ProductGroup productGroup);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public interface ProductMapper {
"expectedShelfLife," +
"specialStorageInstructions," + "specialTransportInstructions," +
"active," + "fullSupply," + "tracer," + "roundToZero," + "archived," +
"packRoundingThreshold, categoryId," +
"packRoundingThreshold, categoryId, productGroupId," +
"modifiedBy, modifiedDate)" +
"VALUES(" +
"#{code}," +
Expand All @@ -58,7 +58,7 @@ public interface ProductMapper {
"#{expectedShelfLife}," +
"#{specialStorageInstructions}," + "#{specialTransportInstructions}," +
"#{active}," + "#{fullSupply}," + "#{tracer}," + "#{roundToZero}," + "#{archived}," +
"#{packRoundingThreshold}, #{category.id}, " +
"#{packRoundingThreshold}, #{category.id}, #{productGroup.id}," +
"#{modifiedBy}, #{modifiedDate})")
@Options(useGeneratedKeys = true)
Integer insert(Product product);
Expand Down Expand Up @@ -91,7 +91,7 @@ public interface ProductMapper {
"expectedShelfLife=#{expectedShelfLife},",
"specialStorageInstructions=#{specialStorageInstructions},specialTransportInstructions=#{specialTransportInstructions},",
"active=#{active},fullSupply=#{fullSupply},tracer=#{tracer},roundToZero=#{roundToZero},archived=#{archived},",
"packRoundingThreshold=#{packRoundingThreshold}, categoryId=#{category.id},",
"packRoundingThreshold=#{packRoundingThreshold}, categoryId=#{category.id}, productGroupId = #{productGroup.id},",
"modifiedBy=#{modifiedBy}, modifiedDate=#{modifiedDate} WHERE id=#{id}"})
void update(Product product);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ public ProductGroupService(ProductGroupRepository productGroupRepository) {
}

public void save(ProductGroup productGroup) {
productGroupRepository.save(productGroup);
if(productGroup.getId() == null) {
productGroupRepository.insert(productGroup);
}
productGroupRepository.update(productGroup);
}

public ProductGroup getByCode(String code) {
return productGroupRepository.getByCode(code);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import lombok.NoArgsConstructor;
import org.openlmis.core.domain.Product;
import org.openlmis.core.domain.ProductCategory;
import org.openlmis.core.domain.ProductGroup;
import org.openlmis.core.exception.DataException;
import org.openlmis.core.repository.ProductGroupRepository;
import org.openlmis.core.repository.ProductRepository;
import org.openlmis.upload.Importable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand All @@ -21,13 +22,16 @@
public class ProductService {

private ProductRepository repository;
private ProductGroupRepository productGroupRepository;
private ProductCategoryService categoryService;
public static final String INVALID_PRODUCT_CATEGORY_CODE = "product.reference.category.invalid";
private static final String INVALID_PRODUCT_GROUP_CODE = "product.reference.group.invalid";

@Autowired
public ProductService(ProductRepository repository, ProductCategoryService categoryService) {
public ProductService(ProductRepository repository, ProductCategoryService categoryService, ProductGroupRepository productGroupRepository) {
this.repository = repository;
this.categoryService = categoryService;
this.productGroupRepository = productGroupRepository;
}

public void save(Product product) {
Expand All @@ -38,18 +42,24 @@ public void save(Product product) {
return;
}

setProductFormIdAndDosageUnitId(product);
setReferenceDataForProduct(product);

repository.update(product);
}

private void setProductFormIdAndDosageUnitId(Product product) {
if(product.getForm()!=null ) {
private void setReferenceDataForProduct(Product product) {
if (product.getForm() != null) {
product.getForm().setId(repository.getProductFormIdForCode(product.getForm().getCode()));
}
if(product.getDosageUnit()!=null) {
if (product.getDosageUnit() != null) {
product.getDosageUnit().setId(repository.getDosageUnitIdForCode(product.getDosageUnit().getCode()));
}
if (product.getProductGroup() != null) {
ProductGroup productGroup = productGroupRepository.getByCode(product.getProductGroup().getCode());
if (productGroup == null) throw new DataException(INVALID_PRODUCT_GROUP_CODE);
product.getProductGroup().setId(productGroup.getId());

}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import com.natpryce.makeiteasy.Instantiator;
import com.natpryce.makeiteasy.Property;
import com.natpryce.makeiteasy.PropertyLookup;
import org.openlmis.core.domain.DosageUnit;
import org.openlmis.core.domain.Product;
import org.openlmis.core.domain.ProductCategory;
import org.openlmis.core.domain.ProductForm;
import org.openlmis.core.domain.*;

import static com.natpryce.makeiteasy.Property.newProperty;

Expand Down Expand Up @@ -68,6 +65,10 @@ public Product instantiate(PropertyLookup<Product> lookup) {
form.setCode("Tablet");
form.setId(1L);
product.setForm(form);
ProductGroup productGroup = new ProductGroup();
productGroup.setCode("PG");
productGroup.setId(1L);
product.setProductGroup(productGroup);

ProductCategory category = new ProductCategory();
category.setCode(lookup.valueOf(productCategoryCode, CATEGORY_CODE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,27 @@ public void shouldSaveProductGroup() throws Exception {
ProductGroup productGroup = new ProductGroup();
repository = new ProductGroupRepository(mapper);

repository.save(productGroup);
repository.insert(productGroup);
verify(mapper).insert(productGroup);
}

@Test
public void shouldUpdateProductGroup() throws Exception {
ProductGroup productGroup = new ProductGroup();
repository = new ProductGroupRepository(mapper);

repository.update(productGroup);
verify(mapper).update(productGroup);
}

@Test
public void shouldThrowDuplicateKeyExceptionWhenDuplicateProductGroupCodeFound() throws Exception {
expectedEx.expect(DataException.class);
expectedEx.expectMessage("Duplicate Product Group Code Found");
repository = new ProductGroupRepository(mapper);
ProductGroup productGroup = new ProductGroup();
doThrow(new DuplicateKeyException("")).when(mapper).insert(productGroup);
repository.save(productGroup);
repository.insert(productGroup);
}

@Test
Expand All @@ -63,7 +72,7 @@ public void shouldThrowDataIntegrityViolationExceptionWhenMissingMandatoryData()
repository = new ProductGroupRepository(mapper);
ProductGroup productGroup = new ProductGroup();
doThrow(new DataIntegrityViolationException("violates not-null constraint")).when(mapper).insert(productGroup);
repository.save(productGroup);
repository.insert(productGroup);
}

@Test
Expand All @@ -73,6 +82,6 @@ public void shouldThrowIncorrectDataLengthErrorWhenInvalidDataLength() throws Ex
repository = new ProductGroupRepository(mapper);
ProductGroup productGroup = new ProductGroup();
doThrow(new DataIntegrityViolationException("")).when(mapper).insert(productGroup);
repository.save(productGroup);
repository.insert(productGroup);
}
}
Loading

0 comments on commit e1df780

Please sign in to comment.