Skip to content

Commit

Permalink
Backend/reservation/s02 (#35)
Browse files Browse the repository at this point in the history
* update Reservation

	- updateReservation(Reservaton reservation) added.

Issues: [n/a]
Signed-off-by: Kuzu <[email protected]>

* Moving Reservation

	- moveFromReservationToMeineHardware(Reservation reservation).

* Reservation update

---------

Signed-off-by: Kuzu <[email protected]>
  • Loading branch information
Serberx authored Feb 27, 2024
1 parent f332d51 commit 6331983
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/samic/samic/data/entity/Reservation.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Reservation extends AbstractIdentityClass<Long>{

@OneToMany(mappedBy = "reservation",
fetch = FetchType.LAZY,
cascade = {CascadeType.PERSIST})
cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
private List<StorageObjectHistory> storageObjectHistory = new ArrayList<>();

@JoinColumn(name = "fk_user",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import jakarta.persistence.*;
import jakarta.validation.constraints.PastOrPresent;
import lombok.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import java.time.LocalDateTime;

Expand Down Expand Up @@ -36,8 +38,8 @@ public class StorageObjectHistory extends AbstractIdentityClass<Long>{

@JoinColumn(name = "fk_reservation",
foreignKey = @ForeignKey(name = "fk_reservation_2_storageObjectHistory"))
@ManyToOne(fetch = FetchType.EAGER,
cascade = {CascadeType.MERGE})
@ManyToOne( fetch = FetchType.EAGER)
@OnDelete(action = OnDeleteAction.NO_ACTION)
private Reservation reservation;

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class ServiceLagerObjectErfassen{
private final ServiceStorage serviceStorage;
@Autowired
private final ServiceStorageObject serviceStorageObject;
@Autowired
private final ServiceStorageObjectHistory serviceStorageObjectHistory;


public StorageObject LagerOBjectErfassenCPE(StorageObject storageObject,
Expand All @@ -51,7 +53,7 @@ public StorageObject LagerOBjectErfassenCPE(StorageObject storageObject,
var cpe2 = serviceCPE.saveCPEByObject(cpe);
cpe2.setProducer(producer);
storageObject.setCpe(cpe2);

// serviceStorageObjectHistory.setStorageOBjectHistory(storageObject);
return serviceStorageObject.saveStorageObject(storageObject);

}
Expand All @@ -72,6 +74,7 @@ public StorageObject LagerOBjectErfassenSUPPLY(StorageObject storageObject,
var supply1 = serviceSupply.saveSupplyByObject(supply);
storageObject.setSupply(supply1);

// serviceStorageObjectHistory.setStorageOBjectHistory(storageObject);
return serviceStorageObject.saveStorageObject(storageObject);


Expand Down Expand Up @@ -124,6 +127,7 @@ public StorageObject LagerOBjectErfassenSFP(StorageObject storageObject,
// sfp2.setProducer(producer);
// storageObject.setSfp(sfp2);

// serviceStorageObjectHistory.setStorageOBjectHistory(storageObject);
return serviceStorageObject.saveStorageObject(storageObject3);
}

Expand Down
17 changes: 11 additions & 6 deletions src/main/java/com/samic/samic/services/ServiceReservation.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -20,8 +21,7 @@
@Service
@Transactional
@RequiredArgsConstructor
//@ComponentScan(basePackages = {"com.samic.samic.security"})

@ComponentScan(basePackages = {"com.samic.samic.security"})
public class ServiceReservation{

@Autowired
Expand All @@ -30,6 +30,9 @@ public class ServiceReservation{
private final ServiceStorageObject serviceStorageObject;
@Autowired
private final AuthenticatedUser authenticatedUser;
@Autowired
private final ServiceStorageObjectHistory serviceStorageObjectHistory;


@Transactional
public Reservation saveReservationByObject(Reservation reservation){
Expand Down Expand Up @@ -61,12 +64,12 @@ public Reservation saveReservationByObject(Reservation reservation){

public Reservation updateReservation(Reservation reservation){
if(reservation != null){
if(reservation.getId() != null && reservation.getReservedFrom()
if(reservation.getId() != null && reservation.getReservedFrom().getId()
.equals(authenticatedUser.getUser()
.get())){
.get().getId())){
return repositoryReservation.save(reservation);
}else{
throw new ReservationException(reservation.getId() == null ? "Reservation ID is not set!" : "User does not match with Logged in user!");
throw new ReservationException(reservation.getId() == null ? "Reservation ID is not set!" : "User does not match with Logged-in user!");
}
}else{
throw new ReservationException("Given Reservation is null");
Expand Down Expand Up @@ -114,10 +117,12 @@ public void deleteByObject(Reservation reservation){
if(reservation != null){
if(repositoryReservation.findById(reservation.getId())
.isPresent()){

StorageObject storageObjectByReservationID = serviceStorageObject.findStorageObjectByReservationID(reservation.getId());
reservation.setReservedFrom(null);
StorageObject tempSto = serviceStorageObject.findStorageObjectByReservationID(reservation.getId());
tempSto.setReservation(null);
storageObjectByReservationID.setReservation(null);
serviceStorageObjectHistory.setStorageOBjectHistory(tempSto);
repositoryReservation.deleteById(reservation.getId());

}
Expand Down
87 changes: 67 additions & 20 deletions src/main/java/com/samic/samic/services/ServiceStorageObject.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.samic.samic.services;

import com.samic.samic.data.entity.ObjectType;
import com.samic.samic.data.entity.Reservation;
import com.samic.samic.data.entity.StorageObject;
import com.samic.samic.data.entity.User;
import com.samic.samic.data.entity.*;
import com.samic.samic.data.foundation.DateTimeFactory;
import com.samic.samic.data.repositories.RepositoryObjectType;
import com.samic.samic.data.repositories.RepositoryReservation;
import com.samic.samic.data.repositories.RepositoryStorageObject;
import com.samic.samic.data.repositories.RepositoryStorageObjectHistory;
import com.samic.samic.exceptions.ObjectTypeException;
import com.samic.samic.exceptions.ReservationException;
import com.samic.samic.exceptions.StorageObjectException;
import com.samic.samic.exceptions.UserException;
import com.samic.samic.security.AuthenticatedUser;
import com.vaadin.flow.data.provider.Query;
import com.vaadin.flow.spring.data.VaadinSpringDataHelpers;
import jakarta.persistence.AttributeOverride;
import jakarta.persistence.EntityManagerFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
Expand All @@ -32,16 +32,26 @@
@Transactional
@RequiredArgsConstructor
@ComponentScan(basePackages = "com.samic.samic.data.services")
@ComponentScan(basePackages = {"com.samic.samic.security"})

public class ServiceStorageObject{

@Autowired
private final RepositoryStorageObject repositoryStorageObject;
@Autowired
private final RepositoryReservation repositoryReservation;
private final RepositoryReservation repositoryReservation;
@Autowired
private final EntityManagerFactory emf;
@Autowired
private final RepositoryObjectType repositoryObjectType;
private final RepositoryObjectType repositoryObjectType;
@Autowired
private final RepositoryStorageObjectHistory repositoryStorageObjectHistory;
@Autowired
private final ServiceStorageObjectHistory serviceStorageObjectHistory;
// @Autowired
// private final ServiceReservation serviceReservation;
@Autowired
private final AuthenticatedUser authenticatedUser;
// private final Logger log = LoggerFactory.getLogger(this.getClass());
// public ServiceStorageObject(@Qualifier("storageObject") RepositoryStorageObject repositoryStorageObject){
// this.repositoryStorageObject = repositoryStorageObject;
Expand All @@ -55,8 +65,9 @@ public StorageObject saveStorageObject(StorageObject storageObject){
}*/

if(storageObject.getStoredAtUser() != null){
if(storageObject.getReservation() != null ){
if(storageObject.getReservation().getReservedFrom() != null){
if(storageObject.getReservation() != null){
if(storageObject.getReservation()
.getReservedFrom() != null){
throw new ReservationException("User already set to STO!");
}
}
Expand Down Expand Up @@ -110,6 +121,7 @@ public StorageObject saveStorageObject(StorageObject storageObject){
log.debug("saveStorageObejct() | given StorageObject is set to fetched StorageObject, fetchedStorageObject: {},\n storageObject: {}",
fetchedStorageObject,
storageObject);
serviceStorageObjectHistory.setStorageOBjectHistory(fetchedStorageObject);
return repositoryStorageObject.save(fetchedStorageObject);
}else{
log.debug("saveStorageObject() | fetched StorageObject does not match with given StorageObject, fetchedStorageObject: {},\nStorageObject: {}",
Expand All @@ -126,12 +138,14 @@ public StorageObject saveStorageObject(StorageObject storageObject){
}else{
log.debug("saveStorageObject() | StorageObject does not exist in DB, ID: {},\nStorageObject: {}",
storageObject.getId());
serviceStorageObjectHistory.setStorageOBjectHistory(storageObject);
return repositoryStorageObject.save(storageObject);

}
}else{
log.debug("saveStorageObject() | StorageObject id is null, Saving StorageObject: {}",
storageObject); //TODO T3600
serviceStorageObjectHistory.setStorageOBjectHistory(storageObject);
StorageObject saved = repositoryStorageObject.save(storageObject);
return saved;
}
Expand Down Expand Up @@ -697,17 +711,18 @@ public Map<String, Long> findAmountOfObjectType2(){
//Einzelwert für die Anzahl der Geräte holen
public Long findAmountOfObjectType2(String objectType){
return repositoryStorageObject.findAll()
.stream()
.filter(a -> a.getObjectTypeName() != null)
.filter(b -> b.getObjectTypeName()
.getName()
.equals(objectType)).count();
// return repositoryStorageObject.findAll()
// .stream()
// .filter(name -> name.getObjectTypeName()
// .getName()
// .equals(objectType))
// .count();
.stream()
.filter(a -> a.getObjectTypeName() != null)
.filter(b -> b.getObjectTypeName()
.getName()
.equals(objectType))
.count();
// return repositoryStorageObject.findAll()
// .stream()
// .filter(name -> name.getObjectTypeName()
// .getName()
// .equals(objectType))
// .count();
}

// public Stream<StorageObject> findStorageObjectByUserId(Long id, PageRequest query){
Expand All @@ -720,4 +735,36 @@ public Stream<Reservation> findStorageObjectByUserId(Long id,
request)
.stream();
}

public void moveFromReservationToMeineHardware(Reservation reservation){
if(reservation != null){
if(reservation.getReservedFrom() == authenticatedUser.getUser()
.get()){
if(reservation.getId() != null && repositoryReservation.existsById(reservation.getId())){
Reservation reservationById = repositoryReservation.findById(reservation.getId())
.get();

StorageObject storageObjectByReservationId = repositoryStorageObject.findStorageObjectByReservation_Id(reservationById.getId());
storageObjectByReservationId.setRemark(reservationById.getReservedDescription());

// reservationById.setReservedFrom(null);
// storageObjectByReservationId.setReservation(null);
reservationById.setReservedFrom(null);
storageObjectByReservationId.setReservation(null);
repositoryReservation.deleteById(Objects.requireNonNull(reservationById.getId()));
// serviceReservation.deleteByObject(reservationById);

serviceStorageObjectHistory.setStorageOBjectHistory(storageObjectByReservationId);
saveStorageObject(storageObjectByReservationId);

}else{
throw new StorageObjectException(reservation.getId() == null ? "Reservation ID is null!" : "Reservation doesn't exist in DB!");
}
}else{
throw new StorageObjectException("User does not match with the logged-in User!");
}
}else{
throw new StorageObjectException("Given Reservation is null!");
}
}
}
Loading

0 comments on commit 6331983

Please sign in to comment.