Projekt

Obecné

Profil

« Předchozí | Další » 

Revize f6ad0e8a

Přidáno uživatelem Dominik Poch před téměř 6 roky(ů)

Moved database exception handler

Zobrazit rozdíly:

server/src/main/java/cz/zcu/yamanager/business/ApiManager.java
9 9
import org.slf4j.Logger;
10 10
import org.slf4j.LoggerFactory;
11 11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.dao.DataAccessException;
12 13
import org.springframework.stereotype.Component;
13 14

  
15
import java.sql.SQLException;
14 16
import java.time.LocalDate;
15 17
import java.util.List;
16 18
import java.util.Optional;
......
38 40

  
39 41
    @Override
40 42
    public List<BasicProfileUser> getUsers(Status status) throws RESTFullException {
41
        List<BasicProfileUser> users = status == null ? this.userRepository.getAllBasicUsers() : this.userRepository.getAllBasicUsers(status);
43
        try {
44
            List<BasicProfileUser> users = status == null ? this.userRepository.getAllBasicUsers() : this.userRepository.getAllBasicUsers(status);
45

  
46
            LocalDate today = LocalDate.now();
47
            LocalDate weekBefore = today.minusDays(ApiManager.WEEK_LENGTH);
48
            LocalDate weekAfter = today.plusDays(ApiManager.WEEK_LENGTH);
49
            for (BasicProfileUser user : users) {
50
                user.setCalendar(this.vacationRepository.getVacationDays(user.getId(), weekBefore, weekAfter));
51
            }
42 52

  
43
        LocalDate today = LocalDate.now();
44
        LocalDate weekBefore = today.minusDays(ApiManager.WEEK_LENGTH);
45
        LocalDate weekAfter = today.plusDays(ApiManager.WEEK_LENGTH);
46
        for (BasicProfileUser user : users) {
47
            user.setCalendar(this.vacationRepository.getVacationDays(user.getId(), weekBefore, weekAfter));
53
            return users;
54
        } catch (DataAccessException e) {
55
            log.error(e.getMessage());
56
            throw new RESTFullException(e.getMessage(), "database.error");
48 57
        }
49

  
50
        return users;
51 58
    }
52 59

  
53 60
    @Override
54 61
    public List<VacationRequest> getVacationRequests(Status status) throws RESTFullException {
55
        return status == null ? this.requestRepository.getAllVacationRequests() : this.requestRepository.getAllVacationRequests(status);
62
        try {
63
            return status == null ? this.requestRepository.getAllVacationRequests() : this.requestRepository.getAllVacationRequests(status);
64
        } catch (DataAccessException e) {
65
            log.error(e.getMessage());
66
            throw new RESTFullException(e.getMessage(), "database.error");
67
        }
56 68
    }
57 69

  
58 70
    @Override
59 71
    public List<AuthorizationRequest> getAuthorizationRequests(Status status) throws RESTFullException {
60
        return status == null ? this.requestRepository.getAllAuthorizations() : this.requestRepository.getAllAuthorizations(status);
72
        try {
73
            return status == null ? this.requestRepository.getAllAuthorizations() : this.requestRepository.getAllAuthorizations(status);
74
        } catch (DataAccessException e) {
75
            log.error(e.getMessage());
76
            throw new RESTFullException(e.getMessage(), "database.error");
77
        }
61 78
    }
62 79

  
63 80
    @Override
64 81
    public FullUserProfile getUserProfile(Long userId) throws RESTFullException {
65
        return this.userRepository.getFullUser(userId);
82
        try {
83
            return this.userRepository.getFullUser(userId);
84
        } catch (DataAccessException e) {
85
            log.error(e.getMessage());
86
            throw new RESTFullException(e.getMessage(), "database.error");
87
        }
66 88
    }
67 89

  
68 90
    @Override
69 91
    public DefaultSettings getDefaultSettings() throws RESTFullException {
70
        return this.userRepository.getLastDefaultSettings().orElse(new DefaultSettings());
92
        try {
93
            return this.userRepository.getLastDefaultSettings().orElse(new DefaultSettings());
94
        } catch (DataAccessException e) {
95
            log.error(e.getMessage());
96
            throw new RESTFullException(e.getMessage(), "database.error");
97
        }
71 98
    }
72 99

  
73 100
    @Override
74 101
    public List<VacationDay> getUserCalendar(Long userId, LocalDate fromDate, LocalDate toDate, Status status) throws RESTFullException {
75
        List<VacationDay> vacations;
76
        if(status == null && toDate == null) {
77
            vacations = this.vacationRepository.getVacationDays(userId, fromDate);
78
        } else if(status == null) {
79
            vacations = this.vacationRepository.getVacationDays(userId, fromDate, toDate);
80
        } else if(toDate != null) {
81
            vacations = this.vacationRepository.getVacationDays(userId, fromDate, toDate, status);
82
        } else {
83
            vacations = this.vacationRepository.getVacationDays(userId, fromDate, status);
84
        }
102
        try {
103
            List<VacationDay> vacations;
104
            if (status == null && toDate == null) {
105
                vacations = this.vacationRepository.getVacationDays(userId, fromDate);
106
            } else if (status == null) {
107
                vacations = this.vacationRepository.getVacationDays(userId, fromDate, toDate);
108
            } else if (toDate != null) {
109
                vacations = this.vacationRepository.getVacationDays(userId, fromDate, toDate, status);
110
            } else {
111
                vacations = this.vacationRepository.getVacationDays(userId, fromDate, status);
112
            }
85 113

  
86
        return vacations;
114
            return vacations;
115
        } catch (DataAccessException e) {
116
            log.error(e.getMessage());
117
            throw new RESTFullException(e.getMessage(), "database.error");
118
        }
87 119
    }
88 120

  
89 121
    @Override
90 122
    public void createSettings(DefaultSettings settings) throws RESTFullException {
91
        cz.zcu.yamanager.domain.DefaultSettings defaultSettings = new cz.zcu.yamanager.domain.DefaultSettings();
92
        defaultSettings.setSickDayCount(settings.getSickDayCount());
93
        defaultSettings.setNotification(settings.getNotification());
94
        this.userRepository.insertSettings(defaultSettings);
123
        try {
124
            cz.zcu.yamanager.domain.DefaultSettings defaultSettings = new cz.zcu.yamanager.domain.DefaultSettings();
125
            defaultSettings.setSickDayCount(settings.getSickDayCount());
126
            defaultSettings.setNotification(settings.getNotification());
127
            this.userRepository.insertSettings(defaultSettings);
128
        } catch (DataAccessException e) {
129
            log.error(e.getMessage());
130
            throw new RESTFullException(e.getMessage(), "database.error");
131
        }
95 132
    }
96 133

  
97 134
    @Override
98 135
    public void createVacation(Long userId, VacationDay vacationDay) throws RESTFullException {
99
        User user = this.userRepository.getUser(userId);
100
        vacationDay.setStatus(user.getRole() == UserRole.EMPLOYER ? Status.ACCEPTED : Status.PENDING);
101

  
102
        cz.zcu.yamanager.domain.VacationDay vacation = new cz.zcu.yamanager.domain.VacationDay();
103
        vacation.setDate(vacationDay.getDate());
104
        vacation.setFrom(vacationDay.getFrom());
105
        vacation.setTo(vacationDay.getTo());
106
        vacation.setStatus(vacationDay.getStatus());
107
        vacation.setType(vacationDay.getType());
108

  
109
        if(vacation.getType() == VacationType.VACATION) {
110
            user.takeVacation(vacation.getFrom(), vacation.getTo());
111
        } else {
112
            user.takeSickDay();
113
        }
136
        try {
137
            User user = this.userRepository.getUser(userId);
138
            vacationDay.setStatus(user.getRole() == UserRole.EMPLOYER ? Status.ACCEPTED : Status.PENDING);
139

  
140
            cz.zcu.yamanager.domain.VacationDay vacation = new cz.zcu.yamanager.domain.VacationDay();
141
            vacation.setDate(vacationDay.getDate());
142
            vacation.setFrom(vacationDay.getFrom());
143
            vacation.setTo(vacationDay.getTo());
144
            vacation.setStatus(vacationDay.getStatus());
145
            vacation.setType(vacationDay.getType());
146

  
147
            if (vacation.getType() == VacationType.VACATION) {
148
                user.takeVacation(vacation.getFrom(), vacation.getTo());
149
            } else {
150
                user.takeSickDay();
151
            }
114 152

  
115
        this.vacationRepository.insertVacationDay(userId, vacation);
116
        this.userRepository.updateUser(user);
153
            this.vacationRepository.insertVacationDay(userId, vacation);
154
            this.userRepository.updateUser(user);
155
        } catch (DataAccessException e) {
156
            log.error(e.getMessage());
157
            throw new RESTFullException(e.getMessage(), "database.error");
158
        }
117 159
    }
118 160

  
119 161
    @Override
120 162
    public void changeSettings(Long userId, UserSettings settings) throws RESTFullException {
121
        User user = this.userRepository.getUser(userId);
122

  
123
        if(settings.getRole() == null && settings.getSickDayCount() == null && settings.getVacationCount() == null) {
124
            user.setNotification(settings.getNotification());
125
        } else {
126
            user.addVacationCount(settings.getVacationCount());
127
            user.setTotalSickDayCount(settings.getSickDayCount());
128
            user.setRole(settings.getRole());
129
        }
163
        try {
164
            User user = this.userRepository.getUser(userId);
130 165

  
131
        this.userRepository.updateUserSettings(user);
166
            if (settings.getRole() == null && settings.getSickDayCount() == null && settings.getVacationCount() == null) {
167
                user.setNotification(settings.getNotification());
168
            } else {
169
                user.addVacationCount(settings.getVacationCount());
170
                user.setTotalSickDayCount(settings.getSickDayCount());
171
                user.setRole(settings.getRole());
172
            }
173

  
174
            this.userRepository.updateUserSettings(user);
175
        } catch (DataAccessException e) {
176
            log.error(e.getMessage());
177
            throw new RESTFullException(e.getMessage(), "database.error");
178
        }
132 179
    }
133 180

  
134 181
    @Override
135 182
    public void changeVacation(Long userId, VacationDay vacationDay) throws RESTFullException {
136
        Optional<cz.zcu.yamanager.domain.VacationDay> vacation = this.vacationRepository.getVacationDay(vacationDay.getId());
137
        if(vacation.isPresent()) {
138
            vacation.get().setDate(vacationDay.getDate());
139
            vacation.get().setStatus(vacationDay.getStatus());
140
            vacation.get().setType(vacationDay.getType());
141
            vacation.get().setTime(vacationDay.getFrom(), vacationDay.getTo());
142
            this.vacationRepository.updateVacationDay(vacation.get());
143
        } else {
183
        try {
184
            Optional<cz.zcu.yamanager.domain.VacationDay> vacation = this.vacationRepository.getVacationDay(vacationDay.getId());
185
            if (vacation.isPresent()) {
186
                vacation.get().setDate(vacationDay.getDate());
187
                vacation.get().setStatus(vacationDay.getStatus());
188
                vacation.get().setType(vacationDay.getType());
189
                vacation.get().setTime(vacationDay.getFrom(), vacationDay.getTo());
190
                this.vacationRepository.updateVacationDay(vacation.get());
191
            } else {
144 192

  
193
            }
194
        } catch (DataAccessException e) {
195
            log.error(e.getMessage());
196
            throw new RESTFullException(e.getMessage(), "database.error");
145 197
        }
146 198
    }
147 199

  
148 200
    @Override
149 201
    public void changeRequest(RequestType type, BasicRequest request) throws RESTFullException {
150
        if(RequestType.VACATION == type) {
151
            Optional<User> user = this.vacationRepository.findUserByVacationID(request.getId());
152
            Optional<cz.zcu.yamanager.domain.VacationDay> vacationDay = this.vacationRepository.getVacationDay(request.getId());
153
            if(user.isPresent() && request.getStatus() == Status.REJECTED) {
154
                if(vacationDay.get().getType() == VacationType.SICK_DAY) {
155
                    user.get().addTakenSickDayCount(-1);
156
                } else {
157
                    user.get().addVacationCount(vacationDay.get().getFrom(), vacationDay.get().getTo());
202
        try {
203
            if (RequestType.VACATION == type) {
204
                Optional<User> user = this.vacationRepository.findUserByVacationID(request.getId());
205
                Optional<cz.zcu.yamanager.domain.VacationDay> vacationDay = this.vacationRepository.getVacationDay(request.getId());
206
                if (user.isPresent() && request.getStatus() == Status.REJECTED) {
207
                    if (vacationDay.get().getType() == VacationType.SICK_DAY) {
208
                        user.get().addTakenSickDayCount(-1);
209
                    } else {
210
                        user.get().addVacationCount(vacationDay.get().getFrom(), vacationDay.get().getTo());
211
                    }
158 212
                }
159
            }
160 213

  
161
            this.requestRepository.updateVacationRequest(request);
162
        } else {
163
            this.requestRepository.updateAuthorization(request);
214
                this.requestRepository.updateVacationRequest(request);
215
            } else {
216
                this.requestRepository.updateAuthorization(request);
217
            }
218
        } catch (DataAccessException e) {
219
            log.error(e.getMessage());
220
            throw new RESTFullException(e.getMessage(), "database.error");
164 221
        }
165 222
    }
166 223

  
167 224
    @Override
168 225
    public void deleteVacation(Long userId, Long vacationId) throws RESTFullException {
169
        User user = this.userRepository.getUser(userId);
170
        Optional<cz.zcu.yamanager.domain.VacationDay> vacation = this.vacationRepository.getVacationDay(vacationId);
171
        if (vacation.isPresent()){
172
            if(vacation.get().getType() == VacationType.SICK_DAY) {
173
                user.addTakenSickDayCount(-1);
174
            } else {
175
                user.addVacationCount(vacation.get().getFrom(), vacation.get().getTo());
226
        try {
227
            User user = this.userRepository.getUser(userId);
228
            Optional<cz.zcu.yamanager.domain.VacationDay> vacation = this.vacationRepository.getVacationDay(vacationId);
229
            if (vacation.isPresent()) {
230
                if (vacation.get().getType() == VacationType.SICK_DAY) {
231
                    user.addTakenSickDayCount(-1);
232
                } else {
233
                    user.addVacationCount(vacation.get().getFrom(), vacation.get().getTo());
234
                }
176 235
            }
177
        }
178 236

  
179
        this.userRepository.updateUser(user);
180
        this.vacationRepository.deleteVacationDay(vacationId);
237
            this.userRepository.updateUser(user);
238
            this.vacationRepository.deleteVacationDay(vacationId);
239
        } catch (DataAccessException e) {
240
            log.error(e.getMessage());
241
            throw new RESTFullException(e.getMessage(), "database.error");
242
        }
181 243
    }
182 244
}
server/src/main/java/cz/zcu/yamanager/ws/rest/ApiController.java
64 64
        } catch (RESTFullException e) {
65 65
            log.error(e.getMessage());
66 66
            return sendError(400, e.getLocalizedMessage(), language);
67
        } catch (DataAccessException | SQLException e) {
68
            log.error(e.getMessage());
69
            return sendError(500, "database.error", language);
70 67
        } catch (Exception e) {
71 68
            log.error(e.getMessage());
72 69
            return sendError(401, e.getMessage(), language);

Také k dispozici: Unified diff