@Override public void add(IdentityContext identityContext, Partition partition, String configurationName) { add(identityContext, partition); // now that the partition entity is created, let`s populate the configuration name. // the configuration name is not part of the Model API, so we need to do this manually. EntityMapper entityMapper = getRootMapper(partition.getClass()); EntityManager entityManager = getEntityManager(identityContext); Object partitionEntity = getRootEntity(partition, entityManager); Property configurationNameProperty = entityMapper.getProperty(partition.getClass(), ConfigurationName.class).getValue(); configurationNameProperty.setValue(partitionEntity, configurationName); entityManager.merge(partitionEntity); }
private void validateTypeMapping(Set<Class<? extends AttributedType>> supportedTypes) { for (Class<? extends AttributedType> supportedType : supportedTypes) { // we only validate user-defined types if (Relationship.class.equals(supportedType) || Partition.class.equals(supportedType) || IdentityType.class.equals(supportedType) || Account.class.equals(supportedType)) { continue; } checkIfAnnotationIsDefined(supportedType, Identifier.class); if (Partition.class.isAssignableFrom(supportedType)) { checkIfAnnotationIsDefined(supportedType, PartitionClass.class); } if (IdentityType.class.isAssignableFrom(supportedType)) { checkIfAnnotationIsDefined(supportedType, IdentityClass.class); if (getConfig().supportsPartition()) { checkIfAnnotationIsDefined(supportedType, OwnerReference.class); } } if (Relationship.class.isAssignableFrom(supportedType)) { checkIfAnnotationIsDefined(supportedType, RelationshipClass.class); checkIfAnnotationIsDefined(supportedType, RelationshipDescriptor.class); checkIfAnnotationIsDefined(supportedType, RelationshipMember.class); checkIfAnnotationIsDefined(supportedType, OwnerReference.class); } } }
private void validateAttributeMapping() { if (getConfig().supportsAttribute()) { for (EntityMapper entityMapper : getEntityMappers()) { if (entityMapper.getProperty(AttributeClass.class) != null) { checkIfAnnotationIsDefinedForAttribute(entityMapper, AttributeName.class); checkIfAnnotationIsDefinedForAttribute(entityMapper, AttributeValue.class); checkIfAnnotationIsDefinedForAttribute(entityMapper, OwnerReference.class); return; } } throw MESSAGES.configJpaStoreMappedNoAttributeMappingFound(); } }
@Override public <T extends CredentialStorage> T retrieveCurrentCredential(Agent agent, Class<T> storageClass) { Object lastCredential = retrieveCurrentCredentialEntity(agent, storageClass); return (T) convertToCredentialStorage(lastCredential, storageClass); }
@Override protected void removeFromRelationships(IdentityContext context, IdentityType identityType) { // First we build a list of all the relationships that the specified identity // is participating in List<?> relationshipsToRemove = findIdentityTypeRelationships(context, Relationship.class, identityType); // Now that we have the list, we can iterate through and remove the records for (Object relationship : relationshipsToRemove) { remove(context, convertToRelationshipType(context, relationship)); } }
@Override public <T extends CredentialStorage> List<T> retrieveCredentials(IdentityContext context, Account account, Class<T> storageClass) { List<T> storages = new ArrayList<T>(); for (Object object : findCredentials(context, account, storageClass)) { storages.add(convertToCredentialStorage(object, storageClass)); } return storages; }
@Override public <V extends Relationship> List<V> fetchQueryResults(IdentityContext context, RelationshipQuery<V> query) { EntityManager entityManager = getEntityManager(context); List entities = new ArrayList(); for (Object parameterValue : identityParameterValues) { if (IdentityType.class.isInstance(parameterValue)) { entities = findIdentityTypeRelationships(context, query.getRelationshipClass(), (IdentityType) parameterValue); } else { throw MESSAGES.queryUnsupportedParameterValue("Relationship.IDENTITY", parameterValue); EntityMapper entityMapper = getRootMapper(relationshipType); CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<?> cq = cb.createQuery(entityMapper.getEntityType()); RelationshipQueryParameter identityTypeParameter = (RelationshipQueryParameter) entry.getKey(); List<String> identityTypeIdentifiers = new ArrayList<String>(); EntityMapper relationshipMemberMapper = getEntityMapperForProperty(relationshipType, RelationshipMember.class); } else { Join join = fromRelationshipIdentityType.join(identityProperty.getName()); EntityMapper identityTypeMapper = getMapperForEntity(identityProperty.getJavaClass()); Property identifierProperty = identityTypeMapper.getProperty(Identifier.class).getValue(); Object[] parameterValues = entry.getValue(); EntityMapper parameterEntityMapper = getEntityMapperForProperty(relationshipType, attributeParameter.getName()); if (isMappedType(mappedProperty.getJavaClass())) {
V identityType = (V) lookupIdentityTypeById(context, type, value.toString()); EntityMapper rootMapper = getRootMapper(type); EntityManager entityManager = getEntityManager(context); CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(rootMapper.getEntityType()); if (partitionProperty != null && getConfig().supportsPartition()) { Join<Object, Object> join = rootEntity.join(partitionProperty.getValue().getName()); predicates.add(cb.equal(join, entityManager.find(partitionProperty.getValue().getJavaClass(), partition.getId()))); AttributeParameter attributeParameter = (AttributeParameter) queryParameter; EntityMapper parameterEntityMapper = getEntityMapperForProperty(type, attributeParameter.getName()); addCondition(entityManager, cb, predicates, condition, attributeProperty, attributeOwnerEntity, false); } else if (getConfig().supportsAttribute()) { addAttributeQueryPredicates(type, entityManager, cb, cq, rootEntity, predicates, attributeParameter, condition, null);
private <T extends Relationship> T convertToRelationshipType(IdentityContext context, Object relationshipObject) { EntityMapper relationshipTypeMapper = getMapperForEntity(relationshipObject.getClass()); Property typeProperty = relationshipTypeMapper.getTypeProperty(); Object relationshipTypeName = typeProperty.getValue(relationshipObject); EntityMapper relationshipMemberMapper = getEntityMapperForProperty(relationshipType, RelationshipMember.class); Property<Object> identityProperty = relationshipMemberMapper.getProperty(relationshipType, RelationshipMember.class).getValue(); Property<String> descriptorProperty = relationshipMemberMapper.getProperty(relationshipType, RelationshipDescriptor.class).getValue(); EntityManager entityManager = getEntityManager(context); EntityMapper relMapper = getRootMapper(relationshipType); for (Object object : findChildRelationships(context, relationshipInstance)) { String descriptor = descriptorProperty.getValue(object).toString(); reference.addIdentityTypeReference(descriptor, identityTypeEntity.toString()); } else { EntityMapper entityMapper = getRootMapperForEntity(identityTypeEntity.getClass());
@Override public void setup(JPAIdentityStoreConfiguration config) { super.setup(config); if (config.getContextInitializers().isEmpty()) { JPA_STORE_LOGGER.jpaContextInitializerNotProvided(); } for (Class<?> entityType : config.getEntityTypes()) { configureEntityMapper(entityType); } logEntityMappers(); validateConfiguration(); }
private void validateCredentialMapping() { if (getConfig().supportsCredential()) { for (EntityMapper entityMapper : getEntityMappers()) { ManagedCredential managedCredential = entityMapper.getEntityType().getAnnotation(ManagedCredential.class); if (managedCredential != null) { checkIfAnnotationIsDefinedForCredential(entityMapper, CredentialClass.class); checkIfAnnotationIsDefinedForCredential(entityMapper, EffectiveDate.class); checkIfAnnotationIsDefinedForCredential(entityMapper, ExpiryDate.class); return; } } throw MESSAGES.configJpaStoreMappedNoCredentialStorageMappingFound(); } }
EntityMapper attributeMapper = getAttributeMapper(attributedType); Subquery<?> subQueryOwnerAttributesByValue = cq.subquery(attributeMapper.getEntityType()); Root fromAttributeType = subQueryOwnerAttributesByValue.from(attributeMapper.getEntityType()); Property ownerProperty = attributeMapper.getProperty(Attribute.class, OwnerReference.class).getValue(); String ownerIdentifierPropertyName = getRootMapper(attributedType).getProperty(Identifier.class).getValue().getName(); Path selection; addCondition(entityManager, cb, conjunction, condition, attributeValueProperty, fromAttributeType, true);
@Override public <T extends CredentialStorage> List<T> retrieveCredentials(Agent agent, Class<T> storageClass) { Property<Object> identityTypeProperty = getConfig().getModelProperty(PROPERTY_CREDENTIAL_IDENTITY); Property<Object> typeProperty = getConfig().getModelProperty(PROPERTY_CREDENTIAL_TYPE); EntityManager em = getEntityManager(); CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<?> criteria = builder.createQuery(getConfig().getCredentialClass()); Root<?> root = criteria.from(getConfig().getCredentialClass()); List<Predicate> predicates = new ArrayList<Predicate>(); Object agentInstance = lookupIdentityObjectById(agent); predicates.add(builder.equal(root.get(identityTypeProperty.getName()), agentInstance)); predicates.add(builder.equal(root.get(typeProperty.getName()), storageClass.getName())); criteria.where(predicates.toArray(new Predicate[predicates.size()])); List<?> result = em.createQuery(criteria).getResultList(); List<T> storages = new ArrayList<T>(); for (Object object : result) { storages.add((T) convertToCredentialStorage(object, storageClass)); } return storages; }
if (getConfig().getEntityTypes().contains(ownerClass)) { configureEntityMapper(ownerClass); configureEntityMapper(entityType.getSuperclass());
@Override protected void doPopulateIdentityInstance(Object toIdentity, Group fromGroup, JPAIdentityStore store) { store.setModelProperty(toIdentity, PROPERTY_IDENTITY_NAME, fromGroup.getName(), true); if (fromGroup.getParentGroup() != null) { Object parentIdentity = store.lookupIdentityObjectById(fromGroup.getParentGroup()); if (parentIdentity == null) { store.add(fromGroup.getParentGroup()); parentIdentity = store.lookupIdentityObjectById(fromGroup.getParentGroup()); } store.setModelProperty(toIdentity, PROPERTY_PARENT_GROUP, parentIdentity, true); } }
private void removeChildRelationships(final IdentityContext context, final Relationship attributedType, final EntityManager entityManager) { for (Object child : findChildRelationships(context, (Relationship) attributedType)) { entityManager.remove(child); } }
@Override public <V extends Relationship> List<V> fetchQueryResults(IdentityContext context, RelationshipQuery<V> query) { EntityManager entityManager = getEntityManager(context); List entities = new ArrayList(); for (Object parameterValue : identityParameterValues) { if (IdentityType.class.isInstance(parameterValue)) { entities = findIdentityTypeRelationships(context, query.getRelationshipClass(), (IdentityType) parameterValue); } else { throw MESSAGES.queryUnsupportedParameterValue("Relationship.IDENTITY", parameterValue); EntityMapper entityMapper = getRootMapper(relationshipType); CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<?> cq = cb.createQuery(entityMapper.getEntityType()); RelationshipQueryParameter identityTypeParameter = (RelationshipQueryParameter) entry.getKey(); List<String> identityTypeIdentifiers = new ArrayList<String>(); EntityMapper relationshipMemberMapper = getEntityMapperForProperty(relationshipType, RelationshipMember.class); } else { Join join = fromRelationshipIdentityType.join(identityProperty.getName()); EntityMapper identityTypeMapper = getMapperForEntity(identityProperty.getJavaClass()); Property identifierProperty = identityTypeMapper.getProperty(Identifier.class).getValue(); Object[] parameterValues = entry.getValue(); EntityMapper parameterEntityMapper = getEntityMapperForProperty(relationshipType, attributeParameter.getName()); if (isMappedType(mappedProperty.getJavaClass())) {
V identityType = (V) lookupIdentityTypeById(context, type, value.toString()); EntityMapper rootMapper = getRootMapper(type); EntityManager entityManager = getEntityManager(context); CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(rootMapper.getEntityType()); if (partitionProperty != null && getConfig().supportsPartition()) { Join<Object, Object> join = rootEntity.join(partitionProperty.getValue().getName()); predicates.add(cb.equal(join, entityManager.find(partitionProperty.getValue().getJavaClass(), partition.getId()))); AttributeParameter attributeParameter = (AttributeParameter) queryParameter; EntityMapper parameterEntityMapper = getEntityMapperForProperty(type, attributeParameter.getName()); addCondition(entityManager, cb, predicates, condition, attributeProperty, attributeOwnerEntity, false); } else if (getConfig().supportsAttribute()) { addAttributeQueryPredicates(type, entityManager, cb, cq, rootEntity, predicates, attributeParameter, condition, null);
private <T extends Relationship> T convertToRelationshipType(IdentityContext context, Object relationshipObject) { EntityMapper relationshipTypeMapper = getMapperForEntity(relationshipObject.getClass()); Property typeProperty = relationshipTypeMapper.getTypeProperty(); Object relationshipTypeName = typeProperty.getValue(relationshipObject); EntityMapper relationshipMemberMapper = getEntityMapperForProperty(relationshipType, RelationshipMember.class); Property<Object> identityProperty = relationshipMemberMapper.getProperty(relationshipType, RelationshipMember.class).getValue(); Property<String> descriptorProperty = relationshipMemberMapper.getProperty(relationshipType, RelationshipDescriptor.class).getValue(); EntityManager entityManager = getEntityManager(context); EntityMapper relMapper = getRootMapper(relationshipType); for (Object object : findChildRelationships(context, relationshipInstance)) { String descriptor = descriptorProperty.getValue(object).toString(); reference.addIdentityTypeReference(descriptor, identityTypeEntity.toString()); } else { EntityMapper entityMapper = getRootMapperForEntity(identityTypeEntity.getClass());
@Override protected void removeFromRelationships(IdentityContext context, IdentityType identityType) { // First we build a list of all the relationships that the specified identity // is participating in List<?> relationshipsToRemove = findIdentityTypeRelationships(context, Relationship.class, identityType); // Now that we have the list, we can iterate through and remove the records for (Object relationship : relationshipsToRemove) { remove(context, convertToRelationshipType(context, relationship)); } }