<commit_before>package de.rwth.idsg.bikeman.ixsi.processor.query.user;

import com.google.common.base.Optional;
import de.rwth.idsg.bikeman.domain.Booking;
import de.rwth.idsg.bikeman.ixsi.ErrorFactory;
import de.rwth.idsg.bikeman.ixsi.IxsiProcessingException;
import de.rwth.idsg.bikeman.ixsi.processor.api.UserRequestProcessor;
import de.rwth.idsg.bikeman.ixsi.service.AvailabilityPushService;
import de.rwth.idsg.bikeman.ixsi.service.BookingService;
import de.rwth.idsg.bikeman.web.rest.exception.DatabaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xjc.schema.ixsi.BookingType;
import xjc.schema.ixsi.ChangeBookingRequestType;
import xjc.schema.ixsi.ChangeBookingResponseType;
import xjc.schema.ixsi.ErrorType;
import xjc.schema.ixsi.Language;
import xjc.schema.ixsi.TimePeriodType;
import xjc.schema.ixsi.UserInfoType;

/**
 * @author <NAME> <<EMAIL>>
 * @since 26.09.2014
 */
@Component
public class ChangeBookingRequestProcessor implements
        UserRequestProcessor<ChangeBookingRequestType, ChangeBookingResponseType> {

    @Autowired private BookingService bookingService;
    @Autowired private AvailabilityPushService availabilityPushService;

    @Override
    public ChangeBookingResponseType processAnonymously(ChangeBookingRequestType request, Optional<Language> lan) {
        return buildError(ErrorFactory.Auth.notAnonym("Anonymous change booking request not allowed", null));
    }

    @Override
    public ChangeBookingResponseType processForUser(ChangeBookingRequestType request, Optional<Language> lan,
                                                    UserInfoType userInfo) {
        try {
            if (request.isSetCancel() && request.isCancel()) {
                return proceedCancel(request);

            } else {
                return proceedChange(request);
            }
        } catch (DatabaseException e) {
            return buildError(ErrorFactory.Booking.idUnknown(e.getMessage(), null));

        } catch (IxsiProcessingException e) {
            return buildError(ErrorFactory.Booking.changeNotPossible(e.getMessage(), e.getMessage()));
        }
    }

    private ChangeBookingResponseType proceedChange(ChangeBookingRequestType request) {
        Booking oldBooking = bookingService.get(request.getBookingID());
        TimePeriodType oldTimePeriod = buildTimePeriod(oldBooking);

        Booking newBooking = bookingService.update(oldBooking, request.getNewTimePeriodProposal());
        TimePeriodType newTimePeriod = buildTimePeriod(newBooking);

        BookingType responseBooking = new BookingType()
            .withID(newBooking.getIxsiBookingId())
            .withTimePeriod(newTimePeriod);

        String placeId = newBooking.getReservation()
                                   .getPedelec()
                                   .getStationSlot()
                                   .getStation()
                                   .getManufacturerId();

        availabilityPushService.changedBooking(request.getBookingID(), placeId, oldTimePeriod, newTimePeriod);
        return new ChangeBookingResponseType().withBooking(responseBooking);
    }

    private ChangeBookingResponseType proceedCancel(ChangeBookingRequestType request) {
        Booking booking = bookingService.get(request.getBookingID());
        bookingService.cancel(booking);

        TimePeriodType timePeriod = buildTimePeriod(booking);

        String placeId = booking.getReservation()
                                .getPedelec()
                                .getStationSlot()
                                .getStation()
                                .getManufacturerId();

        availabilityPushService.cancelledBooking(request.getBookingID(), placeId, timePeriod);
        return new ChangeBookingResponseType();
    }

    private TimePeriodType buildTimePeriod(Booking booking) {
        return new TimePeriodType()
            .withBegin(booking.getReservation().getStartDateTime().toDateTime())
            .withEnd(booking.getReservation().getEndDateTime().toDateTime());
    }

    // -------------------------------------------------------------------------
    // Error handling
    // -------------------------------------------------------------------------

    @Override
    public ChangeBookingResponseType buildError(ErrorType e) {
        return new ChangeBookingResponseType().withError(e);
    }
}
<commit_msg>change/cancel are transactional executions<commit_after>package de.rwth.idsg.bikeman.ixsi.processor.query.user;

import com.google.common.base.Optional;
import de.rwth.idsg.bikeman.domain.Booking;
import de.rwth.idsg.bikeman.ixsi.ErrorFactory;
import de.rwth.idsg.bikeman.ixsi.IxsiProcessingException;
import de.rwth.idsg.bikeman.ixsi.processor.api.UserRequestProcessor;
import de.rwth.idsg.bikeman.ixsi.service.AvailabilityPushService;
import de.rwth.idsg.bikeman.ixsi.service.BookingService;
import de.rwth.idsg.bikeman.web.rest.exception.DatabaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import xjc.schema.ixsi.BookingType;
import xjc.schema.ixsi.ChangeBookingRequestType;
import xjc.schema.ixsi.ChangeBookingResponseType;
import xjc.schema.ixsi.ErrorType;
import xjc.schema.ixsi.Language;
import xjc.schema.ixsi.TimePeriodType;
import xjc.schema.ixsi.UserInfoType;

/**
 * @author <NAME> <<EMAIL>>
 * @since 26.09.2014
 */
@Component
public class ChangeBookingRequestProcessor implements
        UserRequestProcessor<ChangeBookingRequestType, ChangeBookingResponseType> {

    @Autowired private BookingService bookingService;
    @Autowired private AvailabilityPushService availabilityPushService;

    @Override
    public ChangeBookingResponseType processAnonymously(ChangeBookingRequestType request, Optional<Language> lan) {
        return buildError(ErrorFactory.Auth.notAnonym("Anonymous change booking request not allowed", null));
    }

    @Override
    public ChangeBookingResponseType processForUser(ChangeBookingRequestType request, Optional<Language> lan,
                                                    UserInfoType userInfo) {
        try {
            if (request.isSetCancel() && request.isCancel()) {
                return proceedCancel(request);

            } else {
                return proceedChange(request);
            }
        } catch (DatabaseException e) {
            return buildError(ErrorFactory.Booking.idUnknown(e.getMessage(), null));

        } catch (IxsiProcessingException e) {
            return buildError(ErrorFactory.Booking.changeNotPossible(e.getMessage(), e.getMessage()));
        }
    }

    @Transactional
    private ChangeBookingResponseType proceedChange(ChangeBookingRequestType request) {
        Booking oldBooking = bookingService.get(request.getBookingID());
        TimePeriodType oldTimePeriod = buildTimePeriod(oldBooking);

        Booking newBooking = bookingService.update(oldBooking, request.getNewTimePeriodProposal());
        TimePeriodType newTimePeriod = buildTimePeriod(newBooking);

        BookingType responseBooking = new BookingType()
            .withID(newBooking.getIxsiBookingId())
            .withTimePeriod(newTimePeriod);

        String placeId = newBooking.getReservation()
                                   .getPedelec()
                                   .getStationSlot()
                                   .getStation()
                                   .getManufacturerId();

        availabilityPushService.changedBooking(request.getBookingID(), placeId, oldTimePeriod, newTimePeriod);
        return new ChangeBookingResponseType().withBooking(responseBooking);
    }

    @Transactional
    private ChangeBookingResponseType proceedCancel(ChangeBookingRequestType request) {
        Booking booking = bookingService.get(request.getBookingID());
        bookingService.cancel(booking);

        TimePeriodType timePeriod = buildTimePeriod(booking);

        String placeId = booking.getReservation()
                                .getPedelec()
                                .getStationSlot()
                                .getStation()
                                .getManufacturerId();

        availabilityPushService.cancelledBooking(request.getBookingID(), placeId, timePeriod);
        return new ChangeBookingResponseType();
    }

    private TimePeriodType buildTimePeriod(Booking booking) {
        return new TimePeriodType()
            .withBegin(booking.getReservation().getStartDateTime().toDateTime())
            .withEnd(booking.getReservation().getEndDateTime().toDateTime());
    }

    // -------------------------------------------------------------------------
    // Error handling
    // -------------------------------------------------------------------------

    @Override
    public ChangeBookingResponseType buildError(ErrorType e) {
        return new ChangeBookingResponseType().withError(e);
    }
}

{'fraction_non_alphanumeric': 0.11763407636283381, 'fraction_numerical': 0.0017656146546016333, 'mean_word_length': 2.612196094061379, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5017249', 'n_tokens_mistral': 2369, 'n_tokens_neox': 2298, 'n_words': 369}