Skip to content

Commit 846a9b4

Browse files
committed
Merge pull request #277 in WALTZ/waltz from WALTZ/waltz-jws:CTCTOWALTZ-2729-measurable-merge-6608 to db-feature/waltz-6608-measujrable-merge
* commit 'dea987dd4586432f79ff605720b95ef35791cd13': Add errors checking on server side Add errors checking on server side Add errors checking on server side Works to merge all rating info where possible Works to merge all rating info where possible Works to merge all rating info where possible
2 parents 9b6dfe1 + dea987d commit 846a9b4

23 files changed

Lines changed: 1097 additions & 188 deletions

File tree

waltz-data/src/main/java/org/finos/waltz/data/assessment_rating/AssessmentRatingDao.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,15 @@ public List<AssessmentRating> findByGenericSelector(GenericSelector genericSelec
228228
}
229229

230230

231+
public int deleteByGenericSelector(GenericSelector genericSelector) {
232+
return dsl
233+
.deleteFrom(ar)
234+
.where(ar.ENTITY_KIND.eq(genericSelector.kind().name()))
235+
.and(ar.ENTITY_ID.in(genericSelector.selector()))
236+
.execute();
237+
}
238+
239+
231240
public boolean store(SaveAssessmentRatingCommand command) {
232241
checkNotNull(command, "command cannot be null");
233242
AssessmentRatingRecord record = COMMAND_TO_RECORD_MAPPER.apply(command);

waltz-data/src/main/java/org/finos/waltz/data/entity_named_note/EntityNamedNoteDao.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package org.finos.waltz.data.entity_named_note;
2020

21+
import org.finos.waltz.data.GenericSelector;
2122
import org.finos.waltz.schema.tables.records.EntityNamedNoteRecord;
2223
import org.finos.waltz.model.EntityKind;
2324
import org.finos.waltz.model.EntityReference;
@@ -138,4 +139,12 @@ public Set<EntityNamedNote> findByNoteTypeExtIdAndEntityReference(String noteTyp
138139
.and(ENTITY_NAMED_NOTE.ENTITY_ID.eq(entityReference.id()))
139140
.fetchSet(TO_DOMAIN_MAPPER);
140141
}
142+
143+
public int deleteByParentSelector(GenericSelector selector) {
144+
return dsl
145+
.deleteFrom(ENTITY_NAMED_NOTE)
146+
.where(ENTITY_NAMED_NOTE.ENTITY_ID.in(selector.selector())
147+
.and(ENTITY_NAMED_NOTE.ENTITY_KIND.eq(selector.kind().name())))
148+
.execute();
149+
}
141150
}

waltz-data/src/main/java/org/finos/waltz/data/entity_relationship/EntityRelationshipDao.java

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@
2525
import org.finos.waltz.model.EntityReference;
2626
import org.finos.waltz.model.ImmutableEntityReference;
2727
import org.finos.waltz.model.entity_relationship.*;
28+
import org.finos.waltz.schema.Tables;
2829
import org.finos.waltz.schema.tables.records.EntityRelationshipRecord;
2930
import org.jooq.*;
3031
import org.jooq.impl.DSL;
32+
import org.slf4j.Logger;
33+
import org.slf4j.LoggerFactory;
3134
import org.springframework.beans.factory.annotation.Autowired;
3235
import org.springframework.stereotype.Repository;
3336

