Package com.google.gwt.sample.contacts.server

Source Code of com.google.gwt.sample.contacts.server.ContactsServiceImpl

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.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 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 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;
  }
}
TOP

Related Classes of com.google.gwt.sample.contacts.server.ContactsServiceImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.