@Override public void removeAttributedType(IdentityContext context, AttributedType attributedType) { EntityManager entityManager = getEntityManager(context); EntityMapper rootMapper = getRootMapper(attributedType.getClass()); if (Relationship.class.isAssignableFrom(attributedType.getClass())) { removeChildRelationships(context, (Relationship) attributedType, entityManager); } removeAssociatedEntities(attributedType, entityManager, rootMapper); entityManager.remove(getRootEntity(attributedType, entityManager)); }
@Override public void removeAttributedType(IdentityContext context, AttributedType attributedType) { EntityManager entityManager = getEntityManager(context); EntityMapper rootMapper = getRootMapper(attributedType.getClass()); if (Relationship.class.isAssignableFrom(attributedType.getClass())) { removeChildRelationships(context, (Relationship) attributedType, entityManager); } removeAssociatedEntities(attributedType, entityManager, rootMapper); entityManager.remove(getRootEntity(attributedType, entityManager)); }
@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); }
@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 <T extends CredentialStorage> List<?> findCredentials(IdentityContext context, Account account, Class<T> storageClass) { EntityMapper attributeMapper = getCredentialAttributeMapper(storageClass); EntityManager entityManager = getEntityManager(context); CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<?> criteria = builder.createQuery(attributeMapper.getEntityType()); Root<?> root = criteria.from(attributeMapper.getEntityType()); List<Predicate> predicates = new ArrayList<Predicate>(); Object agentInstance = getRootEntity(account, entityManager); Property identityTypeProperty = attributeMapper.getProperty(storageClass, OwnerReference.class).getValue(); predicates.add(builder.equal(root.get(identityTypeProperty.getName()), agentInstance)); Property typeProperty = attributeMapper.getProperty(storageClass, CredentialClass.class).getValue(); Property effectiveProperty = attributeMapper.getProperty(storageClass, EffectiveDate.class).getValue(); predicates.add(builder.equal(root.get(typeProperty.getName()), storageClass.getName())); predicates.add(builder.lessThanOrEqualTo(root.<Date>get(effectiveProperty.getName()), new Date())); criteria.where(predicates.toArray(new Predicate[predicates.size()])); criteria.orderBy(builder.desc(root.get(effectiveProperty.getName()))); return entityManager.createQuery(criteria).getResultList(); }
private <T extends CredentialStorage> List<?> findCredentials(IdentityContext context, Account account, Class<T> storageClass) { EntityMapper attributeMapper = getCredentialAttributeMapper(storageClass); EntityManager entityManager = getEntityManager(context); CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<?> criteria = builder.createQuery(attributeMapper.getEntityType()); Root<?> root = criteria.from(attributeMapper.getEntityType()); List<Predicate> predicates = new ArrayList<Predicate>(); Object agentInstance = getRootEntity(account, entityManager); Property identityTypeProperty = attributeMapper.getProperty(storageClass, OwnerReference.class).getValue(); predicates.add(builder.equal(root.get(identityTypeProperty.getName()), agentInstance)); Property typeProperty = attributeMapper.getProperty(storageClass, CredentialClass.class).getValue(); Property effectiveProperty = attributeMapper.getProperty(storageClass, EffectiveDate.class).getValue(); predicates.add(builder.equal(root.get(typeProperty.getName()), storageClass.getName())); predicates.add(builder.lessThanOrEqualTo(root.<Date>get(effectiveProperty.getName()), new Date())); criteria.where(predicates.toArray(new Predicate[predicates.size()])); criteria.orderBy(builder.desc(root.get(effectiveProperty.getName()))); return entityManager.createQuery(criteria).getResultList(); }
public Object getOwnerEntity(final AttributedType attributedType, final Property ownerProperty, final EntityManager entityManager) { EntityMapper attributedTypeMapper = getRootMapper(attributedType.getClass()); Object entity = null; if (ownerProperty.getJavaClass().isAssignableFrom(attributedTypeMapper.getEntityType())) { entity = getRootEntity(attributedType, entityManager); } else { EntityMapper ownerMapper = getMapperForEntity(ownerProperty.getJavaClass()); List associatedEntities = attributedTypeMapper.getAssociatedEntities(attributedType, ownerMapper, entityManager); if (!associatedEntities.isEmpty()) { entity = associatedEntities.get(0); } } return entity; }
@Override protected void removeCredentials(IdentityContext context, Account account) { EntityManager entityManager = getEntityManager(context); List entities = new ArrayList(); for (EntityMapper attributeMapper : getEntityMappers()) { if (attributeMapper.getEntityType().isAnnotationPresent(ManagedCredential.class)) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<?> criteria = builder.createQuery(attributeMapper.getEntityType()); Root<?> root = criteria.from(attributeMapper.getEntityType()); Object agentInstance = getRootEntity(account, entityManager); Property identityTypeProperty = attributeMapper.getProperty(OwnerReference.class).getValue(); criteria.where(builder.equal(root.get(identityTypeProperty.getName()), agentInstance)); Property effectiveProperty = attributeMapper.getProperty(EffectiveDate.class).getValue(); criteria.orderBy(builder.desc(root.get(effectiveProperty.getName()))); List result = entityManager.createQuery(criteria).getResultList(); for (Object storageEntity : result) { entities.add(storageEntity); } } } for (Object credentialEntity : entities) { entityManager.remove(credentialEntity); } }
public Object getOwnerEntity(final AttributedType attributedType, final Property ownerProperty, final EntityManager entityManager) { EntityMapper attributedTypeMapper = getRootMapper(attributedType.getClass()); Object entity = null; if (ownerProperty.getJavaClass().isAssignableFrom(attributedTypeMapper.getEntityType())) { entity = getRootEntity(attributedType, entityManager); } else { EntityMapper ownerMapper = getMapperForEntity(ownerProperty.getJavaClass()); List associatedEntities = attributedTypeMapper.getAssociatedEntities(attributedType, ownerMapper, entityManager); if (!associatedEntities.isEmpty()) { entity = associatedEntities.get(0); } } return entity; }
@Override protected void removeCredentials(IdentityContext context, Account account) { EntityManager entityManager = getEntityManager(context); List entities = new ArrayList(); for (EntityMapper attributeMapper : getEntityMappers()) { if (attributeMapper.getEntityType().isAnnotationPresent(ManagedCredential.class)) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<?> criteria = builder.createQuery(attributeMapper.getEntityType()); Root<?> root = criteria.from(attributeMapper.getEntityType()); Object agentInstance = getRootEntity(account, entityManager); Property identityTypeProperty = attributeMapper.getProperty(OwnerReference.class).getValue(); criteria.where(builder.equal(root.get(identityTypeProperty.getName()), agentInstance)); Property effectiveProperty = attributeMapper.getProperty(EffectiveDate.class).getValue(); criteria.orderBy(builder.desc(root.get(effectiveProperty.getName()))); List result = entityManager.createQuery(criteria).getResultList(); for (Object storageEntity : result) { entities.add(storageEntity); } } } for (Object credentialEntity : entities) { entityManager.remove(credentialEntity); } }
private void storeRelationshipMembers(Relationship relationship, EntityManager entityManager) { Object ownerEntity = getRootEntity(relationship, entityManager); List<Property<IdentityType>> props = PropertyQueries.<IdentityType>createQuery(relationship.getClass()) .addCriteria(new TypedPropertyCriteria(IdentityType.class, MatchOption.SUB_TYPE)).getResultList(); EntityMapper relationshipMemberMapper = getEntityMapperForProperty(relationship.getClass(), RelationshipMember.class); for (Property<IdentityType> prop : props) { Object relationshipIdentity = relationshipMemberMapper.createEntity(); IdentityType identityType = prop.getValue(relationship); if (identityType != null) { Property<Object> identityTypeProperty = relationshipMemberMapper.getProperty(RelationshipMember.class).getValue(); // in this case we hold only the reference to the identity type identifier if (identityTypeProperty.getJavaClass().equals(String.class)) { identityTypeProperty.setValue(relationshipIdentity, IdentityTypeUtil.formatId(identityType)); } else { identityTypeProperty.setValue(relationshipIdentity, getRootEntity(identityType, entityManager)); } Property<Object> descriptorProperty = relationshipMemberMapper.getProperty(RelationshipDescriptor.class).getValue(); Property<Object> ownerProperty = relationshipMemberMapper.getProperty(OwnerReference.class).getValue(); descriptorProperty.setValue(relationshipIdentity, prop.getName()); ownerProperty.setValue(relationshipIdentity, ownerEntity); } entityManager.persist(relationshipIdentity); } }
private void storeRelationshipMembers(Relationship relationship, EntityManager entityManager) { Object ownerEntity = getRootEntity(relationship, entityManager); List<Property<IdentityType>> props = PropertyQueries.<IdentityType>createQuery(relationship.getClass()) .addCriteria(new TypedPropertyCriteria(IdentityType.class, MatchOption.SUB_TYPE)).getResultList(); EntityMapper relationshipMemberMapper = getEntityMapperForProperty(relationship.getClass(), RelationshipMember.class); for (Property<IdentityType> prop : props) { Object relationshipIdentity = relationshipMemberMapper.createEntity(); IdentityType identityType = prop.getValue(relationship); if (identityType != null) { Property<Object> identityTypeProperty = relationshipMemberMapper.getProperty(RelationshipMember.class).getValue(); // in this case we hold only the reference to the identity type identifier if (identityTypeProperty.getJavaClass().equals(String.class)) { identityTypeProperty.setValue(relationshipIdentity, IdentityTypeUtil.formatId(identityType)); } else { identityTypeProperty.setValue(relationshipIdentity, getRootEntity(identityType, entityManager)); } Property<Object> descriptorProperty = relationshipMemberMapper.getProperty(RelationshipDescriptor.class).getValue(); Property<Object> ownerProperty = relationshipMemberMapper.getProperty(OwnerReference.class).getValue(); descriptorProperty.setValue(relationshipIdentity, prop.getName()); ownerProperty.setValue(relationshipIdentity, ownerEntity); } entityManager.persist(relationshipIdentity); } }