private <T extends CountStats> T getCountStats(final TableImpl table, final TableField<? extends Record, Timestamp> createdField, final T countStats) { final Field<Integer> totalField = DSL.selectCount().from(table).asField(); final Field<Integer> last7daysField = DSL.selectCount() .from(table) .where(createdField.ge(DSL.currentTimestamp().minus(7))) .asField(); final Field<Integer> last30daysField = DSL.selectCount() .from(table) .where(createdField.ge(DSL.currentTimestamp().minus(30))) .asField(); return jooq.select(totalField, last7daysField, last30daysField) .fetchOne() .map(r -> { countStats.setTotal(r.getValue(totalField)); countStats.setLast7days(r.getValue(last7daysField)); countStats.setLast30days(r.getValue(last30daysField)); return countStats; }); }
@Override public void delete(Object obj) { if (obj == null) { return; } Object id = ObjectUtils.getId(obj); String type = getType(obj); Table<?> table = JooqUtils.getTableFromRecordClass(JooqUtils.getRecordClass(getSchemaFactory(), obj.getClass())); TableField<?, Object> idField = JooqUtils.getTableField(getMetaDataManager(), type, ObjectMetaDataManager.ID_FIELD); if (idField == null) { throw new IllegalStateException("No ID field to delete object [" + obj + "]"); } create().delete(table).where(idField.eq(id)).execute(); }
@SuppressWarnings("unchecked") public static <T extends UpdatableRecord<?>> T findById(DSLContext context, Class<T> clz, Object id) { if (id == null) return null; Table<?> table = getTableFromRecordClass(clz); if (table == null) return null; UniqueKey<?> key = table.getPrimaryKey(); if (key == null || key.getFieldsArray().length != 1) return null; TableField<?, Object> keyField = (TableField<?, Object>) key.getFieldsArray()[0]; /* Convert object because we are abusing type safety here */ Object converted = keyField.getDataType().convert(id); return (T) context.selectFrom(table).where(keyField.eq(converted)).fetchOne(); }
@SuppressWarnings("unchecked") @Override public <T> List<? extends T> findNonRemoved(Class<T> mapType, Class<?> resourceType, long resourceId) { String type = schemaFactory.getSchemaName(mapType); Table<?> table = getTable(mapType); Relationship reference = getRelationship(mapType, resourceType); TableField<?, Object> removed = JooqUtils.getTableField(metaDataManager, type, ObjectMetaDataManager.REMOVED_FIELD); TableField<?, Object> referenceField = JooqUtils.getTableField(metaDataManager, type, reference.getPropertyName()); if ( removed == null || referenceField == null ) { throw new IllegalArgumentException("Type [" + mapType + "] is missing required removed or reference column"); } return (List<? extends T>)create() .selectFrom(table) .where( removed.isNull() .and(referenceField.eq(resourceId))) .fetch(); }
@SuppressWarnings("unchecked") @Override public <T> T findNonRemoved(Class<T> mapType, Class<?> leftResourceType, long leftResourceId, Class<?> rightResourceType, long rightResourceId) { String type = schemaFactory.getSchemaName(mapType); Table<?> table = getTable(mapType); Relationship leftReference = getRelationship(mapType, leftResourceType); Relationship rightReference = getRelationship(mapType, rightResourceType); TableField<?, Object> removed = JooqUtils.getTableField(metaDataManager, type, ObjectMetaDataManager.REMOVED_FIELD); TableField<?, Object> leftReferenceField = JooqUtils.getTableField(metaDataManager, type, leftReference.getPropertyName()); TableField<?, Object> rightReferenceField = JooqUtils.getTableField(metaDataManager, type, rightReference.getPropertyName()); if ( removed == null || leftReferenceField == null || rightReferenceField == null ) { throw new IllegalArgumentException("Type [" + mapType + "] is missing required removed or references column"); } return (T)create() .selectFrom(table) .where( removed.isNull() .and(leftReferenceField.eq(leftResourceId)) .and(rightReferenceField.eq(rightResourceId))) .fetchOne(); }
@SuppressWarnings("unchecked") @Override public <T> List<? extends T> findToRemove(Class<T> mapType, Class<?> resourceType, long resourceId) { String type = schemaFactory.getSchemaName(mapType); Table<?> table = getTable(mapType); Relationship reference = getRelationship(mapType, resourceType); TableField<?, Object> removed = JooqUtils.getTableField(metaDataManager, type, ObjectMetaDataManager.REMOVED_FIELD); TableField<?, Object> referenceField = JooqUtils.getTableField(metaDataManager, type, reference.getPropertyName()); TableField<?, Object> state = JooqUtils.getTableField(metaDataManager, type, ObjectMetaDataManager.STATE_FIELD); if ( removed == null || referenceField == null || state == null ) { throw new IllegalArgumentException("Type [" + mapType + "] is missing required removed, reference, or state column"); } return (List<? extends T>)create() .selectFrom(table) .where( referenceField.eq(resourceId) .and( removed.isNull() .or(state.eq(CommonStatesConstants.REMOVING)))) .fetch(); }
protected void addMappingJoins(SelectQuery<?> query, Table<?> toTable, SchemaFactory schemaFactory, String fromType, Table<?> from, String asName, MapRelationship rel) { Table<?> mappingTable = JooqUtils.getTableFromRecordClass(rel.getMappingType()); /* We don't required the mapping type to be visible external, that's why we use the schemaFactory * from the objectManager, because it is the superset schemaFactory. */ String mappingType = getObjectManager().getSchemaFactory().getSchemaName(rel.getMappingType()); TableField<?, Object> fieldFrom = JooqUtils.getTableField(getMetaDataManager(), fromType, ObjectMetaDataManager.ID_FIELD); TableField<?, Object> fieldTo = JooqUtils.getTableField(getMetaDataManager(), mappingType, rel.getPropertyName()); TableField<?, Object> fieldRemoved = JooqUtils.getTableField(getMetaDataManager(), mappingType, ObjectMetaDataManager.REMOVED_FIELD); org.jooq.Condition cond = fieldFrom.eq(fieldTo.getTable().field(fieldTo.getName())).and(fieldRemoved == null ? DSL.trueCondition() : fieldRemoved.isNull()); query.addJoin(mappingTable, JoinType.LEFT_OUTER_JOIN, cond); fieldFrom = JooqUtils.getTableField(getMetaDataManager(), mappingType, rel.getOtherRelationship().getPropertyName()); fieldTo = JooqUtils.getTableField(getMetaDataManager(), schemaFactory.getSchemaName(rel.getObjectType()), ObjectMetaDataManager.ID_FIELD); cond = fieldFrom.eq(fieldTo.getTable().asTable(asName).field(fieldTo.getName())); query.addJoin(toTable, JoinType.LEFT_OUTER_JOIN, cond); }
protected org.jooq.Condition getJoinCondition(SchemaFactory schemaFactory, String fromType, Table<?> from, String asName, Relationship rel) { TableField<?, Object> fieldFrom = null; TableField<?, Object> fieldTo = null; switch(rel.getRelationshipType()) { case REFERENCE: fieldFrom = JooqUtils.getTableField(getMetaDataManager(), fromType, rel.getPropertyName()); fieldTo = JooqUtils.getTableField(getMetaDataManager(), schemaFactory.getSchemaName(rel.getObjectType()), ObjectMetaDataManager.ID_FIELD); break; case CHILD: fieldFrom = JooqUtils.getTableField(getMetaDataManager(), fromType, ObjectMetaDataManager.ID_FIELD); fieldTo = JooqUtils.getTableField(getMetaDataManager(), schemaFactory.getSchemaName(rel.getObjectType()), rel.getPropertyName()); break; default: throw new IllegalArgumentException("Illegal Relationship type [" + rel.getRelationshipType() + "]"); } if ( fieldFrom == null || fieldTo == null ) { throw new IllegalStateException("Failed to construction join query for [" + fromType + "] [" + from + "] [" + rel + "]"); } return fieldFrom.eq(fieldTo.getTable().as(asName).field(fieldTo.getName())); }
protected void addSort(SchemaFactory schemaFactory, String type, Sort sort, SelectQuery<?> query) { if ( sort == null ) { return; } TableField<?, Object> sortField = JooqUtils.getTableField(getMetaDataManager(), type, sort.getName()); if ( sortField == null ) { return; } switch (sort.getOrderEnum()) { case DESC: query.addOrderBy(sortField.desc()); default: query.addOrderBy(sortField.asc()); } }
@Override protected void addAccountAuthorization(String type, Map<Object, Object> criteria, Policy policy) { super.addAccountAuthorization(type, criteria, policy); if ( ! policy.isOption(Policy.AUTHORIZED_FOR_ALL_ACCOUNTS) ) { TableField<?, Object> accountField = JooqUtils.getTableField(getMetaDataManager(), type, ObjectMetaDataManager.ACCOUNT_FIELD); TableField<?, Object> publicField = JooqUtils.getTableField(getMetaDataManager(), type, ObjectMetaDataManager.PUBLIC_FIELD); Object accountValue = criteria.get(ObjectMetaDataManager.ACCOUNT_FIELD); if ( accountField == null || publicField == null || accountValue == null ) { return; } criteria.remove(ObjectMetaDataManager.ACCOUNT_FIELD); Condition accountCondition = null; if ( accountValue instanceof io.github.ibuildthecloud.gdapi.condition.Condition ) { accountCondition = accountField.in(((io.github.ibuildthecloud.gdapi.condition.Condition)accountValue).getValues()); } else { accountCondition = accountField.eq(accountValue); } criteria.put(Condition.class, publicField.isTrue().or(accountCondition)); } }
@Override protected Object removeFromStore(String type, String id, Object obj, ApiRequest request) { Table<?> table = JooqUtils.getTableFromRecordClass(JooqUtils.getRecordClass(request.getSchemaFactory(), obj.getClass())); TableField<?, Object> idField = JooqUtils.getTableField(getMetaDataManager(), type, ObjectMetaDataManager.ID_FIELD); int result = create() .delete(table) .where(idField.eq(id)) .execute(); if ( result != 1 ) { log.error("While deleting type [{}] and id [{}] got a result of [{}]", type, id, result); throw new ClientVisibleException(ResponseCodes.CONFLICT); } return obj; }
@SuppressWarnings("unchecked") @Override protected <T> List<T> getListByRelationshipMap(Object obj, MapRelationship rel) { Class<UpdatableRecord<?>> typeClass = JooqUtils.getRecordClass(schemaFactory, rel.getObjectType()); String mappingType = schemaFactory.getSchemaName(rel.getMappingType()); String fromType = schemaFactory.getSchemaName(rel.getObjectType()); TableField<?, Object> fieldFrom = JooqUtils.getTableField(getMetaDataManager(), fromType, ObjectMetaDataManager.ID_FIELD); TableField<?, Object> mappingTo = JooqUtils.getTableField(getMetaDataManager(), mappingType, rel.getOtherRelationship().getPropertyName()); TableField<?, Object> mappingOther = JooqUtils.getTableField(getMetaDataManager(), mappingType, rel.getPropertyName()); TableField<?, Object> mappingRemoved = JooqUtils.getTableField(getMetaDataManager(), mappingType, ObjectMetaDataManager.REMOVED_FIELD); Table<?> table = JooqUtils.getTable(schemaFactory, typeClass); Table<?> mapTable = JooqUtils.getTable(schemaFactory, rel.getMappingType()); SelectQuery<?> query = create().selectQuery(); query.addFrom(table); query.addSelect(table.fields()); query.addJoin(mapTable, fieldFrom.eq(mappingTo) .and(mappingRemoved == null ? DSL.trueCondition() : mappingRemoved.isNull()) .and(mappingOther.eq(ObjectUtils.getId(obj)))); return (List<T>)query.fetchInto(typeClass); }
@Override public void delete(Object obj) { if ( obj == null ) { return; } Object id = ObjectUtils.getId(obj); String type = getType(obj); Table<?> table = JooqUtils.getTableFromRecordClass(JooqUtils.getRecordClass(getSchemaFactory(), obj.getClass())); TableField<?, Object> idField = JooqUtils.getTableField(getMetaDataManager(), type, ObjectMetaDataManager.ID_FIELD); int result = create() .delete(table) .where(idField.eq(id)) .execute(); if ( result != 1 ) { throw new IllegalStateException("Failed to delete [" + type + "] id [" + id + "]"); } }
@SuppressWarnings("unchecked") public static <T extends UpdatableRecord<?>> T findById(DSLContext context, Class<T> clz, Object id) { if ( id == null ) return null; Table<?> table = getTableFromRecordClass(clz); if ( table == null ) return null; UniqueKey<?> key = table.getPrimaryKey(); if ( key == null || key.getFieldsArray().length != 1 ) return null; TableField<?, Object> keyField = (TableField<?, Object>)key.getFieldsArray()[0]; /* Convert object because we are abusing type safety here */ Object converted = keyField.getDataType().convert(id); return (T)context.selectFrom(table) .where(keyField.eq(converted)) .fetchOne(); }
protected void addMappingJoins(SelectQuery<?> query, Table<?> toTable, SchemaFactory schemaFactory, String fromType, Table<?> from, String asName, MapRelationship rel) { Table<?> mappingTable = JooqUtils.getTableFromRecordClass(rel.getMappingType()); String mappingType = schemaFactory.getSchemaName(rel.getMappingType()); TableField<?, Object> fieldFrom = JooqUtils.getTableField(getMetaDataManager(), fromType, ObjectMetaDataManager.ID_FIELD); TableField<?, Object> fieldTo = JooqUtils.getTableField(getMetaDataManager(), mappingType, rel.getPropertyName()); TableField<?, Object> fieldRemoved = JooqUtils.getTableField(getMetaDataManager(), mappingType, ObjectMetaDataManager.REMOVED_FIELD); org.jooq.Condition cond = fieldFrom.eq(fieldTo.getTable().field(fieldTo.getName())).and(fieldRemoved == null ? DSL.trueCondition() : fieldRemoved.isNull()); query.addJoin(mappingTable, JoinType.LEFT_OUTER_JOIN, cond); fieldFrom = JooqUtils.getTableField(getMetaDataManager(), mappingType, rel.getOtherRelationship().getPropertyName()); fieldTo = JooqUtils.getTableField(getMetaDataManager(), schemaFactory.getSchemaName(rel.getObjectType()), ObjectMetaDataManager.ID_FIELD); cond = fieldFrom.eq(fieldTo.getTable().asTable(asName).field(fieldTo.getName())); query.addJoin(toTable, JoinType.LEFT_OUTER_JOIN, cond); }
protected void register(ForeignKey<?, ?> foreignKey) { TableField<?, ?>[] fields = foreignKey.getFieldsArray(); if ( fields.length == 0 || fields.length > 1 ) { return; } TableField<?, ?> field = fields[0]; String propertyName = getNameFromField(field.getTable().getRecordType(), field.getName()); String referenceName = propertyName; if ( field.getName().endsWith(ID_FIELD) ) { referenceName = referenceName.substring(0, referenceName.length() - 2); } Class<?> localType = foreignKey.getTable().getRecordType(); Class<?> foreignType = foreignKey.getKey().getTable().getRecordType(); Schema localSchema = schemaFactory.getSchema(localType); String childName = localSchema.getPluralName(); register(localType, new ForeignKeyRelationship(REFERENCE, referenceName, propertyName, foreignType, foreignKey)); register(foreignType, new ForeignKeyRelationship(CHILD, childName, propertyName, localType, foreignKey)); }
private void addMask(SelectQuery<Record> selectQuery, UUID classifier, UUID classification, TableField<?, UUID> field, ExistentialDomain domain) { if (classifier.equals(WellKnownRelationship.ANY.id())) { if (log.isTraceEnabled()) { log.trace("Match ANY {}", field.getName()); } return; } Condition condition; if (classifier.equals(WellKnownRelationship.SAME.id())) { condition = field.eq(classification); if (log.isTraceEnabled()) { log.trace("Match SAME {}", field.getName()); } } else { condition = field.in(inferenceSubQuery(classifier, classification)); if (log.isTraceEnabled()) { log.trace("Match on inferrred {}", field.getName()); } } condition = condition.or(field.equal(getAnyId(domain))) .or(field.equal(getSameId(domain))) .or(field.equal(getNotApplicable(domain))); selectQuery.addConditions(condition); }
private void processBooleanType(SelectQuery selectQuery, TableField<OcppTagRecord, Boolean> field, OcppTagQueryForm.BooleanType type) { switch (type) { case ALL: break; default: selectQuery.addConditions(field.eq(type.getBoolValue())); } }
protected final void setPrimaryKey(TableField<R, ?>... fields) { this.primaryKey = Keys.uniqueKey(this, fields); this.uniqueKeys.add(primaryKey); if (fields.length == 1) { TableField<R, ?> key = fields[0]; if (key.getDataType().identity()) { this.identity = Keys.identity(key.getTable(), key); } } }
@Override public Account updateAccount(Account account, String name, String kind, String externalId, String externalType) { Map<TableField<AccountRecord, String>, String> properties = new HashMap<>(); if (StringUtils.isNotEmpty(name)) { properties.put(ACCOUNT.NAME, name); } if (StringUtils.isNotEmpty(kind)) { properties.put(ACCOUNT.KIND, kind); } if (StringUtils.isNotEmpty(externalId)) { properties.put(ACCOUNT.EXTERNAL_ID, externalId); } if (StringUtils.isNotEmpty(externalType)) { properties.put(ACCOUNT.EXTERNAL_ID_TYPE, externalType); } int updateCount = create() .update(ACCOUNT) .set(properties) .where(ACCOUNT.ID .eq(account.getId())) .execute(); if (1 != updateCount) { throw new RuntimeException("UpdateAccount failed."); } return objectManager.reload(account); }
protected void addSort(SchemaFactory schemaFactory, String type, Sort sort, SelectQuery<?> query) { if (sort != null) { TableField<?, Object> sortField = JooqUtils.getTableField(metaDataManager, type, sort.getName()); if (sortField == null) { return; } switch (sort.getOrderEnum()) { case DESC: query.addOrderBy(sortField.desc()); break; default: query.addOrderBy(sortField.asc()); } } TableField<?, Object> idSort = JooqUtils.getTableField(metaDataManager, type, ObjectMetaDataManager.ID_FIELD); if (idSort == null) { return; } if (sort != null) { switch (sort.getOrderEnum()) { case DESC: query.addOrderBy(idSort.desc()); break; default: query.addOrderBy(idSort.asc()); } } else { query.addOrderBy(idSort.asc()); } }
protected void register(ForeignKey<?, ?> foreignKey) { TableField<?, ?>[] fields = foreignKey.getFieldsArray(); if (fields.length == 0 || fields.length > 1) { return; } TableField<?, ?> field = fields[0]; if (!field.getDataType().isNumeric()) { return; } String propertyName = getNameFromField(field.getTable().getRecordType(), field.getName()); String referenceName = propertyName; if (field.getName().endsWith(ID_FIELD)) { referenceName = referenceName.substring(0, referenceName.length() - 2); } Class<?> localType = foreignKey.getTable().getRecordType(); Class<?> foreignType = foreignKey.getKey().getTable().getRecordType(); Schema localSchema = schemaFactory.getSchema(localType); String childName = localSchema.getPluralName(); String childNameOverride = ArchaiusUtil.getString( String.format("object.link.name.%s.%s.override", localType.getSimpleName(), propertyName).toLowerCase()).get(); if (childNameOverride != null) { childName = childNameOverride; } register(localType, new ForeignKeyRelationship(REFERENCE, referenceName, propertyName, foreignType, foreignKey)); register(foreignType, new ForeignKeyRelationship(CHILD, childName, propertyName, localType, foreignKey)); }
protected TableField<?, ?> getTableFieldFor(String type, Object key) { if (key instanceof TableField<?, ?>) return (TableField<?, ?>) key; Class<?> clz = schemaFactory.getSchemaClass(type); FieldCacheKey cacheKey = new FieldCacheKey(clz, key.toString()); return tableFields.get(cacheKey); }
@Override public String convertToPropertyNameString(Class<?> recordClass, Object key) { if (key instanceof String) { return (String) key; } if (key instanceof TableField) { TableField<?, ?> field = (TableField<?, ?>) key; return getNameFromField(recordClass, field.getName()); } return key == null ? null : key.toString(); }
@SuppressWarnings("unchecked") public static TableField<?, Object> getTableField(ObjectMetaDataManager metaData, String type, Object key) { Object objField = metaData.convertFieldNameFor(type, key); if (objField instanceof TableField) { return (TableField<?, Object>) objField; } else { return null; } }
protected static org.jooq.Condition listToCondition(TableField<?, Object> field, List<?> list) { org.jooq.Condition condition = null; for (Object value : list) { if (value instanceof Condition) { org.jooq.Condition newCondition = toCondition(field, (Condition) value); condition = condition == null ? newCondition : condition.and(newCondition); } else { condition = condition == null ? field.eq(value) : condition.and(field.eq(value)); } } return condition; }
private static boolean hasValues(Connection conn, TableField<?, ?>... fields) { for (TableField<?, ?> field : fields) { for (Record1<?> r : DSL.using(conn, SQLDialect.HSQLDB).selectDistinct(field).from(field.getTable())) { if (r.value1() != null) { return true; } } } return false; }
@SuppressWarnings("unchecked") @Override public Set<String> getMappedUuids(String uuid, Class<?> resourceType, Class<?> mappingType, Class<?> otherType) { Table<?> resourceTable = JooqUtils.getTable(schemaFactory, resourceType); Table<?> mappingTable = JooqUtils.getTable(schemaFactory, mappingType); Table<?> otherTable = JooqUtils.getTable(schemaFactory, otherType); TableField<Record,Field<Long>> resourceTableField = getReferenceField(mappingTable, resourceTable); TableField<Record,Field<Long>> otherTableField = getReferenceField(mappingTable, otherTable); final Set<String> result = new HashSet<String>(); create() .select((Field<String>)otherTable.field(ObjectMetaDataManager.UUID_FIELD)) .from(resourceTable) .join(mappingTable) .on(resourceTableField.eq((Field<Long>)resourceTable.field(ObjectMetaDataManager.ID_FIELD))) .join(otherTable) .on(otherTableField.eq((Field<Long>)otherTable.field(ObjectMetaDataManager.ID_FIELD))) .where( ((Field<String>)resourceTable.field(ObjectMetaDataManager.UUID_FIELD)).eq(uuid)) .fetchInto(new RecordHandler<Record1<String>>() { @Override public void next(Record1<String> record) { result.add(record.value1()); } }); return result; }
@Override public <T> T newResource(Class<T> resourceType, Class<?> mappingType, Class<?> otherType, Map<String, Object> properties, String mappedUuid) throws MissingDependencyException { Table<?> resourceTable = JooqUtils.getTable(schemaFactory, resourceType); Table<?> mappingTable = JooqUtils.getTable(schemaFactory, mappingType); Table<?> otherTable = JooqUtils.getTable(schemaFactory, otherType); TableField<Record,Field<Long>> resourceTableField = getReferenceField(mappingTable, resourceTable); TableField<Record,Field<Long>> otherTableField = getReferenceField(mappingTable, otherTable); if ( mappedUuid == null ) { throw new IllegalStateException("Expected a mapped resource"); } Object otherResource = objectManager.findOne(otherType, ObjectMetaDataManager.UUID_FIELD, mappedUuid); if ( otherResource == null ) { throw new MissingDependencyException(); } T resource = resourceDao.createAndSchedule(resourceType, properties); Map<Object,Object> fields = new HashMap<Object, Object>(); fields.put(resourceTableField, ObjectUtils.getId(resource)); fields.put(otherTableField, ObjectUtils.getId(otherResource)); Map<String,Object> mappingResource = objectManager.convertToPropertiesFor(mappingType, fields); resourceDao.createAndSchedule(mappingType, mappingResource); return resource; }
@SuppressWarnings("unchecked") protected TableField<Record, Field<Long>> getReferenceField(Table<?> mappingTable, Table<?> otherTable) { List<?> foreignKeys = mappingTable.getReferencesTo(otherTable); if ( foreignKeys.size() != 1 ) { throw new IllegalStateException("Expected one foreign key from [" + mappingTable + "] to [" + otherTable + "]"); } List<TableField<Record, ?>> fields = ((ForeignKey<Record, Record>)foreignKeys.get(0)).getFields(); if ( fields.size() != 1 ) { throw new IllegalStateException("Expected on field for foreign key [" + foreignKeys.get(0) + "]"); } return (TableField<Record, Field<Long>>)fields.get(0); }
@Override protected Object getMapLink(String fromType, String id, MapRelationship rel, ApiRequest request) { SchemaFactory schemaFactory = request.getSchemaFactory(); /* We don't required the mapping type to be visible external, that's why we use the schemaFactory * from the objectManager, because it is the superset schemaFactory. */ String mappingType = getObjectManager().getSchemaFactory().getSchemaName(rel.getMappingType()); String type = schemaFactory.getSchemaName(rel.getObjectType()); Map<Table<?>,Condition> joins = new LinkedHashMap<Table<?>, Condition>(); Map<Object, Object> criteria = new LinkedHashMap<Object, Object>(); if ( mappingType == null || type == null ) { return null; } Table<?> mappingTable = JooqUtils.getTable(schemaFactory, rel.getMappingType()); TableField<?, Object> fieldFrom = JooqUtils.getTableField(getMetaDataManager(), type, ObjectMetaDataManager.ID_FIELD); TableField<?, Object> fieldTo = JooqUtils.getTableField(getMetaDataManager(), mappingType, rel.getOtherRelationship().getPropertyName()); TableField<?, Object> fieldRemoved = JooqUtils.getTableField(getMetaDataManager(), mappingType, ObjectMetaDataManager.REMOVED_FIELD); TableField<?, Object> fromTypeIdField = JooqUtils.getTableField(getMetaDataManager(), mappingType, rel.getSelfRelationship().getPropertyName()); org.jooq.Condition cond = fieldFrom.eq(fieldTo.getTable().field(fieldTo.getName())) .and(fieldRemoved == null ? DSL.trueCondition() : fieldRemoved.isNull()); joins.put(mappingTable, cond); criteria.put(Condition.class, fromTypeIdField.eq(id)); return listInternal(schemaFactory, type, criteria, new ListOptions(request), joins); }