package com.casamind.adware.server.domain;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import com.casamind.adware.server.proxy.DatastoreProxy;
import com.casamind.adware.shared.model.CompanySummaryDTO;
import com.casamind.adware.shared.model.ProductSummaryDTO;
import com.casamind.adware.shared.model.PublisherDTO;
import com.casamind.adware.shared.model.PublisherSummaryDTO;
import com.casamind.adware.shared.model.SlotSummaryDTO;
public class Publisher extends UserAccount {
private Long companyId;
private Long groupId;
public Publisher(int accessLevel, String login, String service, String firstname, String lastname, String phone, String email, Date lastLoginOn, boolean isReceiveNewsLetter, boolean isReceiveNotifications, Long companyId, Long groupId, String gdataLogin, String gdataPassword) {
super(accessLevel, login, service, firstname, lastname, phone, email, lastLoginOn, isReceiveNewsLetter, isReceiveNotifications, gdataLogin, gdataPassword);
this.companyId = companyId;
this.groupId = groupId;
}
public Publisher() {
}
public Long getCompanyId() {
return companyId;
}
public void setCompanyId(Long companyId) {
this.companyId = companyId;
}
public void setGroupId(Long groupId) {
this.groupId = groupId;
}
public Long getGroupId() {
return groupId;
}
@Override
public String toString() {
return super.toString() + ", companyId=" + companyId + "]";
}
public static PublisherSummaryDTO toSummaryDTO(Publisher entity) {
if (entity == null)
return null;
return new PublisherSummaryDTO(entity.getId(), entity.getFirstname(), entity.getLastname(), entity.getGroupId());
}
public static PublisherDTO toDTO(Publisher entity) {
if (entity == null)
return null;
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("Africa/Casablanca"));
// get a calendar instance, which defaults to "now"
// get a date to represent "today" midnight
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date today = calendar.getTime();
CompanySummaryDTO company = null;
List<Product> products = new ArrayList<Product>();
List<Slot> slots = new ArrayList<Slot>();
List<ProductSummaryDTO> productsDTO = new ArrayList<ProductSummaryDTO>();
List<SlotSummaryDTO> slotsDTO = new ArrayList<SlotSummaryDTO>();
products.addAll(DatastoreProxy.getProductsByPublisherId(entity.getId()));
slots.addAll(DatastoreProxy.getSlotsByOwnerId(entity.getId()));
company = Company.toSummaryDTO(DatastoreProxy.getCompanyById(entity.getCompanyId()));
for (Product p : products) {
productsDTO.add(Product.toSummaryDTO(p));
}
for (Slot s : slots) {
if (s != null && s.getEndDate().compareTo(today) >= 0)
slotsDTO.add(Slot.toSummaryDTO(s));
}
return new PublisherDTO(entity.getId(), entity.getUUID(), entity.getAccessLevel(), entity.getLogin(), entity.getService(), entity.getFirstname(), entity.getLastname(), entity.getPhone(), entity.getEmail(), entity.getLastLoginOn(), entity.isReceiveNewsLetter(), entity.isReceiveNotifications(), entity.getGroupId(), entity.getBudget(), company, slotsDTO, productsDTO);
}
public static Publisher toEntity(Publisher entity, PublisherDTO dto) {
if (dto == null) {
return null;
}
if (entity == null) {
entity = new Publisher();
}
entity.setAccessLevel(dto.getAccessLevel());
entity.setEmail(dto.getEmail());
entity.setPhone(dto.getPhone());
entity.setLogin(dto.getLogin());
entity.setService(dto.getService());
entity.setUniqueId(dto.getLogin() + "-" + dto.getService());
entity.setFirstname(dto.getFirstname());
entity.setLastname(dto.getLastname());
entity.setCompanyId(dto.getCompany().getId());
entity.setReceiveNewsLetter(dto.isReceiveNewsLetter());
entity.setReceiveNotifications(dto.isReceiveNotifications());
entity.setBudget(dto.getBudget());
return entity;
}
}