Search in sources :

Example 1 with CommitException

use of com.vaadin.v7.data.fieldgroup.FieldGroup.CommitException in project SORMAS-Project by hzi-braunschweig.

the class CommitDiscardWrapperComponent method doCommit.

private void doCommit() throws InvalidValueException, SourceException, CommitRuntimeException {
    if (fieldGroups != null) {
        if (fieldGroups.size() > 1) {
            List<InvalidValueException> invalidValueExceptions = fieldGroups.stream().filter(fieldGroup -> !fieldGroup.isValid()).map(fieldGroup -> {
                try {
                    // all invalid fieldGroups are committed to fetch the CommitExceptions
                    fieldGroup.commit();
                } catch (CommitException e) {
                    return e;
                }
                // when the fieldGroup did not throw a CommitException, it is invalid and committed
                throw new IllegalStateException();
            }).map(e -> {
                // keep invalid value exceptions, throw the rest
                Throwable c = e.getCause();
                if (c instanceof InvalidValueException) {
                    return (InvalidValueException) c;
                } else if (c instanceof SourceException) {
                    throw (SourceException) c;
                } else {
                    throw new CommitRuntimeException(e);
                }
            }).collect(Collectors.toList());
            if (invalidValueExceptions.isEmpty()) {
            // NOOP
            } else if (invalidValueExceptions.size() == 1) {
                throw invalidValueExceptions.get(0);
            } else {
                throw new InvalidValueException(null, invalidValueExceptions.stream().map(InvalidValueException::getCauses).flatMap(Arrays::stream).toArray(InvalidValueException[]::new));
            }
        }
        try {
            for (FieldGroup fieldGroup : fieldGroups) {
                fieldGroup.commit();
            }
        } catch (CommitException e) {
            Throwable c = e.getCause();
            if (c instanceof InvalidValueException) {
                throw (InvalidValueException) c;
            } else if (c instanceof SourceException) {
                throw (SourceException) c;
            } else {
                throw new CommitRuntimeException(e);
            }
        }
    } else if (wrappedComponent instanceof Buffered) {
        ((Buffered) wrappedComponent).commit();
    } else {
    // NOOP
    }
    dirty = false;
    onCommit();
    commited = true;
    onDone();
}
Also used : InvalidValueException(com.vaadin.v7.data.Validator.InvalidValueException) Panel(com.vaadin.ui.Panel) EventDataForm(de.symeda.sormas.ui.events.EventDataForm) Arrays(java.util.Arrays) ClickListener(com.vaadin.ui.Button.ClickListener) I18nProperties(de.symeda.sormas.api.i18n.I18nProperties) VerticalLayout(com.vaadin.ui.VerticalLayout) Alignment(com.vaadin.ui.Alignment) Notifier(com.vaadin.event.Action.Notifier) ArrayUtils(org.apache.commons.lang3.ArrayUtils) AccessibleTextField(de.symeda.sormas.ui.location.AccessibleTextField) PersonDto(de.symeda.sormas.api.person.PersonDto) ArrayList(java.util.ArrayList) Buffered(com.vaadin.v7.data.Buffered) DeletionDetails(de.symeda.sormas.api.common.DeletionDetails) Notification(com.vaadin.ui.Notification) RichTextArea(com.vaadin.v7.ui.RichTextArea) Page(com.vaadin.server.Page) KeyCode(com.vaadin.event.ShortcutAction.KeyCode) AbstractLegacyComponent(com.vaadin.v7.ui.AbstractLegacyComponent) CannotProceedException(javax.naming.CannotProceedException) ValoTheme(com.vaadin.ui.themes.ValoTheme) LocationDto(de.symeda.sormas.api.location.LocationDto) ClickEvent(com.vaadin.ui.Button.ClickEvent) Collection(java.util.Collection) Field(com.vaadin.v7.ui.Field) EventDto(de.symeda.sormas.api.event.EventDto) InvalidValueException(com.vaadin.v7.data.Validator.InvalidValueException) Collectors(java.util.stream.Collectors) Captions(de.symeda.sormas.api.i18n.Captions) FieldGroup(com.vaadin.v7.data.fieldgroup.FieldGroup) List(java.util.List) Button(com.vaadin.ui.Button) Stream(java.util.stream.Stream) Type(com.vaadin.ui.Notification.Type) PersonEditForm(de.symeda.sormas.ui.person.PersonEditForm) HorizontalLayout(com.vaadin.ui.HorizontalLayout) LocationEditForm(de.symeda.sormas.ui.location.LocationEditForm) TextArea(com.vaadin.v7.ui.TextArea) CommitException(com.vaadin.v7.data.fieldgroup.FieldGroup.CommitException) Descriptions(de.symeda.sormas.api.i18n.Descriptions) Objects.nonNull(java.util.Objects.nonNull) Strings(de.symeda.sormas.api.i18n.Strings) Component(com.vaadin.ui.Component) CommitException(com.vaadin.v7.data.fieldgroup.FieldGroup.CommitException) FieldGroup(com.vaadin.v7.data.fieldgroup.FieldGroup) Buffered(com.vaadin.v7.data.Buffered)

