Skip to content

Commit

Permalink
chore: merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
eschrewe committed Jun 20, 2024
2 parents 92a3a2e + 7c7f4cf commit 37bb8e0
Show file tree
Hide file tree
Showing 37 changed files with 1,187 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public class DataInjectionCommandLineRunner implements CommandLineRunner {

@Autowired
private VariablesService variablesService;

@Autowired
private ErpAdapterRequestService erpAdapterRequestService;

Expand Down Expand Up @@ -491,5 +490,4 @@ private Material getNewCentralControlUnitMaterial() {
material.setName("Central Control Unit");
return material;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
"/planned-production/**",
"/material-demand/**",
"/delivery-information/**",
"/days-of-supply/**",
"/edc/**",
"/erp-adapter/**",
"/parttypeinformation/**"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ public List<DeliveryDto> getAllDeliveries(String ownMaterialNumber, Optional<Str
if (material == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Material does not exist.");
}
var reportedDeliveries = reportedDeliveryService.findAllByFilters(Optional.of(ownMaterialNumber), bpns, bpnl)
var reportedDeliveries = reportedDeliveryService.findAllByFilters(Optional.of(ownMaterialNumber), bpns, bpnl, Optional.empty(), Optional.empty())
.stream().map(this::convertToDto).collect(Collectors.toList());
var ownDeliveries = ownDeliveryService.findAllByFilters(Optional.of(ownMaterialNumber), bpns, bpnl)
var ownDeliveries = ownDeliveryService.findAllByFilters(Optional.of(ownMaterialNumber), bpns, bpnl, Optional.empty(), Optional.empty())
.stream().map(this::convertToDto).collect(Collectors.toList());
return List.of(reportedDeliveries, ownDeliveries).stream().flatMap(List::stream).toList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2024 Volkswagen AG
* Copyright (c) 2024 Contributors to the Eclipse Foundation
* Copyright (c) 2023, 2024 Volkswagen AG
* Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -22,9 +22,9 @@

import java.util.UUID;

import org.eclipse.tractusx.puris.backend.delivery.domain.model.OwnDelivery;
import org.eclipse.tractusx.puris.backend.delivery.domain.model.Delivery;
import org.springframework.data.jpa.repository.JpaRepository;

public interface DeliveryRepository extends JpaRepository<OwnDelivery, UUID> {
public interface DeliveryRepository<T extends Delivery> extends JpaRepository<T, UUID> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2024 Volkswagen AG
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

package org.eclipse.tractusx.puris.backend.delivery.domain.repository;

import org.eclipse.tractusx.puris.backend.delivery.domain.model.OwnDelivery;

public interface OwnDeliveryRepository extends DeliveryRepository<OwnDelivery> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@

package org.eclipse.tractusx.puris.backend.delivery.domain.repository;

import java.util.UUID;

import org.eclipse.tractusx.puris.backend.delivery.domain.model.ReportedDelivery;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ReportedDeliveryRepository extends JpaRepository<ReportedDelivery, UUID> {
public interface ReportedDeliveryRepository extends DeliveryRepository<ReportedDelivery> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ public DeliveryInformation handleDeliverySubmodelRequest(String bpnl, String mat
List<OwnDelivery> currentDeliveries = ownDeliveryService.findAllByFilters(
Optional.of(material.getOwnMaterialNumber()),
Optional.empty(),
Optional.of(partner.getBpnl())
);
Optional.of(partner.getBpnl()),
Optional.empty(),
Optional.empty());

Predicate<OwnDelivery> parnterRoleDirectionPredicate = partnerRoleDirectionPredicate(partnerIsCustomer, mpr);
currentDeliveries = currentDeliveries.stream().filter(
Expand Down Expand Up @@ -167,7 +168,7 @@ public void doReportedDeliveryRequest(Partner partner, Material material) {
}
}
// delete older data:
var oldDeliveries = reportedDeliveryService.findAllByFilters(Optional.of(material.getOwnMaterialNumber()), Optional.empty(), Optional.of(partner.getBpnl()));
var oldDeliveries = reportedDeliveryService.findAllByFilters(Optional.of(material.getOwnMaterialNumber()), Optional.empty(), Optional.of(partner.getBpnl()), Optional.empty(), Optional.empty());
for (var oldDelivery : oldDeliveries) {
reportedDeliveryService.delete(oldDelivery.getUuid());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright (c) 2024 Volkswagen AG
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

package org.eclipse.tractusx.puris.backend.delivery.logic.service;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Stream;

import org.eclipse.tractusx.puris.backend.delivery.domain.model.Delivery;
import org.eclipse.tractusx.puris.backend.delivery.domain.repository.DeliveryRepository;
import org.eclipse.tractusx.puris.backend.stock.logic.dto.itemstocksamm.DirectionCharacteristic;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class DeliveryService<T extends Delivery> {
@Autowired
protected DeliveryRepository<T> repository;

public final List<T> findAll() {
return repository.findAll();
}

public final T findById(UUID id) {
return repository.findById(id).orElse(null);
}

public final List<T> findAllByFilters(
Optional<String> ownMaterialNumber,
Optional<String> bpns,
Optional<String> bpnl,
Optional<Date> day,
Optional<DirectionCharacteristic> direction) {
Stream<T> stream = repository.findAll().stream();
if (ownMaterialNumber.isPresent()) {
stream = stream.filter(delivery -> delivery.getMaterial().getOwnMaterialNumber().equals(ownMaterialNumber.get()));
}
if (bpns.isPresent()) {
stream = stream.filter(delivery -> delivery.getDestinationBpns().equals(bpns.get()) || delivery.getOriginBpns().equals(bpns.get()));
}
if (bpnl.isPresent()) {
stream = stream.filter(delivery -> delivery.getPartner().getBpnl().equals(bpnl.get()));
}
if (day.isPresent()) {
LocalDate localDayDate = Instant.ofEpochMilli(day.get().getTime())
.atOffset(ZoneOffset.UTC)
.toLocalDate();
stream = stream.filter(delivery -> {
long time = direction.get() == DirectionCharacteristic.INBOUND
? delivery.getDateOfArrival().getTime()
: delivery.getDateOfDeparture().getTime();
LocalDate deliveryDayDate = Instant.ofEpochMilli(time)
.atOffset(ZoneOffset.UTC)
.toLocalDate();
return deliveryDayDate.getDayOfMonth() == localDayDate.getDayOfMonth();
});
}
if (direction.isPresent()) {
if (direction.get() == DirectionCharacteristic.INBOUND) {
stream = stream.filter(delivery -> delivery.getDestinationBpns().equals(bpns.get()));
} else {
stream = stream.filter(delivery -> delivery.getOriginBpns().equals(bpns.get()));
}
}
return stream.toList();
}

public final double getSumOfQuantities(List<T> deliveries) {
double sum = 0;
for (T delivery : deliveries) {
sum += delivery.getQuantity();
}
return sum;
}

public final List<Double> getQuantityForDays(String material, String partnerBpnl, String siteBpns, DirectionCharacteristic direction, int numberOfDays) {
List<Double> deliveryQtys = new ArrayList<>();
LocalDate localDate = LocalDate.now();

for (int i = 0; i < numberOfDays; i++) {
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
List<T> deliveries = findAllByFilters(Optional.of(material), Optional.of(siteBpns), Optional.of(partnerBpnl), Optional.of(date), Optional.of(direction));
deliveryQtys.add(getSumOfQuantities(deliveries));

localDate = localDate.plusDays(1);
}
return deliveryQtys;
}

public final T update(T delivery) {
if (delivery.getUuid() == null || repository.findById(delivery.getUuid()).isEmpty()) {
return null;
}
return repository.save(delivery);
}

public final void delete(UUID id) {
repository.deleteById(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,33 @@

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Stream;

import javax.management.openmbean.KeyAlreadyExistsException;

import org.eclipse.tractusx.puris.backend.delivery.domain.model.EventTypeEnumeration;
import org.eclipse.tractusx.puris.backend.delivery.domain.model.OwnDelivery;
import org.eclipse.tractusx.puris.backend.delivery.domain.repository.DeliveryRepository;
import org.eclipse.tractusx.puris.backend.delivery.domain.repository.OwnDeliveryRepository;
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner;
import org.eclipse.tractusx.puris.backend.masterdata.logic.service.PartnerService;
import org.springframework.stereotype.Service;

@Service
public class OwnDeliveryService {
public final DeliveryRepository repository;
public class OwnDeliveryService extends DeliveryService<OwnDelivery> {
private final OwnDeliveryRepository repository;

private final PartnerService partnerService;

protected final Function<OwnDelivery, Boolean> validator;

private Partner ownPartnerEntity;

public OwnDeliveryService(DeliveryRepository repository, PartnerService partnerService) {
public OwnDeliveryService(OwnDeliveryRepository repository, PartnerService partnerService) {
this.repository = repository;
this.partnerService = partnerService;
this.validator = this::validate;
}

public final List<OwnDelivery> findAll() {
return repository.findAll();
}

public final List<OwnDelivery> findAllByBpnl(String bpnl) {
return repository.findAll().stream().filter(delivery -> delivery.getPartner().getBpnl().equals(bpnl))
.toList();
Expand All @@ -66,24 +59,6 @@ public final List<OwnDelivery> findAllByOwnMaterialNumber(String ownMaterialNumb
.toList();
}

public final List<OwnDelivery> findAllByFilters(Optional<String> ownMaterialNumber, Optional<String> bpns, Optional<String> bpnl) {
Stream<OwnDelivery> stream = repository.findAll().stream();
if (ownMaterialNumber.isPresent()) {
stream = stream.filter(delivery -> delivery.getMaterial().getOwnMaterialNumber().equals(ownMaterialNumber.get()));
}
if (bpns.isPresent()) {
stream = stream.filter(delivery -> delivery.getDestinationBpns().equals(bpns.get()) || delivery.getOriginBpns().equals(bpns.get()));
}
if (bpnl.isPresent()) {
stream = stream.filter(delivery -> delivery.getPartner().getBpnl().equals(bpnl.get()));
}
return stream.toList();
}

public final OwnDelivery findById(UUID id) {
return repository.findById(id).orElse(null);
}

public final OwnDelivery create(OwnDelivery delivery) {
if (!validator.apply(delivery)) {
throw new IllegalArgumentException("Invalid delivery");
Expand All @@ -105,23 +80,12 @@ public final List<OwnDelivery> createAll(List<OwnDelivery> deliveries) {
return repository.saveAll(deliveries);
}

public final OwnDelivery update(OwnDelivery delivery) {
if (delivery.getUuid() == null || repository.findById(delivery.getUuid()).isEmpty()) {
return null;
}
return repository.save(delivery);
}

public final void delete(UUID id) {
repository.deleteById(id);
}

public boolean validate(OwnDelivery delivery) {
if (ownPartnerEntity == null) {
ownPartnerEntity = partnerService.getOwnPartnerEntity();
}
return
delivery.getQuantity() > 0 &&
delivery.getQuantity() >= 0 &&
delivery.getMeasurementUnit() != null &&
delivery.getMaterial() != null &&
delivery.getPartner() != null &&
Expand Down
Loading

0 comments on commit 37bb8e0

Please sign in to comment.