use of com.enonic.xp.data.PropertyPath in project xp by enonic.
the class OccurrenceValidator method validateOccurrences.
private static void validateOccurrences(final FormItem formItem, final List<PropertySet> parentDataSets, final ValidationErrors.Builder validationErrorsBuilder) {
final Occurrences occurrences;
if (formItem instanceof Input) {
occurrences = ((Input) formItem).getOccurrences();
} else if (formItem instanceof FormItemSet) {
occurrences = ((FormItemSet) formItem).getOccurrences();
} else if (formItem instanceof FormOptionSet) {
occurrences = ((FormOptionSet) formItem).getOccurrences();
} else {
return;
}
for (final PropertySet parentDataSet : parentDataSets) {
final int entryCount = parentDataSet.countProperties(formItem.getName());
final PropertyPath parentPath = Optional.ofNullable(parentDataSet.getProperty()).map(Property::getPath).orElseGet(() -> PropertyPath.from(formItem.getPath().getParent().toString()));
final PropertyPath path = PropertyPath.from(parentPath, formItem.getName());
final int minOccurrences = occurrences.getMinimum();
if (occurrences.impliesRequired() && entryCount < minOccurrences) {
validationErrorsBuilder.add(ValidationError.dataError(ValidationErrorCode.from(ApplicationKey.SYSTEM, "cms.occurrencesInvalid"), path).i18n("system.cms.validation.minOccurrencesInvalid").args(formItem.getPath(), minOccurrences, entryCount).build());
}
final int maxOccurrences = occurrences.getMaximum();
if (maxOccurrences > 0 && entryCount > maxOccurrences) {
validationErrorsBuilder.add(ValidationError.dataError(ValidationErrorCode.from(ApplicationKey.SYSTEM, "cms.occurrencesInvalid"), path).i18n("system.cms.validation.maxOccurrencesInvalid").args(formItem.getPath(), occurrences.getMaximum(), entryCount).build());
}
}
}
use of com.enonic.xp.data.PropertyPath in project xp by enonic.
the class HtmlAreaNodeDataUpgrader method upgradeNodeData.
private void upgradeNodeData(final PropertySet nodeData, final PatternIndexConfigDocument indexConfigDocument) {
final List<Pattern> htmlAreaPatterns = indexConfigDocument.getPathIndexConfigs().stream().filter(this::hasHtmlStripperProcessor).map(PathIndexConfig::getPath).map(PropertyPath::toString).map(HtmlAreaNodeDataUpgrader::toPattern).collect(Collectors.toList());
final PropertyVisitor propertyVisitor = new PropertyVisitor() {
@Override
public void visit(final Property property) {
if (isHtmlAreaProperty(property)) {
upgradeHtmlAreaProperty(property, false);
} else if (isBackwardCompatibleHtmlAreaProperty(property)) {
upgradeHtmlAreaProperty(property, true);
}
}
private boolean isHtmlAreaProperty(Property property) {
return ValueTypes.STRING.equals(property.getType()) && htmlAreaPatterns.stream().anyMatch(htmlPattern -> htmlPattern.matcher(property.getPath().toString()).matches());
}
private boolean isBackwardCompatibleHtmlAreaProperty(Property property) {
return ValueTypes.STRING.equals(property.getType()) && BACKWARD_COMPATIBILITY_HTML_PROPERTY_PATH_PATTERNS.stream().anyMatch(htmlPattern -> htmlPattern.matcher(property.getPath().toString()).matches());
}
};
propertyVisitor.traverse(nodeData);
}
use of com.enonic.xp.data.PropertyPath in project xp by enonic.
the class FormDefaultValuesProcessorImpl method processFormItems.
private void processFormItems(final Iterable<FormItem> formItems, final PropertySet dataSet) {
StreamSupport.stream(formItems.spliterator(), false).forEach(formItem -> {
if (formItem.getType() == INPUT) {
Input input = formItem.toInput();
if (input.getDefaultValue() != null) {
try {
final Value defaultValue = InputTypes.BUILTIN.resolve(input.getInputType()).createDefaultValue(input);
final PropertyPath propertyPath = PropertyPath.from(input.getName());
if (defaultValue != null && dataSet.getProperty(propertyPath) == null) {
if (input.getOccurrences().getMinimum() > 0) {
for (int i = 0; i < input.getOccurrences().getMinimum(); i++) {
dataSet.setProperty(input.getName(), i, defaultValue);
}
} else {
dataSet.setProperty(input.getName(), defaultValue);
}
}
} catch (IllegalArgumentException ex) {
LOG.warn("Invalid default value for " + input.getInputType() + " input type with name '" + input.getName() + "': '" + input.getDefaultValue().getRootValue() + "'" + (ex.getMessage() == null ? "" : " - " + ex.getMessage()));
}
}
} else if (formItem.getType() == FORM_ITEM_SET) {
processFormItems(formItem.getName(), formItem.toFormItemSet().getFormItems(), dataSet, formItem.toFormItemSet().getOccurrences().getMinimum());
} else if (formItem.getType() == LAYOUT && formItem.toLayout() instanceof FieldSet) {
processFormItems((FieldSet) formItem.toLayout(), dataSet);
} else if (formItem.getType() == FORM_OPTION_SET_OPTION) {
FormOptionSetOption option = formItem.toFormOptionSetOption();
if (option.isDefaultOption()) {
dataSet.setProperty("_selected", ValueFactory.newString(formItem.getName()));
if (dataSet.getProperty(formItem.getName()) == null) {
Property property = dataSet.setProperty(formItem.getName(), ValueFactory.newPropertySet(new PropertySet()));
processFormItems(option.getFormItems(), property.getSet());
}
}
} else if (formItem.getType() == FORM_OPTION_SET) {
processFormItems(formItem.getName(), formItem.toFormOptionSet().getFormItems(), dataSet, formItem.toFormOptionSet().getOccurrences().getMinimum());
}
});
}
use of com.enonic.xp.data.PropertyPath in project xp by enonic.
the class ContentDataSerializer method mapValidationError.
private ValidationError mapValidationError(final PropertySet ve) {
final Object[] args = Optional.ofNullable(ve.getString("args")).map(argsJson -> {
try {
return OBJECT_MAPPER.readValue(argsJson, Object[].class);
} catch (JsonProcessingException e) {
throw new UncheckedIOException(e);
}
}).orElse(null);
final ValidationErrorCode errorCode = ValidationErrorCode.parse(ve.getString("errorCode"));
if (ve.hasProperty("propertyPath")) {
return ValidationError.dataError(errorCode, PropertyPath.from(ve.getString("propertyPath"))).message(ve.getString("message"), true).i18n(ve.getString("i18n")).args(args).build();
} else if (ve.hasProperty("attachment")) {
return ValidationError.attachmentError(errorCode, BinaryReference.from(ve.getString("attachment"))).message(ve.getString("message"), true).i18n(ve.getString("i18n")).args(args).build();
} else {
return ValidationError.generalError(errorCode).message(ve.getString("message"), true).i18n(ve.getString("i18n")).args(args).build();
}
}
Aggregations