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

Commit

Permalink
Add user Profile Page
Browse files Browse the repository at this point in the history
  • Loading branch information
Bennyphoe committed Mar 20, 2021
1 parent b8a91d5 commit 4a1ae68
Show file tree
Hide file tree
Showing 7 changed files with 606 additions and 3 deletions.
2 changes: 1 addition & 1 deletion OTFood-ejb/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ j2ee.platform=1.8
<<<<<<< HEAD
=======
>>>>>>> 56c20105daa1681579e236e3c01d9c5da800e244
j2ee.platform.classpath=${j2ee.server.home}/modules/bean-validator.jar:${j2ee.server.home}/modules/cdi-api.jar:${j2ee.server.home}/modules/endorsed/grizzly-npn-bootstrap.jar:${j2ee.server.home}/modules/endorsed/jakarta.annotation-api.jar:${j2ee.server.home}/modules/endorsed/jakarta.xml.bind-api.jar:${j2ee.server.home}/modules/endorsed/webservices-api-osgi.jar:${j2ee.server.home}/modules/javax.batch-api.jar:${j2ee.server.home}/modules/jaxb-osgi.jar:${j2ee.server.home}/modules/webservices-osgi.jar:${j2ee.server.home}/modules/weld-osgi-bundle.jar:${j2ee.server.middleware}/mq/lib/jaxm-api.jar
j2ee.platform.classpath=
j2ee.platform.classpath=${j2ee.server.home}/modules/bean-validator.jar:${j2ee.server.home}/modules/cdi-api.jar:${j2ee.server.home}/modules/endorsed/grizzly-npn-bootstrap.jar:${j2ee.server.home}/modules/endorsed/jakarta.annotation-api.jar:${j2ee.server.home}/modules/endorsed/jakarta.xml.bind-api.jar:${j2ee.server.home}/modules/endorsed/webservices-api-osgi.jar:${j2ee.server.home}/modules/javax.batch-api.jar:${j2ee.server.home}/modules/jaxb-osgi.jar:${j2ee.server.home}/modules/webservices-osgi.jar:${j2ee.server.home}/modules/weld-osgi-bundle.jar:${j2ee.server.middleware}/mq/lib/jaxm-api.jar
j2ee.platform.embeddableejb.classpath=${j2ee.server.home}/lib/embedded/glassfish-embedded-static-shell.jar
j2ee.platform.wscompile.classpath=${j2ee.server.home}/modules/webservices-osgi.jar
j2ee.platform.wsgen.classpath=${j2ee.server.home}/modules/webservices-osgi.jar:${j2ee.server.home}/modules/endorsed/webservices-api-osgi.jar:${j2ee.server.home}/modules/jaxb-osgi.jar
Expand Down
298 changes: 298 additions & 0 deletions OTFood-war/src/java/jsf/managedbean/ProfileManagedBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,298 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package jsf.managedbean;

import ejb.session.stateless.OTUserEntitySessionBeanLocal;
import entity.AddressEntity;
import entity.CreditCardEntity;
import entity.IngredientEntity;
import entity.OTUserEntity;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.view.ViewScoped;
import org.primefaces.event.FileUploadEvent;
import util.enumeration.RegionEnum;
import util.exception.InputDataValidationException;
import util.exception.UpdateUserException;
import util.exception.UserNotFoundException;

/**
*
* @author benny
*/
@Named(value = "profileManagedBean")
@ViewScoped
public class ProfileManagedBean implements Serializable {

@EJB(name = "OTUserEntitySessionBeanLocal")
private OTUserEntitySessionBeanLocal oTUserEntitySessionBeanLocal;


/**
* Creates a new instance of ProfileManagedBean
*/
private OTUserEntity profile;
private String profileDate;
private List<AddressEntity> addresses;
private List<CreditCardEntity> creditCards;
private AddressEntity newAddress;
private CreditCardEntity newCreditCard;
private List<RegionEnum> regions;
private RegionEnum region;


public ProfileManagedBean() {
addresses = new ArrayList<>();
creditCards = new ArrayList<>();
regions = new ArrayList<>();
newAddress = new AddressEntity();
newCreditCard = new CreditCardEntity();
}

@PostConstruct
public void postConstruct() {
profile = (OTUserEntity)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("currentUser");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateInString = formatter.format(profile.getDob());
profileDate = dateInString;
try {
Date newDate = formatter.parse(dateInString);
profile.setDob(newDate);
} catch (ParseException ex) {
Logger.getLogger(ProfileManagedBean.class.getName()).log(Level.SEVERE, null, ex);
}
addresses = profile.getAddress();
creditCards = profile.getCreditCard();
regions = Arrays.asList(RegionEnum.values());

}


public void handleFileUpload(FileUploadEvent event) throws InputDataValidationException, UpdateUserException, UserNotFoundException
{

try
{
String newFilePath = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("alternatedocroot_1") + System.getProperty("file.separator") + event.getFile().getFileName();

System.err.println("********** Demo03ManagedBean.handleFileUpload(): File name: " + event.getFile().getFileName());
System.err.println("********** Demo03ManagedBean.handleFileUpload(): newFilePath: " + newFilePath);

File file = new File(newFilePath);
FileOutputStream fileOutputStream = new FileOutputStream(file);

int a;
int BUFFER_SIZE = 8192;
byte[] buffer = new byte[BUFFER_SIZE];

InputStream inputStream = event.getFile().getInputStream();

while (true)
{
a = inputStream.read(buffer);

if (a < 0)
{
break;
}

fileOutputStream.write(buffer, 0, a);
fileOutputStream.flush();
}

fileOutputStream.close();
inputStream.close();
String newFile = event.getFile().getFileName();
profile.setProfilePic(event.getFile().getFileName().substring(0, newFile.length()-4));
oTUserEntitySessionBeanLocal.updateUserDetails(profile);
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "File uploaded successfully", ""));
}
catch(IOException ex)
{
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "File upload error: " + ex.getMessage(), ""));
}
}

public void updateDetails(ActionEvent event) {
try {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date updateDate = formatter.parse(profileDate);
profile.setDob(updateDate);
oTUserEntitySessionBeanLocal.updateUserDetails(profile);
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Profile updated successfully", ""));
} catch (UpdateUserException | UserNotFoundException | InputDataValidationException ex) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Profile Update error: " + ex.getMessage(), ""));
} catch (ParseException ex) {
Logger.getLogger(ProfileManagedBean.class.getName()).log(Level.SEVERE, null, ex);
}

}

public void removeAddress(ActionEvent event) {
try {
Long addressId = (Long)event.getComponent().getAttributes().get("addressId");
for (int i = 0; i < addresses.size(); i++) {
if (addresses.get(i).getAddressId() == addressId) {
addresses.remove(i);
break;
}
}
profile.setAddress(addresses);
oTUserEntitySessionBeanLocal.updateUserDetails(profile);
} catch (UpdateUserException ex) {
Logger.getLogger(ProfileManagedBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (UserNotFoundException ex) {
Logger.getLogger(ProfileManagedBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (InputDataValidationException ex) {
Logger.getLogger(ProfileManagedBean.class.getName()).log(Level.SEVERE, null, ex);
}

}

public void removeCreditCard(ActionEvent event) {
try {
Long creditCardId = (Long)event.getComponent().getAttributes().get("cardId");
for (int i = 0; i < creditCards.size(); i++) {
if (creditCards.get(i).getCreditCardId() == creditCardId) {
creditCards.remove(i);
break;
}
}
profile.setCreditCard(creditCards);
oTUserEntitySessionBeanLocal.updateUserDetails(profile);
} catch (UpdateUserException ex) {
Logger.getLogger(ProfileManagedBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (UserNotFoundException ex) {
Logger.getLogger(ProfileManagedBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (InputDataValidationException ex) {
Logger.getLogger(ProfileManagedBean.class.getName()).log(Level.SEVERE, null, ex);
}

}

public void addAddress(ActionEvent event) {
try {
addresses.add(newAddress);
profile.setAddress(addresses);
oTUserEntitySessionBeanLocal.updateUserDetails(profile);
region = null;
newAddress = new AddressEntity();
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Address Added successfully", ""));

} catch (UpdateUserException | UserNotFoundException | InputDataValidationException ex) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Add Address error: " + ex.getMessage(), ""));
}
}

public void addCreditCard(ActionEvent event) {
try {
creditCards.add(newCreditCard);
profile.setCreditCard(creditCards);
oTUserEntitySessionBeanLocal.updateUserDetails(profile);
newCreditCard = new CreditCardEntity();
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Credit Card Added successfully", ""));

} catch (UpdateUserException | UserNotFoundException | InputDataValidationException ex) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Add Card error: " + ex.getMessage(), ""));
}
}

public void setRegion(AjaxBehaviorEvent event) {
System.out.print(region);
newAddress.setRegion(region);
System.out.print(newAddress.getRegion());
}

public OTUserEntity getProfile() {
return profile;
}

public void setProfile(OTUserEntity profile) {
this.profile = profile;
}

public String getProfileDate() {
return profileDate;
}

public void setProfileDate(String profileDate) {
this.profileDate = profileDate;
}

public List<AddressEntity> getAddresses() {
return addresses;
}

public void setAddresses(List<AddressEntity> addresses) {
this.addresses = addresses;
}

public List<CreditCardEntity> getCreditCards() {
return creditCards;
}

public void setCreditCards(List<CreditCardEntity> creditCards) {
this.creditCards = creditCards;
}

public AddressEntity getNewAddress() {
return newAddress;
}

public void setNewAddress(AddressEntity newAddress) {
this.newAddress = newAddress;
}

public CreditCardEntity getNewCreditCard() {
return newCreditCard;
}

public void setNewCreditCard(CreditCardEntity newCreditCard) {
this.newCreditCard = newCreditCard;
}

public List<RegionEnum> getRegions() {
return regions;
}

public void setRegions(List<RegionEnum> regions) {
this.regions = regions;
}

public RegionEnum getRegion() {
return region;
}

public void setRegion(RegionEnum region) {
this.region = region;
}






}
16 changes: 16 additions & 0 deletions OTFood-war/web/WEB-INF/glassfish-web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app error-url="">
<context-root>/OTFood-war</context-root>
<resource-ref>
<res-ref-name>OTFoodDataSource</res-ref-name>
<jndi-name>jdbc/OTFood</jndi-name>
</resource-ref>
<class-loader delegate="true"/>
<jsp-config>
<property name="keepgenerated" value="true">
<description>Keep a copy of the generated servlet class' java code.</description>
</property>
</jsp-config>
<property name="alternatedocroot_1" value="from=/uploadedFiles/* dir=C:/glassfish-5.1.0-uploadedfiles"/>
</glassfish-web-app>
4 changes: 4 additions & 0 deletions OTFood-war/web/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>alternatedocroot_1</param-name>
<param-value>C:/glassfish-5.1.0-uploadedfiles/uploadedFiles</param-value>
</context-param>
<listener>
<description>ServletContextListener, HttpSessionListener</description>
<listener-class>web.listener.OOTDFoodListener</listener-class>
Expand Down
54 changes: 54 additions & 0 deletions OTFood-war/web/resources/css/profile.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@


.ui-widget-header {
background: transparent
}

.ui-panelGrid {
border: transparent
}

.ui-panelgrid {
border: none !important
}

td {
display: table-cell;
vertical-align: inherit;
font-size: 30px
}

.column1 {
width: 10%;
background: black
}

.column2 {
width: 90%;
background: black
}

.ui-widget-content {
border: transparent
}

.address-list-item {
display: flex;
flex-direction: row;
}

.address-list-detail {
flex: 1 1;
}

.address-list-action {
display: flex;
flex-direction: column;
}

.address {
font-size: 20px;
font: bolder
}


Loading

0 comments on commit 4a1ae68

Please sign in to comment.