diff --git a/nimbus-core/src/main/java/com/antheminc/oss/nimbus/domain/cmd/exec/ValidationResult.java b/nimbus-core/src/main/java/com/antheminc/oss/nimbus/domain/cmd/exec/ValidationResult.java index 94ebc05ea..406ad30eb 100644 --- a/nimbus-core/src/main/java/com/antheminc/oss/nimbus/domain/cmd/exec/ValidationResult.java +++ b/nimbus-core/src/main/java/com/antheminc/oss/nimbus/domain/cmd/exec/ValidationResult.java @@ -70,8 +70,11 @@ private void add(Set> violations) { e.setModelAlias(null); e.setMsg(c.getMessage()); - this.template.add(e); + this.addValidationError(e); } } + public void addValidationError(ValidationError violation) { + this.template.add(violation); + } } diff --git a/nimbus-core/src/main/java/com/antheminc/oss/nimbus/domain/defn/extension/NotExists.java b/nimbus-core/src/main/java/com/antheminc/oss/nimbus/domain/defn/extension/NotExists.java new file mode 100644 index 000000000..00321bac1 --- /dev/null +++ b/nimbus-core/src/main/java/com/antheminc/oss/nimbus/domain/defn/extension/NotExists.java @@ -0,0 +1,53 @@ +/** + * Copyright 2016-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 com.antheminc.oss.nimbus.domain.defn.extension; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Repeatable; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import com.antheminc.oss.nimbus.domain.RepeatContainer; +import com.antheminc.oss.nimbus.domain.defn.event.StateEvent.OnStateChange; +import com.antheminc.oss.nimbus.domain.defn.event.StateEvent.OnStateLoad; +import com.antheminc.oss.nimbus.domain.defn.extension.NotExists.List; + +/** + * @author Tony Lopez + * + */ +@Documented +@Retention(RUNTIME) +@Target(FIELD) +@Repeatable(List.class) +@OnStateChange +@OnStateLoad +public @interface NotExists { + + String url(); + + @Documented + @Retention(RUNTIME) + @Target(FIELD) + @RepeatContainer(NotExists.class) + @interface List { + + NotExists[] value(); + } +} diff --git a/nimbus-core/src/main/java/com/antheminc/oss/nimbus/domain/model/state/EntityState.java b/nimbus-core/src/main/java/com/antheminc/oss/nimbus/domain/model/state/EntityState.java index fe4f2fce0..9f5ba6c00 100644 --- a/nimbus-core/src/main/java/com/antheminc/oss/nimbus/domain/model/state/EntityState.java +++ b/nimbus-core/src/main/java/com/antheminc/oss/nimbus/domain/model/state/EntityState.java @@ -35,6 +35,7 @@ import com.antheminc.oss.nimbus.domain.cmd.Action; import com.antheminc.oss.nimbus.domain.cmd.Command; +import com.antheminc.oss.nimbus.domain.cmd.exec.ValidationResult; import com.antheminc.oss.nimbus.domain.defn.extension.ValidateConditional.ValidationGroup; import com.antheminc.oss.nimbus.domain.model.config.EntityConfig; import com.antheminc.oss.nimbus.domain.model.config.ModelConfig; @@ -679,6 +680,8 @@ public int hashCode() { void onStateLoadEvent(); void onStateChangeEvent(ExecutionTxnContext txnCtx, Action a); + + ValidationResult getValidationResult(); } public interface LeafParam extends Param { diff --git a/nimbus-core/src/main/java/com/antheminc/oss/nimbus/domain/model/state/extension/NotExistsStateEventHandler.java b/nimbus-core/src/main/java/com/antheminc/oss/nimbus/domain/model/state/extension/NotExistsStateEventHandler.java new file mode 100644 index 000000000..3e2e89411 --- /dev/null +++ b/nimbus-core/src/main/java/com/antheminc/oss/nimbus/domain/model/state/extension/NotExistsStateEventHandler.java @@ -0,0 +1,56 @@ +/** + * Copyright 2016-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 com.antheminc.oss.nimbus.domain.model.state.extension; + +import java.util.List; + +import com.antheminc.oss.nimbus.context.BeanResolverStrategy; +import com.antheminc.oss.nimbus.domain.cmd.CommandBuilder; +import com.antheminc.oss.nimbus.domain.cmd.exec.CommandExecution.MultiOutput; +import com.antheminc.oss.nimbus.domain.defn.extension.NotExists; +import com.antheminc.oss.nimbus.domain.cmd.exec.CommandExecutorGateway; +import com.antheminc.oss.nimbus.domain.cmd.exec.ValidationError; +import com.antheminc.oss.nimbus.domain.model.state.EntityState.Param; +import com.antheminc.oss.nimbus.support.EnableLoggingInterceptor; + +import lombok.AccessLevel; +import lombok.Getter; + +/** + * @author Tony Lopez + * + */ +@EnableLoggingInterceptor +@Getter(AccessLevel.PROTECTED) +public class NotExistsStateEventHandler extends AbstractConditionalStateEventHandler { + + private final CommandExecutorGateway commandGateway; + + public NotExistsStateEventHandler(BeanResolverStrategy beanResolver) { + super(beanResolver); + this.commandGateway = beanResolver.get(CommandExecutorGateway.class); + } + + @Override + protected void handleInternal(Param onChangeParam, NotExists configuredAnnotation) { + MultiOutput response = getCommandGateway().execute(CommandBuilder.withUri(configuredAnnotation.url()).getCommand(), null); + List searchResults = (List) response.getSingleResult(); + if (!searchResults.isEmpty()) { + ValidationError violation = new ValidationError.Param(); + onChangeParam.getValidationResult().addValidationError(violation); + } + } +} diff --git a/nimbus-core/src/main/java/com/antheminc/oss/nimbus/domain/model/state/internal/DefaultParamState.java b/nimbus-core/src/main/java/com/antheminc/oss/nimbus/domain/model/state/internal/DefaultParamState.java index b9884b161..102c249b2 100644 --- a/nimbus-core/src/main/java/com/antheminc/oss/nimbus/domain/model/state/internal/DefaultParamState.java +++ b/nimbus-core/src/main/java/com/antheminc/oss/nimbus/domain/model/state/internal/DefaultParamState.java @@ -79,7 +79,7 @@ public class DefaultParamState extends AbstractEntityState implements Para private StateType type; - private ValidationResult validationResult; + private ValidationResult validationResult = new ValidationResult(); @JsonIgnore final private Model parentModel;