Search in sources :

Example 1 with SimpleValidationError

use of com.evolveum.midpoint.web.util.validation.SimpleValidationError in project midpoint by Evolveum.

the class PageAdminObjectDetails method saveOrPreviewPerformed.

public void saveOrPreviewPerformed(AjaxRequestTarget target, OperationResult result, boolean previewOnly) {
    boolean isAnythingChanged = processDeputyAssignments();
    ObjectWrapper<O> objectWrapper = getObjectWrapper();
    LOGGER.debug("Saving object {}", objectWrapper);
    // todo: improve, delta variable is quickfix for MID-1006
    // redirecting to user list page everytime user is created in repository
    // during user add in gui,
    // and we're not taking care about account/assignment create errors
    // (error message is still displayed)
    delta = null;
    Task task = createSimpleTask(OPERATION_SEND_TO_SUBMIT);
    ModelExecuteOptions options = getExecuteChangesOptions();
    LOGGER.debug("Using execute options {}.", new Object[] { options });
    try {
        reviveModels();
        delta = objectWrapper.getObjectDelta();
        if (objectWrapper.getOldDelta() != null) {
            delta = ObjectDelta.summarize(objectWrapper.getOldDelta(), delta);
        }
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("User delta computed from form:\n{}", new Object[] { delta.debugDump(3) });
        }
    } catch (Exception ex) {
        result.recordFatalError(getString("pageUser.message.cantCreateUser"), ex);
        LoggingUtils.logUnexpectedException(LOGGER, "Create user failed", ex);
        showResult(result);
        return;
    }
    switch(objectWrapper.getStatus()) {
        case ADDING:
            try {
                PrismObject<O> objectToAdd = delta.getObjectToAdd();
                WebComponentUtil.encryptCredentials(objectToAdd, true, getMidpointApplication());
                prepareObjectForAdd(objectToAdd);
                getPrismContext().adopt(objectToAdd, getCompileTimeClass());
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("Delta before add user:\n{}", new Object[] { delta.debugDump(3) });
                }
                if (!delta.isEmpty()) {
                    delta.revive(getPrismContext());
                    final Collection<ObjectDelta<? extends ObjectType>> deltas = WebComponentUtil.createDeltaCollection(delta);
                    final Collection<SimpleValidationError> validationErrors = performCustomValidation(objectToAdd, deltas);
                    if (checkValidationErrors(target, validationErrors)) {
                        return;
                    }
                    progressReporter.executeChanges(deltas, previewOnly, options, task, result, target);
                } else {
                    result.recordSuccess();
                }
            } catch (Exception ex) {
                result.recordFatalError(getString("pageFocus.message.cantCreateFocus"), ex);
                LoggingUtils.logUnexpectedException(LOGGER, "Create user failed", ex);
                showResult(result);
            }
            break;
        case MODIFYING:
            try {
                WebComponentUtil.encryptCredentials(delta, true, getMidpointApplication());
                prepareObjectDeltaForModify(delta);
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("Delta before modify user:\n{}", new Object[] { delta.debugDump(3) });
                }
                Collection<ObjectDelta<? extends ObjectType>> deltas = new ArrayList<>();
                if (!delta.isEmpty()) {
                    delta.revive(getPrismContext());
                    deltas.add(delta);
                }
                List<ObjectDelta<? extends ObjectType>> additionalDeltas = getAdditionalModifyDeltas(result);
                if (additionalDeltas != null) {
                    for (ObjectDelta additionalDelta : additionalDeltas) {
                        if (!additionalDelta.isEmpty()) {
                            additionalDelta.revive(getPrismContext());
                            deltas.add(additionalDelta);
                        }
                    }
                }
                if (delta.isEmpty() && ModelExecuteOptions.isReconcile(options)) {
                    ObjectDelta emptyDelta = ObjectDelta.createEmptyModifyDelta(getCompileTimeClass(), objectWrapper.getObject().getOid(), getPrismContext());
                    deltas.add(emptyDelta);
                    Collection<SimpleValidationError> validationErrors = performCustomValidation(null, deltas);
                    if (checkValidationErrors(target, validationErrors)) {
                        return;
                    }
                    progressReporter.executeChanges(deltas, previewOnly, options, task, result, target);
                } else if (!deltas.isEmpty()) {
                    Collection<SimpleValidationError> validationErrors = performCustomValidation(null, deltas);
                    if (checkValidationErrors(target, validationErrors)) {
                        return;
                    }
                    progressReporter.executeChanges(deltas, previewOnly, options, task, result, target);
                } else {
                    // from previous attempts (useful only if we would call finishProcessing at the end, but that's not the case now)
                    progressReporter.clearProgressPanel();
                    if (!previewOnly) {
                        if (!isAnythingChanged) {
                            result.recordWarning(getString("PageAdminObjectDetails.noChangesSave"));
                            showResult(result);
                        }
                        redirectBack();
                    } else {
                        warn(getString("PageAdminObjectDetails.noChangesPreview"));
                        target.add(getFeedbackPanel());
                    }
                }
            } catch (Exception ex) {
                if (!executeForceDelete(objectWrapper, task, options, result)) {
                    result.recordFatalError(getString("pageUser.message.cantUpdateUser"), ex);
                    LoggingUtils.logUnexpectedException(LOGGER, getString("pageUser.message.cantUpdateUser"), ex);
                } else {
                    result.recomputeStatus();
                }
                showResult(result);
            }
            break;
        // support for add/delete containers (e.g. delete credentials)
        default:
            error(getString("pageAdminFocus.message.unsupportedState", objectWrapper.getStatus()));
    }
    //		result.recomputeStatus();
    //
    //		if (!result.isInProgress()) {
    //			LOGGER.trace("Result NOT in progress, calling finishProcessing");
    //			finishProcessing(target, result, false);
    //		}
    LOGGER.trace("returning from saveOrPreviewPerformed");
}
Also used : Task(com.evolveum.midpoint.task.api.Task) ArrayList(java.util.ArrayList) ModelExecuteOptions(com.evolveum.midpoint.model.api.ModelExecuteOptions) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) AuthorizationException(com.evolveum.midpoint.util.exception.AuthorizationException) RestartResponseException(org.apache.wicket.RestartResponseException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) SystemException(com.evolveum.midpoint.util.exception.SystemException) ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) Collection(java.util.Collection) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) SimpleValidationError(com.evolveum.midpoint.web.util.validation.SimpleValidationError)

