/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.syncope.core.persistence.jpa.entity;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import org.apache.syncope.core.persistence.api.entity.Any;
import org.apache.syncope.core.persistence.api.entity.Groupable;
import org.apache.syncope.core.persistence.api.entity.Membership;
import org.apache.syncope.core.persistence.api.entity.PlainAttr;
import org.apache.syncope.core.persistence.api.entity.Relationship;

public abstract class AbstractGroupableRelatable<
        L extends Any, 
        M extends Membership<L>, 
        R extends Any,
        REL extends Relationship<L, R>>
        extends AbstractRelatable<L, R, REL> implements Groupable<L, M, R, REL> {

    private static final long serialVersionUID = -2269285197388729673L;

    @Override
    public List<PlainAttr> getPlainAttrs() {
        return getPlainAttrsList().stream().
                filter(attr -> attr.getMembership() == null).
                toList();
    }

    @Override
    public Optional<PlainAttr> getPlainAttr(final String plainSchema) {
        return getPlainAttrsList().stream().
                filter(attr -> attr.getMembership() == null
                && plainSchema.equals(attr.getSchema())).
                findFirst();
    }

    @Override
    public Optional<PlainAttr> getPlainAttr(final String plainSchema, final Membership<?> membership) {
        return getPlainAttrsList().stream().
                filter(attr -> plainSchema.equals(attr.getSchema())
                && membership.getKey().equals(attr.getMembership())).
                findFirst();
    }

    @Override
    public Collection<PlainAttr> getPlainAttrs(final String plainSchema) {
        return getPlainAttrsList().stream().
                filter(attr -> plainSchema.equals(attr.getSchema())).
                toList();
    }

    @Override
    public Collection<PlainAttr> getPlainAttrs(final Membership<?> membership) {
        return getPlainAttrsList().stream().
                filter(attr -> membership.getKey().equals(attr.getMembership())).
                toList();
    }

    @Override
    public Optional<? extends M> getMembership(final String groupKey) {
        return getMemberships().stream().
                filter(membership -> groupKey.equals(membership.getRightEnd().getKey())).
                findFirst();
    }
}
