|
25 | 25 | import org.finos.waltz.model.EntityReference; |
26 | 26 | import org.finos.waltz.model.ImmutableEntityReference; |
27 | 27 | import org.finos.waltz.model.entity_relationship.*; |
| 28 | +import org.finos.waltz.schema.Tables; |
28 | 29 | import org.finos.waltz.schema.tables.records.EntityRelationshipRecord; |
29 | 30 | import org.jooq.*; |
30 | 31 | import org.jooq.impl.DSL; |
| 32 | +import org.slf4j.Logger; |
| 33 | +import org.slf4j.LoggerFactory; |
31 | 34 | import org.springframework.beans.factory.annotation.Autowired; |
32 | 35 | import org.springframework.stereotype.Repository; |
33 | 36 |
|
|
48 | 51 | @Repository |
49 | 52 | public class EntityRelationshipDao { |
50 | 53 |
|
| 54 | + private static final Logger LOG = LoggerFactory.getLogger(EntityRelationshipDao.class); |
51 | 55 |
|
52 | 56 | private static final List<EntityKind> POSSIBLE_ENTITIES = newArrayList( |
53 | 57 | EntityKind.APPLICATION, |
@@ -319,4 +323,103 @@ public int removeAll(long groupId, List<Long> changeInitiativeIds) { |
319 | 323 | .and(ENTITY_RELATIONSHIP.ID_B.in(changeInitiativeIds)) |
320 | 324 | .execute(); |
321 | 325 | } |
| 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 | + } |
322 | 425 | } |
0 commit comments