Repository: finos/waltz
Issue: Revert Maria Union fix

bringing in way too much on app-selector with groups (and probably elsewhere)

Changed files:
waltz-data/src/main/java/com/khartec/waltz/data/change_initiative/ChangeInitiativeIdSelectorFactory.java
waltz-data/src/main/java/com/khartec/waltz/data/measurable/MeasurableIdSelectorFactory.java
waltz-data/src/main/java/com/khartec/waltz/data/licence/LicenceIdSelectorFactory.java
waltz-data/src/main/java/com/khartec/waltz/data/application/ApplicationIdSelectorFactory.java
waltz-data/src/main/java/com/khartec/waltz/data/entity_hierarchy/AbstractIdSelectorFactory.java
waltz-data/src/main/java/com/khartec/waltz/data/change_initiative/ChangeInitiativeDao.java
waltz-data/src/main/java/com/khartec/waltz/data/application/ApplicationDao.java

diff --git a/waltz-data/src/main/java/com/khartec/waltz/data/application/ApplicationDao.java b/waltz-data/src/main/java/com/khartec/waltz/data/application/ApplicationDao.java
index d629081c3..778c85b8a 100644
--- a/waltz-data/src/main/java/com/khartec/waltz/data/application/ApplicationDao.java
+++ b/waltz-data/src/main/java/com/khartec/waltz/data/application/ApplicationDao.java
@@ -231,8 +231,7 @@ public class ApplicationDao {
 
 
     public List<Application> findByAppIdSelector(Select<Record1<Long>> selector) {
-        return dsl
-                .selectFrom(APPLICATION)
+        return dsl.selectFrom(APPLICATION)
                 .where(APPLICATION.ID.in(selector))
                 .fetch(TO_DOMAIN_MAPPER);
     }
diff --git a/waltz-data/src/main/java/com/khartec/waltz/data/application/ApplicationIdSelectorFactory.java b/waltz-data/src/main/java/com/khartec/waltz/data/application/ApplicationIdSelectorFactory.java
index 3d3c7a4b6..75b5ed492 100644
--- a/waltz-data/src/main/java/com/khartec/waltz/data/application/ApplicationIdSelectorFactory.java
+++ b/waltz-data/src/main/java/com/khartec/waltz/data/application/ApplicationIdSelectorFactory.java
@@ -81,10 +81,14 @@ public class ApplicationIdSelectorFactory implements Function<ApplicationIdSelec
                 return mkForAppGroup(options);
             case APPLICATION:
                 return mkForApplication(options);
+            case CHANGE_INITIATIVE:
+                return mkForEntityRelationship(options);
             case DATA_TYPE:
                 return mkForDataType(options);
             case FLOW_DIAGRAM:
                 return mkForFlowDiagram(options);
+            case LICENCE:
+                return mkForEntityRelationship(options);
             case MEASURABLE:
                 return mkForMeasurable(options);
             case SCENARIO:
@@ -97,9 +101,6 @@ public class ApplicationIdSelectorFactory implements Function<ApplicationIdSelec
                 return mkForPerson(options);
             case SOFTWARE:
                 return mkForSoftwarePackage(options);
-            case CHANGE_INITIATIVE: // drop thru
-            case LICENCE:
-                return mkForEntityRelationship(options);
             default:
                 throw new IllegalArgumentException("Cannot create selector for entity kind: " + ref.kind());
         }
@@ -172,10 +173,8 @@ public class ApplicationIdSelectorFactory implements Function<ApplicationIdSelec
                         .and(logicalFlow.ENTITY_LIFECYCLE_STATUS.ne(REMOVED.name())))
                         .and(applicationConditions);
 
-        // UNION like this to support maria (see #4267)
-        return DSL
-                .select(DSL.field("id", Long.class))
-                .from(sourceAppIds.unionAll(targetAppIds));
+        return sourceAppIds
+                .union(targetAppIds);
     }
 
 
@@ -238,10 +237,9 @@ public class ApplicationIdSelectorFactory implements Function<ApplicationIdSelec
                 .and(ENTITY_RELATIONSHIP.ID_A.eq(options.entityReference().id()))
                 .and(applicationConditions);
 
-        // UNION like this to support maria (see #4267)
-        return DSL
-                .select(DSL.field("id", Long.class))
-                .from(appToEntity.unionAll(entityToApp));
+
+        return appToEntity
+                .union(entityToApp);
     }
 
 