Example 2 with CommitException

use of com.vaadin.v7.data.fieldgroup.FieldGroup.CommitException in project opencms-core by alkacon.

the class CmsEditModuleForm method updateModule.

/**
 * Writes the form data back to the module.<p>
 */
public void updateModule() {
    try {
        m_fieldGroup.commit();
        // validate 'dynamic' tabs here
        TreeMap<String, String> params = Maps.newTreeMap();
        for (I_CmsEditableGroupRow row : m_parameterGroup.getRows()) {
            TextField paramField = (TextField) (row.getComponent());
            String paramStr = paramField.getValue();
            int eqPos = paramStr.indexOf("=");
            if (eqPos >= 0) {
                String key = paramStr.substring(0, eqPos);
                key = key.trim();
                String value = paramStr.substring(eqPos + 1);
                value = value.trim();
                if (!CmsStringUtil.isEmpty(key)) {
                    params.put(key, value);
                }
            }
        }
        m_module.setParameters(params);
        List<CmsExportPoint> exportPoints = Lists.newArrayList();
        for (I_CmsEditableGroupRow row : m_exportPointGroup.getRows()) {
            CmsExportPointWidget widget = (CmsExportPointWidget) (row.getComponent());
            String source = widget.getUri().trim();
            String target = widget.getDestination().trim();
            if (CmsStringUtil.isEmpty(source) || CmsStringUtil.isEmpty(target)) {
                continue;
            }
            CmsExportPoint point = new CmsExportPoint(source, target);
            exportPoints.add(point);
        }
        m_module.setExportPoints(exportPoints);
        List<CmsModuleDependency> dependencies = Lists.newArrayList();
        for (CmsModuleDependencyWidget widget : getFormRowChildren(m_dependencies, CmsModuleDependencyWidget.class)) {
            String moduleName = widget.getModuleName();
            String moduleVersion = widget.getModuleVersion();
            try {
                CmsModuleDependency dep = new CmsModuleDependency(moduleName, new CmsModuleVersion(moduleVersion));
                dependencies.add(dep);
            } catch (Exception e) {
                LOG.debug(e.getLocalizedMessage(), e);
            }
        }
        m_module.setDependencies(dependencies);
        List<String> moduleResources = Lists.newArrayList();
        for (I_CmsEditableGroupRow row : m_moduleResourcesGroup.getRows()) {
            CmsModuleResourceSelectField field = (CmsModuleResourceSelectField) (row.getComponent());
            String moduleResource = field.getValue().trim();
            if (!moduleResource.isEmpty()) {
                moduleResources.add(moduleResource);
            }
        }
        m_module.setResources(moduleResources);
        List<String> excludedResources = Lists.newArrayList();
        for (I_CmsEditableGroupRow row : m_excludedResourcesGroup.getRows()) {
            CmsModuleResourceSelectField field = (CmsModuleResourceSelectField) (row.getComponent());
            String moduleResource = field.getValue().trim();
            if (!moduleResource.isEmpty()) {
                excludedResources.add(moduleResource);
            }
        }
        m_module.setExcludeResources(excludedResources);
        if (!m_oldModuleInstance.isAutoIncrement() && m_module.isAutoIncrement()) {
            m_module.setCheckpointTime(System.currentTimeMillis());
        }
        CmsObject cms = A_CmsUI.getCmsObject();
        if (m_new) {
            createModuleFolders(cms, m_module);
            OpenCms.getModuleManager().addModule(cms, m_module);
        } else {
            OpenCms.getModuleManager().updateModule(cms, m_module);
        }
        CmsVaadinUtils.getWindow(this).close();
        m_updateCallback.run();
    } catch (CommitException e) {
        if (e.getCause() instanceof FieldGroup.FieldGroupInvalidValueException) {
            int minTabIdx = 999;
            for (Field<?> field : e.getInvalidFields().keySet()) {
                int tabIdx = getTabIndex(field);
                if (tabIdx != -1) {
                    minTabIdx = Math.min(tabIdx, minTabIdx);
                }
            }
            m_tabs.setSelectedTab(minTabIdx);
        } else {
            CmsErrorDialog.showErrorDialog(e);
        }
        return;
    } catch (Exception e) {
        CmsErrorDialog.showErrorDialog(e);
    }
}
Also used : CommitException(com.vaadin.v7.data.fieldgroup.FieldGroup.CommitException) BeanFieldGroup(com.vaadin.v7.data.fieldgroup.BeanFieldGroup) FieldGroup(com.vaadin.v7.data.fieldgroup.FieldGroup) CmsExportPoint(org.opencms.db.CmsExportPoint) CmsExportPoint(org.opencms.db.CmsExportPoint) CmsException(org.opencms.main.CmsException) CmsLockException(org.opencms.lock.CmsLockException) CommitException(com.vaadin.v7.data.fieldgroup.FieldGroup.CommitException) AbstractField(com.vaadin.v7.ui.AbstractField) CmsComponentField(org.opencms.ui.util.CmsComponentField) Field(com.vaadin.v7.ui.Field) TextField(com.vaadin.v7.ui.TextField) CmsObject(org.opencms.file.CmsObject) CmsModuleVersion(org.opencms.module.CmsModuleVersion) CmsModuleDependency(org.opencms.module.CmsModuleDependency) TextField(com.vaadin.v7.ui.TextField) I_CmsEditableGroupRow(org.opencms.ui.components.editablegroup.I_CmsEditableGroupRow)

