Revize a2d2386e
Přidáno uživatelem Pavel Fidransky před více než 4 roky(ů)
server/src/main/java/org/danekja/ymanager/repository/RequestRepository.java | ||
---|---|---|
1 | 1 |
package org.danekja.ymanager.repository; |
2 | 2 |
|
3 | 3 |
import org.danekja.ymanager.domain.Status; |
4 |
import org.danekja.ymanager.domain.VacationType; |
|
5 |
import org.danekja.ymanager.dto.*; |
|
6 |
import org.slf4j.Logger; |
|
7 |
import org.slf4j.LoggerFactory; |
|
8 |
import org.springframework.beans.factory.annotation.Autowired; |
|
9 |
import org.springframework.jdbc.core.JdbcTemplate; |
|
10 |
import org.springframework.jdbc.core.RowMapper; |
|
11 |
import org.springframework.stereotype.Repository; |
|
4 |
import org.danekja.ymanager.dto.AuthorizationRequestDTO; |
|
5 |
import org.danekja.ymanager.dto.BasicRequestDTO; |
|
6 |
import org.danekja.ymanager.dto.VacationRequestDTO; |
|
12 | 7 |
|
13 |
import java.sql.ResultSet; |
|
14 |
import java.sql.SQLException; |
|
15 |
import java.sql.Time; |
|
16 | 8 |
import java.util.List; |
17 | 9 |
|
18 | 10 |
/** |
... | ... | |
22 | 14 |
* request which works like the authorization request but for vacations and contains informations showed to an employer |
23 | 15 |
* to help him approve or reject the vacation. |
24 | 16 |
*/ |
25 |
@Repository |
|
26 |
public class RequestRepository { |
|
27 |
/** |
|
28 |
* The mapper maps a row from a result of a query to an AuthorizationRequest. |
|
29 |
*/ |
|
30 |
private class AuthorizationRequestMapper implements RowMapper<AuthorizationRequestDTO> { |
|
31 |
|
|
32 |
/** |
|
33 |
* Maps a row from a result of a query to an AuthorizationRequest. |
|
34 |
* @param resultSet the row from the result |
|
35 |
* @param i the index of the row |
|
36 |
* @return the AuthorizationRequest object |
|
37 |
* @throws SQLException if the columnLabel is not valid; if a database access error occurs or this method is called on a closed result set |
|
38 |
*/ |
|
39 |
@Override |
|
40 |
public AuthorizationRequestDTO mapRow(ResultSet resultSet, int i) throws SQLException { |
|
41 |
final AuthorizationRequestDTO request = new AuthorizationRequestDTO(); |
|
42 |
request.setId(resultSet.getLong("id")); |
|
43 |
request.setFirstName(resultSet.getString("first_name")); |
|
44 |
request.setLastName(resultSet.getString("last_name")); |
|
45 |
request.setTimestamp(resultSet.getTimestamp("creation_date").toLocalDateTime()); |
|
46 |
request.setStatus(Status.valueOf(resultSet.getString("status"))); |
|
47 |
return request; |
|
48 |
} |
|
49 |
} |
|
50 |
|
|
51 |
/** |
|
52 |
* The mapper maps each row from a result of a query to a VacationRequest. |
|
53 |
*/ |
|
54 |
private class VacationRequestMapper implements RowMapper<VacationRequestDTO> { |
|
55 |
|
|
56 |
/** |
|
57 |
* Maps a row from a result of a query to an VacationRequest. |
|
58 |
* @param resultSet the row from the result |
|
59 |
* @param i the index of the row |
|
60 |
* @return the VacationRequest 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 VacationRequestDTO mapRow(ResultSet resultSet, int i) throws SQLException { |
|
65 |
final VacationRequestDTO request = new VacationRequestDTO(); |
|
66 |
request.setId(resultSet.getLong("v.id")); |
|
67 |
request.setFirstName(resultSet.getString("u.first_name")); |
|
68 |
request.setLastName(resultSet.getString("u.last_name")); |
|
69 |
request.setDate(resultSet.getDate("v.vacation_date").toLocalDate()); |
|
70 |
|
|
71 |
/* |
|
72 |
When a result contains a sick day it doesn't have specified a start and end time because |
|
73 |
it can be taken only as a whole day. In this case the v.time_from and v.time_to are null. |
|
74 |
Which must be handled. |
|
75 |
*/ |
|
76 |
final Time timeFrom = resultSet.getTime("v.time_from"); |
|
77 |
if (timeFrom != null) { |
|
78 |
request.setFrom(timeFrom.toLocalTime()); |
|
79 |
} |
|
80 |
|
|
81 |
final Time timeTo = resultSet.getTime("v.time_to"); |
|
82 |
if (timeTo != null) { |
|
83 |
request.setTo(timeTo.toLocalTime()); |
|
84 |
} |
|
85 |
|
|
86 |
request.setTimestamp(resultSet.getTimestamp("v.creation_date").toLocalDateTime()); |
|
87 |
request.setType(VacationType.getVacationType(resultSet.getString("v.vacation_type"))); |
|
88 |
request.setStatus(Status.valueOf(resultSet.getString("v.status"))); |
|
89 |
return request; |
|
90 |
} |
|
91 |
} |
|
92 |
|
|
93 |
/** |
|
94 |
* The logger. |
|
95 |
*/ |
|
96 |
private static final Logger log = LoggerFactory.getLogger(RequestRepository.class); |
|
97 |
|
|
98 |
/** |
|
99 |
* The connection to a database. |
|
100 |
*/ |
|
101 |
private final JdbcTemplate jdbc; |
|
102 |
|
|
103 |
/** |
|
104 |
* Creates a manager which can select and update requests in a database with the specified connection. |
|
105 |
* |
|
106 |
* @param jdbc the connection to the database |
|
107 |
*/ |
|
108 |
@Autowired |
|
109 |
public RequestRepository(final JdbcTemplate jdbc) { |
|
110 |
RequestRepository.log.trace("Creating a new instance of the class RequestRepository"); |
|
111 |
|
|
112 |
this.jdbc = jdbc; |
|
113 |
} |
|
17 |
public interface RequestRepository { |
|
114 | 18 |
|
115 | 19 |
/** |
116 | 20 |
* Gets all authorization requests from a database. Method returns all authorization requests despite |
... | ... | |
120 | 24 |
* |
121 | 25 |
* @return the list of all authorization requests |
122 | 26 |
*/ |
123 |
public List<AuthorizationRequestDTO> getAllAuthorizations() { |
|
124 |
RequestRepository.log.trace("Selecting all authorization requests from a database."); |
|
125 |
|
|
126 |
return this.jdbc.query("SELECT id, first_name, last_name, creation_date, status FROM end_user", new AuthorizationRequestMapper()); |
|
127 |
} |
|
27 |
List<AuthorizationRequestDTO> getAllAuthorizations(); |
|
128 | 28 |
|
129 | 29 |
/** |
130 | 30 |
* Gets all authorization request with the given authorization status from a database. |
... | ... | |
135 | 35 |
* @param status the approval status of the requests |
136 | 36 |
* @return the list of all authorization requests with the given status |
137 | 37 |
*/ |
138 |
public List<AuthorizationRequestDTO> getAllAuthorizations(final Status status) { |
|
139 |
RequestRepository.log.trace("Selecting all authorization requests from a database with requested status."); |
|
140 |
RequestRepository.log.debug("Status: {}", status); |
|
141 |
|
|
142 |
return this.jdbc.query("SELECT id, first_name, last_name, creation_date, status FROM end_user WHERE status = ?", new Object[]{status.name()}, new AuthorizationRequestMapper()); |
|
143 |
} |
|
38 |
List<AuthorizationRequestDTO> getAllAuthorizations(final Status status); |
|
144 | 39 |
|
145 | 40 |
/** |
146 | 41 |
* Updates the status of an authorization request with the given id. |
... | ... | |
148 | 43 |
* @param id the id of the request |
149 | 44 |
* @param status the new status of the request |
150 | 45 |
*/ |
151 |
public void updateAuthorization(final long id, final Status status) { |
|
152 |
RequestRepository.log.trace("Updating status of an authorization request."); |
|
153 |
RequestRepository.log.debug("Id: {}, status: {}.", id, status); |
|
154 |
|
|
155 |
this.jdbc.update("UPDATE end_user SET status=? WHERE id=?", status.name(), id); |
|
156 |
} |
|
46 |
void updateAuthorization(final long id, final Status status); |
|
157 | 47 |
|
158 | 48 |
/** |
159 | 49 |
* Updates the status of an authorization request from the specified BasicRequest object. |
160 | 50 |
* |
161 | 51 |
* @param request the BasicRequest object with new values of the vacation request |
162 | 52 |
*/ |
163 |
public void updateAuthorization(final BasicRequestDTO request) { |
|
164 |
this.updateAuthorization(request.getId(), request.getStatus()); |
|
165 |
} |
|
53 |
void updateAuthorization(final BasicRequestDTO request); |
|
166 | 54 |
|
167 | 55 |
/** |
168 | 56 |
* Gets all vacation requests from a database. Method returns all vacation requests despite |
... | ... | |
172 | 60 |
* |
173 | 61 |
* @return the list of all vacation requests |
174 | 62 |
*/ |
175 |
public List<VacationRequestDTO> getAllVacationRequests() { |
|
176 |
RequestRepository.log.trace("Selecting all vacation requests from a database."); |
|
177 |
|
|
178 |
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 " + |
|
179 |
"FROM vacation_day v " + |
|
180 |
"INNER JOIN end_user u ON v.user_id = u.id", new VacationRequestMapper()); |
|
181 |
} |
|
63 |
List<VacationRequestDTO> getAllVacationRequests(); |
|
182 | 64 |
|
183 | 65 |
/** |
184 | 66 |
* Gets all vacation requests with the given approval status from a database. |
... | ... | |
188 | 70 |
* @param status the approval status of the requests |
189 | 71 |
* @return the list of all vacation requests with the given status |
190 | 72 |
*/ |
191 |
public List<VacationRequestDTO> getAllVacationRequests(final Status status) { |
|
192 |
RequestRepository.log.trace("Selecting all vacation requests from a database with requested status."); |
|
193 |
RequestRepository.log.debug("Status: {}", status); |
|
194 |
|
|
195 |
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 " + |
|
196 |
"FROM vacation_day v " + |
|
197 |
"INNER JOIN end_user u ON v.user_id = u.id " + |
|
198 |
"WHERE v.status=?", |
|
199 |
new Object[]{status.name()}, new VacationRequestMapper()); |
|
200 |
} |
|
73 |
List<VacationRequestDTO> getAllVacationRequests(final Status status); |
|
201 | 74 |
|
202 | 75 |
/** |
203 | 76 |
* Updates a status of a vacation request with the given id. |
... | ... | |
205 | 78 |
* @param id the id of the request |
206 | 79 |
* @param status the new status of the request |
207 | 80 |
*/ |
208 |
public void updateVacationRequest(final long id, final Status status) { |
|
209 |
RequestRepository.log.trace("Updating status of a vacation request"); |
|
210 |
RequestRepository.log.debug("Id: {}, status: {}.", id, status); |
|
211 |
|
|
212 |
this.jdbc.update("UPDATE vacation_day SET status=? WHERE id=?", status.name(), id); |
|
213 |
} |
|
81 |
void updateVacationRequest(final long id, final Status status); |
|
214 | 82 |
|
215 | 83 |
/** |
216 | 84 |
* Updates a status of a vacation request from a BasicRequest object. |
217 | 85 |
* |
218 | 86 |
* @param request the BasicRequest object with new values of the vacation request |
219 | 87 |
*/ |
220 |
public void updateVacationRequest(final BasicRequestDTO request) { |
|
221 |
this.updateVacationRequest(request.getId(), request.getStatus()); |
|
222 |
} |
|
88 |
void updateVacationRequest(final BasicRequestDTO request); |
|
223 | 89 |
} |
server/src/main/java/org/danekja/ymanager/repository/UserRepository.java | ||
---|---|---|
3 | 3 |
import org.danekja.ymanager.domain.*; |
4 | 4 |
import org.danekja.ymanager.dto.BasicProfileUserDTO; |
5 | 5 |
import org.danekja.ymanager.dto.FullUserProfileDTO; |
6 |
import org.danekja.ymanager.repository.jdbc.mappers.UserDataRowMapper; |
|
7 |
import org.danekja.ymanager.repository.jdbc.mappers.UserRowMapper; |
|
8 |
import org.slf4j.Logger; |
|
9 |
import org.slf4j.LoggerFactory; |
|
10 |
import org.springframework.beans.factory.annotation.Autowired; |
|
11 |
import org.springframework.jdbc.core.*; |
|
12 |
import org.springframework.jdbc.support.GeneratedKeyHolder; |
|
13 |
import org.springframework.stereotype.Repository; |
|
14 | 6 |
|
15 |
import java.sql.*; |
|
16 |
import java.util.ArrayList; |
|
17 | 7 |
import java.util.List; |
18 |
import java.util.Map; |
|
19 |
import java.util.Objects; |
|
20 |
|
|
21 |
import static org.danekja.ymanager.domain.UserRole.getUserRole; |
|
22 | 8 |
|
23 | 9 |
/** |
24 | 10 |
* An instance of the class UserRepository handles queries which selects and updates users and their settings in a database. |
25 | 11 |
*/ |
26 |
@Repository |
|
27 |
public class UserRepository { |
|
28 |
|
|
29 |
private final RowMapper<UserData> USER_DATA_MAPPER = new UserDataRowMapper(); |
|
30 |
private final RowMapper<RegisteredUser> USER_MAPPER = new UserRowMapper(USER_DATA_MAPPER); |
|
31 |
|
|
32 |
|
|
33 |
/** |
|
34 |
* The mapper maps a row from a result of a query to an BasicProfileUser. |
|
35 |
*/ |
|
36 |
private class BasicProfileUserMapper implements RowMapper<BasicProfileUserDTO> { |
|
37 |
|
|
38 |
/** |
|
39 |
* Maps a row from a result of a query to an BasicProfileUser. |
|
40 |
* @param resultSet the row from the result |
|
41 |
* @param i the index of the row |
|
42 |
* @return the BasicProfileUser object |
|
43 |
* @throws SQLException if the columnLabel is not valid; if a database access error occurs or this method is called on a closed result set |
|
44 |
*/ |
|
45 |
@Override |
|
46 |
public BasicProfileUserDTO mapRow(ResultSet resultSet, int i) throws SQLException { |
|
47 |
final BasicProfileUserDTO user = new BasicProfileUserDTO(); |
|
48 |
user.setId(resultSet.getLong("id")); |
|
49 |
user.setFirstName(resultSet.getString("first_name")); |
|
50 |
user.setLastName(resultSet.getString("last_name")); |
|
51 |
user.setPhoto(resultSet.getString("photo")); |
|
52 |
return user; |
|
53 |
} |
|
54 |
} |
|
55 |
|
|
56 |
/** |
|
57 |
* The logger. |
|
58 |
*/ |
|
59 |
private static final Logger log = LoggerFactory.getLogger(UserRepository.class); |
|
60 |
|
|
61 |
/** |
|
62 |
* The connection to a database. |
|
63 |
*/ |
|
64 |
private final JdbcTemplate jdbc; |
|
65 |
|
|
66 |
/** |
|
67 |
* Creates a new instance of the class UserRepository which selects and updates users and their settings in a database. |
|
68 |
* |
|
69 |
* @param jdbc A connection to the database. |
|
70 |
*/ |
|
71 |
@Autowired |
|
72 |
public UserRepository(final JdbcTemplate jdbc) { |
|
73 |
UserRepository.log.trace("Creating a new instance of the class UserRepository"); |
|
74 |
this.jdbc = jdbc; |
|
75 |
} |
|
76 |
|
|
77 |
private Map<String, Object> getUserColumns(final long id) { |
|
78 |
final List<SqlParameter> paramList = new ArrayList<>(); |
|
79 |
paramList.add(new SqlParameter("in_id", Types.BIGINT)); |
|
80 |
paramList.add(new SqlOutParameter("out_id", Types.BIGINT)); |
|
81 |
paramList.add(new SqlOutParameter("out_first_name", Types.VARCHAR)); |
|
82 |
paramList.add(new SqlOutParameter("out_last_name", Types.VARCHAR)); |
|
83 |
paramList.add(new SqlOutParameter("out_no_vacations", Types.FLOAT)); |
|
84 |
paramList.add(new SqlOutParameter("out_no_sick_days", Types.INTEGER)); |
|
85 |
paramList.add(new SqlOutParameter("out_taken_sick_days", Types.INTEGER)); |
|
86 |
paramList.add(new SqlOutParameter("out_alert", Types.TIMESTAMP)); |
|
87 |
paramList.add(new SqlOutParameter("out_token", Types.LONGVARCHAR)); |
|
88 |
paramList.add(new SqlOutParameter("out_email", Types.VARCHAR)); |
|
89 |
paramList.add(new SqlOutParameter("out_photo", Types.LONGVARCHAR)); |
|
90 |
paramList.add(new SqlOutParameter("out_creation_date", Types.TIMESTAMP)); |
|
91 |
paramList.add(new SqlOutParameter("out_role", Types.VARCHAR)); |
|
92 |
paramList.add(new SqlOutParameter("out_status", Types.VARCHAR)); |
|
93 |
|
|
94 |
return jdbc.call(con -> { |
|
95 |
final CallableStatement callableStatement = con.prepareCall("{call GetUserId(?,?,?,?,?,?,?,?,?,?,?,?,?,?)}"); |
|
96 |
callableStatement.setLong(1, id); |
|
97 |
callableStatement.registerOutParameter(2, Types.BIGINT); |
|
98 |
callableStatement.registerOutParameter(3, Types.VARCHAR); |
|
99 |
callableStatement.registerOutParameter(4, Types.VARCHAR); |
|
100 |
callableStatement.registerOutParameter(5, Types.FLOAT); |
|
101 |
callableStatement.registerOutParameter(6, Types.INTEGER); |
|
102 |
callableStatement.registerOutParameter(7, Types.INTEGER); |
|
103 |
callableStatement.registerOutParameter(8, Types.TIMESTAMP); |
|
104 |
callableStatement.registerOutParameter(9, Types.LONGVARCHAR); |
|
105 |
callableStatement.registerOutParameter(10, Types.VARCHAR); |
|
106 |
callableStatement.registerOutParameter(11, Types.LONGVARCHAR); |
|
107 |
callableStatement.registerOutParameter(12, Types.TIMESTAMP); |
|
108 |
callableStatement.registerOutParameter(13, Types.VARCHAR); |
|
109 |
callableStatement.registerOutParameter(14, Types.VARCHAR); |
|
110 |
return callableStatement; |
|
111 |
}, paramList); |
|
112 |
} |
|
113 |
|
|
114 |
//---------------------------------- DTO ----------------------------------- |
|
115 |
|
|
12 |
public interface UserRepository { |
|
116 | 13 |
/** |
117 | 14 |
* Gets basic profile of each user from a database. The basic profile contains default, the most important |
118 | 15 |
* informations which helps identify a user like a name or photo. Every line of output is converted to a BasicProfileUser |
... | ... | |
120 | 17 |
* |
121 | 18 |
* @return A list of all basic profiles. |
122 | 19 |
*/ |
123 |
public List<BasicProfileUserDTO> getAllBasicUsers() { |
|
124 |
UserRepository.log.trace("Selecting basic profiles of all users from a database."); |
|
125 |
|
|
126 |
return this.jdbc.query("SELECT id, first_name, last_name, photo FROM end_user", new BasicProfileUserMapper()); |
|
127 |
} |
|
20 |
List<BasicProfileUserDTO> getAllBasicUsers(); |
|
128 | 21 |
|
129 | 22 |
/** |
130 | 23 |
* Gets basic profile of each user with the given authorization status from a database. The basic profile contains |
... | ... | |
135 | 28 |
* @param status The authentication status. |
136 | 29 |
* @return A list of all basic profiles with the given status. |
137 | 30 |
*/ |
138 |
public List<BasicProfileUserDTO> getAllBasicUsers(final Status status) { |
|
139 |
UserRepository.log.trace("Selecting basic profiles of all users with a required status from a database."); |
|
140 |
UserRepository.log.debug("Status: {}", status); |
|
31 |
List<BasicProfileUserDTO> getAllBasicUsers(final Status status); |
|
141 | 32 |
|
142 |
return this.jdbc.query("SELECT id, first_name, last_name, photo FROM end_user WHERE status = ?", |
|
143 |
new Object[]{status.name()}, new BasicProfileUserMapper()); |
|
144 |
} |
|
145 |
|
|
146 |
public UserRole getPermission(Long id) { |
|
147 |
return jdbc.queryForObject("SELECT user_role FROM end_user WHERE id = ?" ,new Object[]{id}, (rs, rowNum) -> |
|
148 |
getUserRole(rs.getString("user_role")) |
|
149 |
); |
|
150 |
} |
|
33 |
UserRole getPermission(Long id); |
|
151 | 34 |
|
152 | 35 |
/** |
153 | 36 |
* |
154 | 37 |
* @param id |
155 | 38 |
* @return |
156 | 39 |
*/ |
157 |
public FullUserProfileDTO getFullUser(final long id) { |
|
158 |
UserRepository.log.debug("Selecting full profile of a user with the specified id from a database: {}", id); |
|
159 |
|
|
160 |
final Map<String, Object> resultMap = this.getUserColumns(id); |
|
161 |
|
|
162 |
final FullUserProfileDTO user = new FullUserProfileDTO(); |
|
163 |
user.setId(id); |
|
164 |
user.setFirstName((String) resultMap.get("out_first_name")); |
|
165 |
user.setLastName((String) resultMap.get("out_last_name")); |
|
166 |
user.setVacationCount(((Double) resultMap.get("out_no_vacations")).floatValue()); |
|
167 |
user.setSickDayCount((Integer) resultMap.get("out_no_sick_days")); |
|
168 |
user.setTakenSickDayCount((Integer) resultMap.get("out_taken_sick_days")); |
|
169 |
user.setNotification(((Timestamp) resultMap.get("out_alert")).toLocalDateTime()); |
|
170 |
user.setEmail((String) resultMap.get("out_email")); |
|
171 |
user.setPhoto((String) resultMap.get("out_photo")); |
|
172 |
user.setRole(getUserRole((String) resultMap.get("out_role"))); |
|
173 |
user.setStatus(Status.valueOf((String) resultMap.get("out_status"))); |
|
174 |
return user; |
|
175 |
|
|
176 |
} |
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
//---------------------------------- DOMAIN ----------------------------------- |
|
40 |
FullUserProfileDTO getFullUser(final long id); |
|
181 | 41 |
|
182 | 42 |
/** |
183 | 43 |
* Gets user from database based on its email |
... | ... | |
185 | 45 |
* @param email email value, used as search key |
186 | 46 |
* @return found user object or null (if not found) |
187 | 47 |
*/ |
188 |
public RegisteredUser getUser(final String email) { |
|
189 |
return this.jdbc.queryForObject("SELECT * FROM end_user WHERE email = ?", USER_MAPPER, email); |
|
190 |
} |
|
48 |
RegisteredUser getUser(final String email); |
|
191 | 49 |
|
192 | 50 |
/** |
193 | 51 |
* Gets user from database based on its id |
... | ... | |
195 | 53 |
* @param id id value, used as search key |
196 | 54 |
* @return found user object or null (if not found) |
197 | 55 |
*/ |
198 |
public RegisteredUser getUser(final long id) { |
|
199 |
return this.jdbc.queryForObject("SELECT * FROM end_user WHERE id = ?", USER_MAPPER, id); |
|
200 |
} |
|
56 |
RegisteredUser getUser(final long id); |
|
201 | 57 |
|
202 | 58 |
/** |
203 | 59 |
* Gets user data by user's id |
... | ... | |
205 | 61 |
* @param id TODO replace by subject Id for google and numeric id for registered (multiple queries) |
206 | 62 |
* @return |
207 | 63 |
*/ |
208 |
public UserData getUserData(final long id) { |
|
209 |
return this.jdbc.queryForObject("SELECT * FROM end_user WHERE id = ?", USER_DATA_MAPPER, id); |
|
210 |
} |
|
211 |
|
|
212 |
public void updateUser(final User user) { |
|
213 |
this.jdbc.update("UPDATE end_user SET first_name = ?, last_name = ?, no_vacations = ?, taken_sick_days = ?, email = ?, photo = ?, user_role = ?, status = ? WHERE id = ?", |
|
214 |
user.getFirstName(), user.getLastName(), user.getVacationCount(), user.getTakenSickDayCount(), user.getEmail(), user.getPhoto(), user.getRole().name(), user.getStatus().name(), user.getId()); |
|
215 |
} |
|
216 |
|
|
217 |
public long insertUser(final User user) { |
|
218 |
PreparedStatementCreatorFactory factory = new PreparedStatementCreatorFactory("INSERT INTO end_user (first_name, last_name, no_vacations, no_sick_days, taken_sick_days, alert, email, photo, user_role, status) VALUES (?,?,?,?,?,?,?,?,?,?)", |
|
219 |
Types.VARCHAR, |
|
220 |
Types.VARCHAR, |
|
221 |
Types.FLOAT, |
|
222 |
Types.INTEGER, |
|
223 |
Types.INTEGER, |
|
224 |
Types.TIMESTAMP, |
|
225 |
Types.VARCHAR, |
|
226 |
Types.VARCHAR, |
|
227 |
Types.VARCHAR, |
|
228 |
Types.VARCHAR); |
|
229 |
|
|
230 |
|
|
231 |
factory.setReturnGeneratedKeys(true); |
|
232 |
|
|
233 |
PreparedStatementCreator stmt = factory.newPreparedStatementCreator(new Object[]{user.getFirstName(), user.getLastName(), user.getVacationCount(), user.getTotalSickDayCount(), user.getTakenSickDayCount(), user.getNotification(), user.getEmail(), user.getPhoto(), user.getRole().name(), user.getStatus().name()}); |
|
64 |
UserData getUserData(final long id); |
|
234 | 65 |
|
235 |
GeneratedKeyHolder key = new GeneratedKeyHolder(); |
|
236 |
this.jdbc.update(stmt, key); |
|
66 |
void updateUser(final User user); |
|
237 | 67 |
|
238 |
return Objects.requireNonNull(key.getKey()).longValue(); |
|
239 |
} |
|
68 |
long insertUser(final User user); |
|
240 | 69 |
|
241 |
public void insertSettings(final DefaultSettings settings) { |
|
242 |
this.jdbc.update("INSERT INTO default_settings (no_sick_days, alert) VALUES (?, ?)", settings.getSickDayCount(), settings.getNotification()); |
|
243 |
} |
|
70 |
void insertSettings(final DefaultSettings settings); |
|
244 | 71 |
|
245 |
public void updateUserSettings(User user) { |
|
246 |
jdbc.update("UPDATE end_user SET alert = ?, user_role = ?, no_sick_days = ?, no_vacations = ? WHERE id = ?", |
|
247 |
user.getNotification(), user.getRole().name(), user.getTotalSickDayCount(), user.getVacationCount(), user.getId()); |
|
248 |
} |
|
72 |
void updateUserSettings(User user); |
|
249 | 73 |
|
250 |
public void updateUserTakenVacation(User user) { |
|
251 |
jdbc.update("UPDATE end_user SET no_vacations = ? WHERE id = ?", |
|
252 |
user.getVacationCount(), user.getId()); |
|
253 |
} |
|
74 |
void updateUserTakenVacation(User user); |
|
254 | 75 |
|
255 |
public void updateUserTakenSickDay(User user) { |
|
256 |
jdbc.update("UPDATE end_user SET taken_sick_days = ? WHERE id = ?", |
|
257 |
user.getTakenSickDayCount(), user.getId()); |
|
258 |
} |
|
76 |
void updateUserTakenSickDay(User user); |
|
259 | 77 |
} |
server/src/main/java/org/danekja/ymanager/repository/VacationRepository.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import org.danekja.ymanager.domain.Status; |
4 | 4 |
import org.danekja.ymanager.domain.Vacation; |
5 |
import org.danekja.ymanager.domain.VacationType; |
|
6 | 5 |
import org.danekja.ymanager.dto.VacationDayDTO; |
7 |
import org.slf4j.Logger; |
|
8 |
import org.slf4j.LoggerFactory; |
|
9 |
import org.springframework.beans.factory.annotation.Autowired; |
|
10 |
import org.springframework.jdbc.core.JdbcTemplate; |
|
11 |
import org.springframework.jdbc.core.RowMapper; |
|
12 |
import org.springframework.stereotype.Repository; |
|
13 | 6 |
|
14 |
import java.sql.ResultSet; |
|
15 |
import java.sql.SQLException; |
|
16 |
import java.sql.Time; |
|
17 | 7 |
import java.time.LocalDate; |
18 | 8 |
import java.util.List; |
19 | 9 |
import java.util.Optional; |
20 | 10 |
|
21 |
import static java.util.Optional.ofNullable;
|
|
11 |
public interface VacationRepository {
|
|
22 | 12 |
|
23 |
@Repository |
|
24 |
public class VacationRepository { |
|
25 |
/** |
|
26 |
* The mapper maps a row from a result of a query to an Vacation. |
|
27 |
*/ |
|
28 |
private class VacationDayMapper implements RowMapper<VacationDayDTO> { |
|
13 |
List<VacationDayDTO> getVacationDays(final long userId, final LocalDate from); |
|
29 | 14 |
|
30 |
/** |
|
31 |
* Maps a row from a result of a query to an Vacation. |
|
32 |
* @param resultSet the row from the result |
|
33 |
* @param i the index of the row |
|
34 |
* @return the Vacation 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 VacationDayDTO mapRow(ResultSet resultSet, int i) throws SQLException { |
|
39 |
final VacationDayDTO item = new VacationDayDTO(); |
|
40 |
item.setId(resultSet.getLong("v.id")); |
|
41 |
item.setDate(resultSet.getDate("v.vacation_date").toLocalDate()); |
|
15 |
List<VacationDayDTO> getVacationDays(final long userId, final LocalDate from, final Status status); |
|
42 | 16 |
|
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 |
} |
|
17 |
List<VacationDayDTO> getVacationDays(final long userId, final LocalDate from, final LocalDate to); |
|
52 | 18 |
|
53 |
final Time timeTo = resultSet.getTime("v.time_to"); |
|
54 |
if (timeTo != null) { |
|
55 |
item.setTo(timeTo.toLocalTime()); |
|
56 |
} |
|
19 |
List<VacationDayDTO> getVacationDays(final long userId, final LocalDate from, final LocalDate to, final Status status); |
|
57 | 20 |
|
58 |
item.setStatus(Status.valueOf(resultSet.getString("v.status"))); |
|
59 |
item.setType(VacationType.getVacationType(resultSet.getString("v.vacation_type"))); |
|
60 |
return item; |
|
61 |
} |
|
62 |
} |
|
21 |
Optional<Vacation> getVacationDay(final long id); |
|
63 | 22 |
|
64 |
/** |
|
65 |
* The logger. |
|
66 |
*/ |
|
67 |
private static final Logger log = LoggerFactory.getLogger(VacationRepository.class); |
|
23 |
void insertVacationDay(final Long userId, final Vacation day); |
|
68 | 24 |
|
69 |
/** |
|
70 |
* The connection to a database. |
|
71 |
*/ |
|
72 |
private final JdbcTemplate jdbc; |
|
25 |
void updateVacationDay(final Vacation item); |
|
73 | 26 |
|
74 |
/** |
|
75 |
* Creates a new instance of the class VacationRepository which selects and updates users in a database. |
|
76 |
* |
|
77 |
* @param jdbc A connection to the database. |
|
78 |
*/ |
|
79 |
@Autowired |
|
80 |
public VacationRepository(final JdbcTemplate jdbc) { |
|
81 |
VacationRepository.log.trace("Creating a new instance of the class VacationRepository"); |
|
27 |
void deleteVacationDay(final long id); |
|
82 | 28 |
|
83 |
this.jdbc = jdbc; |
|
84 |
} |
|
85 |
|
|
86 |
public List<VacationDayDTO> getVacationDays(final long userId, final LocalDate from) { |
|
87 |
return jdbc.query("SELECT v.id, v.vacation_date, v.time_from, v.time_to, v.status, v.vacation_type " + |
|
88 |
"FROM vacation_day v " + |
|
89 |
"INNER JOIN end_user u ON v.user_id = u.id " + |
|
90 |
"WHERE v.user_id = ? AND v.vacation_date >= ?", |
|
91 |
new Object[]{userId, from}, new VacationDayMapper()); |
|
92 |
} |
|
93 |
|
|
94 |
public List<VacationDayDTO> getVacationDays(final long userId, final LocalDate from, final Status status) { |
|
95 |
return jdbc.query("SELECT v.id, v.vacation_date, v.time_from, v.time_to, v.status, v.vacation_type " + |
|
96 |
"FROM vacation_day v " + |
|
97 |
"INNER JOIN end_user u ON v.user_id = u.id " + |
|
98 |
"WHERE v.user_id = ? AND v.vacation_date >= ? AND v.status = ?", |
|
99 |
new Object[]{userId, from, status.name()}, new VacationDayMapper()); |
|
100 |
} |
|
101 |
|
|
102 |
public List<VacationDayDTO> getVacationDays(final long userId, final LocalDate from, final LocalDate to) { |
|
103 |
return jdbc.query("SELECT v.id, v.vacation_date, v.time_from, v.time_to, v.status, v.vacation_type " + |
|
104 |
"FROM vacation_day v " + |
|
105 |
"INNER JOIN end_user u ON v.user_id = u.id " + |
|
106 |
"WHERE v.user_id=? AND v.vacation_date >= ? AND v.vacation_date <= ?", |
|
107 |
new Object[]{userId, from, to}, new VacationDayMapper()); |
|
108 |
|
|
109 |
} |
|
110 |
|
|
111 |
public List<VacationDayDTO> getVacationDays(final long userId, final LocalDate from, final LocalDate to, final Status status) { |
|
112 |
return jdbc.query("SELECT v.id, v.vacation_date, v.time_from, v.time_to, v.status, v.vacation_type " + |
|
113 |
"FROM vacation_day v " + |
|
114 |
"INNER JOIN end_user u ON v.user_id = u.id " + |
|
115 |
"WHERE v.user_id=? AND v.vacation_date >= ? AND v.vacation_date <= ? AND v.status = ?", |
|
116 |
new Object[]{userId, from, to, status.name()}, new VacationDayMapper()); |
|
117 |
} |
|
118 |
|
|
119 |
public Optional<Vacation> getVacationDay(final long id) { |
|
120 |
return ofNullable(jdbc.queryForObject("SELECT id, vacation_date, time_from, time_to, creation_date, status, vacation_type, user_id " + |
|
121 |
"FROM vacation_day WHERE id = ?", new Object[]{id}, |
|
122 |
(ResultSet rs, int rowNum) -> { |
|
123 |
Vacation vacation = new Vacation(); |
|
124 |
vacation.setId(rs.getLong("id")); |
|
125 |
vacation.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"); |
|
132 |
if (timeFrom != null) { |
|
133 |
vacation.setFrom(timeFrom.toLocalTime()); |
|
134 |
} |
|
135 |
|
|
136 |
final Time timeTo = rs.getTime("time_to"); |
|
137 |
if (timeTo != null) { |
|
138 |
vacation.setTo(timeTo.toLocalTime()); |
|
139 |
} |
|
140 |
|
|
141 |
vacation.setCreationDate(rs.getTimestamp("creation_date").toLocalDateTime()); |
|
142 |
vacation.setStatus(Status.valueOf(rs.getString("status"))); |
|
143 |
vacation.setType(VacationType.getVacationType(rs.getString("vacation_type"))); |
|
144 |
vacation.setUserId(rs.getLong("user_id")); |
|
145 |
return vacation; |
|
146 |
})); |
|
147 |
} |
|
148 |
|
|
149 |
public void insertVacationDay(final Long userId, final Vacation day) { |
|
150 |
jdbc.update("INSERT INTO vacation_day (vacation_date, time_from, time_to, status, vacation_type, user_id) VALUES (?,?,?,?,?,?)", |
|
151 |
day.getDate(), day.getFrom(), day.getTo(), day.getStatus().name(), day.getType().name(), userId); |
|
152 |
} |
|
153 |
|
|
154 |
public void updateVacationDay(final Vacation item) { |
|
155 |
jdbc.update("UPDATE vacation_day SET vacation_date=?, time_from=?, time_to=?, status=?, vacation_type=? WHERE id=?", |
|
156 |
item.getDate(), item.getFrom(), item.getTo(), item.getStatus().name(), item.getType().name(), item.getId()); |
|
157 |
} |
|
158 |
|
|
159 |
public void deleteVacationDay(final long id) { |
|
160 |
jdbc.update("DELETE FROM vacation_day WHERE id=?", id); |
|
161 |
} |
|
162 |
|
|
163 |
|
|
164 |
public boolean isExistVacationForUser(Long userId, LocalDate date) { |
|
165 |
return jdbc.queryForObject("SELECT count(id) AS exist FROM vacation_day WHERE vacation_date = ? AND user_id = ?", |
|
166 |
new Object[]{date, userId}, (rs, rowNum) -> rs.getBoolean("exist")); |
|
167 |
} |
|
29 |
boolean isExistVacationForUser(Long userId, LocalDate date); |
|
168 | 30 |
} |
server/src/main/java/org/danekja/ymanager/repository/jdbc/JdbcRequestRepository.java | ||
---|---|---|
1 |
package org.danekja.ymanager.repository.jdbc; |
|
2 |
|
|
3 |
import org.danekja.ymanager.domain.Status; |
|
4 |
import org.danekja.ymanager.dto.AuthorizationRequestDTO; |
|
5 |
import org.danekja.ymanager.dto.BasicRequestDTO; |
|
6 |
import org.danekja.ymanager.dto.VacationRequestDTO; |
|
7 |
import org.danekja.ymanager.repository.RequestRepository; |
|
8 |
import org.danekja.ymanager.repository.jdbc.mappers.AuthorizationRequestMapper; |
|
9 |
import org.danekja.ymanager.repository.jdbc.mappers.VacationRequestMapper; |
|
10 |
import org.slf4j.Logger; |
|
11 |
import org.slf4j.LoggerFactory; |
|
12 |
import org.springframework.beans.factory.annotation.Autowired; |
|
13 |
import org.springframework.jdbc.core.JdbcTemplate; |
|
14 |
import org.springframework.stereotype.Repository; |
|
15 |
|
|
16 |
import java.util.List; |
|
17 |
|
|
18 |
@Repository |
|
19 |
public class JdbcRequestRepository implements RequestRepository { |
|
20 |
|
|
21 |
/** |
|
22 |
* The logger. |
|
23 |
*/ |
|
24 |
private static final Logger log = LoggerFactory.getLogger(JdbcRequestRepository.class); |
|
25 |
|
|
26 |
private static final VacationRequestMapper VACATION_REQUEST_MAPPER = new VacationRequestMapper(); |
|
27 |
private static final AuthorizationRequestMapper AUTHORIZATION_REQUEST_MAPPER = new AuthorizationRequestMapper(); |
|
28 |
|
|
29 |
/** |
|
30 |
* The connection to a database. |
|
31 |
*/ |
|
32 |
private final JdbcTemplate jdbc; |
|
33 |
|
|
34 |
/** |
|
35 |
* Creates a manager which can select and update requests in a database with the specified connection. |
|
36 |
* |
|
37 |
* @param jdbc the connection to the database |
|
38 |
*/ |
|
39 |
@Autowired |
|
40 |
public JdbcRequestRepository(final JdbcTemplate jdbc) { |
|
41 |
JdbcRequestRepository.log.trace("Creating a new instance of the class RequestRepository"); |
|
42 |
|
|
43 |
this.jdbc = jdbc; |
|
44 |
} |
|
45 |
|
|
46 |
@Override |
|
47 |
public List<AuthorizationRequestDTO> getAllAuthorizations() { |
|
48 |
JdbcRequestRepository.log.trace("Selecting all authorization requests from a database."); |
|
49 |
|
|
50 |
return this.jdbc.query("SELECT id, first_name, last_name, creation_date, status FROM end_user", AUTHORIZATION_REQUEST_MAPPER); |
|
51 |
} |
|
52 |
|
|
53 |
@Override |
|
54 |
public List<AuthorizationRequestDTO> getAllAuthorizations(final Status status) { |
|
55 |
JdbcRequestRepository.log.trace("Selecting all authorization requests from a database with requested status."); |
|
56 |
JdbcRequestRepository.log.debug("Status: {}", status); |
|
57 |
|
|
58 |
return this.jdbc.query("SELECT id, first_name, last_name, creation_date, status FROM end_user WHERE status = ?", AUTHORIZATION_REQUEST_MAPPER, status.name()); |
|
59 |
} |
|
60 |
|
|
61 |
@Override |
|
62 |
public void updateAuthorization(final long id, final Status status) { |
|
63 |
JdbcRequestRepository.log.trace("Updating status of an authorization request."); |
|
64 |
JdbcRequestRepository.log.debug("Id: {}, status: {}.", id, status); |
|
65 |
|
|
66 |
this.jdbc.update("UPDATE end_user SET status=? WHERE id=?", status.name(), id); |
|
67 |
} |
|
68 |
|
|
69 |
@Override |
|
70 |
public void updateAuthorization(final BasicRequestDTO request) { |
|
71 |
this.updateAuthorization(request.getId(), request.getStatus()); |
|
72 |
} |
|
73 |
|
|
74 |
@Override |
|
75 |
public List<VacationRequestDTO> getAllVacationRequests() { |
|
76 |
JdbcRequestRepository.log.trace("Selecting all vacation requests from a database."); |
|
77 |
|
|
78 |
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 " + |
|
79 |
"FROM vacation_day v " + |
|
80 |
"INNER JOIN end_user u ON v.user_id = u.id", VACATION_REQUEST_MAPPER); |
|
81 |
} |
|
82 |
|
|
83 |
@Override |
|
84 |
public List<VacationRequestDTO> getAllVacationRequests(final Status status) { |
|
85 |
JdbcRequestRepository.log.trace("Selecting all vacation requests from a database with requested status."); |
|
86 |
JdbcRequestRepository.log.debug("Status: {}", status); |
|
87 |
|
|
88 |
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 " + |
|
89 |
"FROM vacation_day v " + |
|
90 |
"INNER JOIN end_user u ON v.user_id = u.id " + |
|
91 |
"WHERE v.status=?", VACATION_REQUEST_MAPPER, status.name()); |
|
92 |
} |
|
93 |
|
|
94 |
@Override |
|
95 |
public void updateVacationRequest(final long id, final Status status) { |
|
96 |
JdbcRequestRepository.log.trace("Updating status of a vacation request"); |
|
97 |
JdbcRequestRepository.log.debug("Id: {}, status: {}.", id, status); |
|
98 |
|
|
99 |
this.jdbc.update("UPDATE vacation_day SET status=? WHERE id=?", status.name(), id); |
|
100 |
} |
|
101 |
|
|
102 |
@Override |
|
103 |
public void updateVacationRequest(final BasicRequestDTO request) { |
|
104 |
this.updateVacationRequest(request.getId(), request.getStatus()); |
|
105 |
} |
|
106 |
} |
server/src/main/java/org/danekja/ymanager/repository/jdbc/JdbcUserRepository.java | ||
---|---|---|
1 |
package org.danekja.ymanager.repository.jdbc; |
|
2 |
|
|
3 |
import org.danekja.ymanager.domain.*; |
|
4 |
import org.danekja.ymanager.dto.BasicProfileUserDTO; |
|
5 |
import org.danekja.ymanager.dto.FullUserProfileDTO; |
|
6 |
import org.danekja.ymanager.repository.UserRepository; |
|
7 |
import org.danekja.ymanager.repository.jdbc.mappers.BasicProfileUserMapper; |
|
8 |
import org.danekja.ymanager.repository.jdbc.mappers.UserDataRowMapper; |
|
9 |
import org.danekja.ymanager.repository.jdbc.mappers.UserRowMapper; |
|
10 |
import org.slf4j.Logger; |
|
11 |
import org.slf4j.LoggerFactory; |
|
12 |
import org.springframework.beans.factory.annotation.Autowired; |
|
13 |
import org.springframework.jdbc.core.*; |
|
14 |
import org.springframework.jdbc.support.GeneratedKeyHolder; |
|
15 |
import org.springframework.stereotype.Repository; |
|
16 |
|
|
17 |
import java.sql.CallableStatement; |
|
18 |
import java.sql.Timestamp; |
|
19 |
import java.sql.Types; |
|
20 |
import java.util.ArrayList; |
|
21 |
import java.util.List; |
|
22 |
import java.util.Map; |
|
23 |
import java.util.Objects; |
|
24 |
|
|
25 |
import static org.danekja.ymanager.domain.UserRole.getUserRole; |
|
26 |
|
|
27 |
@Repository |
|
28 |
public class JdbcUserRepository implements UserRepository { |
|
29 |
|
|
30 |
private static final RowMapper<BasicProfileUserDTO> BASIC_PROFILE_USER_MAPPER = new BasicProfileUserMapper(); |
|
31 |
private static final RowMapper<UserData> USER_DATA_MAPPER = new UserDataRowMapper(); |
|
32 |
private static final RowMapper<RegisteredUser> USER_MAPPER = new UserRowMapper(USER_DATA_MAPPER); |
|
33 |
|
|
34 |
/** |
|
35 |
* The logger. |
|
36 |
*/ |
|
37 |
private static final Logger log = LoggerFactory.getLogger(JdbcUserRepository.class); |
|
38 |
|
|
39 |
/** |
|
40 |
* The connection to a database. |
|
41 |
*/ |
|
42 |
private final JdbcTemplate jdbc; |
|
43 |
|
|
44 |
/** |
|
45 |
* Creates a new instance of the class UserRepository which selects and updates users and their settings in a database. |
|
46 |
* |
|
47 |
* @param jdbc A connection to the database. |
|
48 |
*/ |
|
49 |
@Autowired |
|
50 |
public JdbcUserRepository(final JdbcTemplate jdbc) { |
|
51 |
JdbcUserRepository.log.trace("Creating a new instance of the class UserRepository"); |
|
52 |
this.jdbc = jdbc; |
|
53 |
} |
|
54 |
|
|
55 |
private Map<String, Object> getUserColumns(final long id) { |
|
56 |
final List<SqlParameter> paramList = new ArrayList<>(); |
|
57 |
paramList.add(new SqlParameter("in_id", Types.BIGINT)); |
|
58 |
paramList.add(new SqlOutParameter("out_id", Types.BIGINT)); |
|
59 |
paramList.add(new SqlOutParameter("out_first_name", Types.VARCHAR)); |
|
60 |
paramList.add(new SqlOutParameter("out_last_name", Types.VARCHAR)); |
|
61 |
paramList.add(new SqlOutParameter("out_no_vacations", Types.FLOAT)); |
|
62 |
paramList.add(new SqlOutParameter("out_no_sick_days", Types.INTEGER)); |
|
63 |
paramList.add(new SqlOutParameter("out_taken_sick_days", Types.INTEGER)); |
|
64 |
paramList.add(new SqlOutParameter("out_alert", Types.TIMESTAMP)); |
|
65 |
paramList.add(new SqlOutParameter("out_token", Types.LONGVARCHAR)); |
|
66 |
paramList.add(new SqlOutParameter("out_email", Types.VARCHAR)); |
|
67 |
paramList.add(new SqlOutParameter("out_photo", Types.LONGVARCHAR)); |
|
68 |
paramList.add(new SqlOutParameter("out_creation_date", Types.TIMESTAMP)); |
|
69 |
paramList.add(new SqlOutParameter("out_role", Types.VARCHAR)); |
|
70 |
paramList.add(new SqlOutParameter("out_status", Types.VARCHAR)); |
|
71 |
|
|
72 |
return jdbc.call(con -> { |
|
73 |
final CallableStatement callableStatement = con.prepareCall("{call GetUserId(?,?,?,?,?,?,?,?,?,?,?,?,?,?)}"); |
|
74 |
callableStatement.setLong(1, id); |
|
75 |
callableStatement.registerOutParameter(2, Types.BIGINT); |
|
76 |
callableStatement.registerOutParameter(3, Types.VARCHAR); |
|
77 |
callableStatement.registerOutParameter(4, Types.VARCHAR); |
|
78 |
callableStatement.registerOutParameter(5, Types.FLOAT); |
|
79 |
callableStatement.registerOutParameter(6, Types.INTEGER); |
|
80 |
callableStatement.registerOutParameter(7, Types.INTEGER); |
|
81 |
callableStatement.registerOutParameter(8, Types.TIMESTAMP); |
|
82 |
callableStatement.registerOutParameter(9, Types.LONGVARCHAR); |
|
83 |
callableStatement.registerOutParameter(10, Types.VARCHAR); |
|
84 |
callableStatement.registerOutParameter(11, Types.LONGVARCHAR); |
|
85 |
callableStatement.registerOutParameter(12, Types.TIMESTAMP); |
|
86 |
callableStatement.registerOutParameter(13, Types.VARCHAR); |
|
87 |
callableStatement.registerOutParameter(14, Types.VARCHAR); |
|
88 |
return callableStatement; |
|
89 |
}, paramList); |
|
90 |
} |
|
91 |
|
|
92 |
//---------------------------------- DTO ----------------------------------- |
|
93 |
|
|
94 |
@Override |
|
95 |
public List<BasicProfileUserDTO> getAllBasicUsers() { |
|
96 |
JdbcUserRepository.log.trace("Selecting basic profiles of all users from a database."); |
|
97 |
|
|
98 |
return this.jdbc.query("SELECT id, first_name, last_name, photo FROM end_user", BASIC_PROFILE_USER_MAPPER); |
|
99 |
} |
|
100 |
|
|
101 |
@Override |
|
102 |
public List<BasicProfileUserDTO> getAllBasicUsers(final Status status) { |
|
103 |
JdbcUserRepository.log.trace("Selecting basic profiles of all users with a required status from a database."); |
|
104 |
JdbcUserRepository.log.debug("Status: {}", status); |
|
105 |
|
|
106 |
return this.jdbc.query("SELECT id, first_name, last_name, photo FROM end_user WHERE status = ?", |
|
107 |
BASIC_PROFILE_USER_MAPPER, status.name()); |
|
108 |
} |
|
109 |
|
|
110 |
@Override |
|
111 |
public UserRole getPermission(Long id) { |
|
112 |
return jdbc.queryForObject("SELECT user_role FROM end_user WHERE id = ?" ,new Object[]{id}, (rs, rowNum) -> |
|
113 |
getUserRole(rs.getString("user_role")) |
|
114 |
); |
|
115 |
} |
|
116 |
|
|
117 |
@Override |
|
118 |
public FullUserProfileDTO getFullUser(final long id) { |
|
119 |
JdbcUserRepository.log.debug("Selecting full profile of a user with the specified id from a database: {}", id); |
|
120 |
|
|
121 |
final Map<String, Object> resultMap = this.getUserColumns(id); |
|
122 |
|
|
123 |
final FullUserProfileDTO user = new FullUserProfileDTO(); |
|
124 |
user.setId(id); |
|
125 |
user.setFirstName((String) resultMap.get("out_first_name")); |
|
126 |
user.setLastName((String) resultMap.get("out_last_name")); |
|
127 |
user.setVacationCount(((Double) resultMap.get("out_no_vacations")).floatValue()); |
|
128 |
user.setSickDayCount((Integer) resultMap.get("out_no_sick_days")); |
|
129 |
user.setTakenSickDayCount((Integer) resultMap.get("out_taken_sick_days")); |
|
130 |
user.setNotification(((Timestamp) resultMap.get("out_alert")).toLocalDateTime()); |
|
131 |
user.setEmail((String) resultMap.get("out_email")); |
|
132 |
user.setPhoto((String) resultMap.get("out_photo")); |
|
133 |
user.setRole(getUserRole((String) resultMap.get("out_role"))); |
|
134 |
user.setStatus(Status.valueOf((String) resultMap.get("out_status"))); |
|
135 |
return user; |
|
136 |
|
|
137 |
} |
|
138 |
|
|
139 |
//---------------------------------- DOMAIN ----------------------------------- |
|
140 |
|
|
141 |
@Override |
|
142 |
public RegisteredUser getUser(final String email) { |
|
143 |
return this.jdbc.queryForObject("SELECT * FROM end_user WHERE email = ?", USER_MAPPER, email); |
|
144 |
} |
|
145 |
|
|
146 |
@Override |
|
147 |
public RegisteredUser getUser(final long id) { |
|
148 |
return this.jdbc.queryForObject("SELECT * FROM end_user WHERE id = ?", USER_MAPPER, id); |
|
149 |
} |
|
150 |
|
|
151 |
@Override |
|
152 |
public UserData getUserData(final long id) { |
|
153 |
return this.jdbc.queryForObject("SELECT * FROM end_user WHERE id = ?", USER_DATA_MAPPER, id); |
|
154 |
} |
|
155 |
|
|
156 |
@Override |
|
157 |
public void updateUser(final User user) { |
|
158 |
this.jdbc.update("UPDATE end_user SET first_name = ?, last_name = ?, no_vacations = ?, taken_sick_days = ?, email = ?, photo = ?, user_role = ?, status = ? WHERE id = ?", |
|
159 |
user.getFirstName(), user.getLastName(), user.getVacationCount(), user.getTakenSickDayCount(), user.getEmail(), user.getPhoto(), user.getRole().name(), user.getStatus().name(), user.getId()); |
|
160 |
} |
|
161 |
|
|
162 |
@Override |
|
163 |
public long insertUser(final User user) { |
|
164 |
PreparedStatementCreatorFactory factory = new PreparedStatementCreatorFactory("INSERT INTO end_user (first_name, last_name, no_vacations, no_sick_days, taken_sick_days, alert, email, photo, user_role, status) VALUES (?,?,?,?,?,?,?,?,?,?)", |
|
165 |
Types.VARCHAR, |
|
166 |
Types.VARCHAR, |
|
167 |
Types.FLOAT, |
|
168 |
Types.INTEGER, |
|
169 |
Types.INTEGER, |
|
170 |
Types.TIMESTAMP, |
|
171 |
Types.VARCHAR, |
|
172 |
Types.VARCHAR, |
|
173 |
Types.VARCHAR, |
|
174 |
Types.VARCHAR); |
|
175 |
|
|
176 |
|
|
177 |
factory.setReturnGeneratedKeys(true); |
|
178 |
|
|
179 |
PreparedStatementCreator stmt = factory.newPreparedStatementCreator(new Object[]{user.getFirstName(), user.getLastName(), user.getVacationCount(), user.getTotalSickDayCount(), user.getTakenSickDayCount(), user.getNotification(), user.getEmail(), user.getPhoto(), user.getRole().name(), user.getStatus().name()}); |
|
180 |
|
|
181 |
GeneratedKeyHolder key = new GeneratedKeyHolder(); |
|
182 |
this.jdbc.update(stmt, key); |
|
183 |
|
|
184 |
return Objects.requireNonNull(key.getKey()).longValue(); |
|
185 |
} |
|
186 |
|
|
187 |
@Override |
|
188 |
public void insertSettings(final DefaultSettings settings) { |
|
189 |
this.jdbc.update("INSERT INTO default_settings (no_sick_days, alert) VALUES (?, ?)", settings.getSickDayCount(), settings.getNotification()); |
|
190 |
} |
|
191 |
|
|
192 |
@Override |
|
193 |
public void updateUserSettings(User user) { |
|
194 |
jdbc.update("UPDATE end_user SET alert = ?, user_role = ?, no_sick_days = ?, no_vacations = ? WHERE id = ?", |
|
195 |
user.getNotification(), user.getRole().name(), user.getTotalSickDayCount(), user.getVacationCount(), user.getId()); |
|
196 |
} |
|
197 |
|
|
198 |
@Override |
|
199 |
public void updateUserTakenVacation(User user) { |
|
200 |
jdbc.update("UPDATE end_user SET no_vacations = ? WHERE id = ?", |
|
201 |
user.getVacationCount(), user.getId()); |
|
202 |
} |
|
203 |
|
|
204 |
@Override |
|
205 |
public void updateUserTakenSickDay(User user) { |
|
206 |
jdbc.update("UPDATE end_user SET taken_sick_days = ? WHERE id = ?", |
|
207 |
user.getTakenSickDayCount(), user.getId()); |
|
208 |
} |
|
209 |
} |
server/src/main/java/org/danekja/ymanager/repository/jdbc/JdbcVacationRepository.java | ||
---|---|---|
1 |
package org.danekja.ymanager.repository.jdbc; |
|
2 |
|
|
3 |
import org.danekja.ymanager.domain.Status; |
|
4 |
import org.danekja.ymanager.domain.Vacation; |
|
5 |
import org.danekja.ymanager.domain.VacationType; |
|
6 |
import org.danekja.ymanager.dto.VacationDayDTO; |
|
7 |
import org.danekja.ymanager.repository.VacationRepository; |
|
8 |
import org.danekja.ymanager.repository.jdbc.mappers.VacationDayMapper; |
|
9 |
import org.danekja.ymanager.repository.jdbc.mappers.VacationMapper; |
|
10 |
import org.slf4j.Logger; |
|
11 |
import org.slf4j.LoggerFactory; |
|
12 |
import org.springframework.beans.factory.annotation.Autowired; |
|
13 |
import org.springframework.jdbc.core.JdbcTemplate; |
|
14 |
import org.springframework.jdbc.core.RowMapper; |
|
15 |
import org.springframework.stereotype.Repository; |
|
16 |
|
|
17 |
import java.sql.ResultSet; |
|
18 |
import java.sql.Time; |
|
19 |
import java.time.LocalDate; |
|
20 |
import java.util.List; |
|
21 |
import java.util.Optional; |
|
22 |
|
|
23 |
import static java.util.Optional.ofNullable; |
|
24 |
|
|
25 |
@Repository |
|
26 |
public class JdbcVacationRepository implements VacationRepository { |
|
27 |
|
|
28 |
private static final RowMapper<Vacation> VACATION_MAPPER = new VacationMapper(); |
|
29 |
private static final RowMapper<VacationDayDTO> VACATION_DAY_MAPPER = new VacationDayMapper(); |
|
30 |
|
|
31 |
/** |
|
32 |
* The logger. |
|
33 |
*/ |
|
34 |
private static final Logger log = LoggerFactory.getLogger(JdbcVacationRepository.class); |
|
35 |
|
|
36 |
/** |
|
37 |
* The connection to a database. |
|
38 |
*/ |
|
39 |
private final JdbcTemplate jdbc; |
|
40 |
|
|
41 |
/** |
|
42 |
* Creates a new instance of the class VacationRepository which selects and updates users in a database. |
|
43 |
* |
|
44 |
* @param jdbc A connection to the database. |
|
45 |
*/ |
|
46 |
@Autowired |
|
47 |
public JdbcVacationRepository(final JdbcTemplate jdbc) { |
|
48 |
JdbcVacationRepository.log.trace("Creating a new instance of the class VacationRepository"); |
|
49 |
|
|
50 |
this.jdbc = jdbc; |
|
51 |
} |
|
52 |
|
|
53 |
@Override |
|
54 |
public List<VacationDayDTO> getVacationDays(final long userId, final LocalDate from) { |
|
55 |
return jdbc.query("SELECT v.id, v.vacation_date, v.time_from, v.time_to, v.status, v.vacation_type " + |
|
56 |
"FROM vacation_day v " + |
|
57 |
"INNER JOIN end_user u ON v.user_id = u.id " + |
|
58 |
"WHERE v.user_id = ? AND v.vacation_date >= ?", |
|
59 |
VACATION_DAY_MAPPER, userId, from); |
|
60 |
} |
|
61 |
|
|
62 |
@Override |
|
63 |
public List<VacationDayDTO> getVacationDays(final long userId, final LocalDate from, final Status status) { |
|
64 |
return jdbc.query("SELECT v.id, v.vacation_date, v.time_from, v.time_to, v.status, v.vacation_type " + |
|
65 |
"FROM vacation_day v " + |
|
66 |
"INNER JOIN end_user u ON v.user_id = u.id " + |
|
67 |
"WHERE v.user_id = ? AND v.vacation_date >= ? AND v.status = ?", |
|
68 |
VACATION_DAY_MAPPER, userId, from, status.name()); |
|
69 |
} |
|
70 |
|
|
71 |
@Override |
|
72 |
public List<VacationDayDTO> getVacationDays(final long userId, final LocalDate from, final LocalDate to) { |
|
73 |
return jdbc.query("SELECT v.id, v.vacation_date, v.time_from, v.time_to, v.status, v.vacation_type " + |
|
74 |
"FROM vacation_day v " + |
|
75 |
"INNER JOIN end_user u ON v.user_id = u.id " + |
|
76 |
"WHERE v.user_id=? AND v.vacation_date >= ? AND v.vacation_date <= ?", |
|
77 |
VACATION_DAY_MAPPER, userId, from, to); |
|
78 |
|
|
79 |
} |
|
80 |
|
|
81 |
@Override |
|
82 |
public List<VacationDayDTO> getVacationDays(final long userId, final LocalDate from, final LocalDate to, final Status status) { |
|
83 |
return jdbc.query("SELECT v.id, v.vacation_date, v.time_from, v.time_to, v.status, v.vacation_type " + |
|
84 |
"FROM vacation_day v " + |
|
85 |
"INNER JOIN end_user u ON v.user_id = u.id " + |
|
86 |
"WHERE v.user_id=? AND v.vacation_date >= ? AND v.vacation_date <= ? AND v.status = ?", |
|
87 |
VACATION_DAY_MAPPER, userId, from, to, status.name()); |
|
88 |
} |
|
89 |
|
|
90 |
@Override |
|
91 |
public Optional<Vacation> getVacationDay(final long id) { |
|
92 |
return ofNullable(jdbc.queryForObject("SELECT * FROM vacation_day WHERE id = ?", VACATION_MAPPER, id)); |
|
93 |
} |
|
94 |
|
|
95 |
@Override |
|
96 |
public void insertVacationDay(final Long userId, final Vacation day) { |
|
97 |
jdbc.update("INSERT INTO vacation_day (vacation_date, time_from, time_to, status, vacation_type, user_id) VALUES (?,?,?,?,?,?)", |
|
98 |
day.getDate(), day.getFrom(), day.getTo(), day.getStatus().name(), day.getType().name(), userId); |
|
99 |
} |
|
100 |
|
|
101 |
@Override |
|
102 |
public void updateVacationDay(final Vacation item) { |
|
103 |
jdbc.update("UPDATE vacation_day SET vacation_date=?, time_from=?, time_to=?, status=?, vacation_type=? WHERE id=?", |
|
104 |
item.getDate(), item.getFrom(), item.getTo(), item.getStatus().name(), item.getType().name(), item.getId()); |
|
105 |
} |
|
106 |
|
|
107 |
@Override |
|
108 |
public void deleteVacationDay(final long id) { |
|
109 |
jdbc.update("DELETE FROM vacation_day WHERE id=?", id); |
|
110 |
} |
|
111 |
|
|
112 |
@Override |
|
113 |
public boolean isExistVacationForUser(Long userId, LocalDate date) { |
|
114 |
return jdbc.queryForObject("SELECT count(id) AS exist FROM vacation_day WHERE vacation_date = ? AND user_id = ?", |
|
115 |
new Object[]{date, userId}, (rs, rowNum) -> rs.getBoolean("exist")); |
|
116 |
} |
|
117 |
} |
server/src/main/java/org/danekja/ymanager/repository/jdbc/mappers/AuthorizationRequestMapper.java | ||
---|---|---|
1 |
package org.danekja.ymanager.repository.jdbc.mappers; |
|
2 |
|
|
3 |
import org.danekja.ymanager.domain.Status; |
|
4 |
import org.danekja.ymanager.dto.AuthorizationRequestDTO; |
|
5 |
import org.springframework.jdbc.core.RowMapper; |
|
6 |
|
|
7 |
import java.sql.ResultSet; |
|
8 |
import java.sql.SQLException; |
|
9 |
|
|
10 |
/** |
|
11 |
* The mapper maps a row from a result of a query to an AuthorizationRequest. |
|
12 |
*/ |
|
13 |
public class AuthorizationRequestMapper implements RowMapper<AuthorizationRequestDTO> { |
|
14 |
|
|
15 |
/** |
|
16 |
* Maps a row from a result of a query to an AuthorizationRequest. |
|
17 |
* @param resultSet the row from the result |
|
18 |
* @param i the index of the row |
|
19 |
* @return the AuthorizationRequest object |
|
20 |
* @throws SQLException if the columnLabel is not valid; if a database access error occurs or this method is called on a closed result set |
|
21 |
*/ |
|
22 |
@Override |
|
23 |
public AuthorizationRequestDTO mapRow(ResultSet resultSet, int i) throws SQLException { |
|
24 |
final AuthorizationRequestDTO request = new AuthorizationRequestDTO(); |
|
25 |
request.setId(resultSet.getLong("id")); |
|
26 |
request.setFirstName(resultSet.getString("first_name")); |
|
27 |
request.setLastName(resultSet.getString("last_name")); |
|
28 |
request.setTimestamp(resultSet.getTimestamp("creation_date").toLocalDateTime()); |
|
29 |
request.setStatus(Status.valueOf(resultSet.getString("status"))); |
|
30 |
return request; |
|
31 |
} |
|
32 |
} |
server/src/main/java/org/danekja/ymanager/repository/jdbc/mappers/BasicProfileUserMapper.java | ||
---|---|---|
1 |
package org.danekja.ymanager.repository.jdbc.mappers; |
|
2 |
|
|
3 |
import org.danekja.ymanager.dto.BasicProfileUserDTO; |
|
4 |
import org.springframework.jdbc.core.RowMapper; |
|
5 |
|
|
6 |
import java.sql.ResultSet; |
|
7 |
import java.sql.SQLException; |
|
8 |
|
|
9 |
/** |
|
10 |
* The mapper maps a row from a result of a query to an BasicProfileUser. |
|
11 |
*/ |
|
12 |
public class BasicProfileUserMapper implements RowMapper<BasicProfileUserDTO> { |
|
13 |
|
|
14 |
/** |
|
15 |
* Maps a row from a result of a query to an BasicProfileUser. |
|
16 |
* @param resultSet the row from the result |
|
17 |
* @param i the index of the row |
|
18 |
* @return the BasicProfileUser object |
|
19 |
* @throws SQLException if the columnLabel is not valid; if a database access error occurs or this method is called on a closed result set |
|
20 |
*/ |
|
21 |
@Override |
|
22 |
public BasicProfileUserDTO mapRow(ResultSet resultSet, int i) throws SQLException { |
|
23 |
final BasicProfileUserDTO user = new BasicProfileUserDTO(); |
|
24 |
user.setId(resultSet.getLong("id")); |
|
25 |
user.setFirstName(resultSet.getString("first_name")); |
|
26 |
user.setLastName(resultSet.getString("last_name")); |
|
27 |
user.setPhoto(resultSet.getString("photo")); |
|
28 |
return user; |
|
29 |
} |
|
30 |
} |
server/src/main/java/org/danekja/ymanager/repository/jdbc/mappers/VacationDayMapper.java | ||
---|---|---|
1 |
package org.danekja.ymanager.repository.jdbc.mappers; |
|
2 |
|
|
3 |
import org.danekja.ymanager.domain.Status; |
|
4 |
import org.danekja.ymanager.domain.VacationType; |
|
5 |
import org.danekja.ymanager.dto.VacationDayDTO; |
|
6 |
import org.springframework.jdbc.core.RowMapper; |
|
7 |
|
|
8 |
import java.sql.ResultSet; |
|
9 |
import java.sql.SQLException; |
|
10 |
import java.sql.Time; |
|
11 |
|
|
12 |
/** |
|
13 |
* The mapper maps a row from a result of a query to an Vacation. |
|
14 |
*/ |
|
15 |
public class VacationDayMapper implements RowMapper<VacationDayDTO> { |
|
16 |
|
|
17 |
/** |
|
18 |
* Maps a row from a result of a query to an Vacation. |
|
19 |
* @param resultSet the row from the result |
|
20 |
* @param i the index of the row |
|
21 |
* @return the Vacation object |
|
22 |
* @throws SQLException if the columnLabel is not valid; if a database access error occurs or this method is called on a closed result set |
|
23 |
*/ |
|
24 |
@Override |
|
25 |
public VacationDayDTO mapRow(ResultSet resultSet, int i) throws SQLException { |
|
26 |
final VacationDayDTO item = new VacationDayDTO(); |
|
27 |
item.setId(resultSet.getLong("v.id")); |
|
28 |
item.setDate(resultSet.getDate("v.vacation_date").toLocalDate()); |
|
29 |
|
|
30 |
/* |
|
31 |
When a result contains a sick day it doesn't have specified a start and end time because |
|
32 |
it can be taken only as a whole day. In this case the v.time_from and v.time_to are null. |
|
33 |
Which must be handled. |
|
34 |
*/ |
|
35 |
final Time timeFrom = resultSet.getTime("v.time_from"); |
|
36 |
if (timeFrom != null) { |
|
37 |
item.setFrom(timeFrom.toLocalTime()); |
|
38 |
} |
|
39 |
|
|
40 |
final Time timeTo = resultSet.getTime("v.time_to"); |
|
41 |
if (timeTo != null) { |
|
42 |
item.setTo(timeTo.toLocalTime()); |
|
43 |
} |
|
44 |
|
|
45 |
item.setStatus(Status.valueOf(resultSet.getString("v.status"))); |
|
46 |
item.setType(VacationType.getVacationType(resultSet.getString("v.vacation_type"))); |
|
47 |
return item; |
|
48 |
} |
|
49 |
} |
server/src/main/java/org/danekja/ymanager/repository/jdbc/mappers/VacationMapper.java | ||
---|---|---|
1 |
package org.danekja.ymanager.repository.jdbc.mappers; |
|
2 |
|
|
3 |
import org.danekja.ymanager.domain.Status; |
|
4 |
import org.danekja.ymanager.domain.Vacation; |
|
5 |
import org.danekja.ymanager.domain.VacationType; |
|
6 |
import org.springframework.jdbc.core.RowMapper; |
|
7 |
|
|
8 |
import java.sql.ResultSet; |
|
9 |
import java.sql.SQLException; |
|
10 |
import java.sql.Time; |
|
11 |
|
|
12 |
/** |
|
13 |
* Maps "vacation_day" query rows into Vacation object. |
|
14 |
*/ |
|
15 |
public class VacationMapper implements RowMapper<Vacation> { |
|
16 |
@Override |
|
17 |
public Vacation mapRow(ResultSet rs, int i) throws SQLException { |
|
18 |
Vacation vacation = new Vacation(); |
|
19 |
vacation.setId(rs.getLong("id")); |
|
20 |
vacation.setDate(rs.getDate("vacation_date").toLocalDate()); |
|
21 |
|
|
22 |
/* |
|
23 |
When a result contains a sick day it doesn't have specified a start and end time because |
|
24 |
it can be taken only as a whole day. In this case the v.time_from and v.time_to are null. |
|
25 |
Which must be handled. |
|
26 |
*/ |
|
27 |
final Time timeFrom = rs.getTime("time_from"); |
|
28 |
if (timeFrom != null) { |
|
29 |
vacation.setFrom(timeFrom.toLocalTime()); |
|
30 |
} |
|
31 |
|
|
32 |
final Time timeTo = rs.getTime("time_to"); |
|
33 |
if (timeTo != null) { |
|
34 |
vacation.setTo(timeTo.toLocalTime()); |
|
35 |
} |
|
36 |
|
|
37 |
vacation.setCreationDate(rs.getTimestamp("creation_date").toLocalDateTime()); |
|
38 |
vacation.setStatus(Status.valueOf(rs.getString("status"))); |
|
39 |
vacation.setType(VacationType.getVacationType(rs.getString("vacation_type"))); |
|
40 |
vacation.setUserId(rs.getLong("user_id")); |
|
41 |
return vacation; |
|
42 |
} |
|
43 |
} |
server/src/main/java/org/danekja/ymanager/repository/jdbc/mappers/VacationRequestMapper.java | ||
---|---|---|
1 |
package org.danekja.ymanager.repository.jdbc.mappers; |
|
2 |
|
|
3 |
import org.danekja.ymanager.domain.Status; |
|
4 |
import org.danekja.ymanager.domain.VacationType; |
|
5 |
import org.danekja.ymanager.dto.VacationRequestDTO; |
|
6 |
import org.springframework.jdbc.core.RowMapper; |
|
7 |
|
|
8 |
import java.sql.ResultSet; |
|
9 |
import java.sql.SQLException; |
|
10 |
import java.sql.Time; |
|
11 |
|
|
12 |
/** |
|
13 |
* The mapper maps each row from a result of a query to a VacationRequest. |
|
14 |
*/ |
|
15 |
public class VacationRequestMapper implements RowMapper<VacationRequestDTO> { |
|
16 |
|
|
17 |
/** |
|
18 |
* Maps a row from a result of a query to an VacationRequest. |
|
19 |
* @param resultSet the row from the result |
|
20 |
* @param i the index of the row |
|
21 |
* @return the VacationRequest object |
|
22 |
* @throws SQLException if the columnLabel is not valid; if a database access error occurs or this method is called on a closed result set |
|
23 |
*/ |
|
24 |
@Override |
|
25 |
public VacationRequestDTO mapRow(ResultSet resultSet, int i) throws SQLException { |
|
26 |
final VacationRequestDTO request = new VacationRequestDTO(); |
|
27 |
request.setId(resultSet.getLong("v.id")); |
|
28 |
request.setFirstName(resultSet.getString("u.first_name")); |
|
29 |
request.setLastName(resultSet.getString("u.last_name")); |
|
30 |
request.setDate(resultSet.getDate("v.vacation_date").toLocalDate()); |
|
31 |
|
|
32 |
/* |
|
33 |
When a result contains a sick day it doesn't have specified a start and end time because |
|
34 |
it can be taken only as a whole day. In this case the v.time_from and v.time_to are null. |
|
35 |
Which must be handled. |
|
36 |
*/ |
|
37 |
final Time timeFrom = resultSet.getTime("v.time_from"); |
|
38 |
if (timeFrom != null) { |
|
39 |
request.setFrom(timeFrom.toLocalTime()); |
|
40 |
} |
|
41 |
|
|
42 |
final Time timeTo = resultSet.getTime("v.time_to"); |
|
43 |
if (timeTo != null) { |
|
44 |
request.setTo(timeTo.toLocalTime()); |
|
45 |
} |
|
46 |
|
|
47 |
request.setTimestamp(resultSet.getTimestamp("v.creation_date").toLocalDateTime()); |
|
48 |
request.setType(VacationType.getVacationType(resultSet.getString("v.vacation_type"))); |
|
49 |
request.setStatus(Status.valueOf(resultSet.getString("v.status"))); |
|
50 |
return request; |
|
51 |
} |
|
52 |
} |
Také k dispozici: Unified diff
re #31 extract repository interfaces