2012年1月8日星期日

OEPE_008:Trouble Ticket System 项目实战开发之JPA:创建JPA DAO类

开发运行环境:OEPE 12c(12.1.1.1.0)+ WebLogic Server 12c(12.1.1.1.0)+ Oracle Database 10g Express Edition 10.2.0.1

JPA Entity创建好了,谁来使用这些对象呢?答案是Data Access Objects(DAO)。
DAO负责对Entity的增加、修改、删除、查询。

1. LoginJPADao.java
package com.oracle.ticketsystem.dao.impl;

import com.oracle.ticketsystem.beans.Technician;
import com.oracle.ticketsystem.dao.ILoginDao;

public class LoginJPADao extends BaseJPADao implements ILoginDao {

public LoginJPADao() {
// TODO Auto-generated constructor stub
}

@Override
public Technician login(String id, String pwd) {

Technician technician = getEntityManager().find(Technician.class, id);
if (technician == null) {
return null;
}

if (technician.getPassword().equals(pwd)) {
return technician;
}

return null;
}

}

2. ProductJPADao.java

/**
 * 
 */
package com.oracle.ticketsystem.dao.impl;

import java.util.List;

import javax.persistence.Query;

import com.oracle.ticketsystem.beans.Product;
import com.oracle.ticketsystem.dao.IProductDao;

/**
 * The JPA DAO implementation for Product entity.
 * 
 */
public class ProductJPADao extends BaseJPADao implements IProductDao {

/**
* Default no-arg constructor
*/
public ProductJPADao() {
super();
}

/*
* (non-Javadoc)
* @see com.oracle.ticketsystem.dao.IProductDAO#getAllProducts()
*/
@SuppressWarnings("unchecked")
@Override
public List getAllProducts() {
Query allProductsQuery = getEntityManager().createQuery(
"select p from Product p");
List allProducts = allProductsQuery.getResultList();
return allProducts;
}

/*
* (non-Javadoc)
* @see com.oracle.ticketsystem.dao.IProductDAO#getProduct(int)
*/
@Override
public Product getProduct(long id) {
return getEntityManager().find(Product.class, id);
}

}

3. TicketJPADao.java
package com.oracle.ticketsystem.dao.impl;

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import com.oracle.ticketsystem.beans.Product;
import com.oracle.ticketsystem.beans.Technician;
import com.oracle.ticketsystem.beans.Ticket;
import com.oracle.ticketsystem.beans.TicketHistory;
import com.oracle.ticketsystem.dao.ITicketDao;