Example 3 with CommitException

use of com.vaadin.v7.data.fieldgroup.FieldGroup.CommitException in project opencms-core by alkacon.

the class CmsUserDataFormLayout method submit.

/**
 * Store fields to given user.<p>
 *
 * @param user User to write information to
 * @param cms CmsObject
 * @param afterWrite runnable which gets called after writing user
 * @param force force write even if data are not valid
 */
public void submit(CmsUser user, CmsObject cms, Runnable afterWrite, boolean force) {
    try {
        if (isValid() | force) {
            if (force) {
                removeValidators();
            }
            m_binder.commit();
            PropertyUtilsBean propUtils = new PropertyUtilsBean();
            for (CmsAccountInfo info : OpenCms.getWorkplaceManager().getAccountInfos()) {
                boolean editable = (m_editLevel == EditLevel.all) || ((m_editLevel == EditLevel.configured) && info.isEditable());
                if (editable) {
                    if (info.isAdditionalInfo()) {
                        user.setAdditionalInfo(info.getAddInfoKey(), m_infos.getItemProperty(info).getValue());
                    } else {
                        if (!CmsStringUtil.isEmptyOrWhitespaceOnly((String) m_infos.getItemProperty(info).getValue())) {
                            propUtils.setProperty(user, info.getField().name(), m_infos.getItemProperty(info).getValue());
                        }
                    }
                }
            }
            cms.writeUser(user);
            afterWrite.run();
        }
    } catch (CmsException | CommitException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        LOG.error("Unable to commit changes to user..", e);
    }
}
Also used : CommitException(com.vaadin.v7.data.fieldgroup.FieldGroup.CommitException) PropertyUtilsBean(org.apache.commons.beanutils.PropertyUtilsBean) CmsAccountInfo(org.opencms.workplace.CmsAccountInfo) CmsException(org.opencms.main.CmsException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

CommitException (com.vaadin.v7.data.fieldgroup.FieldGroup.CommitException)3 FieldGroup (com.vaadin.v7.data.fieldgroup.FieldGroup)2 Field (com.vaadin.v7.ui.Field)2 CmsException (org.opencms.main.CmsException)2 Notifier (com.vaadin.event.Action.Notifier)1 KeyCode (com.vaadin.event.ShortcutAction.KeyCode)1 Page (com.vaadin.server.Page)1 Alignment (com.vaadin.ui.Alignment)1 Button (com.vaadin.ui.Button)1 ClickEvent (com.vaadin.ui.Button.ClickEvent)1 ClickListener (com.vaadin.ui.Button.ClickListener)1 Component (com.vaadin.ui.Component)1 HorizontalLayout (com.vaadin.ui.HorizontalLayout)1 Notification (com.vaadin.ui.Notification)1 Type (com.vaadin.ui.Notification.Type)1 Panel (com.vaadin.ui.Panel)1 VerticalLayout (com.vaadin.ui.VerticalLayout)1 ValoTheme (com.vaadin.ui.themes.ValoTheme)1 Buffered (com.vaadin.v7.data.Buffered)1 InvalidValueException (com.vaadin.v7.data.Validator.InvalidValueException)1