Skip to content

Commit

Permalink
[SERVER] fix DataIntegrityViolationException onCreateParkingLot
Browse files Browse the repository at this point in the history
  • Loading branch information
Huynh Thanh Binh committed Aug 3, 2020
1 parent f15b6e6 commit b3dc5f9
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import com.bht.saigonparking.api.grpc.booking.BookingServiceGrpc;
import com.bht.saigonparking.api.grpc.booking.BookingStatus;
import com.bht.saigonparking.api.grpc.booking.UpdateBookingStatusRequest;
import com.bht.saigonparking.api.grpc.contact.BookingRequestContent;
import com.bht.saigonparking.api.grpc.contact.SaigonParkingMessage;
import com.bht.saigonparking.api.grpc.contact.TextMessageContent;
import com.bht.saigonparking.api.grpc.parkinglot.ParkingLot;
import com.bht.saigonparking.api.grpc.parkinglot.ParkingLotInformation;
import com.bht.saigonparking.api.grpc.parkinglot.ParkingLotServiceGrpc;
import com.bht.saigonparking.api.grpc.parkinglot.ParkingLotType;
import com.bht.saigonparking.emulator.configuration.SpringApplicationContext;
import com.bht.saigonparking.emulator.handler.WebSocketHandler;
import com.neovisionaries.ws.client.WebSocket;
Expand Down Expand Up @@ -65,7 +66,7 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc
private static void runTest() throws ExecutionException, InterruptedException, IOException, WebSocketException {
// testAuthWithWebSocketUri();
// testAuthWithWebSocketWebUri();
testNewSocketLibrary();
// testNewSocketLibrary();

// Thread.sleep(5000);
//
Expand All @@ -92,6 +93,30 @@ private static void runTest() throws ExecutionException, InterruptedException, I
// .setStatus(BookingStatus.REJECTED)
// .setTimestamp(new Timestamp(System.currentTimeMillis()).toString())
// .build());

// System.out.println(SpringApplicationContext.getBean(BookingServiceGrpc.BookingServiceBlockingStub.class)
// .createBooking(CreateBookingRequest.newBuilder()
// .setCustomerId(4)
// .setParkingLotId(4)
// .setLicensePlate("59H1-762.17")
// .build()));

SpringApplicationContext.getBean(ParkingLotServiceGrpc.ParkingLotServiceBlockingStub.class)
.createNewParkingLot(ParkingLot.newBuilder()
.setType(ParkingLotType.BUILDING)
.setLatitude(10.762622)
.setLongitude(106.660172)
.setOpeningHour("00:00:00")
.setClosingHour("20:00:00")
.setAvailableSlot(100)
.setTotalSlot(100)
.setInformation(ParkingLotInformation.newBuilder()
.setName("BX Test")
.setAddress("227 Nguyen Van Cu")
.setPhone("0123456789")
.build())
.build())
.getValue();
}

