use of com.haulmont.cuba.core.entity.Categorized 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);
}
}
}
use of com.haulmont.cuba.core.entity.Categorized in project cuba by cuba-platform.
the class EditorWindowDelegate method setItem.
@SuppressWarnings("unchecked")
public void setItem(Entity item) {
Datasource ds = getDatasource();
DataSupplier dataservice = ds.getDataSupplier();
DatasourceImplementation parentDs = (DatasourceImplementation) ((DatasourceImplementation) ds).getParent();
DynamicAttributesGuiTools dynamicAttributesGuiTools = AppBeans.get(DynamicAttributesGuiTools.NAME);
if (dynamicAttributesGuiTools.screenContainsDynamicAttributes(ds.getView(), getWrapper().getId())) {
ds.setLoadDynamicAttributes(true);
}
Class<? extends Entity> entityClass = item.getClass();
Object entityId = item.getId();
if (parentDs != null) {
if (!PersistenceHelper.isNew(item) && !parentDs.getItemsToCreate().contains(item) && !parentDs.getItemsToUpdate().contains(item) && parentDs instanceof CollectionDatasource && ((CollectionDatasource) parentDs).containsItem(item.getId()) && !entityStates.isLoadedWithView(item, ds.getView())) {
item = dataservice.reload(item, ds.getView(), ds.getMetaClass(), ds.getLoadDynamicAttributes());
if (parentDs instanceof CollectionPropertyDatasourceImpl) {
((CollectionPropertyDatasourceImpl) parentDs).replaceItem(item);
} else {
((CollectionDatasource) parentDs).updateItem(item);
}
}
item = EntityCopyUtils.copyCompositions(item);
handlePreviouslyDeletedCompositionItems(item, parentDs);
} else if (!PersistenceHelper.isNew(item)) {
item = dataservice.reload(item, ds.getView(), ds.getMetaClass(), ds.getLoadDynamicAttributes());
}
if (item == null) {
throw new EntityAccessException(entityClass, entityId);
}
if (PersistenceHelper.isNew(item) && !ds.getMetaClass().equals(item.getMetaClass())) {
Entity newItem = ds.getDataSupplier().newInstance(ds.getMetaClass());
metadata.getTools().copy(item, newItem);
item = newItem;
}
if (ds.getLoadDynamicAttributes() && item instanceof BaseGenericIdEntity) {
if (PersistenceHelper.isNew(item)) {
dynamicAttributesGuiTools.initDefaultAttributeValues((BaseGenericIdEntity) item, item.getMetaClass());
}
if (item instanceof Categorized) {
dynamicAttributesGuiTools.listenCategoryChanges(ds);
}
}
ds.setItem(item);
if (PersistenceHelper.isNew(item)) {
// make sure that they will be saved on commit.
for (Datasource datasource : ds.getDsContext().getAll()) {
if (datasource instanceof NestedDatasource && ((NestedDatasource) datasource).getMaster() == ds) {
if (datasource.getItem() != null && PersistenceHelper.isNew(datasource.getItem()))
((DatasourceImplementation) datasource).modified(datasource.getItem());
}
}
}
((DatasourceImplementation) ds).setModified(false);
Security security = AppBeans.get(Security.NAME);
if (!PersistenceHelper.isNew(item) && security.isEntityOpPermitted(ds.getMetaClass(), EntityOp.UPDATE)) {
readOnly = false;
LockInfo lockInfo = lockService.lock(getMetaClassForLocking(ds).getName(), item.getId().toString());
if (lockInfo == null) {
justLocked = true;
} else if (!(lockInfo instanceof LockNotSupported)) {
window.getWindowManager().showNotification(messages.getMainMessage("entityLocked.msg"), String.format(messages.getMainMessage("entityLocked.desc"), lockInfo.getUser().getLogin(), Datatypes.getNN(Date.class).format(lockInfo.getSince(), userSessionSource.getLocale())), Frame.NotificationType.HUMANIZED);
Action action = window.getAction(Window.Editor.WINDOW_COMMIT);
if (action != null)
action.setEnabled(false);
action = window.getAction(Window.Editor.WINDOW_COMMIT_AND_CLOSE);
if (action != null)
action.setEnabled(false);
readOnly = true;
}
}
}
Aggregations