<commit_before>package org.sagebionetworks.repo.model.gaejdo;

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

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

import org.sagebionetworks.repo.model.BaseDAO;
import org.sagebionetworks.repo.model.DatastoreException;
import org.sagebionetworks.repo.model.InvalidModelException;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

/**
 * 
 * @author bhoff
 *
 * @param <S> the DTO class
 * @param <T> the JDO class
 */
abstract public class GAEJDOBaseDAOHelper<S,T extends GAEJDOBase> {
	
	abstract public S newDTO();
	
	abstract public T newJDO();
	
	/**
	 * Do a shallow copy from the JDO object to the DTO object.
	 * 
	 * @param jdo
	 * @param dto
	 */
	abstract public void copyToDto(T jdo, S dto);
	
	
	/**
	 * Do a shallow copy from the DTO object to the JDO object.
	 * 
	 * @param dto
	 * @param jdo
	 * @throws InvalidModelException 
	 */
	abstract public void copyFromDto(S dto, T jdo) throws InvalidModelException;
	
	/**
	 * @param jdoClass the class parameterized by T
	 */
	abstract public Class<T> getJdoClass();

	
	public String create(S dto) throws InvalidModelException {
		T jdo = newJDO();
		copyFromDto(dto, jdo);
		PersistenceManager pm = PMF.get();		
				Transaction tx=null;
		try {
			 	tx=pm.currentTransaction();
				tx.begin();
				pm.makePersistent(jdo);
				tx.commit();
				return KeyFactory.keyToString(jdo.getId());
		} finally {
				if(tx.isActive()) {
					tx.rollback();
				}
				pm.close();
		}	
	}
	
	public S get(String id) throws DatastoreException {
		PersistenceManager pm = PMF.get();	
		try {
			Key key = KeyFactory.stringToKey(id);
			@SuppressWarnings("unchecked")
			T jdo = (T)pm.getObjectById(getJdoClass(), key);
			S dto = newDTO();
			copyToDto(jdo, dto);
			pm.close();
			return dto;
		} catch (Exception e) {
			throw new DatastoreException(e);
		}
	}
	
	public void delete(String id) throws DatastoreException {
		PersistenceManager pm = PMF.get();		
		Transaction tx=null;
		try {
			 	tx=pm.currentTransaction();
				tx.begin();
				Key key = KeyFactory.stringToKey(id);
				@SuppressWarnings("unchecked")
				T jdo = (T)pm.getObjectById(key);
				pm.deletePersistent(jdo);
				tx.commit();
		} catch (Exception e) {
			throw new DatastoreException(e);
		} finally {
				if(tx.isActive()) {
					tx.rollback();
				}
				pm.close();
		}	
	}
	

	/**
	 * 
	 * @param start
	 * @param end
	 * @return a subset of the results, starting at index 'start' and not going beyond index 'end'
	 */
	public List<S> getInRange(int start, int end) {
		PersistenceManager pm = PMF.get();		
		try {
			Query query = pm.newQuery(getJdoClass());<commit_msg>Bug fix for call to getObjectById<commit_after>package org.sagebionetworks.repo.model.gaejdo;

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

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

import org.sagebionetworks.repo.model.BaseDAO;
import org.sagebionetworks.repo.model.DatastoreException;
import org.sagebionetworks.repo.model.InvalidModelException;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

/**
 * 
 * @author bhoff
 *
 * @param <S> the DTO class
 * @param <T> the JDO class
 */
abstract public class GAEJDOBaseDAOHelper<S,T extends GAEJDOBase> {
	
	abstract public S newDTO();
	
	abstract public T newJDO();
	
	/**
	 * Do a shallow copy from the JDO object to the DTO object.
	 * 
	 * @param jdo
	 * @param dto
	 */
	abstract public void copyToDto(T jdo, S dto);
	
	
	/**
	 * Do a shallow copy from the DTO object to the JDO object.
	 * 
	 * @param dto
	 * @param jdo
	 * @throws InvalidModelException 
	 */
	abstract public void copyFromDto(S dto, T jdo) throws InvalidModelException;
	
	/**
	 * @param jdoClass the class parameterized by T
	 */
	abstract public Class<T> getJdoClass();

	
	public String create(S dto) throws InvalidModelException {
		T jdo = newJDO();
		copyFromDto(dto, jdo);
		PersistenceManager pm = PMF.get();		
				Transaction tx=null;
		try {
			 	tx=pm.currentTransaction();
				tx.begin();
				pm.makePersistent(jdo);
				tx.commit();
				return KeyFactory.keyToString(jdo.getId());
		} finally {
				if(tx.isActive()) {
					tx.rollback();
				}
				pm.close();
		}	
	}
	
	public S get(String id) throws DatastoreException {
		PersistenceManager pm = PMF.get();	
		try {
			Key key = KeyFactory.stringToKey(id);
			@SuppressWarnings("unchecked")
			T jdo = (T)pm.getObjectById(getJdoClass(), key);
			S dto = newDTO();
			copyToDto(jdo, dto);
			pm.close();
			return dto;
		} catch (Exception e) {
			throw new DatastoreException(e);
		}
	}
	
	public void delete(String id) throws DatastoreException {
		PersistenceManager pm = PMF.get();		
		Transaction tx=null;
		try {
			 	tx=pm.currentTransaction();
				tx.begin();
				Key key = KeyFactory.stringToKey(id);
				@SuppressWarnings("unchecked")
				T jdo = (T)pm.getObjectById(getJdoClass(), key);
				pm.deletePersistent(jdo);
				tx.commit();
		} catch (Exception e) {
			throw new DatastoreException(e);
		} finally {
				if(tx.isActive()) {
					tx.rollback();
				}
				pm.close();
		}	
	}
	

	/**
	 * 
	 * @param start
	 * @param end
	 * @return a subset of the results, starting at index 'start' and not going beyond index 'end'
	 */
	public List<S> getInRange(int start, int end) {
		PersistenceManager pm = PMF.get();		
		try {
			Query query = pm.newQuery(getJdoClass());
{'fraction_non_alphanumeric': 0.1247084155750942, 'fraction_numerical': 0.0, 'mean_word_length': 3.392434988179669, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3321584', 'n_tokens_mistral': 2210, 'n_tokens_neox': 1934, 'n_words': 477}