Example 2 with SimpleValidationError

use of com.evolveum.midpoint.web.util.validation.SimpleValidationError in project midpoint by Evolveum.

the class PageAdminObjectDetails method saveOrPreviewPerformed.

public void saveOrPreviewPerformed(AjaxRequestTarget target, OperationResult result, boolean previewOnly, Task task) {
    boolean delegationChangesExist = processDeputyAssignments(previewOnly);
    PrismObjectWrapper<O> objectWrapper = getObjectWrapper();
    LOGGER.debug("Saving object {}", objectWrapper);
    // todo: improve, delta variable is quickfix for MID-1006
    // redirecting to user list page everytime user is created in repository
    // during user add in gui,
    // and we're not taking care about account/assignment create errors
    // (error message is still displayed)
    delta = null;
    if (task == null) {
        task = createSimpleTask(OPERATION_SEND_TO_SUBMIT);
    }
    ModelExecuteOptions options = getOptions(previewOnly);
    LOGGER.debug("Using execute options {}.", options);
    try {
        reviveModels();
        delta = objectWrapper.getObjectDelta();
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("User delta computed from form:\n{}", delta.debugDump(3));
        }
    } catch (Exception ex) {
        result.recordFatalError(getString("pageAdminObjectDetails.message.cantCreateObject"), ex);
        LoggingUtils.logUnexpectedException(LOGGER, "Create Object failed", ex);
        showResult(result);
        target.add(getFeedbackPanel());
        return;
    }
    switch(objectWrapper.getStatus()) {
        case ADDED:
            try {
                PrismObject<O> objectToAdd = delta.getObjectToAdd();
                WebComponentUtil.encryptCredentials(objectToAdd, true, getMidpointApplication());
                prepareObjectForAdd(objectToAdd);
                getPrismContext().adopt(objectToAdd, getCompileTimeClass());
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("Delta before add user:\n{}", delta.debugDump(3));
                }
                if (!delta.isEmpty()) {
                    delta.revive(getPrismContext());
                    final Collection<ObjectDelta<? extends ObjectType>> deltas = MiscUtil.createCollection(delta);
                    final Collection<SimpleValidationError> validationErrors = performCustomValidation(objectToAdd, deltas);
                    if (checkValidationErrors(target, validationErrors)) {
                        return;
                    }
                    if (isSaveInBackground() && !previewOnly) {
                        progressPanel.executeChangesInBackground(deltas, previewOnly, options, task, result, target);
                    } else {
                        progressPanel.executeChanges(deltas, previewOnly, options, task, result, target);
                    }
                } else {
                    result.recordSuccess();
                }
            } catch (Exception ex) {
                result.recordFatalError(getString("pageFocus.message.cantCreateFocus"), ex);
                LoggingUtils.logUnexpectedException(LOGGER, "Create user failed", ex);
                showResult(result);
            }
            break;
        case NOT_CHANGED:
            try {
                WebComponentUtil.encryptCredentials(delta, true, getMidpointApplication());
                // preparing of deltas for projections (ADD, DELETE, UNLINK)
                prepareObjectDeltaForModify(delta);
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("Delta before modify user:\n{}", delta.debugDump(3));
                }
                Collection<ObjectDelta<? extends ObjectType>> deltas = new ArrayList<>();
                if (!delta.isEmpty()) {
                    delta.revive(getPrismContext());
                    deltas.add(delta);
                }
                List<ObjectDelta<? extends ObjectType>> additionalDeltas = getAdditionalModifyDeltas(result);
                if (additionalDeltas != null) {
                    for (ObjectDelta additionalDelta : additionalDeltas) {
                        if (!additionalDelta.isEmpty()) {
                            additionalDelta.revive(getPrismContext());
                            deltas.add(additionalDelta);
                        }
                    }
                }
                if (delta.isEmpty() && ModelExecuteOptions.isReconcile(options)) {
                    ObjectDelta emptyDelta = getPrismContext().deltaFactory().object().createEmptyModifyDelta(getCompileTimeClass(), objectWrapper.getObject().getOid());
                    deltas.add(emptyDelta);
                    Collection<SimpleValidationError> validationErrors = performCustomValidation(null, deltas);
                    if (checkValidationErrors(target, validationErrors)) {
                        return;
                    }
                    if (isSaveInBackground() && !previewOnly) {
                        progressPanel.executeChangesInBackground(deltas, previewOnly, options, task, result, target);
                    } else {
                        progressPanel.executeChanges(deltas, previewOnly, options, task, result, target);
                    }
                } else if (!deltas.isEmpty()) {
                    Collection<SimpleValidationError> validationErrors = performCustomValidation(null, deltas);
                    if (checkValidationErrors(target, validationErrors)) {
                        return;
                    }
                    if (isSaveInBackground() && !previewOnly) {
                        progressPanel.executeChangesInBackground(deltas, previewOnly, options, task, result, target);
                    } else {
                        progressPanel.executeChanges(deltas, previewOnly, options, task, result, target);
                    }
                } else if (previewOnly && delta.isEmpty() && delegationChangesExist) {
                    if (isSaveInBackground() && !previewOnly) {
                        progressPanel.executeChangesInBackground(deltas, previewOnly, options, task, result, target);
                    } else {
                        progressPanel.executeChanges(deltas, previewOnly, options, task, result, target);
                    }
                } else {
                    // from previous attempts (useful only if we would call finishProcessing at the end, but that's not the case now)
                    progressPanel.clearProgressPanel();
                    if (!previewOnly) {
                        if (!delegationChangesExist) {
                            result.recordWarning(getString("PageAdminObjectDetails.noChangesSave"));
                            showResult(result);
                        }
                        redirectBack();
                    } else {
                        if (!delegationChangesExist) {
                            warn(getString("PageAdminObjectDetails.noChangesPreview"));
                            target.add(getFeedbackPanel());
                        }
                    }
                }
            } catch (Exception ex) {
                if (!executeForceDelete(objectWrapper, task, options, result)) {
                    result.recordFatalError(getString("pageUser.message.cantUpdateUser"), ex);
                    LoggingUtils.logUnexpectedException(LOGGER, getString("pageUser.message.cantUpdateUser"), ex);
                } else {
                    result.recomputeStatus();
                }
                showResult(result);
            }
            break;
        // support for add/delete containers (e.g. delete credentials)
        default:
            error(getString("pageAdminFocus.message.unsupportedState", objectWrapper.getStatus()));
    }
    LOGGER.trace("returning from saveOrPreviewPerformed");
}
Also used : ModelExecuteOptions(com.evolveum.midpoint.model.api.ModelExecuteOptions) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) AuthorizationException(com.evolveum.midpoint.util.exception.AuthorizationException) RestartResponseException(org.apache.wicket.RestartResponseException) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) SimpleValidationError(com.evolveum.midpoint.web.util.validation.SimpleValidationError)

