Package de.paulwein.notes.dao.impl

Source Code of de.paulwein.notes.dao.impl.NotesDAOJDOImpl

package de.paulwein.notes.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;

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 JDO
* @author Paul
*
*/
public class NotesDAOJDOImpl implements NotesDAO {

  PersistenceManagerFactory mPmf = null;
 
  public NotesDAOJDOImpl(){
    mPmf = PMF.get();
  }
 
  @Override
  public String getSupportedEntity() {
    return Note.class.getName();
  }

  @Override
  public Key createNotesList(String userId, String name) throws DAOException {
    PersistenceManager pm = mPmf.getPersistenceManager();

    NoteList notesList = new NoteList();
    notesList.setName(name);
    notesList.setUserId(userId);
   
    try{
      notesList = pm.makePersistent(notesList);
    } finally{
      pm.close();
    }
   
    return notesList.getKey();
  }

  @Override
  public void deleteNotesList(NoteList notesList) throws DAOException {
    PersistenceManager pm = mPmf.getPersistenceManager();
    try {
      pm.currentTransaction().begin();

      notesList = pm.getObjectById(NoteList.class, notesList.getKey());
      pm.deletePersistent(notesList);

      pm.currentTransaction().commit();
    } catch (Exception ex) {
      pm.currentTransaction().rollback();
      throw new DAOException();
    } finally {
      pm.close();
    }
  }

  @Override
  public void updateNotesList(NoteList notesList) throws DAOException {
    PersistenceManager pm = mPmf.getPersistenceManager();
    String name = notesList.getName();
    String userId = notesList.getUserId();
    List<Note> notes = notesList.getNotes();

    try {
      pm.currentTransaction().begin();
     
      notesList = pm.getObjectById(NoteList.class, notesList.getKey());
      notesList.setName(name);
      notesList.setUserId(userId);
      notesList.setNotes(notes);
      pm.makePersistent(notesList);
     
      pm.currentTransaction().commit();
    } catch (Exception ex) {
      pm.currentTransaction().rollback();
      throw new DAOException();
    } finally {
      pm.close();
    }
  }

  @Override
  public void addNote(Key listId, Note note) throws DAOException {
    PersistenceManager pm = mPmf.getPersistenceManager();

    try {
      pm.currentTransaction().begin();
     
      NoteList notesList = pm.getObjectById(NoteList.class, listId);
      List<Note> notes = notesList.getNotes();     
      if(notes == null){
        notes = new ArrayList<Note>();
      }
      notes.add(note);
      notesList.setNotes(notes);
      pm.makePersistent(notesList)
     
      pm.currentTransaction().commit();
    } catch (Exception ex) {
      pm.currentTransaction().rollback();
      ex.printStackTrace();
      throw new DAOException();
    } finally {
      pm.close();
    }
  }

  @Override
  public void deleteNote(Note note) throws DAOException {
    PersistenceManager pm = mPmf.getPersistenceManager();
    Key parentKey = note.getKey().getParent();
    try{
      pm.currentTransaction().begin();

      NoteList notesList = pm.getObjectById(NoteList.class, parentKey);
      note = pm.getObjectById(Note.class, note.getKey());
      notesList.getNotes().remove(note);
      pm.makePersistent(notesList);

      pm.currentTransaction().commit();
    }catch (Exception ex) {
      pm.currentTransaction().rollback();
      throw new DAOException();
    } finally {
      pm.close();
    }
  }

  @Override
  public void updateNote(Note note) throws DAOException {
    PersistenceManager pm = mPmf.getPersistenceManager();
    Key parentKey = note.getKey().getParent();
    try{
      pm.currentTransaction().begin();

      NoteList notesList = pm.getObjectById(NoteList.class, parentKey);
      notesList.getNotes().remove(note);
      notesList.getNotes().add(note); // TODO maybe findObjectById(note.getKey) before add/remove
      pm.makePersistent(notesList);

      pm.currentTransaction().commit();
    }catch (Exception ex) {
      pm.currentTransaction().rollback();
      throw new DAOException();
    } finally {
      pm.close();
    }
  }

  @SuppressWarnings("unchecked")
  @Override
  public List<NoteList> fetchNotesLists(String userId) throws DAOException {
    PersistenceManager pm = mPmf.getPersistenceManager();
    Query q = pm.newQuery(NoteList.class,"userId == userIdParam");
    q.declareParameters("String userIdParam");
    List<NoteList> results = (List<NoteList>) q.execute(userId);
    return results;
  }

  @Override
  public NoteList loadNotesList(Key key) throws DAOException {
    PersistenceManager pm = mPmf.getPersistenceManager();
    NoteList notesList = null;
    try {
      notesList = pm.getObjectById(NoteList.class, key);
      notesList.getNotes(); // lazyfetch
    } catch (Exception ex) {
      throw new DAOException();
    } finally {
      pm.close()
    }
    return notesList;
  }

  @Override
  public Note loadNote(Key key) throws DAOException {
    PersistenceManager pm = mPmf.getPersistenceManager();
    Note note = null;
    try {
      note = pm.getObjectById(Note.class, key);
    } catch (Exception ex) {
      ex.printStackTrace();
      throw new DAOException();
    } finally {
      pm.close();
    }
    return note;
  }

  @Override
  public List<Note> search(String search, String userId) throws DAOException {
    PersistenceManager pm = mPmf.getPersistenceManager();
    Query q = pm.newQuery(Note.class,"note.startsWith(\"" + search + "\") && userId == keyParam");
    q.declareParameters("String keyParam");
    @SuppressWarnings("unchecked")
    List<Note> results = (List<Note>) q.execute(userId);
    return results;
  }

}
TOP

Related Classes of de.paulwein.notes.dao.impl.NotesDAOJDOImpl

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.