Commit da8bd9e8 authored by femiadeyemi's avatar femiadeyemi
Browse files

add relationship between user and service entity

Motivation:

User belonging to known Helmholtz organisation can create
and be part of a management team to manage a marketplace
service. This patch introduce the code to model this
relationship and expose it usage through the restful API.

Modification:

- create a relationship class for "MANAGES" called Management.
- provide the ability to add and delete the relationship. Hence,
    the market-service controller expose the api, the service
    provide the bridge between the api and the repository. The
    repository contain the neccessary query to interact with
    the database.
- fix minor typo inside the market-service-controller,
    market-service-service and market-service-repository class
- overide both the equals and hashCode method of the
    market-service class

Result:

MANAGES relationship can now be added and deleted through the
restful api.

Target: master
Review-at: https://gitlab.hzdr.de/hifis-technical-platform/helmholtz-cerebrum/-/merge_requests/33
parent 5bf5cac9
Pipeline #41893 failed with stages
in 8 minutes and 2 seconds
......@@ -33,6 +33,7 @@ import javax.validation.constraints.Min;
import java.util.List;
import de.helmholtz.marketplace.cerebrum.entities.MarketService;
import de.helmholtz.marketplace.cerebrum.entities.relationship.Management;
import de.helmholtz.marketplace.cerebrum.entities.relationship.ServiceProvider;
import de.helmholtz.marketplace.cerebrum.errorhandling.CerebrumApiError;
import de.helmholtz.marketplace.cerebrum.service.MarketServiceService;
......@@ -202,4 +203,26 @@ public class MarketServiceController
{
return marketServiceService.deleteProviders(serviceKey, serviceValue, organizationKey, organizationValue);
}
//Management team
@PreAuthorize("isAuthenticated()")
@Operation(security = @SecurityRequirement(name = "hdf-aai"))
@PostMapping(path = "/management",
consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<MarketService> addAffiliation(@Valid @RequestBody Management management)
{
return marketServiceService.addTeamMember(management);
}
@PreAuthorize("isAuthenticated()")
@Operation(security = @SecurityRequirement(name = "hdf-aai"))
@DeleteMapping(path = "/management")
public ResponseEntity<MarketService> deleteTeamMember(
@RequestParam String serviceKey,
@RequestParam String serviceValue,
@RequestParam String userKey,
@RequestParam String userValue)
{
return marketServiceService.deleteTeamMember(serviceKey, serviceValue, userKey, userValue);
}
}
......@@ -9,11 +9,14 @@ import org.neo4j.ogm.annotation.Relationship;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import de.helmholtz.marketplace.cerebrum.entities.relationship.Management;
import de.helmholtz.marketplace.cerebrum.entities.relationship.ServiceProvider;
import de.helmholtz.marketplace.cerebrum.utils.CerebrumEntityUuidGenerator;
import static de.helmholtz.marketplace.cerebrum.utils.CerebrumEntityUuidGenerator.generate;
import static org.neo4j.ogm.annotation.Relationship.INCOMING;
public class MarketService
{
......@@ -50,6 +53,10 @@ public class MarketService
@Relationship(type = "HOSTED_BY")
private List<ServiceProvider> serviceProviders;
@JsonIgnoreProperties({"marketService"})
@Relationship(type = "MANAGES", direction = INCOMING)
private List<Management> managementTeam;
public String getUuid()
{
return uuid;
......@@ -137,8 +144,35 @@ public class MarketService
return serviceProviders;
}
public void setOrganizations(List<ServiceProvider> serviceProviders)
public void setServiceProviders(List<ServiceProvider> serviceProviders)
{
this.serviceProviders = serviceProviders;
}
public List<Management> getManagementTeam()
{
return managementTeam;
}
public void setManagementTeam(List<Management> managementTeam)
{
this.managementTeam = managementTeam;
}
@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MarketService service = (MarketService) o;
return name.equals(service.name) &&
url.equals(service.url) &&
Objects.equals(created, service.created);
}
@Override
public int hashCode()
{
return Objects.hash(name, url, created);
}
}
......@@ -14,6 +14,7 @@ import java.util.List;
import java.util.Objects;
import de.helmholtz.marketplace.cerebrum.entities.relationship.Affiliation;
import de.helmholtz.marketplace.cerebrum.entities.relationship.Management;
import de.helmholtz.marketplace.cerebrum.utils.CerebrumEntityUuidGenerator;
import static de.helmholtz.marketplace.cerebrum.utils.CerebrumEntityUuidGenerator.generate;
......@@ -58,6 +59,10 @@ public class MarketUser
@Relationship(type = "BELONGS_TO")
private List<Affiliation> affiliations;
@JsonIgnoreProperties("marketUser")
@Relationship(type = "MANAGES")
private List<Management> managedServices;
public String getUuid()
{
return uuid;
......@@ -129,6 +134,16 @@ public class MarketUser
this.affiliations = affiliations;
}
public List<Management> getManagedServices()
{
return managedServices;
}
public void setManagedServices(List<Management> managedServices)
{
this.managedServices = managedServices;
}
@Override
public boolean equals(Object o)
{
......
package de.helmholtz.marketplace.cerebrum.entities.relationship;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.neo4j.ogm.annotation.EndNode;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.RelationshipEntity;
import org.neo4j.ogm.annotation.StartNode;
import java.util.Arrays;
import java.util.Objects;
import de.helmholtz.marketplace.cerebrum.entities.MarketService;
import de.helmholtz.marketplace.cerebrum.entities.MarketUser;
@RelationshipEntity(type = "MANAGES")
public class Management
{
@Id @GeneratedValue Long id;
private String[] roles = {"OTHERS"};
@StartNode
@JsonIgnoreProperties({"managedServices"})
private MarketUser marketUser;
@EndNode
@JsonIgnoreProperties({"managementTeam"})
private MarketService marketService;
public String[] getRoles()
{
return roles;
}
public void setRoles(String[] roles)
{
this.roles = roles;
}
public MarketUser getMarketUser()
{
return marketUser;
}
public void setMarketUser(MarketUser marketUser)
{
this.marketUser = marketUser;
}
public MarketService getMarketService() {
return marketService;
}
public void setMarketService(MarketService marketService)
{
this.marketService = marketService;
}
@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Management that = (Management) o;
return Arrays.equals(roles, that.roles) &&
marketUser.equals(that.marketUser) &&
marketService.equals(that.marketService);
}
@Override
public int hashCode()
{
return Objects.hash(roles, marketUser, marketService);
}
}
......@@ -20,16 +20,30 @@ public interface MarketServiceRepository extends Neo4jRepository<MarketService,
"WHERE service.uuid = $serviceUuid AND org.uuid = $orgUuid " +
"CREATE (service)-[r:HOSTED_BY { serviceTechnicalName : $serviceTechnicalName }]->(org) " +
"RETURN service, r")
MarketService createHostedInRelationship(@Param("serviceUuid") String serviceUuid,
MarketService createHostedByRelationship(@Param("serviceUuid") String serviceUuid,
@Param("orgUuid") String orgUuid,
@Param("serviceTechnicalName") String serviceTechnicalName);
@Query("MATCH (service:MarketService),(user:MarketUser) " +
"WHERE service.uuid = $serviceUuid AND user.uuid = $orgUuid " +
"CREATE (user)-[r:MANAGES { roles : $roles }]->(service) " +
"RETURN service, r")
MarketService createManagesRelationship(@Param("serviceUuid") String serviceUuid,
@Param("orgUuid") String orgUuid,
@Param("roles") String[] roles);
@SuppressWarnings("UnusedReturnValue")
@Query("MATCH (service:MarketService)-[r:HOSTED_BY]->(org:Organization) " +
"WHERE service.uuid = $serviceUuid AND org.uuid = $orgUuid " +
"DELETE r")
Long deleteServiceProviders(@Param("serviceUuid") String serviceUuid, @Param("orgUuid") String orgUuid);
@SuppressWarnings("UnusedReturnValue")
@Query("MATCH (user:MarketUser)-[r:MANAGES]->(service:MarketService) " +
"WHERE service.uuid = $serviceUuid AND user.uuid = $userUuid " +
"DELETE r")
Long deleteManagementMember(@Param("serviceUuid") String serviceUuid, @Param("userUuid") String userUuid);
@SuppressWarnings("UnusedReturnValue")
Long deleteByUuid(String id);
}
......@@ -12,7 +12,9 @@ import org.springframework.web.util.UriComponentsBuilder;
import java.lang.reflect.Field;
import de.helmholtz.marketplace.cerebrum.entities.MarketService;
import de.helmholtz.marketplace.cerebrum.entities.MarketUser;
import de.helmholtz.marketplace.cerebrum.entities.Organization;
import de.helmholtz.marketplace.cerebrum.entities.relationship.Management;
import de.helmholtz.marketplace.cerebrum.entities.relationship.ServiceProvider;
import de.helmholtz.marketplace.cerebrum.repository.MarketServiceRepository;
import de.helmholtz.marketplace.cerebrum.service.common.CerebrumServiceBase;
......@@ -24,13 +26,16 @@ public class MarketServiceService extends CerebrumServiceBase<MarketService, Mar
{
private final MarketServiceRepository marketServiceRepository;
private final OrganizationService organizationService;
private final MarketUserService marketUserService;
public MarketServiceService(MarketServiceRepository marketServiceRepository,
OrganizationService organizationService)
OrganizationService organizationService,
MarketUserService marketUserService)
{
super(MarketService.class, MarketServiceRepository.class);
this.marketServiceRepository = marketServiceRepository;
this.organizationService = organizationService;
this.marketUserService = marketUserService;
}
public Page<MarketService> getServices(PageRequest page)
......@@ -87,7 +92,7 @@ public class MarketServiceService extends CerebrumServiceBase<MarketService, Mar
}
}
Organization organization = organizationService.getOrganization(inputOrganization.getUuid());
MarketService updatedService = marketServiceRepository.createHostedInRelationship(
MarketService updatedService = marketServiceRepository.createHostedByRelationship(
service.getUuid(), organization.getUuid(), serviceProvider.getServiceTechnicalName());
return ResponseEntity.ok().body(updatedService);
} else {
......@@ -123,4 +128,53 @@ public class MarketServiceService extends CerebrumServiceBase<MarketService, Mar
}
return ResponseEntity.noContent().build();
}
//Service Management Team
public ResponseEntity<MarketService> addTeamMember(Management management)
{
MarketService inputService = management.getMarketService();
MarketUser inputUser = management.getMarketUser();
if (inputService.getUuid() != null && inputUser.getUuid() != null) {
MarketService service = getServiceByAttributes("uuid", inputService.getUuid());
if (service.getManagementTeam() != null) {
for (Management member : service.getManagementTeam()) {
if (member.equals(management)) {
return ResponseEntity.noContent().build();
}
}
}
MarketUser user = marketUserService.getUser(inputUser.getUuid());
MarketService updatedService = marketServiceRepository.createManagesRelationship(
service.getUuid(), user.getUuid(), management.getRoles());
return ResponseEntity.ok().body(updatedService);
}
return null;
}
public ResponseEntity<MarketService> deleteTeamMember(
String serviceKey, String serviceValue, String userKey, String userValue)
{
Boolean serviceFieldExist = checkField(serviceKey, MarketService.class);
Boolean userFieldExist = checkField(userKey, MarketUser.class);
if (serviceFieldExist && userFieldExist) {
MarketService service = getServiceByAttributes(serviceKey, serviceValue);
if (service.getManagementTeam() != null) {
for (Management member : service.getManagementTeam()) {
try {
Field field = MarketUser.class.getDeclaredField(userKey);
field.setAccessible(true);
if (field.get(member.getMarketUser()).equals(userValue)) {
marketServiceRepository.deleteManagementMember(
service.getUuid(), member.getMarketUser().getUuid());
break;
}
} catch (IllegalAccessException | NoSuchFieldException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
}
}
}
}
return ResponseEntity.noContent().build();
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment