Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MOSIP-23551: Added validators for DocType and DocCat code in valid document entity #937

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.mosip.admin.bulkdataupload.constant;

public class ErrorConstants {

public static final String INVALID_DOC_CAT_CODE = "DocCategory is Invalid";
public static final String INVALID_DOC_TYPE_CODE ="DocType is Invalid";
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import jakarta.persistence.Table;

import io.mosip.admin.bulkdataupload.entity.id.ValidDocumentID;
import io.mosip.admin.validator.DocCatCode;
import io.mosip.admin.validator.DocTypeCode;
import io.mosip.admin.bulkdataupload.constant.ErrorConstants;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
Expand Down Expand Up @@ -43,12 +46,13 @@ public class ValidDocument extends BaseEntity implements Serializable {
private static final long serialVersionUID = -3111581667845281498L;

@Id
@AttributeOverrides({
@AttributeOverride(name = "docTypeCode", column = @Column(name = "doctyp_code", nullable = false, length = 36)),
@AttributeOverride(name = "docCategoryCode", column = @Column(name = "doccat_code", nullable = false, length = 36)) })

@Column(name = "doctyp_code", nullable = false, length = 36)
@DocTypeCode(message = ErrorConstants.INVALID_DOC_TYPE_CODE)
private String docTypeCode;

@Id
@Column(name = "doccat_code", nullable = false, length = 36)
@DocCatCode(message = ErrorConstants.INVALID_DOC_CAT_CODE)
private String docCategoryCode;

@ManyToOne(fetch = FetchType.LAZY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,7 @@ public interface DocumentCategoryRepository extends BaseRepository<DocumentCateg

@Query("FROM DocumentCategory WHERE code =?1 AND langCode =?2 AND (isDeleted is null OR isDeleted = false)")
DocumentCategory findByCodeAndLangCode(String code, String langCode);

@Query(value = "select dt.code from master.doc_category dt where (dt.is_deleted = false or dt.is_deleted is null) AND dt.is_active = true", nativeQuery = true)
List<String> findAllByIsDeletedFalseOrIsDeletedIsNull();
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,7 @@ public interface DocumentTypeRepository extends BaseRepository<DocumentType, Cod
*/
@Query("FROM DocumentType WHERE langCode =?1 AND (isDeleted is null OR isDeleted = false) AND isActive = true")
List<DocumentType> findAllByLangCodeAndIsDeletedFalseOrIsDeletedIsNull(String langCode);

@Query(value = "select dt.code from master.doc_type dt where (dt.is_deleted = false or dt.is_deleted is null) AND dt.is_active = true", nativeQuery = true)
List<String> findAllByIsDeletedFalseOrIsDeletedIsNull();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@

import jakarta.servlet.Filter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.web.filter.CommonsRequestLoggingFilter;

import io.mosip.admin.bulkdataupload.repositories.DocumentCategoryRepository;
import io.mosip.admin.bulkdataupload.repositories.DocumentTypeRepository;
import io.mosip.admin.httpfilter.ReqResFilter;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

/**
Expand All @@ -21,6 +31,36 @@
@Configuration
public class CommonConfig {

@Autowired
private DocumentCategoryRepository documentCategoryRepository;

@Autowired
private DocumentTypeRepository documentTypeRepository;

private List<String> docCatCodes;

private List<String> docTypeCodes;

private List<String> getDocCatCodes(){
if(docCatCodes.isEmpty()) {
docCatCodes = documentCategoryRepository.findAllByIsDeletedFalseOrIsDeletedIsNull();
}
return docCatCodes;
}

private List<String> getDocTypeCodes(){
if(docTypeCodes.isEmpty()) {
docTypeCodes = documentTypeRepository.findAllByIsDeletedFalseOrIsDeletedIsNull();
}
return docTypeCodes;
}

@Scheduled(fixedRateString = "#{60 * 60 * 1000 * ${mosip.admin.doccodes-cleanup.fixed-rate}}")
private void clearDocCodes() {
docCatCodes.clear();
docTypeCodes.clear();
}

@Bean
public CommonsRequestLoggingFilter logFilter() {
CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter();
Expand Down Expand Up @@ -53,4 +93,16 @@ public Properties packetProperties() {
} catch (IOException e) { }
return properties;
}

@Bean("DocCatCodes")
@Scope(value = "prototype")
public List<String> docCatCodes(){
return getDocCatCodes();
}

@Bean("DocTypeCodes")
@Scope(value = "prototype")
public List<String> docTypeCodes(){
return getDocTypeCodes();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.mosip.admin.validator;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import io.mosip.admin.bulkdataupload.constant.ErrorConstants;

import javax.validation.Constraint;
import javax.validation.Payload;

@Documented
@Constraint(validatedBy = DocCatCodeValidator.class)
@Target({ ElementType.FIELD, ElementType.TYPE_USE, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
public @interface DocCatCode {

String message() default ErrorConstants.INVALID_DOC_CAT_CODE;

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.mosip.admin.validator;

import java.util.List;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import io.mosip.admin.bulkdataupload.repositories.DocumentCategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;

public class DocCatCodeValidator implements ConstraintValidator<DocCatCode, String> {

@Autowired
private List<String> docCatCodes;

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {

if(docCatCodes == null){
/* Note: An additional validation was getting triggered by doInvoke() method of
* RepositoryListItemWriter class with documentCategoryRepository equal to null
* which is not desired. This if clause is being used to escape that additional
* validation step.
*/
return true;
}

if(null != value && !value.isEmpty()) {
return docCatCodes.contains(value);
}
return false;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.mosip.admin.validator;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;

import io.mosip.admin.bulkdataupload.constant.ErrorConstants;




@Documented
@Constraint(validatedBy = DocTypeCodeValidator.class)
@Target({ ElementType.FIELD, ElementType.TYPE_USE, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
public @interface DocTypeCode {

String message() default ErrorConstants.INVALID_DOC_TYPE_CODE;

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.mosip.admin.validator;

import java.util.List;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.beans.factory.annotation.Autowired;

import io.mosip.admin.bulkdataupload.repositories.DocumentTypeRepository;

public class DocTypeCodeValidator implements ConstraintValidator<DocTypeCode, String> {

@Autowired
private List<String> docTypeCodes;


@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if(docTypeCodes == null){
/* Note: An additional validation was getting triggered by doInvoke() method of
* RepositoryListItemWriter class with documentTypeRepository equal to null
* which is not desired. This if clause is being used to escape that additional
* validation step.
*/
return true;
}

if(null != value && !value.isEmpty()) {
return docTypeCodes.contains(value);
}
return false;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ auth.server.user-add-password-url=https://api-internal.dev.mosip.io/v1/authmanag
mosip.kernel.config.server.file.storage.uri=https://qa3.mosip.net/config/print/mz/qa3-1.1.5/
mosip.admin.identityMappingJson=identity-mapping.json
mosip.admin.applicant-details.exposed-identity-fields=dob,applicantPhoto
mosip.admin.doccodes-cleanup.fixed-rate=24
RETRIEVE_IDENTITY_API=https://api-internal.dev1.mosip.net/idrepository/v1/identity/idvid
## this property is used to configure max limit of search for the login user to get applicantDetails
mosip.admin.applicant-details.max.login.count=30
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void testLostRidSearch_withValidRequest_returnsResults() throws Exception
AdminDataUtil.checkResponse(
(mockMvc.perform(MockMvcRequestBuilders.post("/lostRid").contentType(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsString(searchInfoReq))).andReturn()),
null);
"ADMN-LRID-001");

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ spring.batch.initializer.enabled=false
mosip.admin-appid=admin
mosip.admin-otp-context=auth-otp
mosip.admin-userid-otp-type=USERID
mosip.admin.doccodes-cleanup.fixed-rate=24

#-----------------UINActive/Deactive---------------------------------
mosip.admin.uinmgmt.uin-detail-search=http://localhost:8080/idrepository/v1/identity/uin/{uin}
Expand Down Expand Up @@ -475,3 +476,5 @@ DIGITAL_CARD_STATUS_URL=https://qa3.mosip.net/v1/digitalcard
RETRIEVE_IDENTITY_API=https://dev.mosip.net/idrepository/v1/identity/idvid
PACKET_MANAGER_BIOMETRIC=https://api-internal.dev.mosip.net/commons/v1/packetmanager/biometrics
PACKET_MANAGER_SEARCHFIELDS=https://api-internal.dev.mosip.net/commons/v1/packetmanager/searchFields
mosip.registration.processor.lostrid.max-registration-date-filter-interval=30