@@ -292,15 +290,10 @@ public class ApplicationIdSelectorFactory implements Function<ApplicationIdSelec
                 .from(APPLICATION_GROUP_ENTRY)
                 .where(APPLICATION_GROUP_ENTRY.GROUP_ID.eq(options.entityReference().id()));
 
-        // UNION like this to support maria (see #4267)
-        SelectJoinStep<Record1<Long>> appIds = DSL
-                .select(DSL.field("id", Long.class))
-                .from(directApps.unionAll(applicationIdsFromAssociatedOrgUnits));
-
         return DSL
                 .select(APPLICATION.ID)
                 .from(APPLICATION)
-                .where(APPLICATION.ID.in(appIds))
+                .where(APPLICATION.ID.in(directApps.unionAll(applicationIdsFromAssociatedOrgUnits)))
                 .and(applicationConditions);
     }
 
@@ -390,10 +383,10 @@ public class ApplicationIdSelectorFactory implements Function<ApplicationIdSelec
                         .and(applicationConditions),
                 LOGICAL_FLOW.TARGET_ENTITY_ID);
 
-        // UNION like this to support maria (see #4267)
         return DSL
-                .select(DSL.field("id", Long.class))
-                .from(sources.unionAll(targets));
+                .selectDistinct(appId)
+                .from(sources)
+                .union(targets);
     }
 
 
diff --git a/waltz-data/src/main/java/com/khartec/waltz/data/change_initiative/ChangeInitiativeDao.java b/waltz-data/src/main/java/com/khartec/waltz/data/change_initiative/ChangeInitiativeDao.java
index 14203b32d..dd16e95b0 100644
--- a/waltz-data/src/main/java/com/khartec/waltz/data/change_initiative/ChangeInitiativeDao.java
+++ b/waltz-data/src/main/java/com/khartec/waltz/data/change_initiative/ChangeInitiativeDao.java
@@ -111,10 +111,7 @@ public class ChangeInitiativeDao implements FindEntityReferencesByIdSelector {
                 .where(ENTITY_HIERARCHY.ANCESTOR_ID.in(selector)
                         .and(ENTITY_HIERARCHY.KIND.eq(EntityKind.CHANGE_INITIATIVE.name())));
 
-        // UNION like this to support maria (see #4267)
-        SelectOrderByStep<Record1<Long>> hierarchySelector = DSL
-                .select(DSL.field("id", Long.class))
-                .from(descendants.unionAll(ancestors));
+        SelectOrderByStep<Record1<Long>> hierarchySelector = descendants.unionAll(ancestors);
 
         return findForSelector(hierarchySelector);
     }
diff --git a/waltz-data/src/main/java/com/khartec/waltz/data/change_initiative/ChangeInitiativeIdSelectorFactory.java b/waltz-data/src/main/java/com/khartec/waltz/data/change_initiative/ChangeInitiativeIdSelectorFactory.java
index f045e3df0..ae868c86a 100644
--- a/waltz-data/src/main/java/com/khartec/waltz/data/change_initiative/ChangeInitiativeIdSelectorFactory.java
+++ b/waltz-data/src/main/java/com/khartec/waltz/data/change_initiative/ChangeInitiativeIdSelectorFactory.java
@@ -27,7 +27,6 @@ import com.khartec.waltz.model.IdSelectionOptions;
 import org.jooq.Record1;
 import org.jooq.Select;
 import org.jooq.SelectConditionStep;
-import org.jooq.SelectJoinStep;
 import org.jooq.impl.DSL;
 
 import static com.khartec.waltz.data.SelectorUtilities.ensureScopeIsExact;
@@ -131,10 +130,7 @@ public class ChangeInitiativeIdSelectorFactory extends AbstractIdSelectorFactory
                 .and(ENTITY_RELATIONSHIP.KIND_A.eq(ref.kind().name()))
                 .and(ENTITY_RELATIONSHIP.ID_A.eq(ref.id()));
 
-        // UNION like this to support maria (see #4267)
-        return DSL
-                .select(DSL.field("id", Long.class))
-                .from(aToB.unionAll(bToA));
+        return aToB.union(bToA);
     }
 
 }
