All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.mosip.kernel.masterdata.service.impl.MachineSpecificationServiceImpl Maven / Gradle / Ivy

There is a newer version: 1.2.1.0
Show newest version
package io.mosip.kernel.masterdata.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import io.mosip.kernel.core.dataaccess.exception.DataAccessLayerException;
import io.mosip.kernel.masterdata.constant.MachineSpecificationErrorCode;
import io.mosip.kernel.masterdata.constant.MasterDataConstant;
import io.mosip.kernel.masterdata.dto.FilterData;
import io.mosip.kernel.masterdata.dto.MachineSpecificationDto;
import io.mosip.kernel.masterdata.dto.getresponse.PageDto;
import io.mosip.kernel.masterdata.dto.getresponse.extn.MachineSpecificationExtnDto;
import io.mosip.kernel.masterdata.dto.postresponse.IdResponseDto;
import io.mosip.kernel.masterdata.dto.request.FilterDto;
import io.mosip.kernel.masterdata.dto.request.FilterValueDto;
import io.mosip.kernel.masterdata.dto.request.Pagination;
import io.mosip.kernel.masterdata.dto.request.SearchDto;
import io.mosip.kernel.masterdata.dto.request.SearchFilter;
import io.mosip.kernel.masterdata.dto.request.SearchSort;
import io.mosip.kernel.masterdata.dto.response.ColumnCodeValue;
import io.mosip.kernel.masterdata.dto.response.FilterResponseCodeDto;
import io.mosip.kernel.masterdata.dto.response.PageResponseDto;
import io.mosip.kernel.masterdata.entity.Machine;
import io.mosip.kernel.masterdata.entity.MachineSpecification;
import io.mosip.kernel.masterdata.entity.MachineType;
import io.mosip.kernel.masterdata.entity.id.IdAndLanguageCodeID;
import io.mosip.kernel.masterdata.exception.DataNotFoundException;
import io.mosip.kernel.masterdata.exception.MasterDataServiceException;
import io.mosip.kernel.masterdata.exception.RequestException;
import io.mosip.kernel.masterdata.repository.MachineRepository;
import io.mosip.kernel.masterdata.repository.MachineSpecificationRepository;
import io.mosip.kernel.masterdata.repository.MachineTypeRepository;
import io.mosip.kernel.masterdata.service.MachineSpecificationService;
import io.mosip.kernel.masterdata.utils.AuditUtil;
import io.mosip.kernel.masterdata.utils.ExceptionUtils;
import io.mosip.kernel.masterdata.utils.MachineUtil;
import io.mosip.kernel.masterdata.utils.MapperUtils;
import io.mosip.kernel.masterdata.utils.MasterDataFilterHelper;
import io.mosip.kernel.masterdata.utils.MasterdataCreationUtil;
import io.mosip.kernel.masterdata.utils.MasterdataSearchHelper;
import io.mosip.kernel.masterdata.utils.MetaDataUtils;
import io.mosip.kernel.masterdata.utils.OptionalFilter;
import io.mosip.kernel.masterdata.utils.PageUtils;
import io.mosip.kernel.masterdata.validator.FilterColumnValidator;
import io.mosip.kernel.masterdata.validator.FilterTypeEnum;
import io.mosip.kernel.masterdata.validator.FilterTypeValidator;

/**
 * This class have methods to save a Machine Specification Details
 * 
 * @author Megha Tanga
 * @author Ayush Saxena
 * @since 1.0.0
 *
 */
@Service
public class MachineSpecificationServiceImpl implements MachineSpecificationService {

	/**
	 * Field to hold Machine Repository object
	 */
	@Autowired
	MachineSpecificationRepository machineSpecificationRepository;

	@Autowired
	MachineTypeRepository machineTypeRepository;

	@Autowired
	MachineRepository machineRepository;

	@Autowired
	private MasterdataSearchHelper masterdataSearchHelper;

	@Autowired
	private FilterTypeValidator filterValidator;

	@Autowired
	private MasterDataFilterHelper masterDataFilterHelper;

	@Autowired
	private FilterColumnValidator filterColumnValidator;

	@Autowired
	private MachineUtil machineUtil;

	@Autowired
	private PageUtils pageUtils;

	@Autowired
	private AuditUtil auditUtil;

