/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Java.ORM;
import Java.DatabaseKoppeling;
import Java.Email;
import Java.Shoppingcart;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
/**
*
* @author Geert
*/
public class DatabaseORM {
public static final String entityManagerFactoryString = "WebApplicationPU";
/**
* Create a new account in the database.
* @param em the entitymanager to use.
* @param account the account that has to be added to the database.
* @return true if the account has successfully been added to the database, otherwise false.
*/
public static boolean insertAccount(EntityManager em, Account account) {
try {
em.getTransaction().begin();
String psw = account.getPassword();
account.setPassword(DatabaseKoppeling.encrypt(psw));
em.persist(account);
if (account.getPhotographer() != 0) {
if (!DatabaseORM.insertCategory(em, new Category("Groepsfoto", account, 0.5d)) ||
!DatabaseORM.insertCategory(em, new Category("Portret", account, 0.5d))) {
em.getTransaction().rollback();
return false;
}
}
em.getTransaction().commit();
Email.sendRegistrationMail(account.getEmail(), account.getUsername(), psw);
return true;
} catch (Exception eex) {
return false;
}
}
/**
* Update an existing account in the database.
* @param em the entitymanager to use.
* @param account the account that has to be updated.
* @return true if the account has been successfully updated, otherwise false.
*/
public static boolean updateAccount(EntityManager em, Account account) {
try {
em.getTransaction().begin();
em.merge(account);
em.getTransaction().commit();
return true;
} catch (EntityExistsException eex) {
return false;
}
}
/**
* Get the account corresponding to the username.
* @param em the entitymanager to use.
* @param username the username of the account to find.
* @return an Account-Object if the account exists, otherwise null.
*/
public static Account findAccount(EntityManager em, String username) {
TypedQuery<Account> query = em.createNamedQuery("Account.findByUsername", Account.class);
query.setParameter("username", username);
try {
return query.getSingleResult();
} catch (NoResultException ex) {
return null;
}
}
/**
* Check if the Account with the given username is an A-Photographer.
* @param em the entitymanager to use.
* @param username the username of the account to check.
* @return true if the account is a photographer, otherwise false.
*/
public static boolean accountIsPhotographer(EntityManager em, String username) {
Account account = DatabaseORM.findAccount(em, username);
if (account != null) {
return account.getPhotographer() == 1;
}
return false;
}
/**
* Get the registrationdate of an account.
* @param em the entitymanager to use.
* @param username the username of the account.
* @return a Timestamp-Object containing the registrationdate of the account, otherwise null.
*/
public static Timestamp getAccountRegistrationDate(EntityManager em, String username) {
Account account = DatabaseORM.findAccount(em, username);
if (account != null) {
return account.getRegisterDate();
}
return null;
}
/**
* Create a new category in the database.
* @param em the entitymanager to use.
* @param category the category that has to be added to the database.
* @return true if the category has successfully been added to the database, otherwise false.
*/
private static boolean insertCategory(EntityManager em, Category category) {
try {
em.getTransaction().begin();
em.persist(category);
em.getTransaction().commit();
return true;
} catch (EntityExistsException eex) {
return false;
}
}
/**
* Update an existing category in the database.
* @param em the entitymanager to use.
* @param account the category that has to be updated.
* @return true if the category has been successfully updated, otherwise false.
*/
public static boolean updateCategory(EntityManager em, Category category) {
try {
em.getTransaction().begin();
em.merge(category);
em.getTransaction().commit();
return true;
} catch (EntityExistsException eex) {
return false;
}
}
/**
* Get the categories of a certain account.
* @param em the entitymanager to use.
* @param username the username of the account.
* @return a List-Object containing all categories of the account, otherwise null.
*/
public static List<Category> getCategories(EntityManager em, String username) {
Query q = em.createNamedQuery("Category.findByUsername", Category.class);
q.setParameter("username", username);
try {
return q.getResultList();
} catch (NoResultException ex) {
return null;
}
}
/**
* Insert a new order into the database.
* @param em the entitymanager to use.
* @param order the order to put in the database.
* @return true if the order has successfully been added to the database, otherwise false.
*/
public static void insertOrder(EntityManager em, Order order) {
em.getTransaction().begin();
em.persist(order);
em.getTransaction().commit();
}
/**
* Find the order corresponding with the given order number.
* @param em the entitymanager to use.
* @param ordernr the number of the order.
* @return an Order-Object if the order exists, otherwise null.
*/
public static Order findOrder(EntityManager em, int ordernr) {
Query query = em.createNamedQuery("Order.findById");
query.setParameter("orderNo", ordernr);
try {
return (Order)query.getSingleResult();
} catch (NoResultException ex) {
return null;
}
}
/**
* Check if the order with the given order number is cancellable.
* @param em the entitymanager to use.
* @param ordernr the ordernr of the order.
* @return true if the order can be cancelled, otherwise false.
*/
public static boolean isOrderCancellable(EntityManager em, int ordernr) {
return isOrderCancellable(findOrder(em, ordernr));
}
/**
* Check if the given order is cancellable.
* @param order the order.
* @return true if the order can be cancelled, otherwise false.
*/
public static boolean isOrderCancellable(Order order) {
if (order != null) {
Timestamp payDate = order.getPaymentDate();
Date now = new Date();
long timepassed = now.getTime() / 1000 - payDate.getTime() / 1000;
// Check if 3 hours have passed after the payment.
return timepassed <= 60 * 60 * 3;
}
return false;
}
/**
* Cancel the order with the given order number.
* @param em the entitymanager to use.
* @param ordernr the order number of the order which has to be cancelled.
* @return true if cancelling the order has succeeded, otherwise false.
*/
public static boolean cancelOrder(EntityManager em, int ordernr) {
Order order = findOrder(em, ordernr);
if (order != null && isOrderCancellable(order)) {
em.getTransaction().begin();
em.remove(order);
em.getTransaction().commit();
return true;
}
return false;
}
/**
* Get the items which are in the order.
* @param em the entitymanager to use.
* @param ordernr the number of the order.
* @return a list containing all the items in the order, if the order doesn't exist it will return null.
*/
public static List<OrderItem> getOrderItems(EntityManager em, int ordernr) {
Order order = findOrder(em, ordernr);
if (order != null) {
return order.getItems();
}
return null;
}
/**
* Get all orders in the database.
* @param em the entitymanager to use.
* @return a List containing all Orders.
*/
public static List<Order> getAllOrders(EntityManager em) {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Order.class));
return em.createQuery(cq).getResultList();
}
/**
* Get all orders which the user with the given username has made.
* @param em the entitymanager to use.
* @param username the username of the user.
* @return a list containing all orders of the user, if the user doesn't exist it will return null.
*/
public static List<Order> getOrders(EntityManager em, String username) {
Query query = em.createNamedQuery("Order.findByUser");
query.setParameter("username", username);
try {
return query.getResultList();
} catch (NoResultException ex) {
return null;
}
}
/**
* Get all order which are (not) handled.
* @param em the entitymanager to use.
* @param handled true if you want the handled orders, otherwise false.
* @return a list containing all orders which are (not) handled.
*/
public static List<Order> getOrders(EntityManager em, boolean handled) {
Query query = em.createNamedQuery("Order.findHandled");
query.setParameter("handled", handled ? 1 : 0);
return query.getResultList();
}
/**
* Put the shoppingcart into the database.
* @param em the entitymanager to use.
* @param cart the shoppingcart to put in the database.
*/
public static void insertShoppingcart(EntityManager em, Shoppingcart cart) {
em.getTransaction().begin();
em.persist(cart);
em.getTransaction().commit();
}
/**
* Update the shoppingcart in the database.
* @param em the entitymanager to use.
* @param cart the shoppingcart to update.
*/
public static void updateShoppingcart(EntityManager em, Shoppingcart cart) {
em.getTransaction().begin();
em.merge(cart);
em.getTransaction().commit();
}
/**
* Get the shoppingcart from the database which belongs to the user.
* @param em the entitymanager to use.
* @param username the username of the user.
* @return the shoppingcart of the user, if it doesn't exist, returns null.
*/
public static Shoppingcart getShoppingcart(EntityManager em, String username) {
Query query = em.createNamedQuery("Shoppingcart.findByUsername");
query.setParameter("username", username);
try {
return (Shoppingcart)query.getSingleResult();
} catch (Exception ex) {
return null;
}
}
/**
* Remove the shoppingcart of the given user from the database.
* @param em the entitymanager to use.
* @param username the username of the user of which the shoppingcart has to be removed.
* @return true if the shoppingcart has been succesfully removed, otherwise false.
*/
public static boolean removeShoppingcart(EntityManager em, String username) {
return removeShoppingcart(em, getShoppingcart(em, username));
}
/**
* Remove the shoppingcart from the database.
* @param em the entitymanager to use.
* @param shoppingcart the shoppingcart to remove from the database.
* @return true if the shoppingcart has been succesfully removed, otherwise false.
*/
public static boolean removeShoppingcart(EntityManager em, Shoppingcart shoppingcart) {
if (shoppingcart != null) {
em.getTransaction().begin();
em.remove(shoppingcart);
em.getTransaction().commit();
return true;
}
return false;
}
}