View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.persistence.jpa.entity;
20  
21  import java.util.Collection;
22  import java.util.List;
23  import java.util.Optional;
24  import java.util.stream.Collectors;
25  import org.apache.syncope.core.persistence.api.entity.Any;
26  import org.apache.syncope.core.persistence.api.entity.GroupablePlainAttr;
27  import org.apache.syncope.core.persistence.api.entity.GroupableRelatable;
28  import org.apache.syncope.core.persistence.api.entity.Membership;
29  import org.apache.syncope.core.persistence.api.entity.Relationship;
30  import org.apache.syncope.core.persistence.api.entity.RelationshipType;
31  
32  public abstract class AbstractGroupableRelatable<
33          L extends Any<P>, 
34          M extends Membership<L>, 
35          P extends GroupablePlainAttr<L, M>,
36          R extends Any<?>,
37          REL extends Relationship<L, R>>
38          extends AbstractAny<P> implements GroupableRelatable<L, M, P, R, REL> {
39  
40      private static final long serialVersionUID = -2269285197388729673L;
41  
42      protected abstract List<? extends P> internalGetPlainAttrs();
43  
44      @Override
45      public boolean remove(final P attr) {
46          return internalGetPlainAttrs().remove(attr);
47      }
48  
49      @Override
50      public Optional<? extends P> getPlainAttr(final String plainSchema) {
51          return internalGetPlainAttrs().stream().
52                  filter(attr -> attr != null && attr.getSchema() != null && attr.getMembership() == null
53                  && plainSchema.equals(attr.getSchema().getKey())).
54                  findFirst();
55      }
56  
57      @Override
58      public Optional<? extends P> getPlainAttr(final String plainSchema, final Membership<?> membership) {
59          return internalGetPlainAttrs().stream().
60                  filter(attr -> attr != null && attr.getSchema() != null
61                  && attr.getMembership() != null && attr.getMembership().equals(membership)
62                  && plainSchema.equals(attr.getSchema().getKey())).
63                  findFirst();
64      }
65  
66      @Override
67      public List<? extends P> getPlainAttrs() {
68          return internalGetPlainAttrs().stream().
69                  filter(attr -> attr != null && attr.getSchema() != null && attr.getMembership() == null).
70                  collect(Collectors.toList());
71      }
72  
73      @Override
74      public Collection<? extends P> getPlainAttrs(final String plainSchema) {
75          return internalGetPlainAttrs().stream().
76                  filter(attr -> attr != null && attr.getSchema() != null
77                  && plainSchema.equals(attr.getSchema().getKey())).
78                  collect(Collectors.toList());
79      }
80  
81      @Override
82      public Collection<? extends P> getPlainAttrs(final Membership<?> membership) {
83          return internalGetPlainAttrs().stream().
84                  filter(attr -> attr != null && attr.getSchema() != null && membership.equals(attr.getMembership())).
85                  collect(Collectors.toList());
86      }
87  
88      @Override
89      public Optional<? extends M> getMembership(final String groupKey) {
90          return getMemberships().stream().
91                  filter(membership -> groupKey != null && groupKey.equals(membership.getRightEnd().getKey())).
92                  findFirst();
93      }
94  
95      @Override
96      public Collection<? extends REL> getRelationships(final RelationshipType relationshipType) {
97          return getRelationships().stream().
98                  filter(relationship -> relationshipType != null && relationshipType.equals(relationship.getType())).
99                  collect(Collectors.toList());
100     }
101 
102     @Override
103     public Collection<? extends REL> getRelationships(final String otherEndKey) {
104         return getRelationships().stream().
105                 filter(relationship -> otherEndKey != null && otherEndKey.equals(relationship.getRightEnd().getKey())).
106                 collect(Collectors.toList());
107     }
108 }