Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2014 Andrew Hall 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.statefulj.persistence.common;
 
 import java.util.List;
 
 
 public abstract class AbstractPersister<T> implements Persister<T> {
 	
 	private static final Logger logger = LoggerFactory.getLogger(AbstractPersister.class);
 	
 	private Field idField;
 	private Field stateField;
 	private State<T> start;
 	private Class<T> clazz;
 	private HashMap<StringState<T>> states = new HashMap<StringState<T>>();
 	
 			List<State<T>> states
 			String stateFieldName
 			State<T> start
 			Class<T> clazz) {
 		
 		this. = clazz;
 		
 		// Find the Id and State<T> field of the Entity
 		//
 		this. = findIdField(clazz);
 		
 		if (this. == null) {
 			throw new RuntimeException("No Id field defined");
 		}
 		this..setAccessible(true);
 		
 		this. = findStateField(stateFieldNameclazz);
 
 		if (this. == null) {
 			throw new RuntimeException("No State field defined");
 		}
 		
 		if (!validStateField(this.)) {
 			throw new RuntimeException(
 					String.format(
 							"State field, %s, of class %s, is not of type %s",
 							clazz,
 		}
 
 
 		// Start state - returned when no state is set
 		//
 		this. = start;
 		
 		// Index States into a HashMap
 		//
 		for(State<T> state : states) {
 			this..put(state.getName(), state);
 		}
 	}
 
 	public State<T> getCurrent(T stateful) {
 		State<T> state = null;
 		try {
 			String stateKey = this.getState(stateful);
 			state = (stateKey == null) ? this. : this..get(stateKey);
 		} catch (NoSuchFieldException e) {
 			throw new RuntimeException(e);
 		} catch (SecurityException e) {
 			throw new RuntimeException(e);
 		} catch (IllegalArgumentException e) {
 			throw new RuntimeException(e);
catch (IllegalAccessException e) {
			throw new RuntimeException(e);
		}
		state = (state == null) ? this. : state;
		return state;
	}

Set the current State. This method will ensure that the state in the db matches the expected current state. If not, it will throw a StateStateException

Parameters:
stateful Stateful Entity
current Expected current State
next The value of the next State
Throws:
org.statefulj.fsm.StaleStateException thrown if the value of the State does not equal to the provided current State
	public abstract void setCurrent(T statefulState<T> currentState<T> nextthrows StaleStateException;
	protected abstract boolean validStateField(Field stateField); 
	protected abstract Field findIdField(Class<?> clazz); 
	protected Field findStateField(String stateFieldNameClass<?> clazz) {
		Field stateField = null;
		if (StringUtils.isEmpty(stateFieldName)) {
			stateField = ReflectionUtils.getFirstAnnotatedField(clazzorg.statefulj.persistence.annotations.State.class);
else {
			try {
				stateField = clazz.getDeclaredField(stateFieldName);
catch (NoSuchFieldException e) {
				.error("Unable to locate state field for {}, stateFieldName={}"clazz.getName(), stateFieldName);
catch (SecurityException e) {
				.error("Security exception trying to locate state field for {}, stateFieldName={}"clazz.getName(), stateFieldName);
				.error("Exception"e);
			}
		}
		return stateField;
	}
	protected abstract Class<?> getStateFieldType(); 
	protected Field getIdField() {
		return ;
	}
	protected void setIdField(Field idField) {
		this. = idField;
	}
	protected Field getStateField() {
		return ;
	}
	protected void setStateField(Field stateField) {
		this. = stateField;
	}
	protected State<T> getStart() {
		return ;
	}
	protected void setStart(State<T> start) {
		this. = start;
	}
	protected Class<T> getClazz() {
		return ;
	}
	protected void setClazz(Class<T> clazz) {
		this. = clazz;
	}
	protected HashMap<StringState<T>> getStates() {
		return ;
	}
	protected void setStates(HashMap<StringState<T>> states) {
		this. = states;
	}
		return this..get(obj);
	}
		return (String)this..get(obj);
	}
		state = (state == null) ? this..getName() : state;
		this..set(objstate);
	}
	protected void throwStaleState(State<T> currentState<T> nextthrows StaleStateException {
		String err = String.format(
				"Unable to update state, entity.state=%s, db.state=%s",
				current.getName(),
				next.getName());
		throw new StaleStateException(err);
	}
New to GrepCode? Check out our FAQ X