use of com.haulmont.cuba.gui.data.impl.DatasourceImplementation in project cuba by cuba-platform.
the class Param method createOptionsDataSource.
protected CollectionDatasource<Entity<Object>, Object> createOptionsDataSource(MetaClass metaClass) {
CollectionDatasource ds = DsBuilder.create(datasource.getDsContext()).setMetaClass(metaClass).setViewName(entityView).buildCollectionDatasource();
ds.setRefreshOnComponentValueChange(true);
((DatasourceImplementation) ds).initialized();
if (!StringUtils.isBlank(entityWhere)) {
QueryTransformer transformer = QueryTransformerFactory.createTransformer("select e from " + metaClass.getName() + " e");
transformer.addWhere(entityWhere);
String q = transformer.getResult();
ds.setQuery(q);
}
if (WindowParams.DISABLE_AUTO_REFRESH.getBool(datasource.getDsContext().getFrameContext())) {
if (ds instanceof CollectionDatasource.Suspendable)
((CollectionDatasource.Suspendable) ds).refreshIfNotSuspended();
else
ds.refresh();
}
return ds;
}
use of com.haulmont.cuba.gui.data.impl.DatasourceImplementation in project cuba by cuba-platform.
the class AbstractEditor method setItemInternal.
@SuppressWarnings("unchecked")
protected void setItemInternal(Entity item) {
Datasource ds = getDatasourceInternal();
DataSupplier dataservice = ds.getDataSupplier();
DatasourceImplementation parentDs = (DatasourceImplementation) ((DatasourceImplementation) ds).getParent();
DynamicAttributesGuiTools dynamicAttributesGuiTools = getBeanLocator().get(DynamicAttributesGuiTools.NAME);
if (dynamicAttributesGuiTools.screenContainsDynamicAttributes(ds.getView(), getFrame().getId())) {
ds.setLoadDynamicAttributes(true);
}
Class<? extends Entity> entityClass = item.getClass();
Object entityId = item.getId();
EntityStates entityStates = getBeanLocator().get(EntityStates.class);
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());
MetadataTools metadataTools = getBeanLocator().get(MetadataTools.NAME);
metadataTools.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 = getBeanLocator().get(Security.NAME);
if (!PersistenceHelper.isNew(item)) {
if (security.isEntityOpPermitted(ds.getMetaClass(), EntityOp.UPDATE)) {
readOnlyDueToLock = false;
LockService lockService = getBeanLocator().get(LockService.NAME);
LockInfo lockInfo = lockService.lock(getMetaClassForLocking(ds).getName(), item.getId().toString());
if (lockInfo == null) {
justLocked = true;
addAfterDetachListener(afterCloseEvent -> {
releaseLock();
});
} else if (!(lockInfo instanceof LockNotSupported)) {
UserSessionSource userSessionSource = getBeanLocator().get(UserSessionSource.NAME);
getFrame().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);
readOnlyDueToLock = true;
showEnableEditingBtn = false;
setReadOnly(true);
}
} else {
showEnableEditingBtn = false;
setReadOnly(true);
}
}
}
use of com.haulmont.cuba.gui.data.impl.DatasourceImplementation in project cuba by cuba-platform.
the class UserEditor method preCommit.
@Override
protected boolean preCommit() {
User user = getItem();
if (PersistenceHelper.isNew(user)) {
String password = passwField.getValue();
String passwordConfirmation = confirmPasswField.getValue();
if (passwField.isRequired() && (StringUtils.isBlank(password) || StringUtils.isBlank(passwordConfirmation))) {
showNotification(getMessage("emptyPassword"), NotificationType.WARNING);
return false;
} else {
if (Objects.equals(password, passwordConfirmation)) {
if (StringUtils.isNotEmpty(password)) {
ClientConfig passwordPolicyConfig = configuration.getConfig(ClientConfig.class);
if (passwordPolicyConfig.getPasswordPolicyEnabled()) {
String regExp = passwordPolicyConfig.getPasswordPolicyRegExp();
if (!password.matches(regExp)) {
showNotification(getMessage("simplePassword"), NotificationType.WARNING);
return false;
}
}
String passwordHash = passwordEncryption.getPasswordHash(user.getId(), password);
user.setPassword(passwordHash);
}
} else {
showNotification(getMessage("passwordsDoNotMatch"), NotificationType.WARNING);
return false;
}
}
}
boolean isDsModified = rolesDs.isModified();
Collection<UserRole> userRoles = new ArrayList<>(rolesDs.getItems());
for (UserRole userRole : userRoles) {
if (userRole.getRole() != null && userRole.getRole().isPredefined()) {
if (userRole.getRoleName() == null) {
userRole.setRoleName(userRole.getRole().getName());
}
userRole.setRole(null);
rolesDs.modifyItem(userRole);
}
}
for (Object itemToDelete : ((AbstractDatasource) rolesDs).getItemsToDelete()) {
if (itemToDelete instanceof UserRole && ((UserRole) itemToDelete).getRoleName() != null) {
((UserRole) itemToDelete).setRole(null);
}
}
((AbstractDatasource) rolesDs).setModified(isDsModified);
if (rolesDs.isModified()) {
@SuppressWarnings("unchecked") DatasourceImplementation<UserRole> rolesDsImpl = (DatasourceImplementation) rolesDs;
CommitContext ctx = new CommitContext(Collections.emptyList(), rolesDsImpl.getItemsToDelete());
dataSupplier.commit(ctx);
List<UserRole> modifiedRoles = new ArrayList<>(rolesDsImpl.getItemsToCreate());
modifiedRoles.addAll(rolesDsImpl.getItemsToUpdate());
rolesDsImpl.committed(Collections.emptySet());
for (UserRole userRole : modifiedRoles) {
rolesDsImpl.modified(userRole);
}
}
return true;
}
use of com.haulmont.cuba.gui.data.impl.DatasourceImplementation in project cuba by cuba-platform.
the class WebAbstractTable method getItemDatasource.
@SuppressWarnings("unchecked")
@Override
public Datasource getItemDatasource(Entity item) {
if (fieldDatasources == null) {
fieldDatasources = new WeakHashMap<>();
}
Object fieldDatasource = fieldDatasources.get(item);
if (fieldDatasource instanceof Datasource) {
return (Datasource) fieldDatasource;
}
EntityTableItems containerTableItems = (EntityTableItems) getItems();
Datasource datasource = DsBuilder.create().setAllowCommit(false).setMetaClass(containerTableItems.getEntityMetaClass()).setRefreshMode(CollectionDatasource.RefreshMode.NEVER).setViewName(View.LOCAL).buildDatasource();
((DatasourceImplementation) datasource).valid();
datasource.setItem(item);
fieldDatasources.put(item, datasource);
return datasource;
}
use of com.haulmont.cuba.gui.data.impl.DatasourceImplementation 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