diff --git a/step-framework-collections/src/main/java/step/core/objectenricher/ObjectHook.java b/step-framework-collections/src/main/java/step/core/objectenricher/ObjectHook.java index ef964d23..676f55bf 100644 --- a/step-framework-collections/src/main/java/step/core/objectenricher/ObjectHook.java +++ b/step-framework-collections/src/main/java/step/core/objectenricher/ObjectHook.java @@ -20,6 +20,8 @@ import step.core.AbstractContext; +import java.util.Map; + /** * An {@link ObjectHook} is a factory for * {@link ObjectFilter} and {@link ObjectEnricher} @@ -31,6 +33,10 @@ public interface ObjectHook { ObjectEnricher getObjectEnricher(AbstractContext context); + default ObjectValidator getObjectValidator(AbstractContext context, Map config) { + return null; + } + /** * Rebuilds an {@link AbstractContext} based on an object that has been * previously enriched with an {@link ObjectEnricher} provided by this class diff --git a/step-framework-collections/src/main/java/step/core/objectenricher/ObjectHookRegistry.java b/step-framework-collections/src/main/java/step/core/objectenricher/ObjectHookRegistry.java index 874752f2..2c3946c7 100644 --- a/step-framework-collections/src/main/java/step/core/objectenricher/ObjectHookRegistry.java +++ b/step-framework-collections/src/main/java/step/core/objectenricher/ObjectHookRegistry.java @@ -19,17 +19,19 @@ package step.core.objectenricher; import step.core.AbstractContext; -import step.core.collections.Filter; import step.core.collections.PojoFilter; -import step.core.collections.PojoFilters; import step.core.ql.OQLFilterBuilder; import java.util.ArrayList; -import java.util.Objects; +import java.util.Collections; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; public class ObjectHookRegistry extends ArrayList { + private final Map validatorConfiguration = new ConcurrentHashMap<>(); + /** * @param context * @return the composed {@link ObjectFilter} based on all the registered hooks @@ -48,6 +50,19 @@ public ObjectEnricher getObjectEnricher(AbstractContext context) { .compose(stream().map(hook -> hook.getObjectEnricher(context)).collect(Collectors.toList())); } + /** + * @param context + * @return the composed {@link ObjectValidator} based on all the registered hooks + */ + public ObjectValidator getObjectValidator(AbstractContext context) { + return ObjectValidatorComposer + .compose(stream().map(hook -> hook.getObjectValidator(context, Collections.unmodifiableMap(this.validatorConfiguration))).collect(Collectors.toList())); + } + + public Map getValidatorConfiguration(){ + return this.validatorConfiguration; + } + /** * Rebuilds an {@link AbstractContext} based on an object that has been * previously enriched with the composed {@link ObjectEnricher} of this registry diff --git a/step-framework-collections/src/main/java/step/core/objectenricher/ObjectValidator.java b/step-framework-collections/src/main/java/step/core/objectenricher/ObjectValidator.java new file mode 100644 index 00000000..12b4374a --- /dev/null +++ b/step-framework-collections/src/main/java/step/core/objectenricher/ObjectValidator.java @@ -0,0 +1,27 @@ +/* + * ****************************************************************************** + * * Copyright (C) 2020, exense GmbH + * * + * * This file is part of STEP + * * + * * STEP is free software: you can redistribute it and/or modify + * * it under the terms of the GNU Affero General Public License as published by + * * the Free Software Foundation, either version 3 of the License, or + * * (at your option) any later version. + * * + * * STEP is distributed in the hope that it will be useful, + * * but WITHOUT ANY WARRANTY; without even the implied warranty of + * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * * GNU Affero General Public License for more details. + * * + * * You should have received a copy of the GNU Affero General Public License + * * along with STEP. If not, see . + * ***************************************************************************** + */ +package step.core.objectenricher; + +public interface ObjectValidator { + + void validateOnSave(EnricheableObject obj); + +} diff --git a/step-framework-collections/src/main/java/step/core/objectenricher/ObjectValidatorComposer.java b/step-framework-collections/src/main/java/step/core/objectenricher/ObjectValidatorComposer.java new file mode 100644 index 00000000..f8eb3b93 --- /dev/null +++ b/step-framework-collections/src/main/java/step/core/objectenricher/ObjectValidatorComposer.java @@ -0,0 +1,39 @@ +/******************************************************************************* + * Copyright (C) 2020, exense GmbH + * + * This file is part of STEP + * + * STEP is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * STEP is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with STEP. If not, see . + ******************************************************************************/ +package step.core.objectenricher; + +import java.util.*; +import java.util.stream.Stream; + +public class ObjectValidatorComposer { + + public static ObjectValidator compose(List list) { + return new ObjectValidator() { + + @Override + public void validateOnSave(EnricheableObject obj) { + nonNullList(list).forEach(overlapper -> overlapper.validateOnSave(obj)); + } + + private Stream nonNullList(List list) { + return list.stream().filter(Objects::nonNull); + } + }; + } +}