Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 18b7c801

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

Fixed repositories

Zobrazit rozdíly:

server/src/main/java/cz/zcu/yamanager/repository/RequestRepository.java
5 5
import org.slf4j.LoggerFactory;
6 6
import org.springframework.beans.factory.annotation.Autowired;
7 7
import org.springframework.jdbc.core.JdbcTemplate;
8
import org.springframework.jdbc.core.RowMapper;
8 9
import org.springframework.stereotype.Repository;
9 10

  
10 11
import java.sql.ResultSet;
12
import java.sql.SQLException;
11 13
import java.sql.Time;
12 14
import java.util.List;
13 15

  
......
20 22
 */
21 23
@Repository
22 24
public class RequestRepository {
25
    /**
26
     * The mapper maps a row from a result of a query to an AuthorizationRequest.
27
     */
28
    private class AuthorizationRequestMapper implements RowMapper<AuthorizationRequest> {
29

  
30
        /**
31
         * Maps a row from a result of a query to an AuthorizationRequest.
32
         * @param resultSet the row from the result
33
         * @param i the index of the row
34
         * @return the AuthorizationRequest object
35
         * @throws SQLException if the columnLabel is not valid; if a database access error occurs or this method is called on a closed result set
36
         */
37
        @Override
38
        public AuthorizationRequest mapRow(ResultSet resultSet, int i) throws SQLException {
39
            final AuthorizationRequest request = new AuthorizationRequest();
40
            request.setId(resultSet.getLong("id"));
41
            request.setFirstName(resultSet.getString("first_name"));
42
            request.setLastName(resultSet.getString("last_name"));
43
            request.setTimestamp(resultSet.getTimestamp("creation_date").toLocalDateTime());
44
            request.setStatus(Status.getStatus(resultSet.getString("status")));
45
            return request;
46
        }
47
    }
48

  
49
    /**
50
     * The mapper maps each row from a result of a query to a VacationRequest.
51
     */
52
    private class VacationRequestMapper implements RowMapper<VacationRequest> {
53

  
54
        /**
55
         * Maps a row from a result of a query to an VacationRequest.
56
         * @param resultSet the row from the result
57
         * @param i the index of the row
58
         * @return the VacationRequest object
59
         * @throws SQLException if the columnLabel is not valid; if a database access error occurs or this method is called on a closed result set
60
         */
61
        @Override
62
        public VacationRequest mapRow(ResultSet resultSet, int i) throws SQLException {
63
            final VacationRequest request = new VacationRequest();
64
            request.setId(resultSet.getLong("v.id"));
65
            request.setFirstName(resultSet.getString("u.first_name"));
66
            request.setLastName(resultSet.getString("u.last_name"));
67
            request.setDate(resultSet.getDate("v.vacation_date").toLocalDate());
68

  
69
            /*
70
                When a result contains a sick day it doesn't have specified a start and end time because
71
                it can be taken only as a whole day. In this case the v.time_from and v.time_to are null.
72
                Which must be handled.
73
            */
74
            final Time timeFrom = resultSet.getTime("v.time_from");
75
            if (timeFrom != null) {
76
                request.setFrom(timeFrom.toLocalTime());
77
            }
78

  
79
            final Time timeTo = resultSet.getTime("v.time_to");
80
            if (timeTo != null) {
81
                request.setTo(timeTo.toLocalTime());
82
            }
83

  
84
            request.setTimestamp(resultSet.getTimestamp("v.creation_date").toLocalDateTime());
85
            request.setType(VacationType.getVacationType(resultSet.getString("v.vacation_type")));
86
            request.setStatus(Status.getStatus(resultSet.getString("v.status")));
87
            return request;
88
        }
89
    }
90

  
23 91
    /**
24 92
     * The logger.
25 93
     */
......
38 106
    @Autowired
39 107
    public RequestRepository(final JdbcTemplate jdbc) {
40 108
        RequestRepository.log.trace("Creating a new instance of the class RequestRepository");
109

  
41 110
        this.jdbc = jdbc;
42 111
    }
43 112

  
......
52 121
    public List<AuthorizationRequest> getAllAuthorizations() {
53 122
        RequestRepository.log.trace("Selecting all authorization requests from a database.");
54 123

  
55
        return this.jdbc.query("SELECT id, first_name, last_name, creation_date, status FROM end_user",
56
                (ResultSet rs, int rowNum) -> {
57
                    final AuthorizationRequest request = new AuthorizationRequest();
58
                    request.setId(rs.getLong("id"));
59
                    request.setFirstName(rs.getString("first_name"));
60
                    request.setLastName(rs.getString("last_name"));
61
                    request.setTimestamp(rs.getTimestamp("creation_date").toLocalDateTime());
62
                    request.setStatus(Status.getStatus(rs.getString("status")));
63
                    return request;
64
                });
124
        return this.jdbc.query("SELECT id, first_name, last_name, creation_date, status FROM end_user", new AuthorizationRequestMapper());
65 125
    }
66 126

  
67 127
    /**
......
77 137
        RequestRepository.log.trace("Selecting all authorization requests from a database with requested status.");
78 138
        RequestRepository.log.debug("Status: {}", status);
79 139

  
80
        return this.jdbc.query("SELECT id, first_name, last_name, creation_date FROM end_user WHERE status = ?",
81
                new Object[]{status.name()},
82
                (ResultSet rs, int rowNum) -> {
83
                    final AuthorizationRequest request = new AuthorizationRequest();
84
                    request.setId(rs.getLong("id"));
85
                    request.setFirstName(rs.getString("first_name"));
86
                    request.setLastName(rs.getString("last_name"));
87
                    request.setTimestamp(rs.getTimestamp("creation_date").toLocalDateTime());
88
                    request.setStatus(status);
89
                    return request;
90
                });
140
        return this.jdbc.query("SELECT id, first_name, last_name, creation_date, status FROM end_user WHERE status = ?", new Object[]{status.name()}, new AuthorizationRequestMapper());
91 141
    }
92 142

  
93 143
    /**
......
124 174
        RequestRepository.log.trace("Selecting all vacation requests from a database.");
125 175

  
126 176
        return this.jdbc.query("SELECT v.id, v.vacation_date, v.time_from, v.time_to, v.creation_date, v.vacation_type, v.status, u.first_name, u.last_name " +
127
                        "FROM vacation_day v " +
128
                        "INNER JOIN end_user u ON v.user_id = u.id",
129
                (ResultSet rs, int rowNum) -> {
130
                    final VacationRequest request = new VacationRequest();
131
                    request.setId(rs.getLong("v.id"));
132
                    request.setFirstName(rs.getString("u.first_name"));
133
                    request.setLastName(rs.getString("u.last_name"));
134
                    request.setDate(rs.getDate("v.vacation_date").toLocalDate());
135

  
136
                    /*
137
                        When a result contains a sickday it doesn't have specified a start and end time because
138
                        it can be taken only as a whole day. In this case the v.time_from and v.time_to are null.
139
                        Which must be handled.
140
                     */
141
                    final Time timeFrom = rs.getTime("v.time_from");
142
                    if (timeFrom != null) {
143
                        request.setFrom(timeFrom.toLocalTime());
144
                    }
145

  
146
                    final Time timeTo = rs.getTime("v.time_to");
147
                    if (timeTo != null) {
148
                        request.setTo(timeTo.toLocalTime());
149
                    }
150

  
151
                    request.setTimestamp(rs.getTimestamp("v.creation_date").toLocalDateTime());
152
                    request.setType(VacationType.getVacationType(rs.getString("v.vacation_type")));
153
                    request.setStatus(Status.getStatus(rs.getString("v.status")));
154
                    return request;
155
                });
