Skip to content

Commit

Permalink
Adapt repo and mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
dwendland committed Oct 2, 2023
1 parent 0784432 commit d14d812
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 67 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/**
56 changes: 48 additions & 8 deletions src/main/java/org/fiware/iam/ServiceMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,58 @@
import org.mapstruct.Mapper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
* Responsible for mapping entities from the Service api domain to the internal model.
*/
@Mapper(componentModel = "jsr330")
public interface ServiceMapper {

Service map(ServiceVO serviceVO);
default Service map(ServiceVO serviceVO) {
return new Service()
.setDefaultOidcScope(serviceVO.getDefaultOidcScope())
.setId(serviceVO.getId())
.setOidcScopes(map(serviceVO.getOidcScopes(), serviceVO.getId()));
}

ServiceVO map(Service service);

ServiceScopesEntry map(ServiceScopesEntryVO serviceScopesEntryVO);
default ServiceScope map(ServiceScopesEntryVO serviceScopesEntryVO, String scopeName, String serviceName) {
return new ServiceScope()
.setId("%s_%s".formatted(scopeName, serviceName))
.setScopeName(scopeName)
.setCredentials(serviceScopesEntryVO.stream().map(this::map).toList());
}

ServiceScopesEntryVO map(ServiceScopesEntry serviceScopesEntry);
ServiceScopesEntryVO map(ServiceScope serviceScope);

ServiceScopes map(ServiceScopesVO serviceScopesVO);
default Collection<ServiceScope> map(ServiceScopesVO value, String serviceName) {
if (value.getAdditionalProperties() == null) {
return List.of();
}
return value
.getAdditionalProperties()
.entrySet()
.stream()
.map(e -> map(e.getValue(), e.getKey(), serviceName))
.toList();
}

ServiceScopesVO map(ServiceScopes serviceScopes);
default ServiceScopesVO mapEntries(Collection<ServiceScope> value) {
ServiceScopesVO mappedScopes = new ServiceScopesVO();
if (value != null) {
value.forEach(e -> {
ServiceScopesEntryVO scopes = new ServiceScopesEntryVO();
scopes.addAll(map(e.getCredentials()));
mappedScopes.setAdditionalProperties(e.getScopeName(), scopes);
}
);
}
return mappedScopes;
}

default Credential map(CredentialVO credentialVO) {
if (credentialVO == null) {
Expand All @@ -35,12 +68,19 @@ default Credential map(CredentialVO credentialVO) {
Credential credential = new Credential()
.setCredentialType(credentialVO.getType());
List<EndpointEntry> trustedList = new ArrayList<>();
trustedList.addAll(issuersToEntries(credentialVO.getTrustedIssuersLists()));
trustedList.addAll(participantsToEntries(credentialVO.getTrustedParticipantsLists()));
Optional.ofNullable(issuersToEntries(credentialVO.getTrustedIssuersLists())).ifPresent(trustedList::addAll);
Optional.ofNullable(participantsToEntries(credentialVO.getTrustedParticipantsLists())).ifPresent(trustedList::addAll);
credential.setTrustedLists(trustedList);
return credential;
}

default Collection<CredentialVO> map(Collection<Credential> credentials) {
if (credentials == null) {
return null;
}
return credentials.stream().map(this::map).toList();
}

default CredentialVO map(Credential credential) {
if (credential == null) {
return null;
Expand Down Expand Up @@ -107,4 +147,4 @@ default List<String> entriesToParticipants(List<EndpointEntry> endpoints) {
.toList();
}

}
}
10 changes: 5 additions & 5 deletions src/main/java/org/fiware/iam/repository/Credential.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
@Data
@Accessors(chain = true)
@Entity
@EqualsAndHashCode(exclude = "service")
@ToString(exclude = "service")
@EqualsAndHashCode(exclude = "serviceScope")
@ToString(exclude = "serviceScope")
public class Credential {

@GeneratedValue
Expand All @@ -37,10 +37,10 @@ public class Credential {
private List<EndpointEntry> trustedLists;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "service_id")
private Service service;
@JoinColumn(name = "service_scope_id")
private ServiceScope serviceScope;




}
}
8 changes: 3 additions & 5 deletions src/main/java/org/fiware/iam/repository/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public class Service {

private String defaultOidcScope;

private ServiceScopes oidcScopes;

//@OneToMany(mappedBy = "service", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
//private Collection<Credential> credentials;
}
@OneToMany(mappedBy = "service", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
private Collection<ServiceScope> oidcScopes;
}
11 changes: 6 additions & 5 deletions src/main/java/org/fiware/iam/repository/ServiceRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public interface ServiceRepository extends PageableRepository<Service, String> {
* @param id of the service
* @return the complete service
*/
@Join(value = "credentials", type = Join.Type.LEFT_FETCH)
@Join(value = "credentials.trustedLists", type = Join.Type.LEFT_FETCH)
@Join(value = "oidcScopes", type = Join.Type.LEFT_FETCH)
@Join(value = "oidcScopes.credentials", type = Join.Type.LEFT_FETCH)
@Join(value = "oidcScopes.credentials.trustedLists", type = Join.Type.LEFT_FETCH)
Service getById(String id);

/**
Expand All @@ -30,8 +31,8 @@ public interface ServiceRepository extends PageableRepository<Service, String> {
* @return the current page
*/
@NonNull
@Join(value = "credentials", type = Join.Type.LEFT_FETCH)
@Join(value = "credentials.trustedLists", type = Join.Type.LEFT_FETCH)
@Join(value = "oidcScopes", type = Join.Type.LEFT_FETCH)
@Join(value = "oidcScopes.credentials", type = Join.Type.LEFT_FETCH)
@Join(value = "oidcScopes.credentials.trustedLists", type = Join.Type.LEFT_FETCH)
Page<Service> findAll(@NonNull Pageable pageable);
}

33 changes: 33 additions & 0 deletions src/main/java/org/fiware/iam/repository/ServiceScope.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.fiware.iam.repository;

import io.micronaut.core.annotation.Introspected;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.Collection;
import java.util.UUID;

@Introspected
@Accessors(chain = true)
@Data
@Entity
@ToString(exclude = "service")
@EqualsAndHashCode(exclude = "service")
public class ServiceScope {

@Id
private String id;

private String scopeName;

@OneToMany(mappedBy = "serviceScope", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
private Collection<Credential> credentials;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "service_id")
private Service service;

}
19 changes: 0 additions & 19 deletions src/main/java/org/fiware/iam/repository/ServiceScopes.java

This file was deleted.

21 changes: 0 additions & 21 deletions src/main/java/org/fiware/iam/repository/ServiceScopesEntry.java

This file was deleted.

16 changes: 12 additions & 4 deletions src/main/resources/db/migration/V0_0_1__initial-migration.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
CREATE TABLE IF NOT EXISTS `service` (
`id` varchar(255) NOT NULL PRIMARY KEY
`id` varchar(255) NOT NULL PRIMARY KEY,
`default_oidc_scope` varchar(255) NOT NULL
);

CREATE TABLE IF NOT EXISTS `service_scope` (
`id` varchar(255) NOT NULL PRIMARY KEY,
`scope_name` varchar(255) NOT NULL,
`service_id` varchar(255) NOT NULL,
CONSTRAINT `fk_service` FOREIGN KEY (`service_id`) REFERENCES `service` (`id`) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS `credential` (
`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
`credential_type` varchar(255) NOT NULL,
`service_id` varchar(255) NOT NULL,
CONSTRAINT `fk_service` FOREIGN KEY (`service_id`) REFERENCES `service` (`id`) ON DELETE CASCADE
`service_scope_id` varchar(255) NOT NULL,
CONSTRAINT `fk_scope` FOREIGN KEY (`service_scope_id`) REFERENCES `service_scope` (`id`) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS `endpoint_entry` (
Expand All @@ -15,4 +23,4 @@ CREATE TABLE IF NOT EXISTS `endpoint_entry` (
`type` varchar(100) NOT NULL,
`credential_id` int NOT NULL,
CONSTRAINT `fk_credential` FOREIGN KEY (`credential_id`) REFERENCES `credential` (`id`) ON DELETE CASCADE
);
);

0 comments on commit d14d812

Please sign in to comment.