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

@@ -1,3 +1,18 @@
/*
* 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.jpa;
import org.springframework.statemachine.data.ActionRepository;

View File

@@ -0,0 +1,27 @@
/*
* 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.jpa;
import org.springframework.statemachine.data.GuardRepository;
/**
* A {@link GuardRepository} interface for JPA used for guards.
*
* @author Janne Valkealahti
*
*/
public interface JpaGuardRepository extends GuardRepository<JpaRepositoryGuard> {
}

View File

@@ -0,0 +1,58 @@
/*
* 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.jpa;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.springframework.statemachine.data.RepositoryGuard;
/**
* JPA entity for actions.
*
* @author Janne Valkealahti
*
*/
@Entity
public class JpaRepositoryGuard implements RepositoryGuard {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
private String spel;
@Override
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String getSpel() {
return spel;
}
public void setSpel(String spel) {
this.spel = spel;
}
}

View File

@@ -24,6 +24,7 @@ import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import org.springframework.statemachine.data.RepositoryTransition;
import org.springframework.statemachine.transition.TransitionKind;
@@ -50,6 +51,8 @@ public class JpaRepositoryTransition implements RepositoryTransition {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<JpaRepositoryAction> actions;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private JpaRepositoryGuard guard;
/**
* Instantiates a new jpa repository transition.
@@ -143,6 +146,15 @@ public class JpaRepositoryTransition implements RepositoryTransition {
this.actions = actions;
}
@Override
public JpaRepositoryGuard getGuard() {
return guard;
}
public void setGuard(JpaRepositoryGuard guard) {
this.guard = guard;
}
@Override
public TransitionKind getKind() {
return kind;

View File

@@ -25,7 +25,11 @@
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryTransition",
"source": "S2",
"target": "S3",
"event": "E2"
"event": "E2",
"guard": {
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryGuard",
"spel": "true"
}
},
{
"_class": "org.springframework.statemachine.data.jpa.JpaRepositoryTransition",

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.
*