Skip to content

Commit

Permalink
Merge pull request #6633 from deutschebank/db-contrib/waltz-6608-meas…
Browse files Browse the repository at this point in the history
…ujrable-merge

Db contrib/waltz 6608 measujrable merge
  • Loading branch information
davidwatkins73 authored May 26, 2023
2 parents 9b6dfe1 + 846a9b4 commit 2574aa4
Show file tree
Hide file tree
Showing 23 changed files with 1,097 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ public List<AssessmentRating> findByGenericSelector(GenericSelector genericSelec
}


public int deleteByGenericSelector(GenericSelector genericSelector) {
return dsl
.deleteFrom(ar)
.where(ar.ENTITY_KIND.eq(genericSelector.kind().name()))
.and(ar.ENTITY_ID.in(genericSelector.selector()))
.execute();
}


public boolean store(SaveAssessmentRatingCommand command) {
checkNotNull(command, "command cannot be null");
AssessmentRatingRecord record = COMMAND_TO_RECORD_MAPPER.apply(command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.finos.waltz.data.entity_named_note;

import org.finos.waltz.data.GenericSelector;
import org.finos.waltz.schema.tables.records.EntityNamedNoteRecord;
import org.finos.waltz.model.EntityKind;
import org.finos.waltz.model.EntityReference;
Expand Down Expand Up @@ -138,4 +139,12 @@ public Set<EntityNamedNote> findByNoteTypeExtIdAndEntityReference(String noteTyp
.and(ENTITY_NAMED_NOTE.ENTITY_ID.eq(entityReference.id()))
.fetchSet(TO_DOMAIN_MAPPER);
}

public int deleteByParentSelector(GenericSelector selector) {
return dsl
.deleteFrom(ENTITY_NAMED_NOTE)
.where(ENTITY_NAMED_NOTE.ENTITY_ID.in(selector.selector())
.and(ENTITY_NAMED_NOTE.ENTITY_KIND.eq(selector.kind().name())))
.execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
import org.finos.waltz.model.EntityReference;
import org.finos.waltz.model.ImmutableEntityReference;
import org.finos.waltz.model.entity_relationship.*;
import org.finos.waltz.schema.Tables;
import org.finos.waltz.schema.tables.records.EntityRelationshipRecord;
import org.jooq.*;
import org.jooq.impl.DSL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

Expand All @@ -48,6 +51,7 @@
@Repository
public class EntityRelationshipDao {

private static final Logger LOG = LoggerFactory.getLogger(EntityRelationshipDao.class);

private static final List<EntityKind> POSSIBLE_ENTITIES = newArrayList(
EntityKind.APPLICATION,
Expand Down Expand Up @@ -319,4 +323,103 @@ public int removeAll(long groupId, List<Long> changeInitiativeIds) {
.and(ENTITY_RELATIONSHIP.ID_B.in(changeInitiativeIds))
.execute();
}


public void migrateEntityRelationships(EntityReference sourceReference, EntityReference targetReference, String userId) {

dsl.transaction(ctx -> {

DSLContext tx = ctx.dsl();

LOG.info("Migrating entity relationships from source: {}/{} to target: {}/{}",
sourceReference.kind().prettyName(),
sourceReference.id(),
targetReference.kind().prettyName(),
targetReference.id());

Condition measurableIsA = Tables.ENTITY_RELATIONSHIP.ID_A.eq(sourceReference.id()).and(Tables.ENTITY_RELATIONSHIP.KIND_A.eq(sourceReference.kind().name()));
Condition measurableIsB = Tables.ENTITY_RELATIONSHIP.ID_B.eq(sourceReference.id()).and(Tables.ENTITY_RELATIONSHIP.KIND_B.eq(sourceReference.kind().name()));

SelectOrderByStep<Record3<Long, String, String>> allowedKindAUpdates = selectKindARelationshipsThatCanBeAdded(sourceReference, targetReference);

int kindARelsUpdated = tx
.update(Tables.ENTITY_RELATIONSHIP)
.set(Tables.ENTITY_RELATIONSHIP.ID_A, targetReference.id())
.set(Tables.ENTITY_RELATIONSHIP.LAST_UPDATED_AT, DateTimeUtilities.nowUtcTimestamp())
.set(Tables.ENTITY_RELATIONSHIP.LAST_UPDATED_BY, userId)
.from(allowedKindAUpdates)
.where(measurableIsA)
.and(Tables.ENTITY_RELATIONSHIP.KIND_B.eq(allowedKindAUpdates.field(Tables.ENTITY_RELATIONSHIP.KIND_B))
.and(Tables.ENTITY_RELATIONSHIP.ID_B.eq(allowedKindAUpdates.field(Tables.ENTITY_RELATIONSHIP.ID_B))
.and(Tables.ENTITY_RELATIONSHIP.RELATIONSHIP.eq(allowedKindAUpdates.field(Tables.ENTITY_RELATIONSHIP.RELATIONSHIP)))))
.execute();

SelectOrderByStep<Record3<Long, String, String>> allowedKindBUpdates = selectKindBRelationshipsThatCanBeAdded(sourceReference, targetReference);

int kindBRelsUpdated = tx
.update(Tables.ENTITY_RELATIONSHIP)
.set(Tables.ENTITY_RELATIONSHIP.ID_B, targetReference.id())
.set(Tables.ENTITY_RELATIONSHIP.LAST_UPDATED_AT, DateTimeUtilities.nowUtcTimestamp())
.set(Tables.ENTITY_RELATIONSHIP.LAST_UPDATED_BY, userId)
.from(allowedKindBUpdates)
.where(measurableIsB)
.and(Tables.ENTITY_RELATIONSHIP.KIND_A.eq(allowedKindBUpdates.field(Tables.ENTITY_RELATIONSHIP.KIND_A))
.and(Tables.ENTITY_RELATIONSHIP.ID_A.eq(allowedKindBUpdates.field(Tables.ENTITY_RELATIONSHIP.ID_A))
.and(Tables.ENTITY_RELATIONSHIP.RELATIONSHIP.eq(allowedKindBUpdates.field(Tables.ENTITY_RELATIONSHIP.RELATIONSHIP)))))
.execute();

int entityRelsRemoved = tx
.deleteFrom(Tables.ENTITY_RELATIONSHIP)
.where(measurableIsA.or(measurableIsB))
.execute();

LOG.info("Migrated {} relationships from source: {}/{} to target: {}/{}",
kindARelsUpdated + kindBRelsUpdated,
sourceReference.kind().prettyName(),
sourceReference.id(),
targetReference.kind().prettyName(),
targetReference.id());

LOG.info("Removed {} relationships that could not be migrated from source: {}/{} to target: {}/{} as a relationship already exists",
entityRelsRemoved,
sourceReference.kind().prettyName(),
sourceReference.id(),
targetReference.kind().prettyName(),
targetReference.id());
});
}

private SelectOrderByStep<Record3<Long, String, String>> selectKindARelationshipsThatCanBeAdded(EntityReference source, EntityReference target) {

SelectConditionStep<Record3<Long, String, String>> targets = DSL
.select(Tables.ENTITY_RELATIONSHIP.ID_B, Tables.ENTITY_RELATIONSHIP.KIND_B, Tables.ENTITY_RELATIONSHIP.RELATIONSHIP)
.from(Tables.ENTITY_RELATIONSHIP)
.where(Tables.ENTITY_RELATIONSHIP.ID_A.eq(target.id())
.and(Tables.ENTITY_RELATIONSHIP.KIND_A.eq(target.kind().name())));

SelectConditionStep<Record3<Long, String, String>> migrations = DSL
.select(Tables.ENTITY_RELATIONSHIP.ID_B, Tables.ENTITY_RELATIONSHIP.KIND_B, Tables.ENTITY_RELATIONSHIP.RELATIONSHIP)
.from(Tables.ENTITY_RELATIONSHIP)
.where(Tables.ENTITY_RELATIONSHIP.ID_A.eq(source.id())
.and(Tables.ENTITY_RELATIONSHIP.KIND_A.eq(source.kind().name())));

return migrations.except(targets);
}

private SelectOrderByStep<Record3<Long, String, String>> selectKindBRelationshipsThatCanBeAdded(EntityReference source, EntityReference target) {

SelectConditionStep<Record3<Long, String, String>> targets = DSL
.select(Tables.ENTITY_RELATIONSHIP.ID_A, Tables.ENTITY_RELATIONSHIP.KIND_A, Tables.ENTITY_RELATIONSHIP.RELATIONSHIP)
.from(Tables.ENTITY_RELATIONSHIP)
.where(Tables.ENTITY_RELATIONSHIP.ID_B.eq(target.id())
.and(Tables.ENTITY_RELATIONSHIP.KIND_B.eq(target.kind().name())));

SelectConditionStep<Record3<Long, String, String>> migrations = DSL
.select(Tables.ENTITY_RELATIONSHIP.ID_A, Tables.ENTITY_RELATIONSHIP.KIND_A, Tables.ENTITY_RELATIONSHIP.RELATIONSHIP)
.from(Tables.ENTITY_RELATIONSHIP)
.where(Tables.ENTITY_RELATIONSHIP.ID_B.eq(source.id())
.and(Tables.ENTITY_RELATIONSHIP.KIND_B.eq(source.kind().name())));

return migrations.except(targets);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
package org.finos.waltz.data.measurable;


import org.finos.waltz.schema.tables.records.MeasurableRecord;
import org.finos.waltz.common.DateTimeUtilities;
import org.finos.waltz.data.FindEntityReferencesByIdSelector;
import org.finos.waltz.model.EntityKind;
import org.finos.waltz.model.EntityLifecycleStatus;
import org.finos.waltz.model.EntityReference;
import org.finos.waltz.model.measurable.ImmutableMeasurable;
import org.finos.waltz.model.measurable.Measurable;
import org.finos.waltz.schema.tables.records.MeasurableRecord;
import org.jooq.*;
import org.jooq.impl.DSL;
import org.slf4j.Logger;
Expand All @@ -41,15 +41,15 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import static org.finos.waltz.data.JooqUtilities.summarizeResults;
import static org.finos.waltz.schema.Tables.*;
import static org.finos.waltz.schema.tables.EntityHierarchy.ENTITY_HIERARCHY;
import static org.finos.waltz.schema.tables.Measurable.MEASURABLE;
import static java.util.Optional.ofNullable;
import static org.finos.waltz.common.Checks.checkNotNull;
import static org.finos.waltz.common.EnumUtilities.readEnum;
import static org.finos.waltz.common.StringUtilities.mkSafe;
import static org.finos.waltz.data.JooqUtilities.TO_ENTITY_REFERENCE;
import static org.finos.waltz.data.JooqUtilities.summarizeResults;
import static org.finos.waltz.schema.Tables.*;
import static org.finos.waltz.schema.tables.EntityHierarchy.ENTITY_HIERARCHY;
import static org.finos.waltz.schema.tables.Measurable.MEASURABLE;


@Repository
Expand Down Expand Up @@ -229,6 +229,31 @@ public boolean updateParentId(Long measurableId, Long destinationId, String user
.execute() == 1;
}

public boolean moveChildren(Long measurableId, Long targetId, String userId) {

if (targetId == null) {
throw new IllegalArgumentException("Cannot move children without specifying a new target");
}

LOG.info("Moving children from measurable: {} to {}",
measurableId,
targetId);

Select<? extends Record1<String>> destinationExtId = DSL
.select(MEASURABLE.EXTERNAL_ID)
.from(MEASURABLE)
.where(MEASURABLE.ID.eq(targetId));

return dsl
.update(MEASURABLE)
.set(MEASURABLE.PARENT_ID, targetId)
.set(MEASURABLE.EXTERNAL_PARENT_ID, destinationExtId)
.set(MEASURABLE.LAST_UPDATED_AT, DateTimeUtilities.nowUtcTimestamp())
.set(MEASURABLE.LAST_UPDATED_BY, userId)
.where(MEASURABLE.PARENT_ID.eq(measurableId))
.execute() == 1;
}


public List<Measurable> findByCategoryId(Long categoryId) {
return dsl
Expand Down
Loading

0 comments on commit 2574aa4

Please sign in to comment.