/*
 * Copyright (c) 2011, Patrik Dufresne. All rights reserved.
 * Patrik Dufresne PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.planimod.ui.views.admin.product;

import java.util.Collection;
import java.util.Collections;

import org.eclipse.core.databinding.observable.IObservable;
import org.eclipse.core.databinding.observable.value.IObservableValue;

import com.patrikdufresne.managers.ManagerException;
import com.patrikdufresne.managers.databinding.ManagedObjectComputedSet;
import com.planimod.core.PlanimodManagers;
import com.planimod.core.Product;
import com.planimod.core.ProductPosition;

public class ProductPositionByProductComputedSet extends
		ManagedObjectComputedSet {

	protected IObservableValue product;

	public ProductPositionByProductComputedSet(PlanimodManagers managers,
			IObservableValue product) {
		super(managers, ProductPosition.class, new IObservable[] { product });
		this.product = product;
	}

	@Override
	protected Collection doList() throws ManagerException {
		if (getProduct() == null) {
			return Collections.EMPTY_SET;
		}
		return ((PlanimodManagers) getManagers()).getProductPositionManager()
				.listByProduct(getProduct());
	}

	@Override
	protected boolean doSelect(Object element) {
		if (getProduct() == null) {
			return false;
		}
		return element instanceof ProductPosition
				&& ((ProductPosition) element).getProduct()
						.equals(getProduct());
	}

	protected Product getProduct() {
		if (this.product.getValue() instanceof Product) {
			return (Product) this.product.getValue();
		}
		return null;
	}

}
