Setup Persistence Layer
- Create Database in pgadmin
- Load data to database
psql -U postgres customerpaymentsystem < [path to sql file]
- Configure standalone file
<datasource jndi-name="java:/[jndi]" pool-name="[pool name]">
<connection-url>jdbc:postgresql://localhost:5432/[database name]</connection-url>
<driver-class>org.postgresql.Driver</driver-class>
<driver>postgresql</driver>
<security>
<user-name>postgres</user-name>
<password>jinyeeU</password>
</security>
</datasource>
- Configure database connection to Intellij
- new db connections
- data source
- add new postgresSQL driver (not Postgres Driver)
- enter username, password, database name, and change schema to default schema with public
- then test connection and add connection
- Create entity, repository and webservices package inside model package
- Create Entity java object from database
- Go to Persistence units
- New JPA entities from DB
- Select db connection with "current schema" keyword
- Select table and adjust datatype then add.
- Implement serializable in JPA entity (both id entity and non id entity)
- Generate constructor for entity ID and constructor for none (non id entity)
- Generate constructor for composite ID and constructor for none (id entity)
- Change local date to date
- Create Named Query
@NamedQuery(name="Payment.findAll", query = "Select p from Payment p")
@NamedQuery(name="Payment.findByFLName", query = "Select p from Payment p where p.customerFirstName = :customerFirstName and p.customerLastName = :customerLastName")
- Make sure bean discovery mode equal to all in beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
bean-discovery-mode="all">
</beans>
- Configure Persistence.xml
<persistence-unit name="default">
<jta-data-source>java:/payment</jta-data-source>
<class>model.entity.Payment</class>
<class>model.entity.PaymentId</class>
</persistence-unit>
- Create repository file PostGresDatabase and EntityManagerProducer
PostGresDatabase
package model.repository;
import javax.inject.Qualifier;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD,
ElementType.TYPE, ElementType.PARAMETER})
public @interface PostGresDatabase {}
// @interface mean create annotation
EntityManagerProducer
package model.repository;
import javax.ejb.EJBException;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
public class EntityManagerProducer {
@Produces
@PersistenceContext(unitName = "Default")
@PostGresDatabase
private EntityManager em;
public EntityManagerProducer() {
super();
// TODO Auto-generated constructor stub
}
}
- Create repository file (business logic file)
- Create Wrapper to wrap data from client and pass to repository file
Business Logic
- Add necessary dependency to pom.xml
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.6.2.Final</version>
<scope>provided</scope>
</dependency>
- Modify service file Example
@Path("payment")
@RequestScoped
public class PaymentService {
@Inject
private PaymentRepository paymentBean;
@GET
@Path("getPaymentList")
@Produces(MediaType.APPLICATION_JSON)
public Response getPaymentList(){
return Response.ok(paymentBean.getPaymentList()).build();
}
@GET
@Path("getPaymentFLName/{fname: [A-Z][a-zA-Z]*}-{lname: [A-Z][a-zA-Z]*}")
@Produces(MediaType.APPLICATION_JSON)
public Response getPaymentFLName(@PathParam("fname") String fname, @PathParam("lname") String lname ){
if (paymentBean.getPaymentByName(fname, lname) != null){
return Response.ok(paymentBean.getPaymentByName(fname, lname)).build();
}else{
return Response.status(Response.Status.OK).entity("Payment Record Not Found").type(MediaType.TEXT_PLAIN).build();
}
}
@POST
@Path("addPayment")
@Produces(MediaType.APPLICATION_JSON)
public Response addPayment(PaymentWrapper payment){
BigDecimal amountAfterDiscount = null;
BigDecimal amount1 = new BigDecimal(payment.getAmount());
if (payment.getCustomerType().equals("silver")){
amountAfterDiscount = amount1.subtract(amount1.multiply(new BigDecimal(0.05)));
}else{
amountAfterDiscount = amount1.subtract(amount1.multiply(new BigDecimal(0.10)));
}
payment.setAmountAfterDiscount(amountAfterDiscount.toString());
paymentBean.addPayment(payment);
List<Payment> list = paymentBean.getPaymentByName(payment.getCustomerFirstName(), payment.getCustomerLastName());
if (list != null) {
return Response.ok(list.get(0)).build();
}else{
return Response.status(Response.Status.OK).entity("Payment Record not added").type(MediaType.TEXT_PLAIN).build();
}
}
}
- Create APIApplication class
package model.webservices;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;
@ApplicationPath("/rest")
public class APIApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> set = new HashSet<>();
set.add(PaymentService.class );
return set;
}
}