Skip to content

Commit

Permalink
Merge branch 'Demo_branch' of https://github.com/catenax-ng/tx-demand…
Browse files Browse the repository at this point in the history
…-capacity-mgmt into Demo_branch
  • Loading branch information
Ruskyy committed Oct 26, 2023
2 parents a579dbf + 5bfa8f4 commit 4d2ae90
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.services;

public interface StatusManager {
void calculateBottleneck(String userID);
void calculateBottleneck(String userID,boolean postLog);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,29 @@
package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.services.impl;

import eclipse.tractusx.demand_capacity_mgmt_specification.model.*;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.*;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.enums.EventObjectType;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.enums.EventType;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.exceptions.type.NotFoundException;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.repositories.*;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.repositories.CapacityGroupRepository;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.repositories.DemandSeriesRepository;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.repositories.LinkedCapacityGroupMaterialDemandRepository;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.repositories.MaterialDemandRepository;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.services.*;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.utils.UUIDUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;

@RequiredArgsConstructor
@Service
@Slf4j
Expand Down Expand Up @@ -81,7 +85,7 @@ public CapacityGroupResponse createCapacityGroup(CapacityGroupRequest capacityGr
matchedMaterialDemands.addAll(materialDemandEntity.getDemandSeries());
}
linkedCapacityGroupMaterialDemandRepository.save(entity);
statusManager.calculateBottleneck(userID);
statusManager.calculateBottleneck(userID,true);
capacityGroupEntity.setLinkStatus(EventType.GENERAL_EVENT);
capacityGroupRepository.save(capacityGroupEntity);
}
Expand Down Expand Up @@ -161,7 +165,7 @@ public void linkCapacityGroupToMaterialDemand(LinkCGDSRequest linkCGDSRequest, S
materialDemandRepository.save(demandEntity);
}
}
statusManager.calculateBottleneck(userID);
statusManager.calculateBottleneck(userID,true);
}

private CapacityGroupEntity enrichCapacityGroup(CapacityGroupRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@

import eclipse.tractusx.demand_capacity_mgmt_specification.model.*;
import jakarta.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.*;
Expand All @@ -48,6 +40,15 @@
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.utils.UserUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;

@RequiredArgsConstructor
@Service
@Slf4j
Expand Down Expand Up @@ -86,7 +87,7 @@ public MaterialDemandResponse createDemand(MaterialDemandRequest materialDemandR
materialDemandEntity = materialDemandRepository.save(materialDemandEntity);
postLogs(materialDemandEntity.getId().toString(), "Material Demand created", EventType.GENERAL_EVENT, userID);
statusesService.addOrSubtractTodos(true, userID);
statusManager.calculateBottleneck(userID);
statusManager.calculateBottleneck(userID,true);
return convertDemandResponseDto(materialDemandEntity);
}

Expand Down Expand Up @@ -167,7 +168,7 @@ public MaterialDemandResponse updateDemand(
);
demand = materialDemandRepository.save(demand);
postLogs(demandId, "MATERIAL DEMAND Updated", EventType.GENERAL_EVENT, userID);
statusManager.calculateBottleneck(userID);
statusManager.calculateBottleneck(userID,true);
return convertDemandResponseDto(demand);
}

Expand All @@ -184,7 +185,7 @@ public void deleteDemandById(String demandId, String userID) {
linkedCapacityGroupMaterialDemandRepository.deleteByMaterialDemandID(demand.getId());
materialDemandRepository.delete(demand);
statusesService.addOrSubtractTodos(false, userID);
statusManager.calculateBottleneck(userID);
statusManager.calculateBottleneck(userID,true);
}

@Override
Expand Down Expand Up @@ -270,7 +271,7 @@ public void unlinkComposites(DemandSeriesUnlinkRequest demandSeriesUnlinkRequest
List<MaterialDemandEntity> oldMaterialDemands = getAllDemands();
oldMaterialDemands.removeIf(md -> md.getId().equals(mdID));
statusesService.addOrSubtractTodos(true, userID);
statusManager.calculateBottleneck(userID);
statusManager.calculateBottleneck(userID,true);
}

