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

org.jboss.pressgang.ccms.wrapper.collection.DBUpdateableCollectionWrapper Maven / Gradle / Ivy

There is a newer version: 1.9
Show newest version
package org.jboss.pressgang.ccms.wrapper.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.jboss.pressgang.ccms.provider.exception.BadRequestException;
import org.jboss.pressgang.ccms.wrapper.DBWrapperFactory;
import org.jboss.pressgang.ccms.wrapper.base.BaseWrapper;
import org.jboss.pressgang.ccms.wrapper.collection.base.CollectionEventListener;
import org.jboss.pressgang.ccms.wrapper.collection.base.UpdateableCollectionEventListener;
import org.jboss.pressgang.ccms.wrapper.collection.handler.DBCollectionHandler;
import org.jboss.pressgang.ccms.wrapper.collection.handler.DBUpdateableCollectionHandler;

public abstract class DBUpdateableCollectionWrapper, U> extends DBCollectionWrapper implements UpdateableCollectionWrapper {
    private static final Integer UPDATE_STATE = 3;

    protected DBUpdateableCollectionWrapper(final DBWrapperFactory wrapperFactory, final Collection items, boolean isRevisionList,
            Class wrapperClass) {
        super(wrapperFactory, items, isRevisionList, wrapperClass);
    }

    protected DBUpdateableCollectionWrapper(final DBWrapperFactory wrapperFactory, final Collection items, boolean isRevisionList,
            Class wrapperClass, final DBCollectionHandler handler) {
        super(wrapperFactory, items, isRevisionList, wrapperClass, handler);
    }

    @Override
    @SuppressWarnings("unchecked")
    public void addUpdateItem(T entity) {
        final U unwrappedEntity = (U) entity.unwrap();
        if (getCollectionItems().contains(unwrappedEntity)) {
            getCollection().put(entity, UPDATE_STATE);
            if (!isRevisionList()) {
                notifyOnUpdateEvent(unwrappedEntity);
            }
        } else {
            throw new BadRequestException("Update Entity is not part of the collection");
        }
    }

    @Override
    public List getUpdateItems() {
        final List updateItems = new ArrayList();
        for (final Map.Entry entity : getCollection().entrySet()) {
            if (UPDATE_STATE.equals(entity.getValue())) {
                updateItems.add(entity.getKey());
            }
        }

        return updateItems;
    }

    public void registerEventListener(UpdateableCollectionEventListener listener) {
        super.registerEventListener(listener);
    }

    private void notifyOnUpdateEvent(U entity) {
        if (getHandler() instanceof DBUpdateableCollectionHandler) {
            ((DBUpdateableCollectionHandler) getHandler()).updateItem(getCollectionItems(), entity);
        }
        for (final CollectionEventListener listener : getEventListeners()) {
            if (listener instanceof UpdateableCollectionEventListener) {
                ((UpdateableCollectionEventListener) listener).onUpdateItem(entity);
            }
        }
    }
}