<file_sep>/*
 * 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 entities;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author it354f715
 */
@Entity
@Table(name = "DECKS")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Decks.findAll", query = "SELECT d FROM Decks d"),
    @NamedQuery(name = "Decks.findById", query = "SELECT d FROM Decks d WHERE d.id = :id"),
    @NamedQuery(name = "Decks.findByDeckname", query = "SELECT d FROM Decks d WHERE d.deckname = :deckname"),
    @NamedQuery(name = "Decks.findByUserid", query = "SELECT d FROM Decks d WHERE d.userid = :userid"),
    @NamedQuery(name = "Decks.findByClassid", query = "SELECT d FROM Decks d WHERE d.classid = :classid")})
public class Decks implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "DECKNAME")
    private String deckname;
    @Basic(optional = false)
    @NotNull
    @Column(name = "USERID")
    private int userid;
    @Column(name = "CLASSID")
    private Integer classid;

    public Decks() {
    }

    public Decks(Integer id) {
        this.id = id;
    }

    public Decks(Integer id, String deckname, int userid) {
        this.id = id;
        this.deckname = deckname;
        this.userid = userid;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getDeckname() {
        return deckname;
    }

    public void setDeckname(String deckname) {
        this.deckname = deckname;
    }

    public int getUserid() {
        return userid;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public Integer getClassid() {
        return classid;
    }

    public void setClassid(Integer classid) {
        this.classid = classid;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Decks)) {
            return false;
        }
        Decks other = (Decks) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Decks[ id=" + id + " ]";
    }
    
}
<file_sep>/*
 * 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 entities.service;

import entities.Decks;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

/**
 *
 * @author it354f715
 */
@Stateless
@Path("entities.decks")
public class DecksFacadeREST extends AbstractFacade<Decks> {

    @PersistenceContext(unitName = "MyFlashCardWSPU")
    private EntityManager em;

    public DecksFacadeREST() {
        super(Decks.class);
    }

    @POST
    @Override
    @Consumes({"application/xml", "application/json"})
    public void create(Decks entity) {
        super.create(entity);
    }

    @PUT
    @Path("{id}")
    @Consumes({"application/xml", "application/json"})
    public void edit(@PathParam("id") Integer id, Decks entity) {
        super.edit(entity);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public Decks find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<Decks> findAll() {
        return super.findAll();
    }

    @GET
    @Path("findAllOrderByDeckname")
    @Produces({"application/xml", "application/json"})
    public List<Decks> findAllOrderByDeckname() {
        Query q = em.createQuery("SELECT DISTINCT d FROM Decks d ORDER BY d.deckname");
        return q.getResultList();
    }

    @GET
    @Path("findByClassid/{classid}")
    @Produces({"application/xml", "application/json"})
    public List<Decks> findByClassid(@PathParam("classid") String classid) {
        Query q = em.createQuery("SELECT DISTINCT d FROM Decks d WHERE d.classid = " + classid + " ORDER BY d.deckname");
        return q.getResultList();
    }

    @GET
    @Path("findByClassnumber/{classnumber}")
    @Produces({"application/xml", "application/json"})
    public List<Decks> findByClassnumber(@PathParam("classnumber") String classnumber) {
        Query q = em.createQuery("SELECT DISTINCT d FROM Decks d JOIN Assignments a ON d.id = a.deckid JOIN Classes c ON a.classid = c.id WHERE c.classnumber = :classnumber ORDER BY d.deckname");
        q.setParameter("classnumber", classnumber);
        return q.getResultList();
    }

    @GET
    @Path("findByUsername/{username}")
    @Produces({"application/xml", "application/json"})
    public List<Decks> findByUsername(@PathParam("username") String username) {
        Query q = em.createQuery("SELECT DISTINCT d FROM Decks d JOIN Users u ON d.userid = u.id WHERE u.username = :username ORDER BY d.deckname");
        q.setParameter("username", username);
        return q.getResultList();
    }
    
    @GET
    @Path("findByUseridAndClassidAndDeckname/{userid}/{classid}/{deckname}")
    @Produces({"application/xml", "application/json"})
    public List<Decks> findByUseridAndDecknameAndClassid(@PathParam("deckname") String deckname, @PathParam("classid") String classid, @PathParam("userid") String userid) {
        Query q = em.createQuery("SELECT DISTINCT d FROM Decks d WHERE lower(d.deckname) = lower(:deckname) AND d.classid = " + classid + " AND d.userid = " + userid);
        q.setParameter("deckname", deckname);
        return q.getResultList();
    }

    @GET
    @Path("findByClassnmae/{classname}")
    @Produces({"application/xml", "application/json"})
    public List<Decks> findByClassname(@PathParam("classname") String classname) {
        Query q = em.createQuery("SELECT DISTINCT d FROM Decks d JOIN Assignments a ON d.id = a.deckid JOIN Classes c ON a.classid = c.id WHERE lower(c.classname) LIKE lower(concat('%', :classname, '%')) ORDER BY d.deckname");
        q.setParameter("classname", classname);
        return q.getResultList();
    }
    
    @GET
    @Path("byDeckname/{deckname}")
    @Produces({"application/xml", "application/json"})
    public List<Decks> findByWord(@PathParam("deckname") String deckname) {
        Query q = em.createQuery("SELECT DISTINCT d FROM Decks d WHERE lower(d.deckname) = lower(:deckname)");
        q.setParameter("deckname", deckname);
        return q.getResultList();
    }

    @GET
    @Path("findByKeyword/{keyword}")
    @Produces({"application/xml", "application/json"})
    public List<Decks> findBykeyword(@PathParam("keyword") String keyword) {
        Query q = em.createQuery("SELECT DISTINCT d FROM Decks d JOIN Cards c ON d.id = c.deckid WHERE lower(c.answer) LIKE lower(concat('%', :keyword, '%')) OR lower(c.question) LIKE lower(concat('%', :keyword, '%')) ORDER BY d.deckname");
        q.setParameter("keyword", keyword);
        return q.getResultList();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<Decks> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

}