private static void testAuthWithWebSocketUri() throws ExecutionException, InterruptedException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ public final class ParkingLotEntity extends BaseEntity {
@Column(name = "[IS_AVAILABLE]")
private Boolean isAvailable;

@OneToOne(mappedBy = "parkingLotEntity", cascade = CascadeType.ALL, orphanRemoval = true, optional = false)
@OneToOne(mappedBy = "parkingLotEntity", cascade = CascadeType.ALL, orphanRemoval = true)
private ParkingLotLimitEntity parkingLotLimitEntity;

@OneToOne(mappedBy = "parkingLotEntity", cascade = CascadeType.ALL, orphanRemoval = true, optional = false)
@OneToOne(mappedBy = "parkingLotEntity", cascade = CascadeType.ALL, orphanRemoval = true)
private ParkingLotInformationEntity parkingLotInformationEntity;

@ToString.Exclude
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.bht.saigonparking.service.parkinglot.configuration.AppConfiguration;
import com.bht.saigonparking.service.parkinglot.entity.ParkingLotEntity;
import com.bht.saigonparking.service.parkinglot.entity.ParkingLotInformationEntity;
import com.bht.saigonparking.service.parkinglot.entity.ParkingLotLimitEntity;

/**
*
Expand Down Expand Up @@ -127,6 +128,27 @@ public interface ParkingLotMapper {
ParkingLotRating toParkingLotRating(@NotNull Map.Entry<Tuple, String> parkingLotRatingTupleEntry);


@Named("toParkingLotLimitEntityIgnoreParkingLotEntity")
@Mapping(target = "parkingLotEntity", ignore = true)
@Mapping(target = "id", ignore = true)
@Mapping(target = "version", ignore = true)
@Mapping(target = "totalSlot", source = "totalSlot", defaultExpression = "java(customizedMapper.DEFAULT_SHORT_VALUE)")
@Mapping(target = "availableSlot", source = "availableSlot", defaultExpression = "java(customizedMapper.DEFAULT_SHORT_VALUE)")
ParkingLotLimitEntity toParkingLotLimitEntityIgnoreParkingLotEntity(@NotNull ParkingLot parkingLot);


@Named("toParkingLotInformationEntityIgnoreParkingLotEntity")
@Mapping(target = "parkingLotEntity", ignore = true)
@Mapping(target = "id", ignore = true)
@Mapping(target = "version", ignore = true)
@Mapping(target = "ratingAverage", ignore = true)
@Mapping(target = "nRating", ignore = true)
@Mapping(target = "name", source = "information.name", defaultExpression = "java(customizedMapper.DEFAULT_STRING_VALUE)")
@Mapping(target = "phone", source = "information.phone", defaultExpression = "java(customizedMapper.DEFAULT_STRING_VALUE)")
@Mapping(target = "address", source = "information.address", defaultExpression = "java(customizedMapper.DEFAULT_STRING_VALUE)")
ParkingLotInformationEntity toParkingLotInformationEntityIgnoreParkingLotEntity(@NotNull ParkingLot parkingLot);


@Named("toParkingLotResultListWithoutName")
default List<ParkingLotResult> toParkingLotResultListWithoutName(@NotNull List<Tuple> parkingLotWithoutNameTupleList) {
return parkingLotWithoutNameTupleList.stream().map(this::toParkingLotResultWithoutName).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
@AllArgsConstructor(onConstructor = @__(@Autowired))
public final class ParkingLotMapperExtImpl implements ParkingLotMapperExt {

private final EnumMapper enumMapper;
private final CustomizedMapper customizedMapper;
private final ParkingLotRepository parkingLotRepository;

Expand All @@ -36,36 +37,35 @@ public final ParkingLotEntity toParkingLotEntity(@NotNull ParkingLot parkingLot,
ParkingLotInformation parkingLotInformation = parkingLot.getInformation();

if (!isAboutToCreate) {

/* case: update parkingLotEntity */
parkingLotEntity = parkingLotRepository.getById(parkingLot.getId());
parkingLotLimitEntity = parkingLotEntity.getParkingLotLimitEntity();
parkingLotInformationEntity = parkingLotEntity.getParkingLotInformationEntity();
parkingLotEntity.setVersion(parkingLot.getVersion());

parkingLotLimitEntity.setTotalSlot((short) parkingLot.getTotalSlot());
parkingLotLimitEntity.setAvailableSlot((short) parkingLot.getAvailableSlot());

parkingLotInformationEntity.setName(parkingLotInformation.getName());
parkingLotInformationEntity.setAddress(parkingLotInformation.getAddress());
parkingLotInformationEntity.setPhone(parkingLotInformation.getPhone().isEmpty() ? "" : parkingLotInformation.getPhone());

} else {

/* case: create parkingLotEntity */
parkingLotEntity = new ParkingLotEntity();
parkingLotLimitEntity = new ParkingLotLimitEntity();
parkingLotInformationEntity = new ParkingLotInformationEntity();

parkingLotEntity.setVersion(1L);
parkingLotEntity.setParkingLotUnitEntitySet(Collections.emptySet());
parkingLotEntity.setParkingLotRatingEntitySet(Collections.emptySet());
parkingLotEntity.setParkingLotEmployeeEntitySet(Collections.emptySet());
parkingLotEntity.setParkingLotTypeEntity(enumMapper.toParkingLotTypeEntity(parkingLot.getType()));
}

parkingLotLimitEntity.setTotalSlot((short) parkingLot.getTotalSlot());
parkingLotLimitEntity.setAvailableSlot((short) parkingLot.getAvailableSlot());