177
                "FROM vacation_day v " +
178
                "INNER JOIN end_user u ON v.user_id = u.id", new VacationRequestMapper());
156 179
    }
157 180

  
158 181
    /**
......
171 194
                        "FROM vacation_day v " +
172 195
                        "INNER JOIN end_user u ON v.user_id = u.id " +
173 196
                        "WHERE v.status=?",
174
                new Object[]{status.name()},
175
                (ResultSet rs, int rowNum) -> {
176
                    final VacationRequest request = new VacationRequest();
177
                    request.setId(rs.getLong("v.id"));
178
                    request.setFirstName(rs.getString("u.first_name"));
179
                    request.setLastName(rs.getString("u.last_name"));
180
                    request.setDate(rs.getDate("v.vacation_date").toLocalDate());
181

  
182
                     /*
183
                        When a result contains a sickday it doesn't have specified start and end time because
184
                        it can be taken only as a whole day. In this case the v.time_from and v.time_to are null.
185
                        Which must be handled.
186
                     */
187
                    final Time timeFrom = rs.getTime("v.time_from");
188
                    if (timeFrom != null) {
189
                        request.setFrom(timeFrom.toLocalTime());
190
                    }
191

  
192
                    final Time timeTo = rs.getTime("v.time_to");
193
                    if (timeTo != null) {
194
                        request.setTo(timeTo.toLocalTime());
195
                    }