private MaterialDemandEntity getDemandEntity(String demandId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import eclipse.tractusx.demand_capacity_mgmt_specification.model.Role;
import eclipse.tractusx.demand_capacity_mgmt_specification.model.TokenResponse;
import eclipse.tractusx.demand_capacity_mgmt_specification.model.User;
import java.util.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.UserEntity;
Expand All @@ -46,6 +45,8 @@
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.*;

@RequiredArgsConstructor
@Service
@Slf4j
Expand Down Expand Up @@ -260,7 +261,7 @@ private User convertUserEntity(UserEntity userEntity, String accessToken, String
user.setAccessToken(accessToken);
user.setRefreshToken(refreshToken);
user.setExpiresIn(expiresIn);
statusManager.calculateBottleneck(user.getUserID());
statusManager.calculateBottleneck(user.getUserID(),false);
return user;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ public class StatusManagerImpl implements StatusManager {
private final LoggingHistoryRepository loggingRepository;

@Override
public void calculateBottleneck(String userID) {
public void calculateBottleneck(String userID, boolean postLog) {
UserEntity user = getUser(userID);
List<CapacityGroupEntity> capacityGroups = capacityGroupRepository.findByUserID(user.getId());

int accumulatedImprovements = 0;
int accumulatedDegradations = 0;

for (CapacityGroupEntity cgs : capacityGroups) {
EventType eventType = processCapacityGroup(userID, cgs);
EventType eventType = processCapacityGroup(userID, cgs,postLog);

if (eventType == EventType.STATUS_IMPROVEMENT) {
accumulatedImprovements++;
Expand All @@ -72,7 +72,7 @@ public void calculateBottleneck(String userID) {
statusesRepository.save(status);
}

private EventType processCapacityGroup(String userID, CapacityGroupEntity cgs) {
private EventType processCapacityGroup(String userID, CapacityGroupEntity cgs,boolean postLog) {
List<LinkedCapacityGroupMaterialDemandEntity> matchedEntities = matchedDemandsRepository.findByCapacityGroupID(
cgs.getId()
);
Expand All @@ -91,7 +91,7 @@ private EventType processCapacityGroup(String userID, CapacityGroupEntity cgs) {
EventType eventType = determineEventType(cgs, aggregatedTotalDemand);
cgs.setLinkStatus(eventType);
capacityGroupRepository.save(cgs);
logEvent(eventType, userID);
logEvent(eventType, userID,postLog);
return eventType;
}

Expand Down Expand Up @@ -139,25 +139,27 @@ private EventType determineEventType(CapacityGroupEntity capacityGroup, double t
}
}

private void logEvent(EventType eventType, String userID) {
LoggingHistoryEntity logEntity = new LoggingHistoryEntity();
logEntity.setObjectType(EventObjectType.CAPACITY_GROUP);
logEntity.setEventType(eventType);
logEntity.setUserAccount(getUser(userID).getUsername());
logEntity.setTime_created(Timestamp.valueOf(LocalDateTime.now()));
logEntity.setLogID(UUID.randomUUID());

switch (eventType) {
case STATUS_IMPROVEMENT:
logEntity.setDescription("Status improvement");
break;
case STATUS_REDUCTION:
logEntity.setDescription("Status degradation");
break;
default:
return;
}
private void logEvent(EventType eventType, String userID,boolean postLog) {
if (postLog) {
LoggingHistoryEntity logEntity = new LoggingHistoryEntity();
logEntity.setObjectType(EventObjectType.CAPACITY_GROUP);
logEntity.setEventType(eventType);
logEntity.setUserAccount(getUser(userID).getUsername());
logEntity.setTime_created(Timestamp.valueOf(LocalDateTime.now()));
logEntity.setLogID(UUID.randomUUID());

switch (eventType) {
case STATUS_IMPROVEMENT:
logEntity.setDescription("Status improvement");
break;
case STATUS_REDUCTION:
logEntity.setDescription("Status degradation");
break;
default:
return;
}

loggingRepository.save(logEntity);
loggingRepository.save(logEntity);
}
}
}

0 comments on commit 4d2ae90

Please sign in to comment.