parkingLotInformationEntity.setName(parkingLotInformation.getName());
parkingLotInformationEntity.setAddress(parkingLotInformation.getAddress());
parkingLotInformationEntity.setPhone(parkingLotInformation.getPhone().isEmpty() ? "" : parkingLotInformation.getPhone());

parkingLotEntity.setLatitude(parkingLot.getLatitude());
parkingLotEntity.setLongitude(parkingLot.getLongitude());
parkingLotEntity.setOpeningHour(customizedMapper.toTime(parkingLot.getOpeningHour()));
parkingLotEntity.setClosingHour(customizedMapper.toTime(parkingLot.getClosingHour()));
parkingLotEntity.setParkingLotLimitEntity(parkingLotLimitEntity);
parkingLotEntity.setParkingLotInformationEntity(parkingLotInformationEntity);

return parkingLotEntity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,8 +685,10 @@ public void createNewParkingLot(ParkingLot request, StreamObserver<Int64Value> r
try {
serverInterceptor.validateAdmin();

Long newParkingLotId = parkingLotService
.createNewParkingLot(parkingLotMapperExt.toParkingLotEntity(request, true));
Long newParkingLotId = parkingLotService.createNewParkingLot(
parkingLotMapperExt.toParkingLotEntity(request, true),
parkingLotMapper.toParkingLotLimitEntityIgnoreParkingLotEntity(request),
parkingLotMapper.toParkingLotInformationEntityIgnoreParkingLotEntity(request));

responseObserver.onNext(Int64Value.of(newParkingLotId));
responseObserver.onCompleted();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.hibernate.validator.constraints.Range;

import com.bht.saigonparking.service.parkinglot.entity.ParkingLotEntity;
import com.bht.saigonparking.service.parkinglot.entity.ParkingLotInformationEntity;
import com.bht.saigonparking.service.parkinglot.entity.ParkingLotLimitEntity;
import com.bht.saigonparking.service.parkinglot.entity.ParkingLotTypeEntity;

Expand Down Expand Up @@ -102,7 +103,9 @@ List<Tuple> getTopParkingLotInRegionOrderByDistanceWithName(@NotNull Double lat,

Map<Long, Long> countAllParkingLotGroupByType();

Long createNewParkingLot(@NotNull ParkingLotEntity parkingLotEntity);
Long createNewParkingLot(@NotNull ParkingLotEntity parkingLotEntity,
@NotNull ParkingLotLimitEntity parkingLotLimitEntity,
@NotNull ParkingLotInformationEntity parkingLotInformationEntity);

void createNewRating(@NotNull Long parkingLotId, @NotNull Long customerId,
@NotNull Integer rating, @NotEmpty String comment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.bht.saigonparking.common.util.LoggingUtil;
import com.bht.saigonparking.service.parkinglot.entity.ParkingLotEmployeeEntity;
import com.bht.saigonparking.service.parkinglot.entity.ParkingLotEntity;
import com.bht.saigonparking.service.parkinglot.entity.ParkingLotInformationEntity;
import com.bht.saigonparking.service.parkinglot.entity.ParkingLotLimitEntity;
import com.bht.saigonparking.service.parkinglot.entity.ParkingLotRatingEntity;
import com.bht.saigonparking.service.parkinglot.entity.ParkingLotTypeEntity;
Expand Down Expand Up @@ -375,8 +376,19 @@ public String getParkingLotNameByParkingLotId(@NotNull Long parkingLotId) {
}

@Override
public Long createNewParkingLot(@NotNull ParkingLotEntity parkingLotEntity) {
return parkingLotRepository.saveAndFlush(parkingLotEntity).getId();
public Long createNewParkingLot(@NotNull ParkingLotEntity parkingLotEntity,
@NotNull ParkingLotLimitEntity parkingLotLimitEntity,
@NotNull ParkingLotInformationEntity parkingLotInformationEntity) {

ParkingLotEntity result = parkingLotRepository.saveAndFlush(parkingLotEntity);

parkingLotLimitEntity.setParkingLotEntity(result);
parkingLotLimitRepository.saveAndFlush(parkingLotLimitEntity);

parkingLotInformationEntity.setParkingLotEntity(result);
parkingLotInformationRepository.saveAndFlush(parkingLotInformationEntity);

return result.getId();
}

@Override
Expand Down

0 comments on commit b3dc5f9

Please sign in to comment.