Commit d2dd4258 authored by Arne Styve's avatar Arne Styve
Browse files

Some minor adjustments prior to merging to develop

parent 853c31e9
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="CheckStyle-IDEA-Module">
<option name="configuration">
<map />
</option>
<component name="FacetManager">
<facet type="jpa" name="JPA">
<configuration>
<setting name="validation-enabled" value="true" />
<datasource-mapping>
<factory-entry name="contacts-pu" />
</datasource-mapping>
<naming-strategy-map />
<deploymentDescriptor name="persistence.xml" url="file://$MODULE_DIR$/src/META-INF/persistence.xml" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
......
......@@ -206,6 +206,11 @@ public class MainController {
return AddressBookFileHandler.loadFromFile(inFile);
}
/**
* Loads an entire AddressBook from a database.
*
* @return an address book populated by contact details loaded from the database.
*/
public AddressBook loadAddressBookFromDB() {
return new AddressBookDAO();
}
......
......@@ -64,6 +64,11 @@ public interface AddressBook extends Serializable, Iterable<ContactDetails> {
*/
Collection<ContactDetails> getAllContacts();
/**
* Close the data source.
*/
void close();
@Override
Iterator<ContactDetails> iterator();
}
package no.ntnu.idata2001.contacts.model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
public class AddressBookDAO implements AddressBook {
public AddressBookDAO() {
// Note the 'transient'-keyword. This is to indicate that these fields
// should never be serialized. Since the AddressBook-interface
// implements Serializable (used for object serialisation), this class
// to is tagged to be serializable. But the two fields in this class is used to
// communicate with the database, and should hence not be serialized. Neither are
// the classes EntityManager nor EntityManagerFactory serializable.
private final transient EntityManagerFactory efact;
private final transient EntityManager eman;
/**
* Creates an instance of the AddressbookDAO.
*/
public AddressBookDAO() {
this.efact = Persistence.createEntityManagerFactory("contacts-pu");
this.eman = efact.createEntityManager();
}
@Override
......@@ -30,61 +39,45 @@ public class AddressBookDAO implements AddressBook {
@Override
public void addContact(ContactDetails contact) {
EntityManagerFactory efact = Persistence.createEntityManagerFactory("contacts-pu");
EntityManager eman = efact.createEntityManager();
try {
eman.getTransaction().begin();
eman.persist(contact);
eman.getTransaction().commit();
} finally {
eman.close();
efact.close();
}
this.eman.getTransaction().begin();
this.eman.persist(contact);
this.eman.getTransaction().commit();
}
@Override
public void changeDetails(String oldKey, ContactDetails contact) {
//TODO: To be implemented later....
}
@Override
public int getNumberOfEntries() {
return 0;
return this.getAllContacts().size();
}
@Override
public void removeContact(String phoneNumber) {
//TODO: To be implemented later...
}
@Override
public Collection<ContactDetails> getAllContacts() {
EntityManagerFactory efact = Persistence.createEntityManagerFactory("contacts-pu");
EntityManager eman = efact.createEntityManager();
List<ContactDetails> contactsList = null;
try {
String sql = "SELECT c FROM ContactDetails c";
String sql = "SELECT c FROM ContactDetails c";
Query query = eman.createQuery(sql);
contactsList = query.getResultList();
} finally {
eman.close();
efact.close();
}
Query query = eman.createQuery(sql);
contactsList = query.getResultList();
return contactsList;
}
@Override
public void close() {
this.eman.close();
this.efact.close();
}
@Override
public Iterator<ContactDetails> iterator() {
return getAllContacts().iterator();
......
......@@ -76,6 +76,11 @@ public class AddressBookPlain implements AddressBook {
return this.book.values();
}
@Override
public void close() {
// Nothing needed to be done. Intentionally left empty.
}
@Override
public Iterator<ContactDetails> iterator() {
return this.book.values().iterator();
......
......@@ -46,8 +46,12 @@ public class ContactDetails implements Comparable<ContactDetails>, Serializable
this.address = address.trim();
}
/**
* Default constructor. Required according to the Java Beans standard,
* required by JPA.
*/
public ContactDetails() {
// Intentionally left empty.
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment