package com.casamind.adware.server.proxy;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.casamind.adware.server.dao.ObjectifyGenericDAO;
import com.casamind.adware.server.domain.Ad;
import com.casamind.adware.server.domain.Company;
import com.casamind.adware.server.domain.OrphanBlob;
import com.casamind.adware.server.domain.Product;
import com.casamind.adware.server.domain.Publisher;
import com.casamind.adware.server.domain.Resource;
import com.casamind.adware.server.domain.Schedule;
import com.casamind.adware.server.domain.Slot;
import com.casamind.adware.server.domain.UserAccount;
import com.casamind.adware.shared.GoogleDocumentsTaskTypes;
import com.casamind.adware.shared.model.CompanyDTO;
import com.casamind.adware.shared.model.GroupDTO;
import com.casamind.adware.shared.model.ProductDTO;
import com.casamind.adware.shared.model.PublisherDTO;
import com.casamind.adware.shared.model.ResourceDTO;
import com.casamind.adware.shared.model.SlotDTO;
import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreFailureException;
import com.google.appengine.api.blobstore.BlobstoreInputStream;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.googlecode.objectify.Key;
public class DatastoreProxy {
public static UserAccount getUserAccountById(Long id) {
UserAccount user = null;
user = new ObjectifyGenericDAO<UserAccount>(UserAccount.class).getByProperty("id", id);
if (user != null)
return user;
user = new ObjectifyGenericDAO<Company>(Company.class).getByProperty("id", id);
if (user != null)
return user;
user = new ObjectifyGenericDAO<Publisher>(Publisher.class).getByProperty("id", id);
if (user != null)
return user;
return null;
}
public static UserAccount getUserAccountByLogin(String login) {
UserAccount user = null;
user = new ObjectifyGenericDAO<UserAccount>(UserAccount.class).getByProperty("login", login);
if (user != null)
return user;
user = new ObjectifyGenericDAO<Company>(Company.class).getByProperty("login", login);
if (user != null)
return user;
user = new ObjectifyGenericDAO<Publisher>(Publisher.class).getByProperty("login", login);
if (user != null)
return user;
return null;
}
public static List<Company> getCompanies() {
return new ObjectifyGenericDAO<Company>(Company.class).listAll();
}
public static Company getCompanyByLogin(String login) {
return new ObjectifyGenericDAO<Company>(Company.class).getByProperty("login", login);
}
public static Publisher getPublisherByLogin(String login) {
return new ObjectifyGenericDAO<Publisher>(Publisher.class).getByProperty("login", login);
}
public static List<Publisher> getPublishersByCompanyId(Long id) {
return new ObjectifyGenericDAO<Publisher>(Publisher.class).listByProperty("companyId", id);
}
public static List<Publisher> getPublishersByGroupId(Long id) {
return new ObjectifyGenericDAO<Publisher>(Publisher.class).listByProperty("groupId", id);
}
public static List<GroupDTO> getGroupsByCompanyId(Long id) {
return new ObjectifyGenericDAO<GroupDTO>(GroupDTO.class).listByProperty("companyId", id);
}
public static Company getCompanyById(Long id) {
return new ObjectifyGenericDAO<Company>(Company.class).getByProperty("id", id);
}
public static Publisher getPublisherById(Long id) {
return new ObjectifyGenericDAO<Publisher>(Publisher.class).getByProperty("id", id);
}
public static Product getProductById(Long id) {
return new ObjectifyGenericDAO<Product>(Product.class).getByProperty("id", id);
}
public static Slot getSlotById(Long id) {
return new ObjectifyGenericDAO<Slot>(Slot.class).getByProperty("id", id);
}
public static Company updateCompany(CompanyDTO dto) {
Company entity = Company.toEntity(getCompanyById(dto.getId()), dto);
if (entity != null)
new ObjectifyGenericDAO<Company>(Company.class).put(entity);
return entity;
}
public static void deleteCompany(Long id) {
new ObjectifyGenericDAO<Company>(Company.class).delete(getCompanyById(id));
}
public static Company createCompany(Company entity) {
ObjectifyGenericDAO<Company> dao = new ObjectifyGenericDAO<Company>(Company.class);
Company datastoreItem = null;
if (entity != null) {
Key<Company> key = dao.put(entity);
try {
datastoreItem = dao.get(key);
} catch (EntityNotFoundException e) {
e.printStackTrace();
}
}
return datastoreItem;
}
public static Schedule createSchedule(Schedule entity) {
ObjectifyGenericDAO<Schedule> dao = new ObjectifyGenericDAO<Schedule>(Schedule.class);
Schedule datastoreItem = null;
if (entity != null) {
Key<Schedule> key = dao.put(entity);
try {
datastoreItem = dao.get(key);
} catch (EntityNotFoundException e) {
e.printStackTrace();
}
}
return datastoreItem;
}
public static Schedule getScheduleById(Long id) {
return new ObjectifyGenericDAO<Schedule>(Schedule.class).getByProperty("id", id);
}
public static Ad getAdById(Long id) {
return new ObjectifyGenericDAO<Ad>(Ad.class).getByProperty("id", id);
}
public static Publisher updatePublisher(PublisherDTO dto) {
Publisher entity = Publisher.toEntity(getPublisherById(dto.getId()), dto);
if (entity != null)
new ObjectifyGenericDAO<Publisher>(Publisher.class).put(entity);
return entity;
}
public static void deletePublisher(Long id) {
new ObjectifyGenericDAO<Publisher>(Publisher.class).delete(getPublisherById(id));
}
public static Publisher createPublisher(Publisher entity) {
ObjectifyGenericDAO<Publisher> dao = new ObjectifyGenericDAO<Publisher>(Publisher.class);
Publisher datastoreItem = null;
if (entity != null) {
Key<Publisher> key = dao.put(entity);
try {
datastoreItem = dao.get(key);
} catch (EntityNotFoundException e) {
e.printStackTrace();
}
}
return datastoreItem;
}
public static List<Resource> getResourcesByProductId(Long id) {
return new ObjectifyGenericDAO<Resource>(Resource.class).listByProperty("productId", id);
}
public static List<Product> getProductsByPublisherId(Long id) {
return new ObjectifyGenericDAO<Product>(Product.class).listByProperty("publisherId", id);
}
public static List<Slot> getSlotsByOwnerId(Long id) {
return new ObjectifyGenericDAO<Slot>(Slot.class).listByProperty("ownerId", id);
}
public static List<Slot> getSlotsByStatus(int status) {
return new ObjectifyGenericDAO<Slot>(Slot.class).listByProperty("status", status);
}
public static List<Slot> getSlotsByOwnerIdAndStatus(Long id, int status) {
return new ObjectifyGenericDAO<Slot>(Slot.class).ofy().query(Slot.class).filter("ownerId ==", id).filter("status ==", status).list();
}
public static List<Slot> getSlotsByCompanyIdAndStatus(Long id, Integer status) {
List<Slot> list = new ArrayList<Slot>();
// slots booked by the company
list.addAll(getSlotsByOwnerIdAndStatus(id, status));
// slots booked by publishers
for (Publisher entity : getPublishersByCompanyId(id)) {
list.addAll(getSlotsByOwnerIdAndStatus(entity.getId(), status));
}
return list;
}
public static List<Slot> getSlotsByStatus(Date startDate, Date endDate, Boolean isConfigured, Boolean isSequential, Integer status) {
List<Slot> list = new ArrayList<Slot>();
for (Company entity : getCompanies()) {
list.addAll(getSlotsByCompanyIdAndStatus(entity.getId(), status));
}
return list;
}
public static List<Slot> getSlotsByPublisherIdAndDates(Long id, Date startDate, Date endDate, Boolean isConfigured, Boolean isSequential, Integer status) {
// 'tmp' collection is to avoid concurrency exception
List<Slot> list = new ArrayList<Slot>();
List<Slot> tmp = new ArrayList<Slot>();
list.addAll(new ObjectifyGenericDAO<Slot>(Slot.class).ofy().query(Slot.class).filter("ownerId ==", id).filter("startDate >=", startDate).filter("startDate <", endDate).list());
tmp.addAll(list);
// performing other filters
if (status != null) {
if (isConfigured == null && isSequential != null) {
for (Slot entity : tmp) {
if (entity.isSequential() != isSequential || entity.getStatus() != status) {
list.remove(list.indexOf(entity));
}
}
} else if (isConfigured != null && isSequential == null) {
for (Slot entity : tmp) {
if (entity.isConfigured() != isConfigured || entity.getStatus() != status) {
list.remove(list.indexOf(entity));
}
}
} else if (isConfigured != null && isSequential != null) {
for (Slot entity : tmp) {
if (entity.isConfigured() != isConfigured || entity.isConfigured() != isConfigured || entity.getStatus() != status) {
list.remove(list.indexOf(entity));
}
}
} else {
for (Slot entity : tmp) {
if (entity.getStatus() != status) {
list.remove(list.indexOf(entity));
}
}
}
} else {
if (isConfigured == null && isSequential != null) {
for (Slot entity : tmp) {
if (entity.isSequential() != isSequential) {
list.remove(list.indexOf(entity));
}
}
} else if (isConfigured != null && isSequential == null) {
for (Slot entity : tmp) {
if (entity.isConfigured() != isConfigured) {
list.remove(list.indexOf(entity));
}
}
} else if (isConfigured != null && isSequential != null) {
for (Slot entity : tmp) {
if (entity.isConfigured() != isConfigured || entity.isConfigured() != isConfigured) {
list.remove(list.indexOf(entity));
}
}
}
}
return list;
}
public static List<Slot> getSlotsByCompanyIdAndDates(Long id, Date startDate, Date endDate, Boolean isConfigured, Boolean isSequential, Integer status) {
List<Slot> list = new ArrayList<Slot>();
// slots booked by the company
list.addAll(getSlotsByPublisherIdAndDates(id, startDate, endDate, isConfigured, isSequential, status));
// slots booked by publishers
for (Publisher entity : getPublishersByCompanyId(id)) {
list.addAll(getSlotsByPublisherIdAndDates(entity.getId(), startDate, endDate, isConfigured, isSequential, status));
}
return list;
}
public static List<Slot> getSlotsByDates(Date startDate, Date endDate, Boolean isConfigured, Boolean isSequential, Integer status) {
List<Slot> list = new ArrayList<Slot>();
for (Company entity : getCompanies()) {
list.addAll(getSlotsByCompanyIdAndDates(entity.getId(), startDate, endDate, isConfigured, isSequential, status));
}
return list;
}
public static List<Slot> getOrphanSlots() {
return new ObjectifyGenericDAO<Slot>(Slot.class).listByProperty("ownerId", null);
}
public static List<Slot> getSlotsByProductId(Long id) {
List<Slot> list = new ArrayList<Slot>();
Product product = getProductById(id);
if (product != null) {
for (Slot slot : getSlotsByOwnerId(product.getPublisherId())) {
if (slot.getProductIds().contains(id))
list.add(slot);
}
}
return list;
}
public static void deleteProduct(Long id) {
new ObjectifyGenericDAO<Product>(Product.class).delete(getProductById(id));
}
public static void deleteSlot(Long id) {
new ObjectifyGenericDAO<Slot>(Slot.class).delete(getSlotById(id));
}
public static UserAccount updateUser(UserAccount entity) {
if (entity != null)
new ObjectifyGenericDAO<UserAccount>(UserAccount.class).put(entity);
return entity;
}
public static Product updateProduct(ProductDTO dto) {
Product entity = Product.toEntity(getProductById(dto.getId()), dto);
if (entity != null)
new ObjectifyGenericDAO<Product>(Product.class).put(entity);
return entity;
}
public static Slot updateSlot(SlotDTO dto) {
Slot entity = Slot.toEntity(getSlotById(dto.getId()), dto);
if (entity != null)
new ObjectifyGenericDAO<Slot>(Slot.class).put(entity);
return entity;
}
public static Product createProduct(Product entity) {
ObjectifyGenericDAO<Product> dao = new ObjectifyGenericDAO<Product>(Product.class);
Product datastoreItem = null;
if (entity != null) {
Key<Product> key = dao.put(entity);
try {
datastoreItem = dao.get(key);
} catch (EntityNotFoundException e) {
e.printStackTrace();
}
}
return datastoreItem;
}
public static Slot createSlot(Slot entity) {
ObjectifyGenericDAO<Slot> dao = new ObjectifyGenericDAO<Slot>(Slot.class);
Slot datastoreItem = null;
if (entity != null) {
Key<Slot> key = dao.put(entity);
try {
datastoreItem = dao.get(key);
} catch (EntityNotFoundException e) {
e.printStackTrace();
}
}
return datastoreItem;
}
public static Resource getResourceById(Long id) {
return new ObjectifyGenericDAO<Resource>(Resource.class).getByProperty("id", id);
}
public static void deleteResource(Long id) {
new ObjectifyGenericDAO<Resource>(Resource.class).delete(getResourceById(id));
}
public static Resource updateResource(ResourceDTO dto) {
Resource entity = Resource.toEntity(getResourceById(dto.getId()), dto);
if (entity != null)
new ObjectifyGenericDAO<Resource>(Resource.class).put(entity);
return entity;
}
public static Resource createResource(Resource entity) {
ObjectifyGenericDAO<Resource> dao = new ObjectifyGenericDAO<Resource>(Resource.class);
Resource datastoreItem = null;
if (entity != null) {
Key<Resource> key = dao.put(entity);
try {
datastoreItem = dao.get(key);
} catch (EntityNotFoundException e) {
e.printStackTrace();
}
}
return datastoreItem;
}
public static List<Resource> getResourcesByPublisherId(Long id) {
List<Resource> list = new ArrayList<Resource>();
for (Product product : getProductsByPublisherId(id)) {
list.addAll(getResourcesByProductId(product.getId()));
}
return list;
}
public static List<Resource> getResourcesByCompanyId(Long id) {
List<Resource> list = new ArrayList<Resource>();
for (Publisher publisher : getPublishersByCompanyId(id)) {
list.addAll(getResourcesByPublisherId(publisher.getId()));
}
return list;
}
public static List<Product> getProductsBySlotId(Long id) {
return new ObjectifyGenericDAO<Product>(Product.class).listByProperty("publisherId", id);
}
public static void getBlob(String key) {
try {
DatastoreServiceFactory.getDatastoreService().get(KeyFactory.stringToKey(key));
} catch (EntityNotFoundException e) {
e.printStackTrace();
}
}
public static void deleteBlob(String key) {
try {
BlobstoreServiceFactory.getBlobstoreService().delete(new BlobKey(key));
} catch (BlobstoreFailureException e) {
QueueFactory.getQueue("blob").add(TaskOptions.Builder.withUrl("/tasks/blobstore").param("task", GoogleDocumentsTaskTypes.DELETE).param("blobKey", key));
}
}
public static BlobInfo getBlobInfo(String key) {
return new BlobInfoFactory().loadBlobInfo(new BlobKey(key));
}
public static OrphanBlob createOrphantBlob(OrphanBlob entity){
ObjectifyGenericDAO<OrphanBlob> dao = new ObjectifyGenericDAO<OrphanBlob>(OrphanBlob.class);
OrphanBlob datastoreItem = null;
if (entity != null) {
Key<OrphanBlob> key = dao.put(entity);
try {
datastoreItem = dao.get(key);
} catch (EntityNotFoundException e) {
e.printStackTrace();
}
}
return datastoreItem;
}
public static OrphanBlob getOrphantBlobByKey(String blobKey){
return new ObjectifyGenericDAO<OrphanBlob>(OrphanBlob.class).getByProperty("blobKey", blobKey);
}
public static String getMimeType(String key) {
return new BlobInfoFactory().loadBlobInfo(new BlobKey(key)).getContentType();
}
public static byte[] getMediaFromBlob(String key) {
byte[] byteArray = null;
BlobKey blobKey = new BlobKey(key);
BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey);
BlobstoreInputStream stream;
try {
stream = new BlobstoreInputStream(blobKey);
byteArray = new byte[(int) blobInfo.getSize()];
stream.read(byteArray);
} catch (IOException e) {
e.printStackTrace();
}
return byteArray;
}
public static UserAccount getUserAccountByUniqueId(String uniqueId) {
UserAccount user = null;
user = new ObjectifyGenericDAO<UserAccount>(UserAccount.class).getByProperty("uniqueId", uniqueId);
if (user != null)
return user;
user = new ObjectifyGenericDAO<Company>(Company.class).getByProperty("uniqueId", uniqueId);
if (user != null)
return user;
user = new ObjectifyGenericDAO<Publisher>(Publisher.class).getByProperty("uniqueId", uniqueId);
if (user != null)
return user;
return null;
}
public static UserAccount getOwnerById(Long ownerId) {
Company company = getCompanyById(ownerId);
if (company != null){
return company;
} else {
return getPublisherById(ownerId);
}
}
}