<commit_before>/**
 * Copyright (c) 2016 LARUS Business Automation [http://www.larus-ba.it]
 * <p>
 * This file is part of the "LARUS Integration Framework for Neo4j".
 * <p>
 * The "LARUS Integration Framework for Neo4j" is 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 it.larusba.integration.neo4j.jsonloader.transformer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

/**
 * {@link JsonTransformer} implementation converting a JSON document into
 * a Cypher statement
 * 
 * @author <NAME>
 * 
 * @see <a href="http://neo4j.com/blog/cypher-load-json-from-url/">http://neo4j.
 *      com/blog/cypher-load-json-from-url/</a>
 */
public class AttributeBasedJsonTransformer implements JsonTransformer<String> {

	/**
	 * @see it.larusba.integration.neo4j.jsonloader.transformer.JsonTransformer#transform(java.lang.String,
	 *      java.lang.String, java.lang.String)
	 */
	@Override
	public String transform(String documentId, String documentType, String jsonDocument)
			throws JsonParseException, JsonMappingException, IOException {

		Map<String, Object> documentMap = new ObjectMapper().readValue(jsonDocument,
				new TypeReference<Map<String, Object>>() {
				});

		return transform(documentId, documentType, documentMap);
	}

	/**
	 * @see it.larusba.integration.neo4j.jsonloader.transformer.JsonTransformer#transform(java.lang.String,
	 *      java.lang.String, java.util.Map)
	 */
	@Override
	@SuppressWarnings("unchecked")
	public String transform(String documentId, String documentType, Map<String, Object> documentMap) {

		StringBuffer rootNode = new StringBuffer();
		List<String> childNodes = new ArrayList<String>();
		List<String> childRelationships = new ArrayList<String>();

		rootNode.append("MERGE (").append(documentType).append(":").append(StringUtils.capitalize(documentType))
				.append(" { documentId: '").append(documentId).append("' })\n");

		boolean firstAttr = true;

		for (String attributeName : documentMap.keySet()) {

			Object attributeValue = documentMap.get(attributeName);

			if (attributeValue instanceof Map) {

				childNodes.add(transform(documentId, attributeName, (Map<String, Object>) attributeValue));

				childRelationships.add(new StringBuffer().append("MERGE (").append(documentType).append(")-[")
						.append(":").append(documentType.toUpperCase()).append("_").append(attributeName.toUpperCase())
						.append("]->(").append(attributeName).append(")").toString());
			} else {

				if (firstAttr) {
					rootNode.append("ON CREATE SET ");
					firstAttr = false;
				} else {
					rootNode.append(", ");
				}

				rootNode.append(documentType).append(".").append(attributeName).append(" = ");

				if (attributeValue instanceof String) {
					rootNode.append("'").append(attributeValue).append("'");
				} else {
					rootNode.append(attributeValue);
				}
			}
		}

		StringBuffer cypher = new StringBuffer();

		cypher.append(rootNode);

		for (String childNode : childNodes) {

			cypher.append("\n").append(childNode);
		}

		for (String childRelationship : childRelationships) {

			cypher.append("\n").append(childRelationship);
		}

		return cypher.toString();
	}
}<commit_msg>attribute based always creates new nodes as it's domain agnostic<commit_after>/**
 * Copyright (c) 2016 LARUS Business Automation [http://www.larus-ba.it]
 * <p>
 * This file is part of the "LARUS Integration Framework for Neo4j".
 * <p>
 * The "LARUS Integration Framework for Neo4j" is 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 it.larusba.integration.neo4j.jsonloader.transformer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

/**
 * {@link JsonTransformer} implementation converting a JSON document into a
 * Cypher statement
 * 
 * @author <NAME>
 * 
 * @see <a href="http://neo4j.com/blog/cypher-load-json-from-url/">http://neo4j.
 *      com/blog/cypher-load-json-from-url/</a>
 */
public class AttributeBasedJsonTransformer implements JsonTransformer<String> {

	/**
	 * @see it.larusba.integration.neo4j.jsonloader.transformer.JsonTransformer#transform(java.lang.String,
	 *      java.lang.String, java.lang.String)
	 */
	@Override
	public String transform(String documentId, String documentType, String jsonDocument)
			throws JsonParseException, JsonMappingException, IOException {

		Map<String, Object> documentMap = new ObjectMapper().readValue(jsonDocument,
				new TypeReference<Map<String, Object>>() {
				});

		return transform(documentId, documentType, documentMap);
	}

	/**
	 * @see it.larusba.integration.neo4j.jsonloader.transformer.JsonTransformer#transform(java.lang.String,
	 *      java.lang.String, java.util.Map)
	 */
	@Override
	@SuppressWarnings("unchecked")
	public String transform(String documentId, String documentType, Map<String, Object> documentMap) {

		StringBuffer rootNode = new StringBuffer();
		List<String> childNodes = new ArrayList<String>();
		List<String> childRelationships = new ArrayList<String>();

		String nodeReference = StringUtils.lowerCase(documentType);
		String nodeLabel = StringUtils.capitalize(nodeReference);
		
		rootNode.append("CREATE (").append(nodeReference).append(":").append(nodeLabel);

		boolean firstAttr = true;

		for (String attributeName : documentMap.keySet()) {

			Object attributeValue = documentMap.get(attributeName);

			if (attributeValue instanceof Map) {

				childNodes.add(transform(documentId, attributeName, (Map<String, Object>) attributeValue));

				childRelationships.add(new StringBuffer().append("CREATE (").append(nodeReference).append(")-[")
						.append(":").append(nodeReference.toUpperCase()).append("_").append(attributeName.toUpperCase())
						.append("]->(").append(attributeName).append(")").toString());
			} else {

				if (firstAttr) {
					rootNode.append(" { ");

					if (documentId != null) {
						rootNode.append("_documentId: '").append(documentId).append("', ");
					}

					firstAttr = false;
				} else {
					rootNode.append(", ");
				}

				if (attributeValue != null) {
					rootNode.append(attributeName).append(": ");

					if (attributeValue instanceof String) {
						rootNode.append("'").append(attributeValue).append("'");
					} else {
						rootNode.append(attributeValue);
					}
				}
			}
		}

		rootNode.append(" })");

		StringBuffer cypher = new StringBuffer();

		cypher.append(rootNode);

		for (String childNode : childNodes) {

			cypher.append("\n").append(childNode);
		}

		for (String childRelationship : childRelationships) {

			cypher.append("\n").append(childRelationship);
		}

		return cypher.toString();
	}
}
{'fraction_non_alphanumeric': 0.13354569958343543, 'fraction_numerical': 0.0039206076941926, 'mean_word_length': 4.405960264900663, 'pattern_counts': {'":': 5, '<': 37, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '425092', 'n_tokens_mistral': 2690, 'n_tokens_neox': 2342, 'n_words': 560}