diff --git a/waltz-data/src/main/java/com/khartec/waltz/data/entity_hierarchy/AbstractIdSelectorFactory.java b/waltz-data/src/main/java/com/khartec/waltz/data/entity_hierarchy/AbstractIdSelectorFactory.java
index 82c303858..c728205cf 100644
--- a/waltz-data/src/main/java/com/khartec/waltz/data/entity_hierarchy/AbstractIdSelectorFactory.java
+++ b/waltz-data/src/main/java/com/khartec/waltz/data/entity_hierarchy/AbstractIdSelectorFactory.java
@@ -64,15 +64,13 @@ public abstract class AbstractIdSelectorFactory implements IdSelectorFactory {
                 selector = DSL.select(DSL.val(options.entityReference().id()));
                 break;
             case CHILDREN:
-                selector = DSL
-                        .select(ENTITY_HIERARCHY.ID)
+                selector = DSL.select(ENTITY_HIERARCHY.ID)
                         .from(ENTITY_HIERARCHY)
                         .where(ENTITY_HIERARCHY.ANCESTOR_ID.eq(options.entityReference().id()))
                         .and(ENTITY_HIERARCHY.KIND.eq(entityKind.name()));
                 break;
             case PARENTS:
-                selector = DSL
-                        .select(ENTITY_HIERARCHY.ANCESTOR_ID)
+                selector = DSL.select(ENTITY_HIERARCHY.ANCESTOR_ID)
                         .from(ENTITY_HIERARCHY)
                         .where(ENTITY_HIERARCHY.ID.eq(options.entityReference().id()))
                         .and(ENTITY_HIERARCHY.KIND.eq(entityKind.name()));
diff --git a/waltz-data/src/main/java/com/khartec/waltz/data/licence/LicenceIdSelectorFactory.java b/waltz-data/src/main/java/com/khartec/waltz/data/licence/LicenceIdSelectorFactory.java
index 0595c38e2..f7589e8e7 100644
--- a/waltz-data/src/main/java/com/khartec/waltz/data/licence/LicenceIdSelectorFactory.java
+++ b/waltz-data/src/main/java/com/khartec/waltz/data/licence/LicenceIdSelectorFactory.java
@@ -83,10 +83,8 @@ public class LicenceIdSelectorFactory extends AbstractIdSelectorFactory {
                 .and(ENTITY_RELATIONSHIP.KIND_A.eq(EntityKind.APPLICATION.name()))
                 .and(ENTITY_RELATIONSHIP.ID_A.in(appSelector));
 
-        // UNION like this to support maria (see #4267)
-        return DSL
-                .select(DSL.field("id", Long.class))
-                .from(aToB.unionAll(bToA));
+        return aToB.unionAll(bToA);
+
     }
 
 
@@ -111,9 +109,6 @@ public class LicenceIdSelectorFactory extends AbstractIdSelectorFactory {
                 .and(ENTITY_RELATIONSHIP.KIND_A.eq(ref.kind().name()))
                 .and(ENTITY_RELATIONSHIP.ID_A.eq(ref.id()));
 
-        // UNION like this to support maria (see #4267)
-        return DSL
-                .select(DSL.field("id", Long.class))
-                .from(aToB.unionAll(bToA));
+        return aToB.union(bToA);
     }
 }
diff --git a/waltz-data/src/main/java/com/khartec/waltz/data/measurable/MeasurableIdSelectorFactory.java b/waltz-data/src/main/java/com/khartec/waltz/data/measurable/MeasurableIdSelectorFactory.java
index ea9249d3c..2497fc8bb 100644
--- a/waltz-data/src/main/java/com/khartec/waltz/data/measurable/MeasurableIdSelectorFactory.java
+++ b/waltz-data/src/main/java/com/khartec/waltz/data/measurable/MeasurableIdSelectorFactory.java
@@ -187,10 +187,8 @@ public class MeasurableIdSelectorFactory implements IdSelectorFactory {
                 .where(FLOW_DIAGRAM_ENTITY.ENTITY_KIND.eq(EntityKind.MEASURABLE.name()))
                 .and(FLOW_DIAGRAM_ENTITY.DIAGRAM_ID.eq(diagramId));
 
-        // UNION like this to support maria (see #4267)
-        return DSL
-                .select(DSL.field("id", Long.class))
-                .from(viaAppRatings.unionAll(viaDirectRelationship));
+        return viaAppRatings.union(viaDirectRelationship);
+
     }