Revize 25fbc04a
Přidáno uživatelem Lukas Cerny před téměř 6 roky(ů)
server/pom.xml | ||
---|---|---|
29 | 29 |
<version>3.9</version> |
30 | 30 |
</dependency> |
31 | 31 |
|
32 |
<dependency> |
|
33 |
<groupId>org.apache.poi</groupId> |
|
34 |
<artifactId>poi</artifactId> |
|
35 |
<version>4.1.0</version> |
|
36 |
</dependency> |
|
37 |
|
|
38 |
<dependency> |
|
39 |
<groupId>org.apache.pdfbox</groupId> |
|
40 |
<artifactId>pdfbox</artifactId> |
|
41 |
<version>2.0.15</version> |
|
42 |
</dependency> |
|
43 |
|
|
32 | 44 |
|
33 | 45 |
<!-- DATABASE --> |
34 | 46 |
<dependency> |
server/src/main/java/cz/zcu/yamanager/business/FileExportResult.java | ||
---|---|---|
1 |
package cz.zcu.yamanager.business; |
|
2 |
|
|
3 |
public class FileExportResult { |
|
4 |
|
|
5 |
private final String name; |
|
6 |
private final byte[] bytes; |
|
7 |
|
|
8 |
public FileExportResult(String name, byte[] bytes) { |
|
9 |
this.name = name; |
|
10 |
this.bytes = bytes; |
|
11 |
} |
|
12 |
|
|
13 |
public String getName() { |
|
14 |
return name; |
|
15 |
} |
|
16 |
|
|
17 |
public byte[] getBytes() { |
|
18 |
return bytes; |
|
19 |
} |
|
20 |
} |
server/src/main/java/cz/zcu/yamanager/business/FileImportResult.java | ||
---|---|---|
1 |
package cz.zcu.yamanager.business; |
|
2 |
|
|
3 |
public class FileImportResult { |
|
4 |
|
|
5 |
private final String name; |
|
6 |
private final Long size; |
|
7 |
|
|
8 |
public FileImportResult(String name, Long size) { |
|
9 |
this.name = name; |
|
10 |
this.size = size; |
|
11 |
} |
|
12 |
|
|
13 |
public String getName() { |
|
14 |
return name; |
|
15 |
} |
|
16 |
|
|
17 |
public Long getSize() { |
|
18 |
return size; |
|
19 |
} |
|
20 |
} |
server/src/main/java/cz/zcu/yamanager/business/FileService.java | ||
---|---|---|
1 |
package cz.zcu.yamanager.business; |
|
2 |
|
|
3 |
import cz.zcu.yamanager.ws.rest.RESTFullException; |
|
4 |
|
|
5 |
public interface FileService { |
|
6 |
|
|
7 |
FileImportResult parseXLSFile(String fileName, byte[] bytes) throws RESTFullException; |
|
8 |
|
|
9 |
FileExportResult createPDFFile() throws RESTFullException; |
|
10 |
|
|
11 |
} |
server/src/main/java/cz/zcu/yamanager/business/Manager.java | ||
---|---|---|
1 |
package cz.zcu.yamanager.business; |
|
2 |
|
|
3 |
import cz.zcu.yamanager.dto.*; |
|
4 |
import cz.zcu.yamanager.ws.rest.RESTFullException; |
|
5 |
|
|
6 |
import java.time.LocalDate; |
|
7 |
import java.util.List; |
|
8 |
|
|
9 |
public interface Manager { |
|
10 |
|
|
11 |
List<BasicProfileUser> getUsers(Status status) throws RESTFullException; |
|
12 |
|
|
13 |
List<VacationRequest> getVacationRequests(Status status) throws RESTFullException; |
|
14 |
|
|
15 |
List<AuthorizationRequest> getAuthorizationRequests(Status status) throws RESTFullException; |
|
16 |
|
|
17 |
FullUserProfile getUserProfile(Long userId) throws RESTFullException; |
|
18 |
|
|
19 |
DefaultSettings getDefaultSettings() throws RESTFullException; |
|
20 |
|
|
21 |
List<VacationDay> getUserCalendar(Long userId, LocalDate fromDate, LocalDate toDate, Status status) throws RESTFullException; |
|
22 |
|
|
23 |
void createSettings(DefaultSettings settings) throws RESTFullException; |
|
24 |
|
|
25 |
void createVacation(Long userId, VacationDay vacationDay) throws RESTFullException; |
|
26 |
|
|
27 |
void changeSettings(Long userId, UserSettings settings) throws RESTFullException; |
|
28 |
|
|
29 |
void changeVacation(Long userId, VacationDay vacationDay) throws RESTFullException; |
|
30 |
|
|
31 |
void changeRequest(RequestType type, BasicRequest request) throws RESTFullException; |
|
32 |
|
|
33 |
void deleteVacation(Long userId, Long vacationId) throws RESTFullException; |
|
34 |
} |
server/src/main/java/cz/zcu/yamanager/business/mock/FileServiceMock.java | ||
---|---|---|
1 |
package cz.zcu.yamanager.business.mock; |
|
2 |
|
|
3 |
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream; |
|
4 |
import cz.zcu.yamanager.business.FileExportResult; |
|
5 |
import cz.zcu.yamanager.business.FileImportResult; |
|
6 |
import cz.zcu.yamanager.business.FileService; |
|
7 |
import cz.zcu.yamanager.ws.rest.RESTFullException; |
|
8 |
import org.apache.pdfbox.pdmodel.PDDocument; |
|
9 |
import org.apache.pdfbox.pdmodel.PDPage; |
|
10 |
import org.apache.pdfbox.pdmodel.PDPageContentStream; |
|
11 |
import org.apache.pdfbox.pdmodel.common.PDRectangle; |
|
12 |
import org.apache.pdfbox.pdmodel.font.PDType1Font; |
|
13 |
import org.apache.poi.hssf.usermodel.HSSFCell; |
|
14 |
import org.apache.poi.hssf.usermodel.HSSFRow; |
|
15 |
import org.apache.poi.hssf.usermodel.HSSFSheet; |
|
16 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
|
17 |
import org.springframework.stereotype.Component; |
|
18 |
|
|
19 |
import java.io.ByteArrayInputStream; |
|
20 |
import java.io.IOException; |
|
21 |
import java.util.ArrayList; |
|
22 |
import java.util.Iterator; |
|
23 |
import java.util.List; |
|
24 |
|
|
25 |
@Component |
|
26 |
public class FileServiceMock implements FileService { |
|
27 |
|
|
28 |
private List<String> content = new ArrayList<>(); |
|
29 |
|
|
30 |
@Override |
|
31 |
public FileImportResult parseXLSFile(String fileName, byte[] bytes) throws RESTFullException { |
|
32 |
try { |
|
33 |
HSSFWorkbook wb = new HSSFWorkbook(new ByteArrayInputStream(bytes)); |
|
34 |
|
|
35 |
HSSFSheet sheet=wb.getSheetAt(0); |
|
36 |
HSSFRow row; |
|
37 |
HSSFCell cell; |
|
38 |
|
|
39 |
Iterator rows = sheet.rowIterator(); |
|
40 |
|
|
41 |
while (rows.hasNext()) { |
|
42 |
row=(HSSFRow) rows.next(); |
|
43 |
Iterator cells = row.cellIterator(); |
|
44 |
|
|
45 |
while (cells.hasNext()) { |
|
46 |
cell = (HSSFCell) cells.next(); |
|
47 |
|
|
48 |
content.add(cell.getStringCellValue()); |
|
49 |
} |
|
50 |
} |
|
51 |
return new FileImportResult(fileName, (long) bytes.length); |
|
52 |
} catch (IOException e) { |
|
53 |
throw new RESTFullException("error", ""); |
|
54 |
} |
|
55 |
} |
|
56 |
|
|
57 |
@Override |
|
58 |
public FileExportResult createPDFFile() throws RESTFullException { |
|
59 |
|
|
60 |
PDDocument document = new PDDocument(); |
|
61 |
PDPage page1 = new PDPage(PDRectangle.A4); |
|
62 |
PDRectangle rect = page1.getMediaBox(); |
|
63 |
document.addPage(page1); |
|
64 |
|
|
65 |
try { |
|
66 |
PDPageContentStream cos = new PDPageContentStream(document, page1); |
|
67 |
|
|
68 |
int line = 0; |
|
69 |
|
|
70 |
for (String word : content) { |
|
71 |
cos.beginText(); |
|
72 |
cos.setFont(PDType1Font.HELVETICA, 12); |
|
73 |
cos.newLineAtOffset(100, rect.getHeight() - 50*(++line)); |
|
74 |
cos.showText(word); |
|
75 |
cos.endText(); |
|
76 |
} |
|
77 |
|
|
78 |
cos.close(); |
|
79 |
|
|
80 |
ByteOutputStream output = new ByteOutputStream(); |
|
81 |
|
|
82 |
document.save(output); |
|
83 |
document.close(); |
|
84 |
|
|
85 |
return new FileExportResult("export.pdf", output.getBytes()); |
|
86 |
|
|
87 |
} catch (IOException ioex) { |
|
88 |
throw new RESTFullException("", ""); |
|
89 |
} |
|
90 |
} |
|
91 |
} |
server/src/main/java/cz/zcu/yamanager/business/mock/ManagerMock.java | ||
---|---|---|
1 |
package cz.zcu.yamanager.business.mock; |
|
2 |
|
|
3 |
import cz.zcu.yamanager.dto.*; |
|
4 |
import cz.zcu.yamanager.business.Manager; |
|
5 |
import cz.zcu.yamanager.ws.rest.RESTFullException; |
|
6 |
import org.springframework.stereotype.Component; |
|
7 |
|
|
8 |
import java.time.LocalDate; |
|
9 |
import java.time.LocalDateTime; |
|
10 |
import java.time.LocalTime; |
|
11 |
import java.time.Period; |
|
12 |
import java.util.*; |
|
13 |
import java.util.function.Predicate; |
|
14 |
import java.util.stream.Collectors; |
|
15 |
|
|
16 |
import static java.util.Arrays.asList; |
|
17 |
|
|
18 |
@Component |
|
19 |
public class ManagerMock implements Manager { |
|
20 |
|
|
21 |
private List<DefaultSettings> settings = new LinkedList<>(); |
|
22 |
|
|
23 |
private Map<Long, List<VacationDay>> vacations = new HashMap<>(); |
|
24 |
|
|
25 |
private Map<Long, UserSettings> userSettings = new HashMap<>(); |
|
26 |
|
|
27 |
private BasicProfileUser createBasicProfileUser(long id, Status status) { |
|
28 |
BasicProfileUser user = new BasicProfileUser(); |
|
29 |
|
|
30 |
user.setId(id); |
|
31 |
user.setFirstName("Tomas"); |
|
32 |
user.setLastName(status == null ? "unknown" : status.name().toLowerCase()); |
|
33 |
user.setPhoto("https://st2.depositphotos.com/9223672/12056/v/950/depositphotos_120568236-stock-illustration-male-face-avatar-logo-template.jpg"); |
|
34 |
user.setCalendar(asList( |
|
35 |
createVacationDay(1L, LocalDate.now(), VacationType.VACATION, Status.ACCEPTED), |
|
36 |
createVacationDay(2L, LocalDate.now().plusDays(1), VacationType.VACATION, Status.ACCEPTED)) |
|
37 |
); |
|
38 |
|
|
39 |
return user; |
|
40 |
} |
|
41 |
|
|
42 |
private VacationDay createVacationDay(long id, LocalDate date, VacationType vacationType, Status status) { |
|
43 |
VacationDay vacationDay = new VacationDay(); |
|
44 |
|
|
45 |
vacationDay.setId(id); |
|
46 |
vacationDay.setType(vacationType); |
|
47 |
vacationDay.setDate(date); |
|
48 |
vacationDay.setStatus(status); |
|
49 |
|
|
50 |
switch (vacationType) { |
|
51 |
case SICKDAY: { |
|
52 |
vacationDay.setFrom(null); |
|
53 |
vacationDay.setTo(null); |
|
54 |
} break; |
|
55 |
case VACATION: { |
|
56 |
vacationDay.setFrom(LocalTime.of(9, 00)); |
|
57 |
vacationDay.setTo(LocalTime.of(13, 00)); |
|
58 |
} break; |
|
59 |
} |
|
60 |
|
|
61 |
return vacationDay; |
|
62 |
} |
|
63 |
|
|
64 |
private VacationRequest createVacationRequest(long id, VacationType vacationType, Status status) { |
|
65 |
VacationRequest request = new VacationRequest(); |
|
66 |
|
|
67 |
request.setId(id); |
|
68 |
request.setFirstName("Tomas"); |
|
69 |
request.setLastName("Novak"); |
|
70 |
request.setDate(LocalDate.now()); |
|
71 |
request.setType(vacationType); |
|
72 |
request.setStatus(status); |
|
73 |
request.setTimestamp(LocalDateTime.now()); |
|
74 |
|
|
75 |
switch (vacationType) { |
|
76 |
case SICKDAY: { |
|
77 |
request.setFrom(null); |
|
78 |
request.setTo(null); |
|
79 |
} break; |
|
80 |
case VACATION: { |
|
81 |
request.setFrom(LocalTime.of(9, 30)); |
|
82 |
request.setTo(LocalTime.of(18, 30)); |
|
83 |
} break; |
|
84 |
} |
|
85 |
|
|
86 |
return request; |
|
87 |
} |
|
88 |
|
|
89 |
private AuthorizationRequest createAuthorizationRequest(long id, Status status) { |
|
90 |
AuthorizationRequest request = new AuthorizationRequest(); |
|
91 |
|
|
92 |
request.setId(id); |
|
93 |
request.setFirstName("Tomas"); |
|
94 |
request.setLastName("Novak"); |
|
95 |
request.setStatus(status); |
|
96 |
request.setTimestamp(LocalDateTime.now()); |
|
97 |
|
|
98 |
return request; |
|
99 |
} |
|
100 |
|
|
101 |
private FullUserProfile createFullUserProfile(Long id) { |
|
102 |
FullUserProfile user = new FullUserProfile(); |
|
103 |
|
|
104 |
user.setId(id); |
|
105 |
user.setFirstName("Tomas"); |
|
106 |
user.setLastName("Novak"); |
|
107 |
user.setPhoto("https://st2.depositphotos.com/9223672/12056/v/950/depositphotos_120568236-stock-illustration-male-face-avatar-logo-template.jpg"); |
|
108 |
user.setStatus(Status.ACCEPTED); |
|
109 |
|
|
110 |
if (userSettings.containsKey(id)) { |
|
111 |
UserSettings settings = userSettings.get(id); |
|
112 |
user.setVacationCount(settings.getVacationCount()); |
|
113 |
user.setSickdayCount(settings.getSickdayCount()); |
|
114 |
user.setRole(settings.getRole()); |
|
115 |
} else { |
|
116 |
user.setVacationCount(8.5F); |
|
117 |
user.setSickdayCount(3); |
|
118 |
user.setRole(UserRole.EMPLOYER); |
|
119 |
} |
|
120 |
|
|
121 |
user.setNotification(LocalDateTime.of(2000, 12, 1, 9, 0)); |
|
122 |
|
|
123 |
return user; |
|
124 |
} |
|
125 |
|
|
126 |
private DefaultSettings createDefaultSettings() { |
|
127 |
DefaultSettings settings = new DefaultSettings(); |
|
128 |
|
|
129 |
settings.setSickdayCount(3); |
|
130 |
settings.setNotification(LocalDateTime.now()); |
|
131 |
|
|
132 |
return settings; |
|
133 |
} |
|
134 |
|
|
135 |
@Override |
|
136 |
public List<BasicProfileUser> getUsers(Status status) throws RESTFullException { |
|
137 |
int count = 5; |
|
138 |
List<BasicProfileUser> users = new ArrayList<>(count); |
|
139 |
for (int i = 1; i <= count; i++) { |
|
140 |
users.add(createBasicProfileUser(i, status)); |
|
141 |
} |
|
142 |
return users; |
|
143 |
} |
|
144 |
|
|
145 |
@Override |
|
146 |
public List<VacationRequest> getVacationRequests(Status status) throws RESTFullException { |
|
147 |
int count = 5; |
|
148 |
List<VacationRequest> requests = new ArrayList<>(count); |
|
149 |
int length = VacationType.values().length; |
|
150 |
for (int i = 1; i <= count; i++) { |
|
151 |
requests.add(createVacationRequest(i, VacationType.values()[i % length], status)); |
|
152 |
} |
|
153 |
return requests; |
|
154 |
} |
|
155 |
|
|
156 |
@Override |
|
157 |
public List<AuthorizationRequest> getAuthorizationRequests(Status status) { |
|
158 |
int count = 5; |
|
159 |
List<AuthorizationRequest> requests = new ArrayList<>(count); |
|
160 |
for (int i = 1; i <= count; i++) { |
|
161 |
requests.add(createAuthorizationRequest(i, status)); |
|
162 |
} |
|
163 |
return requests; |
|
164 |
} |
|
165 |
|
|
166 |
@Override |
|
167 |
public FullUserProfile getUserProfile(Long userId) { |
|
168 |
return createFullUserProfile(userId); |
|
169 |
} |
|
170 |
|
|
171 |
@Override |
|
172 |
public DefaultSettings getDefaultSettings() { |
|
173 |
return settings.isEmpty() ? createDefaultSettings() : settings.get(settings.size()-1); |
|
174 |
} |
|
175 |
|
|
176 |
@Override |
|
177 |
public List<VacationDay> getUserCalendar(Long userId, LocalDate fromDate, LocalDate toDate, Status status) { |
|
178 |
if (vacations.containsKey(userId)) { |
|
179 |
List<VacationDay> vacationDays = vacations.get(userId); |
|
180 |
Predicate<VacationDay> predicate = (day) -> day.getDate().isAfter(fromDate); |
|
181 |
predicate = predicate.or((day) -> day.getDate().isEqual(fromDate)); |
|
182 |
if (toDate != null) { |
|
183 |
predicate = predicate.and((day) -> day.getDate().isBefore(toDate)).or((day) -> day.getDate().isEqual(toDate)); |
|
184 |
} |
|
185 |
return vacationDays.stream().filter(predicate).collect(Collectors.toList()); |
|
186 |
|
|
187 |
} else { |
|
188 |
int daysDifference = toDate == null ? 5 : Period.between(fromDate, toDate).getDays(); |
|
189 |
List<VacationDay> days = new ArrayList<>(daysDifference); |
|
190 |
for (int i = 0; i < daysDifference; i++) { |
|
191 |
days.add(createVacationDay(i + 1, fromDate.plusDays(i), VacationType.values()[i % 2], status)); |
|
192 |
} |
|
193 |
return days; |
|
194 |
} |
|
195 |
} |
|
196 |
|
|
197 |
@Override |
|
198 |
public void createSettings(DefaultSettings sett) { |
|
199 |
settings.add(sett); |
|
200 |
} |
|
201 |
|
|
202 |
@Override |
|
203 |
public void createVacation(Long userId, VacationDay vacationDay) { |
|
204 |
if (!vacations.containsKey(userId)) { |
|
205 |
vacations.put(userId, new ArrayList<>()); |
|
206 |
} |
|
207 |
|
|
208 |
List<VacationDay> days = vacations.get(userId); |
|
209 |
days.add(vacationDay); |
|
210 |
vacationDay.setId((long)days.size()); |
|
211 |
} |
|
212 |
|
|
213 |
@Override |
|
214 |
public void changeSettings(Long userId, UserSettings settings) { |
|
215 |
userSettings.put(userId, settings); |
|
216 |
} |
|
217 |
|
|
218 |
@Override |
|
219 |
public void changeVacation(Long userId, VacationDay vacationDay) throws RESTFullException { |
|
220 |
if (vacationDay.getId() == null) { |
|
221 |
throw new RESTFullException("Vacation does not contain ID.", "rest.exception.generic"); |
|
222 |
} |
|
223 |
|
|
224 |
if (!vacations.containsKey(userId)) { |
|
225 |
throw new RESTFullException("User does not exist.", "rest.exception.generic"); |
|
226 |
} |
|
227 |
|
|
228 |
List<VacationDay> vacs = vacations.get(userId); |
|
229 |
boolean founded = false; |
|
230 |
for (int i = 0; i < vacs.size(); i++) { |
|
231 |
if (vacs.get(i).getId() == vacationDay.getId()) { |
|
232 |
VacationDay day = vacs.get(i); |
|
233 |
day.setStatus(vacationDay.getStatus()); |
|
234 |
day.setFrom(vacationDay.getFrom()); |
|
235 |
day.setTo(vacationDay.getTo()); |
|
236 |
day.setDate(vacationDay.getDate()); |
|
237 |
day.setType(vacationDay.getType()); |
|
238 |
founded = true; |
|
239 |
break; |
|
240 |
} |
|
241 |
} |
|
242 |
|
|
243 |
if (!founded) { |
|
244 |
throw new RESTFullException("Vacation does not exist.", "rest.exception.generic"); |
|
245 |
} |
|
246 |
} |
|
247 |
|
|
248 |
@Override |
|
249 |
public void changeRequest(RequestType type, BasicRequest request) throws RESTFullException { |
|
250 |
|
|
251 |
if (request.getId() == null) { |
|
252 |
throw new RESTFullException("ID of request can not be empty.", "rest.exception.generic"); |
|
253 |
} |
|
254 |
|
|
255 |
switch (type) { |
|
256 |
case VACATION: { |
|
257 |
boolean found = false; |
|
258 |
for (List<VacationDay> value : vacations.values()) { |
|
259 |
for (VacationDay day : value) { |
|
260 |
if (day.getId() == request.getId()) { |
|
261 |
found = true; |
|
262 |
day.setStatus(request.getStatus()); |
|
263 |
break; |
|
264 |
} |
|
265 |
} |
|
266 |
if (found) break; |
|
267 |
} |
|
268 |
if (!found) { |
|
269 |
throw new RESTFullException("Vacation does not exist.", "rest.exception.generic"); |
|
270 |
} |
|
271 |
} break; |
|
272 |
case AUTHORIZATION: { |
|
273 |
throw new RESTFullException("User does not exist.", "rest.exception.generic"); |
|
274 |
} |
|
275 |
} |
|
276 |
} |
|
277 |
|
|
278 |
@Override |
|
279 |
public void deleteVacation(Long userId, Long vacationId) throws RESTFullException{ |
|
280 |
if (!vacations.containsKey(userId)) { |
|
281 |
throw new RESTFullException("User does not exist.", "rest.exception.generic"); |
|
282 |
} |
|
283 |
|
|
284 |
List<VacationDay> vacs = vacations.get(userId); |
|
285 |
if (!vacs.removeIf(day -> day.getId() == vacationId)) { |
|
286 |
throw new RESTFullException("Vacation does not exist for the user.", "rest.exception.generic"); |
|
287 |
} |
|
288 |
} |
|
289 |
} |
server/src/main/java/cz/zcu/yamanager/manager/Manager.java | ||
---|---|---|
1 |
package cz.zcu.yamanager.manager; |
|
2 |
|
|
3 |
import cz.zcu.yamanager.dto.*; |
|
4 |
import cz.zcu.yamanager.ws.rest.RESTFullException; |
|
5 |
|
|
6 |
import java.time.LocalDate; |
|
7 |
import java.util.List; |
|
8 |
|
|
9 |
public interface Manager { |
|
10 |
|
|
11 |
List<BasicProfileUser> getUsers(Status status) throws RESTFullException; |
|
12 |
|
|
13 |
List<VacationRequest> getVacationRequests(Status status) throws RESTFullException; |
|
14 |
|
|
15 |
List<AuthorizationRequest> getAuthorizationRequests(Status status) throws RESTFullException; |
|
16 |
|
|
17 |
FullUserProfile getUserProfile(Long userId) throws RESTFullException; |
|
18 |
|
|
19 |
DefaultSettings getDefaultSettings() throws RESTFullException; |
|
20 |
|
|
21 |
List<VacationDay> getUserCalendar(Long userId, LocalDate fromDate, LocalDate toDate, Status status) throws RESTFullException; |
|
22 |
|
|
23 |
void createSettings(DefaultSettings settings) throws RESTFullException; |
|
24 |
|
|
25 |
void createVacation(Long userId, VacationDay vacationDay) throws RESTFullException; |
|
26 |
|
|
27 |
void changeSettings(Long userId, UserSettings settings) throws RESTFullException; |
|
28 |
|
|
29 |
void changeVacation(Long userId, VacationDay vacationDay) throws RESTFullException; |
|
30 |
|
|
31 |
void changeRequest(RequestType type, BasicRequest request) throws RESTFullException; |
|
32 |
|
|
33 |
void deleteVacation(Long userId, Long vacationId) throws RESTFullException; |
|
34 |
} |
server/src/main/java/cz/zcu/yamanager/manager/mock/ManagerMock.java | ||
---|---|---|
1 |
package cz.zcu.yamanager.manager.mock; |
|
2 |
|
|
3 |
import cz.zcu.yamanager.dto.*; |
|
4 |
import cz.zcu.yamanager.manager.Manager; |
|
5 |
import cz.zcu.yamanager.ws.rest.RESTFullException; |
|
6 |
import org.springframework.stereotype.Component; |
|
7 |
|
|
8 |
import java.time.LocalDate; |
|
9 |
import java.time.LocalDateTime; |
|
10 |
import java.time.LocalTime; |
|
11 |
import java.time.Period; |
|
12 |
import java.util.*; |
|
13 |
import java.util.function.Predicate; |
|
14 |
import java.util.stream.Collectors; |
|
15 |
|
|
16 |
import static java.util.Arrays.asList; |
|
17 |
|
|
18 |
@Component |
|
19 |
public class ManagerMock implements Manager { |
|
20 |
|
|
21 |
private List<DefaultSettings> settings = new LinkedList<>(); |
|
22 |
|
|
23 |
private Map<Long, List<VacationDay>> vacations = new HashMap<>(); |
|
24 |
|
|
25 |
private Map<Long, UserSettings> userSettings = new HashMap<>(); |
|
26 |
|
|
27 |
private BasicProfileUser createBasicProfileUser(long id, Status status) { |
|
28 |
BasicProfileUser user = new BasicProfileUser(); |
|
29 |
|
|
30 |
user.setId(id); |
|
31 |
user.setFirstName("Tomas"); |
|
32 |
user.setLastName(status == null ? "unknown" : status.name().toLowerCase()); |
|
33 |
user.setPhoto("https://st2.depositphotos.com/9223672/12056/v/950/depositphotos_120568236-stock-illustration-male-face-avatar-logo-template.jpg"); |
|
34 |
user.setCalendar(asList( |
|
35 |
createVacationDay(1L, LocalDate.now(), VacationType.VACATION, Status.ACCEPTED), |
|
36 |
createVacationDay(2L, LocalDate.now().plusDays(1), VacationType.VACATION, Status.ACCEPTED)) |
|
37 |
); |
|
38 |
|
|
39 |
return user; |
|
40 |
} |
|
41 |
|
|
42 |
private VacationDay createVacationDay(long id, LocalDate date, VacationType vacationType, Status status) { |
|
43 |
VacationDay vacationDay = new VacationDay(); |
|
44 |
|
|
45 |
vacationDay.setId(id); |
|
46 |
vacationDay.setType(vacationType); |
|
47 |
vacationDay.setDate(date); |
|
48 |
vacationDay.setStatus(status); |
|
49 |
|
|
50 |
switch (vacationType) { |
|
51 |
case SICKDAY: { |
|
52 |
vacationDay.setFrom(null); |
|
53 |
vacationDay.setTo(null); |
|
54 |
} break; |
|
55 |
case VACATION: { |
|
56 |
vacationDay.setFrom(LocalTime.of(9, 00)); |
|
57 |
vacationDay.setTo(LocalTime.of(13, 00)); |
|
58 |
} break; |
|
59 |
} |
|
60 |
|
|
61 |
return vacationDay; |
|
62 |
} |
|
63 |
|
|
64 |
private VacationRequest createVacationRequest(long id, VacationType vacationType, Status status) { |
|
65 |
VacationRequest request = new VacationRequest(); |
|
66 |
|
|
67 |
request.setId(id); |
|
68 |
request.setFirstName("Tomas"); |
|
69 |
request.setLastName("Novak"); |
|
70 |
request.setDate(LocalDate.now()); |
|
71 |
request.setType(vacationType); |
|
72 |
request.setStatus(status); |
|
73 |
request.setTimestamp(LocalDateTime.now()); |
|
74 |
|
|
75 |
switch (vacationType) { |
|
76 |
case SICKDAY: { |
|
77 |
request.setFrom(null); |
|
78 |
request.setTo(null); |
|
79 |
} break; |
|
80 |
case VACATION: { |
|
81 |
request.setFrom(LocalTime.of(9, 30)); |
|
82 |
request.setTo(LocalTime.of(18, 30)); |
|
83 |
} break; |
|
84 |
} |
|
85 |
|
|
86 |
return request; |
|
87 |
} |
|
88 |
|
|
89 |
private AuthorizationRequest createAuthorizationRequest(long id, Status status) { |
|
90 |
AuthorizationRequest request = new AuthorizationRequest(); |
|
91 |
|
|
92 |
request.setId(id); |
|
93 |
request.setFirstName("Tomas"); |
|
94 |
request.setLastName("Novak"); |
|
95 |
request.setStatus(status); |
|
96 |
request.setTimestamp(LocalDateTime.now()); |
|
97 |
|
|
98 |
return request; |
|
99 |
} |
|
100 |
|
|
101 |
private FullUserProfile createFullUserProfile(Long id) { |
|
102 |
FullUserProfile user = new FullUserProfile(); |
|
103 |
|
|
104 |
user.setId(id); |
|
105 |
user.setFirstName("Tomas"); |
|
106 |
user.setLastName("Novak"); |
|
107 |
user.setPhoto("https://st2.depositphotos.com/9223672/12056/v/950/depositphotos_120568236-stock-illustration-male-face-avatar-logo-template.jpg"); |
|
108 |
user.setStatus(Status.ACCEPTED); |
|
109 |
|
|
110 |
if (userSettings.containsKey(id)) { |
|
111 |
UserSettings settings = userSettings.get(id); |
|
112 |
user.setVacationCount(settings.getVacationCount()); |
|
113 |
user.setSickdayCount(settings.getSickdayCount()); |
|
114 |
user.setRole(settings.getRole()); |
|
115 |
} else { |
|
116 |
user.setVacationCount(8.5F); |
|
117 |
user.setSickdayCount(3); |
|
118 |
user.setRole(UserRole.EMPLOYER); |
|
119 |
} |
|
120 |
|
|
121 |
user.setNotification(LocalDateTime.of(2000, 12, 1, 9, 0)); |
|
122 |
|
|
123 |
return user; |
|
124 |
} |
|
125 |
|
|
126 |
private DefaultSettings createDefaultSettings() { |
|
127 |
DefaultSettings settings = new DefaultSettings(); |
|
128 |
|
|
129 |
settings.setSickdayCount(3); |
|
130 |
settings.setNotification(LocalDateTime.now()); |
|
131 |
|
|
132 |
return settings; |
|
133 |
} |
|
134 |
|
|
135 |
@Override |
|
136 |
public List<BasicProfileUser> getUsers(Status status) throws RESTFullException { |
|
137 |
int count = 5; |
|
138 |
List<BasicProfileUser> users = new ArrayList<>(count); |
|
139 |
for (int i = 1; i <= count; i++) { |
|
140 |
users.add(createBasicProfileUser(i, status)); |
|
141 |
} |
|
142 |
return users; |
|
143 |
} |
|
144 |
|
|
145 |
@Override |
|
146 |
public List<VacationRequest> getVacationRequests(Status status) throws RESTFullException { |
|
147 |
int count = 5; |
|
148 |
List<VacationRequest> requests = new ArrayList<>(count); |
|
149 |
int length = VacationType.values().length; |
|
150 |
for (int i = 1; i <= count; i++) { |
|
151 |
requests.add(createVacationRequest(i, VacationType.values()[i % length], status)); |
|
152 |
} |
|
153 |
return requests; |
|
154 |
} |
|
155 |
|
|
156 |
@Override |
|
157 |
public List<AuthorizationRequest> getAuthorizationRequests(Status status) { |
|
158 |
int count = 5; |
|
159 |
List<AuthorizationRequest> requests = new ArrayList<>(count); |
|
160 |
for (int i = 1; i <= count; i++) { |
|
161 |
requests.add(createAuthorizationRequest(i, status)); |
|
162 |
} |
|
163 |
return requests; |
|
164 |
} |
|
165 |
|
|
166 |
@Override |
|
167 |
public FullUserProfile getUserProfile(Long userId) { |
|
168 |
return createFullUserProfile(userId); |
|
169 |
} |
|
170 |
|
|
171 |
@Override |
|
172 |
public DefaultSettings getDefaultSettings() { |
|
173 |
return settings.isEmpty() ? createDefaultSettings() : settings.get(settings.size()-1); |
|
174 |
} |
|
175 |
|
|
176 |
@Override |
|
177 |
public List<VacationDay> getUserCalendar(Long userId, LocalDate fromDate, LocalDate toDate, Status status) { |
|
178 |
if (vacations.containsKey(userId)) { |
|
179 |
List<VacationDay> vacationDays = vacations.get(userId); |
|
180 |
Predicate<VacationDay> predicate = (day) -> day.getDate().isAfter(fromDate); |
|
181 |
predicate = predicate.or((day) -> day.getDate().isEqual(fromDate)); |
|
182 |
if (toDate != null) { |
|
183 |
predicate = predicate.and((day) -> day.getDate().isBefore(toDate)).or((day) -> day.getDate().isEqual(toDate)); |
|
184 |
} |
|
185 |
return vacationDays.stream().filter(predicate).collect(Collectors.toList()); |
|
186 |
|
|
187 |
} else { |
|
188 |
int daysDifference = toDate == null ? 5 : Period.between(fromDate, toDate).getDays(); |
|
189 |
List<VacationDay> days = new ArrayList<>(daysDifference); |
|
190 |
for (int i = 0; i < daysDifference; i++) { |
|
191 |
days.add(createVacationDay(i + 1, fromDate.plusDays(i), VacationType.values()[i % 2], status)); |
|
192 |
} |
|
193 |
return days; |
|
194 |
} |
|
195 |
} |
|
196 |
|
|
197 |
@Override |
|
198 |
public void createSettings(DefaultSettings sett) { |
|
199 |
settings.add(sett); |
|
200 |
} |
|
201 |
|
|
202 |
@Override |
|
203 |
public void createVacation(Long userId, VacationDay vacationDay) { |
|
204 |
if (!vacations.containsKey(userId)) { |
|
205 |
vacations.put(userId, new ArrayList<>()); |
|
206 |
} |
|
207 |
|
|
208 |
List<VacationDay> days = vacations.get(userId); |
|
209 |
days.add(vacationDay); |
|
210 |
vacationDay.setId((long)days.size()); |
|
211 |
} |
|
212 |
|
|
213 |
@Override |
|
214 |
public void changeSettings(Long userId, UserSettings settings) { |
|
215 |
userSettings.put(userId, settings); |
|
216 |
} |
|
217 |
|
|
218 |
@Override |
|
219 |
public void changeVacation(Long userId, VacationDay vacationDay) throws RESTFullException { |
|
220 |
if (vacationDay.getId() == null) { |
|
221 |
throw new RESTFullException("Vacation does not contain ID.", "rest.exception.generic"); |
|
222 |
} |
|
223 |
|
|
224 |
if (!vacations.containsKey(userId)) { |
|
225 |
throw new RESTFullException("User does not exist.", "rest.exception.generic"); |
|
226 |
} |
|
227 |
|
|
228 |
List<VacationDay> vacs = vacations.get(userId); |
|
229 |
boolean founded = false; |
|
230 |
for (int i = 0; i < vacs.size(); i++) { |
|
231 |
if (vacs.get(i).getId() == vacationDay.getId()) { |
|
232 |
VacationDay day = vacs.get(i); |
|
233 |
day.setStatus(vacationDay.getStatus()); |
|
234 |
day.setFrom(vacationDay.getFrom()); |
|
235 |
day.setTo(vacationDay.getTo()); |
|
236 |
day.setDate(vacationDay.getDate()); |
|
237 |
day.setType(vacationDay.getType()); |
|
238 |
founded = true; |
|
239 |
break; |
|
240 |
} |
|
241 |
} |
|
242 |
|
|
243 |
if (!founded) { |
|
244 |
throw new RESTFullException("Vacation does not exist.", "rest.exception.generic"); |
|
245 |
} |
|
246 |
} |
|
247 |
|
|
248 |
@Override |
|
249 |
public void changeRequest(RequestType type, BasicRequest request) throws RESTFullException { |
|
250 |
|
|
251 |
if (request.getId() == null) { |
|
252 |
throw new RESTFullException("ID of request can not be empty.", "rest.exception.generic"); |
|
253 |
} |
|
254 |
|
|
255 |
switch (type) { |
|
256 |
case VACATION: { |
|
257 |
boolean found = false; |
|
258 |
for (List<VacationDay> value : vacations.values()) { |
|
259 |
for (VacationDay day : value) { |
|
260 |
if (day.getId() == request.getId()) { |
|
261 |
found = true; |
|
262 |
day.setStatus(request.getStatus()); |
|
263 |
break; |
|
264 |
} |
|
265 |
} |
|
266 |
if (found) break; |
|
267 |
} |
|
268 |
if (!found) { |
|
269 |
throw new RESTFullException("Vacation does not exist.", "rest.exception.generic"); |
|
270 |
} |
|
271 |
} break; |
|
272 |
case AUTHORIZATION: { |
|
273 |
throw new RESTFullException("User does not exist.", "rest.exception.generic"); |
|
274 |
} |
|
275 |
} |
|
276 |
} |
|
277 |
|
|
278 |
@Override |
|
279 |
public void deleteVacation(Long userId, Long vacationId) throws RESTFullException{ |
|
280 |
if (!vacations.containsKey(userId)) { |
|
281 |
throw new RESTFullException("User does not exist.", "rest.exception.generic"); |
|
282 |
} |
|
283 |
|
|
284 |
List<VacationDay> vacs = vacations.get(userId); |
|
285 |
if (!vacs.removeIf(day -> day.getId() == vacationId)) { |
|
286 |
throw new RESTFullException("Vacation does not exist for the user.", "rest.exception.generic"); |
|
287 |
} |
|
288 |
} |
|
289 |
} |
server/src/main/java/cz/zcu/yamanager/ws/rest/ApiController.java | ||
---|---|---|
1 | 1 |
package cz.zcu.yamanager.ws.rest; |
2 | 2 |
|
3 | 3 |
import com.fasterxml.jackson.databind.ObjectMapper; |
4 |
import cz.zcu.yamanager.business.FileService; |
|
5 |
import cz.zcu.yamanager.business.Manager; |
|
4 | 6 |
import cz.zcu.yamanager.dto.BasicRequest; |
5 | 7 |
import cz.zcu.yamanager.dto.DefaultSettings; |
6 | 8 |
import cz.zcu.yamanager.dto.UserSettings; |
7 | 9 |
import cz.zcu.yamanager.dto.VacationDay; |
8 |
import cz.zcu.yamanager.manager.Manager; |
|
9 | 10 |
import cz.zcu.yamanager.util.localization.Language; |
10 | 11 |
import cz.zcu.yamanager.util.localization.Message; |
11 | 12 |
import org.apache.commons.lang3.StringUtils; |
12 | 13 |
import org.slf4j.Logger; |
13 | 14 |
import org.slf4j.LoggerFactory; |
14 | 15 |
import org.springframework.beans.factory.annotation.Autowired; |
16 |
import org.springframework.http.HttpHeaders; |
|
15 | 17 |
import org.springframework.http.MediaType; |
16 | 18 |
import org.springframework.http.ResponseEntity; |
17 | 19 |
import org.springframework.web.bind.annotation.*; |
20 |
import org.springframework.web.multipart.MultipartFile; |
|
18 | 21 |
|
19 | 22 |
import java.time.LocalDate; |
20 | 23 |
import java.time.format.DateTimeFormatter; |
24 |
import java.util.Arrays; |
|
21 | 25 |
import java.util.HashMap; |
26 |
import java.util.Map; |
|
27 |
import java.util.function.Function; |
|
22 | 28 |
|
23 | 29 |
import static cz.zcu.yamanager.dto.RequestType.getType; |
24 | 30 |
import static cz.zcu.yamanager.dto.Status.getStatus; |
... | ... | |
34 | 40 |
|
35 | 41 |
private final Manager manager; |
36 | 42 |
|
43 |
private final FileService fileService; |
|
44 |
|
|
37 | 45 |
@Autowired |
38 |
public ApiController(Manager manager) { |
|
46 |
public ApiController(Manager manager, FileService fileService) {
|
|
39 | 47 |
this.manager = manager; |
48 |
this.fileService = fileService; |
|
40 | 49 |
} |
41 | 50 |
|
42 | 51 |
private ResponseEntity sendError(Integer errorCode, String messageKey, Language language) { |
43 | 52 |
String localizedMessage = Message.getString(language, messageKey); |
44 |
String msg = String.format("{\"error\": %s,\"message\": \"%s\"}", errorCode, localizedMessage); |
|
45 |
return ResponseEntity.status(errorCode).contentType(MediaType.APPLICATION_JSON).body(msg); |
|
53 |
Map<String, String> result = new HashMap<>(); |
|
54 |
result.put("error", errorCode.toString()); |
|
55 |
result.put("message", localizedMessage); |
|
56 |
return ResponseEntity.status(errorCode).contentType(MediaType.APPLICATION_JSON).body(result); |
|
46 | 57 |
} |
47 | 58 |
|
48 |
private <T> ResponseEntity handle(Language language, RESTGetHandler<T> handler) {
|
|
59 |
private <T> ResponseEntity handle(Language language, RESTInvokeHandler<T> handler) {
|
|
49 | 60 |
try { |
50 |
return ok(handler.get()); |
|
61 |
handler.invoke(); |
|
62 |
return ok(OK); |
|
51 | 63 |
} catch (RESTFullException e) { |
52 | 64 |
log.error(e.getMessage()); |
53 | 65 |
return sendError(400, e.getLocalizedMessage(), language); |
... | ... | |
57 | 69 |
} |
58 | 70 |
} |
59 | 71 |
|
60 |
private <T> ResponseEntity handle(Language language, RESTInvokeHandler<T> handler) { |
|
72 |
private <T> ResponseEntity handle(Language language, RESTGetHandler<T> handler) { |
|
73 |
return handleWithHeader(language, handler, null, null); |
|
74 |
} |
|
75 |
|
|
76 |
private <T> ResponseEntity handleWithHeader(Language language, RESTGetHandler<T> handler, Function<T, String[]> header, Function<T, Object> bodyValue) { |
|
61 | 77 |
try { |
62 |
handler.invoke(); |
|
63 |
return ok(OK); |
|
78 |
T result = handler.get(); |
|
79 |
|
|
80 |
ResponseEntity.BodyBuilder response = ResponseEntity.ok(); |
|
81 |
|
|
82 |
if (header != null) { |
|
83 |
String[] headers = header.apply(result); |
|
84 |
|
|
85 |
if (headers.length > 1) { |
|
86 |
response.header(headers[0], Arrays.copyOfRange(headers, 1, headers.length - 1)); |
|
87 |
} else if (headers.length == 1) { |
|
88 |
response.header(headers[0]); |
|
89 |
} |
|
90 |
} |
|
91 |
|
|
92 |
return response.body(bodyValue != null ? bodyValue.apply(result) : result); |
|
93 |
|
|
64 | 94 |
} catch (RESTFullException e) { |
65 | 95 |
log.error(e.getMessage()); |
66 | 96 |
return sendError(400, e.getLocalizedMessage(), language); |
... | ... | |
216 | 246 |
manager.deleteVacation(getUserId("me"), vacationId); |
217 | 247 |
}); |
218 | 248 |
} |
249 |
|
|
250 |
// *********************** FILE **************************** |
|
251 |
|
|
252 |
@RequestMapping(value = "/import/xls", method=POST) |
|
253 |
public ResponseEntity importXLSFile( |
|
254 |
@RequestParam(value = "lang", required = false) String lang, |
|
255 |
@RequestParam("file") MultipartFile file) |
|
256 |
{ |
|
257 |
return handle(getLanguage(lang), () -> |
|
258 |
fileService.parseXLSFile(file.getOriginalFilename(), file.getBytes()) |
|
259 |
); |
|
260 |
} |
|
261 |
|
|
262 |
@RequestMapping(value = "/export/pdf", method=GET) |
|
263 |
public ResponseEntity exportPDFFile( |
|
264 |
@RequestParam(value = "lang", required = false) String lang) |
|
265 |
{ |
|
266 |
return handleWithHeader(getLanguage(lang), |
|
267 |
() -> fileService.createPDFFile(), |
|
268 |
(res) -> new String[]{HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + res.getName() + "\""}, |
|
269 |
(res) -> res.getBytes() |
|
270 |
); |
|
271 |
} |
|
219 | 272 |
} |
server/src/test/java/cz/zcu/yamanager/ws/rest/TestControllerTest.java | ||
---|---|---|
17 | 17 |
.addLanguage(Language.EN) |
18 | 18 |
.addLanguage(Language.CZ); |
19 | 19 |
|
20 |
testController = new TestController(null); |
|
21 |
} |
|
22 |
|
|
23 |
@Test |
|
24 |
void hello_inCzech_true() { |
|
25 |
assertEquals("český jazyk", testController.hello("cz")); |
|
20 |
testController = new TestController(); |
|
26 | 21 |
} |
27 | 22 |
|
28 | 23 |
@Test |
29 | 24 |
void hello_inEnglish_true() { |
30 |
assertEquals("english language", testController.hello("en"));
|
|
25 |
assertEquals("english language", testController.hello()); |
|
31 | 26 |
} |
32 | 27 |
} |
Také k dispozici: Unified diff
Re #7491 Implemented API for import/export with mocked PDF and XLS processing