package de.paulwein.notes.dao.impl;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import com.google.appengine.api.datastore.Key;
import de.paulwein.notes.dao.DAOException;
import de.paulwein.notes.dao.NotesDAO;
import de.paulwein.notes.pojo.Note;
import de.paulwein.notes.pojo.NoteList;
/**
* NotesDAO implementation with JPA
* @author Paul
*
*/
public class NotesDAOJPAImpl implements NotesDAO {
private EntityManagerFactory mEmf;
public NotesDAOJPAImpl(){
mEmf = EMF.get();
}
@Override
public String getSupportedEntity() {
return Note.class.getName();
}
@Override
public Key createNotesList(String userId, String name) throws DAOException {
EntityManager em = mEmf.createEntityManager();
NoteList notesList = new NoteList();
notesList.setName(name);
notesList.setUserId(userId);
try{
em.getTransaction().begin();
em.persist(notesList);
em.getTransaction().commit();
} finally {
em.close();
}
return notesList.getKey();
}
@Override
public void deleteNotesList(NoteList notesList) throws DAOException {
EntityManager em = mEmf.createEntityManager();
try{
em.getTransaction().begin();
notesList = em.find(NoteList.class, notesList.getKey());
em.remove(notesList);
em.getTransaction().commit();
} finally {
em.close();
}
}
@Override
public void updateNotesList(NoteList notesList) throws DAOException {
EntityManager em = mEmf.createEntityManager();
String name = notesList.getName();
String userId = notesList.getUserId();
List<Note> notes = notesList.getNotes();
try{
em.getTransaction().begin();
notesList = em.find(NoteList.class, notesList.getKey());
notesList.setName(name);
notesList.setUserId(userId);
notesList.setNotes(notes);
em.getTransaction().commit();
} finally {
em.close();
}
}
@Override
public void addNote(Key listId, Note note) throws DAOException {
EntityManager em = mEmf.createEntityManager();
try {
em.getTransaction().begin();
NoteList notesList = em.find(NoteList.class, listId);
List<Note> notes = notesList.getNotes();
if(notes == null){
notes = new ArrayList<Note>();
}
notes.add(note);
notesList.setNotes(notes);
em.getTransaction().commit();
} catch (Exception ex) {
em.getTransaction().rollback();
ex.printStackTrace();
throw new DAOException();
} finally {
em.close();
}
}
@Override
public void deleteNote(Note note) throws DAOException {
EntityManager em = mEmf.createEntityManager();
Key parentKey = note.getKey().getParent();
try{
em.getTransaction().begin();
NoteList notesList = em.find(NoteList.class, parentKey);
note = em.find(Note.class, note.getKey());
notesList.getNotes().remove(note);
em.getTransaction().commit();
}catch (Exception ex) {
em.getTransaction().rollback();
throw new DAOException();
} finally {
em.close();
}
}
@Override
public void updateNote(Note note) throws DAOException {
EntityManager em = mEmf.createEntityManager();
Key parentKey = note.getKey().getParent();
try{
em.getTransaction().begin();
NoteList notesList = em.find(NoteList.class, parentKey);
notesList.getNotes().remove(note);
notesList.getNotes().add(note);
em.getTransaction().commit();
}catch (Exception ex) {
em.getTransaction().rollback();
throw new DAOException();
} finally {
em.close();
}
}
@SuppressWarnings("unchecked")
@Override
public List<NoteList> fetchNotesLists(String userId) throws DAOException {
EntityManager em = mEmf.createEntityManager();
Query query = em.createQuery("select n from " + NoteList.class.getName() + " n where n.userId = :userId");
query.setParameter("userId", userId);
List<NoteList> notes = query.getResultList();
em.close();
return notes;
}
@Override
public NoteList loadNotesList(Key key) throws DAOException {
EntityManager em = mEmf.createEntityManager();
NoteList notesList = null;
try{
notesList = em.find(NoteList.class, key);
notesList.getNotes(); // lazy loading
em.detach(notesList);
} catch (Exception e) {
e.printStackTrace();
throw new DAOException();
} finally {
em.close();
}
return notesList;
}
@Override
public Note loadNote(Key key) throws DAOException {
EntityManager em = mEmf.createEntityManager();
Note note = null;
try{
note = em.find(Note.class, key);
} catch (Exception e) {
e.printStackTrace();
throw new DAOException();
} finally {
em.close();
}
return note;
}
@Override
public List<Note> search(String search, String userId) throws DAOException {
EntityManager em = mEmf.createEntityManager();
TypedQuery<Note> query = em.createQuery("SELECT n FROM " + Note.class.getName() + " n WHERE note LIKE '" + search + "%' AND userId = :id", Note.class);
query.setParameter("id", userId);
List<Note> results = (List<Note>) query.getResultList();
em.close();
return results;
}
}