public Object getRootEntity(AttributedType attributedType, EntityManager entityManager) { return entityManager.find(getRootMapper(attributedType.getClass()).getEntityType(), attributedType.getId()); }
private <V extends IdentityType> IdentityType lookupIdentityTypeById(IdentityContext context, Class<V> type, String identifier) { EntityManager entityManager = getEntityManager(context); if (IdentityType.class.equals(type)) { // when querying based on the IdentityType base type, we try to load the instance from all available mappers. for (EntityMapper entityMapper : getEntityMappers()) { if (entityMapper.getMappingsFor(type) != null && entityMapper.isRoot()) { Object entity = entityManager.find(entityMapper.getEntityType(), identifier); V identityType = entityMapper.<V>createType(entity, entityManager); if (identityType != null) { return identityType; } } } } else { // we know the right type, we just lookup based on its root mapper Object entity = entityManager.find(getRootMapper(type).getEntityType(), identifier); if (entity != null) { return getRootMapperForEntity(entity.getClass()).<V>createType(entity, entityManager); } } return null; }
public Object getRootEntity(AttributedType attributedType, EntityManager entityManager) { return entityManager.find(getRootMapper(attributedType.getClass()).getEntityType(), attributedType.getId()); }
private <V extends IdentityType> IdentityType lookupIdentityTypeById(IdentityContext context, Class<V> type, String identifier) { EntityManager entityManager = getEntityManager(context); if (IdentityType.class.equals(type)) { // when querying based on the IdentityType base type, we try to load the instance from all available mappers. for (EntityMapper entityMapper : getEntityMappers()) { if (entityMapper.getMappingsFor(type) != null && entityMapper.isRoot()) { Object entity = entityManager.find(entityMapper.getEntityType(), identifier); V identityType = entityMapper.<V>createType(entity, entityManager); if (identityType != null) { return identityType; } } } } else { // we know the right type, we just lookup based on its root mapper Object entity = entityManager.find(getRootMapper(type).getEntityType(), identifier); if (entity != null) { return getRootMapperForEntity(entity.getClass()).<V>createType(entity, entityManager); } } return null; }
@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)); }
EntityManager entityManager = getEntityManager(context); EntityMapper relMapper = getRootMapper(relationshipType);
@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)); }
EntityManager entityManager = getEntityManager(context); EntityMapper relMapper = getRootMapper(relationshipType);
@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); }
@Override public <P extends Partition> P lookupById(final IdentityContext context, final Class<P> partitionClass, final String id) { EntityManager entityManager = getEntityManager(context); EntityMapper entityMapper = getRootMapper(Partition.class); CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(entityMapper.getEntityType()); Root from = cq.from(entityMapper.getEntityType()); List<Predicate> predicates = new ArrayList<Predicate>(); Property idProperty = entityMapper.getProperty(Partition.class, Identifier.class).getValue(); predicates.add(cb.equal(from.get(idProperty.getName()), id)); if (!Partition.class.equals(partitionClass)) { Property typeProperty = entityMapper.getProperty(partitionClass, PartitionClass.class).getValue(); predicates.add(cb.equal(from.get(typeProperty.getName()), partitionClass.getName())); } cq.where(predicates.toArray(new Predicate[predicates.size()])); Query query = entityManager.createQuery(cq); query.setMaxResults(1); List result = query.getResultList(); if (!result.isEmpty()) { return entityMapper.createType(result.get(0), entityManager); } return null; }
@Override public <P extends Partition> P lookupById(final IdentityContext context, final Class<P> partitionClass, final String id) { EntityManager entityManager = getEntityManager(context); EntityMapper entityMapper = getRootMapper(Partition.class); CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(entityMapper.getEntityType()); Root from = cq.from(entityMapper.getEntityType()); List<Predicate> predicates = new ArrayList<Predicate>(); Property idProperty = entityMapper.getProperty(Partition.class, Identifier.class).getValue(); predicates.add(cb.equal(from.get(idProperty.getName()), id)); if (!Partition.class.equals(partitionClass)) { Property typeProperty = entityMapper.getProperty(partitionClass, PartitionClass.class).getValue(); predicates.add(cb.equal(from.get(typeProperty.getName()), partitionClass.getName())); } cq.where(predicates.toArray(new Predicate[predicates.size()])); Query query = entityManager.createQuery(cq); query.setMaxResults(1); List result = query.getResultList(); if (!result.isEmpty()) { return entityMapper.createType(result.get(0), entityManager); } return null; }
private List<?> findChildRelationships(IdentityContext context, Relationship relationship) { EntityManager em = getEntityManager(context); EntityMapper relationshipMemberMapper = getEntityMapperForProperty(relationship.getClass(), RelationshipMember.class); CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<?> criteria = builder.createQuery(relationshipMemberMapper.getEntityType()); Root<?> root = criteria.from(relationshipMemberMapper.getEntityType()); List<Predicate> predicates = new ArrayList<Predicate>(); Property ownerProperty = relationshipMemberMapper.getProperty(OwnerReference.class).getValue(); Join<?, ?> join = root.join(ownerProperty.getName()); EntityMapper relationshipMapper = getRootMapper(relationship.getClass()); Property identifierProperty = relationshipMapper.getProperty(Identifier.class).getValue(); predicates.add(builder.equal(join.get(identifierProperty.getName()), relationship.getId())); criteria.where(predicates.toArray(new Predicate[predicates.size()])); return em.createQuery(criteria).getResultList(); }
private List<?> findChildRelationships(IdentityContext context, Relationship relationship) { EntityManager em = getEntityManager(context); EntityMapper relationshipMemberMapper = getEntityMapperForProperty(relationship.getClass(), RelationshipMember.class); CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<?> criteria = builder.createQuery(relationshipMemberMapper.getEntityType()); Root<?> root = criteria.from(relationshipMemberMapper.getEntityType()); List<Predicate> predicates = new ArrayList<Predicate>(); Property ownerProperty = relationshipMemberMapper.getProperty(OwnerReference.class).getValue(); Join<?, ?> join = root.join(ownerProperty.getName()); EntityMapper relationshipMapper = getRootMapper(relationship.getClass()); Property identifierProperty = relationshipMapper.getProperty(Identifier.class).getValue(); predicates.add(builder.equal(join.get(identifierProperty.getName()), relationship.getId())); criteria.where(predicates.toArray(new Predicate[predicates.size()])); return em.createQuery(criteria).getResultList(); }
@Override public String getConfigurationName(IdentityContext identityContext, Partition partition) { EntityMapper entityMapper = getRootMapper(partition.getClass()); EntityManager entityManager = getEntityManager(identityContext); Object partitionEntity = entityManager.find(entityMapper.getEntityType(), partition.getId()); Property configurationNameProperty = entityMapper.getProperty(partition.getClass(), ConfigurationName.class).getValue(); String configurationName = configurationNameProperty.getValue(partitionEntity).toString(); if (isNullOrEmpty(configurationName)) { throw MESSAGES.partitionWithNoConfigurationName(partition); } return configurationName; }
@Override public String getConfigurationName(IdentityContext identityContext, Partition partition) { EntityMapper entityMapper = getRootMapper(partition.getClass()); EntityManager entityManager = getEntityManager(identityContext); Object partitionEntity = entityManager.find(entityMapper.getEntityType(), partition.getId()); Property configurationNameProperty = entityMapper.getProperty(partition.getClass(), ConfigurationName.class).getValue(); String configurationName = configurationNameProperty.getValue(partitionEntity).toString(); if (isNullOrEmpty(configurationName)) { throw MESSAGES.partitionWithNoConfigurationName(partition); } return configurationName; }
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; }
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; }
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;
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;