Integrating with User service

c. Integration with User service - Integration with user service requires the following steps -

i) Create a new class under utils by the name of UserUtil and update the User POJO to have the following content -

@Getter @Setter @AllArgsConstructor @NoArgsConstructor @Builder public class User { @JsonProperty("id") private Long id = null; @JsonProperty("uuid") private String uuid = null; @JsonProperty("userName") private String userName = null; @JsonProperty("password") private String password = null; @JsonProperty("salutation") private String salutation = null; @JsonProperty("name") private String name = null; @JsonProperty("gender") private String gender = null; @JsonProperty("mobileNumber") private String mobileNumber = null; @JsonProperty("emailId") private String emailId = null; @JsonProperty("altContactNumber") private String altContactNumber = null; @JsonProperty("pan") private String pan = null; @JsonProperty("aadhaarNumber") private String aadhaarNumber = null; @JsonProperty("permanentAddress") private String permanentAddress = null; @JsonProperty("permanentCity") private String permanentCity = null; @JsonProperty("permanentPincode") private String permanentPincode = null; @JsonProperty("correspondenceCity") private String correspondenceCity = null; @JsonProperty("correspondencePincode") private String correspondencePincode = null; @JsonProperty("correspondenceAddress") private String correspondenceAddress = null; @JsonProperty("active") private Boolean active = null; @JsonProperty("locale") private String locale = null; @JsonProperty("type") private String type = null; @JsonProperty("signature") private String signature = null; @JsonProperty("accountLocked") private Boolean accountLocked = null; @JsonProperty("roles") @Valid private List<Role> roles = null; @JsonProperty("fatherOrHusbandName") private String fatherOrHusbandName = null; @JsonProperty("bloodGroup") private String bloodGroup = null; @JsonProperty("identificationMark") private String identificationMark = null; @JsonProperty("photo") private String photo = null; @JsonProperty("createdBy") private Long createdBy = null; @JsonProperty("lastModifiedBy") private Long lastModifiedBy = null; @JsonProperty("otpReference") private String otpReference = null; @JsonProperty("tenantId") private String tenantId = null; public User addRolesItem(Role rolesItem) { if (this.roles == null) { this.roles = new ArrayList<>(); } this.roles.add(rolesItem); return this; } }

ii) Annotate the created UserUtil class with @Component and add the following code in the created class -

@Component public class UserUtil { private ObjectMapper mapper; private ServiceRequestRepository serviceRequestRepository; private VTRConfiguration config; @Autowired public UserUtil(ObjectMapper mapper, ServiceRequestRepository serviceRequestRepository, VTRConfiguration config) { this.mapper = mapper; this.serviceRequestRepository = serviceRequestRepository; this.config = config; } /** * Returns UserDetailResponse by calling user service with given uri and object * @param userRequest Request object for user service * @param uri The address of the endpoint * @return Response from user service as parsed as userDetailResponse */ public UserDetailResponse userCall(Object userRequest, StringBuilder uri) { String dobFormat = null; if(uri.toString().contains(config.getUserSearchEndpoint()) || uri.toString().contains(config.getUserUpdateEndpoint())) dobFormat="yyyy-MM-dd"; else if(uri.toString().contains(config.getUserCreateEndpoint())) dobFormat = "dd/MM/yyyy"; try{ LinkedHashMap responseMap = (LinkedHashMap)serviceRequestRepository.fetchResult(uri, userRequest); parseResponse(responseMap,dobFormat); UserDetailResponse userDetailResponse = mapper.convertValue(responseMap,UserDetailResponse.class); return userDetailResponse; } catch(IllegalArgumentException e) { throw new CustomException("IllegalArgumentException","ObjectMapper not able to convertValue in userCall"); } } /** * Parses date formats to long for all users in responseMap * @param responeMap LinkedHashMap got from user api response */ public void parseResponse(LinkedHashMap responeMap, String dobFormat){ List<LinkedHashMap> users = (List<LinkedHashMap>)responeMap.get("user"); String format1 = "dd-MM-yyyy HH:mm:ss"; if(users!=null){ users.forEach( map -> { map.put("createdDate",dateTolong((String)map.get("createdDate"),format1)); if((String)map.get("lastModifiedDate")!=null) map.put("lastModifiedDate",dateTolong((String)map.get("lastModifiedDate"),format1)); if((String)map.get("dob")!=null) map.put("dob",dateTolong((String)map.get("dob"),dobFormat)); if((String)map.get("pwdExpiryDate")!=null) map.put("pwdExpiryDate",dateTolong((String)map.get("pwdExpiryDate"),format1)); } ); } } /** * Converts date to long * @param date date to be parsed * @param format Format of the date * @return Long value of date */ private Long dateTolong(String date,String format){ SimpleDateFormat f = new SimpleDateFormat(format); Date d = null; try { d = f.parse(date); } catch (ParseException e) { throw new CustomException("INVALID_DATE_FORMAT","Failed to parse date format in user"); } return d.getTime(); } /** * enriches the userInfo with statelevel tenantId and other fields * @param mobileNumber * @param tenantId * @param userInfo */ public void addUserDefaultFields(String mobileNumber,String tenantId, User userInfo){ Role role = getCitizenRole(tenantId); userInfo.setRoles(Collections.singletonList(role)); userInfo.setType("CITIZEN"); userInfo.setUserName(mobileNumber); userInfo.setTenantId(getStateLevelTenant(tenantId)); userInfo.setActive(true); } /** * Returns role object for citizen * @param tenantId * @return */ private Role getCitizenRole(String tenantId){ Role role = new Role(); role.setCode("CITIZEN"); role.setName("Citizen"); role.setTenantId(getStateLevelTenant(tenantId)); return role; } public String getStateLevelTenant(String tenantId){ return tenantId.split("\\.")[0]; } }

iii) Create the following POJOs -

@AllArgsConstructor @Getter @NoArgsConstructor public class CreateUserRequest { @JsonProperty("requestInfo") private RequestInfo requestInfo; @JsonProperty("user") private User user; } @Getter @Setter public class UserSearchRequest { @JsonProperty("RequestInfo") private RequestInfo requestInfo; @JsonProperty("uuid") private List<String> uuid; @JsonProperty("id") private List<String> id; @JsonProperty("userName") private String userName; @JsonProperty("name") private String name; @JsonProperty("mobileNumber") private String mobileNumber; @JsonProperty("aadhaarNumber") private String aadhaarNumber; @JsonProperty("pan") private String pan; @JsonProperty("emailId") private String emailId; @JsonProperty("fuzzyLogic") private boolean fuzzyLogic; @JsonProperty("active") @Setter private Boolean active; @JsonProperty("tenantId") private String tenantId; @JsonProperty("pageSize") private int pageSize; @JsonProperty("pageNumber") private int pageNumber = 0; @JsonProperty("sort") private List<String> sort = Collections.singletonList("name"); @JsonProperty("userType") private String userType; @JsonProperty("roleCodes") private List<String> roleCodes; } @AllArgsConstructor @NoArgsConstructor @Getter public class UserDetailResponse { @JsonProperty("responseInfo") ResponseInfo responseInfo; @JsonProperty("user") List<User> user; }

v) Create a class by the name of UserService under service folder and add the following content to it -

vi) Add the following properties in application.properties file -