commit f6ad0e8a8410269c2f677939fff551fac73d40b2
Author: D0miq <dpoch@email.com>
Date:   Thu Jun 6 12:04:42 2019 +0200

    Moved database exception handler

diff --git a/server/src/main/java/cz/zcu/yamanager/business/ApiManager.java b/server/src/main/java/cz/zcu/yamanager/business/ApiManager.java
index 553db5e..1f9fb2c 100644
--- a/server/src/main/java/cz/zcu/yamanager/business/ApiManager.java
+++ b/server/src/main/java/cz/zcu/yamanager/business/ApiManager.java
@@ -9,8 +9,10 @@ import cz.zcu.yamanager.ws.rest.RESTFullException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.DataAccessException;
 import org.springframework.stereotype.Component;
 
+import java.sql.SQLException;
 import java.time.LocalDate;
 import java.util.List;
 import java.util.Optional;
@@ -38,145 +40,205 @@ public class ApiManager implements Manager {
 
     @Override
     public List<BasicProfileUser> getUsers(Status status) throws RESTFullException {
-        List<BasicProfileUser> users = status == null ? this.userRepository.getAllBasicUsers() : this.userRepository.getAllBasicUsers(status);
+        try {
+            List<BasicProfileUser> users = status == null ? this.userRepository.getAllBasicUsers() : this.userRepository.getAllBasicUsers(status);
+
+            LocalDate today = LocalDate.now();
+            LocalDate weekBefore = today.minusDays(ApiManager.WEEK_LENGTH);
+            LocalDate weekAfter = today.plusDays(ApiManager.WEEK_LENGTH);
+            for (BasicProfileUser user : users) {
+                user.setCalendar(this.vacationRepository.getVacationDays(user.getId(), weekBefore, weekAfter));
+            }
 
-        LocalDate today = LocalDate.now();
-        LocalDate weekBefore = today.minusDays(ApiManager.WEEK_LENGTH);
-        LocalDate weekAfter = today.plusDays(ApiManager.WEEK_LENGTH);
-        for (BasicProfileUser user : users) {
-            user.setCalendar(this.vacationRepository.getVacationDays(user.getId(), weekBefore, weekAfter));
+            return users;
+        } catch (DataAccessException e) {
+            log.error(e.getMessage());
+            throw new RESTFullException(e.getMessage(), "database.error");
         }
-
-        return users;
     }
 
     @Override
     public List<VacationRequest> getVacationRequests(Status status) throws RESTFullException {
-        return status == null ? this.requestRepository.getAllVacationRequests() : this.requestRepository.getAllVacationRequests(status);
+        try {
+            return status == null ? this.requestRepository.getAllVacationRequests() : this.requestRepository.getAllVacationRequests(status);
+        } catch (DataAccessException e) {
+            log.error(e.getMessage());
+            throw new RESTFullException(e.getMessage(), "database.error");
+        }
     }
 
     @Override
     public List<AuthorizationRequest> getAuthorizationRequests(Status status) throws RESTFullException {
-        return status == null ? this.requestRepository.getAllAuthorizations() : this.requestRepository.getAllAuthorizations(status);
+        try {
+            return status == null ? this.requestRepository.getAllAuthorizations() : this.requestRepository.getAllAuthorizations(status);
+        } catch (DataAccessException e) {
+            log.error(e.getMessage());
+            throw new RESTFullException(e.getMessage(), "database.error");
+        }
     }
 
     @Override
     public FullUserProfile getUserProfile(Long userId) throws RESTFullException {
-        return this.userRepository.getFullUser(userId);
+        try {
+            return this.userRepository.getFullUser(userId);
+        } catch (DataAccessException e) {
+            log.error(e.getMessage());
+            throw new RESTFullException(e.getMessage(), "database.error");
+        }
     }
 
     @Override
     public DefaultSettings getDefaultSettings() throws RESTFullException {
-        return this.userRepository.getLastDefaultSettings().orElse(new DefaultSettings());
+        try {
+            return this.userRepository.getLastDefaultSettings().orElse(new DefaultSettings());
+        } catch (DataAccessException e) {
+            log.error(e.getMessage());
+            throw new RESTFullException(e.getMessage(), "database.error");
+        }
     }
 
     @Override
     public List<VacationDay> getUserCalendar(Long userId, LocalDate fromDate, LocalDate toDate, Status status) throws RESTFullException {
-        List<VacationDay> vacations;
-        if(status == null && toDate == null) {
-            vacations = this.vacationRepository.getVacationDays(userId, fromDate);
-        } else if(status == null) {
-            vacations = this.vacationRepository.getVacationDays(userId, fromDate, toDate);
-        } else if(toDate != null) {
-            vacations = this.vacationRepository.getVacationDays(userId, fromDate, toDate, status);
-        } else {
-            vacations = this.vacationRepository.getVacationDays(userId, fromDate, status);
-        }
+        try {
+            List<VacationDay> vacations;
+            if (status == null && toDate == null) {
+                vacations = this.vacationRepository.getVacationDays(userId, fromDate);
+            } else if (status == null) {
+                vacations = this.vacationRepository.getVacationDays(userId, fromDate, toDate);
+            } else if (toDate != null) {
+                vacations = this.vacationRepository.getVacationDays(userId, fromDate, toDate, status);
+            } else {
+                vacations = this.vacationRepository.getVacationDays(userId, fromDate, status);
+            }
 
-        return vacations;
+            return vacations;
+        } catch (DataAccessException e) {
+            log.error(e.getMessage());
+            throw new RESTFullException(e.getMessage(), "database.error");
+        }
     }
 
     @Override
     public void createSettings(DefaultSettings settings) throws RESTFullException {
-        cz.zcu.yamanager.domain.DefaultSettings defaultSettings = new cz.zcu.yamanager.domain.DefaultSettings();
-        defaultSettings.setSickDayCount(settings.getSickDayCount());
-        defaultSettings.setNotification(settings.getNotification());
-        this.userRepository.insertSettings(defaultSettings);
+        try {
+            cz.zcu.yamanager.domain.DefaultSettings defaultSettings = new cz.zcu.yamanager.domain.DefaultSettings();
+            defaultSettings.setSickDayCount(settings.getSickDayCount());
+            defaultSettings.setNotification(settings.getNotification());
+            this.userRepository.insertSettings(defaultSettings);
+        } catch (DataAccessException e) {
+            log.error(e.getMessage());
+            throw new RESTFullException(e.getMessage(), "database.error");
+        }
     }
 
     @Override
     public void createVacation(Long userId, VacationDay vacationDay) throws RESTFullException {
-        User user = this.userRepository.getUser(userId);
-        vacationDay.setStatus(user.getRole() == UserRole.EMPLOYER ? Status.ACCEPTED : Status.PENDING);
-
-        cz.zcu.yamanager.domain.VacationDay vacation = new cz.zcu.yamanager.domain.VacationDay();
-        vacation.setDate(vacationDay.getDate());
-        vacation.setFrom(vacationDay.getFrom());
-        vacation.setTo(vacationDay.getTo());
-        vacation.setStatus(vacationDay.getStatus());
-        vacation.setType(vacationDay.getType());
-
-        if(vacation.getType() == VacationType.VACATION) {
-            user.takeVacation(vacation.getFrom(), vacation.getTo());
-        } else {
-            user.takeSickDay();
-        }
+        try {
+            User user = this.userRepository.getUser(userId);
+            vacationDay.setStatus(user.getRole() == UserRole.EMPLOYER ? Status.ACCEPTED : Status.PENDING);
+
+            cz.zcu.yamanager.domain.VacationDay vacation = new cz.zcu.yamanager.domain.VacationDay();
+            vacation.setDate(vacationDay.getDate());
+            vacation.setFrom(vacationDay.getFrom());
+            vacation.setTo(vacationDay.getTo());
+            vacation.setStatus(vacationDay.getStatus());
+            vacation.setType(vacationDay.getType());
+
+            if (vacation.getType() == VacationType.VACATION) {
+                user.takeVacation(vacation.getFrom(), vacation.getTo());
+            } else {
+                user.takeSickDay();
+            }
 
-        this.vacationRepository.insertVacationDay(userId, vacation);
-        this.userRepository.updateUser(user);
+            this.vacationRepository.insertVacationDay(userId, vacation);
+            this.userRepository.updateUser(user);
+        } catch (DataAccessException e) {
+            log.error(e.getMessage());
+            throw new RESTFullException(e.getMessage(), "database.error");
+        }
     }
 
     @Override
     public void changeSettings(Long userId, UserSettings settings) throws RESTFullException {
-        User user = this.userRepository.getUser(userId);
-
-        if(settings.getRole() == null && settings.getSickDayCount() == null && settings.getVacationCount() == null) {
-            user.setNotification(settings.getNotification());
-        } else {
-            user.addVacationCount(settings.getVacationCount());
-            user.setTotalSickDayCount(settings.getSickDayCount());
-            user.setRole(settings.getRole());
-        }
+        try {
+            User user = this.userRepository.getUser(userId);
 
-        this.userRepository.updateUserSettings(user);
+            if (settings.getRole() == null && settings.getSickDayCount() == null && settings.getVacationCount() == null) {
+                user.setNotification(settings.getNotification());
+            } else {
+                user.addVacationCount(settings.getVacationCount());
+                user.setTotalSickDayCount(settings.getSickDayCount());
+                user.setRole(settings.getRole());
+            }
+
+            this.userRepository.updateUserSettings(user);
+        } catch (DataAccessException e) {
+            log.error(e.getMessage());
+            throw new RESTFullException(e.getMessage(), "database.error");
+        }
     }
 
     @Override
     public void changeVacation(Long userId, VacationDay vacationDay) throws RESTFullException {
-        Optional<cz.zcu.yamanager.domain.VacationDay> vacation = this.vacationRepository.getVacationDay(vacationDay.getId());
-        if(vacation.isPresent()) {
-            vacation.get().setDate(vacationDay.getDate());
-            vacation.get().setStatus(vacationDay.getStatus());
-            vacation.get().setType(vacationDay.getType());
-            vacation.get().setTime(vacationDay.getFrom(), vacationDay.getTo());
-            this.vacationRepository.updateVacationDay(vacation.get());
-        } else {
+        try {
+            Optional<cz.zcu.yamanager.domain.VacationDay> vacation = this.vacationRepository.getVacationDay(vacationDay.getId());
+            if (vacation.isPresent()) {
+                vacation.get().setDate(vacationDay.getDate());
+                vacation.get().setStatus(vacationDay.getStatus());
+                vacation.get().setType(vacationDay.getType());
+                vacation.get().setTime(vacationDay.getFrom(), vacationDay.getTo());
+                this.vacationRepository.updateVacationDay(vacation.get());
+            } else {
 
+            }
+        } catch (DataAccessException e) {
+            log.error(e.getMessage());
+            throw new RESTFullException(e.getMessage(), "database.error");
         }
     }
 
     @Override
     public void changeRequest(RequestType type, BasicRequest request) throws RESTFullException {
-        if(RequestType.VACATION == type) {
-            Optional<User> user = this.vacationRepository.findUserByVacationID(request.getId());
-            Optional<cz.zcu.yamanager.domain.VacationDay> vacationDay = this.vacationRepository.getVacationDay(request.getId());
-            if(user.isPresent() && request.getStatus() == Status.REJECTED) {
-                if(vacationDay.get().getType() == VacationType.SICK_DAY) {
-                    user.get().addTakenSickDayCount(-1);
-                } else {
-                    user.get().addVacationCount(vacationDay.get().getFrom(), vacationDay.get().getTo());
+        try {
+            if (RequestType.VACATION == type) {
+                Optional<User> user = this.vacationRepository.findUserByVacationID(request.getId());
+                Optional<cz.zcu.yamanager.domain.VacationDay> vacationDay = this.vacationRepository.getVacationDay(request.getId());
+                if (user.isPresent() && request.getStatus() == Status.REJECTED) {
+                    if (vacationDay.get().getType() == VacationType.SICK_DAY) {
+                        user.get().addTakenSickDayCount(-1);
+                    } else {
+                        user.get().addVacationCount(vacationDay.get().getFrom(), vacationDay.get().getTo());
+                    }
                 }
-            }
 
-            this.requestRepository.updateVacationRequest(request);
-        } else {
-            this.requestRepository.updateAuthorization(request);
+                this.requestRepository.updateVacationRequest(request);
+            } else {
+                this.requestRepository.updateAuthorization(request);
+            }
+        } catch (DataAccessException e) {
+            log.error(e.getMessage());
+            throw new RESTFullException(e.getMessage(), "database.error");
         }
     }
 
     @Override
     public void deleteVacation(Long userId, Long vacationId) throws RESTFullException {
-        User user = this.userRepository.getUser(userId);
-        Optional<cz.zcu.yamanager.domain.VacationDay> vacation = this.vacationRepository.getVacationDay(vacationId);
-        if (vacation.isPresent()){
-            if(vacation.get().getType() == VacationType.SICK_DAY) {
-                user.addTakenSickDayCount(-1);
-            } else {
-                user.addVacationCount(vacation.get().getFrom(), vacation.get().getTo());
+        try {
+            User user = this.userRepository.getUser(userId);
+            Optional<cz.zcu.yamanager.domain.VacationDay> vacation = this.vacationRepository.getVacationDay(vacationId);
+            if (vacation.isPresent()) {
+                if (vacation.get().getType() == VacationType.SICK_DAY) {
+                    user.addTakenSickDayCount(-1);
+                } else {
+                    user.addVacationCount(vacation.get().getFrom(), vacation.get().getTo());
+                }
             }
-        }
 
-        this.userRepository.updateUser(user);
-        this.vacationRepository.deleteVacationDay(vacationId);
+            this.userRepository.updateUser(user);
+            this.vacationRepository.deleteVacationDay(vacationId);
+        } catch (DataAccessException e) {
+            log.error(e.getMessage());
+            throw new RESTFullException(e.getMessage(), "database.error");
+        }
     }
 }
diff --git a/server/src/main/java/cz/zcu/yamanager/ws/rest/ApiController.java b/server/src/main/java/cz/zcu/yamanager/ws/rest/ApiController.java
index e311bea..fa1788c 100644
--- a/server/src/main/java/cz/zcu/yamanager/ws/rest/ApiController.java
+++ b/server/src/main/java/cz/zcu/yamanager/ws/rest/ApiController.java
@@ -64,9 +64,6 @@ public class ApiController {
         } catch (RESTFullException e) {
             log.error(e.getMessage());
             return sendError(400, e.getLocalizedMessage(), language);
-        } catch (DataAccessException | SQLException e) {
-            log.error(e.getMessage());
-            return sendError(500, "database.error", language);
         } catch (Exception e) {
             log.error(e.getMessage());
             return sendError(401, e.getMessage(), language);
