This commit is contained in:
Egor 2023-03-10 04:20:22 +03:00
parent 8e6d912eea
commit 0e4b1a9fe5
8 changed files with 99 additions and 22 deletions

View file

@ -4,6 +4,7 @@
<data-source source="LOCAL" name="helios postgres" uuid="1d89802b-1b14-4f1f-bdf1-c835d25f1fd2">
<driver-ref>postgresql</driver-ref>
<synchronize>true</synchronize>
<configured-by-url>true</configured-by-url>
<jdbc-driver>org.postgresql.Driver</jdbc-driver>
<jdbc-url>jdbc:postgresql://pg:5432/studs</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>

View file

@ -22,10 +22,11 @@ tasks.withType(JavaCompile) {
}
dependencies {
compileOnly('javax.persistence:javax.persistence-api:2.2')
compileOnly('javax.faces:javax.faces-api:2.3')
compileOnly('javax.enterprise:cdi-api:2.0.SP1')
compileOnly('org.primefaces:primefaces:11.0.0')
compileOnly 'javax.persistence:javax.persistence-api:2.2'
compileOnly 'javax.enterprise:cdi-api:2.0.SP1'
compileOnly 'javax.faces:javax.faces-api:2.3-pfd'
compileOnly 'org.postgresql:postgresql:42.5.4'
compileOnly 'org.hibernate:hibernate-core:6.1.7.Final'
testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")

View file

@ -1,12 +1,27 @@
package ru.erius.weblab3.data;
import javax.persistence.*;
import java.io.Serializable;
@Entity(name = "hit_info")
@Table(name = "hit_info", schema = "s316304")
@NamedQueries({
@NamedQuery(name = "load", query = "select h from hit_info h"),
@NamedQuery(name = "clear", query = "delete from hit_info")
})
public class HitInfo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name = "x")
private float x;
@Column(name = "y")
private float y;
@Column(name = "r")
private float r = 1;
@Column(name = "hit")
private boolean hit;
public HitInfo() {
@ -57,4 +72,12 @@ public class HitInfo implements Serializable {
public void setHit(boolean hit) {
this.hit = hit;
}
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
}

View file

@ -2,6 +2,7 @@ package ru.erius.weblab3.data;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Model;
import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@ -9,24 +10,63 @@ import java.util.List;
@Model
@ApplicationScoped
public class HitInfoBean implements Serializable {
private final EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("hit-info-unit");
private List<HitInfo> hitInfos = new ArrayList<>();
private HitInfo latest = new HitInfo();
private HitInfo plotLatest = new HitInfo();
public void plotAdd() {
plotLatest.hitCheck();
HitInfo toAdd = new HitInfo(plotLatest.getX(), plotLatest.getY(), plotLatest.getR(), plotLatest.isHit());
hitInfos.add(toAdd);
public HitInfoBean() {
load();
}
public void add() {
public void plotAdd() {
plotLatest.hitCheck();
HitInfo hitInfo = new HitInfo(plotLatest.getX(), plotLatest.getY(), plotLatest.getR(), plotLatest.isHit());
add(hitInfo);
}
public void btnAdd() {
latest.hitCheck();
HitInfo toAdd = new HitInfo(latest.getX(), latest.getY(), latest.getR(), latest.isHit());
hitInfos.add(toAdd);
HitInfo hitInfo = new HitInfo(latest.getX(), latest.getY(), latest.getR(), latest.isHit());
add(hitInfo);
}
private void add(HitInfo hitInfo) {
hitInfos.add(hitInfo);
EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
entityManager.getTransaction().begin();
entityManager.persist(hitInfo);
entityManager.getTransaction().commit();
} finally {
entityManager.close();
}
}
private void load() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
entityManager.getTransaction().begin();
TypedQuery<HitInfo> loadAllQuery = entityManager.createNamedQuery("load", HitInfo.class);
hitInfos = loadAllQuery.getResultList();
entityManager.getTransaction().commit();
} finally {
entityManager.close();
}
}
public void clear() {
hitInfos.clear();
EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
entityManager.getTransaction().begin();
Query clearAllQuery = entityManager.createNamedQuery("clear");
clearAllQuery.executeUpdate();
entityManager.getTransaction().commit();
} finally {
entityManager.close();
}
latest = new HitInfo();
}

View file

@ -3,7 +3,9 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
version="3.0">
<persistence-unit name="default">
<persistence-unit name="hit-info-unit" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:/PostgresDS</jta-data-source>
<class>ru.erius.weblab3.data.HitInfo</class>
</persistence-unit>
</persistence>

View file

@ -4,12 +4,10 @@
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url"/>
<property name="connection.driver_class"/>
<!-- <property name="connection.username"/> -->
<!-- <property name="connection.password"/> -->
<!-- DB schema will be updated if needed -->
<!-- <property name="hibernate.hbm2ddl.auto">update</property> -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.jndi.url">java:/PostgresDS</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="ru.erius.weblab3.data.HitInfo"/>
</session-factory>
</hibernate-configuration>

View file

@ -3,5 +3,17 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<navigation-rule>
<from-view-id>/check_hit.xhtml</from-view-id>
<navigation-case>
<from-outcome>index</from-outcome>
<to-view-id>/index.xhtml</to-view-id>
<redirect/>
</navigation-case>
<navigation-case>
<from-outcome>check_hit</from-outcome>
<to-view-id>/check_hit.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
</faces-config>

View file

@ -43,7 +43,7 @@
<f:selectItem itemValue="4" itemLabel="4"/>
<f:selectItem itemValue="5" itemLabel="5"/>
</h:selectOneRadio><br/>
<h:commandButton id="submitBtn" value="Проверить" action="#{hitInfoBean.add()}">
<h:commandButton id="submitBtn" value="Проверить" action="#{hitInfoBean.btnAdd()}">
<f:ajax execute="inputForm" render="results"/>
</h:commandButton>
<h:commandButton value="Очистить" action="#{hitInfoBean.clear()}">