diff --git a/src/io/github/norbipeti/chat/server/Main.java b/src/io/github/norbipeti/chat/server/Main.java index 1ddddb4..711da38 100644 --- a/src/io/github/norbipeti/chat/server/Main.java +++ b/src/io/github/norbipeti/chat/server/Main.java @@ -12,7 +12,8 @@ import org.reflections.util.ConfigurationBuilder; import com.sun.net.httpserver.HttpServer; -import io.github.norbipeti.chat.server.db.DataProvider; +import io.github.norbipeti.chat.server.db.*; +import io.github.norbipeti.chat.server.db.domain.*; import io.github.norbipeti.chat.server.page.*; public class Main { @@ -25,7 +26,11 @@ public class Main { // https://docs.oracle.com/javase/8/docs/api/ System.out.println("Loading database..."); try (DataProvider provider = new DataProvider()) { - + User user = new User(); + provider.addUser(user); + System.out.println(provider.getUsers()); + provider.removeUser(user); + System.out.println(provider.getUsers()); } System.out.println("Starting webserver..."); HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLocalHost(), 8080), 10); diff --git a/src/io/github/norbipeti/chat/server/db/DataProvider.java b/src/io/github/norbipeti/chat/server/db/DataProvider.java index 761c89f..bbaffdf 100644 --- a/src/io/github/norbipeti/chat/server/db/DataProvider.java +++ b/src/io/github/norbipeti/chat/server/db/DataProvider.java @@ -1,13 +1,48 @@ package io.github.norbipeti.chat.server.db; +import java.util.List; + +import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; +import javax.persistence.TypedQuery; + +import io.github.norbipeti.chat.server.db.domain.*; public class DataProvider implements AutoCloseable { private EntityManagerFactory emf; public DataProvider() { - Persistence.createEntityManagerFactory("ChatServerPU"); + emf = Persistence.createEntityManagerFactory("ChatServerPU"); + } + + public void addUser(User user) { + save(user); + } + + private void save(Object object) { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + em.persist(object); + em.getTransaction().commit(); + em.close(); + } + + public List getUsers() { + EntityManager em = emf.createEntityManager(); + TypedQuery query = em.createQuery("SELECT u FROM User u", User.class); + List users = query.getResultList(); + em.close(); + return users; + } + + public void removeUser(User user) { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + User managedUser = em.find(User.class, user.getId()); + em.remove(managedUser); + em.getTransaction().commit(); + em.close(); } @Override diff --git a/src/io/github/norbipeti/chat/server/db/domain/Contact.java b/src/io/github/norbipeti/chat/server/db/domain/Contact.java new file mode 100644 index 0000000..ce754af --- /dev/null +++ b/src/io/github/norbipeti/chat/server/db/domain/Contact.java @@ -0,0 +1,38 @@ +package io.github.norbipeti.chat.server.db.domain; + +import javax.persistence.*; + +@Entity +public class Contact { + @ManyToOne + private User user1; + @ManyToOne + private User user2; + + public User getUser1() { + return user1; + } + + public void setUser1(User user1) { + this.user1 = user1; + } + + public User getUser2() { + return user2; + } + + public void setUser2(User user2) { + this.user2 = user2; + } + + @Id + private Long getId() { + if (user1 == null) + return null; + return user1.getId(); + } + + public Contact() { + + } +} diff --git a/src/io/github/norbipeti/chat/server/db/domain/User.java b/src/io/github/norbipeti/chat/server/db/domain/User.java new file mode 100644 index 0000000..81d9205 --- /dev/null +++ b/src/io/github/norbipeti/chat/server/db/domain/User.java @@ -0,0 +1,56 @@ +package io.github.norbipeti.chat.server.db.domain; + +import javax.persistence.*; + +@Entity +@Table(name = "\"User\"") +public class User { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", unique = true, nullable = false) + private Long id; + private String name; + private String email; + private String password; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + @Override + public String toString() { + return "User [id=" + id + ", name=" + name + ", email=" + email + ", password=" + password + "]"; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public User() { + + } +} diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml index a7a89c1..e2963d1 100644 --- a/src/main/resources/META-INF/persistence.xml +++ b/src/main/resources/META-INF/persistence.xml @@ -5,7 +5,8 @@ - + \ No newline at end of file