@@ -48,6 +51,7 @@
4851
@Repository
4952
public class EntityRelationshipDao {
5053

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

5256
private static final List<EntityKind> POSSIBLE_ENTITIES = newArrayList(
5357
EntityKind.APPLICATION,
@@ -319,4 +323,103 @@ public int removeAll(long groupId, List<Long> changeInitiativeIds) {
319323
.and(ENTITY_RELATIONSHIP.ID_B.in(changeInitiativeIds))
320324
.execute();
321325
}
326+
327+
328+
public void migrateEntityRelationships(EntityReference sourceReference, EntityReference targetReference, String userId) {
329+
330+
dsl.transaction(ctx -> {
331+
332+
DSLContext tx = ctx.dsl();
333+
334+
LOG.info("Migrating entity relationships from source: {}/{} to target: {}/{}",
335+
sourceReference.kind().prettyName(),
336+
sourceReference.id(),
337+
targetReference.kind().prettyName(),
338+
targetReference.id());
339+
340+
Condition measurableIsA = Tables.ENTITY_RELATIONSHIP.ID_A.eq(sourceReference.id()).and(Tables.ENTITY_RELATIONSHIP.KIND_A.eq(sourceReference.kind().name()));
341+
Condition measurableIsB = Tables.ENTITY_RELATIONSHIP.ID_B.eq(sourceReference.id()).and(Tables.ENTITY_RELATIONSHIP.KIND_B.eq(sourceReference.kind().name()));
342+
343+
SelectOrderByStep<Record3<Long, String, String>> allowedKindAUpdates = selectKindARelationshipsThatCanBeAdded(sourceReference, targetReference);
344+
345+
int kindARelsUpdated = tx
346+
.update(Tables.ENTITY_RELATIONSHIP)
347+
.set(Tables.ENTITY_RELATIONSHIP.ID_A, targetReference.id())
348+
.set(Tables.ENTITY_RELATIONSHIP.LAST_UPDATED_AT, DateTimeUtilities.nowUtcTimestamp())
349+
.set(Tables.ENTITY_RELATIONSHIP.LAST_UPDATED_BY, userId)
350+
.from(allowedKindAUpdates)
351+
.where(measurableIsA)
352+
.and(Tables.ENTITY_RELATIONSHIP.KIND_B.eq(allowedKindAUpdates.field(Tables.ENTITY_RELATIONSHIP.KIND_B))
353+
.and(Tables.ENTITY_RELATIONSHIP.ID_B.eq(allowedKindAUpdates.field(Tables.ENTITY_RELATIONSHIP.ID_B))
354+
.and(Tables.ENTITY_RELATIONSHIP.RELATIONSHIP.eq(allowedKindAUpdates.field(Tables.ENTITY_RELATIONSHIP.RELATIONSHIP)))))
355+
.execute();
356+
357+
SelectOrderByStep<Record3<Long, String, String>> allowedKindBUpdates = selectKindBRelationshipsThatCanBeAdded(sourceReference, targetReference);
358+
359+
int kindBRelsUpdated = tx
360+
.update(Tables.ENTITY_RELATIONSHIP)
361+
.set(Tables.ENTITY_RELATIONSHIP.ID_B, targetReference.id())
362+
.set(Tables.ENTITY_RELATIONSHIP.LAST_UPDATED_AT, DateTimeUtilities.nowUtcTimestamp())
363+
.set(Tables.ENTITY_RELATIONSHIP.LAST_UPDATED_BY, userId)
364+
.from(allowedKindBUpdates)
365+
.where(measurableIsB)
366+
.and(Tables.ENTITY_RELATIONSHIP.KIND_A.eq(allowedKindBUpdates.field(Tables.ENTITY_RELATIONSHIP.KIND_A))
367+
.and(Tables.ENTITY_RELATIONSHIP.ID_A.eq(allowedKindBUpdates.field(Tables.ENTITY_RELATIONSHIP.ID_A))
368+
.and(Tables.ENTITY_RELATIONSHIP.RELATIONSHIP.eq(allowedKindBUpdates.field(Tables.ENTITY_RELATIONSHIP.RELATIONSHIP)))))
369+
.execute();
370+
371+
int entityRelsRemoved = tx
372+
.deleteFrom(Tables.ENTITY_RELATIONSHIP)
373+
.where(measurableIsA.or(measurableIsB))
374+
.execute();
375+
376+
LOG.info("Migrated {} relationships from source: {}/{} to target: {}/{}",
377+
kindARelsUpdated + kindBRelsUpdated,
378+
sourceReference.kind().prettyName(),
379+
sourceReference.id(),
380+
targetReference.kind().prettyName(),
381+
targetReference.id());
382+
383+
LOG.info("Removed {} relationships that could not be migrated from source: {}/{} to target: {}/{} as a relationship already exists",
384+
entityRelsRemoved,
385+
sourceReference.kind().prettyName(),
386+
sourceReference.id(),
387+
targetReference.kind().prettyName(),
388+
targetReference.id());
389+
});
390+
}
391+
392+
private SelectOrderByStep<Record3<Long, String, String>> selectKindARelationshipsThatCanBeAdded(EntityReference source, EntityReference target) {
393+
394+
SelectConditionStep<Record3<Long, String, String>> targets = DSL
395+
.select(Tables.ENTITY_RELATIONSHIP.ID_B, Tables.ENTITY_RELATIONSHIP.KIND_B, Tables.ENTITY_RELATIONSHIP.RELATIONSHIP)
396+
.from(Tables.ENTITY_RELATIONSHIP)
397+
.where(Tables.ENTITY_RELATIONSHIP.ID_A.eq(target.id())
398+
.and(Tables.ENTITY_RELATIONSHIP.KIND_A.eq(target.kind().name())));
399+
400+
SelectConditionStep<Record3<Long, String, String>> migrations = DSL
401+
.select(Tables.ENTITY_RELATIONSHIP.ID_B, Tables.ENTITY_RELATIONSHIP.KIND_B, Tables.ENTITY_RELATIONSHIP.RELATIONSHIP)
402+
.from(Tables.ENTITY_RELATIONSHIP)
403+
.where(Tables.ENTITY_RELATIONSHIP.ID_A.eq(source.id())
404+
.and(Tables.ENTITY_RELATIONSHIP.KIND_A.eq(source.kind().name())));
405+
406+
return migrations.except(targets);
407+
}
408+
409+
private SelectOrderByStep<Record3<Long, String, String>> selectKindBRelationshipsThatCanBeAdded(EntityReference source, EntityReference target) {
410+
411+
SelectConditionStep<Record3<Long, String, String>> targets = DSL
412+
.select(Tables.ENTITY_RELATIONSHIP.ID_A, Tables.ENTITY_RELATIONSHIP.KIND_A, Tables.ENTITY_RELATIONSHIP.RELATIONSHIP)
413+
.from(Tables.ENTITY_RELATIONSHIP)
414+
.where(Tables.ENTITY_RELATIONSHIP.ID_B.eq(target.id())
415+
.and(Tables.ENTITY_RELATIONSHIP.KIND_B.eq(target.kind().name())));
416+
417+
SelectConditionStep<Record3<Long, String, String>> migrations = DSL
418+
.select(Tables.ENTITY_RELATIONSHIP.ID_A, Tables.ENTITY_RELATIONSHIP.KIND_A, Tables.ENTITY_RELATIONSHIP.RELATIONSHIP)
419+
.from(Tables.ENTITY_RELATIONSHIP)
420+
.where(Tables.ENTITY_RELATIONSHIP.ID_B.eq(source.id())
421+
.and(Tables.ENTITY_RELATIONSHIP.KIND_B.eq(source.kind().name())));
422+
423+
return migrations.except(targets);
424+
}
322425
}

waltz-data/src/main/java/org/finos/waltz/data/measurable/MeasurableDao.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
package org.finos.waltz.data.measurable;
2020

2121

22-
import org.finos.waltz.schema.tables.records.MeasurableRecord;
2322
import org.finos.waltz.common.DateTimeUtilities;
2423
import org.finos.waltz.data.FindEntityReferencesByIdSelector;
2524
import org.finos.waltz.model.EntityKind;
2625
import org.finos.waltz.model.EntityLifecycleStatus;
2726
import org.finos.waltz.model.EntityReference;
2827
import org.finos.waltz.model.measurable.ImmutableMeasurable;
2928
import org.finos.waltz.model.measurable.Measurable;
29+
import org.finos.waltz.schema.tables.records.MeasurableRecord;
3030
import org.jooq.*;
3131
import org.jooq.impl.DSL;
3232
import org.slf4j.Logger;
@@ -41,15 +41,15 @@
4141
import java.util.concurrent.atomic.AtomicInteger;
4242
import java.util.stream.Collectors;
4343

44-
import static org.finos.waltz.data.JooqUtilities.summarizeResults;
45-
import static org.finos.waltz.schema.Tables.*;
46-
import static org.finos.waltz.schema.tables.EntityHierarchy.ENTITY_HIERARCHY;
47-
import static org.finos.waltz.schema.tables.Measurable.MEASURABLE;
4844
import static java.util.Optional.ofNullable;
4945
import static org.finos.waltz.common.Checks.checkNotNull;
5046
import static org.finos.waltz.common.EnumUtilities.readEnum;
5147
import static org.finos.waltz.common.StringUtilities.mkSafe;
5248
import static org.finos.waltz.data.JooqUtilities.TO_ENTITY_REFERENCE;
49+
import static org.finos.waltz.data.JooqUtilities.summarizeResults;
50+
import static org.finos.waltz.schema.Tables.*;
51+
import static org.finos.waltz.schema.tables.EntityHierarchy.ENTITY_HIERARCHY;
52+
import static org.finos.waltz.schema.tables.Measurable.MEASURABLE;
5353

5454

5555
@Repository
@@ -229,6 +229,31 @@ public boolean updateParentId(Long measurableId, Long destinationId, String user
229229
.execute() == 1;
230230
}
231231

232+
public boolean moveChildren(Long measurableId, Long targetId, String userId) {
233+
234+
if (targetId == null) {
235+
throw new IllegalArgumentException("Cannot move children without specifying a new target");
236+
}
237+
238+
LOG.info("Moving children from measurable: {} to {}",
239+
measurableId,
240+
targetId);
241+
242+
Select<? extends Record1<String>> destinationExtId = DSL
243+
.select(MEASURABLE.EXTERNAL_ID)
244+
.from(MEASURABLE)
245+
.where(MEASURABLE.ID.eq(targetId));
246+
247+
return dsl
248+
.update(MEASURABLE)
249+
.set(MEASURABLE.PARENT_ID, targetId)
250+
.set(MEASURABLE.EXTERNAL_PARENT_ID, destinationExtId)
251+
.set(MEASURABLE.LAST_UPDATED_AT, DateTimeUtilities.nowUtcTimestamp())
252+
.set(MEASURABLE.LAST_UPDATED_BY, userId)
253+
.where(MEASURABLE.PARENT_ID.eq(measurableId))
254+
.execute() == 1;
255+
}
256+
232257

233258
public List<Measurable> findByCategoryId(Long categoryId) {
234259
return dsl

0 commit comments

Comments
 (0)