196

  
197
                    request.setTimestamp(rs.getTimestamp("v.creation_date").toLocalDateTime());
198
                    request.setType(VacationType.getVacationType(rs.getString("v.vacation_type")));
199
                    request.setStatus(status);
200
                    return request;
201
                });
197
                new Object[]{status.name()}, new VacationRequestMapper());
202 198
    }
203 199

  
204 200
    /**
server/src/main/java/cz/zcu/yamanager/repository/UserRepository.java
6 6
import org.slf4j.LoggerFactory;
7 7
import org.springframework.beans.factory.annotation.Autowired;
8 8
import org.springframework.jdbc.core.JdbcTemplate;
9
import org.springframework.jdbc.core.RowMapper;
9 10
import org.springframework.jdbc.core.SqlOutParameter;
10 11
import org.springframework.jdbc.core.SqlParameter;
12
import org.springframework.jdbc.support.GeneratedKeyHolder;
13
import org.springframework.jdbc.support.KeyHolder;
11 14
import org.springframework.stereotype.Repository;
12 15

  
13 16
import java.sql.*;
14 17
import java.util.ArrayList;
15 18
import java.util.List;
16 19
import java.util.Map;
20
import java.util.Optional;
17 21

  
18 22
/**
19 23
 * An instance of the class UserRepository handles queries which selects and updates users and their settings in a database.
20 24
 */
