Search in sources :

Example 1 with PropertyPath

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());
        }
    }
}
Also used : FormOptionSet(com.enonic.xp.form.FormOptionSet) Input(com.enonic.xp.form.Input) PropertyPath(com.enonic.xp.data.PropertyPath) Occurrences(com.enonic.xp.form.Occurrences) PropertySet(com.enonic.xp.data.PropertySet) FormItemSet(com.enonic.xp.form.FormItemSet)

Example 2 with PropertyPath

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);
}
Also used : Property(com.enonic.xp.data.Property) ContentConstants(com.enonic.xp.content.ContentConstants) IndexValueProcessor(com.enonic.xp.index.IndexValueProcessor) Logger(org.slf4j.Logger) PropertyVisitor(com.enonic.xp.data.PropertyVisitor) PropertySet(com.enonic.xp.data.PropertySet) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) ValueFactory(com.enonic.xp.data.ValueFactory) PathIndexConfig(com.enonic.xp.index.PathIndexConfig) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) DumpUpgradeStepResult(com.enonic.xp.dump.DumpUpgradeStepResult) PatternIndexConfigDocument(com.enonic.xp.index.PatternIndexConfigDocument) List(java.util.List) Matcher(java.util.regex.Matcher) Stream(java.util.stream.Stream) Reference(com.enonic.xp.util.Reference) NodeVersion(com.enonic.xp.node.NodeVersion) ValueTypes(com.enonic.xp.data.ValueTypes) PropertyPath(com.enonic.xp.data.PropertyPath) Pattern(java.util.regex.Pattern) PropertyTree(com.enonic.xp.data.PropertyTree) Pattern(java.util.regex.Pattern) PropertyPath(com.enonic.xp.data.PropertyPath) Property(com.enonic.xp.data.Property) PropertyVisitor(com.enonic.xp.data.PropertyVisitor)

Example 3 with PropertyPath

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());
        }
    });
}
Also used : Input(com.enonic.xp.form.Input) FieldSet(com.enonic.xp.form.FieldSet) Value(com.enonic.xp.data.Value) PropertyPath(com.enonic.xp.data.PropertyPath) PropertySet(com.enonic.xp.data.PropertySet) FormOptionSetOption(com.enonic.xp.form.FormOptionSetOption) Property(com.enonic.xp.data.Property)

Example 4 with PropertyPath

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();
    }
}
Also used : OWNER(com.enonic.xp.content.ContentPropertyNames.OWNER) WorkflowInfo(com.enonic.xp.content.WorkflowInfo) CREATOR(com.enonic.xp.content.ContentPropertyNames.CREATOR) AttachmentSerializer(com.enonic.xp.attachment.AttachmentSerializer) Page(com.enonic.xp.page.Page) ValidationErrors(com.enonic.xp.content.ValidationErrors) ContentName(com.enonic.xp.content.ContentName) ContentId(com.enonic.xp.content.ContentId) INHERIT(com.enonic.xp.content.ContentPropertyNames.INHERIT) DataValidationError(com.enonic.xp.content.DataValidationError) Locale(java.util.Locale) Map(java.util.Map) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) ValidationError(com.enonic.xp.content.ValidationError) WORKFLOW_INFO_CHECKS(com.enonic.xp.content.ContentPropertyNames.WORKFLOW_INFO_CHECKS) Thumbnail(com.enonic.xp.icon.Thumbnail) BinaryReference(com.enonic.xp.util.BinaryReference) DISPLAY_NAME(com.enonic.xp.content.ContentPropertyNames.DISPLAY_NAME) ValidationErrorCode(com.enonic.xp.content.ValidationErrorCode) PROCESSED_REFERENCES(com.enonic.xp.content.ContentPropertyNames.PROCESSED_REFERENCES) PUBLISH_TO(com.enonic.xp.content.ContentPropertyNames.PUBLISH_TO) WORKFLOW_INFO_STATE(com.enonic.xp.content.ContentPropertyNames.WORKFLOW_INFO_STATE) Set(java.util.Set) LayoutDescriptorService(com.enonic.xp.region.LayoutDescriptorService) Collectors(java.util.stream.Collectors) ExtraDatas(com.enonic.xp.content.ExtraDatas) NodeId(com.enonic.xp.node.NodeId) UncheckedIOException(java.io.UncheckedIOException) TYPE(com.enonic.xp.content.ContentPropertyNames.TYPE) AttachmentNames(com.enonic.xp.attachment.AttachmentNames) VALID(com.enonic.xp.content.ContentPropertyNames.VALID) Optional(java.util.Optional) MODIFIED_TIME(com.enonic.xp.content.ContentPropertyNames.MODIFIED_TIME) UpdateContentTranslatorParams(com.enonic.xp.content.UpdateContentTranslatorParams) ARCHIVED_TIME(com.enonic.xp.content.ContentPropertyNames.ARCHIVED_TIME) CREATED_TIME(com.enonic.xp.content.ContentPropertyNames.CREATED_TIME) BinaryReferences(com.enonic.xp.util.BinaryReferences) Strings.nullToEmpty(com.google.common.base.Strings.nullToEmpty) PUBLISH_FIRST(com.enonic.xp.content.ContentPropertyNames.PUBLISH_FIRST) ORIGINAL_PARENT_PATH(com.enonic.xp.content.ContentPropertyNames.ORIGINAL_PARENT_PATH) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) Attachment(com.enonic.xp.attachment.Attachment) ContentTypeName(com.enonic.xp.schema.content.ContentTypeName) ORIGIN_PROJECT(com.enonic.xp.content.ContentPropertyNames.ORIGIN_PROJECT) ORIGINAL_NAME(com.enonic.xp.content.ContentPropertyNames.ORIGINAL_NAME) LinkedHashMap(java.util.LinkedHashMap) PUBLISH_FROM(com.enonic.xp.content.ContentPropertyNames.PUBLISH_FROM) VALIDATION_ERRORS(com.enonic.xp.content.ContentPropertyNames.VALIDATION_ERRORS) PageDescriptorService(com.enonic.xp.page.PageDescriptorService) PUBLISH_INFO(com.enonic.xp.content.ContentPropertyNames.PUBLISH_INFO) CreateAttachments(com.enonic.xp.attachment.CreateAttachments) Exceptions(com.enonic.xp.util.Exceptions) ATTACHMENT(com.enonic.xp.content.ContentPropertyNames.ATTACHMENT) ProjectName(com.enonic.xp.project.ProjectName) DATA(com.enonic.xp.content.ContentPropertyNames.DATA) StreamSupport(java.util.stream.StreamSupport) PropertyPath(com.enonic.xp.data.PropertyPath) LANGUAGE(com.enonic.xp.content.ContentPropertyNames.LANGUAGE) WORKFLOW_INFO(com.enonic.xp.content.ContentPropertyNames.WORKFLOW_INFO) PropertyTree(com.enonic.xp.data.PropertyTree) ObjectMapperHelper(com.enonic.xp.json.ObjectMapperHelper) PartDescriptorService(com.enonic.xp.region.PartDescriptorService) ContentPropertyNames(com.enonic.xp.content.ContentPropertyNames) ContentPath(com.enonic.xp.content.ContentPath) PropertySet(com.enonic.xp.data.PropertySet) MODIFIER(com.enonic.xp.content.ContentPropertyNames.MODIFIER) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ContentInheritType(com.enonic.xp.content.ContentInheritType) Content(com.enonic.xp.content.Content) ARCHIVED_BY(com.enonic.xp.content.ContentPropertyNames.ARCHIVED_BY) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ContentPublishInfo(com.enonic.xp.content.ContentPublishInfo) AttachmentValidationError(com.enonic.xp.content.AttachmentValidationError) COMPONENTS(com.enonic.xp.core.impl.content.serializer.ComponentDataSerializer.COMPONENTS) PrincipalKey(com.enonic.xp.security.PrincipalKey) Reference(com.enonic.xp.util.Reference) EXTRA_DATA(com.enonic.xp.content.ContentPropertyNames.EXTRA_DATA) ContentIds(com.enonic.xp.content.ContentIds) Attachments(com.enonic.xp.attachment.Attachments) Preconditions(com.google.common.base.Preconditions) CreateContentTranslatorParams(com.enonic.xp.content.CreateContentTranslatorParams) UncheckedIOException(java.io.UncheckedIOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ValidationErrorCode(com.enonic.xp.content.ValidationErrorCode)

Aggregations

PropertyPath (com.enonic.xp.data.PropertyPath)4 PropertySet (com.enonic.xp.data.PropertySet)4 Property (com.enonic.xp.data.Property)2 PropertyTree (com.enonic.xp.data.PropertyTree)2 Reference (com.enonic.xp.util.Reference)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Attachment (com.enonic.xp.attachment.Attachment)1 AttachmentNames (com.enonic.xp.attachment.AttachmentNames)1 AttachmentSerializer (com.enonic.xp.attachment.AttachmentSerializer)1 Attachments (com.enonic.xp.attachment.Attachments)1 CreateAttachment (com.enonic.xp.attachment.CreateAttachment)1 CreateAttachments (com.enonic.xp.attachment.CreateAttachments)1 AttachmentValidationError (com.enonic.xp.content.AttachmentValidationError)1 Content (com.enonic.xp.content.Content)1 ContentConstants (com.enonic.xp.content.ContentConstants)1 ContentId (com.enonic.xp.content.ContentId)1 ContentIds (com.enonic.xp.content.ContentIds)1 ContentInheritType (com.enonic.xp.content.ContentInheritType)1 ContentName (com.enonic.xp.content.ContentName)1