EntityManager em = getEntityManager(ctx);
Property<?> nameProperty = modelProperties.get(PROPERTY_IDENTITY_NAME);
Property<?> typeProperty = modelProperties.get(PROPERTY_IDENTITY_TYPE);
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<?> criteria = builder.createQuery(identityClass);
Root<?> root = criteria.from(identityClass);
List<Predicate> predicates = new ArrayList<Predicate>();
predicates.add(builder.equal(root.get(nameProperty.getName()),
identity.getName()));
predicates.add(builder.equal(root.get(typeProperty.getName()),
lookupIdentityType(identity.getIdentityType().getName(), em)));
criteria.where(predicates.toArray(new Predicate[predicates.size()]));
try {
Object instance = em.createQuery(criteria).getSingleResult();
// If there is a credential class, delete any credentials
if (credentialClass != null) {
Property<?> credentialIdentityProp = modelProperties.get(PROPERTY_CREDENTIAL_IDENTITY);
criteria = builder.createQuery(credentialClass);
root = criteria.from(credentialClass);
predicates = new ArrayList<Predicate>();
predicates.add(builder.equal(root.get(credentialIdentityProp.getName()),
instance));
criteria.where(predicates.toArray(new Predicate[predicates.size()]));
List<?> results = em.createQuery(criteria).getResultList();
for (Object result : results) {
em.remove(result);
}
}
// If there is an attribute class, delete any attributes
if (attributeClass != null) {
Property<?> attributeIdentityProperty = modelProperties.get(PROPERTY_ATTRIBUTE_IDENTITY);
criteria = builder.createQuery(attributeClass);
root = criteria.from(attributeClass);
predicates = new ArrayList<Predicate>();
predicates.add(builder.equal(root.get(attributeIdentityProperty.getName()),
instance));
criteria.where(predicates.toArray(new Predicate[predicates.size()]));
List<?> results = em.createQuery(criteria).getResultList();
for (Object result : results) {