View Javadoc
1   package de.dlr.shepard.neo4Core.dao;
2   
3   import de.dlr.shepard.neo4Core.entities.Permissions;
4   import de.dlr.shepard.util.Constants;
5   import de.dlr.shepard.util.CypherQueryHelper;
6   import jakarta.enterprise.context.RequestScoped;
7   import java.util.Collections;
8   
9   @RequestScoped
10  public class PermissionsDAO extends GenericDAO<Permissions> {
11  
12    public Permissions findByEntityNeo4jId(long entityId) {
13      String query =
14        String.format("MATCH (e)-[:has_permissions]->(p:Permissions) WHERE ID(e) = %d ", entityId) +
15        CypherQueryHelper.getReturnPart("p");
16      var permissions = findByQuery(query, Collections.emptyMap());
17      if (permissions.iterator().hasNext()) return permissions.iterator().next();
18      return null;
19    }
20  
21    public Permissions findByEntityShepardId(long entityShepardId) {
22      String query =
23        String.format(
24          "MATCH (e)-[:has_permissions]->(p:Permissions) WHERE e." + Constants.SHEPARD_ID + " = %d ",
25          entityShepardId
26        ) +
27        CypherQueryHelper.getReturnPart("p");
28      var permissions = findByQuery(query, Collections.emptyMap());
29      Permissions ret = null;
30      if (permissions.iterator().hasNext()) ret = permissions.iterator().next();
31      return ret;
32    }
33  
34    public Permissions findByCollectionShepardId(long collectionShepardId) {
35      String query =
36        String.format(
37          "MATCH (c:Collection)-[:has_permissions]->(p:Permissions) WHERE c." + Constants.SHEPARD_ID + " = %d ",
38          collectionShepardId
39        ) +
40        CypherQueryHelper.getReturnPart("p");
41      var permissions = findByQuery(query, Collections.emptyMap());
42      Permissions ret = null;
43      if (permissions.iterator().hasNext()) ret = permissions.iterator().next();
44      return ret;
45    }
46  
47    public Permissions createWithEntityNeo4jId(Permissions permissions, long entityId) {
48      var created = createOrUpdate(permissions);
49      String query = String.format(
50        "MATCH (e) WHERE ID(e) = %d MATCH (p:Permissions) WHERE ID(p) = %d CREATE path = (e)-[r:has_permissions]->(p)",
51        entityId,
52        created.getId()
53      );
54      runQuery(query, Collections.emptyMap());
55      return findByNeo4jId(created.getId());
56    }
57  
58    public Permissions createWithEntityShepardId(Permissions permissions, long entityShepardId) {
59      var created = createOrUpdate(permissions);
60      String query = String.format(
61        "MATCH (e) WHERE e." +
62        Constants.SHEPARD_ID +
63        " = %d MATCH (p:Permissions) WHERE ID(p) = %d CREATE path = (e)-[r:has_permissions]->(p)",
64        entityShepardId,
65        created.getId()
66      );
67      runQuery(query, Collections.emptyMap());
68      return findByNeo4jId(created.getId());
69    }
70  
71    @Override
72    public Class<Permissions> getEntityType() {
73      return Permissions.class;
74    }
75  }