Example 3 with SimpleValidationError

use of com.evolveum.midpoint.web.util.validation.SimpleValidationError in project midpoint by Evolveum.

the class PageAdminObjectDetails method performCustomValidation.

protected Collection<SimpleValidationError> performCustomValidation(PrismObject<O> object, Collection<ObjectDelta<? extends ObjectType>> deltas) throws SchemaException {
    Collection<SimpleValidationError> errors = null;
    if (object == null) {
        if (getObjectWrapper() != null && getObjectWrapper().getObjectOld() != null) {
            // otherwise original object could get corrupted e.g. by applying the delta below
            object = getObjectWrapper().getObjectOld().clone();
            for (ObjectDelta delta : deltas) {
                // because among deltas there can be also ShadowType deltas
                if (UserType.class.isAssignableFrom(delta.getObjectTypeClass())) {
                    delta.applyTo(object);
                }
            }
        }
    } else {
        object = object.clone();
    }
    performAdditionalValidation(object, deltas, errors);
    for (MidpointFormValidator validator : getFormValidatorRegistry().getValidators()) {
        if (errors == null) {
            errors = validator.validateObject(object, deltas);
        } else {
            errors.addAll(validator.validateObject(object, deltas));
        }
    }
    return errors;
}
Also used : SimpleValidationError(com.evolveum.midpoint.web.util.validation.SimpleValidationError) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) MidpointFormValidator(com.evolveum.midpoint.web.util.validation.MidpointFormValidator)