	@Autowired
	private MasterdataCreationUtil masterdataCreationUtil;
	
	@Value("${mosip.primary-language:eng}")
	private String primaryLang;

	@Value("${mosip.secondary-language:ara}")
	private String secondaryLang;

	/*
	 * (non-Javadoc)
	 * 
	 * @see io.mosip.kernel.masterdata.service.MachineSpecificationService#
	 * createMachineSpecification(io.mosip.kernel.masterdata.dto.RequestDto)
	 */
	@Override
	public IdAndLanguageCodeID createMachineSpecification(MachineSpecificationDto machineSpecification) {

		MachineSpecification renMachineSpecification = null;


		try {
			if (StringUtils.isNotEmpty(primaryLang) && primaryLang.equals(machineSpecification.getLangCode())) {
				String uniqueId = generateId();
				machineSpecification.setId(uniqueId);
			}
			machineSpecification = masterdataCreationUtil.createMasterData(MachineSpecification.class,
					machineSpecification);
			MachineSpecification entity = MetaDataUtils.setCreateMetaData(machineSpecification,
					MachineSpecification.class);
			renMachineSpecification = machineSpecificationRepository.create(entity);
			Objects.requireNonNull(renMachineSpecification);
		} catch (DataAccessLayerException | DataAccessException | NullPointerException | IllegalArgumentException
				| IllegalAccessException | NoSuchFieldException | SecurityException e) {
			auditUtil.auditRequest(
					String.format(MasterDataConstant.FAILURE_CREATE, MachineSpecification.class.getCanonicalName()),
					MasterDataConstant.AUDIT_SYSTEM,
					String.format(MasterDataConstant.FAILURE_DESC,
							MachineSpecificationErrorCode.MACHINE_SPECIFICATION_INSERT_EXCEPTION.getErrorCode(),
							MachineSpecificationErrorCode.MACHINE_SPECIFICATION_INSERT_EXCEPTION.getErrorMessage()),
					"ADM-673");
			throw new MasterDataServiceException(
					MachineSpecificationErrorCode.MACHINE_SPECIFICATION_INSERT_EXCEPTION.getErrorCode(),
					MachineSpecificationErrorCode.MACHINE_SPECIFICATION_INSERT_EXCEPTION.getErrorMessage()
							+ ExceptionUtils.parseException(e));
		}

		IdAndLanguageCodeID idAndLanguageCodeID = new IdAndLanguageCodeID();
		MapperUtils.map(renMachineSpecification, idAndLanguageCodeID);
		auditUtil.auditRequest(String.format(MasterDataConstant.SUCCESSFUL_CREATE, MachineSpecification.class.getSimpleName()),
				MasterDataConstant.AUDIT_SYSTEM, String.format(MasterDataConstant.SUCCESSFUL_CREATE_DESC,
						MachineSpecification.class.getSimpleName(), idAndLanguageCodeID.getId()));
		return idAndLanguageCodeID;

	}
	
