package com.casamind.adware.server.domain;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.casamind.adware.server.proxy.DatastoreProxy;
import com.casamind.adware.shared.SlotStatus;
import com.casamind.adware.shared.model.CompanySummaryDTO;
import com.casamind.adware.shared.model.ProductSummaryDTO;
import com.casamind.adware.shared.model.PublisherSummaryDTO;
import com.casamind.adware.shared.model.SlotDTO;
import com.casamind.adware.shared.model.SlotSummaryDTO;
public class Slot extends DatastoreObject {
private Boolean isConfigured = false;
private Boolean isSequential = false;
private Integer status;
private Integer price;
private Integer nbSlots;
private Long invoiceId;
private Date startDate;
private Date endDate;
private Long ownerId;
private List<Long> productIds;
public Slot(Integer price, Integer nbSlots, Date startDate, Date endDate, Long ownerId) {
this.isSequential = false;
this.isConfigured = false;
this.price = price;
this.nbSlots = nbSlots;
this.startDate = startDate;
this.endDate = endDate;
this.ownerId = ownerId;
this.status = SlotStatus.Ordered;
this.productIds = new ArrayList<Long>();
}
public Slot() {
}
public Boolean isConfigured() {
return isConfigured && this.getProductIds().size() > 0;
}
public void setConfigured(Boolean isConfigured) {
this.isConfigured = isConfigured;
}
public Boolean isSequential() {
return isSequential;
}
public void setIsSequential(Boolean isSequential) {
this.isSequential = isSequential;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public Integer getNbSlots() {
return nbSlots;
}
public void setNbSlots(Integer nbSlots) {
this.nbSlots = nbSlots;
}
public Long getInvoiceId() {
return invoiceId;
}
public void setInvoiceId(Long invoiceId) {
this.invoiceId = invoiceId;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public Long getOwnerId() {
return ownerId;
}
public void setOwnerId(Long ownerId) {
this.ownerId = ownerId;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public List<Long> getProductIds() {
return productIds == null ? productIds = new ArrayList<Long>() : productIds;
}
public void setProductIds(List<Long> productIds) {
this.productIds = productIds;
}
@Override
public String toString() {
return "Slot [isConfigured=" + isConfigured + ", isSequential=" + isSequential + ", status=" + status + ", price=" + price + ", nbSlots=" + nbSlots + ", startDate=" + startDate + ", endDate=" + endDate + ", ownerId=" + ownerId + ", productIds=" + productIds + "]";
}
public static SlotSummaryDTO toSummaryDTO(Slot entity) {
if (entity == null)
return null;
return new SlotSummaryDTO(entity.isConfigured(), entity.getId(), entity.getStatus(), entity.getPrice(), entity.getProductIds().size(), entity.getStartDate(), entity.getEndDate());
}
public static SlotDTO toDTO(Slot entity) {
if (entity == null)
return null;
List<ProductSummaryDTO> products = new ArrayList<ProductSummaryDTO>();
Publisher publisher = null;
PublisherSummaryDTO publisherSummary = null;
CompanySummaryDTO companySummary = null;
for (Long id : entity.getProductIds()) {
products.add(Product.toSummaryDTO(DatastoreProxy.getProductById(id)));
}
publisher = DatastoreProxy.getPublisherById(entity.getOwnerId());
if (publisher == null) {
// Slot has been booked by company
companySummary = Company.toSummaryDTO(DatastoreProxy.getCompanyById(entity.getOwnerId()));
publisherSummary = new PublisherSummaryDTO();
if (companySummary == null) {
// should not happen
companySummary = new CompanySummaryDTO();
}
} else {
publisherSummary = Publisher.toSummaryDTO(publisher);
companySummary = Company.toSummaryDTO(DatastoreProxy.getCompanyById(publisher.getCompanyId()));
}
return new SlotDTO(entity.isConfigured(), entity.isSequential(), entity.getId(), entity.getOwnerId(), entity.getUUID(), entity.getStatus(), entity.getPrice(), entity.getNbSlots(), entity.getStartDate(), entity.getEndDate(), companySummary, publisherSummary, products);
}
public static Slot toEntity(Slot entity, SlotDTO dto) {
if (dto == null) {
return null;
}
if (entity == null) {
entity = new Slot();
}
entity.setOwnerId(dto.getOwnerId());
entity.setConfigured(dto.isConfigured());
entity.setEndDate(dto.getEndDate());
entity.setStartDate(dto.getStartDate());
entity.setIsSequential(dto.isSequential());
entity.setStatus(new Integer(dto.getStatus()));
entity.setNbSlots(new Integer(dto.getProducts().size()));
entity.setPrice(new Integer(dto.getPrice()));
entity.getProductIds().clear();
for (ProductSummaryDTO product : dto.getProducts()) {
entity.getProductIds().add(product.getId());
}
return entity;
}
public static Map<CompanySummaryDTO, List<SlotDTO>> sortByCompany(List<SlotDTO> objects){
Map<CompanySummaryDTO, List<SlotDTO>> map = new HashMap<CompanySummaryDTO, List<SlotDTO>>();
for (SlotDTO object : objects) {
if (map.containsKey(object.getCompany())) {
map.get(object.getCompany()).add(object);
} else {
List<SlotDTO> slots = new ArrayList<SlotDTO>();
slots.add(object);
map.put(object.getCompany(), slots);
}
}
return map;
}
}