Skip to content
Open
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 @@ -20,6 +20,8 @@

import step.core.AbstractContext;

import java.util.Map;

/**
* An {@link ObjectHook} is a factory for
* {@link ObjectFilter} and {@link ObjectEnricher}
Expand All @@ -31,6 +33,10 @@ public interface ObjectHook {

ObjectEnricher getObjectEnricher(AbstractContext context);

default ObjectValidator getObjectValidator(AbstractContext context, Map<String, Object> config) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add a javadoc to this method

return null;
}

/**
* Rebuilds an {@link AbstractContext} based on an object that has been
* previously enriched with an {@link ObjectEnricher} provided by this class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ObjectHook> {

private final Map<String, Object> validatorConfiguration = new ConcurrentHashMap<>();

/**
* @param context
* @return the composed {@link ObjectFilter} based on all the registered hooks
Expand All @@ -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<String, Object> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
* *****************************************************************************
*/
package step.core.objectenricher;

public interface ObjectValidator {

void validateOnSave(EnricheableObject obj);

}
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
******************************************************************************/
package step.core.objectenricher;

import java.util.*;
import java.util.stream.Stream;

public class ObjectValidatorComposer {

public static ObjectValidator compose(List<ObjectValidator> list) {
return new ObjectValidator() {

@Override
public void validateOnSave(EnricheableObject obj) {
nonNullList(list).forEach(overlapper -> overlapper.validateOnSave(obj));
}

private Stream<ObjectValidator> nonNullList(List<ObjectValidator> list) {
return list.stream().filter(Objects::nonNull);
}
};
}
}