org.picketlink.idm.internal.RelationshipReference Maven / Gradle / Ivy
package org.picketlink.idm.internal;
import org.picketlink.idm.IdentityManagementException;
import org.picketlink.idm.model.AbstractAttributedType;
import org.picketlink.idm.model.IdentityType;
import org.picketlink.idm.model.Relationship;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* Adapter class that encapsulates a target {@link Relationship} instance and provides additional methods
* to resolve the referenced {@link IdentityType} instances.
*
* This class is particularly used when using multiple stores to store different identity and relationship types.
* In this scenario, the referenced identity type may not be stored in the same store of the relationship, which
* requires
* to hold only an identifier-based reference to the referenced type.
*
* @author Pedro Igor
*/
public final class RelationshipReference extends AbstractAttributedType implements Relationship {
private static final String ID_SEPARATOR = ":";
private final Relationship relationship;
private final Map identityTypeReference;
public RelationshipReference(Relationship relationship) {
this.relationship = relationship;
this.identityTypeReference = new HashMap();
}
@Override
public String getId() {
return getRelationship().getId();
}
/**
* Add a reference to a {@link IdentityType}.
*
* @param descriptor The descriptor for the identity type. The descriptor usually matches the property name on the
* target
* relationship instance used to store the identity type instance.
* @param referencedId The identifier of the identity type.
*/
public void addIdentityTypeReference(String descriptor, String referencedId) {
this.identityTypeReference.put(descriptor, referencedId);
}
/**
* Returns a {@link Set} of strings representing all registered identity type references.
*
* Descriptors have a format: typeName:partitionId:identityTypeId
.
*
* @return
*/
public Set getDescriptors() {
return this.identityTypeReference.keySet();
}
/**
* Return the type given a descriptor.
*
* @param descriptor
*
* @return
*/
public String getIdentityType(String descriptor) {
String[] referencedIds = getReferencedIds(descriptor);
if (referencedIds != null) {
return referencedIds[0];
}
throw new IdentityManagementException("No type defined for descriptor [" + descriptor + "].");
}
/**
* Return the identifier of the partition where the identity type is stored.
*
* @param descriptor
*
* @return
*/
public String getPartitionId(String descriptor) {
String[] referencedIds = getReferencedIds(descriptor);
if (referencedIds != null) {
return referencedIds[1];
}
throw new IdentityManagementException("No Partition id for descriptor [" + descriptor + "].");
}
/**
* Return the identifier of the identity type referenced by the descriptor.
*
* @param descriptor
*
* @return
*/
public String getIdentityTypeId(String descriptor) {
String[] referencedIds = getReferencedIds(descriptor);
if (referencedIds != null) {
return referencedIds[2];
}
throw new IdentityManagementException("No IdentityType id for descriptor [" + descriptor + "].");
}
/**
* Return the target relationship instance.
*
* @return
*/
public Relationship getRelationship() {
return relationship;
}
private String[] getReferencedIds(String descriptor) {
String referencedId = this.identityTypeReference.get(descriptor);
if (referencedId != null) {
String[] ids = referencedId.split(ID_SEPARATOR);
if (ids.length < 2) {
throw new IdentityManagementException("Wrong format for referenced identitytype id.");
}
return ids;
}
return null;
}
/**
* Return a formatted string representing the reference to the given {@link IdentityType}.
*
* @param identityType
*
* @return
*/
public static String formatId(final IdentityType identityType) {
return identityType.getClass().getName() + ID_SEPARATOR + identityType.getPartition().getId() + ID_SEPARATOR + identityType.getId();
}
}