Skip to content

Commit

Permalink
🎨 (bommel-api): Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
byte-sized-emi authored and d135-1r43 committed Oct 31, 2024
1 parent 98ed0f3 commit fd7ede3
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 166 deletions.
104 changes: 52 additions & 52 deletions backend/app.hopps.org/src/main/java/app/hopps/org/jpa/Bommel.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,58 @@
@Entity
@Table(indexes = @Index(columnList = "parent_id"))
@NamedQueries({
@NamedQuery(
name = "Bommel.GetParentsRecursive",
query = """
with parents as (
select n.parent.id as bommel
from Bommel n
where n.id = :startId
union
select n.parent.id as bommel
from Bommel n
join parents c on n.id = c.bommel
) cycle bommel set cycleMark using cyclePath
select n as bommel, cycleMark as cycleMark, cyclePath as cyclePath
from Bommel n
join parents c on n.id = c.bommel
"""
),
@NamedQuery(
name = "Bommel.GetChildrenRecursive",
query = """
with children_query as (
select n.id as bommel
from Bommel n
where n.id = :startId
union
select n.id as bommel
from Bommel n
join children_query c on n.parent.id = c.bommel
) cycle bommel set cycleMark using cyclePath
select n as bommel, cycleMark as cycleMark, cyclePath as cyclePath
from Bommel n
join children_query c on n.id = c.bommel
where n.id != :startId or cycleMark = true
"""
)
@NamedQuery(name = "Bommel.GetParentsRecursive", query = """
with parents as (
select n.parent.id as bommel
from Bommel n
where n.id = :startId
union
select n.parent.id as bommel
from Bommel n
join parents c on n.id = c.bommel
) cycle bommel set cycleMark using cyclePath
select n as bommel, cycleMark as cycleMark, cyclePath as cyclePath
from Bommel n
join parents c on n.id = c.bommel
"""),
@NamedQuery(name = "Bommel.GetChildrenRecursive", query = """
with children_query as (
select n.id as bommel
from Bommel n
where n.id = :startId
union
select n.id as bommel
from Bommel n
join children_query c on n.parent.id = c.bommel
) cycle bommel set cycleMark using cyclePath
select n as bommel, cycleMark as cycleMark, cyclePath as cyclePath
from Bommel n
join children_query c on n.id = c.bommel
where n.id != :startId or cycleMark = true
""")
})
public class Bommel extends PanacheEntity {

private String name;
private String emoji;

@ManyToOne(cascade = {CascadeType.DETACH, CascadeType.REFRESH})
@ManyToOne(cascade = { CascadeType.DETACH, CascadeType.REFRESH })
private Member responsibleMember;

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.REFRESH, CascadeType.DETACH})
@ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.REFRESH, CascadeType.DETACH })
private Bommel parent;

@OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.DETACH, CascadeType.REMOVE}, mappedBy = "parent")
@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.DETACH,
CascadeType.REMOVE }, mappedBy = "parent")
private Set<Bommel> children;

