Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced the ImporterFactory with Several JcrImporter implementations… #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<groupId>nl.openweb.jcr</groupId>
<artifactId>jcr-mocking-tool</artifactId>
<version>1.3.4-SNAPSHOT</version>
<version>1.4.0-SNAPSHOT</version>

<name>JCR Mocking Tool</name>
<description>This a tool to facilitate setup of a JCR repository for testing purposes.</description>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,154 +1,118 @@
/*
* Copyright 2017 Open Web IT B.V. (https://www.openweb.nl/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package nl.openweb.jcr;
package nl.openweb.jcr.importer;

import nl.openweb.jcr.domain.NodeBean;
import nl.openweb.jcr.domain.PropertyBean;
import nl.openweb.jcr.json.JsonUtils;
import nl.openweb.jcr.JcrImporterException;
import nl.openweb.jcr.NodeBeanUtils;
import nl.openweb.jcr.utils.NodeTypeUtils;
import nl.openweb.jcr.utils.PathUtils;
import nl.openweb.jcr.utils.ReflectionUtils;
import org.apache.commons.beanutils.ConvertUtilsBean;

import javax.jcr.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.*;

import static nl.openweb.jcr.utils.ReflectionUtils.unwrapNodeDecorator;

/**
* Created by Ebrahim on 5/20/2017.
* @author Ivor
*/
public class Importer {
public abstract class AbstractJcrImporter implements JcrImporter {

public static final String JCR_PRIMARY_TYPE = "jcr:primaryType";
public static final String JCR_MIXIN_TYPES = "jcr:mixinTypes";
public static final String JCR_UUID = "jcr:uuid";
private final Set<String> protectedProperties;
private final boolean setProtectedProperties;
private final boolean saveSession;
private final boolean addMixins;
private final boolean addUuid;
private final boolean addUnknownTypes;
private boolean addMixins = true;
private boolean addUuid = false;
private boolean setProtectedProperties = false;
private boolean saveSession = true;
private boolean addUnknownTypes = false;
private final Node rootNode;
private JAXBContext jaxbContext;

private Importer(Builder builder) {
try {
this.addMixins = builder.addMixins;
this.rootNode = builder.rootNodeSupplier.get();
this.addUuid = builder.addUuid;
this.setProtectedProperties = builder.setProtectedProperties;
this.saveSession = builder.saveSession;
this.addUnknownTypes = builder.addUnknownTypes;
HashSet<String> set = new HashSet<>();
set.add(JCR_PRIMARY_TYPE);
set.add(JCR_MIXIN_TYPES);
set.add(JCR_UUID);
this.protectedProperties = Collections.unmodifiableSet(set);
this.jaxbContext = JAXBContext.newInstance(NodeBean.class, PropertyBean.class);
} catch (Exception e) {
throw new JcrImporterException(e.getMessage(), e);
public AbstractJcrImporter(Node rootNode) {
this.rootNode = rootNode;
if (rootNode == null) {
throw new JcrImporterException("rootNode is not allowed to be null");
}
HashSet<String> set = new HashSet<>();
set.add(JCR_PRIMARY_TYPE);
set.add(JCR_MIXIN_TYPES);
set.add(JCR_UUID);
this.protectedProperties = Collections.unmodifiableSet(set);
}

public Node createNodesFromJson(String json) {
return createNodesFromJson(json, null, null);
@Override
public JcrImporter addMixins(boolean addMixins) {
this.addMixins = addMixins;
return this;
}

public Node createNodesFromJson(String json, String path) {
return createNodesFromJson(json, path, null);
public boolean isAddMixins() {
return addMixins;
}

public Node createNodesFromJson(String json, String path, String intermediateNodeType) {
try {
return createNodeFromNodeBean(JsonUtils.parseJsonMap(json), path, intermediateNodeType);
} catch (IOException e) {
throw new JcrImporterException(e.getMessage(), e);
}
@Override
public AbstractJcrImporter addUuid(boolean addUuid) {
this.addUuid = addUuid;
return this;
}

public Node createNodesFromJson(InputStream inputStream) {
return createNodesFromJson(inputStream, null, null);
public boolean isAddUuid() {
return addUuid;
}

public Node createNodesFromJson(InputStream inputStream, String path) {
return createNodesFromJson(inputStream, path, null);
@Override
public AbstractJcrImporter setProtectedProperties(boolean setProtectedProperties) {
this.setProtectedProperties = setProtectedProperties;
return this;
}

public Node createNodesFromJson(InputStream inputStream, String path, String intermediateNodeType) {
try {
validate(inputStream);
return createNodeFromNodeBean(JsonUtils.parseJsonMap(inputStream), path, intermediateNodeType);
} catch (IOException e) {
throw new JcrImporterException(e.getMessage(), e);
}
public boolean isSetProtectedProperties() {
return setProtectedProperties;
}

@Override
public AbstractJcrImporter saveSession(boolean saveSession) {
this.saveSession = saveSession;
return this;
}

public Node createNodesFromXml(String xml) {
return createNodesFromXml(xml, null);
public boolean isSaveSession() {
return saveSession;
}

public Node createNodesFromXml(String xml, String path) {
return createNodesFromXml(xml, path, null);
@Override
public AbstractJcrImporter addUnknownTypes(boolean addUnknownTypes) {
this.addUnknownTypes = addUnknownTypes;
return this;
}

public Node createNodesFromXml(String xml, String path, String intermediateNodeType) {
return this.createNodesFromXml(new ByteArrayInputStream(xml.getBytes()), path, intermediateNodeType);
public boolean isAddUnknownTypes() {
return addUnknownTypes;
}

public Node createNodesFromXml(InputStream inputStream) {
return createNodesFromXml(inputStream, null, null);
@Override
public Node getRootNode() {
return rootNode;
}

public Node createNodesFromXml(InputStream inputStream, String path) {
return createNodesFromXml(inputStream, path, null);

public Node createNodes(String source) {
return createNodes(source, null, null);
}

public Node createNodesFromXml(InputStream inputStream, String path, String intermediateNodeType) {
try {
validate(inputStream);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Object unmarshaled = unmarshaller.unmarshal(inputStream);
if (unmarshaled instanceof NodeBean) {
return createNodeFromNodeBean(NodeBeanUtils.nodeBeanToMap((NodeBean) unmarshaled), path, intermediateNodeType);
} else {
throw new JcrImporterException("The given XML file is not of the right format");
}
} catch (JAXBException e) {
throw new JcrImporterException(e.getMessage(), e);
}
public Node createNodes(String source, String path) {
return createNodes(source, path, null);
}

private void validate(InputStream inputStream) {
if (inputStream == null) {
throw new JcrImporterException("InputSteam may not be null.");
}
public Node createNodes(InputStream inputStream) {
return createNodes(inputStream, null, null);
}

public Node createNodes(InputStream inputStream, String path) {
return createNodes(inputStream, path, null);
}


private Node createNodeFromNodeBean(Map<String, Object> map, String path, String intermediateNodeType) {
Node createNodeFromNodeBean(Map<String, Object> map, String path, String intermediateNodeType) {
try {
Node node = getOrCreateNode(rootNode, path, intermediateNodeType, map);
updateNode(node, map);
Expand Down Expand Up @@ -364,55 +328,5 @@ private Value toJcrValue(Session session, Object value, String propertyName) thr
return result;
}

public static class Builder {
private boolean addMixins = true;
private boolean addUuid = false;
private boolean setProtectedProperties = false;
private boolean saveSession = true;
private boolean addUnknownTypes = false;
private final SupplierWithException<Node> rootNodeSupplier;


public Builder(SupplierWithException<Node> rootNodeSupplier) {
this.rootNodeSupplier = rootNodeSupplier;
if (this.rootNodeSupplier == null) {
throw new IllegalArgumentException("supplier is required.");
}
}

public Builder addMixins(boolean addMixins) {
this.addMixins = addMixins;
return this;
}

public Builder addUuid(boolean addUuid) {
this.addUuid = addUuid;
return this;
}

public Builder setProtectedProperties(boolean setProtectedProperties) {
this.setProtectedProperties = setProtectedProperties;
return this;
}

public Builder saveSession(boolean saveSession) {
this.saveSession = saveSession;
return this;
}

public Builder addUnknownTypes(boolean addUnknownTypes) {
this.addUnknownTypes = addUnknownTypes;
return this;
}

public Importer build() {
return new Importer(this);
}
}

@FunctionalInterface
public interface SupplierWithException<T> {
T get() throws Exception;
}

}
76 changes: 76 additions & 0 deletions src/main/java/nl/openweb/jcr/importer/JcrImporter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package nl.openweb.jcr.importer;

import javax.jcr.Node;
import java.io.InputStream;


/**
* Imports JCR nodes from an outside source
* @author Ivor Boers
*/
public interface JcrImporter {
String JCR_PRIMARY_TYPE = "jcr:primaryType";
String JCR_MIXIN_TYPES = "jcr:mixinTypes";
String JCR_UUID = "jcr:uuid";

JcrImporter addMixins(boolean addMixins);

JcrImporter addUuid(boolean addUuid);

JcrImporter setProtectedProperties(boolean setProtectedProperties);

JcrImporter saveSession(boolean saveSession);

JcrImporter addUnknownTypes(boolean addUnknownTypes);

Node getRootNode();

/**
* Create nodes from source on the root
* @param source the textual representation of the nodes
* @return the created (top)node
*/
Node createNodes(String source);

/**
* Create nodes from source at a specified path without a specified intermediate nodetype
* @param source the textual representation of the nodes
* @param path the path where the topnode should be imported
* @return the created (top)node
*/
Node createNodes(String source, String path);

/**
*
* @param source the textual representation of the nodes
* @param path the path where the topnode should be imported
* @param intermediateNodeType the type of the nodes between the exisiting path and the path where to create the nodes
* @return the created (top)node
*/
Node createNodes(String source, String path, String intermediateNodeType);

/**
* Create nodes from source on the root
* @param inputStream the stream representation of the nodes
* @return the created (top)node
*/
Node createNodes(InputStream inputStream);

/**
* Create nodes from source at a specified path without a specified intermediate nodetype
* @param inputStream the stream representation of the nodes
* @param path the path where the topnode should be imported
* @return the created (top)node
*/
Node createNodes(InputStream inputStream, String path);

/**
*
* @param inputStream the stream representation of the nodes
* @param path the path where the topnode should be imported
* @param intermediateNodeType the type of the nodes between the exisiting path and the path where to create the nodes
* @return the created (top)node
*/
Node createNodes(InputStream inputStream, String path, String intermediateNodeType);

}
Loading