use of com.walmartlabs.concord.server.jooq.tables.ProcessCheckpoints in project concord by walmartlabs.
the class ProcessQueueDao method buildSelect.
private SelectQuery<Record> buildSelect(DSLContext tx, ProcessKey key, ProcessFilter filter) {
SelectQuery<Record> query = tx.selectQuery();
// process_queue
query.addSelect(PROCESS_QUEUE_FIELDS);
query.addFrom(PROCESS_QUEUE);
// users
query.addSelect(USERS.USERNAME);
query.addJoin(USERS, JoinType.LEFT_OUTER_JOIN, USERS.USER_ID.eq(PROCESS_QUEUE.INITIATOR_ID));
// repositories
query.addSelect(REPOSITORIES.REPO_NAME);
query.addJoin(REPOSITORIES, JoinType.LEFT_OUTER_JOIN, REPOSITORIES.REPO_ID.eq(PROCESS_QUEUE.REPO_ID));
// organizations
Field<String> orgNameField = select(ORGANIZATIONS.ORG_NAME).from(ORGANIZATIONS).where(ORGANIZATIONS.ORG_ID.eq(Tables.PROJECTS.ORG_ID)).asField(ORGANIZATIONS.ORG_NAME.getName());
query.addSelect(orgNameField);
// projects
query.addSelect(Tables.PROJECTS.PROJECT_NAME, Tables.PROJECTS.ORG_ID);
query.addJoin(Tables.PROJECTS, JoinType.LEFT_OUTER_JOIN, Tables.PROJECTS.PROJECT_ID.eq(PROCESS_QUEUE.PROJECT_ID));
Set<UUID> orgIds = filter.orgIds();
if (orgIds != null && !orgIds.isEmpty()) {
if (filter.includeWithoutProject()) {
query.addConditions(PROJECTS.ORG_ID.in(filter.orgIds()).or(PROCESS_QUEUE.PROJECT_ID.isNull()));
} else {
query.addConditions(PROJECTS.ORG_ID.in(filter.orgIds()));
}
}
if (filter.projectId() != null) {
if (filter.includeWithoutProject()) {
query.addConditions(PROCESS_QUEUE.PROJECT_ID.eq(filter.projectId()).or(PROCESS_QUEUE.PROJECT_ID.isNull()));
} else {
query.addConditions(PROCESS_QUEUE.PROJECT_ID.eq(filter.projectId()));
}
}
if (filter.repoId() != null) {
if (filter.includeWithoutProject()) {
query.addConditions(PROCESS_QUEUE.REPO_ID.eq(filter.repoId()).or(PROCESS_QUEUE.REPO_ID.isNull()));
} else {
query.addConditions(PROCESS_QUEUE.REPO_ID.eq(filter.repoId()));
}
}
if (filter.repoName() != null && filter.repoId() == null) {
SelectConditionStep<Record1<UUID>> repoIdSelect = select(REPOSITORIES.REPO_ID).from(REPOSITORIES).where(REPOSITORIES.REPO_NAME.startsWith(filter.repoName()));
if (filter.projectId() != null) {
repoIdSelect = repoIdSelect.and(REPOSITORIES.PROJECT_ID.eq(filter.projectId()));
}
query.addConditions(PROCESS_QUEUE.REPO_ID.in(repoIdSelect));
}
if (filter.initiator() != null) {
query.addConditions(USERS.USERNAME.startsWith(filter.initiator()));
}
if (filter.afterCreatedAt() != null) {
query.addConditions(PROCESS_QUEUE.CREATED_AT.greaterThan(filter.afterCreatedAt()));
}
if (filter.beforeCreatedAt() != null) {
query.addConditions(PROCESS_QUEUE.CREATED_AT.lessThan(filter.beforeCreatedAt()));
}
ProcessStatus status = filter.status();
if (status != null) {
query.addConditions(PROCESS_QUEUE.CURRENT_STATUS.eq(status.name()));
}
if (filter.parentId() != null) {
query.addConditions(PROCESS_QUEUE.PARENT_INSTANCE_ID.eq(filter.parentId()));
}
MetadataUtils.apply(query, PROCESS_QUEUE.META, filter.metaFilters());
filterByTags(query, filter.tags());
FilterUtils.applyDate(query, PROCESS_QUEUE.START_AT, filter.startAt());
FilterUtils.applyJson(query, PROCESS_QUEUE.REQUIREMENTS, filter.requirements());
Set<ProcessDataInclude> includes = filter.includes();
if (includes.contains(ProcessDataInclude.CHILDREN_IDS)) {
ProcessQueue pq = PROCESS_QUEUE.as("pq");
SelectConditionStep<Record1<UUID>> childIds = DSL.select(pq.INSTANCE_ID).from(pq).where(pq.PARENT_INSTANCE_ID.eq(PROCESS_QUEUE.INSTANCE_ID));
Field<UUID[]> childIdsField = DSL.field("array({0})", UUID[].class, childIds).as("children_ids");
query.addSelect(childIdsField);
}
if (includes.contains(ProcessDataInclude.CHECKPOINTS)) {
ProcessCheckpoints pc = PROCESS_CHECKPOINTS.as("pc");
SelectJoinStep<Record1<JSONB>> checkpoints = tx.select(function("to_jsonb", JSONB.class, function("array_agg", Object.class, jsonbStripNulls(jsonbBuildObject(inline("id"), pc.CHECKPOINT_ID, inline("name"), pc.CHECKPOINT_NAME, inline("correlationId"), pc.CORRELATION_ID, inline("createdAt"), toJsonDate(pc.CHECKPOINT_DATE)))))).from(pc);
if (key != null) {
checkpoints.where(pc.INSTANCE_ID.eq(key.getInstanceId()).and(pc.INSTANCE_CREATED_AT.eq(key.getCreatedAt())));
} else {
checkpoints.where(pc.INSTANCE_ID.eq(PROCESS_QUEUE.INSTANCE_ID).and(pc.INSTANCE_CREATED_AT.eq(PROCESS_QUEUE.CREATED_AT)));
}
query.addSelect(checkpoints.asField("checkpoints"));
}
if (includes.contains(ProcessDataInclude.CHECKPOINTS_HISTORY)) {
ProcessEvents pe = PROCESS_EVENTS.as("pe");
SelectJoinStep<Record1<JSONB>> history = tx.select(function("to_jsonb", JSONB.class, function("array_agg", Object.class, checkpointHistoryEntryToJsonb(pe)))).from(pe);
if (key != null) {
history.where(pe.INSTANCE_ID.eq(key.getInstanceId()).and(pe.INSTANCE_CREATED_AT.eq(key.getCreatedAt()).and(pe.EVENT_TYPE.eq(EventType.CHECKPOINT_RESTORE.name()))));
} else {
history.where(PROCESS_QUEUE.INSTANCE_ID.eq(pe.INSTANCE_ID).and(pe.INSTANCE_CREATED_AT.eq(PROCESS_QUEUE.CREATED_AT).and(pe.EVENT_TYPE.eq(EventType.CHECKPOINT_RESTORE.name()))));
}
query.addSelect(history.asField("checkpoints_history"));
}
if (includes.contains(ProcessDataInclude.STATUS_HISTORY)) {
ProcessEvents pe = PROCESS_EVENTS.as("pe");
SelectJoinStep<Record1<JSONB>> history = tx.select(function("to_jsonb", JSONB.class, function("array_agg", Object.class, statusHistoryEntryToJsonb(pe)))).from(pe);
if (key != null) {
history.where(pe.INSTANCE_ID.eq(key.getInstanceId()).and(pe.INSTANCE_CREATED_AT.eq(key.getCreatedAt()).and(pe.EVENT_TYPE.eq(EventType.PROCESS_STATUS.name()))));
} else {
history.where(PROCESS_QUEUE.INSTANCE_ID.eq(pe.INSTANCE_ID).and(pe.INSTANCE_CREATED_AT.eq(PROCESS_QUEUE.CREATED_AT).and(pe.EVENT_TYPE.eq(EventType.PROCESS_STATUS.name()))));
}
query.addSelect(history.asField("status_history"));
}
Integer limit = filter.limit();
if (limit != null && limit > 0) {
query.addLimit(limit);
}
Integer offset = filter.offset();
if (offset != null && offset > 0) {
query.addOffset(offset);
}
return query;
}
Aggregations