io.evitadb.api.requestResponse.schema.mutation.reference.ModifyReferenceAttributeSchemaMutation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of evita_api Show documentation
Show all versions of evita_api Show documentation
Module contains external API of the evitaDB.
/*
*
* _ _ ____ ____
* _____ _(_) |_ __ _| _ \| __ )
* / _ \ \ / / | __/ _` | | | | _ \
* | __/\ V /| | || (_| | |_| | |_) |
* \___| \_/ |_|\__\__,_|____/|____/
*
* Copyright (c) 2023
*
* Licensed under the Business Source License, Version 1.1 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/FgForrest/evitaDB/blob/master/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.evitadb.api.requestResponse.schema.mutation.reference;
import io.evitadb.api.exception.InvalidSchemaMutationException;
import io.evitadb.api.requestResponse.schema.CatalogSchemaContract;
import io.evitadb.api.requestResponse.schema.EntitySchemaContract;
import io.evitadb.api.requestResponse.schema.ReferenceSchemaContract;
import io.evitadb.api.requestResponse.schema.builder.InternalSchemaBuilderHelper.MutationCombinationResult;
import io.evitadb.api.requestResponse.schema.mutation.AttributeSchemaMutation;
import io.evitadb.api.requestResponse.schema.mutation.CombinableEntitySchemaMutation;
import io.evitadb.api.requestResponse.schema.mutation.EntitySchemaMutation;
import io.evitadb.api.requestResponse.schema.mutation.ReferenceSchemaMutation;
import io.evitadb.utils.ArrayUtils;
import io.evitadb.utils.Assert;
import io.evitadb.utils.StringUtils;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.ThreadSafe;
import java.io.Serial;
import java.util.Arrays;
import java.util.Optional;
/**
* Mutation is a holder for a single {@link AttributeSchemaMutation} that affect any
* of {@link ReferenceSchemaContract#getAttributes()} in the {@link EntitySchemaContract}.
* Mutation implements {@link CombinableEntitySchemaMutation} allowing to resolve conflicts with the same mutation
* combination if it is placed twice in the mutation pipeline.
*
* @author Jan Novotný ([email protected]), FG Forrest a.s. (c) 2022
*/
@ThreadSafe
@Immutable
@EqualsAndHashCode(callSuper = true)
public class ModifyReferenceAttributeSchemaMutation extends AbstractModifyReferenceDataSchemaMutation
implements CombinableEntitySchemaMutation {
@Serial private static final long serialVersionUID = -5779012919587623154L;
@Nonnull @Getter private final ReferenceSchemaMutation attributeSchemaMutation;
public ModifyReferenceAttributeSchemaMutation(@Nonnull String name, @Nonnull ReferenceSchemaMutation attributeSchemaMutation) {
super(name);
Assert.isTrue(attributeSchemaMutation instanceof AttributeSchemaMutation, "The mutation must implement AttributeSchemaMutation interface!");
this.attributeSchemaMutation = attributeSchemaMutation;
}
@Nullable
@Override
public MutationCombinationResult combineWith(@Nonnull CatalogSchemaContract currentCatalogSchema, @Nonnull EntitySchemaContract currentEntitySchema, @Nonnull EntitySchemaMutation existingMutation) {
if (existingMutation instanceof ModifyReferenceAttributeSchemaMutation theExistingMutation && name.equals(theExistingMutation.getName())
&& attributeSchemaMutation.getName().equals(theExistingMutation.getAttributeSchemaMutation().getName())) {
if (attributeSchemaMutation instanceof CombinableEntitySchemaMutation combinableAttributeCombinationMutation) {
final MutationCombinationResult result = combinableAttributeCombinationMutation.combineWith(
currentCatalogSchema, currentEntitySchema, (EntitySchemaMutation) theExistingMutation.getAttributeSchemaMutation()
);
if (result == null) {
return null;
} else {
final EntitySchemaMutation origin;
if (result.origin() == null) {
origin = null;
} else if (result.origin() == combinableAttributeCombinationMutation) {
origin = theExistingMutation;
} else {
origin = new ModifyReferenceAttributeSchemaMutation(name, (ReferenceSchemaMutation) result.origin());
}
final EntitySchemaMutation[] current;
if (ArrayUtils.isEmpty(result.current())) {
current = result.current();
} else {
current = Arrays.stream(result.current())
.map(it -> {
if (it == ((ModifyReferenceAttributeSchemaMutation) existingMutation).getAttributeSchemaMutation()) {
return existingMutation;
} else {
return new ModifyReferenceAttributeSchemaMutation(name, (ReferenceSchemaMutation) it);
}
})
.toArray(EntitySchemaMutation[]::new);
}
return new MutationCombinationResult<>(origin, current);
}
} else {
return null;
}
} else {
return null;
}
}
@Nullable
@Override
public ReferenceSchemaContract mutate(@Nonnull EntitySchemaContract entitySchema, @Nullable ReferenceSchemaContract referenceSchema) {
return attributeSchemaMutation.mutate(entitySchema, referenceSchema);
}
@Nullable
@Override
public EntitySchemaContract mutate(@Nonnull CatalogSchemaContract catalogSchema, @Nullable EntitySchemaContract entitySchema) {
Assert.isPremiseValid(entitySchema != null, "Entity schema is mandatory!");
final Optional existingReferenceSchema = entitySchema.getReference(name);
if (existingReferenceSchema.isEmpty()) {
// ups, the reference schema is missing
throw new InvalidSchemaMutationException(
"The reference `" + name + "` is not defined in entity `" + entitySchema.getName() + "` schema!"
);
} else {
final ReferenceSchemaContract theSchema = existingReferenceSchema.get();
final ReferenceSchemaContract updatedSchema = mutate(entitySchema, theSchema);
Assert.isPremiseValid(updatedSchema != null, "Updated reference schema is not expected to be null!");
return replaceReferenceSchema(entitySchema, theSchema, updatedSchema);
}
}
@Override
public String toString() {
return "Modify entity reference `" + name + "` schema, " +
StringUtils.uncapitalize(attributeSchemaMutation.toString());
}
}