Example 4 with SimpleValidationError

use of com.evolveum.midpoint.web.util.validation.SimpleValidationError in project midpoint by Evolveum.

the class AbstractPageObjectDetails method checkValidationErrors.

private void checkValidationErrors(AjaxRequestTarget target, Collection<SimpleValidationError> validationErrors) {
    if (validationErrors != null && !validationErrors.isEmpty()) {
        for (SimpleValidationError error : validationErrors) {
            LOGGER.error("Validation error, attribute: '" + error.printAttribute() + "', message: '" + error.getMessage() + "'.");
            error("Validation error, attribute: '" + error.printAttribute() + "', message: '" + error.getMessage() + "'.");
        }
        target.add(getFeedbackPanel());
        throw new IllegalStateException("Validation errors found");
    }
}
Also used : SimpleValidationError(com.evolveum.midpoint.web.util.validation.SimpleValidationError)

Example 5 with SimpleValidationError

use of com.evolveum.midpoint.web.util.validation.SimpleValidationError in project midpoint by Evolveum.

the class InducedEntitlementsValidator method validateInducement.

private Collection<SimpleValidationError> validateInducement(AssignmentType assignment) {
    List<SimpleValidationError> errors = new ArrayList<>();
    // TODO impelemnt findContainer(ItemPath)
    com.evolveum.midpoint.prism.Item<PrismContainerValue<ResourceObjectAssociationType>, PrismContainerDefinition<ResourceObjectAssociationType>> association = assignment.asPrismContainerValue().findItem(ItemPath.create(AssignmentType.F_CONSTRUCTION, ConstructionType.F_ASSOCIATION));
    if (association != null && !association.getValues().isEmpty()) {
        for (PrismContainerValue<ResourceObjectAssociationType> associationValue : association.getValues()) {
            PrismContainer<MappingType> outbound = associationValue.findContainer(ResourceObjectAssociationType.F_OUTBOUND);
            if (outbound == null || outbound.getValues().isEmpty()) {
                SimpleValidationError error = new SimpleValidationError();
                error.setMessage(PageBase.createStringResourceStatic(null, "InducedEntitlementsPanel.validator.message").getString());
                ItemPathType path = new ItemPathType();
                path.setItemPath(ItemPath.create(AbstractRoleType.F_INDUCEMENT, AssignmentType.F_CONSTRUCTION, ConstructionType.F_ASSOCIATION, ResourceObjectAssociationType.F_OUTBOUND));
                error.setAttribute(path);
                errors.add(error);
            }
        }
    }
    return errors;
}
Also used : ItemPathType(com.evolveum.prism.xml.ns._public.types_3.ItemPathType) ArrayList(java.util.ArrayList) SimpleValidationError(com.evolveum.midpoint.web.util.validation.SimpleValidationError) com.evolveum.midpoint.prism(com.evolveum.midpoint.prism)

Aggregations

SimpleValidationError (com.evolveum.midpoint.web.util.validation.SimpleValidationError)6 ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)3 ModelExecuteOptions (com.evolveum.midpoint.model.api.ModelExecuteOptions)2 AuthorizationException (com.evolveum.midpoint.util.exception.AuthorizationException)2 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)2 ArrayList (java.util.ArrayList)2 RestartResponseException (org.apache.wicket.RestartResponseException)2 com.evolveum.midpoint.prism (com.evolveum.midpoint.prism)1 Task (com.evolveum.midpoint.task.api.Task)1 ConfigurationException (com.evolveum.midpoint.util.exception.ConfigurationException)1 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)1 SystemException (com.evolveum.midpoint.util.exception.SystemException)1 MidpointFormValidator (com.evolveum.midpoint.web.util.validation.MidpointFormValidator)1 ObjectType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType)1 ItemPathType (com.evolveum.prism.xml.ns._public.types_3.ItemPathType)1 Collection (java.util.Collection)1