AccessInBuildingSession.Java

package accessinbuilding;

import java.text.DateFormat;

import java.util.Date;

import java.util.List;

import javax.ejb.Stateful;

import javax.ejb.Stateless;

import javax.persistence.EntityManager;

import javax.persistence.PersistenceContext;

import javax.persistence.Query;

@Stateful

public class AccessInBuildingSession implements AccessInBuildingSessionRemote, AccessInBuildingSessionLocal {

@PersistenceContext(unitName = "AccessInBuilding-ejbPU")

private EntityManager em;

private String token = "";

public void login(String login, String pwd) {

if( "client".equals(login) && "pwd_client".equals(pwd) ) token = "token_key";

else token = "";

}

public String getToken() {

return token;

}

public void persist(Object object) {

//em.persist(object);

}

@Override

public int findByNameOrCode(String name, int code) {

Query query = em.createNamedQuery("Access.findByAccess");

query.setParameter("name", name);

query.setParameter("code", code);

List access = query.getResultList();

if( access.size() > 0 ) {

return ((Access)access.get(0)).getId();

} else {

return 0;

}

}

@Override

public void addAccess(String name, int code) {

Access access = new Access();

access.setName(name);

if( code > 0 ) {

access.setCode(code);

}

em.persist(access);

addLog(access.getId(),0,"добавлен новый пользователь");

}

@Override

public int findCountAct() {

Query query = em.createNamedQuery("Access.findCountAct");

List access = query.getResultList();

if( access.size() > 0 ) {

return access.size();

} else {

return 0;

}

}

@Override

public List getAllUsers() {

Query query = em.createNamedQuery("Access.findAll");

List access = query.getResultList();

return access;

}

@Override

public void updateAct(int id, int act) {

Query query = em.createNamedQuery("Access.findById");

query.setParameter("id", id);

List a = query.getResultList();

Access access;

access = (Access)a.get(0);

access.setAct(act);

em.persist(access);

}

@Override

public void addLog(int id, int act, String descr) {

Access access = em.find(Access.class, id);

String name = access.getName();

if( descr == "" )

descr = ( act > 0 ? "вход в здание" : "выход из здания" );

Log log = new Log();

log.setDate( new Date() );

log.setDescr(id + ":" + name +":" + descr);

log.setAssignedAccess(access);

em.persist(log);

}

@Override

public List getLogs() {

Query query = em.createNamedQuery("Log.findAll").setMaxResults(20);

List log = query.getResultList();

return log;

}

}

AccessInBuildingSessionLocal.java

package accessinbuilding;

import javax.ejb.Local;

@Local

public interface AccessInBuildingSessionLocal {

}

Log.java

package accessinbuilding;

import java.io.Serializable;

import java.util.Date;

import javax.persistence.Basic;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.ManyToOne;

import javax.persistence.NamedQueries;

import javax.persistence.NamedQuery;

import javax.persistence.Table;

import javax.persistence.Temporal;

import javax.persistence.TemporalType;

import javax.validation.constraints.NotNull;

import javax.validation.constraints.Size;

import javax.xml.bind.annotation.XmlRootElement;

@Entity

@Table(name = "LOG")

@XmlRootElement

@NamedQueries({

@NamedQuery(name = "Log.findAll", query = "SELECT l FROM Log l ORDER BY l.date DESC"),

@NamedQuery(name = "Log.findById", query = "SELECT l FROM Log l WHERE l.id = :id"),

@NamedQuery(name = "Log.findByDate", query = "SELECT l FROM Log l WHERE l.date = :date"),

@NamedQuery(name = "Log.findByDescr", query = "SELECT l FROM Log l WHERE l.descr = :descr")})

public class Log implements Serializable {

private static final long serialVersionUID = 1L;

@ManyToOne

@NotNull

private Access assignedAccess;

public Access getAssignedAccess() {

return assignedAccess;

}

public void setAssignedAccess(Access access) {

this.assignedAccess = access;

}

@Id

@Basic(optional = false)

@NotNull

@GeneratedValue

@Column(name = "ID")

private Integer id;

@Basic(optional = false)

@NotNull

@Column(name = "DATE")

@Temporal(TemporalType.DATE)

private Date date;

@Basic(optional = false)

@NotNull

@Size(min = 1, max = 120)

@Column(name = "DESCR")

private String descr;

public Log() {

}

public Log(Integer id) {

this.id = id;

}

public Log(Integer id, Date date, String descr) {

this.id = id;

this.date = date;

this.descr = descr;

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public Date getDate() {

return date;

}

public void setDate(Date date) {

this.date = date;

}

public String getDescr() {

return descr;

}

public void setDescr(String descr) {

this.descr = descr;

}

@Override

public int hashCode() {

int hash = 0;

hash += (id != null ? id.hashCode() : 0);

return hash;

}

@Override

public boolean equals(Object object) {

if (!(object instanceof Log)) {

return false;

}

Log other = (Log) 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 "accessinbuilding.Log[ id=" + id + " ]";

}

}

Наши рекомендации