Created
February 3, 2016 17:57
-
-
Save Prologic200/7b9bf8e29238f0688e45 to your computer and use it in GitHub Desktop.
EJB.Recieve.Project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package coupons.web; | |
import javax.ejb.EJB; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpSession; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.PathParam; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.core.Context; | |
import javax.ws.rs.core.MediaType; | |
import coupons.core.AdminFacade; | |
import coupons.core.ClientType; | |
import coupons.core.CouponException; | |
import coupons.core.CouponSystem; | |
import coupons.ejb.Income; | |
import coupons.ejb.dao.IncomeDAO; | |
import coupons.web.beans.Message; | |
@Path("/admin") | |
@Produces(MediaType.APPLICATION_JSON) | |
public class AdminServiceResource { | |
private static final String ADMIN_SESSION_ATTR = "admin"; | |
@EJB | |
private IncomeDAO daoStub; | |
@GET | |
@Path("/login/{username}/{password}") | |
public Message login ( | |
@PathParam("username") String name, | |
@PathParam("password") String password, | |
@Context HttpServletRequest request) throws CouponException{ | |
AdminFacade adminFacade = (AdminFacade) CouponSystem.getInstance().login(name, password, ClientType.ADMIN); | |
request.getSession(true).setAttribute(ADMIN_SESSION_ATTR, adminFacade); | |
return new Message("Successfully logged in as administrator"); | |
} | |
@GET | |
@Path("/logout") | |
public Message logout ( | |
@Context HttpServletRequest request) throws CouponException{ | |
HttpSession session = request.getSession(false); | |
if (session == null || session.getAttribute(ADMIN_SESSION_ATTR) == null){ | |
throw new CouponException("You must be logged in first"); | |
} | |
if (session != null){ | |
session.invalidate(); | |
} | |
return new Message("Successfully logged out administrator"); | |
} | |
@GET | |
@Path("/viewAllIncome") | |
public Income [] veiwAllIncome (@Context HttpServletRequest request) | |
throws CouponException{ | |
return daoStub.viewAllIncome().toArray(new Income [0]); | |
} | |
@GET | |
@Path("/viewAllIncomeByCompany/{companyId}") | |
public Income [] veiwAllIncomeByCompany (@PathParam("companyId") long companyId, | |
@Context HttpServletRequest request) | |
throws CouponException{ | |
return daoStub.viewAllIncomeByCompany(companyId).toArray(new Income [0]); | |
} | |
@GET | |
@Path("/viewAllIncomeByCustomer/{customerId}") | |
public Income [] veiwAllIncomeByCustomer (@PathParam("customerId") long customerId, | |
@Context HttpServletRequest request) | |
throws CouponException{ | |
return daoStub.viewAllIncomeByCustomer(customerId).toArray(new Income [0]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package coupons.web; | |
import javax.ejb.EJB; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpSession; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.PathParam; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.core.Context; | |
import javax.ws.rs.core.MediaType; | |
import coupons.core.ClientType; | |
import coupons.core.CompanyFacade; | |
import coupons.core.CouponException; | |
import coupons.core.CouponSystem; | |
import coupons.ejb.Income; | |
import coupons.ejb.IncomeDeligate; | |
import coupons.ejb.incomeType; | |
import coupons.ejb.dao.IncomeDAO; | |
import coupons.web.beans.Message; | |
@Path("/company") | |
@Produces(MediaType.APPLICATION_JSON) | |
public class CompanyServiceResource { | |
private static final String COMPANY_SESSION_ATTR = "company"; | |
private static final double COMPANY_CREATE = 100; | |
private static final double COMPANY_UPDATE = 10; | |
@EJB | |
private IncomeDeligate deligateStub; | |
@EJB | |
private IncomeDAO daoStub; | |
@GET | |
@Path("/login/{username}/{password}") | |
public Message login(@PathParam("username") String name, @PathParam("password") String password, | |
@Context HttpServletRequest request) throws CouponException { | |
CompanyFacade companyFacade = (CompanyFacade) CouponSystem.getInstance().login(name, password, | |
ClientType.COMPANY); | |
request.getSession(true).setAttribute(COMPANY_SESSION_ATTR, companyFacade); | |
return new Message("Successfully logged in as company: " + name); | |
} | |
@GET | |
@Path("/coupon/create/{couponName}") | |
public Message createCoupon(@PathParam("couponName") String couponName, @Context HttpServletRequest request) | |
throws CouponException { | |
CompanyFacade companyFacade = (CompanyFacade) request.getSession(false).getAttribute(COMPANY_SESSION_ATTR); | |
Income income = new Income(companyFacade.getId(), incomeType.CompanyCreate, COMPANY_CREATE); | |
deligateStub.saveIncome(income); | |
return new Message("Company #" + companyFacade.getId() + " successfully created coupon: " + couponName); | |
} | |
@GET | |
@Path("/coupon/update/{couponName}") | |
public Message updateCoupon(@PathParam("couponName") String couponName, @Context HttpServletRequest request) | |
throws CouponException { | |
CompanyFacade companyFacade = (CompanyFacade) request.getSession(false).getAttribute(COMPANY_SESSION_ATTR); | |
deligateStub.saveIncome(new Income(companyFacade.getId(), incomeType.CompanyUpdate, COMPANY_UPDATE)); | |
return new Message("Company #" + companyFacade.getId() + " successfully updated coupon: " + couponName); | |
} | |
@GET | |
@Path("/logout") | |
public Message logout(@Context HttpServletRequest request) throws CouponException { | |
HttpSession session = request.getSession(false); | |
CompanyFacade companyFacade = (CompanyFacade) request.getSession(false).getAttribute(COMPANY_SESSION_ATTR); | |
long id = companyFacade.getId(); | |
if (session != null) { | |
session.invalidate(); | |
} | |
return new Message("Successfully logged out company #" + id); | |
} | |
@GET | |
@Path("/viewAllIncomeByCompany") | |
public Income [] veiwAllIncomeByCompany (@Context HttpServletRequest request) | |
throws CouponException{ | |
CompanyFacade companyFacade = (CompanyFacade) request.getSession(false).getAttribute(COMPANY_SESSION_ATTR); | |
long id = companyFacade.getId(); | |
return daoStub.viewAllIncomeByCompany(id).toArray(new Income [0]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package coupons.web; | |
import javax.ws.rs.ApplicationPath; | |
import javax.ws.rs.core.Application; | |
@ApplicationPath("/rest") | |
public class CouponsApplication extends Application { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package coupons.web; | |
import javax.ejb.EJB; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpSession; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.PathParam; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.core.Context; | |
import javax.ws.rs.core.MediaType; | |
import coupons.core.ClientType; | |
import coupons.core.CouponException; | |
import coupons.core.CouponSystem; | |
import coupons.core.CustomerFacade; | |
import coupons.ejb.Income; | |
import coupons.ejb.IncomeDeligate; | |
import coupons.ejb.incomeType; | |
import coupons.web.beans.Message; | |
@Path("/customer") | |
@Produces(MediaType.APPLICATION_JSON) | |
public class CustomerServiceResource { | |
private static final String CUSTOMER_SESSION_ATTR = "customer"; | |
@EJB | |
private IncomeDeligate deligateStub; | |
@GET | |
@Path("/login/{username}/{password}") | |
public Message login ( | |
@PathParam("username") String name, | |
@PathParam("password") String password, | |
@Context HttpServletRequest request) throws CouponException{ | |
CustomerFacade customerFacade = (CustomerFacade) CouponSystem.getInstance().login(name, password, ClientType.CUSTOMER); | |
request.getSession(true).setAttribute(CUSTOMER_SESSION_ATTR, customerFacade); | |
return new Message("Successfully logged in as customer: " + name); | |
} | |
@GET | |
@Path("/purchase/{couponName}/{couponPrice}") | |
public Message purchaseCoupon( | |
@PathParam("couponName") String couponName, | |
@PathParam("couponPrice") double couponPrice, | |
@Context HttpServletRequest request) throws CouponException{ | |
CustomerFacade customerFacade = (CustomerFacade) request.getSession(false).getAttribute(CUSTOMER_SESSION_ATTR); | |
deligateStub.saveIncome(new Income(customerFacade.getId(), incomeType.CustomerBuy, couponPrice)); | |
return new Message("Customer #" + customerFacade.getId() + " successfully purcahsed coupon: " + couponName); | |
} | |
@GET | |
@Path("/logout") | |
public Message logout ( | |
@Context HttpServletRequest request) throws CouponException{ | |
HttpSession session = request.getSession(false); | |
CustomerFacade customerFacade = (CustomerFacade) request.getSession(false).getAttribute(CUSTOMER_SESSION_ATTR); | |
long id = customerFacade.getId(); | |
if (session != null){ | |
session.invalidate(); | |
} | |
return new Message("Successfully logged out customer #" + id); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package coupons.ejb; | |
import java.io.Serializable; | |
import java.util.Date; | |
import javax.persistence.Entity; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.Id; | |
import javax.persistence.NamedQueries; | |
import javax.persistence.NamedQuery; | |
import javax.persistence.Table; | |
import javax.xml.bind.annotation.XmlRootElement; | |
@Entity | |
@Table (name ="INCOME_TBL") | |
@NamedQueries ({ | |
@NamedQuery(name="viewAllIncome", query="SELECT i FROM Income AS i"), | |
@NamedQuery(name="viewAllIncomeByCompany", query="SELECT i FROM Income AS i WHERE i.invokerId = :companyId AND i.incomeType <> :customerPurchase ORDER BY i.invokerId ASC"), | |
@NamedQuery(name="viewAllIncomeByCustomer", query="SELECT i FROM Income AS i WHERE i.invokerId = :customerId AND i.incomeType = :customerPurchase ORDER BY i.invokerId ASC") | |
}) | |
@XmlRootElement | |
public class Income implements Serializable { | |
private static final long serialVersionUID = -3940907325227433192L; | |
private long invokerId; | |
private Date timestamp; | |
private incomeType IncomeType; | |
private double amount; //CustomerPurchase = price, CompanyCreate = 100, CompanyUpdate = 10 | |
private Long incomeId; | |
public Income(long invokerId, incomeType incomeType, double amount) { | |
super(); | |
this.invokerId = invokerId; | |
this.timestamp = new Date(); | |
IncomeType = incomeType; | |
this.amount = amount; | |
} | |
public Income () { | |
} | |
public long getInvokerId() { | |
return invokerId; | |
} | |
public void setInvokerId(long invokerId) { | |
this.invokerId = invokerId; | |
} | |
public Date getTimestamp() { | |
return timestamp; | |
} | |
public void setTimestamp(Date timestamp) { | |
this.timestamp = timestamp; | |
} | |
public incomeType getIncomeType() { | |
return IncomeType; | |
} | |
public void setIncomeType(incomeType incomeType) { | |
IncomeType = incomeType; | |
} | |
public double getAmount() { | |
return amount; | |
} | |
public void setAmount(double amount) { | |
this.amount = amount; | |
} | |
@Id | |
@GeneratedValue | |
public Long getIncomeId() { | |
return incomeId; | |
} | |
public void setIncomeId(Long incomeId) { | |
this.incomeId = incomeId; | |
} | |
@Override | |
public String toString() { | |
return "Income [invokerId=" + invokerId + ", timestamp=" + timestamp + ", IncomeType=" + IncomeType | |
+ ", amount=" + amount + ", incomeId=" + incomeId + "]"; | |
} | |
@Override | |
public int hashCode() { | |
final int prime = 31; | |
int result = 1; | |
result = prime * result + ((IncomeType == null) ? 0 : IncomeType.hashCode()); | |
long temp; | |
temp = Double.doubleToLongBits(amount); | |
result = prime * result + (int) (temp ^ (temp >>> 32)); | |
result = prime * result + ((incomeId == null) ? 0 : incomeId.hashCode()); | |
result = prime * result + (int) (invokerId ^ (invokerId >>> 32)); | |
result = prime * result + ((timestamp == null) ? 0 : timestamp.hashCode()); | |
return result; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) | |
return true; | |
if (obj == null) | |
return false; | |
if (getClass() != obj.getClass()) | |
return false; | |
Income other = (Income) obj; | |
if (IncomeType != other.IncomeType) | |
return false; | |
if (Double.doubleToLongBits(amount) != Double.doubleToLongBits(other.amount)) | |
return false; | |
if (incomeId == null) { | |
if (other.incomeId != null) | |
return false; | |
} else if (!incomeId.equals(other.incomeId)) | |
return false; | |
if (invokerId != other.invokerId) | |
return false; | |
if (timestamp == null) { | |
if (other.timestamp != null) | |
return false; | |
} else if (!timestamp.equals(other.timestamp)) | |
return false; | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package coupons.ejb.dao; | |
import java.util.List; | |
import javax.ejb.Local; | |
import coupons.ejb.Income; | |
@Local //Remote (Mashpia al Ziun, leahlit ahrei ze) | |
public interface IncomeDAO { | |
public void storeIncome (Income income); | |
public List<Income> viewAllIncome (); | |
public List<Income> viewAllIncomeByCompany (long companyId); | |
public List<Income> viewAllIncomeByCustomer (long customerId); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package coupons.ejb.dao; | |
import java.util.List; | |
import javax.ejb.Stateless; | |
import javax.persistence.EntityManager; | |
import javax.persistence.PersistenceContext; | |
import coupons.ejb.Income; | |
import coupons.ejb.incomeType; | |
@Stateless | |
public class IncomeDAOBean implements IncomeDAO{ | |
@PersistenceContext(unitName="coupons") | |
private EntityManager em; | |
@Override | |
public void storeIncome(Income income) { | |
em.persist(income); | |
} | |
@Override | |
public List<Income> viewAllIncome() { | |
return em.createNamedQuery("viewAllIncome", Income.class) | |
.getResultList(); | |
} | |
@Override | |
public List<Income> viewAllIncomeByCompany(long companyId) { | |
return em.createNamedQuery("viewAllIncomeByCompany", Income.class) | |
.setParameter("customerPurchase", incomeType.CustomerBuy) | |
.setParameter("companyId", companyId) | |
.getResultList(); | |
} | |
@Override | |
public List<Income> viewAllIncomeByCustomer(long customerId) { | |
return em.createNamedQuery("viewAllIncomeByCustomer", Income.class) | |
.setParameter("customerPurchase", incomeType.CustomerBuy) | |
.setParameter("customerId", customerId) | |
.getResultList(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package coupons.ejb.singleton; | |
import javax.annotation.PostConstruct; | |
import javax.ejb.EJB; | |
import javax.ejb.Singleton; | |
import javax.ejb.Startup; | |
import coupons.ejb.Income; | |
import coupons.ejb.incomeType; | |
import coupons.ejb.dao.IncomeDAO; | |
//@Singleton | |
@Startup | |
public class IncomeDAOSingleton { | |
@EJB | |
private IncomeDAO daoStaub; | |
@PostConstruct | |
public void init (){ | |
daoStaub.storeIncome(new Income(500, incomeType.CompanyCreate, 100)); | |
daoStaub.storeIncome(new Income(400, incomeType.CompanyCreate, 100)); | |
daoStaub.storeIncome(new Income(300, incomeType.CompanyCreate, 100)); | |
daoStaub.storeIncome(new Income(500, incomeType.CompanyUpdate, 10)); | |
daoStaub.storeIncome(new Income(500, incomeType.CustomerBuy, 150)); | |
daoStaub.storeIncome(new Income(6600, incomeType.CustomerBuy, 1050)); | |
daoStaub.storeIncome(new Income(6690, incomeType.CustomerBuy, 130050)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package coupons.ejb.dao; | |
import javax.ejb.EJB; | |
import javax.ejb.Stateless; | |
import javax.jws.WebService; | |
import coupons.ejb.Income; | |
@Stateless | |
@WebService | |
public class IncomeDAOTesterBean { | |
@EJB | |
private IncomeDAO daoStub; | |
public void storeIncome (Income income){ | |
daoStub.storeIncome(income); | |
} | |
public Income [] viewAllIncome () { | |
return daoStub.viewAllIncome().toArray(new Income[0]); | |
} | |
public Income [] viewAllIncomeByCompany (long companyId) { | |
return daoStub.viewAllIncomeByCompany(companyId).toArray(new Income[0]); | |
} | |
public Income [] viewAllIncomeByCustomer (long cutomerId) { | |
return daoStub.viewAllIncomeByCustomer(cutomerId).toArray(new Income[0]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package coupons.ejb; | |
import javax.ejb.Local; | |
@Local //Remote (Mashpia al Ziun, leahlit ahrei ze) | |
public interface IncomeDeligate { | |
public void saveIncome (Income income) ; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package coupons.ejb.bean; | |
import javax.annotation.PostConstruct; | |
import javax.annotation.PreDestroy; | |
import javax.ejb.Stateless; | |
import javax.jms.ObjectMessage; | |
import javax.jms.Queue; | |
import javax.jms.QueueConnection; | |
import javax.jms.QueueConnectionFactory; | |
import javax.jms.QueueSender; | |
import javax.jms.QueueSession; | |
import javax.jws.WebMethod; | |
import javax.naming.InitialContext; | |
import coupons.ejb.Income; | |
import coupons.ejb.IncomeDeligate; | |
@Stateless | |
public class IncomeDeligateBean implements IncomeDeligate { | |
private QueueConnection connection; | |
private QueueSession session; | |
private QueueSender sender; | |
private ObjectMessage message; | |
@PostConstruct | |
@WebMethod(exclude = true) | |
public void init() { | |
try { | |
// 0. get initial context | |
InitialContext ctx = new InitialContext(); | |
// 1. lookup connection factory | |
QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory"); | |
// 2. create connection | |
connection = factory.createQueueConnection(); | |
// 3. create session | |
session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); | |
// 4. lookup topic | |
Queue destination = (Queue) ctx.lookup("java:/jms/queue/ExpiryQueue"); | |
// 5. create publisher | |
sender = session.createSender(destination); | |
// 6. create message | |
message = session.createObjectMessage(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
@PreDestroy | |
public void destroy() { | |
try { | |
sender.close(); | |
} catch (Exception e2) { | |
e2.printStackTrace(); | |
} | |
try { | |
session.close(); | |
} catch (Exception e1) { | |
e1.printStackTrace(); | |
} | |
try { | |
connection.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public void saveIncome(Income income) { | |
try { | |
message.setObject(income); | |
sender.send(message); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package coupons.ejb; | |
public enum incomeType { | |
CompanyCreate, CompanyUpdate, CustomerBuy | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package coupons.ejb.bean; | |
import javax.ejb.ActivationConfigProperty; | |
import javax.ejb.EJB; | |
import javax.ejb.MessageDriven; | |
import javax.jms.Message; | |
import javax.jms.MessageListener; | |
import javax.jms.ObjectMessage; | |
import coupons.ejb.Income; | |
import coupons.ejb.dao.IncomeDAO; | |
@MessageDriven(activationConfig = { | |
@ActivationConfigProperty(propertyName = "destination", propertyValue = "java:/jms/queue/ExpiryQueue"), | |
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") }) | |
public class MessageDrivenBean implements MessageListener { | |
@EJB | |
private IncomeDAO daoStaub; | |
@Override | |
public void onMessage(Message message) { | |
try { | |
if (message instanceof ObjectMessage) { | |
ObjectMessage objectMessage = (ObjectMessage) message; | |
Object object = objectMessage.getObject(); | |
if (object instanceof Income) { | |
Income income = (Income) object; | |
daoStaub.storeIncome(income); | |
} else { | |
throw new Exception("illegal object type"); | |
} | |
} else { | |
throw new Exception("illegal message type!"); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Money side project (self project)
EJB3.2, JPA, RESTfull Web Services, JNDI, Json involved
(Also HTML, CSS, JavaScript(Anqular) involved but hided)