View Javadoc
1   package de.dlr.shepard.neo4Core.entities;
2   
3   import de.dlr.shepard.util.Constants;
4   import de.dlr.shepard.util.HasId;
5   import de.dlr.shepard.util.PermissionType;
6   import java.util.ArrayList;
7   import java.util.List;
8   import java.util.Objects;
9   import lombok.Data;
10  import lombok.NoArgsConstructor;
11  import lombok.ToString;
12  import org.neo4j.ogm.annotation.GeneratedValue;
13  import org.neo4j.ogm.annotation.Id;
14  import org.neo4j.ogm.annotation.NodeEntity;
15  import org.neo4j.ogm.annotation.Relationship;
16  import org.neo4j.ogm.annotation.Relationship.Direction;
17  
18  @NodeEntity
19  @Data
20  @NoArgsConstructor
21  public class Permissions implements HasId {
22  
23    @Id
24    @GeneratedValue
25    private Long id;
26  
27    @Relationship(type = Constants.HAS_PERMISSIONS, direction = Direction.INCOMING)
28    private List<BasicEntity> entities;
29  
30    @Relationship(type = Constants.OWNED_BY, direction = Direction.OUTGOING)
31    private User owner;
32  
33    private PermissionType permissionType;
34  
35    @ToString.Exclude
36    @Relationship(type = Constants.READABLE_BY, direction = Direction.OUTGOING)
37    private List<User> reader = new ArrayList<>();
38  
39    @ToString.Exclude
40    @Relationship(type = Constants.WRITEABLE_BY, direction = Direction.OUTGOING)
41    private List<User> writer = new ArrayList<>();
42  
43    @ToString.Exclude
44    @Relationship(type = Constants.READABLE_BY_GROUP, direction = Direction.OUTGOING)
45    private List<UserGroup> readerGroups = new ArrayList<>();
46  
47    @ToString.Exclude
48    @Relationship(type = Constants.WRITEABLE_BY_GROUP, direction = Direction.OUTGOING)
49    private List<UserGroup> writerGroups = new ArrayList<>();
50  
51    @ToString.Exclude
52    @Relationship(type = Constants.MANAGEABLE_BY, direction = Direction.OUTGOING)
53    private List<User> manager = new ArrayList<>();
54  
55    /**
56     * For testing purposes only
57     *
58     * @param id identifies the entity
59     */
60    public Permissions(long id) {
61      this.id = id;
62    }
63  
64    public Permissions(BasicEntity entity, User owner, PermissionType permissionType) {
65      this.entities = List.of(entity);
66      this.owner = owner;
67      this.permissionType = permissionType;
68    }
69  
70    public Permissions(
71      User owner,
72      List<User> reader,
73      List<User> writer,
74      List<UserGroup> readerGroups,
75      List<UserGroup> writerGroups,
76      List<User> manager,
77      PermissionType permissionType
78    ) {
79      this.owner = owner;
80      this.reader = reader;
81      this.writer = writer;
82      this.writerGroups = writerGroups;
83      this.readerGroups = readerGroups;
84      this.manager = manager;
85      this.permissionType = permissionType;
86    }
87  
88    @Override
89    public int hashCode() {
90      final int prime = 31;
91      int result = 1;
92      result = prime * result + Objects.hash(id, permissionType, readerGroups, writerGroups);
93      result = prime * result + HasId.hashcodeHelper(entities);
94      result = prime * result + HasId.hashcodeHelper(owner);
95      result = prime * result + HasId.hashcodeHelper(reader);
96      result = prime * result + HasId.hashcodeHelper(writer);
97      result = prime * result + HasId.hashcodeHelper(manager);
98      return result;
99    }
100 
101   @Override
102   public boolean equals(Object obj) {
103     if (this == obj) return true;
104     if (!(obj instanceof Permissions)) return false;
105     Permissions other = (Permissions) obj;
106     return (
107       Objects.equals(id, other.id) &&
108       Objects.equals(permissionType, other.permissionType) &&
109       Objects.equals(readerGroups, other.readerGroups) &&
110       Objects.equals(writerGroups, other.writerGroups) &&
111       HasId.equalsHelper(entities, other.entities) &&
112       HasId.equalsHelper(owner, other.owner) &&
113       HasId.equalsHelper(reader, other.reader) &&
114       HasId.equalsHelper(writer, other.writer) &&
115       HasId.equalsHelper(manager, other.manager)
116     );
117   }
118 
119   @Override
120   public String getUniqueId() {
121     return id.toString();
122   }
123 }