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

dk.apaq.crud.core.BaseEditableCrud Maven / Gradle / Ivy

/*
 * CrudContainer
 * Copyright (C) 2011 by Apaq
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 */
package dk.apaq.crud.core;

import dk.apaq.crud.Crud;
import dk.apaq.crud.CrudEvent;
import dk.apaq.crud.CrudListener;

/**
 * An abstract implementation of a Crud.Editable instance taking care
 * of CrudNotifer logic. An implementing class needs only to call the fire* methods(fx. fireOnRead)
 * and this class will make sure to fire evetns on the listeners.
 */
public abstract class BaseEditableCrud extends BaseCrud implements Crud.Editable {

    /**
     * Tells listeners that a new entity is about to be created.
     */
    protected void fireOnBeforeCreate(BT entity) {
        CrudEvent.WithEntity e = createEventWithEntity(entity);

        for (CrudListener listener : getListeners()) {
            listener.onBeforeEntityCreate(e);
        }
    }

    /**
     * Tells listeners that an entity is about to be updated.
     * @param entity The entity that holds changes and is about to be persisted.
     */
    protected void fireOnBeforeUpdate(IDT id, BT entity) {
        CrudEvent.WithIdAndEntity e = createEventWithIdAndEntity(id, entity);

        for (CrudListener listener : getListeners()) {
            listener.onBeforeEntityUpdate(e);
        }
    }

    /**
     * Tells listeners that an eneity is about to be deleted.
     * @param id The id of the entity that is about to be deleted.
     */
    protected void fireOnBeforeDelete(IDT id) {
        CrudEvent.WithId e = createEventWithId(id);
        for (CrudListener listener : getListeners()) {
            listener.onBeforeEntityDelete(e);
        }
    }

    /**
     * Tells listeners that an entity has been created.
     * @param entity The entity that was created.
     */
    protected void fireOnCreate(IDT id, BT entity) {
        CrudEvent.WithIdAndEntity e = createEventWithIdAndEntity(id, entity);

        for (CrudListener listener : getListeners()) {
            listener.onEntityCreate(e);
        }
    }

    /**
     * Tells listeners that an entity was updated.
     * @param entity The entity that was updated.
     */
    protected void fireOnUpdate(IDT id, BT entity) {
        CrudEvent.WithIdAndEntity e = createEventWithIdAndEntity(id, entity);

        for (CrudListener listener : getListeners()) {
            listener.onEntityUpdate(e);
        }
    }

    /**
     * Tells listeners that an entity was deleted.
     * @param id The id of the entity that was deleted.
     */
    protected void fireOnDelete(IDT id) {
        CrudEvent.WithId e = createEventWithId(id);
        for (CrudListener listener : getListeners()) {
            listener.onEntityDelete(e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy