cz.encircled.joiner.query.join.J Maven / Gradle / Ivy
package cz.encircled.joiner.query.join;
import com.querydsl.core.types.EntityPath;
import cz.encircled.joiner.util.Assert;
import cz.encircled.joiner.util.ReflectionUtils;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
/**
* This class contains helper methods for {@link JoinDescription joins} building
*
* @author Kisel on 26.01.2016.
*/
public class J {
/**
* Aliases of nested joins are determined at runtime. To refer a nested join, this method should be used to get a correct alias.
* For example, there is a query
*
* Q.from(QGroup.group).joins(J.left(QPerson.person).nested(J.left(QContact.contact)))
*
* To refer a Contact
entity in the 'where' clause, one should use J.path(QPerson.person, QContact.contact).number.eq(12345)
*
* @param parent parent join path
* @param path target join path
* @param any entity path
* @return entity path with correct alias
*/
@SuppressWarnings("unchcecked")
public static T path(EntityPath> parent, T path) {
if (parent != null) {
return ReflectionUtils.instantiate(path.getClass(), path.toString() + "_on_" + parent.toString());
}
return path;
}
/**
* Aliases of nested joins are determined at runtime. To refer a nested join, this method should be used to get a correct alias.
* For example, there is a query
*
* Q.from(QGroup.group).joins(J.left(QPerson.person).nested(J.left(QContact.contact).nested(QStatus.status)))
*
* To refer a Status
entity in the 'where' clause, one should use J.path(QPerson.person, QContact.contact. QStatus.status).state.eq("active")
*
* @param grandFather parent of parent join path
* @param father parent join path
* @param path target join path
* @param any entity path
* @return entity path with correct alias
*/
@SuppressWarnings("unchcecked")
public static T path(EntityPath> grandFather, EntityPath> father, T path) {
Assert.notNull(father);
Assert.notNull(grandFather);
EntityPath> parentPath = path(grandFather, father);
return path(parentPath, path);
}
/**
* Add left join for given path
*
* @param path alias of object to be joined
* @return join description
*/
public static JoinDescription left(EntityPath> path) {
return getBasicJoin(path).left();
}
/**
* Add inner join for given path
*
* @param path alias of object to be joined
* @return join description
*/
public static JoinDescription inner(EntityPath> path) {
return getBasicJoin(path).inner();
}
/**
* Collect all joins and its children to single collection
*
* @param joins root joins
* @return all joins, including children
*/
public static List unrollChildrenJoins(Collection joins) {
List collection = new LinkedList<>();
for (JoinDescription joinDescription : joins) {
unrollChildrenInternal(joinDescription, collection);
}
return collection;
}
private static void unrollChildrenInternal(JoinDescription join, List collection) {
collection.add(join);
for (JoinDescription child : join.getChildren()) {
unrollChildrenInternal(child, collection);
}
}
private static JoinDescription getBasicJoin(EntityPath> path) {
Assert.notNull(path);
return new JoinDescription(path);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy