Skip to content

Commit

Permalink
Merge branch 'fix/merge67' into feature/DCMFOSS-67-copy2
Browse files Browse the repository at this point in the history
  • Loading branch information
Bailonis committed Oct 4, 2023
2 parents 743b904 + cbeb04f commit e7c5846
Show file tree
Hide file tree
Showing 6 changed files with 501 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import eclipse.tractusx.demand_capacity_mgmt_specification.model.*;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -129,6 +131,7 @@ private CapacityGroupEntity enrichCapacityGroup(CapacityGroupRequest request) {
@Override
public CapacityGroupResponse getCapacityGroupById(String capacityGroupId) {
CapacityGroupEntity capacityGroupEntity = getCapacityGroupEntity(capacityGroupId);

return convertCapacityGroupDto(capacityGroupEntity);
}

Expand All @@ -154,6 +157,168 @@ private CapacityGroupEntity getCapacityGroupEntity(String capacityGroupId) {
return capacityGroup.get();
}

private void validateRequestFields(CapacityGroupRequest capacityGroupRequest) {
if (!UUIDUtil.checkValidUUID(capacityGroupRequest.getCustomer())) {
throw new BadRequestException(
400,
"Not a valid customer ID",
new ArrayList<>(List.of(capacityGroupRequest.getCustomer()))
);
}

if (!UUIDUtil.checkValidUUID(capacityGroupRequest.getSupplier())) {
throw new BadRequestException(
400,
"Not a valid supplier ID",
new ArrayList<>(List.of(capacityGroupRequest.getSupplier()))
);
}

capacityGroupRequest.getSupplierLocations().forEach(UUIDUtil::checkValidUUID);

List<UUID> expectedSuppliersLocation = capacityGroupRequest
.getSupplierLocations()
.stream()
.map(UUIDUtil::generateUUIDFromString)
.toList();

List<CompanyEntity> companyEntities = companyService.getCompanyIn(expectedSuppliersLocation);

boolean hasAllCompanies = companyEntities
.stream()
.map(CompanyEntity::getId)
.allMatch(expectedSuppliersLocation::contains);

if (!hasAllCompanies) {
throw new BadRequestException(
400,
"Not a valid company",
new ArrayList<>(List.of("hasCompanies returned false."))
);
}

List<LocalDateTime> dates = capacityGroupRequest
.getCapacities()
.stream()
.map(capacityResponse -> DataConverterUtil.convertFromString(capacityResponse.getCalendarWeek()))
.toList();

if (
Boolean.TRUE.equals(!DataConverterUtil.checkListAllMonday(dates)) ||
Boolean.TRUE.equals(!DataConverterUtil.checkDatesSequence(dates))
) {
throw new BadRequestException(
400,
"Dates provided failed to verify",
new ArrayList<>(
List.of(
"Dates need to be all Monday",
"Dates need to be aligned one week apart (Ex: monday to monday)"
)
)
);
}
}

private CapacityGroupEntity enrichCapacityGroup(CapacityGroupRequest capacityGroupRequest) {
UUID capacityGroupId = UUID.randomUUID();
AtomicReference<String> materialNumberCustomer = new AtomicReference<>("");
AtomicReference<String> materialDescriptionCustomer = new AtomicReference<>("");
UnitMeasureEntity unitMeasure = unityOfMeasureService.findById(
UUIDUtil.generateUUIDFromString(capacityGroupRequest.getUnitOfMeasure())
);

CompanyEntity supplier = companyService.getCompanyById(
UUIDUtil.generateUUIDFromString(capacityGroupRequest.getSupplier())
);

CompanyEntity customer = companyService.getCompanyById(
UUIDUtil.generateUUIDFromString(capacityGroupRequest.getSupplier())
);

List<CapacityTimeSeries> capacityTimeSeries = capacityGroupRequest
.getCapacities()
.stream()
.map(
capacityRequest ->
enrichCapacityTimeSeries(
LocalDate.parse(capacityRequest.getCalendarWeek()).atStartOfDay(),
capacityRequest.getActualCapacity().doubleValue(),
capacityRequest.getMaximumCapacity().doubleValue()
)
)
.toList();

List<LinkedDemandSeries> linkDemandEntityList = capacityGroupRequest
.getLinkedDemandSeries()
.stream()
.map(
s -> {
LinkDemandEntity linkDemandEntity = linkDemandRepository
.findById(UUIDUtil.generateUUIDFromString(s))
.orElseThrow();

WeekBasedMaterialDemandEntity weekBasedMaterialDemandEntity = linkDemandEntity.getWeekBasedMaterialDemand();
WeekBasedMaterialDemandRequestDto weekBasedMaterialDemandRequestDto = weekBasedMaterialDemandEntity.getWeekBasedMaterialDemand();
CompanyEntity customerId = companyService.getCompanyById(
UUID.fromString(weekBasedMaterialDemandRequestDto.getCustomer())
);

materialNumberCustomer.set(linkDemandEntity.getMaterialNumberCustomer());

materialDescriptionCustomer.set(linkDemandEntity.getMaterialNumberCustomer());

String demandCategoryId = linkDemandEntity.getDemandCategoryId();
DemandCategoryEntity demandCategoryEntity = demandCategoryService.findById(
UUID.fromString(demandCategoryId)
);

linkDemandEntity.setLinked(true);
linkDemandRepository.save(linkDemandEntity);

return LinkedDemandSeries
.builder()
.materialNumberSupplier(linkDemandEntity.getMaterialNumberSupplier())
.materialNumberCustomer(linkDemandEntity.getMaterialNumberCustomer())
.customerId(customerId)
.demandCategory(demandCategoryEntity)
.build();
}
)
.toList();

return CapacityGroupEntity
.builder()
.id(UUID.randomUUID())
.capacityGroupId(capacityGroupId)
.supplierId(supplier)
.supplierLocation(capacityGroupRequest.getSupplierLocations())
.customerId(customer)
.unitMeasure(unitMeasure)
.changedAt(LocalDateTime.now())
.capacityTimeSeries(capacityTimeSeries)
.linkedDemandSeries(linkDemandEntityList)
.name(capacityGroupRequest.getName())
.materialNumberCustomer(materialNumberCustomer.get())
.materialDescriptionCustomer(materialDescriptionCustomer.get())
.status(CapacityGroupStatus.DRAFT)
.build();
}

private CapacityTimeSeries enrichCapacityTimeSeries(
LocalDateTime calendarWeek,
Double actualCapacity,
Double maximumCapacity
) {
return CapacityTimeSeries
.builder()
.id(UUID.randomUUID())
.calendarWeek(calendarWeek)
.actualCapacity(actualCapacity)
.maximumCapacity(maximumCapacity)
.build();
}

private CapacityGroupResponse convertCapacityGroupDto(CapacityGroupEntity capacityGroupEntity) {
final CapacityGroupResponse responseDto = new CapacityGroupResponse();

Expand Down Expand Up @@ -192,6 +357,61 @@ private CapacityGroupResponse convertCapacityGroupDto(CapacityGroupEntity capaci
return responseDto;
}

private UnitMeasure enrichUnitMeasure(UnitMeasureEntity unitMeasureEntity) {
UnitMeasure unitMeasure = new UnitMeasure();

unitMeasure.setId(unitMeasureEntity.getId().toString());
unitMeasure.setCodeValue(unitMeasureEntity.getCodeValue());
unitMeasure.setDisplayValue(unitMeasureEntity.getDisplayValue());

return unitMeasure;
}

private CapacityRequest convertCapacityTimeSeries(CapacityTimeSeries capacityTimeSeries) {
CapacityRequest capacityRequest = new CapacityRequest();

capacityRequest.setActualCapacity(new BigDecimal(capacityTimeSeries.getActualCapacity()));
capacityRequest.setMaximumCapacity(new BigDecimal(capacityTimeSeries.getMaximumCapacity()));

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = capacityTimeSeries.getCalendarWeek().format(formatter);

capacityRequest.setCalendarWeek(formattedDate);

return capacityRequest;
}

private LinkedDemandSeriesResponse convertLinkedDemandSeries(LinkedDemandSeries linkedDemandSeries) {
LinkedDemandSeriesResponse linkedDemandSeriesResponse = new LinkedDemandSeriesResponse();

linkedDemandSeriesResponse.setMaterialNumberCustomer(linkedDemandSeries.getMaterialNumberCustomer());
linkedDemandSeriesResponse.setMaterialNumberSupplier(linkedDemandSeries.getMaterialNumberSupplier());

CompanyDto customer = companyService.convertEntityToDto(linkedDemandSeries.getCustomerId());
linkedDemandSeriesResponse.setCustomerLocation(customer);

DemandCategoryResponse demand = convertDemandCategoryEntity(linkedDemandSeries.getDemandCategory());
linkedDemandSeriesResponse.setDemandCategory(demand);

return linkedDemandSeriesResponse;
}

private DemandCategoryResponse convertDemandCategoryEntity(DemandCategoryEntity demandCategoryEntity) {
DemandCategoryResponse response = new DemandCategoryResponse();

response.setId(demandCategoryEntity.getId().toString());
response.setDemandCategoryCode(demandCategoryEntity.getDemandCategoryCode());
response.setDemandCategoryName(demandCategoryEntity.getDemandCategoryName());

return response;
}

private CompanyDto convertString(String supplier) {
CompanyEntity entity = companyService.getCompanyById(UUID.fromString(supplier));

return companyService.convertEntityToDto(entity);
}

private List<CapacityGroupDefaultViewResponse> convertCapacityGroupEntity(
List<CapacityGroupEntity> capacityGroupEntityList
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ public FavoriteResponse updateFavorite(UUID id, FavoriteType type, FavoriteReque
entity.setType(FavoriteType.valueOf(favoriteRequest.getfType()));
favoriteRepository.saveAndFlush(entity);
return convertFavoriteResponse(entity);

} else throw new NotFoundException(
404,
"The capacity group provided was not found",
new ArrayList<>(List.of("UUID provided : " + UUID.fromString(favoriteRequest.getFavoriteId())))
"Demand category not found",
new ArrayList<>(List.of("provided UUID did not match any records. - " + id))
);

}

@Override
Expand Down
Loading

0 comments on commit e7c5846

Please sign in to comment.