private void createNodes()

in src/main/java/org/apache/sling/jcr/repoinit/impl/NodeVisitor.java [66:134]


    private void createNodes(
            List<PathSegmentDefinition> pathSegmentDefinitions, List<PropertyLine> propertyLines, boolean strict) {
        StringBuilder parentPathBuilder = new StringBuilder();
        for (PathSegmentDefinition psd : pathSegmentDefinitions) {
            String parentPath = parentPathBuilder.toString();
            final String fullPath = String.format("%s/%s", parentPath, psd.getSegment());
            try {
                final Node node;
                if (strict) {
                    if (session.nodeExists(fullPath)) {
                        log.info("Node at {} already exists, checking/adjusting its types", fullPath);
                        node = session.getNode(fullPath);
                        if (psd.getPrimaryType() != null
                                && !node.getPrimaryNodeType().getName().equals(psd.getPrimaryType())) {
                            log.info("Adjusting primary type of node {} to {}", fullPath, psd.getPrimaryType());
                            node.setPrimaryType(psd.getPrimaryType());
                        }
                    } else if (!session.propertyExists(fullPath)) {
                        final Node parent = parentPath.equals("") ? session.getRootNode() : session.getNode(parentPath);
                        log.info("Creating node {} with primary type {}", fullPath, psd.getPrimaryType());
                        node = addChildNode(parent, psd);

                    } else {
                        throw new RepoInitException(
                                "There is a property with the name of the to be created node already at " + fullPath
                                        + ", therefore bailing out here as potentially not supported by the underlying JCR");
                    }
                } else {
                    if (session.itemExists(fullPath)) {
                        log.info(
                                "Path already exists, nothing to do (and not checking its primary type for now): {}",
                                fullPath);
                        node = null;
                    } else {
                        final Node parent = parentPath.equals("") ? session.getRootNode() : session.getNode(parentPath);
                        log.info("Creating node {} with primary type {}", fullPath, psd.getPrimaryType());
                        node = addChildNode(parent, psd);
                    }
                }

                if (node != null) {
                    List<String> mixins = psd.getMixins();
                    if (mixins != null) {
                        log.info("Adding mixins {} to node {}", mixins, fullPath);
                        for (String mixin : mixins) {
                            node.addMixin(mixin);
                        }
                    }
                }
            } catch (Exception e) {
                report(e, "CreatePath execution failed at " + psd + ": " + e);
            }
            parentPathBuilder.append("/").append(psd.getSegment());
        }
        if (!propertyLines.isEmpty()) {
            // delegate to the NodePropertiesVisitor to set the properties
            SetProperties sp =
                    new SetProperties(Collections.singletonList(parentPathBuilder.toString()), propertyLines);
            NodePropertiesVisitor npv = new NodePropertiesVisitor(session);
            npv.visitSetProperties(sp);
        }
        try {
            if (session.hasPendingChanges()) {
                session.save();
            }
        } catch (Exception e) {
            report(e, "Session.save failed: " + e);
        }
    }