Skip to content
This repository has been archived by the owner on Jan 20, 2025. It is now read-only.

Commit

Permalink
Merge pull request #18 from catenax-ng/feature/DCMFOSS-70
Browse files Browse the repository at this point in the history
implemented alert service
  • Loading branch information
Diogo12246 authored Nov 17, 2023
2 parents 98d51d2 + d3dc154 commit 7221f17
Showing 18 changed files with 900 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* ******************************************************************************
* Copyright (c) 2023 BMW AG
* Copyright (c) 2023 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.demandcapacitymgmt.demandcapacitymgmtbackend.controllers;

import eclipse.tractusx.demand_capacity_mgmt_specification.api.AlertsApi;
import eclipse.tractusx.demand_capacity_mgmt_specification.model.*;
import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
import lombok.AllArgsConstructor;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.services.AlertService;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.utils.UserUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

@RestController
@AllArgsConstructor
//@RequestMapping("/alerts")
public class AlertController implements AlertsApi {

private HttpServletRequest request;
private final AlertService alertService;

@Override
public ResponseEntity<AlertResponse> configureAlert(AlertRequest alertRequest) throws Exception {
AlertResponse responseDto = alertService.configureAlert(alertRequest);
return ResponseEntity.status(HttpStatus.CREATED).body(responseDto);
}

@Override
public ResponseEntity<TriggeredAlertResponse> configureTriggeredAlert(TriggeredAlertRequest triggeredAlertRequest)
throws Exception {
TriggeredAlertResponse responseDto = alertService.postTriggeredAlerts(triggeredAlertRequest);
return ResponseEntity.status(HttpStatus.CREATED).body(responseDto);
}

@Override
public ResponseEntity<List<AlertResponse>> getAlerts() throws Exception {
String userID = UserUtil.getUserID(request);
return ResponseEntity.status(HttpStatus.OK).body(alertService.getAlerts(userID));
}

@Override
public ResponseEntity<List<TriggeredAlertResponse>> getTriggeredAlerts() throws Exception {
String userID = UserUtil.getUserID(request);
return ResponseEntity.status(HttpStatus.OK).body(alertService.getTriggeredAlerts(userID)); //
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities;

import jakarta.persistence.*;
import java.util.List;
import java.util.UUID;
import lombok.*;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.enums.AlertThresholdType;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.enums.AlertsMonitoredObjects;

@Entity
@Table(name = "alerts")
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AlertEntity {

@Id
@GeneratedValue
@Column(columnDefinition = "uuid", updatable = false, name = "id")
private UUID id;

@Column(name = "user_id")
private UUID userID;

@Column(name = "alert_name")
private String alertName;

@Column(name = "monitored_objects")
@Enumerated(EnumType.STRING)
private AlertsMonitoredObjects monitoredObjects;

@Column(name = "created")
private String created;

@Column(name = "type")
@Enumerated(EnumType.STRING)
private AlertThresholdType type;

@Column(name = "threshold")
private double threshold;

@Column(name = "description")
private String description;

@ToString.Exclude
@EqualsAndHashCode.Exclude
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
@JoinColumn(name = "alert_id")
private List<DedicatedAlertEntity> dedicatedAlerts;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities;

import jakarta.persistence.*;
import java.util.List;
import java.util.UUID;
import lombok.*;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.enums.EventObjectType;

@Entity
@Table(name = "dedicated_alerts")
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DedicatedAlertEntity {

@Id
@GeneratedValue
@Column(columnDefinition = "uuid", updatable = false, name = "id")
private UUID id;

@Column(name = "object_id")
private UUID objectId;

@Column(name = "type")
@Enumerated(EnumType.STRING)
private EventObjectType type;

@ManyToOne(fetch = FetchType.LAZY)
@ToString.Exclude
@EqualsAndHashCode.Exclude
private AlertEntity alertEntity;
}
Original file line number Diff line number Diff line change
@@ -25,13 +25,12 @@
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import jakarta.persistence.*;
import lombok.*;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.enums.EventType;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.enums.MaterialDemandStatus;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import lombok.*;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.enums.EventType;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.enums.MaterialDemandStatus;

@Entity
@Cacheable(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities;

import jakarta.persistence.*;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.enums.AlertThresholdType;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.enums.AlertsMonitoredObjects;

@Entity
@Table(name = "triggered_alerts")
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TriggeredAlertEntity {

@Id
@Column(name = "id")
private UUID id;

@Column(name = "user_id")
private UUID userID;

@Column(name = "alert_name")
private String alertName;

@Column(name = "monitored_objects")
@Enumerated(EnumType.STRING)
private AlertsMonitoredObjects monitoredObjects;

@Column(name = "created")
private String created;

@Column(name = "trigger_times")
private int triggerTimes;

@Column(name = "trigger_times_in_three_months")
private int triggerTimesInThreeMonths;

@Column(name = "type")
@Enumerated(EnumType.STRING)
private AlertThresholdType type;

@Column(name = "threshold")
private double threshold;

@Column(name = "description")
private String description;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.enums;

public enum AlertThresholdType {
ABSOLUTE,
RELATIVE,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.enums;

public enum AlertsMonitoredObjects {
ALL_DEMANDS,
ALL_CAPACITIES,
ALL_OBJECTS,
DEDICATED,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* *******************************************************************************
* Copyright (c) 2023 BMW AG
* Copyright (c) 2023 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.demandcapacitymgmt.demandcapacitymgmtbackend.repositories;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.AlertEntity;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.StatusesEntity;
import org.springframework.data.jpa.repository.JpaRepository;

public interface AlertsRepository extends JpaRepository<AlertEntity, Integer> {
Optional<AlertEntity> findByUserID(UUID userID);
List<AlertEntity> findAllByUserID(UUID userID);
void deleteByUserID(UUID userID);
}
Original file line number Diff line number Diff line change
@@ -23,19 +23,19 @@
package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.repositories;

import jakarta.persistence.Cacheable;
import java.util.List;
import java.util.UUID;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.MaterialDemandEntity;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.enums.MaterialDemandStatus;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

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

@Repository
@Cacheable(false)
public interface MaterialDemandRepository extends JpaRepository<MaterialDemandEntity, UUID> {
List<MaterialDemandEntity> findBySupplierId_Id(UUID id);

@Query("select m from MaterialDemandEntity m where m.customerId.id = ?1")
List<MaterialDemandEntity> findByCustomerId_Id(UUID id);

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* *******************************************************************************
* Copyright (c) 2023 BMW AG
* Copyright (c) 2023 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.demandcapacitymgmt.demandcapacitymgmtbackend.repositories;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.AlertEntity;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.StatusesEntity;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.TriggeredAlertEntity;
import org.springframework.data.jpa.repository.JpaRepository;

public interface TriggeredAlertsRepository extends JpaRepository<TriggeredAlertEntity, Integer> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* *******************************************************************************
* Copyright (c) 2023 BMW AG
* Copyright (c) 2023 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.demandcapacitymgmt.demandcapacitymgmtbackend.services;

import eclipse.tractusx.demand_capacity_mgmt_specification.model.*;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

public interface AlertService {
AlertResponse configureAlert(AlertRequest alertRequest);
void triggerDemandAlertsIfNeeded(
String userID,
boolean isMaterialDemandChange,
double oldDemandValue,
double newDemandValue,
String materialDemandId
);
List<AlertResponse> getAlerts(String userID);
List<TriggeredAlertResponse> getTriggeredAlerts(String userID);
TriggeredAlertResponse postTriggeredAlerts(TriggeredAlertRequest triggeredAlertRequest);
}
Loading

0 comments on commit 7221f17

Please sign in to comment.