public class TicketJPADao extends BaseJPADao implements ITicketDao {

/**
* Date pattern used across the application.
*/
public static final String DATE_PATTERN = "MM/dd/yyyy hh:mm:ss aa"; //$NON-NLS-1$

/**
* Default no-arg constructor
*/
public TicketJPADao() {
}

@Override
public Ticket add(long productId, String customerName,
String customerEmail, String title, String description) {

Product product = getEntityManager().find(Product.class, productId);

if (product == null) {
throw new RuntimeException("While adding a new ticket, "
+ "could not find reference to the given product Id: "
+ productId);
}

Ticket ticket = new Ticket();
ticket.setProduct(product);
ticket.setCustomerName(customerName);
ticket.setCustomerEmail(customerEmail);
ticket.setTitle(title);
ticket.setDescription(description);
ticket.setState("NEW"); // always NEW state
SimpleDateFormat dtFormat = new SimpleDateFormat(DATE_PATTERN);
ticket.setSubmissionDate(dtFormat.format(new Date()));

Long maxId = getMaxId("SELECT max(t.id) FROM Ticket t");

// setting the ticket Id
ticket.setId((maxId == null) ? 0 : maxId + 1);

EntityTransaction t = getEntityManager().getTransaction();
t.begin();
getEntityManager().persist(ticket);
t.commit();

return ticket;
}

@Override
public Ticket get(long ticketId) {
return getEntityManager().find(Ticket.class, ticketId);
}

@SuppressWarnings("unchecked")
@Override
public List getTicketsOwnedByTechnician(String technicianId) {

Query query = getEntityManager().createQuery(
"SELECT t from Ticket t WHERE t.technician.id = :technicianId");
query.setParameter("technicianId", technicianId);

return query.getResultList();
}

@Override
public Ticket update(long ticketId, String technicianId, String comment,
String state) {

EntityTransaction t = getEntityManager().getTransaction();
t.begin();

Ticket ticket = get(ticketId);

if (ticket == null) {
return null;
}

ticket.setState(state);
Technician technician = null;

if (technicianId != null) {

technician = getEntityManager()
.find(Technician.class, technicianId);

if (technician == null) {
throw new RuntimeException("No technician found for the ID '"
+ technicianId + "'");
}

}
ticket.setTechnician(technician);

TicketHistory ticketHistory = new TicketHistory();
Long maxTicketHistoryId = getMaxId("SELECT max(h.id) FROM TicketHistory h");

// setting the ticketHistory Id
ticketHistory.setId((maxTicketHistoryId == null) ? 0
: maxTicketHistoryId + 1);

if (technician != null) {
ticketHistory.setTechnician(technician);
}
ticketHistory.setState(state);
ticketHistory.setComments(comment);
ticketHistory.setTicket(ticket);

SimpleDateFormat dtFormat = new SimpleDateFormat(DATE_PATTERN);
ticketHistory.setUpdateDate(dtFormat.format(new Date()));

getEntityManager().persist(ticketHistory);

ticket.getTicketHistory().add(ticketHistory);

t.commit();
return ticket;
}

@SuppressWarnings("unchecked")
@Override
public List getOpenTickets() {

Query openTicketsQuery = getEntityManager().createQuery(
"SELECT t FROM Ticket t " + "WHERE t.state = :newState "
+ "OR t.state = :openState");
openTicketsQuery.setParameter("newState", "NEW");
openTicketsQuery.setParameter("openState", "OPEN");

return openTicketsQuery.getResultList();
}

@Override
public void remove(long ticketId) {

Ticket ticket = get(ticketId);
if (ticket != null) {
EntityTransaction trx = getEntityManager().getTransaction();
trx.begin();

for (TicketHistory ticketHistory : ticket.getTicketHistory()) {
getEntityManager().remove(ticketHistory);
}

ticket.getTicketHistory().clear();

getEntityManager().remove(ticket);

trx.commit();
}

}

private long getMaxId(String maxQuery) {

Query maxIdQuery = getEntityManager().createQuery(maxQuery);

Long maxId = 1L;

if ((maxIdQuery.getResultList() != null)
&& (maxIdQuery.getResultList().size() > 0)) {
maxId = (Long) maxIdQuery.getResultList().get(0);
}
return maxId;
}

}

4.  JPADaoFactory .java

/**
 * 
 */
package com.oracle.ticketsystem.dao.impl;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 * A JPA DAO factory for providing reference to EntityManager.
 * 
 */
public class JPADaoFactory {

private static final String PERSISTENCE_UNIT_NAME = "TroubleTicketSystemServer";

private static EntityManagerFactory entityManagerFactory;
private static EntityManager entityManager;

/**
* Returns reference to EntityManager instance. If null then create it using
* the persistence unit name as defined in the persistence.xml
* @return EntityManager
*/
public static EntityManager createEntityManager() {

if (entityManager == null) {
entityManagerFactory = Persistence
.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
entityManager = entityManagerFactory.createEntityManager();
}
return entityManager;
}

public static void close() {
entityManager.close();
entityManagerFactory.close();
}

}

5.   BaseJPADao .java
package com.oracle.ticketsystem.dao.impl;

import javax.persistence.EntityManager;

public class BaseJPADao {

/**
* Default no-arg constructor
*/
public BaseJPADao() {
}

/**
* Returns JPA EntityManager reference.
* @return
*/
public EntityManager getEntityManager() {
return JPADaoFactory.createEntityManager();
}

}

没有评论: