Skip to content

Commit

Permalink
propepr wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
wistefan committed Jun 21, 2024
1 parent 46d08e6 commit 7beb081
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/fiware/odrl/BundleResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public Response getPolicies() {
String mainPolicy = getMainPolicy(policies);

var toZip = policies.entrySet().stream()
.collect(Collectors.toMap(e -> String.format("policy.%s", e.getKey()), e -> e.getValue().rego().getPolicy(), (e1, e2) -> e1));
.collect(Collectors.toMap(e -> String.format("policy.%s", e.getKey()), e -> e.getValue().rego().policy(), (e1, e2) -> e1));
toZip.put("policy.main", mainPolicy);
try {
return Response.ok(zipMap(toZip, "rego", objectMapper.writeValueAsString(getManifest(toZip)))).build();
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/fiware/odrl/PolicyResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public Response getPolicies(Integer page, Integer pageSize) {
.stream()
.map(policyEntry -> new Policy()
.id(policyEntry.getKey())
.odrl(policyEntry.getValue().odrl().getPolicy())
.rego(policyEntry.getValue().rego().getPolicy())).toList();
.odrl(policyEntry.getValue().odrl().policy())
.rego(policyEntry.getValue().rego().policy())).toList();

return Response.ok(policyList).build();
}
Expand All @@ -91,8 +91,8 @@ public Response getPolicyById(String id) {
.getPolicy(id)
.map(pw -> new Policy()
.id(id)
.odrl(pw.odrl().getPolicy())
.rego(pw.rego().getPolicy()))
.odrl(pw.odrl().policy())
.rego(pw.rego().policy()))
.map(Response::ok)
.map(Response.ResponseBuilder::build)
.orElse(Response.status(Response.Status.NOT_FOUND).build());
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/org/fiware/odrl/mapping/EntityMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.fiware.odrl.persistence.Policy;
import org.fiware.odrl.persistence.PolicyEntity;
import org.fiware.odrl.rego.OdrlPolicy;
import org.fiware.odrl.rego.PolicyWrapper;
Expand All @@ -19,18 +20,20 @@ public class EntityMapper {
private ObjectMapper objectMapper;

public PolicyEntity map(String id, PolicyWrapper policyWrapper) {
Policy rego = new Policy();
rego.setPolicy(policyWrapper.rego().policy());
Policy odrl = new Policy();
odrl.setPolicy(policyWrapper.odrl().policy());
PolicyEntity policy = new PolicyEntity();
policy.setPolicyId(id);
policy.setRego(policyWrapper.rego());
policy.setOdrl(policyWrapper.odrl());
policy.setRego(rego);
policy.setOdrl(odrl);
return policy;
}

public PolicyWrapper map(PolicyEntity policyEntity) {
try {
return new PolicyWrapper(new OdrlPolicy(objectMapper.writeValueAsString(policyEntity.getOdrl())), new RegoPolicy(objectMapper.writeValueAsString(policyEntity.getOdrl())));
} catch (JsonProcessingException e) {
throw new RuntimeException("Was not able to read the policy from the db.", e);
}
return new PolicyWrapper(
new OdrlPolicy(policyEntity.getOdrl().getPolicy()),
new RegoPolicy(policyEntity.getRego().getPolicy()));
}
}
13 changes: 13 additions & 0 deletions src/main/java/org/fiware/odrl/persistence/Policy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.fiware.odrl.persistence;

import io.quarkus.runtime.annotations.RegisterForReflection;
import lombok.Data;

/**
* @author <a href="https://github.com/wistefan">Stefan Wiedemann</a>
*/
@RegisterForReflection
@Data
public class Policy {
private String policy;
}
4 changes: 2 additions & 2 deletions src/main/java/org/fiware/odrl/persistence/PolicyEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public class PolicyEntity extends PanacheEntity {
private String policyId;

@JdbcTypeCode(SqlTypes.JSON)
private Object odrl;
private Policy odrl;

@JdbcTypeCode(SqlTypes.JSON)
private Object rego;
private Policy rego;

public static Optional<PolicyEntity> findByPolicyId(String policyId) {
return Optional.ofNullable(find("policyId", policyId).firstResult());
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/org/fiware/odrl/rego/OdrlPolicy.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package org.fiware.odrl.rego;

import io.quarkus.runtime.annotations.RegisterForReflection;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
* @author <a href="https://github.com/wistefan">Stefan Wiedemann</a>
*/
@RegisterForReflection
@Data
@AllArgsConstructor
public class OdrlPolicy {
private String policy;
public record OdrlPolicy(String policy) {
}
7 changes: 1 addition & 6 deletions src/main/java/org/fiware/odrl/rego/RegoPolicy.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package org.fiware.odrl.rego;

import io.quarkus.runtime.annotations.RegisterForReflection;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
* @author <a href="https://github.com/wistefan">Stefan Wiedemann</a>
*/
@RegisterForReflection
@Data
@AllArgsConstructor
public class RegoPolicy {
private String policy;
public record RegoPolicy(String policy) {
}

0 comments on commit 7beb081

Please sign in to comment.