Permalink
Browse files

Cost type adding

Cost adding
  • Loading branch information...
1 parent a8306d9 commit 49577bc5e6bf4380a962884007b1d86c6c12e564 @koncinar committed Oct 30, 2011
@@ -89,4 +89,17 @@ public CreationData getCreationData() {
public void setCreationData(CreationData creationData) {
this.creationData = creationData;
}
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ return id.equals(((Cost) o).id);
+
+ }
+
+ @Override
+ public int hashCode() {
+ return id.hashCode();
+ }
}
@@ -62,4 +62,21 @@ public String getDescription() {
public void setDescription(String description) {
this.description = description;
}
+
+// @Override
+// public boolean equals(Object o) {
+// if (this == o) return true;
+// if (o == null || getClass() != o.getClass()) return false;
+//
+// CostType costType = (CostType) o;
+//
+// if (!id.equals(costType.id)) return false;
+//
+// return true;
+// }
+//
+// @Override
+// public int hashCode() {
+// return id.hashCode();
+// }
}
@@ -0,0 +1,59 @@
+package pro.logica.averagebill.persistence;
+
+import pro.logica.averagebill.datamodel.Cost;
+import pro.logica.averagebill.datamodel.CreationData;
+
+import javax.annotation.PostConstruct;
+import javax.ejb.Stateful;
+import javax.enterprise.event.Event;
+import javax.enterprise.inject.Model;
+import javax.enterprise.inject.Produces;
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import java.util.Date;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: rok
+ * Date: 23.10.11
+ * Time: 20:39
+ */
+@Named
+@Stateful
+@Model
+public class CostCreation {
+ @PersistenceContext
+ private EntityManager em;
+ @Inject
+ private Event<Cost> costTypeEventSrc;
+ private Cost newCost;
+
+ @PostConstruct
+ public void initNewMember() {
+ newCost = new Cost();
+ newCost.setId("" + new Date().getTime());
+ CreationData creationData = new CreationData();
+ creationData.setIp("1.2.3." + (int)(Math.random() * 10));
+ creationData.setNickName("nick " + Math.random());
+ newCost.setCreationData(creationData);
+ }
+
+ @Produces
+ @Named
+ public Cost getNewCost() {
+ return newCost;
+ }
+
+ public void saveCost() {
+ newCost.getCreationData().setCreated(new Date());
+ em.persist(newCost);
+ costTypeEventSrc.fire(newCost);
+ initNewMember();
+ }
+
+ public void delete(Cost cost) {
+ em.remove(em.getReference(Cost.class, cost.getId()));
+ }
+}
@@ -0,0 +1,55 @@
+package pro.logica.averagebill.persistence;
+
+import pro.logica.averagebill.datamodel.Cost;
+
+import javax.annotation.PostConstruct;
+import javax.ejb.Stateful;
+import javax.enterprise.event.Event;
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.Model;
+import javax.enterprise.inject.Produces;
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.Query;
+import java.util.List;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: rok
+ * Date: 23.10.11
+ * Time: 20:47
+ */
+@Stateful
+@Model
+@Named
+public class CostPersistence {
+
+@PersistenceContext
+private EntityManager em;
+
+ private List<Cost> costs;
+ @Inject private Event<Cost> costEvent;
+
+ @Produces
+ @Named("costs")
+ public List<Cost> getCosts() {
+ return costs;
+ }
+
+ @PostConstruct
+ private void retrieveCosts(@Observes Cost cost) {
+ retrieveCosts();
+ }
+
+ public void retrieveCosts() {
+ Query query = em.createQuery("select c from Cost c order by c.creationData");
+ costs = query.getResultList();
+ }
+
+ public void deleteCost(Cost cost) {
+ em.remove(cost);
+ costEvent.fire(cost);
+ }
+}
@@ -0,0 +1,41 @@
+package pro.logica.averagebill.persistence;
+
+import pro.logica.averagebill.datamodel.CostType;
+
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+import javax.inject.Inject;
+import javax.inject.Named;
+import java.util.List;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: rok
+ * Date: 20.10.11
+ * Time: 1:31
+ */
+@Named
+public class CostTypeConverter implements Converter {
+ @Inject private List<CostType> costTypes;
+
+ public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String s) {
+ if (costTypes != null) {
+ System.out.println(costTypes.size());
+ } else {
+ System.out.println("costTypes is null");
+ }
+ for (CostType costType : costTypes) {
+ if (costType.getName() != null && costType.getName().equals(s)) {
+ System.out.println(String.format("Returning costType: %s", costType.getName()));
+ return costType;
+ }
+ }
+ System.out.println("Returning costType: null");
+ return null;
+ }
+
+ public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object o) {
+ return ((CostType) o).getName();
+ }
+}
@@ -0,0 +1,52 @@
+package pro.logica.averagebill.persistence;
+
+import pro.logica.averagebill.datamodel.CostType;
+
+import javax.annotation.PostConstruct;
+import javax.ejb.Stateful;
+import javax.enterprise.event.Event;
+import javax.enterprise.inject.Model;
+import javax.enterprise.inject.Produces;
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: rok
+ * Date: 23.10.11
+ * Time: 20:39
+ */
+@Stateful
+@Model
+public class CostTypeCreation {
+ @PersistenceContext
+ private EntityManager em;
+ @Inject
+ private Event<CostType> costTypeEventSrc;
+
+ private CostType newCostType;
+
+ @PostConstruct
+ public void initNewMember() {
+ newCostType = new CostType();
+ }
+
+ @Produces
+ @Named
+ public CostType getNewCostType() {
+ return newCostType;
+ }
+
+ public void saveCostType() {
+ em.persist(newCostType);
+ costTypeEventSrc.fire(newCostType);
+ initNewMember();
+ }
+
+ public void delete(CostType costType) {
+ em.remove(em.getReference(CostType.class, costType.getId()));
+ costTypeEventSrc.fire(costType);
+ }
+}
@@ -0,0 +1,49 @@
+package pro.logica.averagebill.persistence;
+
+import pro.logica.averagebill.datamodel.CostType;
+
+import javax.annotation.PostConstruct;
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.Produces;
+import javax.faces.bean.ApplicationScoped;
+import javax.inject.Named;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.Query;
+import java.util.List;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: rok
+ * Date: 23.10.11
+ * Time: 20:47
+ */
+//@Stateful
+//@Model
+@ApplicationScoped
+@Named
+public class CostTypePersistence {
+
+@PersistenceContext
+private EntityManager em;
+
+ private List<CostType> costTypes;
+
+ @Produces
+ @Named("costTypes")
+ public List<CostType> getCostTypes() {
+ return costTypes;
+ }
+
+ private void retrieveCostTypes(@Observes CostType costType) {
+ retrieveCostTypes();
+ }
+
+ @PostConstruct
+ public void retrieveCostTypes() {
+ System.out.println("Reading CostType's...");
+ Query query = em.createQuery("select ct from CostType ct order by ct.name");
+ costTypes = query.getResultList();
+ System.out.println(String.format("%s CostType's found.", costTypes == null ? 0 : costTypes.size()));
+ }
+}
@@ -1,8 +1,8 @@
<persistence version="2.0">
<persistence-unit name="primary">
- <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
+ <jta-data-source>java:/mydb</jta-data-source>
<properties>
- <property name="hibernate.hbm2ddl.auto" value="create" />
+ <property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
@@ -1,15 +1,13 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core">
<h:head>
- <title>CDI Demo</title>
- <h:outputStylesheet name="screen.css"/>
</h:head>
<h:body>
<div id="container">
<div id="content">
<a href="index.jsf">index</a>
<h:form id="reg">
- <h2>Register</h2>
+ <h2>Vnesi tipe stroškov</h2>
<h:panelGrid columns="3">
<h:outputLabel for="id" value="Id:"/>
@@ -53,9 +51,6 @@
</h:dataTable>
</h:form>
</div>
- <div id="footer">
- <h:graphicImage value="/resources/weld.png" alt="Weld logo"/>
- </div>
</div>
</h:body>
</html>
@@ -1,8 +1,7 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core">
<h:head>
- <title>CDI Demo</title>
- <h:outputStylesheet name="screen.css"/>
+ <title>Average costs</title>
</h:head>
<h:body>
<div id="container">
@@ -13,8 +12,8 @@
<h:panelGrid columns="3">
<h:outputLabel for="name" value="Name:"/>
- <h:selectOneMenu value="#{newCost.costType}" id="name" converter="#{namedConverter}">
- <f:selectItems value="#{costTypes}" var="cost" itemLabel="#{cost.name}"/>
+ <h:selectOneMenu value="#{newCost.costType}" id="name" converter="#{costTypeConverter}">
+ <f:selectItems value="#{costTypePersistence.costTypes}" var="cost" itemLabel="#{cost.name}"/>
</h:selectOneMenu>
<h:message for="name" errorClass="invalid"/>
@@ -59,10 +58,10 @@
<f:convertNumber minFractionDigits="2" maxFractionDigits="2" currencySymbol=""/>
</h:outputText>
</h:column>
- <!--<h:column>-->
- <!--<f:facet name="header">Delete</f:facet>-->
- <!--<h:commandLink value="Delete" action="#{memberRegistration.delete(cost)}"/>-->
- <!--</h:column>-->
+ <h:column>
+ <f:facet name="header">Delete</f:facet>
+ <h:commandLink value="Delete" action="#{costPersistence.deleteCost(cost)}"/>
+ </h:column>
</h:dataTable>
</h:form>
<h:outputText value="Povprecna poloznica: #{statisticsCalculator.getAverage(costs)}"/>

0 comments on commit 49577bc

Please sign in to comment.