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

com.alterioncorp.jpa.entitygraphbuilder.RelationshipParserImpl Maven / Gradle / Ivy

The newest version!
package com.alterioncorp.jpa.entitygraphbuilder;

import java.util.Arrays;

import com.querydsl.core.types.Path;

class RelationshipParserImpl implements RelationshipParser {
	
	@Override
	public RelationshipTree build(Path... paths) {

		String[] pathsAsStrings = Arrays.stream(paths)
			.map((Path path) -> pathToString(path))
			.toArray(String[]::new);
		
		return this.build(pathsAsStrings);
	}
	
	@Override
	public RelationshipTree build(String... paths) {

		RelationshipTree tree = new RelationshipTree();
		
		for (String path : paths) {
			RelationshipNode node = buildFromPath(path);
			tree.addNode(node);
		}

		return tree;
	}
	
	static RelationshipNode buildFromPath(String path) {
		return buildFromPath(path.split("\\."), 0);
	}

	private static RelationshipNode buildFromPath(String[] pathParsed, int startPosition) {
		RelationshipNode node = new RelationshipNode(pathParsed[startPosition]);
		if (startPosition < (pathParsed.length - 1)) {
			node.getChildren().add(buildFromPath(pathParsed, startPosition + 1));
		}
		return node;
	}
	
	static String pathToString(Path dslQueryPath) {
		String path = dslQueryPath.toString();
		path = path.replaceAll("any\\(", "");
		path = path.replaceAll("\\)", "");
		path = path.substring(path.indexOf('.') + 1);
		return path;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy