Add repository config guards

- Add guard to RepositoryTransition
- New GuardRepository.
- Relates to #262
This commit is contained in:
Janne Valkealahti
2016-10-02 16:28:32 +01:00
parent ddf81c78a0
commit c016fd6e06
9 changed files with 210 additions and 2 deletions

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2016 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 org.springframework.statemachine.data;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.Repository;
/**
* Generic {@link Repository} interface for guards.
*
* @author Janne Valkealahti
*
* @param <T> the guard entity type
*/
@NoRepositoryBean
public interface GuardRepository <T extends RepositoryGuard> extends CrudRepository<T, Long> {
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2016 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 org.springframework.statemachine.data;
/**
* Generic interface representing guard entity.
*
* @author Janne Valkealahti
*
*/
public interface RepositoryGuard {
/**
* Gets the name.
*
* @return the name
*/
String getName();
/**
* Gets the spel.
*
* @return the spel
*/
String getSpel();
}

View File

@@ -33,6 +33,8 @@ import org.springframework.statemachine.config.model.StateMachineModelFactory;
import org.springframework.statemachine.config.model.StatesData;
import org.springframework.statemachine.config.model.TransitionData;
import org.springframework.statemachine.config.model.TransitionsData;
import org.springframework.statemachine.guard.Guard;
import org.springframework.statemachine.guard.SpelExpressionGuard;
import org.springframework.statemachine.transition.TransitionKind;
import org.springframework.util.StringUtils;
@@ -161,7 +163,20 @@ public class RepositoryStateMachineModelFactory extends AbstractStateMachineMode
}
TransitionKind kind = t.getKind();
transitionData.add(new TransitionData<>(t.getSource(), t.getTarget(), t.getEvent(), actions, null, kind != null ? kind : TransitionKind.EXTERNAL));
Guard<String, String> guard = null;
RepositoryGuard repositoryGuard = t.getGuard();
if (repositoryGuard != null) {
if (StringUtils.hasText(repositoryGuard.getName())) {
guard = resolveGuard(repositoryGuard.getName());
} else if (StringUtils.hasText(repositoryGuard.getSpel())) {
SpelExpressionParser parser = new SpelExpressionParser(
new SpelParserConfiguration(SpelCompilerMode.MIXED, null));
guard = new SpelExpressionGuard<>(parser.parseExpression(repositoryGuard.getSpel()));
}
}
transitionData.add(new TransitionData<>(t.getSource(), t.getTarget(), t.getEvent(), actions, guard, kind != null ? kind : TransitionKind.EXTERNAL));
}
TransitionsData<String, String> transitionsData = new TransitionsData<>(transitionData);

View File

@@ -62,6 +62,13 @@ public interface RepositoryTransition {
*/
Set<? extends RepositoryAction> getActions();
/**
* Gets the guard.
*
* @return the guard
*/
RepositoryGuard getGuard();
/**
* Gets the transition kind.
*