我已经有一个 CRUD webApp (JSF+EJB+JPA),我正在尝试开发一个 chartBean 类,以便可以在 View 层中使用它。要渲染的数据(通过 Primefaces-4 BarChart)应该从数据库中读取。
在图表中,我有 2 个要显示的 chartSeries:
- chartSeries1:employeeGoal -> 下面的 Orc 实体类中映射的 'valor' 浮点列;
- chartSeries2:employeeAccomplished -> 在下面的 hr_capacit30h 实体类中映射的 'Realizado' 整数列。
X 轴应显示小时数(基于上面的“chartSeries2”值)。
Y 轴应显示employeeName(下面的hr_capacit30h 实体类中的'nome' 字符串字段)。
有谁知道如何开发下面的 createCartesianChartModel() 方法以在 jsf 页面中使用?
ChartBean 类:
//imports ommited
@ManagedBean
@RequestScoped
public class hrCapacitChart {
private Map<Integer, Map<String, Number>> HrCapacitFuncis = new HashMap<Integer, Map<String, Number>>();
private double totalHoras;
private CartesianChartModel cartesianChartModel;
@EJB
private HrCapacit30hFacade hcf;
public hrCapacitChart() {
}
@PostConstruct
private void initialize() {
cartesianChartModel = new CartesianChartModel();
createCartesianChartModel();
}
public CartesianChartModel getCartesianChartModel() {
return cartesianChartModel;
}
private void createCartesianChartModel() {
List<HrCapacit30h> hrCapacit30h = hcf.findAll();
// THIS IS THE METHOD/(Managed Bean property) TO BE DEVELOPED
}
}
HrCapacit30h 实体类(与 chartSeries2 和 Y 轴相关/见上文描述):
@Entity
@Table(name = "hr_capacit30h", catalog = "DIAGE", schema = "atb")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "HrCapacit30h.findAll", query = "SELECT h FROM HrCapacit30h h"),
@NamedQuery(name = "HrCapacit30h.findByMatricula", query = "SELECT h FROM HrCapacit30h h WHERE h.hrCapacit30hPK.matricula = :matricula"),
@NamedQuery(name = "HrCapacit30h.findByNome", query = "SELECT h FROM HrCapacit30h h WHERE h.nome = :nome"),
@NamedQuery(name = "HrCapacit30h.findByRealizado", query = "SELECT h FROM HrCapacit30h h WHERE h.realizado = :realizado"),
@NamedQuery(name = "HrCapacit30h.findByDtMov", query = "SELECT h FROM HrCapacit30h h WHERE h.hrCapacit30hPK.dtMov = :dtMov")})
public class HrCapacit30h implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected HrCapacit30hPK hrCapacit30hPK;
@Size(max = 100)
@Column(name = "Nome")
private String nome;
@Column(name = "Realizado")
private Integer realizado;
@JoinColumn(name = "codUOR", referencedColumnName = "cod_UOR")
@ManyToOne(optional = false)
private UpbDeps codUOR;
@JoinColumn(name = "status", referencedColumnName = "id")
@ManyToOne(optional = false)
private Status status;
@JoinColumn(name = "idOrc", referencedColumnName = "id")
@ManyToOne
private Orc idOrc;
@JoinColumn(name = "idDiv", referencedColumnName = "id")
@ManyToOne(optional = false)
private DivDeps idDiv;
public HrCapacit30h() {
}
//getters/setters/equals/hashCode ommited
}
Entity Orc 类(与 chartSeries1 相关/见上文描述)::
//imports ommited
@Entity
@Table(name = "orc", catalog = "DIAGE", schema = "atb")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Orc.findAll", query = "SELECT o FROM Orc o"),
@NamedQuery(name = "Orc.findById", query = "SELECT o FROM Orc o WHERE o.id = :id"),
@NamedQuery(name = "Orc.findByNomeItem", query = "SELECT o FROM Orc o WHERE o.nomeItem = :nomeItem"),
@NamedQuery(name = "Orc.findByDescItem", query = "SELECT o FROM Orc o WHERE o.descItem = :descItem"),
@NamedQuery(name = "Orc.findByValor", query = "SELECT o FROM Orc o WHERE o.valor = :valor"),
@NamedQuery(name = "Orc.findByDtRef", query = "SELECT o FROM Orc o WHERE o.dtRef = :dtRef")})
public class Orc implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "id")
private Integer id;
@Size(max = 100)
@Column(name = "NomeItem")
private String nomeItem;
@Size(max = 255)
@Column(name = "DescItem")
private String descItem;
// @Max(value=?) @Min(value=?)//to enforce field validation to known decimal range values
@Column(name = "valor")
private Double valor;
@Column(name = "DtRef")
@Temporal(TemporalType.TIMESTAMP)
private Date dtRef;
@OneToMany(mappedBy = "idOrc")
private Collection<HrCapacit30h> hrCapacit30hCollection;
//getters/setters/equals/hashCode ommited
}
EJB(抽象外观):
//imports ommited
public abstract class AbstractFacade<T> {
private Class<T> entityClass;
public AbstractFacade(Class<T> entityClass) {
this.entityClass = entityClass;
}
protected abstract EntityManager getEntityManager();
public void create(T entity) {
getEntityManager().persist(entity);
}
public void edit(T entity) {
getEntityManager().merge(entity);
}
public void remove(T entity) {
getEntityManager().remove(getEntityManager().merge(entity));
}
public T find(Object id) {
return getEntityManager().find(entityClass, id);
}
public List<T> findAll() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return getEntityManager().createQuery(cq).getResultList();
}
public List<T> findRange(int[] range) {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
javax.persistence.Query q = getEntityManager().createQuery(cq);
q.setMaxResults(range[1] - range[0] + 1);
q.setFirstResult(range[0]);
return q.getResultList();
}
public int count() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
cq.select(getEntityManager().getCriteriaBuilder().count(rt));
javax.persistence.Query q = getEntityManager().createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
}
}
hr_capacit30h EJB 外观:
//imports ommited
@Stateless
public class HrCapacit30hFacade extends AbstractFacade<HrCapacit30h> {
@PersistenceContext(unitName = "atb-hrCapacit30PU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public HrCapacit30hFacade() {
super(HrCapacit30h.class);
}
}
Orc EJB 门面:
//imports ommited
@Stateless
public class OrcFacade extends AbstractFacade<Orc> {
@PersistenceContext(unitName = "atb-hrCapacit30PU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public OrcFacade() {
super(Orc.class);
}
}
提前致谢。