package by.bsuir.mpp.computershop.controller.impl;

import by.bsuir.mpp.computershop.controller.CrudController;
import by.bsuir.mpp.computershop.controller.exception.ControllerException;
import by.bsuir.mpp.computershop.controller.exception.ResourceNotFoundException;
import by.bsuir.mpp.computershop.entity.BaseEntity;
import by.bsuir.mpp.computershop.service.CrudService;
import by.bsuir.mpp.computershop.service.exception.EntityNotFoundException;
import by.bsuir.mpp.computershop.service.exception.ServiceException;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;
import java.io.Serializable;

public abstract class AbstractCrudController<E extends BaseEntity<ID>, ID extends Serializable> implements CrudController<E, ID> {
    private final CrudService<E, ID> service;
    private Logger logger;

    AbstractCrudController(CrudService<E, ID> service, Logger logger) {
        this.service = service;
        this.logger = logger;
    }

    @Override
    public E add(@Valid @RequestBody E entity) throws ControllerException {
        logger.info(String.format("ADD new %s entity", entity.getClass()));
        E result;
        try {
            result = service.add(entity);
        } catch (EntityNotFoundException e) {
            logger.warn(e.getMessage());
            throw new ResourceNotFoundException(e);
        } catch (ServiceException e) {
            logger.warn(e.getMessage());
            throw new ControllerException(e);
        }
        return result;
    }

    @Override
    public E update(@Valid @RequestBody E entity) throws ControllerException {
        logger.info(String.format("UPDATE entity with id = [%s]", entity.getId()));
        E result;
        try {
            result = service.update(entity);
        } catch (EntityNotFoundException e) {
            logger.warn(e.getMessage());
            throw new ResourceNotFoundException(e);
        } catch (ServiceException e) {
            logger.warn(e.getMessage());
            throw new ControllerException(e);
        }
        return result;
    }

    @Override
    public E getById(@PathVariable ID id) throws ControllerException {
        logger.info(String.format("GET entity with id = [%s]", id.toString()));
        E result;
        try {
            result = service.getOne(id);
        } catch (EntityNotFoundException e) {
            logger.warn(e.getMessage());
            throw new ResourceNotFoundException(e);
        } catch (ServiceException e) {
            logger.warn(e.getMessage());
            throw new ControllerException(e);
        }
        return result;
    }

    @Override
    public Iterable<E> getAll() throws ControllerException {
        logger.info("GET ALL entities.");
        Iterable<E> result;
        try {
            result = service.getAll();
        } catch (ServiceException e) {
            logger.warn(e.getMessage());
            throw new ControllerException(e);
        }
        return result;
    }

    @Override
    public void delete(@PathVariable ID id) throws ControllerException {
        logger.info(String.format("DELETE entity with id = [%s]", id.toString()));
        try {
            service.delete(id);
        } catch (EntityNotFoundException e) {
            logger.warn(e.getMessage());
            throw new ResourceNotFoundException(e);
        } catch (ServiceException e) {
            logger.warn(e.getMessage());
            throw new ControllerException(e);
        }
    }
}

@@ -916,16 +916,22 @@
 private 
+final 
 Logger l
@@ -937,16 +937,16 @@
 logger;%0A
-
 %0A    Abs
@@ -1386,37 +1386,24 @@
 ogger.warn(e
-.getMessage()
 );%0A         
@@ -1505,37 +1505,24 @@
 ogger.warn(e
-.getMessage()
 );%0A         
@@ -1919,37 +1919,24 @@
 ogger.warn(e
-.getMessage()
 );%0A         
@@ -2038,37 +2038,24 @@
 ogger.warn(e
-.getMessage()
 );%0A         
@@ -2436,37 +2436,24 @@
 ogger.warn(e
-.getMessage()
 );%0A         
@@ -2555,37 +2555,24 @@
 ogger.warn(e
-.getMessage()
 );%0A         
@@ -2906,37 +2906,24 @@
 ogger.warn(e
-.getMessage()
 );%0A         
@@ -3282,37 +3282,24 @@
 ogger.warn(e
-.getMessage()
 );%0A         
@@ -3372,32 +3372,32 @@
 eException e) %7B%0A
+
             logg
@@ -3409,21 +3409,8 @@
 rn(e
-.getMessage()
 );%0A 

Refactor AbstractCrudController