-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShipServiceImpl.java
More file actions
340 lines (297 loc) · 11.2 KB
/
ShipServiceImpl.java
File metadata and controls
340 lines (297 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package com.space.service;
import com.space.exceptions.BadRequestException;
import com.space.exceptions.NotFoundException;
import com.space.model.Ship;
import com.space.model.ShipType;
import com.space.repository.ShipRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
@Service
@Transactional
public class ShipServiceImpl implements ShipService {
@Autowired
ShipRepository shipRepository;
@Override
public Page<Ship> getShipsList(Specification<Ship> specification, Pageable sortedBy) {
return shipRepository.findAll(specification, sortedBy);
}
@Override
public Integer getShipsCount(Specification<Ship> specification) {
return shipRepository.findAll(specification).size();
}
@Override
public Ship createShip(Ship ship) {
if (ship.getName() == null || ship.getPlanet() == null
|| ship.getShipType() == null || ship.getProdDate() == null
|| ship.getSpeed() == null || ship.getCrewSize() == null) {
throw new BadRequestException();
}
checkShipName(ship);
checkShipPlanet(ship);
checkShipProdDate(ship);
checkShipSpeed(ship);
checkShipCrewSize(ship);
if (ship.getUsed() == null) {
ship.setUsed(false);
}
Double rating = computeRating(ship);
ship.setRating(rating);
return shipRepository.save(ship);
}
@Override
public Ship updateShip(Long id, Ship ship) {
Ship updatedShip = getShip(id);
String name = ship.getName();
if (name != null) {
checkShipName(ship);
updatedShip.setName(name);
}
String planet = ship.getPlanet();
if (planet != null) {
checkShipPlanet(ship);
updatedShip.setPlanet(planet);
}
ShipType shipType = ship.getShipType();
if (shipType != null) {
updatedShip.setShipType(shipType);
}
Date prodDate = ship.getProdDate();
if (prodDate != null) {
checkShipProdDate(ship);
updatedShip.setProdDate(prodDate);
}
Boolean isUsed = ship.getUsed();
if (isUsed != null) {
updatedShip.setUsed(isUsed);
}
Double speed = ship.getSpeed();
if (speed != null) {
checkShipSpeed(ship);
updatedShip.setSpeed(speed);
}
Integer crewSize = ship.getCrewSize();
if (crewSize != null) {
checkShipCrewSize(ship);
updatedShip.setCrewSize(crewSize);
}
Double rating = computeRating(updatedShip);
updatedShip.setRating(rating);
return shipRepository.save(updatedShip);
}
private void checkShipName(Ship ship) {
String name = ship.getName();
if (name.length() > 50 || name.length() < 1) {
throw new BadRequestException();
}
}
private void checkShipPlanet(Ship ship) {
String planet = ship.getPlanet();
if (planet.length() > 50 || planet.length() < 1) {
throw new BadRequestException();
}
}
private void checkShipProdDate(Ship ship) {
Date prodDate = ship.getProdDate();
Calendar calendar = new GregorianCalendar();
calendar.setTime(prodDate);
int year = calendar.get(Calendar.YEAR);
if (year > 3019 || year < 2800) {
throw new BadRequestException();
}
}
private void checkShipSpeed(Ship ship) {
Double speed = ship.getSpeed();
if (speed > 0.99 || speed < 0.01) {
throw new BadRequestException();
}
}
private void checkShipCrewSize(Ship ship) {
Integer crewSize = ship.getCrewSize();
if (crewSize > 9999 || crewSize < 1) {
throw new BadRequestException();
}
}
private Double computeRating(Ship ship) {
double k = ship.getUsed() ? 0.5 : 1;
Calendar calendar = new GregorianCalendar();
calendar.setTime(ship.getProdDate());
int prodYear = calendar.get(Calendar.YEAR);
BigDecimal rating = BigDecimal.valueOf((80 * ship.getSpeed() * k) / (3019 - prodYear + 1));
return rating.setScale(2, RoundingMode.HALF_UP).doubleValue();
}
@Override
public Ship getShip(Long id) {
if (!shipRepository.existsById(id)) {
throw new NotFoundException();
}
return shipRepository.findById(id).get();
}
@Override
public void deleteShip(Long id) {
if (!shipRepository.existsById(id)) {
throw new NotFoundException();
}
shipRepository.deleteById(id);
}
@Override
public Specification<Ship> nameFilter(String name) {
return new Specification<Ship>() {
@Override
public Predicate toPredicate(Root<Ship> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
if (name == null) {
return null;
}
return criteriaBuilder.like(root.get("name"), "%" + name + "%");
}
};
}
@Override
public Specification<Ship> planetFilter(String planet) {
return new Specification<Ship>() {
@Override
public Predicate toPredicate(Root<Ship> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
if (planet == null) {
return null;
}
return criteriaBuilder.like(root.get("planet"), "%" + planet + "%");
}
};
}
@Override
public Specification<Ship> shipTypeFilter(ShipType shipType) {
return new Specification<Ship>() {
@Override
public Predicate toPredicate(Root<Ship> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
if (shipType == null) {
return null;
}
return criteriaBuilder.equal(root.get("shipType"), shipType);
}
};
}
@Override
public Specification<Ship> prodDateFilter(Long after, Long before) {
return new Specification<Ship>() {
@Override
public Predicate toPredicate(Root<Ship> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
if (after == null && before == null) {
return null;
}
if (after == null) {
Date tempBefore = new Date(before);
return criteriaBuilder.lessThanOrEqualTo(root.get("prodDate"), tempBefore);
}
if (before == null) {
Date tempAfter = new Date(after);
return criteriaBuilder.greaterThanOrEqualTo(root.get("prodDate"), tempAfter);
}
Calendar beforeCalendar = new GregorianCalendar();
beforeCalendar.setTime(new Date(before));
beforeCalendar.set(Calendar.HOUR, 0);
beforeCalendar.add(Calendar.MILLISECOND, -1);
Date tempAfter = new Date(after);
Date tempBefore = beforeCalendar.getTime();
return criteriaBuilder.between(root.get("prodDate"), tempAfter, tempBefore);
}
};
}
@Override
public Specification<Ship> isUseFilter(Boolean isUsed) {
return new Specification<Ship>() {
@Override
public Predicate toPredicate(Root<Ship> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
if (isUsed == null) {
return null;
}
if (isUsed) {
return criteriaBuilder.isTrue(root.get("isUsed"));
} else {
return criteriaBuilder.isFalse(root.get("isUsed"));
}
}
};
}
@Override
public Specification<Ship> speedFilter(Double minSpeed, Double maxSpeed) {
return new Specification<Ship>() {
@Override
public Predicate toPredicate(Root<Ship> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
if (minSpeed == null && maxSpeed == null) {
return null;
}
if (minSpeed == null) {
return criteriaBuilder.lessThanOrEqualTo(root.get("speed"), maxSpeed);
}
if (maxSpeed == null) {
return criteriaBuilder.greaterThanOrEqualTo(root.get("speed"), minSpeed);
}
return criteriaBuilder.between(root.get("speed"), minSpeed, maxSpeed);
}
};
}
@Override
public Specification<Ship> crewSizeFilter(Integer minCrewSize, Integer maxCrewSize) {
return new Specification<Ship>() {
@Override
public Predicate toPredicate(Root<Ship> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
if (minCrewSize == null && maxCrewSize == null) {
return null;
}
if (minCrewSize == null) {
return criteriaBuilder.lessThanOrEqualTo(root.get("crewSize"), maxCrewSize);
}
if (maxCrewSize == null) {
return criteriaBuilder.greaterThanOrEqualTo(root.get("crewSize"), minCrewSize);
}
return criteriaBuilder.between(root.get("crewSize"), minCrewSize, maxCrewSize);
}
};
}
@Override
public Specification<Ship> ratingFilter(Double minRating, Double maxRating) {
return new Specification<Ship>() {
@Override
public Predicate toPredicate(Root<Ship> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
if (minRating == null && maxRating == null) {
return null;
}
if (minRating == null) {
return criteriaBuilder.lessThanOrEqualTo(root.get("rating"), maxRating);
}
if (maxRating == null) {
return criteriaBuilder.greaterThanOrEqualTo(root.get("rating"), minRating);
}
return criteriaBuilder.between(root.get("rating"), minRating, maxRating);
}
};
}
@Override
public Long idToLongWithChecking(String id) {
Long longId = null;
if (id == null || id.equals("") || id.equals("0")) {
throw new BadRequestException();
}
try {
longId = Long.parseLong(id);
} catch (NumberFormatException e) {
throw new BadRequestException();
}
if (longId < 0) {
throw new BadRequestException();
}
return longId;
}
}