package com.ning.billing.catalog;
import com.ning.billing.catalog.api.Product;
import com.ning.billing.catalog.api.ProductCategory;
import com.ning.billing.util.config.ValidatingConfig;
import com.ning.billing.util.config.ValidationErrors;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlID;
import javax.xml.bind.annotation.XmlIDREF;
import java.net.URI;
import java.util.Arrays;
@XmlAccessorType(XmlAccessType.NONE)
public class DefaultProduct extends ValidatingConfig<StandaloneCatalog> implements Product {
private static final DefaultProduct[] EMPTY_PRODUCT_LIST = new DefaultProduct[0];
@XmlAttribute(required=true)
@XmlID
private String name;
@XmlAttribute(required=false)
private Boolean retired = false;
@XmlElement(required=true)
private ProductCategory category;
@XmlElementWrapper(name="included", required=false)
@XmlIDREF @XmlElement(name="addonProduct", required=true)
private DefaultProduct[] included = EMPTY_PRODUCT_LIST;
@XmlElementWrapper(name="available", required=false)
@XmlIDREF @XmlElement(name="addonProduct", required=true)
private DefaultProduct[] available = EMPTY_PRODUCT_LIST;
private String catalogName;
@Override
public String getCatalogName() {
return catalogName;
}
@Override
public boolean isRetired() {
return retired;
}
@Override
public ProductCategory getCategory() {
return category;
}
@Override
public DefaultProduct[] getIncluded() {
return included;
}
@Override
public DefaultProduct[] getAvailable() {
return available;
}
public DefaultProduct() {
}
public DefaultProduct(String name, ProductCategory category) {
this.category = category;
this.name = name;
}
@Override
public String getName() {
return name;
}
public boolean isIncluded(DefaultProduct addon) {
for(DefaultProduct p : included) {
if (addon == p) {
return true;
}
}
return false;
}
public boolean isAvailable(DefaultProduct addon) {
for(DefaultProduct p : included) {
if (addon == p) {
return true;
}
}
return false;
}
@Override
public void initialize(StandaloneCatalog catalog, URI sourceURI) {
catalogName = catalog.getCatalogName();
}
@Override
public ValidationErrors validate(StandaloneCatalog catalog, ValidationErrors errors) {
return errors;
}
protected DefaultProduct setName(String name) {
this.name = name;
return this;
}
protected DefaultProduct setCatagory(ProductCategory category) {
this.category = category;
return this;
}
protected DefaultProduct setCategory(ProductCategory category) {
this.category = category;
return this;
}
protected DefaultProduct setIncluded(DefaultProduct[] included) {
this.included = included;
return this;
}
protected DefaultProduct setAvailable(DefaultProduct[] available) {
this.available = available;
return this;
}
protected DefaultProduct setCatalogName(String catalogName) {
this.catalogName = catalogName;
return this;
}
public DefaultProduct setRetired(boolean retired) {
this.retired = retired;
return this;
}
@Override
public String toString() {
return "DefaultProduct [name=" + name + ", retired=" + retired + ", category=" + category + ", included="
+ Arrays.toString(included) + ", available=" + Arrays.toString(available) + ", catalogName="
+ catalogName + "]";
}
}