Friday, 9 March 2018

Populate one object from other object properties

One of my friend got this interview question. Interviewer provided below kind of diagram and wants to write a function to populate one object from other object.

1.   EmployeeDAO can be converted to and back from EmployeeDTO
2.   OrganizationDAO can be converted to and back from OrganizationDTO
3.   ManagerDAO can be converted to and back from ManagerDTO.
Now, you have to implement convertToDTO and convertToDAO methods.

convertToDTO : Convert the DAO to DTO
convertToDAO : Convert the DTO to DAO

I am going to create a hierarchy of classes like below.



Below method converts the dao class to dto class.

public static <T extends DTO> T convertToDTO(DAO dao, Class<T> dtoClass)
  throws InstantiationException, IllegalAccessException {
 if (dao == null) {
  throw new IllegalArgumentException("dao can't be null");
 }

 T result = dtoClass.newInstance();

 if (dao instanceof EmployeeDAO) {
  EmployeeDAO empDAO = (EmployeeDAO) dao;
  EmployeeDTO empDTO = (EmployeeDTO) result;

  empDTO.setFirstName(empDAO.getFirstName());
  empDTO.setLastName(empDAO.getLastName());
  return dtoClass.cast(empDTO);

 }

 if (dao instanceof ManagerDAO) {
  ManagerDAO managerDAO = (ManagerDAO) dao;
  ManagerDTO managerDTO = (ManagerDTO) result;

  managerDTO.setFirstName(managerDAO.getFirstName());
  managerDTO.setLastName(managerDAO.getLastName());
  managerDTO.setEmployeeIds(managerDAO.getEmployeeIds());
  return dtoClass.cast(managerDTO);

 }

 if (dao instanceof OrganizationDAO) {
  OrganizationDAO organizationDAO = (OrganizationDAO) dao;
  OrganizationDTO organizationDTO = (OrganizationDTO) result;

  organizationDTO.setOrgName(organizationDAO.getOrgName());
  organizationDTO.setOrgRegId(organizationDAO.getOrgRegId());
  return dtoClass.cast(organizationDTO);
 }

 throw new IllegalArgumentException("DAO must be one of Employee, Manager, Organizaiton");
}

In the same way, you can write a method to convert dto object to dao object.

Find the below working application.


DAO.java
package com.sample.model.dao;

public class DAO {

 private String id;
 private String creationDate;
 private String modifiedDate;

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }

 public String getCreationDate() {
  return creationDate;
 }

 public void setCreationDate(String creationDate) {
  this.creationDate = creationDate;
 }

 public String getModifiedDate() {
  return modifiedDate;
 }

 public void setModifiedDate(String modifiedDate) {
  this.modifiedDate = modifiedDate;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("DAO [id=").append(id).append(", creationDate=").append(creationDate).append(", modifiedDate=")
    .append(modifiedDate).append("]");
  return builder.toString();
 }

}


EmployeeDAO.java
package com.sample.model.dao;

public class EmployeeDAO extends DAO {

 private String firstName;
 private String lastName;
 private String mailId;

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getMailId() {
  return mailId;
 }

 public void setMailId(String mailId) {
  this.mailId = mailId;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("EmployeeDAO [firstName=").append(firstName).append(", lastName=").append(lastName)
    .append(", mailId=").append(mailId).append(super.toString()).append("]");
  return builder.toString();
 }

}


ManagerDAO.java
package com.sample.model.dao;

import java.util.List;

public class ManagerDAO extends DAO {
 private String firstName;
 private String lastName;
 private String mailId;
 private List<String> employeeIds;

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getMailId() {
  return mailId;
 }

 public void setMailId(String mailId) {
  this.mailId = mailId;
 }

 public List<String> getEmployeeIds() {
  return employeeIds;
 }

 public void setEmployeeIds(List<String> employeeIds) {
  this.employeeIds = employeeIds;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("ManagerDAO [firstName=").append(firstName).append(", lastName=").append(lastName)
    .append(", mailId=").append(mailId).append(", employeeIds=").append(employeeIds)
    .append(super.toString()).append("]");
  return builder.toString();
 }

}


OrganizationDAO.java
package com.sample.model.dao;

public class OrganizationDAO extends DAO {

 private String orgRegId;
 private String orgName;

 public String getOrgRegId() {
  return orgRegId;
 }

 public void setOrgRegId(String orgRegId) {
  this.orgRegId = orgRegId;
 }

 public String getOrgName() {
  return orgName;
 }

 public void setOrgName(String orgName) {
  this.orgName = orgName;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("OrganizationDAO [orgRegId=").append(orgRegId).append(", orgName=").append(orgName)
    .append(super.toString()).append("]");
  return builder.toString();
 }

}


DTO.java
package com.sample.model.dto;

public class DTO {

}


EmployeeDTO.java
package com.sample.model.dto;

