Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] NotExists #635

Draft
wants to merge 1 commit into
base: 1.3.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ private <T> void add(Set<ConstraintViolation<T>> violations) {
e.setModelAlias(null);
e.setMsg(c.getMessage());

this.template.add(e);
this.addValidationError(e);
}
}

public void addValidationError(ValidationError violation) {
this.template.add(violation);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -679,6 +680,8 @@ public int hashCode() {

void onStateLoadEvent();
void onStateChangeEvent(ExecutionTxnContext txnCtx, Action a);

ValidationResult getValidationResult();
}

public interface LeafParam<T> extends Param<T> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<NotExists> {

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class DefaultParamState<T> extends AbstractEntityState<T> implements Para

private StateType type;

private ValidationResult validationResult;
private ValidationResult validationResult = new ValidationResult();

@JsonIgnore
final private Model<?> parentModel;
Expand Down