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

Refactor/move request property to negotiation #459

Open
wants to merge 15 commits into
base: master
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 @@ -18,20 +18,22 @@ public interface ResourceRepository
@Query(
value =
"""
select rs.id as id,
r.negotiation_id as negotiationId,
rs.name as name,
rs.source_id as sourceId,
rspn.current_state as currentState,
o.name as organizationName,
o.external_id as organizationExternalId,
o.id as organizationId
select
rs.id as id,
nrl.negotiation_id as negotiationId,
rs.name as name,
rs.source_id as sourceId,
rspn.current_state as currentState,
o.name as organizationName,
o.external_id as organizationExternalId,
o.id as organizationId
from resource rs
join public.request_resources_link rrl on rs.id = rrl.resource_id
join public.organization o on o.id = rs.organization_id
join public.request r on r.id = rrl.request_id
left join public.resource_state_per_negotiation rspn on rs.source_id = rspn.resource_id and r.negotiation_id = rspn.negotiation_id
where r.negotiation_id = :negotiationId;
join public.negotiation_resource_link nrl on rs.id = nrl.resource_id
join public.organization o on o.id = rs.organization_id
left join public.resource_state_per_negotiation rspn on rs.source_id = rspn.resource_id and nrl.negotiation_id = rspn.negotiation_id
where
nrl.negotiation_id = :negotiationId
order by rs.source_id;
""",
nativeQuery = true)
List<ResourceViewDTO> findByNegotiation(String negotiationId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import eu.bbmri_eric.negotiator.negotiation.NegotiationRepository;
import eu.bbmri_eric.negotiator.negotiation.NewResourcesAddedEvent;
import eu.bbmri_eric.negotiator.negotiation.dto.UpdateResourcesDTO;
import eu.bbmri_eric.negotiator.negotiation.request.Request;
import eu.bbmri_eric.negotiator.negotiation.request.RequestRepository;
import eu.bbmri_eric.negotiator.negotiation.state_machine.negotiation.NegotiationState;
import eu.bbmri_eric.negotiator.negotiation.state_machine.resource.NegotiationResourceState;
Expand Down Expand Up @@ -131,11 +130,9 @@
}

private void persistChanges(Negotiation negotiation, Set<Resource> resources) {
Request request = negotiation.getRequests().iterator().next();
negotiationRepository.saveAndFlush(negotiation);
resources.addAll(negotiation.getResources());
request.setResources(resources);
requestRepository.saveAndFlush(request);
negotiation.setResources(resources);
negotiationRepository.saveAndFlush(negotiation);

Check warning on line 135 in src/main/java/eu/bbmri_eric/negotiator/governance/resource/ResourceServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/eu/bbmri_eric/negotiator/governance/resource/ResourceServiceImpl.java#L134-L135

Added lines #L134 - L135 were not covered by tests
if (negotiation.getCurrentState().equals(NegotiationState.IN_PROGRESS)) {
applicationEventPublisher.publishEvent(new NewResourcesAddedEvent(this, negotiation.getId()));
}
Expand Down Expand Up @@ -167,19 +164,9 @@
}

private Negotiation getNegotiation(String negotiationId) {
Negotiation negotiation =
negotiationRepository
.findById(negotiationId)
.orElseThrow(() -> new EntityNotFoundException(negotiationId));
return negotiation;
}

private Request getRequest(String negotiationId) {
Request request =
requestRepository
.findByNegotiation_Id(negotiationId)
.orElseThrow(() -> new EntityNotFoundException(negotiationId));
return request;
return negotiationRepository
.findById(negotiationId)
.orElseThrow(() -> new EntityNotFoundException(negotiationId));

Check warning on line 169 in src/main/java/eu/bbmri_eric/negotiator/governance/resource/ResourceServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/eu/bbmri_eric/negotiator/governance/resource/ResourceServiceImpl.java#L167-L169

Added lines #L167 - L169 were not covered by tests
}

private boolean userIsntAuthorized(String negotiationId, Long userId) {
Expand Down
88 changes: 58 additions & 30 deletions src/main/java/eu/bbmri_eric/negotiator/negotiation/Negotiation.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package eu.bbmri_eric.negotiator.negotiation;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.vladmihalcea.hibernate.type.json.JsonType;
import eu.bbmri_eric.negotiator.attachment.Attachment;
import eu.bbmri_eric.negotiator.common.AuditEntity;
import eu.bbmri_eric.negotiator.discovery.DiscoveryService;
import eu.bbmri_eric.negotiator.governance.resource.Resource;
import eu.bbmri_eric.negotiator.negotiation.request.Request;
import eu.bbmri_eric.negotiator.negotiation.state_machine.negotiation.NegotiationLifecycleRecord;
import eu.bbmri_eric.negotiator.negotiation.state_machine.negotiation.NegotiationState;
import eu.bbmri_eric.negotiator.negotiation.state_machine.resource.NegotiationResourceLifecycleRecord;
Expand All @@ -17,15 +18,15 @@
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MapKeyColumn;
import jakarta.persistence.NamedAttributeNode;
import jakarta.persistence.NamedEntityGraph;
import jakarta.persistence.NamedSubgraph;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -52,18 +53,6 @@
@Builder
@Table(name = "negotiation")
@Convert(converter = JsonType.class, attributeName = "json")
@NamedEntityGraph(
name = "negotiation-with-detailed-children",
attributeNodes = {
@NamedAttributeNode(value = "requests", subgraph = "requests-detailed"),
@NamedAttributeNode(value = "attachments"),
@NamedAttributeNode(value = "currentStatePerResource")
},
subgraphs = {
@NamedSubgraph(
name = "requests-detailed",
attributeNodes = {@NamedAttributeNode(value = "resources")})
})
public class Negotiation extends AuditEntity {

@Id
Expand All @@ -77,9 +66,12 @@
cascade = {CascadeType.MERGE})
private Set<Attachment> attachments;

@OneToMany(mappedBy = "negotiation", cascade = CascadeType.MERGE)
@Exclude
private Set<Request> requests;
@NotNull
@Column(columnDefinition = "TEXT")
private String humanReadable = "";

@OneToMany @Exclude @NotNull @Builder.Default
private Set<NegotiationResourceLink> resourcesLink = new HashSet<>();

@Formula(value = "JSONB_EXTRACT_PATH_TEXT(payload, 'project', 'title')")
private String title;
Expand Down Expand Up @@ -120,6 +112,13 @@
private Set<NegotiationResourceLifecycleRecord> negotiationResourceLifecycleRecords =
new HashSet<>();

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "discovery_service_id")
@JsonIgnore
@NotNull
@Exclude
private DiscoveryService discoveryService;

private static Set<NegotiationLifecycleRecord> creteInitialHistory() {
Set<NegotiationLifecycleRecord> history = new HashSet<>();
history.add(NegotiationLifecycleRecord.builder().changedTo(NegotiationState.SUBMITTED).build());
Expand All @@ -143,6 +142,16 @@
}
}

public static CustomNegotiationBuilder builder() {
return new CustomNegotiationBuilder();
}

public Set<Resource> getResources() {
return getResourcesLink().stream()
.map(NegotiationResourceLink::getResource)
.collect(Collectors.toSet());
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -160,21 +169,40 @@
return Objects.hash(getId());
}

/**
* Returns all resources involved in the negotiation.
*
* @return an UnmodifiableSet of resources
*/
public Set<Resource> getResources() {
return requests.stream()
.flatMap(request -> request.getResources().stream())
.collect(Collectors.toUnmodifiableSet());
}

private Resource lookupResource(Set<Resource> resources, String resourceId) {
return resources.stream()
.filter(r -> r.getSourceId().equals(resourceId))
.findFirst()
.orElse(null);
}

public void setResources(Set<Resource> resources) {
resources.forEach(
resource -> this.resourcesLink.add(new NegotiationResourceLink(this, resource, null)));
}

public static class NegotiationBuilder {
public NegotiationBuilder resources(Set<Resource> resources) {
return this;

Check warning on line 186 in src/main/java/eu/bbmri_eric/negotiator/negotiation/Negotiation.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/eu/bbmri_eric/negotiator/negotiation/Negotiation.java#L186

Added line #L186 was not covered by tests
}
}

public static class CustomNegotiationBuilder extends NegotiationBuilder {
private Set<Resource> resources;

@Override
public CustomNegotiationBuilder resources(Set<Resource> resources) {
this.resources = resources;
return this;
}

@Override
public Negotiation build() {
Negotiation negotiation = super.build();
if (this.resources != null) {
negotiation.setResources(this.resources);
}
return negotiation;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ List<Negotiation> findByModifiedDateBeforeAndCurrentState(
"SELECT EXISTS ("
+ "SELECT distinct(n.id) "
+ "FROM negotiation n "
+ " JOIN request rq ON n.id = rq.negotiation_id "
+ " JOIN request_resources_link rrl ON rrl.request_id = rq.id "
+ " JOIN negotiation_resource_link rrl ON rrl.negotiation_id = n.id "
+ " JOIN resource rs ON rrl.resource_id = rs.id "
+ " JOIN organization o ON rs.organization_id = o.id "
+ "WHERE n.id = :negotiationId and o.external_id = :organizationExternalId)",
Expand All @@ -53,8 +52,8 @@ List<Negotiation> findByModifiedDateBeforeAndCurrentState(
value =
"SELECT distinct (n) "
+ "FROM Negotiation n "
+ "JOIN n.requests rq "
+ "JOIN rq.resources rs "
+ "JOIN n.resourcesLink rl "
+ "JOIN rl.id.resource rs "
+ "JOIN rs.networks net "
+ "WHERE net.id = :networkId")
Page<Negotiation> findAllForNetwork(Long networkId, Pageable pageable);
Expand All @@ -63,8 +62,8 @@ List<Negotiation> findByModifiedDateBeforeAndCurrentState(
value =
"select count (distinct n.id) "
+ "FROM Negotiation n "
+ "JOIN n.requests rq "
+ "JOIN rq.resources rs "
+ "JOIN n.resourcesLink rl "
+ "JOIN rl.id.resource rs "
+ "JOIN rs.networks net "
+ "WHERE net.id = :networkId")
Integer countAllForNetwork(Long networkId);
Expand All @@ -73,8 +72,8 @@ List<Negotiation> findByModifiedDateBeforeAndCurrentState(
value =
"select n.currentState, COUNT ( distinct n.id)"
+ "FROM Negotiation n "
+ "JOIN n.requests rq "
+ "JOIN rq.resources rs "
+ "JOIN n.resourcesLink rl "
+ "JOIN rl.id.resource rs "
+ "JOIN rs.networks net "
+ "WHERE net.id = :networkId group by n.currentState")
List<Object[]> countStatusDistribution(Long networkId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package eu.bbmri_eric.negotiator.negotiation;

import eu.bbmri_eric.negotiator.governance.resource.Resource;
import eu.bbmri_eric.negotiator.negotiation.state_machine.resource.NegotiationResourceState;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Table(name = "negotiation_resource_link")
public class NegotiationResourceLink {
@EmbeddedId private NegotiationResourceLinkId id;

private NegotiationResourceState currentState;

public NegotiationResourceLink(
Negotiation negotiation, Resource resource, NegotiationResourceState currentState) {
this.id = new NegotiationResourceLinkId(negotiation, resource);
this.currentState = currentState;
}

public Negotiation getNegotiation() {
return this.id.getNegotiation();

Check warning on line 32 in src/main/java/eu/bbmri_eric/negotiator/negotiation/NegotiationResourceLink.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/eu/bbmri_eric/negotiator/negotiation/NegotiationResourceLink.java#L32

Added line #L32 was not covered by tests
}

public Resource getResource() {
return this.id.getResource();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package eu.bbmri_eric.negotiator.negotiation;

import eu.bbmri_eric.negotiator.governance.resource.Resource;
import jakarta.persistence.Embeddable;
import jakarta.persistence.ManyToOne;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Embeddable
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
public class NegotiationResourceLinkId {
@ManyToOne private Negotiation negotiation;

@ManyToOne private Resource resource;
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ NegotiationDTO create(NegotiationCreateDTO negotiationBody, Long creatorId)
NegotiationDTO update(String negotiationId, NegotiationCreateDTO negotiationBody)
throws EntityNotFoundException;

/**
* Adds the request with :requestId to the negotiation with identified by :negotiationId
*
* @param negotiationId the id of the negotiation
* @param requestId the id of the request to add to the negotiation
* @return a NegotiationID with the data of the negotiation
*/
NegotiationDTO addRequestToNegotiation(String negotiationId, String requestId);

/**
* Returns a list of all negotiation in the negotiator
*
Expand Down
Loading
Loading