	private String generateId() throws DataAccessLayerException , DataAccessException{
		UUID uuid = UUID.randomUUID();
		String uniqueId = uuid.toString();
		
		MachineSpecification machineSpecification = machineSpecificationRepository
				.findMachineSpecificationByIDAndLangCode(uniqueId,primaryLang);
			
		return machineSpecification ==null?uniqueId:generateId();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see io.mosip.kernel.masterdata.service.MachineSpecificationService#
	 * updateMachineSpecification(io.mosip.kernel.masterdata.dto.RequestDto)
	 */
	@Override
	public IdAndLanguageCodeID updateMachineSpecification(MachineSpecificationDto machineSpecification) {
		MachineSpecification updMachineSpecification = null;

		try {
			MachineSpecification renMachineSpecification = machineSpecificationRepository
					.findByIdAndLangCodeIsDeletedFalseorIsDeletedIsNull(machineSpecification.getId(),
							machineSpecification.getLangCode());
			if (renMachineSpecification != null) {
				machineSpecification = masterdataCreationUtil.updateMasterData(MachineSpecification.class,
						machineSpecification);
				MetaDataUtils.setUpdateMetaData(machineSpecification, renMachineSpecification, false);
				updMachineSpecification = machineSpecificationRepository.update(renMachineSpecification);
			} else {
				auditUtil.auditRequest(
						String.format(MasterDataConstant.FAILURE_UPDATE, MachineSpecification.class.getCanonicalName()),
						MasterDataConstant.AUDIT_SYSTEM,
						String.format(MasterDataConstant.FAILURE_DESC,
								MachineSpecificationErrorCode.MACHINE_SPECIFICATION_NOT_FOUND_EXCEPTION.getErrorCode(),
								MachineSpecificationErrorCode.MACHINE_SPECIFICATION_NOT_FOUND_EXCEPTION
										.getErrorMessage()),
						"ADM-674");
				throw new RequestException(
						MachineSpecificationErrorCode.MACHINE_SPECIFICATION_NOT_FOUND_EXCEPTION.getErrorCode(),
						MachineSpecificationErrorCode.MACHINE_SPECIFICATION_NOT_FOUND_EXCEPTION.getErrorMessage());
			}
		} catch (DataAccessLayerException | DataAccessException | IllegalArgumentException | IllegalAccessException
				| NoSuchFieldException | SecurityException e) {
			auditUtil.auditRequest(
					String.format(MasterDataConstant.FAILURE_UPDATE, MachineSpecification.class.getCanonicalName()),
					MasterDataConstant.AUDIT_SYSTEM,
					String.format(MasterDataConstant.FAILURE_DESC,
							MachineSpecificationErrorCode.MACHINE_SPECIFICATION_UPDATE_EXCEPTION.getErrorCode(),
							MachineSpecificationErrorCode.MACHINE_SPECIFICATION_UPDATE_EXCEPTION.getErrorMessage()),
					"ADM-675");
			throw new MasterDataServiceException(
					MachineSpecificationErrorCode.MACHINE_SPECIFICATION_UPDATE_EXCEPTION.getErrorCode(),
					MachineSpecificationErrorCode.MACHINE_SPECIFICATION_UPDATE_EXCEPTION.getErrorMessage()
							+ ExceptionUtils.parseException(e));
		}

		IdAndLanguageCodeID idAndLanguageCodeID = new IdAndLanguageCodeID();
		MapperUtils.map(updMachineSpecification, idAndLanguageCodeID);
		auditUtil.auditRequest(String.format(MasterDataConstant.SUCCESSFUL_UPDATE, MachineSpecification.class.getSimpleName()),
				MasterDataConstant.AUDIT_SYSTEM, String.format(MasterDataConstant.SUCCESSFUL_UPDATE_DESC,
						MachineSpecification.class.getSimpleName(), idAndLanguageCodeID.getId()));
		return idAndLanguageCodeID;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see io.mosip.kernel.masterdata.service.MachineSpecificationService#
	 * deleteMachineSpecification(java.lang.String)
	 */
	@Override
	public IdResponseDto deleteMachineSpecification(String id) {
		MachineSpecification delMachineSpecification = null;
		try {
			List renMachineSpecifications = machineSpecificationRepository
					.findByIdAndIsDeletedFalseorIsDeletedIsNull(id);
			if (!renMachineSpecifications.isEmpty()) {
				for (MachineSpecification renMachineSpecification : renMachineSpecifications) {
					List renmachineList = machineRepository
							.findMachineBymachineSpecIdAndIsDeletedFalseorIsDeletedIsNull(
									renMachineSpecification.getId());
					if (renmachineList.isEmpty()) {
						MetaDataUtils.setDeleteMetaData(renMachineSpecification);
						delMachineSpecification = machineSpecificationRepository.update(renMachineSpecification);
					} else {
						auditUtil
								.auditRequest(
										String.format(MasterDataConstant.FAILURE_DECOMMISSION,
												MachineSpecification.class.getCanonicalName()),
										MasterDataConstant.AUDIT_SYSTEM,
										String.format(MasterDataConstant.FAILURE_DESC,
												MachineSpecificationErrorCode.MACHINE_DELETE_DEPENDENCY_EXCEPTION
														.getErrorCode(),
												MachineSpecificationErrorCode.MACHINE_DELETE_DEPENDENCY_EXCEPTION
														.getErrorMessage()),
										"ADM-676");
						throw new MasterDataServiceException(
								MachineSpecificationErrorCode.MACHINE_DELETE_DEPENDENCY_EXCEPTION.getErrorCode(),
								MachineSpecificationErrorCode.MACHINE_DELETE_DEPENDENCY_EXCEPTION.getErrorMessage());
					}
				}
			} else {
				auditUtil.auditRequest(
						String.format(
								MasterDataConstant.FAILURE_DECOMMISSION, MachineSpecification.class.getCanonicalName()),
						MasterDataConstant.AUDIT_SYSTEM,
						String.format(MasterDataConstant.FAILURE_DESC,
								MachineSpecificationErrorCode.MACHINE_SPECIFICATION_NOT_FOUND_EXCEPTION.getErrorCode(),
								MachineSpecificationErrorCode.MACHINE_SPECIFICATION_NOT_FOUND_EXCEPTION
										.getErrorMessage()),
						"ADM-677");
				throw new RequestException(
						MachineSpecificationErrorCode.MACHINE_SPECIFICATION_NOT_FOUND_EXCEPTION.getErrorCode(),
						MachineSpecificationErrorCode.MACHINE_SPECIFICATION_NOT_FOUND_EXCEPTION.getErrorMessage());
			}
		} catch (DataAccessLayerException | DataAccessException e) {
			auditUtil.auditRequest(
					String.format(
							MasterDataConstant.FAILURE_DECOMMISSION, MachineSpecification.class.getCanonicalName()),
					MasterDataConstant.AUDIT_SYSTEM,
					String.format(MasterDataConstant.FAILURE_DESC,
							MachineSpecificationErrorCode.MACHINE_SPECIFICATION_DELETE_EXCEPTION.getErrorCode(),
							MachineSpecificationErrorCode.MACHINE_SPECIFICATION_DELETE_EXCEPTION.getErrorMessage()),
					"ADM-678");
			throw new MasterDataServiceException(
					MachineSpecificationErrorCode.MACHINE_SPECIFICATION_DELETE_EXCEPTION.getErrorCode(),
					MachineSpecificationErrorCode.MACHINE_SPECIFICATION_DELETE_EXCEPTION.getErrorMessage()
							+ ExceptionUtils.parseException(e));
		}

		IdResponseDto idResponseDto = new IdResponseDto();
		MapperUtils.map(delMachineSpecification, idResponseDto);
		return idResponseDto;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see io.mosip.kernel.masterdata.service.MachineSpecificationService#
	 * getAllMachineSpecfication(int, int, java.lang.String, java.lang.String)
	 */
	@Override
	public PageDto getAllMachineSpecfication(int pageNumber, int pageSize, String sortBy,
			String orderBy) {
		List machineSpecs = null;
		PageDto machineSpecificationPages = null;
		try {
			Page pageData = machineSpecificationRepository
					.findAll(PageRequest.of(pageNumber, pageSize, Sort.by(Direction.fromString(orderBy), sortBy)));
			if (pageData != null && pageData.getContent() != null && !pageData.getContent().isEmpty()) {
				machineSpecs = MapperUtils.mapAll(pageData.getContent(), MachineSpecificationExtnDto.class);
				machineSpecificationPages = new PageDto<>(pageData.getNumber(), pageData.getTotalPages(),
						pageData.getTotalElements(), machineSpecs);
			} else {
				throw new DataNotFoundException(
						MachineSpecificationErrorCode.MACHINE_SPECIFICATION_NOT_FOUND_EXCEPTION.getErrorCode(),
						MachineSpecificationErrorCode.MACHINE_SPECIFICATION_NOT_FOUND_EXCEPTION.getErrorMessage());
			}
		} catch (DataAccessLayerException | DataAccessException e) {
			throw new MasterDataServiceException(
					MachineSpecificationErrorCode.MACHINE_SPECIFICATION_FETCH_EXCEPTION.getErrorCode(),
					MachineSpecificationErrorCode.MACHINE_SPECIFICATION_FETCH_EXCEPTION.getErrorMessage()
							+ ExceptionUtils.parseException(e));
		}
		return machineSpecificationPages;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see io.mosip.kernel.masterdata.service.MachineSpecificationService#
	 * searchMachineSpecification(io.mosip.kernel.masterdata.dto.request.SearchDto)
	 */
	@Override
	public PageResponseDto searchMachineSpecification(SearchDto searchRequestDto) {
		PageResponseDto pageDto = new PageResponseDto<>();

		List machineSpecifications = null;
		List addList = new ArrayList<>();
		List removeList = new ArrayList<>();
		pageUtils.validateSortField(MachineSpecification.class, searchRequestDto.getSort());
		for (SearchFilter filter : searchRequestDto.getFilters()) {
			String column = filter.getColumnName();

			if (column.equalsIgnoreCase("machineTypeName")) {
				filter.setColumnName(MasterDataConstant.NAME);
				Page machineTypes = masterdataSearchHelper.searchMasterdata(MachineType.class,
						new SearchDto(Arrays.asList(filter), Collections.emptyList(), new Pagination(), null), null);
				removeList.add(filter);
				addList.addAll(buildMachineTypeSearchFilter(machineTypes.getContent()));
				if (addList.isEmpty()) {
					return pageDto;
				}
			}
		}
		searchRequestDto.getFilters().removeAll(removeList);
		Pagination pagination = searchRequestDto.getPagination();
		List sort = searchRequestDto.getSort();
		searchRequestDto.setPagination(new Pagination(0, Integer.MAX_VALUE));
		searchRequestDto.setSort(Collections.emptyList());
		if (filterValidator.validate(MachineSpecificationExtnDto.class, searchRequestDto.getFilters())) {
			OptionalFilter optionalFilter = new OptionalFilter(addList);
			Page page = masterdataSearchHelper.searchMasterdata(MachineSpecification.class,
					searchRequestDto, new OptionalFilter[] { optionalFilter });
			if (page.getContent() != null && !page.getContent().isEmpty()) {
				machineSpecifications = MapperUtils.mapAll(page.getContent(), MachineSpecificationExtnDto.class);
				setMachineTypeName(machineSpecifications);
				pageDto = pageUtils.sortPage(machineSpecifications, sort, pagination);
			}
		}
		return pageDto;
	}

	private void setMachineTypeName(List machinesSpecifications) {
		List machineTypes = machineUtil.getMachineTypes();
		machinesSpecifications.forEach(machineSpec -> {
			machineTypes.forEach(mt -> {
				if (machineSpec.getMachineTypeCode().equals(mt.getCode())
						&& machineSpec.getLangCode().equals(mt.getLangCode())) {
					machineSpec.setMachineTypeName(mt.getName());
				}
			});
		});
	}

	private List buildMachineTypeSearchFilter(List machineTypes) {
		if (machineTypes != null && !machineTypes.isEmpty())
			return machineTypes.stream().filter(Objects::nonNull).map(this::buildMachineType)
					.collect(Collectors.toList());
		return Collections.emptyList();
	}

	private SearchFilter buildMachineType(MachineType machineType) {
		SearchFilter filter = new SearchFilter();
		filter.setColumnName("machineTypeCode");
		filter.setType(FilterTypeEnum.EQUALS.name());
		filter.setValue(machineType.getCode());
		return filter;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see io.mosip.kernel.masterdata.service.MachineSpecificationService#
	 * machineSpecificationFilterValues(io.mosip.kernel.masterdata.dto.request.
	 * FilterValueDto)
	 */
	@Override
	public FilterResponseCodeDto machineSpecificationFilterValues(FilterValueDto filterValueDto) {
		FilterResponseCodeDto filterResponseDto = new FilterResponseCodeDto();
		List columnValueList = new ArrayList<>();
		if (filterColumnValidator.validate(FilterDto.class, filterValueDto.getFilters(), MachineSpecification.class)) {
			for (FilterDto filterDto : filterValueDto.getFilters()) {
				List filterValues = masterDataFilterHelper.filterValuesWithCode(MachineSpecification.class, filterDto,
						filterValueDto,"id");
				filterValues.forEach(filterValue -> {
					ColumnCodeValue columnValue = new ColumnCodeValue();
					columnValue.setFieldCode(filterValue.getFieldCode());
					columnValue.setFieldID(filterDto.getColumnName());
					columnValue.setFieldValue(filterValue.getFieldValue());
					columnValueList.add(columnValue);
				});
			}
			filterResponseDto.setFilters(columnValueList);
		}
		return filterResponseDto;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy