Skip to content
This repository has been archived by the owner on Mar 13, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' into userProfile
Browse files Browse the repository at this point in the history
  • Loading branch information
BikJeun authored Mar 21, 2021
2 parents f14327d + afbe621 commit 4b1235f
Show file tree
Hide file tree
Showing 15 changed files with 1,078 additions and 638 deletions.
331 changes: 315 additions & 16 deletions OTFood-ejb/nbproject/project.properties

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions OTFood-ejb/src/conf/persistence.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="OTFood-ejbPU" transaction-type="JTA">
<jta-data-source>jdbc/OTFood</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
<persistence-unit name="OTFood-ejbPU" transaction-type="JTA">
<jta-data-source>jdbc/OTFood</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import ejb.session.stateless.IngredientEntitySessionBeanLocal;
import ejb.session.stateless.MealEntitySessionBeanLocal;
import ejb.session.stateless.OTUserEntitySessionBeanLocal;
import ejb.session.stateless.PromoSessionBeanLocal;
import ejb.session.stateless.ReviewEntitySessionBeanLocal;
import ejb.session.stateless.SaleTransactionEntitySessionBeanLocal;
import entity.AddressEntity;
Expand All @@ -22,6 +23,7 @@
import entity.IngredientEntity;
import entity.MealEntity;
import entity.OTUserEntity;
import entity.PromoCodeEntity;
import entity.ReviewEntity;
import entity.SaleTransactionEntity;
import entity.SaleTransactionLineEntity;
Expand All @@ -40,6 +42,7 @@
import javax.persistence.PersistenceContext;
import util.enumeration.CategoryEnum;
import util.enumeration.IngredientTypeEnum;
import util.enumeration.PromoCodeTypeEnum;
import util.enumeration.RegionEnum;
import util.exception.AddressExistException;
import util.exception.CardCreationException;
Expand All @@ -49,6 +52,7 @@
import util.exception.FaqExistException;
import util.exception.IngredientEntityExistsException;
import util.exception.InputDataValidationException;
import util.exception.PromoCodeExistException;
import util.exception.ReviewExistException;
import util.exception.UnknownPersistenceException;
import util.exception.UserExistException;
Expand All @@ -63,6 +67,9 @@
@LocalBean
public class DataInitializationSessionBean {

@EJB
private PromoSessionBeanLocal promoSessionBeanLocal;

@EJB
private CreditCardEntitySessionBeanLocal creditCardEntitySessionBeanLocal;

Expand Down Expand Up @@ -181,6 +188,9 @@ private void dataInitialise() {
for (IngredientEntity ingredients : allIngredients) {
ingredientEntitySessionBeanLocal.createIngredientEntityForMeal(ingredients);
}

// Create Promo Code
promoSessionBeanLocal.createNewPromoCode(new PromoCodeEntity(new Date(), new Date(500, 10, 10), 100,"AAAAAA" ,new BigDecimal(10.0), PromoCodeTypeEnum.PERCENTAGE));

// Create SaleTransactions
List<SaleTransactionLineEntity> saleTransactionLines = new ArrayList<>();
Expand Down Expand Up @@ -217,7 +227,7 @@ private void dataInitialise() {
faqSessionBean.createNewFaq(new FaqEntity("Are there different sizes to the bento?", "Sorry but at the moment, we only offer a fixed protion size.", "Product"));
faqSessionBean.createNewFaq(new FaqEntity("Can I visit your shop?", "Currently we are a home grown business, hence we do not have an outlet store. We do appreciate your continuous support to allow us to achieve the dream of opening our own store", "Product"));

} catch (UserExistException | UnknownPersistenceException | InputDataValidationException | ReviewExistException | UserNotFoundException | FaqExistException | CreateNewSaleTransactionException | DriverExistsException | IngredientEntityExistsException | AddressExistException | CreditCardExistException | CardCreationException ex) {
} catch (UserExistException | UnknownPersistenceException | InputDataValidationException | ReviewExistException | UserNotFoundException | FaqExistException | CreateNewSaleTransactionException | DriverExistsException | IngredientEntityExistsException | AddressExistException | CreditCardExistException | CardCreationException | PromoCodeExistException ex) {
Logger.getLogger(DataInitializationSessionBean.class.getName()).log(Level.SEVERE, null, ex);
}

Expand Down
18 changes: 14 additions & 4 deletions OTFood-ejb/src/java/ejb/session/stateless/PromoSessionBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,24 @@ public List<PromoCodeEntity> retreieveAllPromoCode() {
Query query = em.createQuery("SELECT p FROM PromoCodeEntity p");
return query.getResultList();
}

@Override
public PromoCodeEntity retrieveCodeById(Long promoCodeId) throws PromotionNotFoundException {
PromoCodeEntity promoCode = em.find(PromoCodeEntity.class, promoCodeId);
if (promoCode != null) {
return promoCode;
} else {
throw new PromotionNotFoundException("Promotion Code ID " + promoCodeId + " does not exist!");
}
}

@Override
public PromoCodeEntity retrieveCodeByDiscountCode(String code) throws PromotionNotFoundException {
Query query = em.createQuery("SELECT q FROM PromoCodeEntity q WHERE q.discountCode = :code");
query.setParameter("code", code);

try {
return (PromoCodeEntity) query.getResultList();
return (PromoCodeEntity) query.getSingleResult();
} catch (NoResultException | NonUniqueResultException ex) {
throw new PromotionNotFoundException("Promotion Code " + code + " does not exist!");
}
Expand All @@ -104,14 +114,14 @@ public Boolean checkPromoCode(String code) {
Date date = new Date();

if (promo.getStartDate().compareTo(date) < 0 && promo.getEndDate().compareTo(date) > 0) {
valid = true;
if (promo.getSaleTransaction().size() < promo.getNumAvailable()) {
valid = true;
}
}
} catch (PromotionNotFoundException ex) {
Logger.getLogger(PromoSessionBean.class.getName()).log(Level.SEVERE, null, ex);
}

return valid;

}

private String prepareInputDataValidationErrorsMessage(Set<ConstraintViolation<PromoCodeEntity>> constraintViolations) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public interface PromoSessionBeanLocal {

public Boolean checkPromoCode(String code);

public PromoCodeEntity retrieveCodeById(Long promoCodeId) throws PromotionNotFoundException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
import entity.IngredientEntity;
import entity.MealEntity;
import entity.OTUserEntity;
import entity.PromoCodeEntity;
import entity.SaleTransactionEntity;
import entity.SaleTransactionLineEntity;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
Expand All @@ -32,6 +31,7 @@
import util.exception.InputDataValidationException;
import util.exception.NoAddressFoundException;
import util.exception.NoSaleTransactionFoundException;
import util.exception.PromotionNotFoundException;
import util.exception.UpdateSaleTransactionException;
import util.exception.UserNotFoundException;

Expand All @@ -42,6 +42,9 @@
@Stateless
public class SaleTransactionEntitySessionBean implements SaleTransactionEntitySessionBeanLocal {

@EJB
private PromoSessionBeanLocal promoSessionBeanLocal;

@EJB
private AddressEntitySessionBeanLocal addressEntitySessionBeanLocal;

Expand All @@ -50,7 +53,7 @@ public class SaleTransactionEntitySessionBean implements SaleTransactionEntitySe

@EJB
private OTUserEntitySessionBeanLocal oTUserEntitySessionBeanLocal;

@EJB
private IngredientEntitySessionBeanLocal ingredientEntitySessionBeanLocal;

Expand Down Expand Up @@ -105,6 +108,54 @@ public Long createNewSaleTransaction(Long userId, Long ccId, Long adressId, Sale
}
}

@Override
public Long createNewSaleTransactionWithPromo(Long userId, Long ccId, Long adressId, Long promoId, SaleTransactionEntity saleTransaction) throws CreateNewSaleTransactionException, InputDataValidationException {
Set<ConstraintViolation<SaleTransactionEntity>> constraintViolations = validator.validate(saleTransaction);
if (constraintViolations.isEmpty()) {
if (saleTransaction != null) {
try {
OTUserEntity user = oTUserEntitySessionBeanLocal.retrieveUserById(userId);
CreditCardEntity cc = creditCardEntitySessionBeanLocal.retrieveCardById(ccId);
AddressEntity addresss = addressEntitySessionBeanLocal.retrieveAddressById(adressId);
PromoCodeEntity promoCode = promoSessionBeanLocal.retrieveCodeById(promoId);

saleTransaction.setUser(user);
user.getSaleTransaction().add(saleTransaction);

promoCode.getSaleTransaction().add(saleTransaction);
saleTransaction.setPromoCode(promoCode);

saleTransaction.setCreditCardEntity(cc);

saleTransaction.setAddress(addresss);

em.persist(saleTransaction);
for (int i = 0; i < saleTransaction.getTotalLineItem(); i++) {
SaleTransactionLineEntity lineItem = saleTransaction.getSaleTransactionLineItemEntities().get(i);
em.persist(lineItem);
for (int j = 0; j < lineItem.getQuantity(); j++) {
MealEntity meal = lineItem.getMeal();
List<IngredientEntity> ingredients = meal.getIngredients();
for (IngredientEntity ingredient : ingredients) {
//ingredientEntitySessionBeanLocal.deductStockQuantity(ingredient.getIngredientId(), 1);
}
}
}
em.flush();
return saleTransaction.getSaleTransactionId();
} catch (UserNotFoundException ex) {
throw new CreateNewSaleTransactionException("Error: User not found!");
} catch (CreditCardNotFoundException | NoAddressFoundException | PromotionNotFoundException ex) {
throw new CreateNewSaleTransactionException("Error: Address or Credit Card or Promo Code not found!"); //should never get this
}
} else {
throw new CreateNewSaleTransactionException("Error: saleTransaction provided is null!");
}
} else {
throw new InputDataValidationException(prepareInputDataValidationErrorsMessage(constraintViolations));
}
}

private String prepareInputDataValidationErrorsMessage(Set<ConstraintViolation<SaleTransactionEntity>> constraintViolations) {
String msg = "Input data validation error!:";

Expand All @@ -125,15 +176,11 @@ public List<SaleTransactionEntity> retrieveAllSaleTransaction() throws NoSaleTra
}

@Override
public List<SaleTransactionEntity> retrieveSaleTransactionsByUserId(Long userId) throws NoSaleTransactionFoundException {
try {
public List<SaleTransactionEntity> retrieveSaleTransactionsByUserId(Long userId) {
Query query = em.createQuery("SELECT st FROM OTUserEntity user JOIN user.saleTransaction st WHERE user.UserId = :userId");
query.setParameter("userId", userId);
List<SaleTransactionEntity> saleTransactions = query.getResultList();
return saleTransactions;
} catch (NoResultException | NonUniqueResultException ex) {
throw new NoSaleTransactionFoundException("userId: " + userId + " has no sale transactions!");
}
}

@Override
Expand Down Expand Up @@ -167,7 +214,11 @@ public void updateSaleTransaction(Long userId, SaleTransactionEntity saleTransac
}
}

//delete?
//delete? will add later on -JH

public void persist(Object object) {
em.persist(object);
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ public interface SaleTransactionEntitySessionBeanLocal {

public List<SaleTransactionEntity> retrieveAllSaleTransaction() throws NoSaleTransactionFoundException;

public List<SaleTransactionEntity> retrieveSaleTransactionsByUserId(Long userId) throws NoSaleTransactionFoundException;
public List<SaleTransactionEntity> retrieveSaleTransactionsByUserId(Long userId);

public SaleTransactionEntity retrieveSaleTransactionByUserId(Long userId, Long transactionId) throws NoSaleTransactionFoundException;

public void updateSaleTransaction(Long userId, SaleTransactionEntity saleTransaction) throws UpdateSaleTransactionException;

public Long createNewSaleTransactionWithPromo(Long userId, Long ccId, Long adressId, Long promoId, SaleTransactionEntity saleTransaction) throws CreateNewSaleTransactionException, InputDataValidationException;

}
1 change: 0 additions & 1 deletion OTFood-ejb/src/java/entity/SaleTransactionEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public SaleTransactionEntity() {
}

public SaleTransactionEntity(Integer totalLineItem, Integer totalQuantity, BigDecimal totalAmount, Date transactionDateTime, Date deliveryDateTime) {

this();
this.totalLineItem = totalLineItem;
this.totalQuantity = totalQuantity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@
*/
package jsf.managedbean;

import ejb.session.stateless.SaleTransactionEntitySessionBeanLocal;
import entity.OTUserEntity;
import entity.SaleTransactionEntity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.faces.view.ViewScoped;

Expand All @@ -18,10 +25,21 @@
@ViewScoped
public class ViewTransactionDetailManagedBean implements Serializable {

@EJB
private SaleTransactionEntitySessionBeanLocal saleTransactionEntitySessionBeanLocal;

private List<SaleTransactionEntity> allSaleTransactions;
private SaleTransactionEntity selectedSaleTransaction;

public ViewTransactionDetailManagedBean() {
this.selectedSaleTransaction = new SaleTransactionEntity();
this.allSaleTransactions = new ArrayList<>();
}

@PostConstruct
public void postConstruct() {
OTUserEntity user = (OTUserEntity) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("currentUser");
this.allSaleTransactions = saleTransactionEntitySessionBeanLocal.retrieveSaleTransactionsByUserId(user.getUserId());
}

/**
Expand All @@ -37,5 +55,19 @@ public SaleTransactionEntity getSelectedSaleTransaction() {
public void setSelectedSaleTransaction(SaleTransactionEntity selectedSaleTransaction) {
this.selectedSaleTransaction = selectedSaleTransaction;
}

/**
* @return the allSaleTransactions
*/
public List<SaleTransactionEntity> getAllSaleTransactions() {
return allSaleTransactions;
}

/**
* @param allSaleTransactions the allSaleTransactions to set
*/
public void setAllSaleTransactions(List<SaleTransactionEntity> allSaleTransactions) {
this.allSaleTransactions = allSaleTransactions;
}

}
Loading

0 comments on commit 4b1235f

Please sign in to comment.