Search in sources :

Example 1 with LocalizableMessageListBuilder

use of com.evolveum.midpoint.util.LocalizableMessageListBuilder in project midpoint by Evolveum.

the class ValuePolicyProcessor method validateValue.

public List<StringLimitationResult> validateValue(String newValue, ValuePolicyType pp, ObjectBasedValuePolicyOriginResolver<?> originResolver, List<LocalizableMessage> messages, String shortDesc, Task task, OperationResult parentResult) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException, CommunicationException, ConfigurationException, SecurityViolationException {
    // TODO: do we want to throw exception when no value policy defined??
    Validate.notNull(pp, "Value policy must not be null.");
    OperationResult result = parentResult.createSubresult(OPERATION_STRING_POLICY_VALIDATION);
    result.addArbitraryObjectAsParam("policyName", pp.getName());
    List<StringLimitationResult> limitations = new ArrayList<>();
    try {
        normalize(pp);
        if (newValue == null) {
            newValue = "";
        }
        LimitationsType lims = pp.getStringPolicy().getLimitations();
        CollectionUtils.addIgnoreNull(limitations, testLength(newValue, lims, result, messages));
        CollectionUtils.addIgnoreNull(limitations, testMinimalUniqueCharacters(newValue, lims, result, messages));
        CollectionUtils.addIgnoreNull(limitations, testProhibitedValues(newValue, pp.getProhibitedValues(), originResolver, shortDesc, task, result, messages));
        // TODO: this needs to be determined from ValuePolicyType archetype
        ExpressionProfile expressionProfile = MiscSchemaUtil.getExpressionProfile();
        limitations.addAll(testCheckExpression(newValue, lims, expressionProfile, originResolver, shortDesc, task, result, messages));
        if (!lims.getLimit().isEmpty()) {
            // check limitation
            HashSet<String> validChars;
            HashSet<String> allValidChars = new HashSet<>();
            List<String> characters = StringPolicyUtils.stringTokenizer(newValue);
            for (StringLimitType stringLimitationType : lims.getLimit()) {
                OperationResult limitResult = new OperationResult("Tested limitation: " + stringLimitationType.getDescription());
                validChars = getValidCharacters(stringLimitationType.getCharacterClass(), pp);
                int count = countValidCharacters(validChars, characters);
                allValidChars.addAll(validChars);
                StringLimitationResult limitation = null;
                limitation = testMinimalOccurrence(stringLimitationType, count, limitResult, messages, limitation);
                limitation = testMaximalOccurrence(stringLimitationType, count, limitResult, messages, limitation);
                limitation = testMustBeFirst(stringLimitationType, limitResult, messages, newValue, validChars, limitation);
                if (limitation != null) {
                    PolyStringType name = stringLimitationType.getName();
                    if (name == null) {
                        name = new PolyStringType(stringLimitationType.getDescription());
                        PolyStringTranslationType translation = new PolyStringTranslationType();
                        translation.setKey(stringLimitationType.getDescription());
                        name.setTranslation(translation);
                    }
                    PolyStringType help = new PolyStringType(getCharsetAsString(validChars));
                    limitation.setHelp(help);
                    limitation.setName(name);
                    limitations.add(limitation);
                }
                limitResult.computeStatus();
                result.addSubresult(limitResult);
            }
            CollectionUtils.addIgnoreNull(limitations, testInvalidCharacters(characters, allValidChars, result, messages));
        }
    } catch (Throwable t) {
        result.recordFatalError(t);
        throw t;
    } finally {
        result.computeStatusIfUnknown();
    }
    if (!result.isSuccess() && !messages.isEmpty()) {
        result.setUserFriendlyMessage(new LocalizableMessageListBuilder().messages(messages).separator(LocalizableMessageList.SPACE).buildOptimized());
    }
    return limitations;
}
Also used : PolyStringType(com.evolveum.prism.xml.ns._public.types_3.PolyStringType) StringLimitationResult(com.evolveum.midpoint.model.api.validator.StringLimitationResult) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) LimitationsType(com.evolveum.midpoint.xml.ns._public.common.common_3.LimitationsType) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile) LocalizableMessageListBuilder(com.evolveum.midpoint.util.LocalizableMessageListBuilder) StringLimitType(com.evolveum.midpoint.xml.ns._public.common.common_3.StringLimitType) PolyStringTranslationType(com.evolveum.prism.xml.ns._public.types_3.PolyStringTranslationType)

Aggregations

StringLimitationResult (com.evolveum.midpoint.model.api.validator.StringLimitationResult)1 ExpressionProfile (com.evolveum.midpoint.schema.expression.ExpressionProfile)1 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)1 LocalizableMessageListBuilder (com.evolveum.midpoint.util.LocalizableMessageListBuilder)1 LimitationsType (com.evolveum.midpoint.xml.ns._public.common.common_3.LimitationsType)1 StringLimitType (com.evolveum.midpoint.xml.ns._public.common.common_3.StringLimitType)1 PolyStringTranslationType (com.evolveum.prism.xml.ns._public.types_3.PolyStringTranslationType)1 PolyStringType (com.evolveum.prism.xml.ns._public.types_3.PolyStringType)1