Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: delivery and production services #615

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,37 @@

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

import java.util.UUID;

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

import java.util.List;
import java.util.UUID;

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


default List<T> getForOwnMaterialNumber(String ownMatNbr) {
// default implementation prevents Jpa from trying to
// auto-generate this method.
throw new UnsupportedOperationException("Implementation needed");
}

default List<T> getForOwnMaterialNumberAndPartnerBPNL(String ownMatNbr, String bpnl) {
// default implementation prevents Jpa from trying to
// auto-generate this method.
throw new UnsupportedOperationException("Implementation needed");
}

default List<T> getForOwnMaterialNumberAndBPNS(String ownMatNbr, String bpns) {
// default implementation prevents Jpa from trying to
// auto-generate this method.
throw new UnsupportedOperationException("Implementation needed");
}

default List<T> getForOwnMaterialNumberAndPartnerBPNLAndBPNS(String ownMatNbr, String bpnl, String bpns) {
// default implementation prevents Jpa from trying to
// auto-generate this method.
throw new UnsupportedOperationException("Implementation needed");
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,45 @@

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

import java.util.ArrayList;
import java.util.List;

public interface OwnDeliveryRepository extends DeliveryRepository<OwnDelivery> {

List<OwnDelivery> findAllByMaterial_OwnMaterialNumber(String ownMaterialNumber);

@Override
default List<OwnDelivery> getForOwnMaterialNumber(String ownMatNbr) {
return findAllByMaterial_OwnMaterialNumber(ownMatNbr);
}

List<OwnDelivery> findAllByMaterial_OwnMaterialNumberAndPartner_Bpnl(String ownMatNbr, String partnerBpnl);

@Override
default List<OwnDelivery> getForOwnMaterialNumberAndPartnerBPNL(String ownMatNbr, String bpnl) {
return findAllByMaterial_OwnMaterialNumberAndPartner_Bpnl(ownMatNbr, bpnl);
}

List<OwnDelivery> findAllByMaterial_OwnMaterialNumberAndOriginBpns(String ownMatNbr, String originBpns);

List<OwnDelivery> findAllByMaterial_OwnMaterialNumberAndDestinationBpns(String ownMatNbr, String destinationBpns);

@Override
default List<OwnDelivery> getForOwnMaterialNumberAndBPNS(String ownMatNbr, String bpns) {
var resultList = new ArrayList<>(findAllByMaterial_OwnMaterialNumberAndOriginBpns(ownMatNbr, bpns));
resultList.addAll(findAllByMaterial_OwnMaterialNumberAndDestinationBpns(ownMatNbr, bpns));
return resultList;
}

List<OwnDelivery> findAllByMaterial_OwnMaterialNumberAndPartner_BpnlAndDestinationBpns(String ownMatNbr, String partnerBpnl, String destinationBpnl);

List<OwnDelivery> findAllByMaterial_OwnMaterialNumberAndPartner_BpnlAndOriginBpns(String ownMatNbr, String partnerBpnl, String originBpnl);

@Override
default List<OwnDelivery> getForOwnMaterialNumberAndPartnerBPNLAndBPNS(String ownMatNbr, String bpnl, String bpns) {
var resultList = new ArrayList<>(findAllByMaterial_OwnMaterialNumberAndPartner_BpnlAndDestinationBpns(ownMatNbr, bpnl, bpns));
resultList.addAll(findAllByMaterial_OwnMaterialNumberAndPartner_BpnlAndOriginBpns(ownMatNbr, bpnl, bpns));
return resultList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,45 @@

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

import java.util.ArrayList;
import java.util.List;

public interface ReportedDeliveryRepository extends DeliveryRepository<ReportedDelivery> {

List<ReportedDelivery> findAllByMaterial_OwnMaterialNumber(String ownMaterialNumber);

@Override
default List<ReportedDelivery> getForOwnMaterialNumber(String ownMatNbr) {
return findAllByMaterial_OwnMaterialNumber(ownMatNbr);
}

List<ReportedDelivery> findAllByMaterial_OwnMaterialNumberAndPartner_Bpnl(String ownMatNbr, String partnerBpnl);

@Override
default List<ReportedDelivery> getForOwnMaterialNumberAndPartnerBPNL(String ownMatNbr, String bpnl) {
return findAllByMaterial_OwnMaterialNumberAndPartner_Bpnl(ownMatNbr, bpnl);
}

List<ReportedDelivery> findAllByMaterial_OwnMaterialNumberAndOriginBpns(String ownMatNbr, String originBpns);

List<ReportedDelivery> findAllByMaterial_OwnMaterialNumberAndDestinationBpns(String ownMatNbr, String destinationBpns);

@Override
default List<ReportedDelivery> getForOwnMaterialNumberAndBPNS(String ownMatNbr, String bpns) {
var resultList = new ArrayList<>(findAllByMaterial_OwnMaterialNumberAndOriginBpns(ownMatNbr, bpns));
resultList.addAll(findAllByMaterial_OwnMaterialNumberAndDestinationBpns(ownMatNbr, bpns));
return resultList;
}

List<ReportedDelivery> findAllByMaterial_OwnMaterialNumberAndPartner_BpnlAndDestinationBpns(String ownMatNbr, String partnerBpnl, String destinationBpnl);

List<ReportedDelivery> findAllByMaterial_OwnMaterialNumberAndPartner_BpnlAndOriginBpns(String ownMatNbr, String partnerBpnl, String originBpnl);

@Override
default List<ReportedDelivery> getForOwnMaterialNumberAndPartnerBPNLAndBPNS(String ownMatNbr, String bpnl, String bpns) {
var resultList = new ArrayList<>(findAllByMaterial_OwnMaterialNumberAndPartner_BpnlAndDestinationBpns(ownMatNbr, bpnl, bpns));
resultList.addAll(findAllByMaterial_OwnMaterialNumberAndPartner_BpnlAndOriginBpns(ownMatNbr, bpnl, bpns));
return resultList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,26 @@

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

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 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.*;
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;

final protected DeliveryRepository<T> repository;

protected DeliveryService(DeliveryRepository<T> repository) {
this.repository = repository;
}

public abstract boolean validate(T delivery);

public final List<T> findAll() {
return repository.findAll();
Expand All @@ -54,17 +55,8 @@ public final List<T> findAllByFilters(
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()) {
if (day.isPresent() && direction.isPresent() && ownMaterialNumber.isPresent() && bpns.isPresent() && bpnl.isPresent()) {
Stream<T> stream = repository.getForOwnMaterialNumberAndPartnerBPNLAndBPNS(ownMaterialNumber.get(), bpnl.get(), bpns.get()).stream();
LocalDate localDayDate = Instant.ofEpochMilli(day.get().getTime())
.atOffset(ZoneOffset.UTC)
.toLocalDate();
Expand All @@ -77,15 +69,27 @@ public final List<T> findAllByFilters(
.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();
}
if (ownMaterialNumber.isPresent() && bpns.isPresent() && bpnl.isPresent()) {
return repository.getForOwnMaterialNumberAndPartnerBPNLAndBPNS(ownMaterialNumber.get(), bpnl.get(), bpns.get());
}
if (ownMaterialNumber.isPresent() && bpns.isPresent()) {
return repository.getForOwnMaterialNumberAndBPNS(ownMaterialNumber.get(), bpns.get());
}
return stream.toList();
if (ownMaterialNumber.isPresent() && bpnl.isPresent()) {
return repository.getForOwnMaterialNumberAndPartnerBPNL(ownMaterialNumber.get(), bpnl.get());
}
if (ownMaterialNumber.isPresent()) {
return repository.getForOwnMaterialNumber(ownMaterialNumber.get());
}

return List.of();
}

public final double getSumOfQuantities(List<T> deliveries) {
Expand Down Expand Up @@ -114,7 +118,11 @@ public final T update(T delivery) {
if (delivery.getUuid() == null || repository.findById(delivery.getUuid()).isEmpty()) {
return null;
}
return repository.save(delivery);
if (validate(delivery)) {
return repository.save(delivery);
} else {
return null;
}
}

public final void delete(UUID id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,17 @@

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

import java.util.Date;
import java.util.List;
import java.util.function.Function;

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.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;

import javax.management.openmbean.KeyAlreadyExistsException;
import java.util.Date;
import java.util.function.Function;

@Service
public class OwnDeliveryService extends DeliveryService<OwnDelivery> {
private final OwnDeliveryRepository repository;
Expand All @@ -44,21 +42,12 @@ public class OwnDeliveryService extends DeliveryService<OwnDelivery> {
private Partner ownPartnerEntity;

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

public final List<OwnDelivery> findAllByBpnl(String bpnl) {
return repository.findAll().stream().filter(delivery -> delivery.getPartner().getBpnl().equals(bpnl))
.toList();
}

public final List<OwnDelivery> findAllByOwnMaterialNumber(String ownMaterialNumber) {
return repository.findAll().stream().filter(delivery -> delivery.getMaterial().getOwnMaterialNumber().equals(ownMaterialNumber))
.toList();
}

public final OwnDelivery create(OwnDelivery delivery) {
if (!validator.apply(delivery)) {
throw new IllegalArgumentException("Invalid delivery");
Expand All @@ -69,17 +58,7 @@ public final OwnDelivery create(OwnDelivery delivery) {
return repository.save(delivery);
}

public final List<OwnDelivery> createAll(List<OwnDelivery> deliveries) {
if (deliveries.stream().anyMatch(delivery -> !validator.apply(delivery))) {
throw new IllegalArgumentException("Invalid delivery");
}
if (repository.findAll().stream()
.anyMatch(existing -> deliveries.stream().anyMatch(delivery -> delivery.equals(existing)))) {
throw new KeyAlreadyExistsException("delivery already exists");
}
return repository.saveAll(deliveries);
}

@Override
public boolean validate(OwnDelivery delivery) {
if (ownPartnerEntity == null) {
ownPartnerEntity = partnerService.getOwnPartnerEntity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@

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

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.function.Function;

import org.eclipse.tractusx.puris.backend.delivery.domain.model.EventTypeEnumeration;
import org.eclipse.tractusx.puris.backend.delivery.domain.model.ReportedDelivery;
import org.eclipse.tractusx.puris.backend.delivery.domain.repository.ReportedDeliveryRepository;
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;

import java.util.Date;
import java.util.function.Function;

@Service
public class ReportedDeliveryService extends DeliveryService<ReportedDelivery> {
private final ReportedDeliveryRepository repository;
Expand All @@ -43,16 +41,12 @@ public class ReportedDeliveryService extends DeliveryService<ReportedDelivery> {
private Partner ownPartnerEntity;

public ReportedDeliveryService(ReportedDeliveryRepository repository, PartnerService partnerService) {
super(repository);
this.repository = repository;
this.partnerService = partnerService;
this.validator = this::validate;
}

public final List<ReportedDelivery> findAllByReportedId(UUID reportedId) {
return repository.findAll().stream().filter(delivery -> delivery.getPartner().getUuid().equals(reportedId))
.toList();
}

public final ReportedDelivery create(ReportedDelivery delivery) {
if (delivery.getUuid() != null && repository.findById(delivery.getUuid()).isPresent()) {
return null;
Expand All @@ -63,17 +57,7 @@ public final ReportedDelivery create(ReportedDelivery delivery) {
return repository.save(delivery);
}

public final List<ReportedDelivery> createAll(List<ReportedDelivery> deliveries) {
if (deliveries.stream().anyMatch(delivery -> !validator.apply(delivery))) {
return null;
}
if (repository.findAll().stream()
.anyMatch(existing -> deliveries.stream().anyMatch(delivery -> delivery.equals(existing)))) {
return null;
}
return repository.saveAll(deliveries);
}

@Override
public boolean validate(ReportedDelivery delivery) {
return
delivery.getQuantity() > 0 &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,37 @@ See the NOTICE file(s) distributed with this work for additional
import org.eclipse.tractusx.puris.backend.production.domain.model.OwnProduction;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface OwnProductionRepository extends ProductionRepository<OwnProduction> {

List<OwnProduction> findAllByMaterial_OwnMaterialNumber(String ownMaterialNumber);

@Override
default List<OwnProduction> getForOwnMaterialNumber(String ownMatNbr) {
return findAllByMaterial_OwnMaterialNumber(ownMatNbr);
}

List<OwnProduction> findAllByMaterial_OwnMaterialNumberAndPartner_Bpnl(String ownMaterialNumber, String partnerBpnl);

@Override
default List<OwnProduction> getForOwnMaterialNumberAndPartnerBPNL(String ownMatNbr, String bpnl) {
return findAllByMaterial_OwnMaterialNumberAndPartner_Bpnl(ownMatNbr, bpnl);
}

List<OwnProduction> findAllByMaterial_OwnMaterialNumberAndPartner_BpnlAndProductionSiteBpns(String ownMaterialNumber, String partnerBpnl, String bpns);

@Override
default List<OwnProduction> getForOwnMaterialNumberAndPartnerBPNLAndBPNS(String ownMatNbr, String bpnl, String bpns) {
return findAllByMaterial_OwnMaterialNumberAndPartner_BpnlAndProductionSiteBpns(ownMatNbr, bpnl, bpns);
}

List<OwnProduction> findAllByMaterial_OwnMaterialNumberAndProductionSiteBpns(String ownMatNbr, String bpns);

@Override
default List<OwnProduction> getForOwnMaterialNumberAndBPNS(String ownMatNbr, String bpns) {
return findAllByMaterial_OwnMaterialNumberAndProductionSiteBpns(ownMatNbr, bpns);
}

}
Loading
Loading