@Override public void removeAttribute(IdentityContext context, AttributedType attributedType, String attributeName) { EntityMapper attributeMapper = getAttributeMapper(attributedType.getClass()); EntityManager entityManager = getEntityManager(context); CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<?> cq = cb.createQuery(attributeMapper.getEntityType()); Root<?> from = cq.from(attributeMapper.getEntityType()); List<Predicate> predicates = new ArrayList<Predicate>(); Property attributeNameProperty = attributeMapper.getProperty(Attribute.class, AttributeName.class).getValue(); predicates.add(cb.equal(from.get(attributeNameProperty.getName()), attributeName)); Property ownerProperty = attributeMapper.getProperty(Attribute.class, OwnerReference.class).getValue(); if (getConfig().supportsType(attributedType.getClass(), IdentityOperation.create) && !String.class.equals(ownerProperty.getJavaClass())) { predicates.add(cb.equal(from.get(ownerProperty.getName()), getOwnerEntity(attributedType, ownerProperty, entityManager))); } else { predicates.add(cb.equal(from.get(ownerProperty.getName()), attributedType.getId())); } cq.where(predicates.toArray(new Predicate[predicates.size()])); for (Object entity : entityManager.createQuery(cq).getResultList()) { entityManager.remove(entity); } }
@Override public void removeAttribute(IdentityContext context, AttributedType attributedType, String attributeName) { EntityMapper attributeMapper = getAttributeMapper(attributedType.getClass()); EntityManager entityManager = getEntityManager(context); CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<?> cq = cb.createQuery(attributeMapper.getEntityType()); Root<?> from = cq.from(attributeMapper.getEntityType()); List<Predicate> predicates = new ArrayList<Predicate>(); Property attributeNameProperty = attributeMapper.getProperty(Attribute.class, AttributeName.class).getValue(); predicates.add(cb.equal(from.get(attributeNameProperty.getName()), attributeName)); Property ownerProperty = attributeMapper.getProperty(Attribute.class, OwnerReference.class).getValue(); if (getConfig().supportsType(attributedType.getClass(), IdentityOperation.create) && !String.class.equals(ownerProperty.getJavaClass())) { predicates.add(cb.equal(from.get(ownerProperty.getName()), getOwnerEntity(attributedType, ownerProperty, entityManager))); } else { predicates.add(cb.equal(from.get(ownerProperty.getName()), attributedType.getId())); } cq.where(predicates.toArray(new Predicate[predicates.size()])); for (Object entity : entityManager.createQuery(cq).getResultList()) { entityManager.remove(entity); } }
@Override protected Collection<Attribute<? extends Serializable>> getAttributes(IdentityContext identityContext, AttributedType attributedType) { EntityMapper attributeMapper = getAttributeMapper(attributedType.getClass()); Class<?> attributeEntityClass = attributeMapper.getEntityType(); EntityManager entityManager = getEntityManager(identityContext);
@Override protected Collection<Attribute<? extends Serializable>> getAttributes(IdentityContext identityContext, AttributedType attributedType) { EntityMapper attributeMapper = getAttributeMapper(attributedType.getClass()); Class<?> attributeEntityClass = attributeMapper.getEntityType(); EntityManager entityManager = getEntityManager(identityContext);
@Override public void doSetAttribute(IdentityContext context, AttributedType attributedType, Attribute<? extends Serializable> attribute) { removeAttribute(context, attributedType, attribute.getName()); Serializable values = attribute.getValue(); if (!values.getClass().isArray()) { values = new Serializable[]{values}; } if (values instanceof byte[]) { values = new Serializable[]{values}; } EntityMapper attributeMapper = getAttributeMapper(attributedType.getClass()); Property attributeNameProperty = attributeMapper.getProperty(Attribute.class, AttributeName.class).getValue(); Property attributeValueProperty = attributeMapper.getProperty(Attribute.class, AttributeValue.class).getValue(); Property ownerProperty = attributeMapper.getProperty(Attribute.class, OwnerReference.class).getValue(); EntityManager entityManager = getEntityManager(context); for (Serializable attributeValue : (Serializable[]) values) { Object attributeEntity = attributeMapper.createEntity(); attributeNameProperty.setValue(attributeEntity, attribute.getName()); attributeValueProperty.setValue(attributeEntity, Base64.encodeObject(attributeValue)); if (getConfig().supportsType(attributedType.getClass(), IdentityOperation.create) && !String.class.equals(ownerProperty.getJavaClass())) { ownerProperty.setValue(attributeEntity, getOwnerEntity(attributedType, ownerProperty, entityManager)); } else { ownerProperty.setValue(attributeEntity, attributedType.getId()); } entityManager.persist(attributeEntity); entityManager.flush(); } }
@Override public void doSetAttribute(IdentityContext context, AttributedType attributedType, Attribute<? extends Serializable> attribute) { removeAttribute(context, attributedType, attribute.getName()); Serializable values = attribute.getValue(); if (!values.getClass().isArray()) { values = new Serializable[]{values}; } if (values instanceof byte[]) { values = new Serializable[]{values}; } EntityMapper attributeMapper = getAttributeMapper(attributedType.getClass()); Property attributeNameProperty = attributeMapper.getProperty(Attribute.class, AttributeName.class).getValue(); Property attributeValueProperty = attributeMapper.getProperty(Attribute.class, AttributeValue.class).getValue(); Property ownerProperty = attributeMapper.getProperty(Attribute.class, OwnerReference.class).getValue(); EntityManager entityManager = getEntityManager(context); for (Serializable attributeValue : (Serializable[]) values) { Object attributeEntity = attributeMapper.createEntity(); attributeNameProperty.setValue(attributeEntity, attribute.getName()); attributeValueProperty.setValue(attributeEntity, Base64.encodeObject(attributeValue)); if (getConfig().supportsType(attributedType.getClass(), IdentityOperation.create) && !String.class.equals(ownerProperty.getJavaClass())) { ownerProperty.setValue(attributeEntity, getOwnerEntity(attributedType, ownerProperty, entityManager)); } else { ownerProperty.setValue(attributeEntity, attributedType.getId()); } entityManager.persist(attributeEntity); entityManager.flush(); } }
EntityMapper attributeMapper = getAttributeMapper(attributedType); Subquery<?> subQueryOwnerAttributesByValue = cq.subquery(attributeMapper.getEntityType()); Root fromAttributeType = subQueryOwnerAttributesByValue.from(attributeMapper.getEntityType());
EntityMapper attributeMapper = getAttributeMapper(attributedType); Subquery<?> subQueryOwnerAttributesByValue = cq.subquery(attributeMapper.getEntityType()); Root fromAttributeType = subQueryOwnerAttributesByValue.from(attributeMapper.getEntityType());