From 24ca06d4d29c038fc6fdae8f913ccab0e426a3f8 Mon Sep 17 00:00:00 2001 From: Ramadurai Pandian Date: Fri, 17 Jan 2020 16:12:38 +0530 Subject: [PATCH 1/2] MOS-30862_MOS-30822_Added location fetch query to get exact level --- .../repository/LocationRepository.java | 305 +-- .../utils/RegistrationCenterValidator.java | 1708 ++++++++--------- 2 files changed, 1011 insertions(+), 1002 deletions(-) diff --git a/kernel/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/LocationRepository.java b/kernel/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/LocationRepository.java index 197325892d0..2c8ba9b346e 100644 --- a/kernel/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/LocationRepository.java +++ b/kernel/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/LocationRepository.java @@ -1,148 +1,157 @@ -package io.mosip.kernel.masterdata.repository; - -import java.util.List; -import java.util.Set; - -import org.springframework.data.jpa.repository.Query; -import org.springframework.stereotype.Repository; - -import io.mosip.kernel.core.dataaccess.spi.repository.BaseRepository; -import io.mosip.kernel.masterdata.entity.Location; -import io.mosip.kernel.masterdata.entity.id.CodeAndLanguageCodeID; - -/** - * This interface is JPA repository class which interacts with database and does - * the CRUD function. It is extended from {@link BaseRepository} - * - * @author Srinivasan - * @author uday kumar - * @author Sidhant Agarwal - * - */ -@Repository -public interface LocationRepository extends BaseRepository { - - @Query("FROM Location WHERE (isDeleted is null OR isDeleted = false) AND isActive = true") - List findLocationHierarchyByIsDeletedIsNullOrIsDeletedFalse(); - - @Query(value = "FROM Location l where l.code=?1 and l.langCode=?2 and (l.isDeleted is null or l.isDeleted=false) and l.isActive = true") - List findLocationHierarchyByCodeAndLanguageCode(String locCode, String languagecode); - - @Query(value = "FROM Location l where l.code=?1 and l.langCode=?2 and (l.isDeleted is null or l.isDeleted=false)") - Location findLocationByCodeAndLanguageCode(String locCode, String languagecode); - - @Query(value = "FROM Location l where parentLocCode=?1 and langCode=?2 and (l.isDeleted is null or l.isDeleted=false) and l.isActive=true") - List findLocationHierarchyByParentLocCodeAndLanguageCode(String parentLocCode, String languageCode); - - @Query(value = "select distinct hierarchy_level, hierarchy_level_name, is_active from master.location where lang_code=?1 and (is_deleted='f' or is_deleted is null) and is_active='t' ", nativeQuery = true) - List findDistinctLocationHierarchyByIsDeletedFalse(String langCode); - - @Query(value = "FROM Location l where l.code=?1 and (l.isDeleted is null or l.isDeleted=false) and l.isActive = true") - List findByCode(String locationCode); - - /** - * - * @param hierarchyName - * - hierarchy name - * @return List - */ - @Query(value = "FROM Location l where LOWER(l.hierarchyName)=LOWER(?1) AND (l.isDeleted is null OR l.isDeleted=false) AND l.isActive=true") - List findAllByHierarchyNameIgnoreCase(String hierarchyName); - - /** - * - * @param langCode - * language code - * @param level - * hierarchy level - * @return List of Locations - * - */ - @Query(value = "FROM Location l where l.langCode=?1 and l.hierarchyLevel >=?2 and (l.isDeleted is null or l.isDeleted=false) and l.isActive = true") - List getAllLocationsByLangCodeAndLevel(String langCode, Short level); - - /** - * checks whether the location name is valid location or not - * - * @param locationName - * location name - * @return {@link Boolean} true or false - */ - @Query(value = "SELECT EXISTS(select name FROM master.location where (LOWER(name)=LOWER(?1)) and (is_active=true) and (is_deleted is null or is_deleted =false))", nativeQuery = true) - boolean isLocationNamePresent(String locationName); - - /** - * give list of the immediate Locations for the given parent location code - * - * @param locationName - * location name - * @return {@link Boolean} true or false - */ - @Query("FROM Location l where l.parentLocCode=?1 and l.langCode=?2 and l.isActive=true and (l.isDeleted is null or l.isDeleted=false)") - List findDistinctByparentLocCode(String parentLocCode, String langCode); - - /** - * give list of the immediate Locations for the given parent location code - * - * @param locationName - * location name - * @return {@link Boolean} true or false - */ - @Query("SELECT distinct l.code FROM Location l where l.parentLocCode=?1 GROUP BY l.code") - List findDistinctByparentLocCode(String parentLocCode); - - @Query(value = "select distinct name from master.location where hierarchy_level = ?1 and lang_code = ?2", nativeQuery = true) - List filterByDistinctHierarchyLevel(int hierarchyLevel, String langCode); - - @Query(value = "select name from master.location where hierarchy_level = ?1 and lang_code = ?2", nativeQuery = true) - List filterByHierarchyLevel(int hierarchyLevel, String langCode); - - @Query(value = "FROM Location WHERE langCode=?1 AND (isDeleted is null or isDeleted=false)") - List findAllByLangCodeNonDeleted(String langCode); - - @Query("FROM Location l where l.hierarchyLevel=?1 and l.name =?2 and l.langCode=?3 and l.isActive=?4 and (l.isDeleted is null or l.isDeleted=false)") - Location findLocationByHierarchyLevel(short hierachyLevel, String value, String langCode, boolean isActive); - - @Query("FROM Location l where l.langCode=?1 and l.isActive=true and (l.isDeleted is null or l.isDeleted=false)") - List findAllByLangCode(String langCode); - - @Query("FROM Location l where l.langCode=?1 and l.isActive=?2 and (l.isDeleted is null or l.isDeleted=false)") - List findAllByLangCode(String langCode,boolean isActive); - - @Query("FROM Location l where l.hierarchyLevel=?1 and lower(l.name) like ?2 and l.langCode=?3 and l.isActive=?4 and (l.isDeleted is null or l.isDeleted=false)") - List findLocationByHierarchyLevelContains(short hierarchyLevel, String value, String langCode, boolean isActive); - - @Query("FROM Location l where l.hierarchyLevel=?1 and lower(l.name) like ?2 and l.langCode=?3 and l.isActive=?4 and (l.isDeleted is null or l.isDeleted=false)") - List findLocationByHierarchyLevelStartsWith(Short hierarchyLevel, String value, String langCode, boolean isActive); - - @Query("SELECT l.hierarchyName FROM Location l where l.isActive=true and (l.isDeleted is null or l.isDeleted=false)") - List findLocationAllHierarchyNames(); - - @Query("FROM Location l where l.hierarchyName=?1 and l.langCode=?2 and l.isActive=true and (l.isDeleted is null or l.isDeleted=false)") - List findAllHierarchyNameAndNameValueForEmptyTextFilter(String hierarchyName, String langCode); - - @Query("FROM Location l where l.hierarchyName=?1 and lower(l.name) like ?2 and l.langCode=?3 and l.isActive=true and (l.isDeleted is null or l.isDeleted=false)") - List findAllHierarchyNameAndNameValueForTextFilter(String hierarchyName, String text, String langCode); - - @Query("SELECT DISTINCT l.name FROM Location l where l.hierarchyName=?1 and l.langCode=?2 and l.isActive=true and (l.isDeleted is null or l.isDeleted=false)") - List findDistinctHierarchyNameAndNameValueForEmptyTextFilter(String hierarchyName, String langCode); - - @Query("SELECT DISTINCT l.name FROM Location l where l.hierarchyName=?1 and lower(l.name) like ?2 and l.langCode=?3 and l.isActive=true and (l.isDeleted is null or l.isDeleted=false)") - List findDistinctHierarchyNameAndNameValueForTextFilter(String hierarchyName, String text, String langCode); - - @Query(value = "FROM Location l where (l.isDeleted is null or l.isDeleted=false)") - List findAllNonDeleted(); - - @Query("FROM Location l WHERE l.langCode=?1 and (l.isDeleted is null OR l.isDeleted = false) AND l.isActive = true") - List findByLangCode(String langCode); - - @Query("FROM Location l WHERE l.name=?1 AND l.hierarchyLevel=?2 ") - List findByNameAndLevel(String name, Short hierarchyLevel); - - @Query("FROM Location l WHERE l.name=?1 AND l.hierarchyLevel=?2 AND l.langCode=?3") - List findByNameAndLevelLangCode(String name, Short hierarchyLevel,String langCode); - - @Query("FROM Location l WHERE l.langCode=?1 AND l.hierarchyLevel<=?2") - Set findLocationByLangCodeLevel(String langCode, Short hierarchyLevel); - -} +package io.mosip.kernel.masterdata.repository; + +import java.util.List; +import java.util.Set; + +import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; + +import io.mosip.kernel.core.dataaccess.spi.repository.BaseRepository; +import io.mosip.kernel.masterdata.entity.Location; +import io.mosip.kernel.masterdata.entity.id.CodeAndLanguageCodeID; + +/** + * This interface is JPA repository class which interacts with database and does + * the CRUD function. It is extended from {@link BaseRepository} + * + * @author Srinivasan + * @author uday kumar + * @author Sidhant Agarwal + * + */ +@Repository +public interface LocationRepository extends BaseRepository { + + @Query("FROM Location WHERE (isDeleted is null OR isDeleted = false) AND isActive = true") + List findLocationHierarchyByIsDeletedIsNullOrIsDeletedFalse(); + + @Query(value = "FROM Location l where l.code=?1 and l.langCode=?2 and (l.isDeleted is null or l.isDeleted=false) and l.isActive = true") + List findLocationHierarchyByCodeAndLanguageCode(String locCode, String languagecode); + + @Query(value = "FROM Location l where l.code=?1 and l.langCode=?2 and (l.isDeleted is null or l.isDeleted=false)") + Location findLocationByCodeAndLanguageCode(String locCode, String languagecode); + + @Query(value = "FROM Location l where parentLocCode=?1 and langCode=?2 and (l.isDeleted is null or l.isDeleted=false) and l.isActive=true") + List findLocationHierarchyByParentLocCodeAndLanguageCode(String parentLocCode, String languageCode); + + @Query(value = "select distinct hierarchy_level, hierarchy_level_name, is_active from master.location where lang_code=?1 and (is_deleted='f' or is_deleted is null) and is_active='t' ", nativeQuery = true) + List findDistinctLocationHierarchyByIsDeletedFalse(String langCode); + + @Query(value = "FROM Location l where l.code=?1 and (l.isDeleted is null or l.isDeleted=false) and l.isActive = true") + List findByCode(String locationCode); + + /** + * + * @param hierarchyName + * - hierarchy name + * @return List + */ + @Query(value = "FROM Location l where LOWER(l.hierarchyName)=LOWER(?1) AND (l.isDeleted is null OR l.isDeleted=false) AND l.isActive=true") + List findAllByHierarchyNameIgnoreCase(String hierarchyName); + + /** + * + * @param langCode + * language code + * @param level + * hierarchy level + * @return List of Locations + * + */ + @Query(value = "FROM Location l where l.langCode=?1 and l.hierarchyLevel >=?2 and (l.isDeleted is null or l.isDeleted=false) and l.isActive = true") + List getAllLocationsByLangCodeAndLevel(String langCode, Short level); + + /** + * checks whether the location name is valid location or not + * + * @param locationName + * location name + * @return {@link Boolean} true or false + */ + @Query(value = "SELECT EXISTS(select name FROM master.location where (LOWER(name)=LOWER(?1)) and (is_active=true) and (is_deleted is null or is_deleted =false))", nativeQuery = true) + boolean isLocationNamePresent(String locationName); + + /** + * give list of the immediate Locations for the given parent location code + * + * @param locationName + * location name + * @return {@link Boolean} true or false + */ + @Query("FROM Location l where l.parentLocCode=?1 and l.langCode=?2 and l.isActive=true and (l.isDeleted is null or l.isDeleted=false)") + List findDistinctByparentLocCode(String parentLocCode, String langCode); + + /** + * give list of the immediate Locations for the given parent location code + * + * @param locationName + * location name + * @return {@link Boolean} true or false + */ + @Query("SELECT distinct l.code FROM Location l where l.parentLocCode=?1 GROUP BY l.code") + List findDistinctByparentLocCode(String parentLocCode); + + @Query(value = "select distinct name from master.location where hierarchy_level = ?1 and lang_code = ?2", nativeQuery = true) + List filterByDistinctHierarchyLevel(int hierarchyLevel, String langCode); + + @Query(value = "select name from master.location where hierarchy_level = ?1 and lang_code = ?2", nativeQuery = true) + List filterByHierarchyLevel(int hierarchyLevel, String langCode); + + @Query(value = "FROM Location WHERE langCode=?1 AND (isDeleted is null or isDeleted=false)") + List findAllByLangCodeNonDeleted(String langCode); + + @Query("FROM Location l where l.hierarchyLevel=?1 and l.name =?2 and l.langCode=?3 and l.isActive=?4 and (l.isDeleted is null or l.isDeleted=false)") + Location findLocationByHierarchyLevel(short hierachyLevel, String value, String langCode, boolean isActive); + + @Query("FROM Location l where l.langCode=?1 and l.isActive=true and (l.isDeleted is null or l.isDeleted=false)") + List findAllByLangCode(String langCode); + + @Query("FROM Location l where l.langCode=?1 and l.isActive=?2 and (l.isDeleted is null or l.isDeleted=false)") + List findAllByLangCode(String langCode,boolean isActive); + + @Query("FROM Location l where l.hierarchyLevel=?1 and lower(l.name) like ?2 and l.langCode=?3 and l.isActive=?4 and (l.isDeleted is null or l.isDeleted=false)") + List findLocationByHierarchyLevelContains(short hierarchyLevel, String value, String langCode, boolean isActive); + + @Query("FROM Location l where l.hierarchyLevel=?1 and lower(l.name) like ?2 and l.langCode=?3 and l.isActive=?4 and (l.isDeleted is null or l.isDeleted=false)") + List findLocationByHierarchyLevelStartsWith(Short hierarchyLevel, String value, String langCode, boolean isActive); + + @Query("SELECT l.hierarchyName FROM Location l where l.isActive=true and (l.isDeleted is null or l.isDeleted=false)") + List findLocationAllHierarchyNames(); + + @Query("FROM Location l where l.hierarchyName=?1 and l.langCode=?2 and l.isActive=true and (l.isDeleted is null or l.isDeleted=false)") + List findAllHierarchyNameAndNameValueForEmptyTextFilter(String hierarchyName, String langCode); + + @Query("FROM Location l where l.hierarchyName=?1 and lower(l.name) like ?2 and l.langCode=?3 and l.isActive=true and (l.isDeleted is null or l.isDeleted=false)") + List findAllHierarchyNameAndNameValueForTextFilter(String hierarchyName, String text, String langCode); + + @Query("SELECT DISTINCT l.name FROM Location l where l.hierarchyName=?1 and l.langCode=?2 and l.isActive=true and (l.isDeleted is null or l.isDeleted=false)") + List findDistinctHierarchyNameAndNameValueForEmptyTextFilter(String hierarchyName, String langCode); + + @Query("SELECT DISTINCT l.name FROM Location l where l.hierarchyName=?1 and lower(l.name) like ?2 and l.langCode=?3 and l.isActive=true and (l.isDeleted is null or l.isDeleted=false)") + List findDistinctHierarchyNameAndNameValueForTextFilter(String hierarchyName, String text, String langCode); + + @Query(value = "FROM Location l where (l.isDeleted is null or l.isDeleted=false)") + List findAllNonDeleted(); + + @Query("FROM Location l WHERE l.langCode=?1 and (l.isDeleted is null OR l.isDeleted = false) AND l.isActive = true") + List findByLangCode(String langCode); + + @Query("FROM Location l WHERE l.name=?1 AND l.hierarchyLevel=?2 ") + List findByNameAndLevel(String name, Short hierarchyLevel); + + @Query("FROM Location l WHERE l.name=?1 AND l.hierarchyLevel=?2 AND l.langCode=?3") + List findByNameAndLevelLangCode(String name, Short hierarchyLevel,String langCode); + + /** + * give list of the immediate Locations for the given parent location code + * + * @param locationCode + * location code + * @param hierarchyLevel + * hierarchy level + * @return Set + */ + @Query("FROM Location l WHERE l.langCode=?1 AND l.hierarchyLevel=?2") + Set findLocationByLangCodeLevel(String langCode, Short hierarchyLevel); + +} diff --git a/kernel/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/utils/RegistrationCenterValidator.java b/kernel/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/utils/RegistrationCenterValidator.java index 13b05990958..1e2b46528f8 100644 --- a/kernel/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/utils/RegistrationCenterValidator.java +++ b/kernel/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/utils/RegistrationCenterValidator.java @@ -1,854 +1,854 @@ -package io.mosip.kernel.masterdata.utils; - -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Optional; -import java.util.Set; -import java.util.regex.Pattern; -import java.util.stream.Collectors; - -import javax.annotation.PostConstruct; -import javax.validation.ConstraintViolation; -import javax.validation.Validation; -import javax.validation.Validator; -import javax.validation.ValidatorFactory; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; - -import io.mosip.kernel.core.exception.ServiceError; -import io.mosip.kernel.core.idgenerator.spi.MachineIdGenerator; -import io.mosip.kernel.core.idgenerator.spi.RegistrationCenterIdGenerator; -import io.mosip.kernel.masterdata.constant.MachineErrorCode; -import io.mosip.kernel.masterdata.constant.RegistrationCenterErrorCode; -import io.mosip.kernel.masterdata.constant.ValidationErrorCode; -import io.mosip.kernel.masterdata.dto.RegCenterPostReqDto; -import io.mosip.kernel.masterdata.dto.RegCenterPutReqDto; -import io.mosip.kernel.masterdata.dto.RegcenterBaseDto; -import io.mosip.kernel.masterdata.dto.getresponse.extn.RegistrationCenterExtnDto; -import io.mosip.kernel.masterdata.dto.postresponse.RegistrationCenterPostResponseDto; -import io.mosip.kernel.masterdata.entity.Holiday; -import io.mosip.kernel.masterdata.entity.Machine; -import io.mosip.kernel.masterdata.entity.RegistrationCenter; -import io.mosip.kernel.masterdata.entity.RegistrationCenterHistory; -import io.mosip.kernel.masterdata.entity.Zone; -import io.mosip.kernel.masterdata.exception.MasterDataServiceException; -import io.mosip.kernel.masterdata.exception.RequestException; -import io.mosip.kernel.masterdata.exception.ValidationException; -import io.mosip.kernel.masterdata.repository.HolidayRepository; -import io.mosip.kernel.masterdata.repository.MachineRepository; -import io.mosip.kernel.masterdata.repository.RegistrationCenterHistoryRepository; -import io.mosip.kernel.masterdata.repository.RegistrationCenterRepository; - -@Component -public class RegistrationCenterValidator { - - @Autowired - private ZoneUtils zoneUtils; - - private String negRegex; - private String posRegex; - - /** - * minimum digits after decimal point in Longitude and latitude - */ - @Value("${mosip.min-digit-longitude-latitude:4}") - private int minDegits; - - /** - * get list of secondary languages supported by MOSIP from configuration file - */ - @Value("#{'${mosip.secondary-language}'.split(',')}") - private Set secondaryLangList; - - /** - * get list of secondary languages supported by MOSIP from configuration file - */ - @Value("${mosip.primary-language}") - private String primaryLang; - - /** - * get list of secondary languages supported by MOSIP from configuration file - */ - @Value("${mosip.secondary-language}") - private String secondaryLang; - - private Set supportedLanguages; - - /** - * Constructing regex for matching the Latitude and Longitude format - */ - - @PostConstruct - public void constructRegEx() { - supportedLanguages = new HashSet<>(Arrays.asList(secondaryLang.split(","))); - supportedLanguages.add(primaryLang); - negRegex = "^(\\-\\d{1,2}\\.\\d{" + minDegits + ",})$"; - posRegex = "^(\\d{1,2}\\.\\d{" + minDegits + ",})$"; - } - - @Autowired - RegistrationCenterIdGenerator registrationCenterIdGenerator; - - @Autowired - RegistrationCenterRepository registrationCenterRepository; - - @Autowired - RegistrationCenterHistoryRepository registrationCenterHistoryRepository; - - @Autowired - HolidayRepository holidayRepository; - - - // method to compare data - public boolean isValid(T firstObj, D eachRecord, - List errors) { - - return validateCenterType(firstObj, eachRecord, errors) | valdateLatitude(firstObj, eachRecord, errors) - | validateLongitude(firstObj, eachRecord, errors) | validateContactPhone(firstObj, eachRecord, errors) - | validateWorkingHours(firstObj, eachRecord, errors) - | validateCenterStartTime(firstObj, eachRecord, errors) - | validateCenterEndTime(firstObj, eachRecord, errors) - | validateLunchStartTime(firstObj, eachRecord, errors) - | validateLunchEndTime(firstObj, eachRecord, errors) | validateTimeZone(firstObj, eachRecord, errors) - | validateHolidayCode(firstObj, eachRecord, errors) | validateZoneCode(firstObj, eachRecord, errors); - } - - - - - // method to compare Latitude - private boolean valdateLatitude(T firstObj, D eachRecord, - List errors) { - if (eachRecord.getLatitude() != null && firstObj.getLatitude() != null) { - if (eachRecord.getLatitude().trim().equalsIgnoreCase(firstObj.getLatitude().trim())) { - return true; - } else { - errors.add(new ServiceError(RegistrationCenterErrorCode.LATITUDE_NOT_UNIQUE.getErrorCode(), - String.format(RegistrationCenterErrorCode.LATITUDE_NOT_UNIQUE.getErrorMessage(), - eachRecord.getLatitude()))); - - } - } - return false; - } - - // method to compare Longitude - private boolean validateLongitude(T firstObj, D eachRecord, - List errors) { - if (eachRecord.getLongitude() != null && firstObj.getLongitude() != null) { - if (eachRecord.getLongitude().trim().equalsIgnoreCase(firstObj.getLongitude().trim())) { - return true; - } else { - errors.add(new ServiceError(RegistrationCenterErrorCode.LONGITUDE_NOT_UNIQUE.getErrorCode(), - String.format(RegistrationCenterErrorCode.LONGITUDE_NOT_UNIQUE.getErrorMessage(), - eachRecord.getLongitude()))); - - } - } - return false; - } - - // method to compare ContactPhone - private boolean validateContactPhone(T firstObj, - D eachRecord, List errors) { - if (eachRecord.getContactPhone() != null && firstObj.getContactPhone() != null) { - if (eachRecord.getContactPhone().trim().equalsIgnoreCase(firstObj.getContactPhone().trim())) { - return true; - } else { - errors.add(new ServiceError(RegistrationCenterErrorCode.CONTACT_PHONE_NOT_UNIQUE.getErrorCode(), - String.format(RegistrationCenterErrorCode.CONTACT_PHONE_NOT_UNIQUE.getErrorMessage(), - eachRecord.getContactPhone()))); - - } - } - return false; - } - - // method to compare WorkingHours - private boolean validateWorkingHours(T firstObj, - D eachRecord, List errors) { - if (eachRecord.getWorkingHours() != null && firstObj.getWorkingHours() != null) { - if (eachRecord.getWorkingHours().trim().equalsIgnoreCase(firstObj.getWorkingHours().trim())) { - return true; - } else { - errors.add(new ServiceError(RegistrationCenterErrorCode.WORKING_HOURS_NOT_UNIQUE.getErrorCode(), - String.format(RegistrationCenterErrorCode.WORKING_HOURS_NOT_UNIQUE.getErrorMessage(), - eachRecord.getWorkingHours()))); - - } - } - return false; - } - - // method to compare CenterStartTime - private boolean validateCenterStartTime(T firstObj, - D eachRecord, List errors) { - if (eachRecord.getCenterStartTime() != null && firstObj.getCenterStartTime() != null) { - if (eachRecord.getCenterStartTime().equals(firstObj.getCenterStartTime())) { - return true; - } else { - errors.add(new ServiceError(RegistrationCenterErrorCode.CENTER_STRART_TIME_NOT_UNIQUE.getErrorCode(), - String.format(RegistrationCenterErrorCode.CENTER_STRART_TIME_NOT_UNIQUE.getErrorMessage(), - eachRecord.getCenterStartTime()))); - - } - } - return false; - } - - // method to compare CenterEndTime - private boolean validateCenterEndTime(T firstObj, - D eachRecord, List errors) { - if (eachRecord.getCenterEndTime() != null && firstObj.getCenterEndTime() != null) { - if (eachRecord.getCenterEndTime().equals(firstObj.getCenterEndTime())) { - return true; - } else { - errors.add(new ServiceError(RegistrationCenterErrorCode.CENTER_END_TIME_NOT_UNIQUE.getErrorCode(), - String.format(RegistrationCenterErrorCode.CENTER_END_TIME_NOT_UNIQUE.getErrorMessage(), - eachRecord.getCenterEndTime()))); - - } - } - return false; - } - - // method to compare LunchStartTime - private boolean validateLunchStartTime(T firstObj, - D eachRecord, List errors) { - if (eachRecord.getLunchStartTime() != null && firstObj.getLunchStartTime() != null) { - if (eachRecord.getLunchStartTime().equals(firstObj.getLunchStartTime())) { - return true; - } else { - errors.add(new ServiceError(RegistrationCenterErrorCode.LUNCH_START_TIME_NOT_UNIQUE.getErrorCode(), - String.format(RegistrationCenterErrorCode.LUNCH_START_TIME_NOT_UNIQUE.getErrorMessage(), - eachRecord.getLunchStartTime()))); - - } - } - return false; - } - - // method to compare LunchEndTime - private boolean validateLunchEndTime(T firstObj, - D eachRecord, List errors) { - if (eachRecord.getLunchEndTime() != null && firstObj.getLunchEndTime() != null) { - if (eachRecord.getLunchEndTime().equals(firstObj.getLunchEndTime())) { - return true; - } else { - errors.add(new ServiceError(RegistrationCenterErrorCode.LUNCH_END_TIME_NOT_UNIQUE.getErrorCode(), - String.format(RegistrationCenterErrorCode.LUNCH_END_TIME_NOT_UNIQUE.getErrorMessage(), - eachRecord.getLunchEndTime()))); - - } - } - return false; - } - - // method to compare TimeZone - private boolean validateTimeZone(T firstObj, D eachRecord, - List errors) { - if (eachRecord.getTimeZone() != null && firstObj.getTimeZone() != null) { - if (eachRecord.getTimeZone().trim().equalsIgnoreCase(firstObj.getTimeZone().trim())) { - return true; - } else { - errors.add(new ServiceError(RegistrationCenterErrorCode.TIME_ZONE_NOT_UNIQUE.getErrorCode(), - String.format(RegistrationCenterErrorCode.TIME_ZONE_NOT_UNIQUE.getErrorMessage(), - eachRecord.getTimeZone()))); - - } - } - return false; - } - - // method to compare HolidayCode - private boolean validateHolidayCode(T firstObj, - D eachRecord, List errors) { - if (eachRecord.getHolidayLocationCode() != null && firstObj.getHolidayLocationCode() != null) { - if (eachRecord.getHolidayLocationCode().trim().equalsIgnoreCase(firstObj.getHolidayLocationCode().trim())) { - return true; - } else { - errors.add(new ServiceError(RegistrationCenterErrorCode.HOLIDAY_LOCATION_CODE_NOT_UNIQUE.getErrorCode(), - String.format(RegistrationCenterErrorCode.HOLIDAY_LOCATION_CODE_NOT_UNIQUE.getErrorMessage(), - eachRecord.getHolidayLocationCode()))); - - } - } - return false; - } - - // method to compare ZoneCode - private boolean validateZoneCode(T firstObj, D eachRecord, - List errors) { - if (eachRecord.getZoneCode() != null && firstObj.getZoneCode() != null) { - if (eachRecord.getZoneCode().trim().equalsIgnoreCase(firstObj.getZoneCode().trim())) { - return true; - } else { - errors.add(new ServiceError(RegistrationCenterErrorCode.ZONE_CODE_NOT_UNIQUE.getErrorCode(), - String.format(RegistrationCenterErrorCode.ZONE_CODE_NOT_UNIQUE.getErrorMessage(), - eachRecord.getZoneCode()))); - - } - } - return false; - } - - // method to compare CenterType - private boolean validateCenterType(T firstObj, - D eachRecord, List errors) { - if (eachRecord.getCenterTypeCode() != null && firstObj.getCenterTypeCode() != null) { - if (eachRecord.getCenterTypeCode().trim().equalsIgnoreCase(firstObj.getCenterTypeCode().trim())) { - return true; - } else { - errors.add(new ServiceError(RegistrationCenterErrorCode.CENTER_TYPE_CODE_NOT_UNIQUE.getErrorCode(), - String.format(RegistrationCenterErrorCode.CENTER_TYPE_CODE_NOT_UNIQUE.getErrorMessage(), - eachRecord.getCenterTypeCode()))); - - } - - } - return false; - } - - // ---------------------------------------------------------------------------------------------------------// - - // method to validate the format of the longitude and latitude, zone validation, - // lunch and center start and end time -// public void validateRegCenterCreateReq( -// T registrationCenterDto, List errors) { -// -// String latitude = registrationCenterDto.getLatitude(); -// String longitude = registrationCenterDto.getLongitude(); -// -// zoneUserMapValidation(registrationCenterDto, errors, getZoneIdsForUser()); -// zoneStartEndTimeGtrValidation(registrationCenterDto, errors); -// lunchStartEndTimeGrtValidation(registrationCenterDto, errors); -// formatValidationLongitudeLatitude(errors, latitude, longitude); -// -// } - - public void validateRegCenterCreate( - RegCenterPostReqDto registrationCenterDto, List errors) { - - String latitude = registrationCenterDto.getLatitude(); - String longitude = registrationCenterDto.getLongitude(); - - zoneUserMapValidation(registrationCenterDto, errors, getZoneIdsForUser()); - zoneStartEndTimeGtrValidation(registrationCenterDto, errors); - lunchStartEndTimeGrtValidation(registrationCenterDto, errors); - formatValidationLongitudeLatitude(errors, latitude, longitude); - holidayVlidation(registrationCenterDto, errors); - - } - - // validate Holiday against DB - private void holidayVlidation(RegCenterPostReqDto registrationCenterDto, List errors) { - List holidays = holidayRepository - .findHolidayByHolidayIdLocationCode(registrationCenterDto.getHolidayLocationCode()); - if (holidays.isEmpty()) { - errors.add(new ServiceError(RegistrationCenterErrorCode.HOLIDAY_NOT_FOUND.getErrorCode(), - String.format(RegistrationCenterErrorCode.HOLIDAY_NOT_FOUND.getErrorMessage(), - registrationCenterDto.getZoneCode()))); - } - } - - // list zone Id mapped with the called user - private List getZoneIdsForUser() { - List zoneIds; - List zones = zoneUtils.getUserLeafZones(primaryLang); - zoneIds = zones.parallelStream().map(Zone::getCode).collect(Collectors.toList()); - return zoneIds; - } - - // validation to check entered zoneCode is mapped with eligible user or not and - // is valid zoneCode - private void zoneUserMapValidation(RegCenterPostReqDto registrationCenterDto, List errors, - List zoneIds) { - - if (!zoneIds.isEmpty()) { - if (!zoneIds.contains(registrationCenterDto.getZoneCode())) { - errors.add(new ServiceError(RegistrationCenterErrorCode.INVALIDE_ZONE.getErrorCode(), - String.format(RegistrationCenterErrorCode.INVALIDE_ZONE.getErrorMessage(), - registrationCenterDto.getZoneCode()))); - } - } - } - - // validate to check the format of latitude and longitude - // Latitude or Longitude must have minimum 4 digits after decimal - private void formatValidationLongitudeLatitude(List errors, String latitude, String longitude) { - - if (!((Pattern.matches(negRegex, latitude) || Pattern.matches(posRegex, latitude)) - && (Pattern.matches(negRegex, longitude) || Pattern.matches(posRegex, longitude)))) { - errors.add( - new ServiceError(RegistrationCenterErrorCode.REGISTRATION_CENTER_FORMATE_EXCEPTION.getErrorCode(), - RegistrationCenterErrorCode.REGISTRATION_CENTER_FORMATE_EXCEPTION.getErrorMessage())); - } - } - - // validation to check the RegCenter Lunch Start Time is greater - // than RegCenter - // Lunch End Time - private void lunchStartEndTimeGrtValidation(RegCenterPostReqDto registrationCenterDto, - List errors) { - // validation to check the RegCenter Lunch Start Time is greater than RegCenter - // Lunch End Time - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); - LocalTime lunchStartTime = LocalTime.parse("00:00:00", formatter); - if ((registrationCenterDto.getLunchStartTime()!=null && !registrationCenterDto.getLunchStartTime().equals(lunchStartTime)) && registrationCenterDto.getLunchStartTime().isAfter(registrationCenterDto.getLunchEndTime())) { - errors.add(new ServiceError( - RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_START_END_EXCEPTION.getErrorCode(), - String.format( - RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_START_END_EXCEPTION.getErrorMessage(), - registrationCenterDto.getLunchEndTime()))); - - } - if ((registrationCenterDto.getLunchEndTime()!=null && !registrationCenterDto.getLunchEndTime().equals(lunchStartTime)) && registrationCenterDto.getLunchEndTime().isAfter(registrationCenterDto.getCenterEndTime())) { - errors.add(new ServiceError( - RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_END_CENTER_END_EXCEPTION.getErrorCode(), - String.format( - RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_END_CENTER_END_EXCEPTION.getErrorMessage(), - registrationCenterDto.getLunchEndTime()))); - - } - if((registrationCenterDto.getLunchStartTime()!=null && !registrationCenterDto.getLunchStartTime().equals(lunchStartTime)) && registrationCenterDto.getLunchStartTime().isBefore(registrationCenterDto.getCenterStartTime())) - { - errors.add(new ServiceError( - RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_START_CENTER_END_EXCEPTION.getErrorCode(), - String.format( - RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_START_CENTER_END_EXCEPTION.getErrorMessage(), - registrationCenterDto.getLunchEndTime()))); - } - } - - // validation to check the RegCenter Start Time is greater than - // RegCenter End Time - private void zoneStartEndTimeGtrValidation(RegCenterPostReqDto registrationCenterDto, - List errors) { - if (registrationCenterDto.getCenterStartTime().isAfter(registrationCenterDto.getCenterEndTime())) { - errors.add(new ServiceError( - RegistrationCenterErrorCode.REGISTRATION_CENTER_START_END_EXCEPTION.getErrorCode(), - String.format(RegistrationCenterErrorCode.REGISTRATION_CENTER_START_END_EXCEPTION.getErrorMessage(), - registrationCenterDto.getCenterEndTime()))); - } - } - - // ------------------------------------------------------------------------------------------------------------ - // method to validate the primary and secondary language input objects mandatory - // fields - public void validatePrimarySencodaryLangMandatoryFields(List reqRegistrationCenterDto, - RegistrationCenterPostResponseDto registrationCenterPostResponseDto, List inputLangCodeList, - List validateRegistrationCenterDtos, - List constraintViolationedSecList, List errors) { - List secErrors = new ArrayList<>(); - RegCenterPostReqDto firstObject = null; - - Optional defualtLangVal = reqRegistrationCenterDto.stream() - .filter(i -> i.getLangCode().equals(primaryLang)).findAny(); - - if (!defualtLangVal.isPresent()) { - throw new RequestException(RegistrationCenterErrorCode.DEFAULT_LANGUAGE.getErrorCode(), - RegistrationCenterErrorCode.DEFAULT_LANGUAGE.getErrorMessage()); - } - - for (RegCenterPostReqDto registrationCenterDto : reqRegistrationCenterDto) { - if (registrationCenterDto.getLangCode() != null - && registrationCenterDto.getLangCode().equals(primaryLang)) { - firstObject = registrationCenterDto; - } else if ((registrationCenterDto.getLangCode() != null) - && (secondaryLangList.contains(registrationCenterDto.getLangCode()))) { - firstObject = reqRegistrationCenterDto.get(0); - } - } - - constraintViolationPrimSecLangData(reqRegistrationCenterDto, registrationCenterPostResponseDto, - inputLangCodeList, validateRegistrationCenterDtos, constraintViolationedSecList, errors, secErrors, - firstObject); - } - - - // method to find the - private void constraintViolationPrimSecLangData(List reqRegistrationCenterDto, - RegistrationCenterPostResponseDto registrationCenterPostResponseDto, List inputLangCodeList, - List validateRegistrationCenterDtos, - List constraintViolationedSecList, List errors, - List secErrors, RegCenterPostReqDto firstObject) { - for (RegCenterPostReqDto registrationCenterDto : reqRegistrationCenterDto) { - - ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); - Validator validator = factory.getValidator(); - - if ((registrationCenterDto.getLangCode() != null) - && (registrationCenterDto.getLangCode().equalsIgnoreCase(primaryLang))) { - - Set> constraintViolations = validator - .validate(registrationCenterDto); - - primaryLanguageValidation(validateRegistrationCenterDtos, errors, registrationCenterDto, - constraintViolations); - - } else if ((registrationCenterDto.getLangCode() != null) - && (secondaryLangList.contains(registrationCenterDto.getLangCode()))) { - - secondaryLangValidation(registrationCenterPostResponseDto, - validateRegistrationCenterDtos, constraintViolationedSecList, secErrors, registrationCenterDto, - validator, firstObject); - } else { - errors.add(new ServiceError(RegistrationCenterErrorCode.LANGUAGE_EXCEPTION.getErrorCode(), - String.format(RegistrationCenterErrorCode.LANGUAGE_EXCEPTION.getErrorMessage(), - registrationCenterDto.getLangCode()))); - registrationCenterPostResponseDto.setConstraintViolationError(errors); - } - inputLangCodeList.add(registrationCenterDto.getLangCode()); - } - } - - private void primaryLanguageValidation(List validateRegistrationCenterDtos, - List errors, RegCenterPostReqDto registrationCenterDto, - Set> constraintViolations) { - if (!constraintViolations.isEmpty()) { - constraintViolationIterator(errors, registrationCenterDto, constraintViolations); - if (!errors.isEmpty()) - throw new ValidationException(errors); - } else { - // call method to validate Zone-Id, longitude and latitude - //validateRegCenterCreateReq(registrationCenterDto, errors); - if (!errors.isEmpty()) { - throw new ValidationException(errors); - } - // add primary language Object to list after all validation are true - validateRegistrationCenterDtos.add(registrationCenterDto); - } - } - - // secondary language validation - public void secondaryLangValidation(RegistrationCenterPostResponseDto registrationCenterPostResponseDto, - List validateRegistrationCenterDtos, - List constraintViolationedSecList, List errors, - RegCenterPostReqDto registrationCenterDto, Validator validator, RegCenterPostReqDto firstObject) { - Set> constraintViolations = validator.validate(registrationCenterDto); - if (!constraintViolations.isEmpty()) { - constraintViolationIterator(errors, registrationCenterDto, constraintViolations); - if (!errors.isEmpty()) { - registrationCenterPostResponseDto.setConstraintViolationError(errors); - } - constraintViolationedSecList.add(registrationCenterDto); - - } else { - //isValid(firstObject, registrationCenterDto, errors); - //validateRegCenterCreateReq(registrationCenterDto, errors); - if (!errors.isEmpty()) { - registrationCenterPostResponseDto.setConstraintViolationError(errors); - constraintViolationedSecList.add(registrationCenterDto); - } else { - validateRegistrationCenterDtos.add(registrationCenterDto); - } - - } - } - - // List constraint violation iterator - private void constraintViolationIterator(List errors, RegCenterPostReqDto registrationCenterDto, - Set> constraintViolations) { - - Iterator> iterator = constraintViolations.iterator(); - while (iterator.hasNext()) { - ConstraintViolation cv = iterator.next(); - errors.add(new ServiceError(ValidationErrorCode.CONSTRAINT_VIOLATION.getErrorCode(), - ValidationErrorCode.CONSTRAINT_VIOLATION.getErrorMessage() + " for the LangCode- " - + registrationCenterDto.getLangCode() + " - " + cv.getPropertyPath() + " " - + cv.getMessage())); - } - } - - //map DTO to Entity - public void mapBaseDtoEntity(RegistrationCenter registrationCenterEntity, - T registrationCenterDto) { - registrationCenterEntity.setCenterTypeCode(registrationCenterDto.getCenterTypeCode()); - registrationCenterEntity.setLatitude(registrationCenterDto.getLatitude()); - registrationCenterEntity.setLongitude(registrationCenterDto.getLongitude()); - registrationCenterEntity.setLocationCode(registrationCenterDto.getLocationCode()); - registrationCenterEntity.setHolidayLocationCode(registrationCenterDto.getHolidayLocationCode()); - registrationCenterEntity.setContactPhone(registrationCenterDto.getContactPhone()); - registrationCenterEntity.setWorkingHours(registrationCenterDto.getWorkingHours()); - registrationCenterEntity.setPerKioskProcessTime(registrationCenterDto.getPerKioskProcessTime()); - registrationCenterEntity.setCenterStartTime(registrationCenterDto.getCenterStartTime()); - registrationCenterEntity.setCenterEndTime(registrationCenterDto.getCenterEndTime()); - registrationCenterEntity.setLunchStartTime(registrationCenterDto.getLunchStartTime()); - registrationCenterEntity.setLunchEndTime(registrationCenterDto.getLunchEndTime()); - registrationCenterEntity.setTimeZone(registrationCenterDto.getTimeZone()); - registrationCenterEntity.setZoneCode(registrationCenterDto.getZoneCode()); - } - - - - // call method generate ID or validate with DB - public String generateIdOrvalidateWithDB(String uniqueId) { - if (uniqueId.isEmpty()) { - // Get RegistrationCenter Id by calling RegistrationCenterIdGenerator - // API - uniqueId = registrationCenterIdGenerator.generateRegistrationCenterId(); - - } else { - List renRegistrationCenters = registrationCenterRepository - .findByRegCenterIdAndIsDeletedFalseOrNull(uniqueId); - if (renRegistrationCenters.isEmpty()) { - // for the given ID, we don't have data in primary language - throw new RequestException(RegistrationCenterErrorCode.REGISTRATION_CENTER_ID.getErrorCode(), - String.format(RegistrationCenterErrorCode.REGISTRATION_CENTER_ID.getErrorMessage(), uniqueId)); - } - } - return uniqueId; - } - - // ----------------------------update operation all methods------------------- - - - - // method to validate Put request DTOs, compare data - public void validatePutRequest(List regCenterPutReqDtos, - List notUpdRegistrationCenterList, List inputIdList, - List idLangList, List langList, List errors) { -// for (RegCenterPutReqDto regCenterDto : regCenterPutReqDtos) { -// // method to compare Id -// validateCenterId(regCenterPutReqDtos.get(0), regCenterDto); -// // method to compare IsActive -// validateCenterIsActive(regCenterPutReqDtos.get(0), regCenterDto, errors); -// //called a method to compare PerKioskProcessTimed -// validatePerKioskProcessTime(regCenterPutReqDtos.get(0), regCenterDto, errors); -// //called a method to compare data -// isValid(regCenterPutReqDtos.get(0), regCenterDto, errors); -// //called a method to validate the format of the longitude and latitude, zone validation, -// // lunch and center start and end time -// validateRegCenterCreateReq(regCenterDto, errors); -// inputIdList.add(regCenterDto.getId()); -// langList.add(regCenterDto.getLangCode()); -// idLangList.add(regCenterDto.getLangCode() + regCenterDto.getId()); -// if (!errors.isEmpty()) -// //if found error then add to notUpdRegistrationCenterList -// notUpdRegistrationCenterList.add(regCenterDto); -// -// } - } - - // method to compare ID - public boolean validateCenterId(RegCenterPutReqDto firstObj, - RegCenterPutReqDto eachRecord) { - if (eachRecord.getId().trim().equalsIgnoreCase(firstObj.getId().trim())) { - return true; - } else { - throw new RequestException(RegistrationCenterErrorCode.ID_NOT_UNIQUE.getErrorCode(), - String.format(RegistrationCenterErrorCode.ID_NOT_UNIQUE.getErrorMessage(), eachRecord.getId())); - } - } - - // method to compare IsActive - public boolean validateCenterIsActive(RegCenterPutReqDto firstObj, - RegCenterPutReqDto eachRecord, List errors) { - if (eachRecord.getIsActive() != null && firstObj.getIsActive() != null) { - if (eachRecord.getIsActive().equals(firstObj.getIsActive())) { - return firstObj.getIsActive(); - } else { - errors.add(new ServiceError(RegistrationCenterErrorCode.IS_ACTIVE_NOT_UNIQUE.getErrorCode(), - String.format(RegistrationCenterErrorCode.IS_ACTIVE_NOT_UNIQUE.getErrorMessage(), - eachRecord.getIsActive()))); - } - } - return false; - - } - - // validate for the given ID, do we have records in all supported languages then make True for all records. - public void isActiveTrueAllSupLang(List registrationCenterPutReqAdmDto) { - if (registrationCenterPutReqAdmDto.get(0).getIsActive() != null - && registrationCenterPutReqAdmDto.get(0).getIsActive()) { - // call method to check isActive is true already for the given object - //isActiveTrueAlreadyValidator(registrationCenterPutReqAdmDto); - - //for the given ID , records are in all supported language or not - List renRegistrationCenterList = registrationCenterRepository - .findByRegCenterIdAndIsDeletedFalseOrNull(registrationCenterPutReqAdmDto.get(0).getId()); - Set languageCodeDb = renRegistrationCenterList.stream().map(regObj -> regObj.getLangCode()) - .collect(Collectors.toSet()); - - if (languageCodeDb.containsAll(supportedLanguages)) { - - // to update isActive for - renRegistrationCenterList.forEach(i -> { - i.setIsActive(true); - i.setUpdatedBy(MetaDataUtils.getContextUser()); - i.setUpdatedDateTime(MetaDataUtils.getCurrentDateTime()); - - RegistrationCenterHistory registrationCenterHistory = new RegistrationCenterHistory(); - MapperUtils.map(i, registrationCenterHistory); - MapperUtils.setBaseFieldValue(i, registrationCenterHistory); - registrationCenterHistory.setEffectivetimes(i.getUpdatedDateTime()); - registrationCenterHistory.setUpdatedDateTime(i.getUpdatedDateTime()); - registrationCenterHistoryRepository.create(registrationCenterHistory); - }); - - registrationCenterRepository.saveAll(renRegistrationCenterList); - - } else { - //for the given ID , records are not in all supported language - throw new RequestException(RegistrationCenterErrorCode.ID_LANGUAGE.getErrorCode(), - RegistrationCenterErrorCode.ID_LANGUAGE.getErrorMessage()); - } - - } - } - - // call a method to validate isActive is already true - public void isActiveTrueAlreadyValidator(List registrationCenterPutReqAdmDto) { - RegistrationCenter renRegistrationCenter = registrationCenterRepository.findByIdAndLangCodeAndIsDeletedTrue( - registrationCenterPutReqAdmDto.get(0).getId(), registrationCenterPutReqAdmDto.get(0).getLangCode()); - if (renRegistrationCenter.getIsActive() != null && renRegistrationCenter.getIsActive()) { - throw new RequestException(RegistrationCenterErrorCode.IS_ACTIVE.getErrorCode(), - RegistrationCenterErrorCode.IS_ACTIVE.getErrorMessage()); - } - } - - // call a method while updating to created new recored for the ID and Language - // which is not - // there in DB - public List createRegCenterPut(List newregistrationCenterList, - RegistrationCenter registrationCenterEntity, RegCenterPutReqDto registrationCenterDto) { - List newrRegistrationCenterDtoList; - RegistrationCenterHistory registrationCenterHistoryEntity; - RegistrationCenter registrationCenter; - registrationCenterEntity.setId(registrationCenterDto.getId()); - - registrationCenter = registrationCenterRepository.create(registrationCenterEntity); - - newregistrationCenterList.add(registrationCenter); - - // creating registration center history - registrationCenterHistoryEntity = MetaDataUtils.setCreateMetaData(registrationCenterEntity, - RegistrationCenterHistory.class); - registrationCenterHistoryEntity.setEffectivetimes(registrationCenterEntity.getCreatedDateTime()); - registrationCenterHistoryEntity.setCreatedDateTime(registrationCenterEntity.getCreatedDateTime()); - registrationCenterHistoryRepository.create(registrationCenterHistoryEntity); - - newrRegistrationCenterDtoList = MapperUtils.mapAll(newregistrationCenterList, RegistrationCenterExtnDto.class); - return newrRegistrationCenterDtoList; - } - - - @Autowired - MachineIdGenerator machineIdGenerator; - - @Autowired - MachineRepository machineRepository; - - // call method generate ID or validate with DB - public String generateMachineIdOrvalidateWithDB(String uniqueId) { - if (uniqueId.isEmpty()) { - // Get Machine Id by calling MachineIdGenerator API - uniqueId = machineIdGenerator.generateMachineId(); - } else { - List renMachine = machineRepository - .findMachineByIdAndIsDeletedFalseorIsDeletedIsNullNoIsActive(uniqueId); - if (renMachine.isEmpty()) { - // for the given ID, we don't have data in primary language - throw new RequestException(MachineErrorCode.MACHINE_ID.getErrorCode(), - String.format(MachineErrorCode.MACHINE_ID.getErrorMessage(), uniqueId)); - } - } - return uniqueId; - } - - - - - public void validateRegCenterUpdate(RegCenterPutReqDto registrationCenterDto, List errors) { - String latitude = registrationCenterDto.getLatitude(); - String longitude = registrationCenterDto.getLongitude(); - - zoneUserMapValidation(registrationCenterDto, errors, getZoneIdsForUser()); - zoneStartEndTimeGtrValidation(registrationCenterDto, errors); - lunchStartEndTimeGrtValidation(registrationCenterDto, errors); - formatValidationLongitudeLatitude(errors, latitude, longitude); - holidayVlidation(registrationCenterDto, errors); - - } - - // validate Holiday against DB - private void holidayVlidation(RegCenterPutReqDto registrationCenterDto, List errors) { - List holidays = holidayRepository - .findHolidayByHolidayIdLocationCode(registrationCenterDto.getHolidayLocationCode()); - if (holidays.isEmpty()) { - errors.add(new ServiceError(RegistrationCenterErrorCode.HOLIDAY_NOT_FOUND.getErrorCode(), - String.format(RegistrationCenterErrorCode.HOLIDAY_NOT_FOUND.getErrorMessage(), - registrationCenterDto.getZoneCode()))); - } - } - - // validation to check entered zoneCode is mapped with eligible user or not and - // is valid zoneCode - private void zoneUserMapValidation(RegCenterPutReqDto registrationCenterDto, List errors, - List zoneIds) { - - if (!zoneIds.isEmpty()) { - if (!zoneIds.contains(registrationCenterDto.getZoneCode())) { - errors.add(new ServiceError(RegistrationCenterErrorCode.INVALIDE_ZONE.getErrorCode(), - String.format(RegistrationCenterErrorCode.INVALIDE_ZONE.getErrorMessage(), - registrationCenterDto.getZoneCode()))); - } - } - } - - // validation to check the RegCenter Lunch Start Time is greater - // than RegCenter - // Lunch End Time - private void lunchStartEndTimeGrtValidation(RegCenterPutReqDto registrationCenterDto, - List errors) { - // validation to check the RegCenter Lunch Start Time is greater than RegCenter - // Lunch End Time - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); - LocalTime lunchStartTime = LocalTime.parse("00:00:00", formatter); - if ((registrationCenterDto.getLunchStartTime()!=null && !registrationCenterDto.getLunchStartTime().equals(lunchStartTime)) && registrationCenterDto.getLunchStartTime().isAfter(registrationCenterDto.getLunchEndTime())) { - errors.add(new ServiceError( - RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_START_END_EXCEPTION.getErrorCode(), - String.format( - RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_START_END_EXCEPTION.getErrorMessage(), - registrationCenterDto.getLunchEndTime()))); - - } - if ((registrationCenterDto.getLunchEndTime()!=null && !registrationCenterDto.getLunchEndTime().equals(lunchStartTime)) && registrationCenterDto.getLunchEndTime().isAfter(registrationCenterDto.getCenterEndTime())) { - errors.add(new ServiceError( - RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_END_CENTER_END_EXCEPTION.getErrorCode(), - String.format( - RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_END_CENTER_END_EXCEPTION.getErrorMessage(), - registrationCenterDto.getLunchEndTime()))); - - } - if((registrationCenterDto.getLunchStartTime()!=null && !registrationCenterDto.getLunchStartTime().equals(lunchStartTime)) && registrationCenterDto.getLunchStartTime().isBefore(registrationCenterDto.getCenterStartTime())) - { - errors.add(new ServiceError( - RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_START_CENTER_END_EXCEPTION.getErrorCode(), - String.format( - RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_START_CENTER_END_EXCEPTION.getErrorMessage(), - registrationCenterDto.getLunchEndTime()))); - } - } - - // validation to check the RegCenter Start Time is greater than - // RegCenter End Time - private void zoneStartEndTimeGtrValidation(RegCenterPutReqDto registrationCenterDto, - List errors) { - if (registrationCenterDto.getCenterStartTime().isAfter(registrationCenterDto.getCenterEndTime())) { - errors.add(new ServiceError( - RegistrationCenterErrorCode.REGISTRATION_CENTER_START_END_EXCEPTION.getErrorCode(), - String.format(RegistrationCenterErrorCode.REGISTRATION_CENTER_START_END_EXCEPTION.getErrorMessage(), - registrationCenterDto.getCenterEndTime()))); - } - } - -} +package io.mosip.kernel.masterdata.utils; + +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +import javax.annotation.PostConstruct; +import javax.validation.ConstraintViolation; +import javax.validation.Validation; +import javax.validation.Validator; +import javax.validation.ValidatorFactory; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import io.mosip.kernel.core.exception.ServiceError; +import io.mosip.kernel.core.idgenerator.spi.MachineIdGenerator; +import io.mosip.kernel.core.idgenerator.spi.RegistrationCenterIdGenerator; +import io.mosip.kernel.masterdata.constant.MachineErrorCode; +import io.mosip.kernel.masterdata.constant.RegistrationCenterErrorCode; +import io.mosip.kernel.masterdata.constant.ValidationErrorCode; +import io.mosip.kernel.masterdata.dto.RegCenterPostReqDto; +import io.mosip.kernel.masterdata.dto.RegCenterPutReqDto; +import io.mosip.kernel.masterdata.dto.RegcenterBaseDto; +import io.mosip.kernel.masterdata.dto.getresponse.extn.RegistrationCenterExtnDto; +import io.mosip.kernel.masterdata.dto.postresponse.RegistrationCenterPostResponseDto; +import io.mosip.kernel.masterdata.entity.Holiday; +import io.mosip.kernel.masterdata.entity.Machine; +import io.mosip.kernel.masterdata.entity.RegistrationCenter; +import io.mosip.kernel.masterdata.entity.RegistrationCenterHistory; +import io.mosip.kernel.masterdata.entity.Zone; +import io.mosip.kernel.masterdata.exception.MasterDataServiceException; +import io.mosip.kernel.masterdata.exception.RequestException; +import io.mosip.kernel.masterdata.exception.ValidationException; +import io.mosip.kernel.masterdata.repository.HolidayRepository; +import io.mosip.kernel.masterdata.repository.MachineRepository; +import io.mosip.kernel.masterdata.repository.RegistrationCenterHistoryRepository; +import io.mosip.kernel.masterdata.repository.RegistrationCenterRepository; + +@Component +public class RegistrationCenterValidator { + + @Autowired + private ZoneUtils zoneUtils; + + private String negRegex; + private String posRegex; + + /** + * minimum digits after decimal point in Longitude and latitude + */ + @Value("${mosip.min-digit-longitude-latitude:4}") + private int minDegits; + + /** + * get list of secondary languages supported by MOSIP from configuration file + */ + @Value("#{'${mosip.secondary-language}'.split(',')}") + private Set secondaryLangList; + + /** + * get list of secondary languages supported by MOSIP from configuration file + */ + @Value("${mosip.primary-language}") + private String primaryLang; + + /** + * get list of secondary languages supported by MOSIP from configuration file + */ + @Value("${mosip.secondary-language}") + private String secondaryLang; + + private Set supportedLanguages; + + /** + * Constructing regex for matching the Latitude and Longitude format + */ + + @PostConstruct + public void constructRegEx() { + supportedLanguages = new HashSet<>(Arrays.asList(secondaryLang.split(","))); + supportedLanguages.add(primaryLang); + negRegex = "^(\\-\\d{1,2}\\.\\d{" + minDegits + ",})$"; + posRegex = "^(\\d{1,2}\\.\\d{" + minDegits + ",})$"; + } + + @Autowired + RegistrationCenterIdGenerator registrationCenterIdGenerator; + + @Autowired + RegistrationCenterRepository registrationCenterRepository; + + @Autowired + RegistrationCenterHistoryRepository registrationCenterHistoryRepository; + + @Autowired + HolidayRepository holidayRepository; + + + // method to compare data + public boolean isValid(T firstObj, D eachRecord, + List errors) { + + return validateCenterType(firstObj, eachRecord, errors) | valdateLatitude(firstObj, eachRecord, errors) + | validateLongitude(firstObj, eachRecord, errors) | validateContactPhone(firstObj, eachRecord, errors) + | validateWorkingHours(firstObj, eachRecord, errors) + | validateCenterStartTime(firstObj, eachRecord, errors) + | validateCenterEndTime(firstObj, eachRecord, errors) + | validateLunchStartTime(firstObj, eachRecord, errors) + | validateLunchEndTime(firstObj, eachRecord, errors) | validateTimeZone(firstObj, eachRecord, errors) + | validateHolidayCode(firstObj, eachRecord, errors) | validateZoneCode(firstObj, eachRecord, errors); + } + + + + + // method to compare Latitude + private boolean valdateLatitude(T firstObj, D eachRecord, + List errors) { + if (eachRecord.getLatitude() != null && firstObj.getLatitude() != null) { + if (eachRecord.getLatitude().trim().equalsIgnoreCase(firstObj.getLatitude().trim())) { + return true; + } else { + errors.add(new ServiceError(RegistrationCenterErrorCode.LATITUDE_NOT_UNIQUE.getErrorCode(), + String.format(RegistrationCenterErrorCode.LATITUDE_NOT_UNIQUE.getErrorMessage(), + eachRecord.getLatitude()))); + + } + } + return false; + } + + // method to compare Longitude + private boolean validateLongitude(T firstObj, D eachRecord, + List errors) { + if (eachRecord.getLongitude() != null && firstObj.getLongitude() != null) { + if (eachRecord.getLongitude().trim().equalsIgnoreCase(firstObj.getLongitude().trim())) { + return true; + } else { + errors.add(new ServiceError(RegistrationCenterErrorCode.LONGITUDE_NOT_UNIQUE.getErrorCode(), + String.format(RegistrationCenterErrorCode.LONGITUDE_NOT_UNIQUE.getErrorMessage(), + eachRecord.getLongitude()))); + + } + } + return false; + } + + // method to compare ContactPhone + private boolean validateContactPhone(T firstObj, + D eachRecord, List errors) { + if (eachRecord.getContactPhone() != null && firstObj.getContactPhone() != null) { + if (eachRecord.getContactPhone().trim().equalsIgnoreCase(firstObj.getContactPhone().trim())) { + return true; + } else { + errors.add(new ServiceError(RegistrationCenterErrorCode.CONTACT_PHONE_NOT_UNIQUE.getErrorCode(), + String.format(RegistrationCenterErrorCode.CONTACT_PHONE_NOT_UNIQUE.getErrorMessage(), + eachRecord.getContactPhone()))); + + } + } + return false; + } + + // method to compare WorkingHours + private boolean validateWorkingHours(T firstObj, + D eachRecord, List errors) { + if (eachRecord.getWorkingHours() != null && firstObj.getWorkingHours() != null) { + if (eachRecord.getWorkingHours().trim().equalsIgnoreCase(firstObj.getWorkingHours().trim())) { + return true; + } else { + errors.add(new ServiceError(RegistrationCenterErrorCode.WORKING_HOURS_NOT_UNIQUE.getErrorCode(), + String.format(RegistrationCenterErrorCode.WORKING_HOURS_NOT_UNIQUE.getErrorMessage(), + eachRecord.getWorkingHours()))); + + } + } + return false; + } + + // method to compare CenterStartTime + private boolean validateCenterStartTime(T firstObj, + D eachRecord, List errors) { + if (eachRecord.getCenterStartTime() != null && firstObj.getCenterStartTime() != null) { + if (eachRecord.getCenterStartTime().equals(firstObj.getCenterStartTime())) { + return true; + } else { + errors.add(new ServiceError(RegistrationCenterErrorCode.CENTER_STRART_TIME_NOT_UNIQUE.getErrorCode(), + String.format(RegistrationCenterErrorCode.CENTER_STRART_TIME_NOT_UNIQUE.getErrorMessage(), + eachRecord.getCenterStartTime()))); + + } + } + return false; + } + + // method to compare CenterEndTime + private boolean validateCenterEndTime(T firstObj, + D eachRecord, List errors) { + if (eachRecord.getCenterEndTime() != null && firstObj.getCenterEndTime() != null) { + if (eachRecord.getCenterEndTime().equals(firstObj.getCenterEndTime())) { + return true; + } else { + errors.add(new ServiceError(RegistrationCenterErrorCode.CENTER_END_TIME_NOT_UNIQUE.getErrorCode(), + String.format(RegistrationCenterErrorCode.CENTER_END_TIME_NOT_UNIQUE.getErrorMessage(), + eachRecord.getCenterEndTime()))); + + } + } + return false; + } + + // method to compare LunchStartTime + private boolean validateLunchStartTime(T firstObj, + D eachRecord, List errors) { + if (eachRecord.getLunchStartTime() != null && firstObj.getLunchStartTime() != null) { + if (eachRecord.getLunchStartTime().equals(firstObj.getLunchStartTime())) { + return true; + } else { + errors.add(new ServiceError(RegistrationCenterErrorCode.LUNCH_START_TIME_NOT_UNIQUE.getErrorCode(), + String.format(RegistrationCenterErrorCode.LUNCH_START_TIME_NOT_UNIQUE.getErrorMessage(), + eachRecord.getLunchStartTime()))); + + } + } + return false; + } + + // method to compare LunchEndTime + private boolean validateLunchEndTime(T firstObj, + D eachRecord, List errors) { + if (eachRecord.getLunchEndTime() != null && firstObj.getLunchEndTime() != null) { + if (eachRecord.getLunchEndTime().equals(firstObj.getLunchEndTime())) { + return true; + } else { + errors.add(new ServiceError(RegistrationCenterErrorCode.LUNCH_END_TIME_NOT_UNIQUE.getErrorCode(), + String.format(RegistrationCenterErrorCode.LUNCH_END_TIME_NOT_UNIQUE.getErrorMessage(), + eachRecord.getLunchEndTime()))); + + } + } + return false; + } + + // method to compare TimeZone + private boolean validateTimeZone(T firstObj, D eachRecord, + List errors) { + if (eachRecord.getTimeZone() != null && firstObj.getTimeZone() != null) { + if (eachRecord.getTimeZone().trim().equalsIgnoreCase(firstObj.getTimeZone().trim())) { + return true; + } else { + errors.add(new ServiceError(RegistrationCenterErrorCode.TIME_ZONE_NOT_UNIQUE.getErrorCode(), + String.format(RegistrationCenterErrorCode.TIME_ZONE_NOT_UNIQUE.getErrorMessage(), + eachRecord.getTimeZone()))); + + } + } + return false; + } + + // method to compare HolidayCode + private boolean validateHolidayCode(T firstObj, + D eachRecord, List errors) { + if (eachRecord.getHolidayLocationCode() != null && firstObj.getHolidayLocationCode() != null) { + if (eachRecord.getHolidayLocationCode().trim().equalsIgnoreCase(firstObj.getHolidayLocationCode().trim())) { + return true; + } else { + errors.add(new ServiceError(RegistrationCenterErrorCode.HOLIDAY_LOCATION_CODE_NOT_UNIQUE.getErrorCode(), + String.format(RegistrationCenterErrorCode.HOLIDAY_LOCATION_CODE_NOT_UNIQUE.getErrorMessage(), + eachRecord.getHolidayLocationCode()))); + + } + } + return false; + } + + // method to compare ZoneCode + private boolean validateZoneCode(T firstObj, D eachRecord, + List errors) { + if (eachRecord.getZoneCode() != null && firstObj.getZoneCode() != null) { + if (eachRecord.getZoneCode().trim().equalsIgnoreCase(firstObj.getZoneCode().trim())) { + return true; + } else { + errors.add(new ServiceError(RegistrationCenterErrorCode.ZONE_CODE_NOT_UNIQUE.getErrorCode(), + String.format(RegistrationCenterErrorCode.ZONE_CODE_NOT_UNIQUE.getErrorMessage(), + eachRecord.getZoneCode()))); + + } + } + return false; + } + + // method to compare CenterType + private boolean validateCenterType(T firstObj, + D eachRecord, List errors) { + if (eachRecord.getCenterTypeCode() != null && firstObj.getCenterTypeCode() != null) { + if (eachRecord.getCenterTypeCode().trim().equalsIgnoreCase(firstObj.getCenterTypeCode().trim())) { + return true; + } else { + errors.add(new ServiceError(RegistrationCenterErrorCode.CENTER_TYPE_CODE_NOT_UNIQUE.getErrorCode(), + String.format(RegistrationCenterErrorCode.CENTER_TYPE_CODE_NOT_UNIQUE.getErrorMessage(), + eachRecord.getCenterTypeCode()))); + + } + + } + return false; + } + + // ---------------------------------------------------------------------------------------------------------// + + // method to validate the format of the longitude and latitude, zone validation, + // lunch and center start and end time +// public void validateRegCenterCreateReq( +// T registrationCenterDto, List errors) { +// +// String latitude = registrationCenterDto.getLatitude(); +// String longitude = registrationCenterDto.getLongitude(); +// +// zoneUserMapValidation(registrationCenterDto, errors, getZoneIdsForUser()); +// zoneStartEndTimeGtrValidation(registrationCenterDto, errors); +// lunchStartEndTimeGrtValidation(registrationCenterDto, errors); +// formatValidationLongitudeLatitude(errors, latitude, longitude); +// +// } + + public void validateRegCenterCreate( + RegCenterPostReqDto registrationCenterDto, List errors) { + + String latitude = registrationCenterDto.getLatitude(); + String longitude = registrationCenterDto.getLongitude(); + + zoneUserMapValidation(registrationCenterDto, errors, getZoneIdsForUser()); + zoneStartEndTimeGtrValidation(registrationCenterDto, errors); + lunchStartEndTimeGrtValidation(registrationCenterDto, errors); + formatValidationLongitudeLatitude(errors, latitude, longitude); + //holidayVlidation(registrationCenterDto, errors); + + } + + // validate Holiday against DB + private void holidayVlidation(RegCenterPostReqDto registrationCenterDto, List errors) { + List holidays = holidayRepository + .findHolidayByHolidayIdLocationCode(registrationCenterDto.getHolidayLocationCode()); + if (holidays.isEmpty()) { + errors.add(new ServiceError(RegistrationCenterErrorCode.HOLIDAY_NOT_FOUND.getErrorCode(), + String.format(RegistrationCenterErrorCode.HOLIDAY_NOT_FOUND.getErrorMessage(), + registrationCenterDto.getZoneCode()))); + } + } + + // list zone Id mapped with the called user + private List getZoneIdsForUser() { + List zoneIds; + List zones = zoneUtils.getUserLeafZones(primaryLang); + zoneIds = zones.parallelStream().map(Zone::getCode).collect(Collectors.toList()); + return zoneIds; + } + + // validation to check entered zoneCode is mapped with eligible user or not and + // is valid zoneCode + private void zoneUserMapValidation(RegCenterPostReqDto registrationCenterDto, List errors, + List zoneIds) { + + if (!zoneIds.isEmpty()) { + if (!zoneIds.contains(registrationCenterDto.getZoneCode())) { + errors.add(new ServiceError(RegistrationCenterErrorCode.INVALIDE_ZONE.getErrorCode(), + String.format(RegistrationCenterErrorCode.INVALIDE_ZONE.getErrorMessage(), + registrationCenterDto.getZoneCode()))); + } + } + } + + // validate to check the format of latitude and longitude + // Latitude or Longitude must have minimum 4 digits after decimal + private void formatValidationLongitudeLatitude(List errors, String latitude, String longitude) { + + if (!((Pattern.matches(negRegex, latitude) || Pattern.matches(posRegex, latitude)) + && (Pattern.matches(negRegex, longitude) || Pattern.matches(posRegex, longitude)))) { + errors.add( + new ServiceError(RegistrationCenterErrorCode.REGISTRATION_CENTER_FORMATE_EXCEPTION.getErrorCode(), + RegistrationCenterErrorCode.REGISTRATION_CENTER_FORMATE_EXCEPTION.getErrorMessage())); + } + } + + // validation to check the RegCenter Lunch Start Time is greater + // than RegCenter + // Lunch End Time + private void lunchStartEndTimeGrtValidation(RegCenterPostReqDto registrationCenterDto, + List errors) { + // validation to check the RegCenter Lunch Start Time is greater than RegCenter + // Lunch End Time + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); + LocalTime lunchStartTime = LocalTime.parse("00:00:00", formatter); + if ((registrationCenterDto.getLunchStartTime()!=null && !registrationCenterDto.getLunchStartTime().equals(lunchStartTime)) && registrationCenterDto.getLunchStartTime().isAfter(registrationCenterDto.getLunchEndTime())) { + errors.add(new ServiceError( + RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_START_END_EXCEPTION.getErrorCode(), + String.format( + RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_START_END_EXCEPTION.getErrorMessage(), + registrationCenterDto.getLunchEndTime()))); + + } + if ((registrationCenterDto.getLunchEndTime()!=null && !registrationCenterDto.getLunchEndTime().equals(lunchStartTime)) && registrationCenterDto.getLunchEndTime().isAfter(registrationCenterDto.getCenterEndTime())) { + errors.add(new ServiceError( + RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_END_CENTER_END_EXCEPTION.getErrorCode(), + String.format( + RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_END_CENTER_END_EXCEPTION.getErrorMessage(), + registrationCenterDto.getLunchEndTime()))); + + } + if((registrationCenterDto.getLunchStartTime()!=null && !registrationCenterDto.getLunchStartTime().equals(lunchStartTime)) && registrationCenterDto.getLunchStartTime().isBefore(registrationCenterDto.getCenterStartTime())) + { + errors.add(new ServiceError( + RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_START_CENTER_END_EXCEPTION.getErrorCode(), + String.format( + RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_START_CENTER_END_EXCEPTION.getErrorMessage(), + registrationCenterDto.getLunchEndTime()))); + } + } + + // validation to check the RegCenter Start Time is greater than + // RegCenter End Time + private void zoneStartEndTimeGtrValidation(RegCenterPostReqDto registrationCenterDto, + List errors) { + if (registrationCenterDto.getCenterStartTime().isAfter(registrationCenterDto.getCenterEndTime())) { + errors.add(new ServiceError( + RegistrationCenterErrorCode.REGISTRATION_CENTER_START_END_EXCEPTION.getErrorCode(), + String.format(RegistrationCenterErrorCode.REGISTRATION_CENTER_START_END_EXCEPTION.getErrorMessage(), + registrationCenterDto.getCenterEndTime()))); + } + } + + // ------------------------------------------------------------------------------------------------------------ + // method to validate the primary and secondary language input objects mandatory + // fields + public void validatePrimarySencodaryLangMandatoryFields(List reqRegistrationCenterDto, + RegistrationCenterPostResponseDto registrationCenterPostResponseDto, List inputLangCodeList, + List validateRegistrationCenterDtos, + List constraintViolationedSecList, List errors) { + List secErrors = new ArrayList<>(); + RegCenterPostReqDto firstObject = null; + + Optional defualtLangVal = reqRegistrationCenterDto.stream() + .filter(i -> i.getLangCode().equals(primaryLang)).findAny(); + + if (!defualtLangVal.isPresent()) { + throw new RequestException(RegistrationCenterErrorCode.DEFAULT_LANGUAGE.getErrorCode(), + RegistrationCenterErrorCode.DEFAULT_LANGUAGE.getErrorMessage()); + } + + for (RegCenterPostReqDto registrationCenterDto : reqRegistrationCenterDto) { + if (registrationCenterDto.getLangCode() != null + && registrationCenterDto.getLangCode().equals(primaryLang)) { + firstObject = registrationCenterDto; + } else if ((registrationCenterDto.getLangCode() != null) + && (secondaryLangList.contains(registrationCenterDto.getLangCode()))) { + firstObject = reqRegistrationCenterDto.get(0); + } + } + + constraintViolationPrimSecLangData(reqRegistrationCenterDto, registrationCenterPostResponseDto, + inputLangCodeList, validateRegistrationCenterDtos, constraintViolationedSecList, errors, secErrors, + firstObject); + } + + + // method to find the + private void constraintViolationPrimSecLangData(List reqRegistrationCenterDto, + RegistrationCenterPostResponseDto registrationCenterPostResponseDto, List inputLangCodeList, + List validateRegistrationCenterDtos, + List constraintViolationedSecList, List errors, + List secErrors, RegCenterPostReqDto firstObject) { + for (RegCenterPostReqDto registrationCenterDto : reqRegistrationCenterDto) { + + ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); + Validator validator = factory.getValidator(); + + if ((registrationCenterDto.getLangCode() != null) + && (registrationCenterDto.getLangCode().equalsIgnoreCase(primaryLang))) { + + Set> constraintViolations = validator + .validate(registrationCenterDto); + + primaryLanguageValidation(validateRegistrationCenterDtos, errors, registrationCenterDto, + constraintViolations); + + } else if ((registrationCenterDto.getLangCode() != null) + && (secondaryLangList.contains(registrationCenterDto.getLangCode()))) { + + secondaryLangValidation(registrationCenterPostResponseDto, + validateRegistrationCenterDtos, constraintViolationedSecList, secErrors, registrationCenterDto, + validator, firstObject); + } else { + errors.add(new ServiceError(RegistrationCenterErrorCode.LANGUAGE_EXCEPTION.getErrorCode(), + String.format(RegistrationCenterErrorCode.LANGUAGE_EXCEPTION.getErrorMessage(), + registrationCenterDto.getLangCode()))); + registrationCenterPostResponseDto.setConstraintViolationError(errors); + } + inputLangCodeList.add(registrationCenterDto.getLangCode()); + } + } + + private void primaryLanguageValidation(List validateRegistrationCenterDtos, + List errors, RegCenterPostReqDto registrationCenterDto, + Set> constraintViolations) { + if (!constraintViolations.isEmpty()) { + constraintViolationIterator(errors, registrationCenterDto, constraintViolations); + if (!errors.isEmpty()) + throw new ValidationException(errors); + } else { + // call method to validate Zone-Id, longitude and latitude + //validateRegCenterCreateReq(registrationCenterDto, errors); + if (!errors.isEmpty()) { + throw new ValidationException(errors); + } + // add primary language Object to list after all validation are true + validateRegistrationCenterDtos.add(registrationCenterDto); + } + } + + // secondary language validation + public void secondaryLangValidation(RegistrationCenterPostResponseDto registrationCenterPostResponseDto, + List validateRegistrationCenterDtos, + List constraintViolationedSecList, List errors, + RegCenterPostReqDto registrationCenterDto, Validator validator, RegCenterPostReqDto firstObject) { + Set> constraintViolations = validator.validate(registrationCenterDto); + if (!constraintViolations.isEmpty()) { + constraintViolationIterator(errors, registrationCenterDto, constraintViolations); + if (!errors.isEmpty()) { + registrationCenterPostResponseDto.setConstraintViolationError(errors); + } + constraintViolationedSecList.add(registrationCenterDto); + + } else { + //isValid(firstObject, registrationCenterDto, errors); + //validateRegCenterCreateReq(registrationCenterDto, errors); + if (!errors.isEmpty()) { + registrationCenterPostResponseDto.setConstraintViolationError(errors); + constraintViolationedSecList.add(registrationCenterDto); + } else { + validateRegistrationCenterDtos.add(registrationCenterDto); + } + + } + } + + // List constraint violation iterator + private void constraintViolationIterator(List errors, RegCenterPostReqDto registrationCenterDto, + Set> constraintViolations) { + + Iterator> iterator = constraintViolations.iterator(); + while (iterator.hasNext()) { + ConstraintViolation cv = iterator.next(); + errors.add(new ServiceError(ValidationErrorCode.CONSTRAINT_VIOLATION.getErrorCode(), + ValidationErrorCode.CONSTRAINT_VIOLATION.getErrorMessage() + " for the LangCode- " + + registrationCenterDto.getLangCode() + " - " + cv.getPropertyPath() + " " + + cv.getMessage())); + } + } + + //map DTO to Entity + public void mapBaseDtoEntity(RegistrationCenter registrationCenterEntity, + T registrationCenterDto) { + registrationCenterEntity.setCenterTypeCode(registrationCenterDto.getCenterTypeCode()); + registrationCenterEntity.setLatitude(registrationCenterDto.getLatitude()); + registrationCenterEntity.setLongitude(registrationCenterDto.getLongitude()); + registrationCenterEntity.setLocationCode(registrationCenterDto.getLocationCode()); + registrationCenterEntity.setHolidayLocationCode(registrationCenterDto.getHolidayLocationCode()); + registrationCenterEntity.setContactPhone(registrationCenterDto.getContactPhone()); + registrationCenterEntity.setWorkingHours(registrationCenterDto.getWorkingHours()); + registrationCenterEntity.setPerKioskProcessTime(registrationCenterDto.getPerKioskProcessTime()); + registrationCenterEntity.setCenterStartTime(registrationCenterDto.getCenterStartTime()); + registrationCenterEntity.setCenterEndTime(registrationCenterDto.getCenterEndTime()); + registrationCenterEntity.setLunchStartTime(registrationCenterDto.getLunchStartTime()); + registrationCenterEntity.setLunchEndTime(registrationCenterDto.getLunchEndTime()); + registrationCenterEntity.setTimeZone(registrationCenterDto.getTimeZone()); + registrationCenterEntity.setZoneCode(registrationCenterDto.getZoneCode()); + } + + + + // call method generate ID or validate with DB + public String generateIdOrvalidateWithDB(String uniqueId) { + if (uniqueId.isEmpty()) { + // Get RegistrationCenter Id by calling RegistrationCenterIdGenerator + // API + uniqueId = registrationCenterIdGenerator.generateRegistrationCenterId(); + + } else { + List renRegistrationCenters = registrationCenterRepository + .findByRegCenterIdAndIsDeletedFalseOrNull(uniqueId); + if (renRegistrationCenters.isEmpty()) { + // for the given ID, we don't have data in primary language + throw new RequestException(RegistrationCenterErrorCode.REGISTRATION_CENTER_ID.getErrorCode(), + String.format(RegistrationCenterErrorCode.REGISTRATION_CENTER_ID.getErrorMessage(), uniqueId)); + } + } + return uniqueId; + } + + // ----------------------------update operation all methods------------------- + + + + // method to validate Put request DTOs, compare data + public void validatePutRequest(List regCenterPutReqDtos, + List notUpdRegistrationCenterList, List inputIdList, + List idLangList, List langList, List errors) { +// for (RegCenterPutReqDto regCenterDto : regCenterPutReqDtos) { +// // method to compare Id +// validateCenterId(regCenterPutReqDtos.get(0), regCenterDto); +// // method to compare IsActive +// validateCenterIsActive(regCenterPutReqDtos.get(0), regCenterDto, errors); +// //called a method to compare PerKioskProcessTimed +// validatePerKioskProcessTime(regCenterPutReqDtos.get(0), regCenterDto, errors); +// //called a method to compare data +// isValid(regCenterPutReqDtos.get(0), regCenterDto, errors); +// //called a method to validate the format of the longitude and latitude, zone validation, +// // lunch and center start and end time +// validateRegCenterCreateReq(regCenterDto, errors); +// inputIdList.add(regCenterDto.getId()); +// langList.add(regCenterDto.getLangCode()); +// idLangList.add(regCenterDto.getLangCode() + regCenterDto.getId()); +// if (!errors.isEmpty()) +// //if found error then add to notUpdRegistrationCenterList +// notUpdRegistrationCenterList.add(regCenterDto); +// +// } + } + + // method to compare ID + public boolean validateCenterId(RegCenterPutReqDto firstObj, + RegCenterPutReqDto eachRecord) { + if (eachRecord.getId().trim().equalsIgnoreCase(firstObj.getId().trim())) { + return true; + } else { + throw new RequestException(RegistrationCenterErrorCode.ID_NOT_UNIQUE.getErrorCode(), + String.format(RegistrationCenterErrorCode.ID_NOT_UNIQUE.getErrorMessage(), eachRecord.getId())); + } + } + + // method to compare IsActive + public boolean validateCenterIsActive(RegCenterPutReqDto firstObj, + RegCenterPutReqDto eachRecord, List errors) { + if (eachRecord.getIsActive() != null && firstObj.getIsActive() != null) { + if (eachRecord.getIsActive().equals(firstObj.getIsActive())) { + return firstObj.getIsActive(); + } else { + errors.add(new ServiceError(RegistrationCenterErrorCode.IS_ACTIVE_NOT_UNIQUE.getErrorCode(), + String.format(RegistrationCenterErrorCode.IS_ACTIVE_NOT_UNIQUE.getErrorMessage(), + eachRecord.getIsActive()))); + } + } + return false; + + } + + // validate for the given ID, do we have records in all supported languages then make True for all records. + public void isActiveTrueAllSupLang(List registrationCenterPutReqAdmDto) { + if (registrationCenterPutReqAdmDto.get(0).getIsActive() != null + && registrationCenterPutReqAdmDto.get(0).getIsActive()) { + // call method to check isActive is true already for the given object + //isActiveTrueAlreadyValidator(registrationCenterPutReqAdmDto); + + //for the given ID , records are in all supported language or not + List renRegistrationCenterList = registrationCenterRepository + .findByRegCenterIdAndIsDeletedFalseOrNull(registrationCenterPutReqAdmDto.get(0).getId()); + Set languageCodeDb = renRegistrationCenterList.stream().map(regObj -> regObj.getLangCode()) + .collect(Collectors.toSet()); + + if (languageCodeDb.containsAll(supportedLanguages)) { + + // to update isActive for + renRegistrationCenterList.forEach(i -> { + i.setIsActive(true); + i.setUpdatedBy(MetaDataUtils.getContextUser()); + i.setUpdatedDateTime(MetaDataUtils.getCurrentDateTime()); + + RegistrationCenterHistory registrationCenterHistory = new RegistrationCenterHistory(); + MapperUtils.map(i, registrationCenterHistory); + MapperUtils.setBaseFieldValue(i, registrationCenterHistory); + registrationCenterHistory.setEffectivetimes(i.getUpdatedDateTime()); + registrationCenterHistory.setUpdatedDateTime(i.getUpdatedDateTime()); + registrationCenterHistoryRepository.create(registrationCenterHistory); + }); + + registrationCenterRepository.saveAll(renRegistrationCenterList); + + } else { + //for the given ID , records are not in all supported language + throw new RequestException(RegistrationCenterErrorCode.ID_LANGUAGE.getErrorCode(), + RegistrationCenterErrorCode.ID_LANGUAGE.getErrorMessage()); + } + + } + } + + // call a method to validate isActive is already true + public void isActiveTrueAlreadyValidator(List registrationCenterPutReqAdmDto) { + RegistrationCenter renRegistrationCenter = registrationCenterRepository.findByIdAndLangCodeAndIsDeletedTrue( + registrationCenterPutReqAdmDto.get(0).getId(), registrationCenterPutReqAdmDto.get(0).getLangCode()); + if (renRegistrationCenter.getIsActive() != null && renRegistrationCenter.getIsActive()) { + throw new RequestException(RegistrationCenterErrorCode.IS_ACTIVE.getErrorCode(), + RegistrationCenterErrorCode.IS_ACTIVE.getErrorMessage()); + } + } + + // call a method while updating to created new recored for the ID and Language + // which is not + // there in DB + public List createRegCenterPut(List newregistrationCenterList, + RegistrationCenter registrationCenterEntity, RegCenterPutReqDto registrationCenterDto) { + List newrRegistrationCenterDtoList; + RegistrationCenterHistory registrationCenterHistoryEntity; + RegistrationCenter registrationCenter; + registrationCenterEntity.setId(registrationCenterDto.getId()); + + registrationCenter = registrationCenterRepository.create(registrationCenterEntity); + + newregistrationCenterList.add(registrationCenter); + + // creating registration center history + registrationCenterHistoryEntity = MetaDataUtils.setCreateMetaData(registrationCenterEntity, + RegistrationCenterHistory.class); + registrationCenterHistoryEntity.setEffectivetimes(registrationCenterEntity.getCreatedDateTime()); + registrationCenterHistoryEntity.setCreatedDateTime(registrationCenterEntity.getCreatedDateTime()); + registrationCenterHistoryRepository.create(registrationCenterHistoryEntity); + + newrRegistrationCenterDtoList = MapperUtils.mapAll(newregistrationCenterList, RegistrationCenterExtnDto.class); + return newrRegistrationCenterDtoList; + } + + + @Autowired + MachineIdGenerator machineIdGenerator; + + @Autowired + MachineRepository machineRepository; + + // call method generate ID or validate with DB + public String generateMachineIdOrvalidateWithDB(String uniqueId) { + if (uniqueId.isEmpty()) { + // Get Machine Id by calling MachineIdGenerator API + uniqueId = machineIdGenerator.generateMachineId(); + } else { + List renMachine = machineRepository + .findMachineByIdAndIsDeletedFalseorIsDeletedIsNullNoIsActive(uniqueId); + if (renMachine.isEmpty()) { + // for the given ID, we don't have data in primary language + throw new RequestException(MachineErrorCode.MACHINE_ID.getErrorCode(), + String.format(MachineErrorCode.MACHINE_ID.getErrorMessage(), uniqueId)); + } + } + return uniqueId; + } + + + + + public void validateRegCenterUpdate(RegCenterPutReqDto registrationCenterDto, List errors) { + String latitude = registrationCenterDto.getLatitude(); + String longitude = registrationCenterDto.getLongitude(); + + zoneUserMapValidation(registrationCenterDto, errors, getZoneIdsForUser()); + zoneStartEndTimeGtrValidation(registrationCenterDto, errors); + lunchStartEndTimeGrtValidation(registrationCenterDto, errors); + formatValidationLongitudeLatitude(errors, latitude, longitude); + //holidayVlidation(registrationCenterDto, errors); + + } + + // validate Holiday against DB + private void holidayVlidation(RegCenterPutReqDto registrationCenterDto, List errors) { + List holidays = holidayRepository + .findHolidayByHolidayIdLocationCode(registrationCenterDto.getHolidayLocationCode()); + if (holidays.isEmpty()) { + errors.add(new ServiceError(RegistrationCenterErrorCode.HOLIDAY_NOT_FOUND.getErrorCode(), + String.format(RegistrationCenterErrorCode.HOLIDAY_NOT_FOUND.getErrorMessage(), + registrationCenterDto.getZoneCode()))); + } + } + + // validation to check entered zoneCode is mapped with eligible user or not and + // is valid zoneCode + private void zoneUserMapValidation(RegCenterPutReqDto registrationCenterDto, List errors, + List zoneIds) { + + if (!zoneIds.isEmpty()) { + if (!zoneIds.contains(registrationCenterDto.getZoneCode())) { + errors.add(new ServiceError(RegistrationCenterErrorCode.INVALIDE_ZONE.getErrorCode(), + String.format(RegistrationCenterErrorCode.INVALIDE_ZONE.getErrorMessage(), + registrationCenterDto.getZoneCode()))); + } + } + } + + // validation to check the RegCenter Lunch Start Time is greater + // than RegCenter + // Lunch End Time + private void lunchStartEndTimeGrtValidation(RegCenterPutReqDto registrationCenterDto, + List errors) { + // validation to check the RegCenter Lunch Start Time is greater than RegCenter + // Lunch End Time + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); + LocalTime lunchStartTime = LocalTime.parse("00:00:00", formatter); + if ((registrationCenterDto.getLunchStartTime()!=null && !registrationCenterDto.getLunchStartTime().equals(lunchStartTime)) && registrationCenterDto.getLunchStartTime().isAfter(registrationCenterDto.getLunchEndTime())) { + errors.add(new ServiceError( + RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_START_END_EXCEPTION.getErrorCode(), + String.format( + RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_START_END_EXCEPTION.getErrorMessage(), + registrationCenterDto.getLunchEndTime()))); + + } + if ((registrationCenterDto.getLunchEndTime()!=null && !registrationCenterDto.getLunchEndTime().equals(lunchStartTime)) && registrationCenterDto.getLunchEndTime().isAfter(registrationCenterDto.getCenterEndTime())) { + errors.add(new ServiceError( + RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_END_CENTER_END_EXCEPTION.getErrorCode(), + String.format( + RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_END_CENTER_END_EXCEPTION.getErrorMessage(), + registrationCenterDto.getLunchEndTime()))); + + } + if((registrationCenterDto.getLunchStartTime()!=null && !registrationCenterDto.getLunchStartTime().equals(lunchStartTime)) && registrationCenterDto.getLunchStartTime().isBefore(registrationCenterDto.getCenterStartTime())) + { + errors.add(new ServiceError( + RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_START_CENTER_END_EXCEPTION.getErrorCode(), + String.format( + RegistrationCenterErrorCode.REGISTRATION_CENTER_LUNCH_START_CENTER_END_EXCEPTION.getErrorMessage(), + registrationCenterDto.getLunchEndTime()))); + } + } + + // validation to check the RegCenter Start Time is greater than + // RegCenter End Time + private void zoneStartEndTimeGtrValidation(RegCenterPutReqDto registrationCenterDto, + List errors) { + if (registrationCenterDto.getCenterStartTime().isAfter(registrationCenterDto.getCenterEndTime())) { + errors.add(new ServiceError( + RegistrationCenterErrorCode.REGISTRATION_CENTER_START_END_EXCEPTION.getErrorCode(), + String.format(RegistrationCenterErrorCode.REGISTRATION_CENTER_START_END_EXCEPTION.getErrorMessage(), + registrationCenterDto.getCenterEndTime()))); + } + } + +} From 69ea4cb0a944533afed5fba80ffc901e50099950 Mon Sep 17 00:00:00 2001 From: Ramadurai Pandian Date: Fri, 17 Jan 2020 16:16:11 +0530 Subject: [PATCH 2/2] MOS-30822 bootstrap and application properties changes --- .../src/main/resources/application-local.properties | 2 ++ .../src/main/resources/bootstrap.properties | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/kernel/kernel-masterdata-service/src/main/resources/application-local.properties b/kernel/kernel-masterdata-service/src/main/resources/application-local.properties index 755b4970a20..3fe2e13dda2 100644 --- a/kernel/kernel-masterdata-service/src/main/resources/application-local.properties +++ b/kernel/kernel-masterdata-service/src/main/resources/application-local.properties @@ -42,3 +42,5 @@ auth.server.admin.validate.url=https://dev.mosip.io/v1/authmanager/authorize/adm master.search.maximum.rows=50 mosip.kernel.masterdata.audit-url=https://dev.mosip.io/v1/auditmanager/audits + +mosip.level=2 diff --git a/kernel/kernel-masterdata-service/src/main/resources/bootstrap.properties b/kernel/kernel-masterdata-service/src/main/resources/bootstrap.properties index 8db5ae03bdc..cd0ad35836e 100644 --- a/kernel/kernel-masterdata-service/src/main/resources/bootstrap.properties +++ b/kernel/kernel-masterdata-service/src/main/resources/bootstrap.properties @@ -17,7 +17,6 @@ server.servlet.path=/v1/masterdata health.config.enabled=false mosip.min-digit-longitude-latitude=4 -mosip.level=4 javax.persistence.jdbc.url=${masterdata_database_url} javax.persistence.jdbc.user=${masterdata_database_username}