Skip to content

Commit

Permalink
Merge pull request #229 from SAP-samples/java-8
Browse files Browse the repository at this point in the history
Made it compilable with Java 8
  • Loading branch information
rjayasinghe authored Jul 11, 2022
2 parents d75b3f9 + db5c138 commit fc1054e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
java-version: [11,17]
java-version: [8,11,17]

steps:
- uses: actions/checkout@v2
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<revision>1.0.0-SNAPSHOT</revision>

<!-- DEPENDENCIES VERSION -->
<jdk.version>11</jdk.version>
<jdk.version>8</jdk.version>
<cds.services.version>1.26.0</cds.services.version>
<spring.boot.version>2.7.1</spring.boot.version>

Expand Down Expand Up @@ -111,9 +111,9 @@
<requireMavenVersion>
<version>3.5.0</version>
</requireMavenVersion>
<requireJavaVersion>
<!-- <requireJavaVersion>
<version>${jdk.version}</version>
</requireJavaVersion>
</requireJavaVersion> -->
<requireProperty>
<property>project.artifactId</property>
<regex>[^_]+</regex>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package com.sap.cap.sflight.processor;

import java.util.List;
import static cds.gen.travelservice.TravelService_.TRAVEL;

import org.springframework.stereotype.Component;

import cds.gen.travelservice.AcceptTravelContext;
import cds.gen.travelservice.RejectTravelContext;
import cds.gen.travelservice.Travel;
import cds.gen.travelservice.TravelService_;
import cds.gen.travelservice.Travel_;
import com.sap.cds.ql.Update;
import com.sap.cds.services.cds.CdsService;
import com.sap.cds.ql.cqn.CqnUpdate;
import com.sap.cds.services.draft.DraftService;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.handler.annotations.After;
import com.sap.cds.services.handler.annotations.Before;
import com.sap.cds.services.handler.annotations.On;
import com.sap.cds.services.handler.annotations.ServiceName;
import com.sap.cds.services.persistence.PersistenceService;
import org.springframework.stereotype.Component;

import static cds.gen.travelservice.TravelService_.TRAVEL;
import cds.gen.travelservice.AcceptTravelContext;
import cds.gen.travelservice.RejectTravelContext;
import cds.gen.travelservice.Travel;
import cds.gen.travelservice.TravelService_;
import cds.gen.travelservice.Travel_;

@Component
@ServiceName(TravelService_.CDS_NAME)
Expand Down Expand Up @@ -49,7 +48,7 @@ public void beforeRejectTravel(final RejectTravelContext context) {

@On(entity = Travel_.CDS_NAME)
public void onRejectTravel(final RejectTravelContext context) {
var travel = draftService.run(context.getCqn()).single(Travel.class);
Travel travel = draftService.run(context.getCqn()).single(Travel.class);
context.getCdsRuntime().requestContext().privilegedUser().run(ctx -> {
updateStatusForTravelId(travel.getTravelUUID(), TRAVEL_STATUS_CANCELLED, travel.getIsActiveEntity());
});
Expand All @@ -58,7 +57,7 @@ public void onRejectTravel(final RejectTravelContext context) {

@On(entity = Travel_.CDS_NAME)
public void onAcceptTravel(final AcceptTravelContext context) {
var travel = draftService.run(context.getCqn()).single(Travel.class);
Travel travel = draftService.run(context.getCqn()).single(Travel.class);
context.getCdsRuntime().requestContext().privilegedUser().run(ctx -> {
updateStatusForTravelId(travel.getTravelUUID(), TRAVEL_STATUS_ACCEPTED, travel.getIsActiveEntity());
});
Expand All @@ -71,7 +70,7 @@ private void updateStatusForTravelId(String travelUUID, String newStatus, boolea
persistenceService.run(Update.entity(TRAVEL).where(t -> t.TravelUUID().eq(travelUUID))
.data(Travel.TRAVEL_STATUS_CODE, newStatus));
} else {
Update<Travel_> travelUpdateDraft = Update.entity(TRAVEL)
CqnUpdate travelUpdateDraft = Update.entity(TRAVEL)
.where(t -> t.TravelUUID().eq(travelUUID).and(t.IsActiveEntity().eq(false)))
.data(Travel.TRAVEL_STATUS_CODE, newStatus);
draftService.patchDraft(travelUpdateDraft).first(Travel.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import java.math.BigDecimal;
import java.math.MathContext;

import org.springframework.stereotype.Component;

import com.sap.cds.ql.Update;
import com.sap.cds.ql.cqn.CqnUpdate;
import com.sap.cds.services.draft.DraftService;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.handler.annotations.On;
import com.sap.cds.services.handler.annotations.ServiceName;

import org.springframework.stereotype.Component;

import cds.gen.travelservice.DeductDiscountContext;
import cds.gen.travelservice.Travel;
import cds.gen.travelservice.TravelService_;
Expand All @@ -32,7 +33,7 @@ public DeductDiscountHandler(DraftService draftService) {
@On(entity = Travel_.CDS_NAME)
public void deductDiscount(final DeductDiscountContext context) {

var travel = draftService.run(context.getCqn()).single(Travel.class);
Travel travel = draftService.run(context.getCqn()).single(Travel.class);

BigDecimal discount = BigDecimal.valueOf(context.getPercent())
.divide(BigDecimal.valueOf(100), new MathContext(3));
Expand All @@ -52,11 +53,11 @@ public void deductDiscount(final DeductDiscountContext context) {
context.getCdsRuntime().requestContext().privilegedUser().run(ctx -> {
//throw exception if travel.getIsActiveEntity is null!.
if (TRUE.equals(travel.getIsActiveEntity())) {
var updateCqn = Update.entity(TRAVEL)
CqnUpdate updateCqn = Update.entity(TRAVEL)
.where(t -> t.TravelUUID().eq(travel.getTravelUUID())).data(update);
draftService.run(updateCqn);
} else {
var updateCqn = Update.entity(TRAVEL)
CqnUpdate updateCqn = Update.entity(TRAVEL)
.where(t -> t.TravelUUID().eq(travel.getTravelUUID()).and(t.IsActiveEntity().eq(travel.getIsActiveEntity()))).data(update);
draftService.patchDraft(updateCqn);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import org.springframework.stereotype.Component;

import com.sap.cds.Row;
import com.sap.cds.ql.Select;
import com.sap.cds.ql.Update;
import com.sap.cds.ql.cqn.CqnUpdate;
import com.sap.cds.services.ErrorStatuses;
import com.sap.cds.services.ServiceException;
import com.sap.cds.services.cds.CdsService;
Expand All @@ -24,8 +29,6 @@
import com.sap.cds.services.persistence.PersistenceService;
import com.sap.cds.services.utils.StringUtils;

import org.springframework.stereotype.Component;

import cds.gen.travelservice.Booking;
import cds.gen.travelservice.BookingSupplement;
import cds.gen.travelservice.BookingSupplement_;
Expand Down Expand Up @@ -54,8 +57,8 @@ public void disableUpdateAndCreateForBookingAndBookingSupplement() {
private static BigDecimal calculateTotalPriceForTravel(CqnService db, String travelUUID,
boolean isActiveEntity) {
// get booking fee
var bookingFee = BigDecimal.valueOf(0);
var bookingFeeRow = db
BigDecimal bookingFee = BigDecimal.valueOf(0);
Optional<Row> bookingFeeRow = db
.run(Select.from(TRAVEL).columns(Travel_::BookingFee)
.where(t -> t.TravelUUID().eq(travelUUID)
.and(t.IsActiveEntity().eq(isActiveEntity))
Expand All @@ -67,8 +70,8 @@ private static BigDecimal calculateTotalPriceForTravel(CqnService db, String tra
}

// get sum of flight prices from all bookings
var flightPriceSum = new BigDecimal(0);
var flightPriceRow = db
BigDecimal flightPriceSum = new BigDecimal(0);
Optional<Row> flightPriceRow = db
.run(Select.from(BOOKING).columns(b -> sum(b.FlightPrice()).as("FlightPriceSum"))
.where(b -> b.to_Travel_TravelUUID().eq(travelUUID).and(b.IsActiveEntity().eq(isActiveEntity))))
.first();
Expand All @@ -78,8 +81,8 @@ private static BigDecimal calculateTotalPriceForTravel(CqnService db, String tra
}

// get sum of the prices of all booking supplements for the travel
var supplementPriceSum = new BigDecimal(0);
var supplementPriceSumRow = db
BigDecimal supplementPriceSum = new BigDecimal(0);
Optional<Row> supplementPriceSumRow = db
.run(Select.from(BOOKING_SUPPLEMENT).columns(c -> sum(c.Price()).as("PriceSum"))
.where(b -> b.to_Travel_TravelUUID().eq(travelUUID).and(b.IsActiveEntity().eq(isActiveEntity))))
.first();
Expand Down Expand Up @@ -146,7 +149,10 @@ public void recalculateTravelPriceIfPriceWasUpdated(final BookingSupplement book
private BigDecimal calculateAndPatchNewTotalPriceForDraft(final String travelUUID) {

BigDecimal totalPrice = calculateTotalPriceForTravel(draftService, travelUUID, false);
var update = Update.entity(TRAVEL).data(Map.of(Travel.TRAVEL_UUID, travelUUID, Travel.TOTAL_PRICE, totalPrice));
Map<String, Object> map = new HashMap<String, Object>();
map.put(Travel.TRAVEL_UUID, travelUUID);
map.put(Travel.TOTAL_PRICE, totalPrice);
CqnUpdate update = Update.entity(TRAVEL).data(map);
draftService.patchDraft(update);
return totalPrice;
}
Expand Down

0 comments on commit fc1054e

Please sign in to comment.