All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.picketlink.idm.file.internal.FileRelationship Maven / Gradle / Ivy

/*
 * JBoss, Home of Professional Open Source
 *
 * Copyright 2013 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed 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.picketlink.idm.file.internal;

import org.picketlink.common.properties.Property;
import org.picketlink.common.properties.query.AnnotatedPropertyCriteria;
import org.picketlink.common.properties.query.PropertyQueries;
import org.picketlink.common.properties.query.TypedPropertyCriteria;
import org.picketlink.idm.internal.RelationshipReference;
import org.picketlink.idm.model.IdentityType;
import org.picketlink.idm.model.Relationship;
import org.picketlink.idm.model.annotation.AttributeProperty;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.util.Map.Entry;
import static org.picketlink.common.properties.query.TypedPropertyCriteria.MatchOption;

/**
 * @author pedroigor
 */
public class FileRelationship extends AbstractFileAttributedType {

    private static final long serialVersionUID = -507972683694827934L;

    private static final transient String FILE_RELATIONSHIP_VERSION = "1";

    private Map identityTypeIds = new HashMap();

    protected FileRelationship(Relationship object) {
        super(FILE_RELATIONSHIP_VERSION, object);
    }

    @Override
    protected void doPopulateProperties(Map properties) throws Exception {
        super.doPopulateProperties(properties);

        List> relationshipIdentityTypes = PropertyQueries
                . createQuery(getEntry().getClass())
                .addCriteria(new TypedPropertyCriteria(IdentityType.class, MatchOption.SUB_TYPE)).getResultList();

        for (Property annotatedProperty : relationshipIdentityTypes) {
            IdentityType identityType = annotatedProperty.getValue(getEntry());

            if (identityType != null) {
                this.identityTypeIds.put(RelationshipReference.formatId(identityType), annotatedProperty.getName());
            }
        }
    }

    @Override
    protected void doWriteObject(ObjectOutputStream s) throws Exception {
        s.writeObject(this.identityTypeIds);
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void doReadObject(ObjectInputStream s) throws Exception {
        this.identityTypeIds = (Map) s.readObject();
    }

    @Override
    protected Relationship doPopulateEntry(Map properties) throws Exception {
        Relationship relationship = super.doPopulateEntry(properties);

        List> relationshipAttributeTypes = PropertyQueries
                . createQuery(relationship.getClass())
                .addCriteria(new AnnotatedPropertyCriteria(AttributeProperty.class)).getResultList();

        for (Property property : relationshipAttributeTypes) {
            property.setValue(relationship, properties.get(property.getName()));
        }

        return relationship;
    }

    public String getIdentityTypeId(String roleName) {
        Set> entrySet = this.identityTypeIds.entrySet();

        for (Entry entry : entrySet) {
            if (roleName.equals(entry.getValue())) {
                return entry.getKey();
            }
        }

        return null;
    }

    public boolean hasIdentityType(IdentityType identityType) {
        return this.identityTypeIds.containsKey(RelationshipReference.formatId(identityType));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy