Add initial infra for config repository

- Initial infra for config abstraction with
  Spring Data Repositories via RepositoryStateMachineModelFactory.
- Initial jpa impl.
- Initial jpa sample atop of H2 DB.
- Relates to #250
This commit is contained in:
Janne Valkealahti
2016-09-23 17:26:39 +01:00
parent 693d7b8951
commit 0d329d9e57
22 changed files with 882 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
/*
* 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 state entity.
*
* @author Janne Valkealahti
*
*/
public interface RepositoryState {
String getState();
void setState(String state);
boolean isInitial();
void setInitial(boolean initial);
}

View File

@@ -0,0 +1,73 @@
/*
* 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 java.util.ArrayList;
import java.util.Collection;
import org.springframework.statemachine.config.model.ConfigurationData;
import org.springframework.statemachine.config.model.DefaultStateMachineModel;
import org.springframework.statemachine.config.model.StateData;
import org.springframework.statemachine.config.model.StateMachineModel;
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;
/**
* A generic {@link StateMachineModelFactory} which is backed by a Spring Data
* Repository abstraction.
*
* @author Janne Valkealahti
*
*/
public class RepositoryStateMachineModelFactory implements StateMachineModelFactory<String, String> {
private final StateRepository<? extends RepositoryState> stateRepository;
private final TransitionRepository<? extends RepositoryTransition> transitionRepository;
/**
* Instantiates a new repository state machine model factory.
*
* @param stateRepository the state repository
* @param transitionRepository the transition repository
*/
public RepositoryStateMachineModelFactory(StateRepository<? extends RepositoryState> stateRepository,
TransitionRepository<? extends RepositoryTransition> transitionRepository) {
this.stateRepository = stateRepository;
this.transitionRepository = transitionRepository;
}
@Override
public StateMachineModel<String, String> build() {
ConfigurationData<String, String> configurationData = new ConfigurationData<>();
Collection<StateData<String, String>> stateData = new ArrayList<>();
for (RepositoryState s : stateRepository.findAll()) {
stateData.add(new StateData<String, String>(s.getState(), s.isInitial()));
}
StatesData<String, String> statesData = new StatesData<>(stateData);
Collection<TransitionData<String, String>> transitionData = new ArrayList<>();
for (RepositoryTransition t : transitionRepository.findAll()) {
transitionData.add(new TransitionData<String, String>(t.getSource(), t.getTarget(), t.getEvent()));
}
TransitionsData<String, String> transitionsData = new TransitionsData<>(transitionData);
StateMachineModel<String, String> stateMachineModel = new DefaultStateMachineModel<>(configurationData, statesData, transitionsData);
return stateMachineModel;
}
}

View File

@@ -0,0 +1,37 @@
/*
* 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 transition entity.
*
* @author Janne Valkealahti
*
*/
public interface RepositoryTransition {
String getSource();
void setSource(String source);
String getTarget();
void setTarget(String target);
String getEvent();
void setEvent(String event);
}

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 states.
*
* @author Janne Valkealahti
*
* @param <S> the state entity type
*/
@NoRepositoryBean
public interface StateRepository<S extends RepositoryState> extends CrudRepository<S, Long> {
}

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 transitions.
*
* @author Janne Valkealahti
*
* @param <T> the transition entity type
*/
@NoRepositoryBean
public interface TransitionRepository<T extends RepositoryTransition> extends CrudRepository<T, Long> {
}