diff --git a/spring-statemachine-data/jpa/src/main/java/org/springframework/statemachine/data/jpa/JpaActionRepository.java b/spring-statemachine-data/jpa/src/main/java/org/springframework/statemachine/data/jpa/JpaActionRepository.java index 5fd6ae51..9796cf2b 100644 --- a/spring-statemachine-data/jpa/src/main/java/org/springframework/statemachine/data/jpa/JpaActionRepository.java +++ b/spring-statemachine-data/jpa/src/main/java/org/springframework/statemachine/data/jpa/JpaActionRepository.java @@ -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; diff --git a/spring-statemachine-data/jpa/src/main/java/org/springframework/statemachine/data/jpa/JpaGuardRepository.java b/spring-statemachine-data/jpa/src/main/java/org/springframework/statemachine/data/jpa/JpaGuardRepository.java new file mode 100644 index 00000000..3044752a --- /dev/null +++ b/spring-statemachine-data/jpa/src/main/java/org/springframework/statemachine/data/jpa/JpaGuardRepository.java @@ -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 { +} diff --git a/spring-statemachine-data/jpa/src/main/java/org/springframework/statemachine/data/jpa/JpaRepositoryGuard.java b/spring-statemachine-data/jpa/src/main/java/org/springframework/statemachine/data/jpa/JpaRepositoryGuard.java new file mode 100644 index 00000000..ddfc9368 --- /dev/null +++ b/spring-statemachine-data/jpa/src/main/java/org/springframework/statemachine/data/jpa/JpaRepositoryGuard.java @@ -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; + } +} diff --git a/spring-statemachine-data/jpa/src/main/java/org/springframework/statemachine/data/jpa/JpaRepositoryTransition.java b/spring-statemachine-data/jpa/src/main/java/org/springframework/statemachine/data/jpa/JpaRepositoryTransition.java index ea1cf817..e86da510 100644 --- a/spring-statemachine-data/jpa/src/main/java/org/springframework/statemachine/data/jpa/JpaRepositoryTransition.java +++ b/spring-statemachine-data/jpa/src/main/java/org/springframework/statemachine/data/jpa/JpaRepositoryTransition.java @@ -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 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; diff --git a/spring-statemachine-data/jpa/src/test/resources/data2.json b/spring-statemachine-data/jpa/src/test/resources/data2.json index 67d99447..092ca721 100644 --- a/spring-statemachine-data/jpa/src/test/resources/data2.json +++ b/spring-statemachine-data/jpa/src/test/resources/data2.json @@ -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", diff --git a/spring-statemachine-data/src/main/java/org/springframework/statemachine/data/GuardRepository.java b/spring-statemachine-data/src/main/java/org/springframework/statemachine/data/GuardRepository.java new file mode 100644 index 00000000..60244755 --- /dev/null +++ b/spring-statemachine-data/src/main/java/org/springframework/statemachine/data/GuardRepository.java @@ -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 the guard entity type + */ +@NoRepositoryBean +public interface GuardRepository extends CrudRepository { +} diff --git a/spring-statemachine-data/src/main/java/org/springframework/statemachine/data/RepositoryGuard.java b/spring-statemachine-data/src/main/java/org/springframework/statemachine/data/RepositoryGuard.java new file mode 100644 index 00000000..b42b8152 --- /dev/null +++ b/spring-statemachine-data/src/main/java/org/springframework/statemachine/data/RepositoryGuard.java @@ -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(); +} diff --git a/spring-statemachine-data/src/main/java/org/springframework/statemachine/data/RepositoryStateMachineModelFactory.java b/spring-statemachine-data/src/main/java/org/springframework/statemachine/data/RepositoryStateMachineModelFactory.java index 25cf2bd1..0501b676 100644 --- a/spring-statemachine-data/src/main/java/org/springframework/statemachine/data/RepositoryStateMachineModelFactory.java +++ b/spring-statemachine-data/src/main/java/org/springframework/statemachine/data/RepositoryStateMachineModelFactory.java @@ -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 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 transitionsData = new TransitionsData<>(transitionData); diff --git a/spring-statemachine-data/src/main/java/org/springframework/statemachine/data/RepositoryTransition.java b/spring-statemachine-data/src/main/java/org/springframework/statemachine/data/RepositoryTransition.java index 722f7031..39dc5498 100644 --- a/spring-statemachine-data/src/main/java/org/springframework/statemachine/data/RepositoryTransition.java +++ b/spring-statemachine-data/src/main/java/org/springframework/statemachine/data/RepositoryTransition.java @@ -62,6 +62,13 @@ public interface RepositoryTransition { */ Set getActions(); + /** + * Gets the guard. + * + * @return the guard + */ + RepositoryGuard getGuard(); + /** * Gets the transition kind. *