use of io.jmix.dynattr.model.CategoryAttributeValue in project jmix by jmix-framework.
the class DynAttrManagerImpl method fetchEntityValues.
/**
* Method loads entity values for CategoryAttributeValues of entity type and sets entity values to the corresponding
* property of the {@code CategoryAttributeValue} entity.
*/
@SuppressWarnings("unchecked")
protected void fetchEntityValues(Collection<AccessConstraint<?>> accessConstraints, List<CategoryAttributeValue> values) {
Multimap<MetaClass, Object> entityIds = HashMultimap.create();
Multimap<MetaClass, CategoryAttributeValue> valuesByType = HashMultimap.create();
for (CategoryAttributeValue value : values) {
String className = value.getCategoryAttribute().getEntityClass();
try {
Class<?> aClass = ReflectionHelper.loadClass(className);
MetaClass metaClass = metadata.getClass(aClass);
CrudEntityContext crudEntityContext = new CrudEntityContext(metaClass);
accessManager.applyConstraints(crudEntityContext, accessConstraints);
if (crudEntityContext.isReadPermitted()) {
entityIds.put(metaClass, value.getObjectEntityValueId());
valuesByType.put(metaClass, value);
}
} catch (ClassNotFoundException e) {
log.error("Class {} not found", className);
}
}
EntityManager entityManager = storeAwareLocator.getEntityManager(dynamicAttributesStore);
for (Map.Entry<MetaClass, Collection<Object>> entry : entityIds.asMap().entrySet()) {
MetaClass metaClass = entry.getKey();
Collection<Object> ids = entry.getValue();
if (!ids.isEmpty()) {
String pkName = referenceToEntitySupport.getPrimaryKeyForLoadingEntity(metaClass);
List<?> resultList = entityManager.createQuery(String.format("select e from %s e where e.%s in :ids", metaClass.getName(), pkName)).setParameter("ids", ids).setHint(PersistenceHints.FETCH_PLAN, fetchPlanRepository.getFetchPlan(metaClass, FetchPlan.INSTANCE_NAME)).getResultList();
Map<Object, Object> entityById = new LinkedHashMap<>();
for (Object entity : resultList) {
entityById.put(EntityValues.getId(entity), entity);
}
for (CategoryAttributeValue value : valuesByType.get(metaClass)) {
value.setTransientEntityValue(entityById.get(value.getObjectEntityValueId()));
}
}
}
}
use of io.jmix.dynattr.model.CategoryAttributeValue in project jmix by jmix-framework.
the class DynAttrManagerImpl method fetchCollectionValues.
protected List<CategoryAttributeValue> fetchCollectionValues(List<CategoryAttributeValue> values) {
EntityManager entityManager = storeAwareLocator.getEntityManager(dynamicAttributesStore);
List<UUID> ids = values.stream().map(CategoryAttributeValue::getId).collect(Collectors.toList());
FetchPlan fetchPlan = fetchPlans.builder(CategoryAttributeValue.class).addFetchPlan(FetchPlan.LOCAL).add("childValues", FetchPlan.LOCAL).add("categoryAttribute", builder -> builder.addFetchPlan(FetchPlan.LOCAL).add("category")).build();
return entityManager.createQuery("select v from dynat_CategoryAttributeValue v where v.id in :ids", CategoryAttributeValue.class).setParameter("ids", ids).setHint(PersistenceHints.FETCH_PLAN, fetchPlan).getResultList();
}
use of io.jmix.dynattr.model.CategoryAttributeValue in project jmix by jmix-framework.
the class DynAttrManagerImpl method doStoreValues.
@SuppressWarnings("unchecked")
protected void doStoreValues(Object entity, Collection<AccessConstraint<?>> accessConstraints) {
DynamicAttributesState state = getExtraState(entity, DynamicAttributesState.class);
if (state != null && state.getDynamicAttributes() != null) {
EntityManager entityManager = storeAwareLocator.getEntityManager(dynamicAttributesStore);
DynamicAttributes dynamicModel = state.getDynamicAttributes();
DynamicAttributes.Changes changes = dynamicModel.getChanges();
if (changes.hasChanges()) {
MetaClass metaClass = metadata.getClass(entity.getClass());
List<CategoryAttributeValue> attributeValues = loadValues(metaClass, accessConstraints, Collections.singletonList(referenceToEntitySupport.getReferenceId(entity)));
for (CategoryAttributeValue attributeValue : attributeValues) {
String attributeName = attributeValue.getCode();
if (changes.isDeleted(attributeName)) {
setValueToCategoryAttributeValue(attributeValue, null);
entityManager.remove(attributeValue);
} else if (changes.isUpdated(attributeName)) {
setValueToCategoryAttributeValue(attributeValue, dynamicModel.getValue(attributeName));
if (BooleanUtils.isTrue(attributeValue.getCategoryAttribute().getIsCollection())) {
doStoreCollectionValue(attributeValue);
}
}
}
List<String> existing = attributeValues.stream().map(CategoryAttributeValue::getCode).collect(Collectors.toList());
List<String> toPersist = Stream.concat(changes.getCreated().keySet().stream(), // Haulmont/jmix-data#43
changes.getUpdated().keySet().stream().filter(a -> !existing.contains(a))).collect(Collectors.toList());
for (String attributeName : toPersist) {
dynAttrMetadata.getAttributeByCode(metaClass, attributeName).ifPresent(attribute -> {
CategoryAttributeValue attributeValue = metadata.create(CategoryAttributeValue.class);
setValueToCategoryAttributeValue(attributeValue, dynamicModel.getValue(attributeName));
attributeValue.setObjectEntityId(referenceToEntitySupport.getReferenceId(entity));
attributeValue.setCode(attributeName);
attributeValue.setCategoryAttribute((CategoryAttribute) attribute.getSource());
entityManager.persist(attributeValue);
if (attribute.isCollection()) {
doStoreCollectionValue(attributeValue);
}
});
}
}
// todo: refresh state
// state.setValues(mergedValues);
}
}
use of io.jmix.dynattr.model.CategoryAttributeValue in project jmix by jmix-framework.
the class DynAttrManagerImpl method findValuesByEntityIds.
protected List<CategoryAttributeValue> findValuesByEntityIds(MetaClass metaClass, List<Object> entityIds) {
EntityManager entityManager = storeAwareLocator.getEntityManager(dynamicAttributesStore);
FetchPlan fetchPlan = fetchPlans.builder(CategoryAttributeValue.class).add("categoryAttribute", builder -> {
builder.addFetchPlan(FetchPlan.LOCAL);
builder.add("defaultEntity", FetchPlan.LOCAL);
builder.add("category");
}).build();
List<CategoryAttributeValue> result;
if (metadataTools.hasUuid(metaClass)) {
result = entityManager.createQuery(String.format("select v from dynat_CategoryAttributeValue v where v.entity.%s in :ids and v.parent is null", referenceToEntitySupport.getReferenceIdPropertyName(metaClass)), CategoryAttributeValue.class).setParameter("ids", entityIds).setHint(PersistenceHints.FETCH_PLAN, fetchPlan).getResultList();
} else {
result = entityManager.createQuery(String.format("select v from dynat_CategoryAttributeValue v where v.entity.%s in :ids " + "and v.categoryAttribute.categoryEntityType = :entityType and v.parent is null", referenceToEntitySupport.getReferenceIdPropertyName(metaClass)), CategoryAttributeValue.class).setParameter("ids", entityIds).setParameter("entityType", metaClass.getName()).setHint(PersistenceHints.FETCH_PLAN, fetchPlan).getResultList();
}
return result.stream().filter(v -> v.getDeleteTs() == null).collect(Collectors.toList());
}
use of io.jmix.dynattr.model.CategoryAttributeValue in project jmix by jmix-framework.
the class DynAttrManagerImpl method doStoreCollectionValue.
/**
* Removes nested {@code CategoryAttributeValue} entities for items that were removed from the collection value
* and creates new child {@code CategoryAttributeValue} instances for just added collection value items.
*
* @param collectionAttributeValue
*/
protected void doStoreCollectionValue(CategoryAttributeValue collectionAttributeValue) {
EntityManager entityManager = storeAwareLocator.getEntityManager(dynamicAttributesStore);
List<Object> collection = collectionAttributeValue.getTransientCollectionValue();
List<Object> newCollection = new ArrayList<>(collection);
if (collectionAttributeValue.getChildValues() != null) {
for (CategoryAttributeValue existingChild : collectionAttributeValue.getChildValues()) {
if (existingChild.getDeleteTs() == null) {
if (!collection.contains(existingChild.getValue())) {
entityManager.remove(existingChild);
}
newCollection.remove(existingChild.getValue());
}
}
}
for (Object value : newCollection) {
CategoryAttributeValue childValue = metadata.create(CategoryAttributeValue.class);
childValue.setParent(collectionAttributeValue);
setValueToCategoryAttributeValue(childValue, value);
if (collectionAttributeValue.getObjectEntityId() != null) {
childValue.setObjectEntityId(collectionAttributeValue.getObjectEntityId());
}
childValue.setCode(collectionAttributeValue.getCode());
childValue.setCategoryAttribute(collectionAttributeValue.getCategoryAttribute());
entityManager.persist(childValue);
}
}
Aggregations