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;
}
Aggregations