mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 05:01:07 +00:00
Add support for orderBys on child-joins
This commit is contained in:
@ -417,7 +417,7 @@ public class MetaDataProducerHelper
|
|||||||
return (null);
|
return (null);
|
||||||
}
|
}
|
||||||
|
|
||||||
ChildJoinFromRecordEntityGenericMetaDataProducer producer = new ChildJoinFromRecordEntityGenericMetaDataProducer(childTableName, parentTableName, possibleValueFieldName);
|
ChildJoinFromRecordEntityGenericMetaDataProducer producer = new ChildJoinFromRecordEntityGenericMetaDataProducer(childTableName, parentTableName, possibleValueFieldName, childTable.childJoin().orderBy());
|
||||||
producer.setSourceClass(entityClass);
|
producer.setSourceClass(entityClass);
|
||||||
return producer;
|
return producer;
|
||||||
}
|
}
|
||||||
|
@ -24,11 +24,13 @@ package com.kingsrook.qqq.backend.core.model.metadata.producers;
|
|||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||||
|
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterOrderBy;
|
||||||
import com.kingsrook.qqq.backend.core.model.metadata.MetaDataProducerInterface;
|
import com.kingsrook.qqq.backend.core.model.metadata.MetaDataProducerInterface;
|
||||||
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
|
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
|
||||||
import com.kingsrook.qqq.backend.core.model.metadata.joins.JoinOn;
|
import com.kingsrook.qqq.backend.core.model.metadata.joins.JoinOn;
|
||||||
import com.kingsrook.qqq.backend.core.model.metadata.joins.JoinType;
|
import com.kingsrook.qqq.backend.core.model.metadata.joins.JoinType;
|
||||||
import com.kingsrook.qqq.backend.core.model.metadata.joins.QJoinMetaData;
|
import com.kingsrook.qqq.backend.core.model.metadata.joins.QJoinMetaData;
|
||||||
|
import com.kingsrook.qqq.backend.core.model.metadata.producers.annotations.ChildJoin;
|
||||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
|
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
|
||||||
|
|
||||||
|
|
||||||
@ -39,12 +41,11 @@ import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
|
|||||||
**
|
**
|
||||||
** e.g., Orders & LineItems - on the Order entity
|
** e.g., Orders & LineItems - on the Order entity
|
||||||
** <code>
|
** <code>
|
||||||
@QMetaDataProducingEntity(
|
@QMetaDataProducingEntity( childTables = { @ChildTable(
|
||||||
childTables = { @ChildTable(
|
childTableEntityClass = LineItem.class,
|
||||||
childTableEntityClass = LineItem.class,
|
childJoin = @ChildJoin(enabled = true),
|
||||||
childJoin = @ChildJoin(enabled = true),
|
childRecordListWidget = @ChildRecordListWidget(enabled = true, label = "Order Lines"))
|
||||||
childRecordListWidget = @ChildRecordListWidget(enabled = true, label = "Order Lines"))
|
}
|
||||||
}
|
|
||||||
)
|
)
|
||||||
public class Order extends QRecordEntity
|
public class Order extends QRecordEntity
|
||||||
** </code>
|
** </code>
|
||||||
@ -62,13 +63,16 @@ public class ChildJoinFromRecordEntityGenericMetaDataProducer implements MetaDat
|
|||||||
private String parentTableName; // e.g., order
|
private String parentTableName; // e.g., order
|
||||||
private String foreignKeyFieldName; // e.g., orderId
|
private String foreignKeyFieldName; // e.g., orderId
|
||||||
|
|
||||||
|
private ChildJoin.OrderBy[] orderBys;
|
||||||
|
|
||||||
private Class<?> sourceClass;
|
private Class<?> sourceClass;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/***************************************************************************
|
/***************************************************************************
|
||||||
**
|
**
|
||||||
***************************************************************************/
|
***************************************************************************/
|
||||||
public ChildJoinFromRecordEntityGenericMetaDataProducer(String childTableName, String parentTableName, String foreignKeyFieldName)
|
public ChildJoinFromRecordEntityGenericMetaDataProducer(String childTableName, String parentTableName, String foreignKeyFieldName, ChildJoin.OrderBy[] orderBys)
|
||||||
{
|
{
|
||||||
Objects.requireNonNull(childTableName, "childTableName cannot be null");
|
Objects.requireNonNull(childTableName, "childTableName cannot be null");
|
||||||
Objects.requireNonNull(parentTableName, "parentTableName cannot be null");
|
Objects.requireNonNull(parentTableName, "parentTableName cannot be null");
|
||||||
@ -77,6 +81,7 @@ public class ChildJoinFromRecordEntityGenericMetaDataProducer implements MetaDat
|
|||||||
this.childTableName = childTableName;
|
this.childTableName = childTableName;
|
||||||
this.parentTableName = parentTableName;
|
this.parentTableName = parentTableName;
|
||||||
this.foreignKeyFieldName = foreignKeyFieldName;
|
this.foreignKeyFieldName = foreignKeyFieldName;
|
||||||
|
this.orderBys = orderBys;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -87,18 +92,39 @@ public class ChildJoinFromRecordEntityGenericMetaDataProducer implements MetaDat
|
|||||||
@Override
|
@Override
|
||||||
public QJoinMetaData produce(QInstance qInstance) throws QException
|
public QJoinMetaData produce(QInstance qInstance) throws QException
|
||||||
{
|
{
|
||||||
QTableMetaData possibleValueTable = qInstance.getTable(parentTableName);
|
QTableMetaData parentTable = qInstance.getTable(parentTableName);
|
||||||
if(possibleValueTable == null)
|
if(parentTable == null)
|
||||||
{
|
{
|
||||||
throw (new QException("Could not find tableMetaData " + parentTableName));
|
throw (new QException("Could not find tableMetaData " + parentTableName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QTableMetaData childTable = qInstance.getTable(childTableName);
|
||||||
|
if(childTable == null)
|
||||||
|
{
|
||||||
|
throw (new QException("Could not find tableMetaData " + childTable));
|
||||||
|
}
|
||||||
|
|
||||||
QJoinMetaData join = new QJoinMetaData()
|
QJoinMetaData join = new QJoinMetaData()
|
||||||
.withLeftTable(parentTableName)
|
.withLeftTable(parentTableName)
|
||||||
.withRightTable(childTableName)
|
.withRightTable(childTableName)
|
||||||
.withInferredName()
|
.withInferredName()
|
||||||
.withType(JoinType.ONE_TO_MANY)
|
.withType(JoinType.ONE_TO_MANY)
|
||||||
.withJoinOn(new JoinOn(possibleValueTable.getPrimaryKeyField(), foreignKeyFieldName));
|
.withJoinOn(new JoinOn(parentTable.getPrimaryKeyField(), foreignKeyFieldName));
|
||||||
|
|
||||||
|
if(orderBys != null && orderBys.length > 0)
|
||||||
|
{
|
||||||
|
for(ChildJoin.OrderBy orderBy : orderBys)
|
||||||
|
{
|
||||||
|
join.withOrderBy(new QFilterOrderBy(orderBy.fieldName(), orderBy.isAscending()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//////////////////////////////////////////////////////////
|
||||||
|
// by default, sort by the id of the child table... mmm //
|
||||||
|
//////////////////////////////////////////////////////////
|
||||||
|
join.withOrderBy(new QFilterOrderBy(childTable.getPrimaryKeyField()));
|
||||||
|
}
|
||||||
|
|
||||||
return (join);
|
return (join);
|
||||||
}
|
}
|
||||||
@ -126,6 +152,7 @@ public class ChildJoinFromRecordEntityGenericMetaDataProducer implements MetaDat
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
** Fluent setter for sourceClass
|
** Fluent setter for sourceClass
|
||||||
**
|
**
|
||||||
|
@ -35,4 +35,13 @@ import java.lang.annotation.RetentionPolicy;
|
|||||||
public @interface ChildJoin
|
public @interface ChildJoin
|
||||||
{
|
{
|
||||||
boolean enabled();
|
boolean enabled();
|
||||||
|
|
||||||
|
OrderBy[] orderBy() default { };
|
||||||
|
|
||||||
|
@interface OrderBy
|
||||||
|
{
|
||||||
|
String fieldName();
|
||||||
|
|
||||||
|
boolean isAscending() default true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user