21 25
@Repository
22 26
public class UserRepository {
27

  
28
    /**
29
     * The mapper maps a row from a result of a query to an BasicProfileUser.
30
     */
31
    private class BasicProfileUserMapper implements RowMapper<BasicProfileUser> {
32

  
33
        /**
34
         * Maps a row from a result of a query to an BasicProfileUser.
35
         * @param resultSet the row from the result
36
         * @param i the index of the row
37
         * @return the BasicProfileUser object
38
         * @throws SQLException if the columnLabel is not valid; if a database access error occurs or this method is called on a closed result set
39
         */
40
        @Override
41
        public BasicProfileUser mapRow(ResultSet resultSet, int i) throws SQLException {
42
            final BasicProfileUser user = new BasicProfileUser();
43
            user.setId(resultSet.getLong("id"));
44
            user.setFirstName(resultSet.getString("first_name"));
45
            user.setLastName(resultSet.getString("last_name"));
46
            user.setPhoto(resultSet.getString("photo"));
47
            return user;
48
        }
49
    }
50

  
51
    /**
52
     * The mapper maps a row from a result of a query to an BasicProfileUser.
53
     */
54
    private class DefaultSettingsMapper implements RowMapper<DefaultSettings> {
55

  
56
        /**
57
         * Maps a row from a result of a query to an DefaultSettings.
58
         * @param resultSet the row from the result
59
         * @param i the index of the row
60
         * @return the DefaultSettings object
61
         * @throws SQLException if the columnLabel is not valid; if a database access error occurs or this method is called on a closed result set
62
         */
63
        @Override
64
        public DefaultSettings mapRow(ResultSet resultSet, int i) throws SQLException {
65
            final DefaultSettings settings = new DefaultSettings();
66
            settings.setSickDayCount(resultSet.getInt("no_sick_days"));
67
            settings.setNotification(resultSet.getTimestamp("alert").toLocalDateTime());
68
            return settings;
69
        }
70
    }
71

  
23 72
    /**
24 73
     * The logger.
25 74
     */
......
127 176
    public List<BasicProfileUser> getAllBasicUsers() {
128 177
        UserRepository.log.trace("Selecting basic profiles of all users from a database.");
129 178

  
130
        return this.jdbc.query("SELECT id, first_name, last_name, photo FROM end_user", (ResultSet rs, int rowNum) -> {
131
            final BasicProfileUser user = new BasicProfileUser();
132
            user.setId(rs.getLong("id"));
133
            user.setFirstName(rs.getString("first_name"));
134
            user.setLastName(rs.getString("last_name"));
135
            user.setPhoto(rs.getString("photo"));
136
            return user;
137
        });
179
        return this.jdbc.query("SELECT id, first_name, last_name, photo FROM end_user", new BasicProfileUserMapper());
138 180
    }
139 181

  
140 182
    /**
......
151 193
        UserRepository.log.debug("Status: {}", status);
152 194

  
153 195
        return this.jdbc.query("SELECT id, first_name, last_name, photo FROM end_user WHERE status = ?",
154
                new Object[]{status.name()}, (ResultSet rs, int rowNum) -> {
155
                    final BasicProfileUser user = new BasicProfileUser();
156
                    user.setId(rs.getLong("id"));
157
                    user.setFirstName(rs.getString("first_name"));
158
                    user.setLastName(rs.getString("last_name"));
159
                    user.setPhoto(rs.getString("photo"));
160
                    return user;
161
                });
196
                new Object[]{status.name()}, new BasicProfileUserMapper());
162 197
    }
163 198

  
164 199
    /**
......
207 242
        return user;
208 243
    }
209 244

  
210
    public DefaultSettings getLastDefaultSettings() {
211
        return this.jdbc.queryForObject("SELECT * FROM default_settings ORDER BY id DESC LIMIT 1", (ResultSet rs, int rowNum) -> {
212
            final DefaultSettings settings = new DefaultSettings();
213
            settings.setSickDayCount(rs.getInt("no_sick_days"));
214
            settings.setNotification(rs.getTimestamp("alert").toLocalDateTime());
215
            return settings;
216
        });
245
    public Optional<DefaultSettings> getLastDefaultSettings() {
246
        return Optional.ofNullable(this.jdbc.queryForObject("SELECT * FROM default_settings ORDER BY id DESC LIMIT 1", new DefaultSettingsMapper()));
217 247
    }
218 248

  
219 249
    //---------------------------------- DOMAIN -----------------------------------
220 250

  
221 251
    public User getUser(final long id) {
222 252
        final Map<String, Object> resultMap = this.getUserColumns(id);
223
        return new User(
224
                id,
225
                (String) resultMap.get("out_first_name"),
226
                (String) resultMap.get("out_last_name"),
227
                ((Double) resultMap.get("out_no_vacations")).floatValue(),
228
                (Integer) resultMap.get("out_no_sick_days"),
229
                (Integer) resultMap.get("out_taken_sick_days"),
230
                ((Timestamp) resultMap.get("out_alert")).toLocalDateTime(),
231
                (String) resultMap.get("out_token"),
232
                (String) resultMap.get("out_email"),
233
                (String) resultMap.get("out_photo"),
234
                ((Timestamp) resultMap.get("out_creation_date")).toLocalDateTime(),
235
                UserRole.getUserRole((String) resultMap.get("out_role")),
236
                Status.getStatus((String) resultMap.get("out_status"))
237
        );
253
        User user = new User();
254
        user.setId((Long) resultMap.get("out_id"));
255
        user.setFirstName((String)resultMap.get("out_first_name"));
256
        user.setLastName((String)resultMap.get("out_last_name"));
257
        user.setVacationCount(((Double)resultMap.get("out_no_vacations")).floatValue());
258
        user.setTotalSickDayCount((Integer)resultMap.get("out_no_sick_days"));
259
        user.setTakenSickDayCount((Integer)resultMap.get("out_taken_sick_days"));
260
        user.setNotification(((Timestamp)resultMap.get("out_alert")).toLocalDateTime());
261
        user.setToken((String)resultMap.get("out_token"));
262
        user.setEmail((String)resultMap.get("out_email"));
263
        user.setPhoto((String)resultMap.get("out_photo"));
264
        user.setCreationDate(((Timestamp)resultMap.get("out_creation_date")).toLocalDateTime());
265
        user.setRole(UserRole.getUserRole((String)resultMap.get("out_role")));
266
        user.setStatus(Status.getStatus((String)resultMap.get("out_status")));
267
        return user;
268
    }
269

  
270
    public void updateUserSettings(final  User user) {
271
        this.jdbc.update("UPDATE end_user SET no_vacations = ?, no_sick_days = ?, alert = ? WHERE id = ?",
272
                user.getVacationCount(), user.getTotalSickDayCount(), user.getNotification(), user.getId());
238 273
    }
239 274

  
240
    public void updateUser(final cz.zcu.yamanager.domain.User user) {
241
        this.jdbc.update("UPDATE end_user SET first_name = ?, last_name = ?, no_vacations = ?, no_sick_days = ?, taken_sick_days = ?, alert = ?, token = ?, email = ?, photo = ?, user_role = ?, status = ? WHERE id = ?",
242
                user.getFirstName(), user.getLastName(), user.getVacationCount(), user.getTotalSickDayCount(), user.getTakenSickDayCount(), user.getNotification(), user.getToken(), user.getEmail(), user.getPhoto(), user.getRole().name(), user.getStatus().name(), user.getId());
275
    public void updateUser(final User user) {
276
        this.jdbc.update("UPDATE end_user SET first_name = ?, last_name = ?, no_vacations = ?, taken_sick_days = ?, token = ?, email = ?, photo = ?, user_role = ?, status = ? WHERE id = ?",
277
                user.getFirstName(), user.getLastName(), user.getVacationCount(), user.getTakenSickDayCount(), user.getToken(), user.getEmail(), user.getPhoto(), user.getRole().name(), user.getStatus().name(), user.getId());
243 278
    }
244 279

  
245 280
    public void insertUser(final User user) {
server/src/main/java/cz/zcu/yamanager/repository/VacationRepository.java
1 1
package cz.zcu.yamanager.repository;
2 2

  
3
import cz.zcu.yamanager.domain.User;
3 4
import cz.zcu.yamanager.dto.Status;
5
import cz.zcu.yamanager.dto.UserRole;
4 6
import cz.zcu.yamanager.dto.VacationDay;
5 7
import cz.zcu.yamanager.dto.VacationType;
6 8

  
......
8 10
import org.slf4j.LoggerFactory;
9 11
import org.springframework.beans.factory.annotation.Autowired;
10 12
import org.springframework.jdbc.core.JdbcTemplate;
13
import org.springframework.jdbc.core.RowMapper;
11 14
import org.springframework.stereotype.Repository;
12 15

  
13 16
import java.sql.ResultSet;
17
import java.sql.SQLException;
14 18
import java.sql.Time;
15 19
import java.time.LocalDate;
16 20
import java.util.List;
21
import java.util.Optional;
17 22

  
18 23
@Repository
19 24
public class VacationRepository {
25
    /**
26
     * The mapper maps a row from a result of a query to an VacationDay.
27
     */
28
    private class VacationDayMapper implements RowMapper<VacationDay> {
29

  
30
        /**
31
         * Maps a row from a result of a query to an VacationDay.
32
         * @param resultSet the row from the result
33
         * @param i the index of the row
34
         * @return the VacationDay object
35
         * @throws SQLException if the columnLabel is not valid; if a database access error occurs or this method is called on a closed result set
36
         */
37
        @Override
38
        public VacationDay mapRow(ResultSet resultSet, int i) throws SQLException {
39
            final VacationDay item = new VacationDay();
40
            item.setId(resultSet.getLong("v.id"));
41
            item.setDate(resultSet.getDate("v.vacation_date").toLocalDate());
42

  
43
            /*
44
                When a result contains a sick day it doesn't have specified a start and end time because
45
                it can be taken only as a whole day. In this case the v.time_from and v.time_to are null.
46
                Which must be handled.
47
            */
48
            final Time timeFrom = resultSet.getTime("v.time_from");
49
            if (timeFrom != null) {
50
                item.setFrom(timeFrom.toLocalTime());
51
            }
52

  
53
            final Time timeTo = resultSet.getTime("v.time_to");
54
            if (timeTo != null) {
55
                item.setTo(timeTo.toLocalTime());
56
            }
57

  
58
            item.setStatus(Status.getStatus(resultSet.getString("v.status")));
59
            item.setType(VacationType.getVacationType(resultSet.getString("v.vacation_type")));
60
            return item;
61
        }
62
    }
63

  
20 64
    /**
21 65
     * The logger.
22 66
     */
......
35 79
    @Autowired
36 80
    public VacationRepository(final JdbcTemplate jdbc) {
37 81
        VacationRepository.log.trace("Creating a new instance of the class VacationRepository");
82

  
38 83
        this.jdbc = jdbc;
39 84
    }
40 85

  
......
43 88
                        "FROM vacation_day v " +
44 89
                        "INNER JOIN end_user u ON v.user_id = u.id " +
45 90
                        "WHERE v.user_id = ? AND v.vacation_date >= ?",
46
                new Object[]{userId, from}, (ResultSet rs, int rowNum) -> {
47
                    final VacationDay item = new VacationDay();
48
                    item.setId(rs.getLong("v.id"));
49
                    item.setDate(rs.getDate("v.vacation_date").toLocalDate());
50
                    final Time timeFrom = rs.getTime("v.time_from");
51
                    if (timeFrom != null) {
52
                        item.setFrom(timeFrom.toLocalTime());
53
                    }
54

  
55
                    final Time timeTo = rs.getTime("v.time_to");
56
                    if (timeTo != null) {
57
                        item.setTo(timeTo.toLocalTime());
58
                    }
59

  
60
                    item.setStatus(Status.getStatus(rs.getString("v.status")));
61
                    item.setType(VacationType.getVacationType(rs.getString("v.vacation_type")));
62
                    return item;
63
                });
91
                new Object[]{userId, from}, new VacationDayMapper());
64 92
    }
65 93

  
66 94
    public List<VacationDay> getVacationDays(final long userId, final LocalDate from, final Status status) {
......
68 96
                        "FROM vacation_day v " +
69 97
                        "INNER JOIN end_user u ON v.user_id = u.id " +
70 98
                        "WHERE v.user_id = ? AND v.vacation_date >= ? AND v.status = ?",
71
                new Object[]{userId, from, status.name()}, (ResultSet rs, int rowNum) -> {
72
                    final VacationDay item = new VacationDay();
73
                    item.setId(rs.getLong("v.id"));
74
                    item.setDate(rs.getDate("v.vacation_date").toLocalDate());
75
                    final Time timeFrom = rs.getTime("v.time_from");
76
                    if (timeFrom != null) {
77
                        item.setFrom(timeFrom.toLocalTime());
78
                    }
79

  
80
                    final Time timeTo = rs.getTime("v.time_to");
81
                    if (timeTo != null) {
82
                        item.setTo(timeTo.toLocalTime());
83
                    }
84

  
85
                    item.setStatus(Status.getStatus(rs.getString("v.status")));
86
                    item.setType(VacationType.getVacationType(rs.getString("v.vacation_type")));
87
                    return item;
88
                });
99
                new Object[]{userId, from, status.name()}, new VacationDayMapper());
89 100
    }
90 101

  
91 102
    public List<VacationDay> getVacationDays(final long userId, final LocalDate from, final LocalDate to) {
......
93 104
                        "FROM vacation_day v " +
94 105
                        "INNER JOIN end_user u ON v.user_id = u.id " +
95 106
                        "WHERE v.user_id=? AND v.vacation_date >= ? AND v.vacation_date <= ?",
96
                new Object[]{userId, from, to}, (ResultSet rs, int rowNum) -> {
97
                    final VacationDay item = new VacationDay();
98
                    item.setId(rs.getLong("v.id"));
99
                    item.setDate(rs.getDate("v.vacation_date").toLocalDate());
100
                    final Time timeFrom = rs.getTime("v.time_from");
101
                    if (timeFrom != null) {
102
                        item.setFrom(timeFrom.toLocalTime());
103
                    }
104

  
105
                    final Time timeTo = rs.getTime("v.time_to");
106
                    if (timeTo != null) {
107
                        item.setTo(timeTo.toLocalTime());
108
                    }
109

  
110
                    item.setStatus(Status.getStatus(rs.getString("v.status")));
111
                    item.setType(VacationType.getVacationType(rs.getString("v.vacation_type")));
112
                    return item;
113
                });
107
                new Object[]{userId, from, to}, new VacationDayMapper());
114 108

  
115 109
    }
116 110

  
......
119 113
                        "FROM vacation_day v " +
120 114
                        "INNER JOIN end_user u ON v.user_id = u.id " +
121 115
                        "WHERE v.user_id=? AND v.vacation_date >= ? AND v.vacation_date <= ? AND v.status = ?",
122
                new Object[]{userId, from, to, status.name()}, (ResultSet rs, int rowNum) -> {
123
                    final VacationDay item = new VacationDay();
124
                    item.setId(rs.getLong("v.id"));
125
                    item.setDate(rs.getDate("v.vacation_date").toLocalDate());
116
                new Object[]{userId, from, to, status.name()}, new VacationDayMapper());
117
    }
126 118

  
127
                    final Time timeFrom = rs.getTime("v.time_from");
