package com.google.gwt.sample.contacts.server;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import com.google.gwt.sample.contacts.client.ContactsService;
import com.google.gwt.sample.contacts.shared.Address;
import com.google.gwt.sample.contacts.shared.Contact;
import com.google.gwt.sample.contacts.shared.ContactDetails;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
@SuppressWarnings("serial")
public class ContactsServiceImpl extends RemoteServiceServlet implements ContactsService {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("Contacts");
private final HashMap<String, Contact> contacts = new HashMap<String, Contact>();
public ContactsServiceImpl() {
initContacts();
}
@Override
public Contact addContact(Contact contact) {
EntityManager em = entityManagerFactory.createEntityManager();
ContactEntity contactEntity = new ContactEntity(contact.getFirstName(), contact.getLastName(), contact.getEmailAddress());
em.getTransaction().begin();
em.persist(contactEntity);
em.getTransaction().commit();
// contact.setId(String.valueOf(contacts.size()));
contact.setId(contactEntity.getId());
contacts.put(contact.getId(), contact);
return contact;
}
@Override
public Boolean deleteContact(String id) {
EntityManager em = entityManagerFactory.createEntityManager();
ContactEntity entity = em.find(ContactEntity.class, id);
em.getTransaction().begin();
em.remove(entity);
em.getTransaction().commit();
contacts.remove(id);
return true;
}
@Override
public ArrayList<ContactDetails> deleteContacts(ArrayList<String> ids) {
for (int i = 0; i < ids.size(); ++i) {
deleteContact(ids.get(i));
}
return getContactDetails();
}
@Override
public Address getAddress(String contactId) {
EntityManager em = entityManagerFactory.createEntityManager();
AddressEntity entity = em.find(AddressEntity.class, contactId);
Address a = new Address();
a.setContactId(contactId);
if (entity != null) {
a.setStreet(entity.getStreet());
a.setCity(entity.getCity());
a.setZip(entity.getZip());
}
return a;
}
@Override
public Contact getContact(String id) {
return contacts.get(id);
}
@Override
public ArrayList<ContactDetails> getContactDetails() {
ArrayList<ContactDetails> contactDetails = new ArrayList<ContactDetails>();
Iterator<String> it = contacts.keySet().iterator();
while (it.hasNext()) {
Contact contact = contacts.get(it.next());
contactDetails.add(contact.getLightWeightContact());
}
return contactDetails;
}
private void initContacts() {
EntityManager em = entityManagerFactory.createEntityManager();
TypedQuery<ContactEntity> query = em.createQuery("select j from ContactEntity j", ContactEntity.class);
List<ContactEntity> contactEntities = query.getResultList();
for (ContactEntity contactEntity : contactEntities) {
Contact contact = new Contact(contactEntity.getId(), contactEntity.getFirstName(), contactEntity.getLastName(),
contactEntity.getEmailAddress());
contacts.put(contactEntity.getId(), contact);
}
}
@Override
public Address updateAddress(Address address) {
EntityManager em = entityManagerFactory.createEntityManager();
AddressEntity entity = em.find(AddressEntity.class, address.getContactId());
boolean insert = false;
if (entity == null) {
insert = true;
entity = new AddressEntity(address.getContactId());
}
entity.setContactId(address.getContactId());
entity.setStreet(address.getStreet());
entity.setCity(address.getCity());
entity.setZip(address.getZip());
em.getTransaction().begin();
if (insert) {
em.persist(entity);
} else {
em.merge(entity);
}
em.getTransaction().commit();
return address;
}
@Override
public Contact updateContact(Contact contact) {
EntityManager em = entityManagerFactory.createEntityManager();
ContactEntity entity;
if (contact.getId() == null) {
entity = new ContactEntity();
} else {
entity = em.find(ContactEntity.class, contact.getId());
}
entity.setFirstName(contact.getFirstName());
entity.setLastName(contact.getLastName());
entity.setEmailAddress(contact.getEmailAddress());
em.getTransaction().begin();
if (contact.getId() == null) {
em.persist(entity);
// contact.setId(String.valueOf(contacts.size()));
contact.setId(entity.getId());
} else {
em.merge(entity);
}
em.getTransaction().commit();
contacts.remove(contact.getId());
contacts.put(contact.getId(), contact);
return contact;
}
}