/**
* Merges other into this object. Changes every field to that of other,
* except for the parent or childrens. Therefore,
* this cannot move the Bommel in the tree, only update it/its
* responsible member.
* Merges other into this object. Changes every field to that of other, except for the parent or childrens.
* Therefore, this cannot move the Bommel in the tree, only update it/its responsible member.
*/
public void merge(Bommel other) {
this.setName(other.getName());
Expand Down Expand Up @@ -105,15 +98,15 @@ public Bommel getParent() {
return parent;
}

// Write-only, because when reading the id-only setter right below this should be used.
// Write-only, because when reading the id-only setter right below this should be used.
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
public void setParent(Bommel parent) {
this.parent = parent;
}

/**
* This gets the parent bommel, with only the id set. Required for jackson,
* otherwise we would have unwanted recursion.
* This gets the parent bommel, with only the id set. Required for jackson, otherwise we would have unwanted
* recursion.
*/
@JsonProperty(value = "parent", access = JsonProperty.Access.READ_ONLY)
public Bommel getOnlyParentId() {
Expand All @@ -134,14 +127,21 @@ public Set<Bommel> getChildren() {

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (this == o) {
return true;
}

if (o == null || getClass() != o.getClass()) {
return false;
}
Bommel bommel = (Bommel) o;

return Objects.equals(id, bommel.id)
&& Objects.equals(getEmoji(), bommel.getEmoji())
&& Objects.equals(getName(), bommel.getName())
&& Objects.equals(getResponsibleMember(), bommel.getResponsibleMember())
&& Objects.equals(getParent() != null ? getParent().id : null, bommel.getParent() != null ? bommel.getParent().id : null)
&& Objects.equals(getParent() != null ? getParent().id : null,
bommel.getParent() != null ? bommel.getParent().id : null)
&& Objects.equals(getChildren(), bommel.getChildren());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@
public class BommelRepository implements PanacheRepository<Bommel> {

/**
* Fetches the lineage of this bommel,
* so the path all the way to the root bommel.
* Does not include the base element itself.
* Goes upwards towards the root element, i.e.
* the root element will always be the last.
* Fetches the lineage of this bommel, so the path all the way to the root bommel. Does not include the base element
* itself. Goes upwards towards the root element, i.e. the root element will always be the last.
*/
public List<TreeSearchBommel> getParents(Bommel base) throws WebApplicationException {
List<TreeSearchBommel> possibleCycleBommels = this.getEntityManager()
Expand All @@ -26,7 +23,8 @@ public List<TreeSearchBommel> getParents(Bommel base) throws WebApplicationExcep
.getResultList();

Optional<TreeSearchBommel> cycle = possibleCycleBommels.stream()
.filter(TreeSearchBommel::cycleMark).findAny();
.filter(TreeSearchBommel::cycleMark)
.findAny();

if (cycle.isPresent()) {
throw new WebApplicationException("Cycle detected on bommel " + cycle.get());
Expand All @@ -36,10 +34,11 @@ public List<TreeSearchBommel> getParents(Bommel base) throws WebApplicationExcep
}

/**
* Gets all children, recursively. The cyclePath of the returned
* record will be the id's from the base Bommel to the found child,
* including the base id and its own id.
* @throws IllegalStateException when there has been a cycle
* Gets all children, recursively. The cyclePath of the returned record will be the id's from the base Bommel to the
* found child, including the base id and its own id.
*
* @throws IllegalStateException
* when there has been a cycle
*/
public List<TreeSearchBommel> getChildrenRecursive(Bommel base) throws IllegalStateException {
List<TreeSearchBommel> possibleCycleBommels = this.getEntityManager()
Expand All @@ -48,7 +47,8 @@ public List<TreeSearchBommel> getChildrenRecursive(Bommel base) throws IllegalSt
.getResultList();

Optional<TreeSearchBommel> cycle = possibleCycleBommels.stream()
.filter(TreeSearchBommel::cycleMark).findAny();
.filter(TreeSearchBommel::cycleMark)
.findAny();

if (cycle.isPresent()) {
throw new WebApplicationException("Cycle detected on bommel " + cycle.get());
Expand All @@ -65,14 +65,15 @@ public Bommel getRoot() {
}

@Transactional
public Bommel createRoot(Bommel root) throws WebApplicationException {
public Bommel createRoot(Bommel root) throws WebApplicationException {
if (root.getParent() != null) {
throw new WebApplicationException("Root bommel cannot have a parent", Response.Status.BAD_REQUEST);
}

long rootNodeCount = count("where parent is null");
if (rootNodeCount != 0) {
throw new WebApplicationException("Expected 0 root nodes, found " + rootNodeCount, Response.Status.CONFLICT);
throw new WebApplicationException("Expected 0 root nodes, found " + rootNodeCount,
Response.Status.CONFLICT);
}

persist(root);
Expand All @@ -81,8 +82,8 @@ public Bommel createRoot(Bommel root) throws WebApplicationException {
}

/**
* Inserts this bommel, linking it to the stored parent.
* This cannot create a root node, use {@link BommelRepository#createRoot} for that.
* Inserts this bommel, linking it to the stored parent. This cannot create a root node, use
* {@link BommelRepository#createRoot} for that.
*/
@Transactional
public Bommel insertBommel(Bommel child) throws WebApplicationException {
Expand All @@ -104,8 +105,7 @@ public void deleteBommel(Bommel bommel, boolean recursive) {
if (!recursive && !bommel.getChildren().isEmpty()) {
throw new WebApplicationException(
"Bommel has children, specify recursive=true to delete it and its children",
Response.Status.BAD_REQUEST
);
Response.Status.BAD_REQUEST);
}

delete(bommel);
Expand All @@ -121,10 +121,8 @@ public Bommel moveBommel(Bommel bommel, Bommel destination) {
}

/**
* Counts the number of edges (k) and vertices (n) in the tree,
* and uses the property n = k - 1 to ensure that no subtrees
* have separated and no cycles exist.
* This is expensive, especially with a large number of bommels.
* Counts the number of edges (k) and vertices (n) in the tree, and uses the property n = k - 1 to ensure that no
* subtrees have separated and no cycles exist. This is expensive, especially with a large number of bommels.
*/
@Transactional
public void ensureConsistency() throws WebApplicationException {
Expand All @@ -136,8 +134,7 @@ public void ensureConsistency() throws WebApplicationException {
}

/**
* Ensures that, starting from child, there is no
* cycle in the graph. Uses getParents internally.
* Ensures that, starting from child, there is no cycle in the graph. Uses getParents internally.
*/
private void ensureNoCycleFromBommel(Bommel child) throws WebApplicationException {
getParents(child);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
public record TreeSearchBommel(
Bommel bommel,
boolean cycleMark,
List<Long> cyclePath
) {
List<Long> cyclePath) {
public TreeSearchBommel(Bommel bommel, Boolean cycleMark, String cyclePath) {
this(bommel, cycleMark != null && cycleMark, parseCyclePath(cyclePath));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public class BommelResource {
boolean authEnabled;

public BommelResource(
@ConfigProperty(name = "quarkus.security.auth.enabled-in-dev-mode", defaultValue = "true")
boolean devModeAuthEnabled
) {
@ConfigProperty(name = "quarkus.security.auth.enabled-in-dev-mode", defaultValue = "true") boolean devModeAuthEnabled) {
this.authEnabled = devModeAuthEnabled || !ConfigUtils.isProfileActive("dev");
}

Expand Down Expand Up @@ -99,7 +97,7 @@ public Bommel createRoot(Bommel root) {
}

// TODO: We're not checking OpenFGA here - not sure what to check for, since there's no
// bommel yet in the tree
// bommel yet in the tree

return bommelRepo.createRoot(root);
}
Expand All @@ -111,8 +109,7 @@ public Bommel createBommel(Bommel bommel) {
if (bommel.getParent() == null) {
throw new WebApplicationException(
"Bommel has no parent, cannot create root bommel",
Response.Status.BAD_REQUEST
);
Response.Status.BAD_REQUEST);
}

checkUserHasPermission(bommel.getParent().id, "write");
Expand All @@ -131,8 +128,7 @@ public Bommel updateBommel(Bommel bommel, @PathParam("id") long id) {
if (existingBommel == null) {
throw new WebApplicationException(
"Could not find bommel with this id",
Response.Status.BAD_REQUEST
);
Response.Status.BAD_REQUEST);
}

existingBommel.merge(bommel);
Expand All @@ -141,8 +137,7 @@ public Bommel updateBommel(Bommel bommel, @PathParam("id") long id) {
}

/**
* Moves the bommel specified by id (first parameter)
* to the new parent (specified by newParentId)
* Moves the bommel specified by id (first parameter) to the new parent (specified by newParentId)
*/
@PUT
@Path("/move/{id}/to/{newParentId}")
Expand All @@ -157,15 +152,13 @@ public Bommel moveBommel(@PathParam("id") long id, @PathParam("newParentId") lon
if (base == null) {
throw new WebApplicationException(
"Base bommel does not exist",
Response.Status.BAD_REQUEST
);
Response.Status.BAD_REQUEST);
}

if (parent == null) {
throw new WebApplicationException(
"Parent bommel does not exist",
Response.Status.BAD_REQUEST
);
Response.Status.BAD_REQUEST);
}

return bommelRepo.moveBommel(base, parent);
Expand All @@ -174,24 +167,24 @@ public Bommel moveBommel(@PathParam("id") long id, @PathParam("newParentId") lon
@DELETE
@Path("/{id}")
@Transactional
public void deleteBommel(@PathParam("id") long id, @QueryParam("recursive") @DefaultValue("false") boolean recursive) {
public void deleteBommel(@PathParam("id") long id,
@QueryParam("recursive") @DefaultValue("false") boolean recursive) {
checkUserHasPermission(id, "write");

Bommel base = bommelRepo.findById(id);

if (base == null) {
throw new WebApplicationException(
"Could not find bommel",
Response.Status.BAD_REQUEST
);
Response.Status.BAD_REQUEST);
}

bommelRepo.deleteBommel(base, recursive);
}

/**
* Checks that the currently signed-in user can access this bommel with this relation.
* Throws a WebApplication exception if anything goes wrong.
* Checks that the currently signed-in user can access this bommel with this relation. Throws a WebApplication
* exception if anything goes wrong.
*/
private void checkUserHasPermission(long bommelId, String relation) throws WebApplicationException {
var principal = securityContext.getUserPrincipal();
Expand Down
Loading

0 comments on commit fd7ede3

Please sign in to comment.