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(); }
@Override public void storeCredential(IdentityContext context, Account account, CredentialStorage storage) { EntityMapper credentialMapper = getCredentialAttributeMapper(storage.getClass()); Object newCredential = credentialMapper.createEntity(); EntityManager entityManager = getEntityManager(context); for (EntityMapping entityMapping : credentialMapper.getEntityMappings()) { for (Property property : entityMapping.getProperties().keySet()) { Property mappedProperty = entityMapping.getProperties().get(property); if (mappedProperty.getAnnotatedElement().isAnnotationPresent(OwnerReference.class)) { mappedProperty.setValue(newCredential, getOwnerEntity(account, mappedProperty, entityManager)); } else { mappedProperty.setValue(newCredential, property.getValue(storage)); } } } entityManager.persist(newCredential); entityManager.flush(); }
@Override public void storeCredential(IdentityContext context, Account account, CredentialStorage storage) { EntityMapper credentialMapper = getCredentialAttributeMapper(storage.getClass()); Object newCredential = credentialMapper.createEntity(); EntityManager entityManager = getEntityManager(context); for (EntityMapping entityMapping : credentialMapper.getEntityMappings()) { for (Property property : entityMapping.getProperties().keySet()) { Property mappedProperty = entityMapping.getProperties().get(property); if (mappedProperty.getAnnotatedElement().isAnnotationPresent(OwnerReference.class)) { mappedProperty.setValue(newCredential, getOwnerEntity(account, mappedProperty, entityManager)); } else { mappedProperty.setValue(newCredential, property.getValue(storage)); } } } entityManager.persist(newCredential); entityManager.flush(); }
private <T extends CredentialStorage> T convertToCredentialStorage(Object entity, Class<T> storageType) { T storage = null; if (entity != null) { EntityMapper credentialMapper = getCredentialAttributeMapper(storageType); try { storage = newInstance(storageType); } catch (Exception e) { throw MESSAGES.instantiationError(storageType, e); } for (EntityMapping entityMapping : credentialMapper.getEntityMappings()) { for (Property property : entityMapping.getProperties().keySet()) { Property mappedProperty = entityMapping.getProperties().get(property); if (!mappedProperty.getAnnotatedElement().isAnnotationPresent(OwnerReference.class)) { property.setValue(storage, mappedProperty.getValue(entity)); } } } } return storage; }
private <T extends CredentialStorage> T convertToCredentialStorage(Object entity, Class<T> storageType) { T storage = null; if (entity != null) { EntityMapper credentialMapper = getCredentialAttributeMapper(storageType); try { storage = newInstance(storageType); } catch (Exception e) { throw MESSAGES.instantiationError(storageType, e); } for (EntityMapping entityMapping : credentialMapper.getEntityMappings()) { for (Property property : entityMapping.getProperties().keySet()) { Property mappedProperty = entityMapping.getProperties().get(property); if (!mappedProperty.getAnnotatedElement().isAnnotationPresent(OwnerReference.class)) { property.setValue(storage, mappedProperty.getValue(entity)); } } } } return storage; }