/** * Validates visible and enabled UI components. <br> * Can be overridden in subclasses. * * @return validation errors */ protected ValidationErrors validateUiComponents() { ScreenValidation screenValidation = getBeanLocator().get(ScreenValidation.NAME); return screenValidation.validateUiComponents(getForm().getComponents()); }
/** * Releases pessimistic lock if the entity was locked. */ public void releaseLock() { if (justLocked) { Entity entity = getEditContainer().getItemOrNull(); if (entity != null) { getBeanLocator().get(LockService.class).unlock(getLockName(), entity.getId().toString()); } } }
/** * Returns the name of the pessimistic lock. */ protected String getLockName() { InstanceContainer<T> container = getEditContainer(); return getBeanLocator().get(ExtendedEntities.class) .getOriginalOrThisMetaClass(container.getEntityMetaClass()) .getName(); }
/** * Pessimistic lock before start of editing, if it is configured for the entity. */ protected boolean lockIfNeeded(Entity entity) { LockService lockService = getBeanLocator().get(LockService.class); LockInfo lockInfo = lockService.lock(getLockName(), entity.getId().toString()); if (lockInfo == null) { justLocked = true; } else if (!(lockInfo instanceof LockNotSupported)) { Messages messages = getBeanLocator().get(Messages.class); DatatypeFormatter datatypeFormatter = getBeanLocator().get(DatatypeFormatter.class); Notifications notifications = getScreenContext().getNotifications(); notifications.create(NotificationType.HUMANIZED) .withCaption(messages.getMainMessage("entityLocked.msg")) .withDescription(String.format(messages.getMainMessage("entityLocked.desc"), lockInfo.getUser().getLogin(), datatypeFormatter.formatDateTime(lockInfo.getSince()))) .show(); return false; } return true; }
/** * Validates the cross-field rules if passed validation errors are empty. * * @param errors errors found during components validation */ protected void validateAdditionalRules(ValidationErrors errors) { // all previous validations return no errors if (isCrossFieldValidate() && errors.isEmpty()) { ScreenValidation screenValidation = getBeanLocator().get(ScreenValidation.NAME); ValidationErrors validationErrors = screenValidation.validateCrossFieldRules(this, getEditContainer().getItem()); errors.addAll(validationErrors); } }
/** * Adds an EditAction that enables controls for editing. */ protected void initBrowseEditAction() { ListComponent<T> table = getTable(); EditAction editAction = (EditAction) table.getActionNN("edit"); editAction.withHandler(actionPerformedEvent -> { if (table.getSelected().size() == 1) { if (lockIfNeeded(table.getSingleSelected())) { refreshOptionsForLookupFields(); enableEditControls(false); } } }); editAction.addEnabledRule(() -> table.getSelected().size() == 1 && getBeanLocator().get(Security.class).isEntityOpPermitted(getEntityClass(), EntityOp.UPDATE)); }
/** * Method invoked when clicking on the Ok button after editing an existing or creating a new record. */ public void saveChanges() { if (!editing) return; ValidationErrors validationErrors = validateEditorForm(); if (!validationErrors.isEmpty()) { ScreenValidation screenValidation = getBeanLocator().get(ScreenValidation.class); screenValidation.showValidationErrors(this, validationErrors); return; } getScreenData().getDataContext().commit(); T editedItem = getEditContainer().getItem(); if (creating) { getBrowseContainer().getMutableItems().add(0, editedItem); } else { getBrowseContainer().replaceItem(editedItem); } getTable().setSelected(editedItem); releaseLock(); disableEditControls(); }
CreateAction createAction = (CreateAction) table.getActionNN("create"); createAction.withHandler(actionPerformedEvent -> { T entity = getBeanLocator().get(Metadata.class).create(getEntityClass()); T trackedEntity = getScreenData().getDataContext().merge(entity); DynamicAttributesGuiTools tools = getBeanLocator().get(DynamicAttributesGuiTools.NAME); String screenId = getScreenContext().getWindowInfo().getId();
/** * Method invoked when clicking the Cancel button, discards changes and disables controls for editing. */ public void discardChanges() { releaseLock(); getScreenData().getDataContext().evict(getEditContainer().getItem()); getEditContainer().setItem(null); T selectedItem = getBrowseContainer().getItemOrNull(); if (selectedItem != null) { View view = getEditContainer().getView(); boolean loadDynamicAttributes = getEditLoader().isLoadDynamicAttributes(); T reloadedItem = getBeanLocator().get(DataManager.class) .reload(selectedItem, view, null, loadDynamicAttributes); getBrowseContainer().setItem(reloadedItem); } disableEditControls(); }
/** * Adds a listener that reloads the selected record and sets it to editCt. */ protected void initBrowseItemChangeListener() { getBrowseContainer().addItemChangeListener(e -> { if (e.getItem() != null) { InstanceLoader<T> editLoader = getEditLoader(); DynamicAttributesGuiTools tools = getBeanLocator().get(DynamicAttributesGuiTools.NAME); String screenId = getScreenContext().getWindowInfo().getId(); if (tools.screenContainsDynamicAttributes(getEditContainer().getView(), screenId)) { editLoader.setLoadDynamicAttributes(true); } editLoader.setEntityId(e.getItem().getId()); editLoader.load(); } else { if (!editing) { getEditContainer().setItem(null); } } }); }