Team:Freiburg software/Code/DatastoreManager.java

From 2009.igem.org

 /*
     Copyright: synbiowave
     
     License: GPL
     
     Authors: Jörg Walossek
     
     Version: 0.1 
    
     DESCRIPTION:
    	This class provides all methods needed for import and export data to an internal database.
 */

 package org.synbiowave.database;

 import java.util.List;

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

 // TODO: Auto-generated Javadoc
 /**
  * The Class DatastoreManager.
  */
 public class DatastoreManager {
	
	/** The Constant pmfInstance. */
	private static final PersistenceManagerFactory pmfInstance=JDOHelper.getPersistenceManagerFactory("transactions-optional");

	/**
	 * Instantiates a new datastore manager.
	 */
	public DatastoreManager(){}
	
	/**
	 * Store.
	 * 
	 * @param o the o
	 * 
	 * @return the t
	 * 
	 * @throws Exception the exception
	 */
	public static <T> T store(T o) throws Exception{
	    PersistenceManager pm=pmfInstance.getPersistenceManager();
	    Transaction tx=pm.currentTransaction();
	    try{
	        tx.begin();
	        pm.makePersistent(o);
	        tx.commit();}
	    finally{
	        if(tx.isActive()){
	            tx.rollback();
	            throw new Exception("Error storing object, sequence rollbacked.");}
	        pm.close();}
	    return o;}
    
	/**
	 * Gets the all.
	 * 
	 * @param o the o
	 * 
	 * @return the all
	 * 
	 * @throws Exception the exception
	 */
	@SuppressWarnings("unchecked")
	public static <T> List<T> getAll(T o) throws Exception{ // add ordered params
    	List<T> results=null;
    	PersistenceManager pm=pmfInstance.getPersistenceManager();
    	Transaction tx=pm.currentTransaction();
    	try{
    	    tx.begin();
    	    Query q = pm.newQuery(o.getClass());
    	    results = (List<T>) q.execute();
    	    tx.commit();}
	    finally{
	        if(tx.isActive()){
	            tx.rollback();
	            throw new Exception("Error getting all objects, sequence rollbacked.");}
	        pm.close();}
	    return results;}
    
    /**
     * Gets the by id.
     * 
     * @param o the o
     * @param qID the q id
     * 
     * @return the by id
     * 
     * @throws Exception the exception
     */
    @SuppressWarnings("unchecked")
	public static <T> T getByID(T o,Long qID) throws Exception{
    	List<T> results=null;
    	PersistenceManager pm=pmfInstance.getPersistenceManager();
    	Transaction tx=pm.currentTransaction();
    	try{
    	    tx.begin();
    	    Query q = pm.newQuery(o.getClass(),"id == :qID");
    	    results = (List<T>) q.execute(qID);
    	    tx.commit();}
	    finally{
	        if(tx.isActive()){
	            tx.rollback();
	            throw new Exception("Error getting object by ID, sequence rollbacked.");}
	        pm.close();}
	    if(!results.isEmpty()){return results.get(0);}
	    else{return null;}}
    
    /**
     * Gets the by key value.
     * 
     * @param o the o
     * @param key the key
     * @param value the value
     * 
     * @return the by key value
     * 
     * @throws Exception the exception
     */
    @SuppressWarnings("unchecked")
	public static <T> List<T> getByKeyValue(T o,String key,String value) throws Exception{ // add ordered params
    	List<T> results=null;
    	PersistenceManager pm=pmfInstance.getPersistenceManager();
    	Transaction tx=pm.currentTransaction();
    	try{
    	    tx.begin();
    	    Query q = pm.newQuery(o.getClass(),key+" == :qName");
    	    results = (List<T>) q.execute(value);
    	    tx.commit();}
	    finally{
	        if(tx.isActive()){
	            tx.rollback();
	            throw new Exception("Error getting object by key/value, sequence rollbacked.");}
	        pm.close();}
	    return results;}
    
    /**
     * Gets the greater than key value.
     * 
     * @param o the o
     * @param key the key
     * @param value the value
     * 
     * @return the greater than key value
     * 
     * @throws Exception the exception
     */
    @SuppressWarnings("unchecked")
	public static <T> List<T> getGreaterThanKeyValue(T o,String key,String value) throws Exception{ // add ordered params
    	List<T> results=null;
    	PersistenceManager pm=pmfInstance.getPersistenceManager();
    	Transaction tx=pm.currentTransaction();
    	try{
    	    tx.begin();
    	    Query q = pm.newQuery(o.getClass(),key+" > :qName");
    	    results = (List<T>) q.execute(value);
    	    tx.commit();}
	    finally{
	        if(tx.isActive()){
	            tx.rollback();
	            throw new Exception("Error getting object greater than value, sequence rollbacked.");}
	        pm.close();}
	    return results;}
    
    /**
     * Delete.
     * 
     * @param o the o
     * 
     * @throws Exception the exception
     */
    public static void delete(Object o) throws Exception{
    	PersistenceManager pm=pmfInstance.getPersistenceManager();
    	Transaction tx=pm.currentTransaction();
    	try{
    	    tx.begin();
    	    Object ps=(Object) pm.getObjectById(o);
    	    pm.deletePersistent(ps);
    	    tx.commit();}
	    finally{
	        if(tx.isActive()){
	            tx.rollback();
	            throw new Exception("Error deleting object, sequence rollbacked.");}
	        pm.close();}}
 }