use of com.haulmont.cuba.core.entity.BaseGenericIdEntity in project cuba by cuba-platform.
the class EditAction method internalOpenEditor.
protected void internalOpenEditor(CollectionDatasource datasource, Entity existingItem, Datasource parentDs, Map<String, Object> params) {
Window.Editor window = target.getFrame().openEditor(getWindowId(), existingItem, getOpenType(), params, parentDs);
if (editorCloseListener == null) {
window.addCloseListener(actionId -> {
// move focus to owner
target.requestFocus();
if (Window.COMMIT_ACTION_ID.equals(actionId)) {
Entity editedItem = window.getItem();
if (editedItem != null) {
if (parentDs == null) {
if (editedItem instanceof BaseGenericIdEntity) {
BaseGenericIdEntity genericEditedEntity = (BaseGenericIdEntity) editedItem;
if (datasource.getLoadDynamicAttributes() && genericEditedEntity.getDynamicAttributes() == null) {
DynamicAttributesGuiTools dynamicAttributesGuiTools = AppBeans.get(DynamicAttributesGuiTools.class);
dynamicAttributesGuiTools.reloadDynamicAttributes(genericEditedEntity);
}
}
// noinspection unchecked
datasource.updateItem(editedItem);
}
afterCommit(editedItem);
if (afterCommitHandler != null) {
afterCommitHandler.handle(editedItem);
}
}
}
afterWindowClosed(window);
if (afterWindowClosedHandler != null) {
afterWindowClosedHandler.handle(window, actionId);
}
});
} else {
window.addCloseListener(editorCloseListener);
}
}
use of com.haulmont.cuba.core.entity.BaseGenericIdEntity in project cuba by cuba-platform.
the class PersistenceSecurityImpl method calculateFilteredData.
@SuppressWarnings("unchecked")
protected boolean calculateFilteredData(Entity entity, Set<EntityId> handled, boolean checkPermitted) {
MetaClass metaClass = entity.getMetaClass();
if (!isPermittedInMemory(entity) && checkPermitted) {
return true;
}
EntityId entityId = new EntityId(referenceToEntitySupport.getReferenceId(entity), metaClass.getName());
if (handled.contains(entityId)) {
return false;
}
handled.add(entityId);
if (entity instanceof BaseGenericIdEntity) {
BaseGenericIdEntity baseGenericIdEntity = (BaseGenericIdEntity) entity;
for (MetaProperty property : metaClass.getProperties()) {
if (metadataTools.isPersistent(property) && PersistenceHelper.isLoaded(entity, property.getName())) {
Object value = entity.getValue(property.getName());
if (value instanceof Collection) {
Set filtered = new LinkedHashSet();
for (Entity item : (Collection<Entity>) value) {
if (calculateFilteredData(item, handled, true)) {
filtered.add(referenceToEntitySupport.getReferenceId(item));
}
}
if (!filtered.isEmpty()) {
securityTokenManager.addFiltered(baseGenericIdEntity, property.getName(), filtered);
}
} else if (value instanceof Entity) {
Entity valueEntity = (Entity) value;
if (calculateFilteredData(valueEntity, handled, true)) {
securityTokenManager.addFiltered(baseGenericIdEntity, property.getName(), referenceToEntitySupport.getReferenceId(valueEntity));
}
}
}
}
securityTokenManager.writeSecurityToken(baseGenericIdEntity);
}
return false;
}
use of com.haulmont.cuba.core.entity.BaseGenericIdEntity in project cuba by cuba-platform.
the class ComponentsHelper method handleFilteredAttributes.
/**
* Set field's "required" flag to false if the value has been filtered by Row Level Security
* This is necessary to allow user to submit form with filtered attribute even if attribute is required
*/
public static void handleFilteredAttributes(Field component, Datasource datasource, MetaPropertyPath mpp) {
if (component.isRequired() && datasource.getState() == Datasource.State.VALID && datasource.getItem() != null && mpp.getMetaProperty().getRange().isClass()) {
Entity targetItem = datasource.getItem();
MetaProperty[] propertiesChain = mpp.getMetaProperties();
if (propertiesChain.length > 1) {
String basePropertyItem = Arrays.stream(propertiesChain).limit(propertiesChain.length - 1).map(MetadataObject::getName).collect(Collectors.joining("."));
targetItem = datasource.getItem().getValueEx(basePropertyItem);
}
if (targetItem instanceof BaseGenericIdEntity) {
String metaPropertyName = mpp.getMetaProperty().getName();
Object value = targetItem.getValue(metaPropertyName);
BaseGenericIdEntity baseGenericIdEntity = (BaseGenericIdEntity) targetItem;
String[] filteredAttributes = getFilteredAttributes(baseGenericIdEntity);
if (value == null && filteredAttributes != null && ArrayUtils.contains(filteredAttributes, metaPropertyName)) {
component.setRequired(false);
}
}
}
}
use of com.haulmont.cuba.core.entity.BaseGenericIdEntity in project cuba by cuba-platform.
the class DynamicAttributesGuiTools method reloadDynamicAttributes.
/**
* Reload dynamic attributes on the entity
*/
@SuppressWarnings("unchecked")
public void reloadDynamicAttributes(BaseGenericIdEntity entity) {
MetaClass metaClass = entity.getMetaClass();
View view = new View(metaClass.getJavaClass(), false).addProperty(metadata.getTools().getPrimaryKeyName(metaClass));
LoadContext loadContext = new LoadContext(metaClass).setView(view).setLoadDynamicAttributes(true).setId(entity.getId());
BaseGenericIdEntity reloadedEntity = (BaseGenericIdEntity) dataManager.load(loadContext);
if (reloadedEntity != null) {
entity.setDynamicAttributes(reloadedEntity.getDynamicAttributes());
}
}
use of com.haulmont.cuba.core.entity.BaseGenericIdEntity in project cuba by cuba-platform.
the class DynamicAttributesGuiTools method initDefaultAttributeValues.
public void initDefaultAttributeValues(BaseGenericIdEntity item, MetaClass metaClass) {
Preconditions.checkNotNullArgument(metaClass, "metaClass is null");
Collection<CategoryAttribute> attributes = dynamicAttributes.getAttributesForMetaClass(metaClass);
if (item.getDynamicAttributes() == null) {
item.setDynamicAttributes(new HashMap<>());
}
Date currentTimestamp = AppBeans.get(TimeSource.NAME, TimeSource.class).currentTimestamp();
boolean entityIsCategorized = item instanceof Categorized && ((Categorized) item).getCategory() != null;
for (CategoryAttribute categoryAttribute : attributes) {
String code = DynamicAttributesUtils.encodeAttributeCode(categoryAttribute.getCode());
if (entityIsCategorized && !categoryAttribute.getCategory().equals(((Categorized) item).getCategory())) {
// cleanup attributes from not dedicated category
item.setValue(code, null);
continue;
}
if (item.getValue(code) != null) {
// skip not null attributes
continue;
}
if (categoryAttribute.getDefaultValue() != null) {
if (BooleanUtils.isTrue(categoryAttribute.getIsEntity())) {
MetaClass entityMetaClass = metadata.getClassNN(categoryAttribute.getJavaClassForEntity());
LoadContext<Entity> lc = new LoadContext<>(entityMetaClass).setView(View.MINIMAL);
String pkName = referenceToEntitySupport.getPrimaryKeyForLoadingEntity(entityMetaClass);
lc.setQueryString(format("select e from %s e where e.%s = :entityId", entityMetaClass.getName(), pkName)).setParameter("entityId", categoryAttribute.getDefaultValue());
Entity defaultEntity = dataManager.load(lc);
item.setValue(code, defaultEntity);
} else {
item.setValue(code, categoryAttribute.getDefaultValue());
}
} else if (Boolean.TRUE.equals(categoryAttribute.getDefaultDateIsCurrent())) {
item.setValue(code, currentTimestamp);
}
}
}
Aggregations