/*
* Copyright 2011-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pl.com.bottega.ecommerce.sales.domain.purchase;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import pl.com.bottega.ddd.annotations.domain.AggregateRoot;
import pl.com.bottega.ddd.support.domain.BaseAggregateRoot;
import pl.com.bottega.ecommerce.canonicalmodel.events.OrderSubmittedEvent;
import pl.com.bottega.ecommerce.canonicalmodel.publishedlanguage.AggregateId;
import pl.com.bottega.ecommerce.canonicalmodel.publishedlanguage.ClientData;
import pl.com.bottega.ecommerce.sharedkernel.Money;
/**
* Models fact of purchase.
*
* @author Slawek
*
*/
@Entity
@AggregateRoot
public class Purchase extends BaseAggregateRoot{
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@Fetch(FetchMode.JOIN)
@OrderColumn(name = "itemNumber")
@JoinColumn(name = "purchase_id")
private List<PurchaseItem> items;
private boolean paid;
@Embedded
private ClientData clientData;
private Date purchaseDate;
@Embedded
private Money totalCost;
@SuppressWarnings("unused")
private Purchase() {}
Purchase(AggregateId aggregateId, ClientData clientData, List<PurchaseItem> items, Date purchaseDate,
boolean paid, Money totalCost){
this.aggregateId = aggregateId;
this.clientData = clientData;
this.items = items;
this.purchaseDate = purchaseDate;
this.paid = paid;
this.totalCost = totalCost;
}
public void confirm() {
paid = true;
eventPublisher.publish(new OrderSubmittedEvent(getAggregateId()));
}
public boolean isPaid() {
return paid;
}
public Money getTotalCost() {
return totalCost;
}
public Date getPurchaseDate() {
return purchaseDate;
}
public ClientData getClientData() {
return clientData;
}
public Collection<PurchaseItem> getItems() {
return (Collection<PurchaseItem>) Collections.unmodifiableCollection(items);
}
}