All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.neo4j.ogm.result.adapter.RestModelAdapter Maven / Gradle / Ivy

There is a newer version: 4.0.11
Show newest version
/*
 * Copyright (c) 2002-2016 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This product is licensed to you under the Apache License, Version 2.0 (the "License").
 * You may not use this product except in compliance with the License.
 *
 * This product may include a number of subcomponents with
 * separate copyright notices and license terms. Your use of the source
 * code for these subcomponents is subject to the terms and
 *  conditions of the subcomponent's license, as noted in the LICENSE file.
 */

package org.neo4j.ogm.result.adapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.neo4j.ogm.response.model.NodeModel;
import org.neo4j.ogm.response.model.RelationshipModel;

/**
 * Adapt embedded response to a NodeModels, RelationshipModels, and objects
 * @author Luanne Misquitta
 */
public abstract class RestModelAdapter extends BaseAdapter implements ResultAdapter, Map> {

	@Override
	public Map adapt(Map result) {
		Map adaptedResults = new LinkedHashMap<>();
		for (Map.Entry entry : result.entrySet()) {
			Object value = entry.getValue();
			if (value instanceof Collection) {
				List adaptedValues = new ArrayList<>();
				List values = (List) value;
				for (Object element : values) {
					adaptedValues.add(processData(element));
				}
				adaptedResults.put(entry.getKey(), adaptedValues);
			}
			else {
				adaptedResults.put(entry.getKey(), processData(value));
			}
		}

		return adaptedResults;
	}

	private Object processData(Object element) {
		if (isNode(element)) {
			return buildNode(element);
		}
		if (isRelationship(element)) {
			return buildRelationship(element);
		}
		return element;
	}

	private NodeModel buildNode(Object node) {
		NodeModel nodeModel = new NodeModel();
		nodeModel.setId(nodeId(node));
		List labels = labels(node);
		nodeModel.setLabels(labels.toArray(new String[labels.size()]));
		nodeModel.setProperties(convertArrayPropertiesToIterable(properties(node)));
		return nodeModel;
	}

	private RelationshipModel buildRelationship(Object relationship) {
		RelationshipModel relationshipModel = new RelationshipModel();
		relationshipModel.setId(relationshipId(relationship));
		relationshipModel.setStartNode(startNodeId(relationship));
		relationshipModel.setEndNode(endNodeId(relationship));
		relationshipModel.setType(relationshipType(relationship));
		relationshipModel.setProperties(properties(relationship));
		return relationshipModel;
	}

	public abstract boolean isNode(Object value);

	public abstract boolean isRelationship(Object value);

	public abstract long nodeId(Object node);

	public abstract List labels(Object node);

	public abstract long relationshipId(Object relationship);

	public abstract String relationshipType(Object relationship);

	public abstract Long startNodeId(Object relationship);

	public abstract Long endNodeId(Object relationship);

	public abstract Map properties(Object container);

}