package com.napa.pulse.service.impl; import com.napa.pulse.dao.interfaces.ProductGroupDAO; import com.napa.pulse.dto.*; import com.napa.pulse.entity.pulseui.GroupCodes; import com.napa.pulse.entity.pulseui.HierarchyItem; import com.napa.pulse.entity.pulseui.NewNumberMasterItemDto; import com.napa.pulse.entity.pulseui.ProductGroupType; import com.napa.pulse.entity.security.User; import com.napa.pulse.service.interfaces.ProductGroupService; import com.napa.pulse.utils.Utility; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Map; @Service @Transactional public class ProductGroupServiceImpl implements ProductGroupService { @Autowired private ProductGroupDAO productGroupDAO; @Override public List getProductGroups(Integer userId) { return productGroupDAO.getProductGroups(userId); } @Override public List getGroupCodeGroups(Integer userId) { return productGroupDAO.getGroupCodeGroups(userId); } @Override public ActionResultItem> getProductsByGroup(User user, Integer groupId) { if (productGroupDAO.isProductGroupForUser(user.getUserId(), groupId)) { return new ActionResultItem<>(productGroupDAO.getProductsByGroup(groupId, user)); } else { return new ActionResultItem<>("groupUnavailable"); } } @Override public ActionResultItem>> getGroupCodesByGroup(User user, Integer groupId) { if (productGroupDAO.isProductGroupForUser(user.getUserId(), groupId)) return new ActionResultItem<>(productGroupDAO.getGroupCodesByGroup(user,groupId)); else { return new ActionResultItem<>("groupUnavailable"); } } @Override public ActionResultItem> getFullHierarchy(User user) { return new ActionResultItem<>(productGroupDAO.getFullHierarchy(user)); } @Override public List getProductGroupTypes(User user) { return productGroupDAO.getProductGroupTypes(user); } @Override public List getNewNumberMasterData() { return productGroupDAO.getNewNumberMasterData(); } @Override public ActionResultItem createNewProductGroup(User user, String groupName, Integer groupType, String listOfHierarchiesString ) { // first check if the name belongs to any global group. If so do not allow the user to create a group boolean exists = productGroupDAO.checkProductGroupExists(groupName, user.getUserId()); if (exists) { //TODO: add language translation return new ActionResultItem<>("groupNameExists"); } else { return new ActionResultItem<>(productGroupDAO.create(user.getUserId(), groupName, groupType, Utility.parseProductGroupHierarchies(listOfHierarchiesString))); } } @Override public ActionResultItem createNewGroupCodeGroup(User user, String groupName, Integer groupType, String listOfGroupCodeProductsString ) { // first check if the name belongs to any global group. If so do not allow the user to create a group boolean exists = productGroupDAO.checkProductGroupExists(groupName, user.getUserId()); if (exists) { //TODO: add language translation return new ActionResultItem<>("groupNameExists"); } else { return new ActionResultItem<>(productGroupDAO.createGroupCodeGroup(user.getUserId(), groupName, groupType, Utility.parseGroupCodeProducts(listOfGroupCodeProductsString))); } } @Override public ActionResult addProductsToGroups(User user, String productIds, String groupIds) { if (StringUtils.isEmpty(productIds)) { return new ActionResult("noProducts"); } if (StringUtils.isEmpty(groupIds)) { return new ActionResult("noGroups"); } List groupIdList = Utility.parseIntegersAsArrayList(groupIds); List newListOfHierarchies = Utility.parseProductGroupHierarchies(productIds); for (Integer gid : groupIdList) { if (productGroupDAO.isProductGroupForUser(user.getUserId(), gid)) { productGroupDAO.addProductsToGroup(user.getUserId(), gid, newListOfHierarchies); return new ActionResult(true); } else { return new ActionResult("groupUnavailable"); } } return new ActionResult(true); } @Override public ActionResult addGroupCodesToGroups(User user, String groupCodes, String groupIds) { if (StringUtils.isEmpty(groupCodes)) { return new ActionResult("noGroupCodes"); } if (StringUtils.isEmpty(groupIds)) { return new ActionResult("noGroups"); } List groupIdList = Utility.parseIntegersAsArrayList(groupIds); List newListOfGroupCodes = Utility.parseGroupCodeProducts(groupCodes); for (Integer gid : groupIdList) { if (productGroupDAO.isProductGroupForUser(user.getUserId(), gid)) { productGroupDAO.addGroupCodesToGroup(user.getUserId(), gid, newListOfGroupCodes); return new ActionResult(true); } else { return new ActionResult("groupUnavailable"); } } return new ActionResult(true); } @Override public void saveKeyline(Integer userId, Integer siteId, String productListString) { productGroupDAO.saveKeyline(userId, siteId, Utility.parseProductGroupHierarchies(productListString)); } @Override public ActionResultItem> getKeyline(Integer siteId, User user) { return new ActionResultItem<>(productGroupDAO.getKeyline(siteId, user)); } @Override public void deleteProductGrp(Integer productGroupId) { productGroupDAO.deleteProductGrp(productGroupId); } @Override public void updateProductGroup(Integer userId, Integer productGroupId, String newGroupName, Boolean newGlobalFlag, String newProductsString) { if (newProductsString != null) { productGroupDAO.updateProductGroupMapping(productGroupId, Utility.parseProductGroupHierarchies(newProductsString)); } productGroupDAO.updateProductGroup(userId, productGroupId, newGroupName, newGlobalFlag); } @Override public void updateGroupCodeGroup(Integer userId, Integer productGroupId, String newGroupName, Boolean newGlobalFlag, String newProductsString) { if (newProductsString != null) { productGroupDAO.updateGroupCodeGroupMapping(productGroupId, Utility.parseGroupCodeProducts(newProductsString)); } productGroupDAO.updateProductGroup(userId, productGroupId, newGroupName, newGlobalFlag); } @Override public void deleteGroupCodeGroup(Integer productGroupId) { productGroupDAO.deleteGroupCodeGroup(productGroupId); } public ActionResultItem>> getAllGroupCodes(){ return new ActionResultItem<>((productGroupDAO.getAllGroupCodes())); } @Override public List getNsdTypes() { return productGroupDAO.getNsdTypes(); } }