119
    public Optional<cz.zcu.yamanager.domain.VacationDay> getVacationDay(final long id) {
120
        return Optional.ofNullable(this.jdbc.queryForObject("SELECT id, vacation_date, time_from, time_to, creation_date, status, vacation_type " +
121
                        "FROM vacation_day WHERE id = ?", new Object[]{id},
122
                (ResultSet rs, int rowNum) -> {
123
                    cz.zcu.yamanager.domain.VacationDay vacationDay = new cz.zcu.yamanager.domain.VacationDay();
124
                    vacationDay.setId(rs.getLong("id"));
125
                    vacationDay.setDate(rs.getDate("vacation_date").toLocalDate());
126
                    /*
127
                        When a result contains a sick day it doesn't have specified a start and end time because
128
                        it can be taken only as a whole day. In this case the v.time_from and v.time_to are null.
129
                        Which must be handled.
130
                    */
131
                    final Time timeFrom = rs.getTime("time_from");
128 132
                    if (timeFrom != null) {
129
                        item.setFrom(timeFrom.toLocalTime());
133
                        vacationDay.setFrom(timeFrom.toLocalTime());
130 134
                    }
131 135

  
132
                    final Time timeTo = rs.getTime("v.time_to");
136
                    final Time timeTo = rs.getTime("time_to");
133 137
                    if (timeTo != null) {
134
                        item.setTo(timeTo.toLocalTime());
138
                        vacationDay.setTo(timeTo.toLocalTime());
135 139
                    }
136 140

  
137
                    item.setStatus(Status.getStatus(rs.getString("v.status")));
138
                    item.setType(VacationType.getVacationType(rs.getString("v.vacation_type")));
139
                    return item;
140
                });
141
    }
142

  
143
    public cz.zcu.yamanager.domain.VacationDay getVacationDay(final long id) {
144
        return this.jdbc.queryForObject("SELECT id, vacation_date, time_from, time_to, creation_date, status, vacation_type" +
145
                        "FROM vacation_day " +
146
                        "WHERE id = ?", new Object[]{id},
147
                (ResultSet rs, int rowNum) ->
148
                    new cz.zcu.yamanager.domain.VacationDay(
149
                            rs.getLong("id"),
150
                            rs.getDate("vacation_date").toLocalDate(),
151
                            rs.getTime("time_from").toLocalTime(),
152
                            rs.getTime("time_to").toLocalTime(),
153
                            rs.getTimestamp("creation_date").toLocalDateTime(),
154
                            Status.getStatus(rs.getString("status")),
155
                            VacationType.getVacationType(rs.getString("v.vacation_type")))
156
                );
141
                    vacationDay.setCreationDate(rs.getTimestamp("creation_date").toLocalDateTime());
142
                    vacationDay.setStatus(Status.getStatus(rs.getString("status")));
143
                    vacationDay.setType(VacationType.getVacationType(rs.getString("vacation_type")));
144
                    return vacationDay;
145
                }));
157 146
    }
158 147

  
159 148
    public void insertVacationDay(final Long userId, final cz.zcu.yamanager.domain.VacationDay day) {
......
169 158
    public void deleteVacationDay(final long id) {
170 159
        this.jdbc.update("DELETE FROM vacation_day WHERE id=?", id);
171 160
    }
161

  
162
    public Optional<User> findUserByVacationID(final long id) {
163
        return Optional.ofNullable(this.jdbc.queryForObject("SELECT u.* FROM vacation_day v INNER JOIN end_user u ON v.user_id = u.id WHERE v.user_id = ?", new Object[]{id}, (ResultSet rs, int rowNum)->
164
        {
165
            User user = new User();
166
            user.setId(rs.getLong("u.id"));
167
            user.setFirstName(rs.getString("u.first_name"));
168
            user.setLastName(rs.getString("u.last_name"));
169
            user.setVacationCount(rs.getFloat("u.no_vacations"));
170
            user.setTotalSickDayCount(rs.getInt("u.total_sick_days"));
171
            user.setTakenSickDayCount(rs.getInt("u.taken_sick_days"));
172
            user.setNotification(rs.getTimestamp("u.alert").toLocalDateTime());
173
            user.setToken(rs.getString("u.token"));
174
            user.setEmail(rs.getString("u.email"));
175
            user.setPhoto(rs.getString("u.photo"));
176
            user.setCreationDate(rs.getTimestamp("u.creation_date").toLocalDateTime());
177
            user.setRole(UserRole.getUserRole(rs.getString("u.role")));
178
            user.setStatus(Status.getStatus(rs.getString("u.status")));
179
            return user;
180
        }));
181
    }
172 182
}

Také k dispozici: Unified diff