use of io.jmix.dynattr.model.Category in project jmix by jmix-framework.
the class DynAttrMetadataImpl method loadCategoryDefinitions.
protected List<CategoryDefinition> loadCategoryDefinitions(String entityName) {
TransactionTemplate template = storeAwareLocator.getTransactionTemplate(dynamicAttributesStore);
template.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
List<CategoryDefinition> result = template.execute(transactionStatus -> {
EntityManager entityManager = storeAwareLocator.getEntityManager(dynamicAttributesStore);
FetchPlan fetchPlan = fetchPlans.builder(Category.class).addFetchPlan(FetchPlan.LOCAL).add("categoryAttrs", builder -> {
builder.addFetchPlan(FetchPlan.LOCAL);
builder.add("category", FetchPlan.LOCAL);
builder.add("defaultEntity", FetchPlan.LOCAL);
}).build();
return entityManager.createQuery("select c from dynat_Category c where c.entityType = :entityType", Category.class).setParameter("entityType", entityName).setHint(PersistenceHints.FETCH_PLAN, fetchPlan).getResultList().stream().map(this::buildCategoryDefinition).collect(Collectors.toList());
});
return result == null ? Collections.emptyList() : result;
}
use of io.jmix.dynattr.model.Category in project jmix by jmix-framework.
the class DynAttrMetadataImpl method buildCategoryDefinition.
protected CategoryDefinition buildCategoryDefinition(Category category) {
MetaClass metaClass = extendedEntities.getOriginalOrThisMetaClass(metadata.getClass(category.getEntityType()));
List<AttributeDefinition> attributes;
if (category.getCategoryAttrs() != null) {
attributes = Collections.unmodifiableList(category.getCategoryAttrs().stream().map(attr -> new CommonAttributeDefinition(attr, buildMetaProperty(attr, metaClass))).collect(Collectors.toList()));
} else {
attributes = Collections.emptyList();
}
return new CommonCategoryDefinition(category, attributes);
}
use of io.jmix.dynattr.model.Category in project jmix by jmix-framework.
the class CategoryEdit method onIsDefaultFieldValueChange.
@Subscribe("isDefaultField")
protected void onIsDefaultFieldValueChange(HasValue.ValueChangeEvent<Boolean> event) {
if (Boolean.TRUE.equals(event.getValue())) {
FetchPlan fetchPlan = fetchPlans.builder(Category.class).add("isDefault").build();
LoadContext<Category> lc = new LoadContext(metadata.getClass(Category.class)).setFetchPlan(fetchPlan);
Category category = getEditedEntity();
lc.setQueryString("select c from dynat_Category c where c.entityType = :entityType and not c.id = :id").setParameter("entityType", category.getEntityType()).setParameter("id", category.getId());
List<Category> result = dataManager.loadList(lc);
for (Category cat : result) {
cat.setIsDefault(false);
}
SaveContext saveContext = new SaveContext().saving(result);
dataManager.save(saveContext);
}
}
use of io.jmix.dynattr.model.Category in project jmix by jmix-framework.
the class DynamicAttributesGuiTools method setDefaultAttributeValue.
protected void setDefaultAttributeValue(Entity item, AttributeDefinition attribute, boolean entityIsCategorized, ZonedDateTime currentTimestamp) {
String propertyName = DynAttrUtils.getPropertyFromAttributeCode(attribute.getCode());
if (entityIsCategorized) {
Category entityCategory = ((Categorized) item).getCategory();
Category attributeCategory = ((CategoryAttribute) attribute.getSource()).getCategory();
if (!Objects.equals(entityCategory, attributeCategory)) {
// cleanup attributes from not dedicated category
EntityValues.setValue(item, propertyName, null);
return;
}
}
if (EntityValues.getValue(item, propertyName) != null) {
// skip not null attributes
return;
}
if (attribute.getDefaultValue() != null) {
if (attribute.getDataType() == AttributeType.ENTITY) {
MetaClass entityMetaClass = metadata.getClassNN(attribute.getJavaType());
LoadContext<Object> lc = new LoadContext<>(entityMetaClass).setFetchPlan(fetchPlanRepository.getFetchPlan(entityMetaClass, FetchPlan.INSTANCE_NAME));
String pkName = referenceToEntitySupport.getPrimaryKeyForLoadingEntity(entityMetaClass);
lc.setQueryString(format("select e from %s e where e.%s = :entityId", entityMetaClass.getName(), pkName)).setParameter("entityId", attribute.getDefaultValue());
Entity defaultEntity = (Entity) dataManager.load(lc);
EntityValues.setValue(item, propertyName, defaultEntity);
} else if (attribute.isCollection()) {
List<Object> list = new ArrayList<>();
list.add(attribute.getDefaultValue());
EntityValues.setValue(item, propertyName, list);
} else {
EntityValues.setValue(item, propertyName, attribute.getDefaultValue());
}
} else if (Boolean.TRUE.equals(attribute.isDefaultDateCurrent())) {
if (attribute.getDataType() == AttributeType.DATE_WITHOUT_TIME) {
EntityValues.setValue(item, propertyName, currentTimestamp.toLocalDate());
} else {
EntityValues.setValue(item, propertyName, Date.from(currentTimestamp.toInstant()));
}
}
}
use of io.jmix.dynattr.model.Category in project jmix by jmix-framework.
the class CategoryAttrsEdit method onValidation.
@Subscribe
protected void onValidation(ValidationEvent event) {
ValidationErrors validationErrors = new ValidationErrors();
CategoryAttribute attribute = getEditedEntity();
AttributeType dataType = attribute.getDataType();
CategoryAttributeConfiguration configuration = attribute.getConfiguration();
if (INTEGER.equals(dataType)) {
ValidationErrors errors = validateNumbers(INTEGER, configuration.getMinInt(), configuration.getMaxInt(), attribute.getDefaultInt());
validationErrors.addAll(errors);
} else if (DOUBLE.equals(dataType)) {
ValidationErrors errors = validateNumbers(DOUBLE, configuration.getMinDouble(), configuration.getMaxDouble(), attribute.getDefaultDouble());
validationErrors.addAll(errors);
} else if (DECIMAL.equals(dataType)) {
ValidationErrors errors = validateNumbers(DECIMAL, configuration.getMinDecimal(), configuration.getMaxDecimal(), attribute.getDefaultDecimal());
validationErrors.addAll(errors);
} else if (ENUMERATION.equals(dataType)) {
ValidationErrors errors = validateEnumeration(attribute.getEnumeration(), attribute.getDefaultString());
validationErrors.addAll(errors);
}
Category category = getEditedEntity().getCategory();
if (category != null && category.getCategoryAttrs() != null) {
for (CategoryAttribute categoryAttribute : category.getCategoryAttrs()) {
if (!categoryAttribute.equals(attribute)) {
if (categoryAttribute.getName().equals(attribute.getName())) {
validationErrors.add(messages.getMessage(CategoryAttrsEdit.class, "uniqueName"));
return;
} else if (categoryAttribute.getCode().equals(attribute.getCode())) {
validationErrors.add(messages.getMessage(CategoryAttrsEdit.class, "uniqueCode"));
return;
}
}
}
}
event.addErrors(validationErrors);
}
Aggregations