diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java index 1e60eaf4953..beacb3c4366 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java @@ -48,6 +48,7 @@ import static org.openmetadata.service.util.EntityUtil.fieldAdded; import static org.openmetadata.service.util.EntityUtil.fieldDeleted; import static org.openmetadata.service.util.EntityUtil.fieldUpdated; import static org.openmetadata.service.util.EntityUtil.getColumnField; +import static org.openmetadata.service.util.EntityUtil.getEntityReferences; import static org.openmetadata.service.util.EntityUtil.getExtensionField; import static org.openmetadata.service.util.EntityUtil.nextMajorVersion; import static org.openmetadata.service.util.EntityUtil.nextVersion; @@ -81,16 +82,17 @@ import java.util.concurrent.TimeUnit; import java.util.function.BiPredicate; import java.util.function.Function; import java.util.stream.Collectors; -import javax.annotation.CheckForNull; import javax.json.JsonPatch; import javax.validation.constraints.NotNull; import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.UriInfo; import lombok.Getter; +import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; import org.openmetadata.common.utils.CommonUtil; +import org.openmetadata.schema.CreateEntity; import org.openmetadata.schema.EntityInterface; import org.openmetadata.schema.api.VoteRequest; import org.openmetadata.schema.api.feed.ResolveTask; @@ -428,6 +430,23 @@ public abstract class EntityRepository { LOG.info("Created a new {} {}", entityType, entity.getFullyQualifiedName()); } + public final T copy(T entity, CreateEntity request, String updatedBy) { + EntityReference owner = validateOwner(request.getOwner()); + EntityReference domain = validateDomain(request.getDomain()); + entity.setId(UUID.randomUUID()); + entity.setName(request.getName()); + entity.setDisplayName(request.getDisplayName()); + entity.setDescription(request.getDescription()); + entity.setOwner(owner); + entity.setDomain(domain); + entity.setDataProducts(getEntityReferences(Entity.DATA_PRODUCT, request.getDataProducts())); + entity.setLifeCycle(request.getLifeCycle()); + entity.setExtension(request.getExtension()); + entity.setUpdatedBy(updatedBy); + entity.setUpdatedAt(System.currentTimeMillis()); + return entity; + } + public EntityUpdater getUpdater(T original, T updated, Operation operation) { return new EntityUpdater(original, updated, operation); } @@ -1370,7 +1389,7 @@ public abstract class EntityRepository { public List findFrom( UUID toId, String toEntityType, Relationship relationship, String fromEntityType) { List records = findFromRecords(toId, toEntityType, relationship, fromEntityType); - return EntityUtil.getEntityReferences(records); + return getEntityReferences(records); } public List findFromRecords( @@ -1431,7 +1450,7 @@ public abstract class EntityRepository { UUID fromId, String fromEntityType, Relationship relationship, String toEntityType) { // When toEntityType is null, all the relationships to any entity is returned List records = findToRecords(fromId, fromEntityType, relationship, toEntityType); - return EntityUtil.getEntityReferences(records); + return getEntityReferences(records); } public final List findToRecords( @@ -2387,7 +2406,7 @@ public abstract class EntityRepository { static class EntityLoaderWithName extends CacheLoader, EntityInterface> { @Override - public EntityInterface load(@CheckForNull Pair fqnPair) { + public @NonNull EntityInterface load(@NotNull Pair fqnPair) { String entityType = fqnPair.getLeft(); String fqn = fqnPair.getRight(); EntityRepository repository = Entity.getEntityRepository(entityType); @@ -2397,7 +2416,7 @@ public abstract class EntityRepository { static class EntityLoaderWithId extends CacheLoader, EntityInterface> { @Override - public EntityInterface load(@NotNull Pair idPair) { + public @NonNull EntityInterface load(@NotNull Pair idPair) { String entityType = idPair.getLeft(); UUID id = idPair.getRight(); EntityRepository repository = Entity.getEntityRepository(entityType); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/EntityResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/EntityResource.java index 3196034f903..5b7366e351b 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/EntityResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/EntityResource.java @@ -19,7 +19,6 @@ import javax.ws.rs.core.SecurityContext; import javax.ws.rs.core.UriInfo; import lombok.Getter; import lombok.extern.slf4j.Slf4j; -import org.openmetadata.schema.CreateEntity; import org.openmetadata.schema.EntityInterface; import org.openmetadata.schema.type.EntityHistory; import org.openmetadata.schema.type.EntityReference; @@ -289,23 +288,6 @@ public abstract class EntityResource getResourceContext() { return new ResourceContext<>(entityType); } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/analytics/WebAnalyticEventResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/analytics/WebAnalyticEventResource.java index 4a4bfb1aa54..de73544766c 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/analytics/WebAnalyticEventResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/analytics/WebAnalyticEventResource.java @@ -463,7 +463,8 @@ public class WebAnalyticEventResource extends EntityResource { } private Bot getBot(CreateBot create, String user) { - return copy(new Bot(), create, user) + return repository + .copy(new Bot(), create, user) .withBotUser(getEntityReference(Entity.USER, create.getBotUser())) .withProvider(create.getProvider()) .withFullyQualifiedName(create.getName()); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/charts/ChartResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/charts/ChartResource.java index 77b2c49ae77..5ffae063a1f 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/charts/ChartResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/charts/ChartResource.java @@ -426,7 +426,8 @@ public class ChartResource extends EntityResource { } private Chart getChart(CreateChart create, String user) { - return copy(new Chart(), create, user) + return repository + .copy(new Chart(), create, user) .withService(EntityUtil.getEntityReference(Entity.DASHBOARD_SERVICE, create.getService())) .withChartType(create.getChartType()) .withSourceUrl(create.getSourceUrl()) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dashboards/DashboardResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dashboards/DashboardResource.java index 7353d4448ab..dfedc791afb 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dashboards/DashboardResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dashboards/DashboardResource.java @@ -437,7 +437,8 @@ public class DashboardResource extends EntityResource { private Table getTable(CreateTable create, String user) { return validateNewTable( - copy(new Table(), create, user) + repository + .copy(new Table(), create, user) .withColumns(create.getColumns()) .withSourceUrl(create.getSourceUrl()) .withTableConstraints(create.getTableConstraints()) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/datainsight/DataInsightChartResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/datainsight/DataInsightChartResource.java index d752610360b..7d38ddeb4d0 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/datainsight/DataInsightChartResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/datainsight/DataInsightChartResource.java @@ -442,7 +442,8 @@ public class DataInsightChartResource extends EntityResource experts = create.getExperts(); DataProduct dataProduct = - copy(new DataProduct(), create, user) + repository + .copy(new DataProduct(), create, user) .withFullyQualifiedName(create.getName()) .withStyle(create.getStyle()) .withExperts(EntityUtil.populateEntityReferences(getEntityReferences(Entity.USER, experts))); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/domains/DomainResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/domains/DomainResource.java index e00d9c87935..55f8bc6bd20 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/domains/DomainResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/domains/DomainResource.java @@ -311,7 +311,8 @@ public class DomainResource extends EntityResource { private Domain getDomain(CreateDomain create, String user) { List experts = create.getExperts(); - return copy(new Domain(), create, user) + return repository + .copy(new Domain(), create, user) .withStyle(create.getStyle()) .withDomainType(create.getDomainType()) .withFullyQualifiedName(create.getName()) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResource.java index 75815647995..86e9c615268 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResource.java @@ -648,7 +648,8 @@ public class TestCaseResource extends EntityResource { } private Kpi getKpi(CreateKpiRequest create, String user) { - return copy(new Kpi(), create, user) + return repository + .copy(new Kpi(), create, user) .withStartDate(create.getStartDate()) .withEndDate(create.getEndDate()) .withTargetDefinition(create.getTargetDefinition()) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/mlmodels/MlModelResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/mlmodels/MlModelResource.java index 2de0e6dbe61..3267ed9f231 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/mlmodels/MlModelResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/mlmodels/MlModelResource.java @@ -440,7 +440,8 @@ public class MlModelResource extends EntityResource } private MlModel getMlModel(CreateMlModel create, String user) { - return copy(new MlModel(), create, user) + return repository + .copy(new MlModel(), create, user) .withService(getEntityReference(Entity.MLMODEL_SERVICE, create.getService())) .withDashboard(getEntityReference(Entity.DASHBOARD, create.getDashboard())) .withAlgorithm(create.getAlgorithm()) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/pipelines/PipelineResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/pipelines/PipelineResource.java index 34d5041be20..4201e374d61 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/pipelines/PipelineResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/pipelines/PipelineResource.java @@ -542,7 +542,8 @@ public class PipelineResource extends EntityResource { } private Policy getPolicy(CreatePolicy create, String user) { - Policy policy = copy(new Policy(), create, user).withRules(create.getRules()).withEnabled(create.getEnabled()); + Policy policy = + repository.copy(new Policy(), create, user).withRules(create.getRules()).withEnabled(create.getEnabled()); if (create.getLocation() != null) { policy = policy.withLocation(new EntityReference().withId(create.getLocation())); } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/query/QueryResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/query/QueryResource.java index ecd5c013d07..108c21ed528 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/query/QueryResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/query/QueryResource.java @@ -509,7 +509,8 @@ public class QueryResource extends EntityResource { } private Query getQuery(CreateQuery create, String user) { - return copy(new Query(), create, user) + return repository + .copy(new Query(), create, user) .withTags(create.getTags()) .withQuery(create.getQuery()) .withService(getEntityReference(Entity.DATABASE_SERVICE, create.getService())) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/searchindex/SearchIndexResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/searchindex/SearchIndexResource.java index 5002ece223f..10f4c961406 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/searchindex/SearchIndexResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/searchindex/SearchIndexResource.java @@ -490,7 +490,8 @@ public class SearchIndexResource extends EntityResource { EntityRepository.getEntitiesFromSeedData(CLASSIFICATION, ".*json/data/tags/.*\\.json$", LoadTags.class); for (LoadTags loadTags : loadTagsList) { Classification classification = - ClassificationResource.getClassification(loadTags.getCreateClassification(), ADMIN_USER_NAME); + ClassificationResource.getClassification( + classificationRepository, loadTags.getCreateClassification(), ADMIN_USER_NAME); classificationRepository.initializeEntity(classification); List tagsToCreate = new ArrayList<>(); @@ -497,7 +498,8 @@ public class TagResource extends EntityResource { String parentFQN = create.getParent() != null ? create.getParent() : create.getClassification(); EntityReference classification = getEntityReference(CLASSIFICATION, create.getClassification()); EntityReference parent = create.getParent() == null ? null : getEntityReference(TAG, create.getParent()); - return copy(new Tag(), create, updateBy) + return repository + .copy(new Tag(), create, updateBy) .withFullyQualifiedName(FullyQualifiedName.add(parentFQN, create.getName())) .withStyle(create.getStyle()) .withParent(parent) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/PersonaResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/PersonaResource.java index 7327cbb69b9..050f95c3137 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/PersonaResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/PersonaResource.java @@ -326,6 +326,8 @@ public class PersonaResource extends EntityResource } private Persona getPersona(CreatePersona cp, String user) { - return copy(new Persona(), cp, user).withUsers(EntityUtil.toEntityReferences(cp.getUsers(), Entity.USER)); + return repository + .copy(new Persona(), cp, user) + .withUsers(EntityUtil.toEntityReferences(cp.getUsers(), Entity.USER)); } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/RoleResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/RoleResource.java index 360bf554fec..e7dcf406295 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/RoleResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/RoleResource.java @@ -408,7 +408,9 @@ public class RoleResource extends EntityResource { if (nullOrEmpty(create.getPolicies())) { throw new IllegalArgumentException("At least one policy is required to create a role"); } - return copy(new Role(), create, user).withPolicies(getEntityReferences(Entity.POLICY, create.getPolicies())); + return repository + .copy(new Role(), create, user) + .withPolicies(getEntityReferences(Entity.POLICY, create.getPolicies())); } public static EntityReference getRole(String roleName) { diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/TeamResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/TeamResource.java index 041aefc4fff..68bcfe325f3 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/TeamResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/TeamResource.java @@ -502,7 +502,8 @@ public class TeamResource extends EntityResource { if (ct.getTeamType().equals(TeamType.GROUP) && ct.getChildren() != null) { throw new IllegalArgumentException(CREATE_GROUP); } - return copy(new Team(), ct, user) + return repository + .copy(new Team(), ct, user) .withProfile(ct.getProfile()) .withIsJoinable(ct.getIsJoinable()) .withUsers(EntityUtil.toEntityReferences(ct.getUsers(), Entity.USER)) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/topics/TopicResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/topics/TopicResource.java index 92ad246b926..ee54b3efdc2 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/topics/TopicResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/topics/TopicResource.java @@ -489,7 +489,8 @@ public class TopicResource extends EntityResource { } private Topic getTopic(CreateTopic create, String user) { - return copy(new Topic(), create, user) + return repository + .copy(new Topic(), create, user) .withService(getEntityReference(Entity.MESSAGING_SERVICE, create.getService())) .withPartitions(create.getPartitions()) .withMessageSchema(create.getMessageSchema()) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/types/TypeResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/types/TypeResource.java index ce72d6bb435..39a9597e36c 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/types/TypeResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/types/TypeResource.java @@ -393,7 +393,8 @@ public class TypeResource extends EntityResource { } private Type getType(CreateType create, String user) { - return copy(new Type(), create, user) + return repository + .copy(new Type(), create, user) .withFullyQualifiedName(create.getName()) .withCategory(create.getCategory()) .withSchema(create.getSchema());