public class EmployeeDTO extends DTO {
 private String firstName;
 private String lastName;

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("EmployeeDTO [firstName=").append(firstName).append(", lastName=").append(lastName).append("]");
  return builder.toString();
 }

}


ManagerDTO.java
package com.sample.model.dto;

import java.util.List;

public class ManagerDTO extends DTO {
 private String firstName;
 private String lastName;
 private List<String> employeeIds;

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public List<String> getEmployeeIds() {
  return employeeIds;
 }

 public void setEmployeeIds(List<String> employeeIds) {
  this.employeeIds = employeeIds;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("ManagerDTO [firstName=").append(firstName).append(", lastName=").append(lastName)
    .append(", employeeIds=").append(employeeIds).append("]");
  return builder.toString();
 }

}


OrganizationDTO.java
package com.sample.model.dto;

public class OrganizationDTO extends DTO {

 private String orgRegId;
 private String orgName;

 public String getOrgRegId() {
  return orgRegId;
 }

 public void setOrgRegId(String orgRegId) {
  this.orgRegId = orgRegId;
 }

 public String getOrgName() {
  return orgName;
 }

 public void setOrgName(String orgName) {
  this.orgName = orgName;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("OrganizationDTO [orgRegId=").append(orgRegId).append(", orgName=").append(orgName).append("]");
  return builder.toString();
 }

}


BeanUtils.java
package com.sample.util;

import com.sample.model.dao.DAO;
import com.sample.model.dao.EmployeeDAO;
import com.sample.model.dao.ManagerDAO;
import com.sample.model.dao.OrganizationDAO;
import com.sample.model.dto.DTO;
import com.sample.model.dto.EmployeeDTO;
import com.sample.model.dto.ManagerDTO;
import com.sample.model.dto.OrganizationDTO;

public class BeanUtils {

 public static <T extends DTO> T convertToDTO(DAO dao, Class<T> dtoClass)
   throws InstantiationException, IllegalAccessException {
  if (dao == null) {
   throw new IllegalArgumentException("dao can't be null");
  }

  T result = dtoClass.newInstance();

  if (dao instanceof EmployeeDAO) {
   EmployeeDAO empDAO = (EmployeeDAO) dao;
   EmployeeDTO empDTO = (EmployeeDTO) result;

   empDTO.setFirstName(empDAO.getFirstName());
   empDTO.setLastName(empDAO.getLastName());
   return dtoClass.cast(empDTO);

  }

  if (dao instanceof ManagerDAO) {
   ManagerDAO managerDAO = (ManagerDAO) dao;
   ManagerDTO managerDTO = (ManagerDTO) result;

   managerDTO.setFirstName(managerDAO.getFirstName());
   managerDTO.setLastName(managerDAO.getLastName());
   managerDTO.setEmployeeIds(managerDAO.getEmployeeIds());
   return dtoClass.cast(managerDTO);

  }

  if (dao instanceof OrganizationDAO) {
   OrganizationDAO organizationDAO = (OrganizationDAO) dao;
   OrganizationDTO organizationDTO = (OrganizationDTO) result;

   organizationDTO.setOrgName(organizationDAO.getOrgName());
   organizationDTO.setOrgRegId(organizationDAO.getOrgRegId());
   return dtoClass.cast(organizationDTO);
  }

  throw new IllegalArgumentException("DAO must be one of Employee, Manager, Organizaiton");
 }

 public static <T extends DAO> T convertToDAO(DTO dto, Class<T> daoClass)
   throws InstantiationException, IllegalAccessException {

  // TODO: You can implement the code like above method.

  throw new IllegalArgumentException("DtO must be one of Employee, Manager, Organizaiton");

 }
}


Test.java
package com.sample.test;

import java.util.Date;

import com.sample.model.dao.EmployeeDAO;
import com.sample.model.dto.EmployeeDTO;
import com.sample.util.BeanUtils;

public class Test {
 public static void main(String args[]) throws Exception {
  EmployeeDAO empDAO = new EmployeeDAO();
  
  Date date = new Date();
  
  empDAO.setCreationDate(date.toString());
  empDAO.setFirstName("Krishna");
  empDAO.setId("I12345");
  empDAO.setLastName("Gurram");
  empDAO.setMailId("abc@abc.com");
  empDAO.setModifiedDate(date.toString());
  
  EmployeeDTO empDTO = BeanUtils.convertToDTO(empDAO, EmployeeDTO.class);
  
  System.out.println("DAO : " + empDAO + "\n\n");
  System.out.println("DTO : " + empDTO);
  
 }
}

Output
DAO : EmployeeDAO [firstName=Krishna, lastName=Gurram, mailId=abc@abc.comDAO [id=I12345, creationDate=Wed Feb 28 10:26:44 IST 2018, modifiedDate=Wed Feb 28 10:26:44 IST 2018]]


DTO : EmployeeDTO [firstName=Krishna, lastName=Gurram]











No comments:

Post a Comment