All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.jhonsapp.bootstrap.object.service.generic.AbstractService Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
package br.com.jhonsapp.bootstrap.object.service.generic;

import br.com.jhonsapp.bootstrap.object.domain.generic.DomainObject;
import br.com.jhonsapp.bootstrap.object.persistence.generic.GenericDAO;

public abstract class AbstractService implements Service {
	private static final long serialVersionUID = 3583603527173873467L;

	@Override
	public boolean saveOrUpdate(T object) {

		if (isStateInvalid(object))
			throw new IllegalStateException("The object state is illegal.");

		if (!isUnique(object))
			throw new IllegalArgumentException("The object already exists.");

		preSaveOrUpdate(object);

		boolean result = false;

		if (object.getId() == 0L)
			result = getDAO().persist(object);
		else
			result = getDAO().update(object) != null;

		postSaveOrUpdate(object, result);

		return result;
	}

	protected void preSaveOrUpdate(T object) {
	}

	protected void postSaveOrUpdate(T object, boolean result) {
	}

	protected abstract GenericDAO getDAO();

	public abstract boolean isStateInvalid(T object);

	public abstract boolean isUnique(T object);

	public abstract boolean hasObject(T object);

	@Override
	public boolean remove(T object) {

		preRemove(object);

		boolean result = getDAO().remove(object);

		postRemove(object, result);

		return result;
	}

	protected void preRemove(T object) {
	}

	protected void postRemove(T object, boolean result) {
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy