Committing the changes

parent aee845c4
package com.poc.controller; package com.poc.controller;
import java.util.List; import java.util.List;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.poc.entity.Employee; import com.poc.entity.Employee;
import com.poc.model.Emp; import com.poc.model.Emp;
import com.poc.service.EmployeeService; import com.poc.service.EmployeeService;
@RestController @RestController
@RequestMapping(path = "/employees") @RequestMapping(path = "/employees")
@Validated @Validated
public class EmployeeController { public class EmployeeController {
@Autowired @Autowired
private EmployeeService employeeService; private EmployeeService employeeService;
@GetMapping(produces = "application/json", consumes = "application/json", path = "/empId/{empId}") @GetMapping(produces = "application/json", consumes = "application/json", path = "/empId/{empId}")
public ResponseEntity<Emp> findByEmpId(@Valid @PathVariable Long empId) { public ResponseEntity<Emp> findByEmpId(@Valid @PathVariable Long empId) {
return new ResponseEntity<>(employeeService.findByEmpId(empId), HttpStatus.OK); return new ResponseEntity<>(employeeService.findByEmpId(empId), HttpStatus.OK);
} }
@GetMapping(produces = "application/json", consumes = "application/json", path = "/") @GetMapping(produces = "application/json", consumes = "application/json", path = "/")
public ResponseEntity<List<Emp>> findAll() { public ResponseEntity<List<Emp>> findAll() {
return new ResponseEntity<>(employeeService.findAll(), HttpStatus.OK); return new ResponseEntity<>(employeeService.findAll(), HttpStatus.OK);
} }
@GetMapping(produces = "application/json", consumes = "application/json", path = "/id/{id}") @GetMapping(produces = "application/json", consumes = "application/json", path = "/id/{id}")
public ResponseEntity<Emp> findById(@Valid @PathVariable String id) { public ResponseEntity<Emp> findById(@Valid @PathVariable String id) {
return new ResponseEntity<>(employeeService.findById(id), HttpStatus.OK); return new ResponseEntity<>(employeeService.findById(id), HttpStatus.OK);
} }
@GetMapping(produces = "application/json", consumes = "application/json", path = "/sal/{sal}") @GetMapping(produces = "application/json", consumes = "application/json", path = "/sal/{sal}")
public ResponseEntity<List<Emp>> findBySal(@Valid @PathVariable Double sal) { public ResponseEntity<List<Emp>> findBySal(@Valid @PathVariable Double sal) {
return new ResponseEntity<>(employeeService.findBySalary(sal), HttpStatus.OK); return new ResponseEntity<>(employeeService.findBySalary(sal), HttpStatus.OK);
} }
@PostMapping(produces = "application/json", consumes = "application/json", path = "/upsert") @PostMapping(produces = "application/json", consumes = "application/json", path = "/upsert")
public ResponseEntity<Emp> saveOrUpdateEmployee(@Valid @RequestBody Emp emp) { public ResponseEntity<Emp> saveOrUpdateEmployee(@Valid @RequestBody Emp emp) {
employeeService.saveOrUpdate(emp); employeeService.saveOrUpdate(emp);
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }
@PostMapping(produces = "application/json", consumes = "application/json", path = "/save") @PostMapping(produces = "application/json", consumes = "application/json", path = "/save")
public ResponseEntity<Emp> saveEmployee(@Valid @RequestBody Emp emp) { public ResponseEntity<Emp> saveEmployee(@Valid @RequestBody Emp emp) {
return new ResponseEntity<>(employeeService.save(emp), HttpStatus.OK); return new ResponseEntity<>(employeeService.save(emp), HttpStatus.OK);
} }
@DeleteMapping(produces = "application/json", consumes = "application/json", path = "/delete/{empId}") @DeleteMapping(produces = "application/json", consumes = "application/json", path = "/delete/{empId}")
public ResponseEntity<Long> deleteByEmpId(@PathVariable Long empId) { public ResponseEntity<Long> deleteByEmpId(@PathVariable Long empId) {
return new ResponseEntity<>(employeeService.deleteByEmpId(empId), HttpStatus.OK); return new ResponseEntity<>(employeeService.deleteByEmpId(empId), HttpStatus.OK);
} }
} }
package com.poc.entity; package com.poc.entity;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field; import org.springframework.data.mongodb.core.mapping.Field;
@Document(collection = "employee") @Document(collection = "employee")
public class Employee { public class Employee {
@Id @Id
private String _id; private String _id;
@Field(name = "emp_id") @Field(name = "emp_id")
private Long empId; private Long empId;
@Field(name = "emp_name") @Field(name = "emp_name")
private String empName; private String empName;
@Field(name = "emp_sal") @Field(name = "emp_sal")
private Double empSal; private Double empSal;
@Field(name = "dept_id") @Field(name = "dept_id")
private String deptId; private String deptId;
@Field(name = "dept_name") @Field(name = "dept_name")
private String deptName; private String deptName;
public Employee() { public Employee() {
super(); super();
} }
/** /**
* @return the _id * @return the _id
*/ */
public String get_id() { public String get_id() {
return _id; return _id;
} }
/** /**
* @param _id the _id to set * @param _id the _id to set
*/ */
public void set_id(String _id) { public void set_id(String _id) {
this._id = _id; this._id = _id;
} }
/** /**
* @return the empId * @return the empId
*/ */
public Long getEmpId() { public Long getEmpId() {
return empId; return empId;
} }
/** /**
* @param empId the empId to set * @param empId the empId to set
*/ */
public void setEmpId(Long empId) { public void setEmpId(Long empId) {
this.empId = empId; this.empId = empId;
} }
/** /**
* @return the empName * @return the empName
*/ */
public String getEmpName() { public String getEmpName() {
return empName; return empName;
} }
/** /**
* @param empName the empName to set * @param empName the empName to set
*/ */
public void setEmpName(String empName) { public void setEmpName(String empName) {
this.empName = empName; this.empName = empName;
} }
/** /**
* @return the empSal * @return the empSal
*/ */
public Double getEmpSal() { public Double getEmpSal() {
return empSal; return empSal;
} }
/** /**
* @param empSal the empSal to set * @param empSal the empSal to set
*/ */
public void setEmpSal(Double empSal) { public void setEmpSal(Double empSal) {
this.empSal = empSal; this.empSal = empSal;
} }
/** /**
* @return the deptId * @return the deptId
*/ */
public String getDeptId() { public String getDeptId() {
return deptId; return deptId;
} }
/** /**
* @param deptId the deptId to set * @param deptId the deptId to set
*/ */
public void setDeptId(String deptId) { public void setDeptId(String deptId) {
this.deptId = deptId; this.deptId = deptId;
} }
/** /**
* @return the deptName * @return the deptName
*/ */
public String getDeptName() { public String getDeptName() {
return deptName; return deptName;
} }
/** /**
* @param deptName the deptName to set * @param deptName the deptName to set
*/ */
public void setDeptName(String deptName) { public void setDeptName(String deptName) {
this.deptName = deptName; this.deptName = deptName;
} }
} }
package com.poc.mapper; package com.poc.mapper;
import java.util.Optional; import java.util.Optional;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.poc.entity.Employee; import com.poc.entity.Employee;
import com.poc.model.Emp; import com.poc.model.Emp;
@Component @Component
public class EmployeeMapper implements EntityMapper { public class EmployeeMapper implements EntityMapper {
@Override @Override
public Object mapPojoToEntity(Object pojo) { public Object mapPojoToEntity(Object pojo) {
Optional<Object> obj=Optional.ofNullable(pojo); Optional<Object> obj=Optional.ofNullable(pojo);
if(obj.isPresent()) { if(obj.isPresent()) {
Emp emp=(Emp)pojo; Emp emp=(Emp)pojo;
Employee employee=new Employee(); Employee employee=new Employee();
employee.setDeptId(emp.getDeptId()); employee.setDeptId(emp.getDeptId());
employee.setDeptName(emp.getDeptName()); employee.setDeptName(emp.getDeptName());
employee.setEmpId(emp.getEmpId()); employee.setEmpId(emp.getEmpId());
employee.setEmpName(emp.getEmpName()); employee.setEmpName(emp.getEmpName());
employee.setEmpSal(emp.getEmpSal()); employee.setEmpSal(emp.getEmpSal());
return employee; return employee;
} }
return null; return null;
} }
@Override @Override
public Object mapEntityToPojo(Object entity) { public Object mapEntityToPojo(Object entity) {
Optional<Object> obj=Optional.ofNullable(entity); Optional<Object> obj=Optional.ofNullable(entity);
if(obj.isPresent()) { if(obj.isPresent()) {
Employee employee=(Employee)entity; Employee employee=(Employee)entity;
Emp emp=new Emp(); Emp emp=new Emp();
emp.setDeptId(employee.getDeptId()); emp.setDeptId(employee.getDeptId());
emp.setDeptName(employee.getDeptName()); emp.setDeptName(employee.getDeptName());
emp.setEmpId(employee.getEmpId()); emp.setEmpId(employee.getEmpId());
emp.setEmpName(employee.getEmpName()); emp.setEmpName(employee.getEmpName());
emp.setEmpSal(employee.getEmpSal()); emp.setEmpSal(employee.getEmpSal());
return emp; return emp;
} }
return null; return null;
} }
} }
package com.poc.mapper; package com.poc.mapper;
public interface EntityMapper { public interface EntityMapper {
public Object mapPojoToEntity(Object pojo); public Object mapPojoToEntity(Object pojo);
public Object mapEntityToPojo(Object entity); public Object mapEntityToPojo(Object entity);
} }
package com.poc.model; package com.poc.model;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
public class Emp { public class Emp {
@NotNull(message = "empID should not be null") @NotNull(message = "empID should not be null")
private Long empId; private Long empId;
@NotNull(message = "empName should not be null") @NotNull(message = "empName should not be null")
private String empName; private String empName;
@NotNull(message = "empSal should not be null") @NotNull(message = "empSal should not be null")
private Double empSal; private Double empSal;
@NotNull(message = "deptId should not be null") @NotNull(message = "deptId should not be null")
private String deptId; private String deptId;
@NotNull(message = "deptName should not be null") @NotNull(message = "deptName should not be null")
private String deptName; private String deptName;
public Emp() { public Emp() {
super(); super();
} }
/** /**
* @return the empId * @return the empId
*/ */
public Long getEmpId() { public Long getEmpId() {
return empId; return empId;
} }
/** /**
* @param empId the empId to set * @param empId the empId to set
*/ */
public void setEmpId(Long empId) { public void setEmpId(Long empId) {
this.empId = empId; this.empId = empId;
} }
/** /**
* @return the empName * @return the empName
*/ */
public String getEmpName() { public String getEmpName() {
return empName; return empName;
} }
/** /**
* @param empName the empName to set * @param empName the empName to set
*/ */
public void setEmpName(String empName) { public void setEmpName(String empName) {
this.empName = empName; this.empName = empName;
} }
/** /**
* @return the empSal * @return the empSal
*/ */
public Double getEmpSal() { public Double getEmpSal() {
return empSal; return empSal;
} }
/** /**
* @param empSal the empSal to set * @param empSal the empSal to set
*/ */
public void setEmpSal(Double empSal) { public void setEmpSal(Double empSal) {
this.empSal = empSal; this.empSal = empSal;
} }
/** /**
* @return the deptId * @return the deptId
*/ */
public String getDeptId() { public String getDeptId() {
return deptId; return deptId;
} }
/** /**
* @param deptId the deptId to set * @param deptId the deptId to set
*/ */
public void setDeptId(String deptId) { public void setDeptId(String deptId) {
this.deptId = deptId; this.deptId = deptId;
} }
/** /**
* @return the deptName * @return the deptName
*/ */
public String getDeptName() { public String getDeptName() {
return deptName; return deptName;
} }
/** /**
* @param deptName the deptName to set * @param deptName the deptName to set
*/ */
public void setDeptName(String deptName) { public void setDeptName(String deptName) {
this.deptName = deptName; this.deptName = deptName;
} }
} }
package com.poc.repository; package com.poc.repository;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query; import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import com.poc.entity.Employee; import com.poc.entity.Employee;
@Repository @Repository
public interface EmpMongoRepository extends MongoRepository<Employee, String> { public interface EmpMongoRepository extends MongoRepository<Employee, String> {
@Query(value="{emp_id : ?0}",sort = "{emp_id : 1}") @Query(value="{emp_id : ?0}",sort = "{emp_id : 1}")
public Employee findByEmpId(Long empId); public Employee findByEmpId(Long empId);
/** /**
* -1 -decending * -1 -decending
* 1 ascending * 1 ascending
* @param salary * @param salary
* @return * @return
*/ */
@Query(value="{emp_sal : {$gte: ?0}}",sort = "{emp_sal : -1}") @Query(value="{emp_sal : {$gte: ?0}}",sort = "{emp_sal : -1}")
public Stream<Employee> findBySalary(Double salary); public Stream<Employee> findBySalary(Double salary);
@Query(value = "{emp_id : ?0}", delete = true) @Query(value = "{emp_id : ?0}", delete = true)
public Long deleteByEmployeeId(Long empId); public Long deleteByEmployeeId(Long empId);
} }
package com.poc.repository; package com.poc.repository;
import java.util.List; import java.util.List;
import com.poc.entity.Employee; import com.poc.entity.Employee;
public interface EmpRepository { public interface EmpRepository {
public List<Employee> findAll(); public List<Employee> findAll();
public Employee findById(String id); public Employee findById(String id);
public Employee save(Employee e); public Employee save(Employee e);
public void saveOrUpdate(Employee e); public void saveOrUpdate(Employee e);
} }
package com.poc.repository; package com.poc.repository;
import java.util.List; import java.util.List;
import org.bson.Document; import org.bson.Document;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update; import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import com.mongodb.client.result.UpdateResult; import com.mongodb.client.result.UpdateResult;
import com.poc.entity.Employee; import com.poc.entity.Employee;
@Repository @Repository
public class EmpRepositoryImpl implements EmpRepository { public class EmpRepositoryImpl implements EmpRepository {
private static final Logger logger = LoggerFactory.getLogger(EmpRepositoryImpl.class); private static final Logger logger = LoggerFactory.getLogger(EmpRepositoryImpl.class);
@Autowired @Autowired
private MongoTemplate mongoTemplate; private MongoTemplate mongoTemplate;
@Override @Override
public List<Employee> findAll() { public List<Employee> findAll() {
return mongoTemplate.findAll(Employee.class); return mongoTemplate.findAll(Employee.class);
} }
@Override @Override
public Employee findById(String id) { public Employee findById(String id) {
Query query = new Query(); Query query = new Query();
query.addCriteria(Criteria.where("emp_id").is(id)); query.addCriteria(Criteria.where("emp_id").is(id));
return mongoTemplate.findOne(query, Employee.class); return mongoTemplate.findOne(query, Employee.class);
} }
@Override @Override
public Employee save(Employee e) { public Employee save(Employee e) {
return mongoTemplate.save(e); return mongoTemplate.save(e);
} }
@Override @Override
public void saveOrUpdate(Employee e) { public void saveOrUpdate(Employee e) {
Query query = new Query(); Query query = new Query();
query.addCriteria(Criteria.where("emp_id").is(e.getEmpId())); query.addCriteria(Criteria.where("emp_id").is(e.getEmpId()));
Document doc = new Document(); Document doc = new Document();
mongoTemplate.getConverter().write(e, doc); mongoTemplate.getConverter().write(e, doc);
Update update = Update.fromDocument(doc); Update update = Update.fromDocument(doc);
UpdateResult result=mongoTemplate.upsert(query, update, Employee.class); UpdateResult result=mongoTemplate.upsert(query, update, Employee.class);
result.getMatchedCount(); result.getMatchedCount();
logger.info("Matched Count:{},{}",result.getMatchedCount(),e.getEmpId()); logger.info("Matched Count:{},{}",result.getMatchedCount(),e.getEmpId());
} }
} }
package com.poc.service; package com.poc.service;
import java.util.List; import java.util.List;
import com.poc.entity.Employee; import com.poc.entity.Employee;
import com.poc.model.Emp; import com.poc.model.Emp;
public interface EmployeeService { public interface EmployeeService {
public Emp findByEmpId(Long empId); public Emp findByEmpId(Long empId);
public Emp findById(String id); public Emp findById(String id);
public List<Emp> findAll(); public List<Emp> findAll();
public Emp save(Emp emp); public Emp save(Emp emp);
public void saveOrUpdate(Emp emp); public void saveOrUpdate(Emp emp);
public List<Emp> findBySalary(Double sal); public List<Emp> findBySalary(Double sal);
public Long deleteByEmpId(Long empId); public Long deleteByEmpId(Long empId);
} }
package com.poc.service; package com.poc.service;
import java.awt.print.Book; import java.awt.print.Book;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.poc.entity.Employee; import com.poc.entity.Employee;
import com.poc.mapper.EmployeeMapper; import com.poc.mapper.EmployeeMapper;
import com.poc.model.Emp; import com.poc.model.Emp;
import com.poc.repository.EmpMongoRepository; import com.poc.repository.EmpMongoRepository;
import com.poc.repository.EmpRepository; import com.poc.repository.EmpRepository;
@Service @Service
public class EmployeeServiceImpl implements EmployeeService { public class EmployeeServiceImpl implements EmployeeService {
@Autowired @Autowired
private EmpMongoRepository empMongoRepository; private EmpMongoRepository empMongoRepository;
@Autowired @Autowired
private EmployeeMapper employeeMapper; private EmployeeMapper employeeMapper;
@Autowired @Autowired
private EmpRepository empRepository; private EmpRepository empRepository;
@Override @Override
public Emp findByEmpId(Long empId) { public Emp findByEmpId(Long empId) {
return (Emp)employeeMapper.mapEntityToPojo(empMongoRepository.findByEmpId(empId)); return (Emp)employeeMapper.mapEntityToPojo(empMongoRepository.findByEmpId(empId));
} }
@Override @Override
public Emp findById(String id) { public Emp findById(String id) {
return (Emp)employeeMapper.mapEntityToPojo(empMongoRepository.findById(id).orElse(new Employee())); return (Emp)employeeMapper.mapEntityToPojo(empMongoRepository.findById(id).orElse(new Employee()));
} }
@Override @Override
public List<Emp> findAll() { public List<Emp> findAll() {
List<Employee> list= empMongoRepository.findAll(); List<Employee> list= empMongoRepository.findAll();
List<Emp> empList= list.stream() List<Emp> empList= list.stream()
.map(e->employeeMapper.mapEntityToPojo(e)) .map(e->employeeMapper.mapEntityToPojo(e))
.map(e->(Emp)e) .map(e->(Emp)e)
.collect(Collectors.toList()); .collect(Collectors.toList());
return empList; return empList;
} }
@Override @Override
public Emp save(Emp emp) { public Emp save(Emp emp) {
Employee employee = (Employee) employeeMapper.mapPojoToEntity(emp); Employee employee = (Employee) employeeMapper.mapPojoToEntity(emp);
return (Emp)employeeMapper.mapEntityToPojo(empMongoRepository.save(employee)); return (Emp)employeeMapper.mapEntityToPojo(empMongoRepository.save(employee));
} }
@Override @Override
public void saveOrUpdate(Emp emp) { public void saveOrUpdate(Emp emp) {
Employee employee = (Employee) employeeMapper.mapPojoToEntity(emp); Employee employee = (Employee) employeeMapper.mapPojoToEntity(emp);
empRepository.saveOrUpdate(employee); empRepository.saveOrUpdate(employee);
} }
@Override @Override
public List<Emp> findBySalary(Double sal) { public List<Emp> findBySalary(Double sal) {
List<Emp> empList= null; List<Emp> empList= null;
try (Stream<Employee> stream = empMongoRepository.findBySalary(sal)) { try (Stream<Employee> stream = empMongoRepository.findBySalary(sal)) {
List<Employee> list = stream.collect(Collectors.toList()); List<Employee> list = stream.collect(Collectors.toList());
empList= list.stream() empList= list.stream()
.map(e->(Emp)employeeMapper.mapEntityToPojo(e)) .map(e->(Emp)employeeMapper.mapEntityToPojo(e))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
return empList; return empList;
} }
@Override @Override
public Long deleteByEmpId(Long empId) { public Long deleteByEmpId(Long empId) {
return empMongoRepository.deleteByEmployeeId(empId); return empMongoRepository.deleteByEmployeeId(empId);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment