<commit_before>package de.popts.verein.person.jpa;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import javax.persistence.EntityManager;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;

import org.amdatu.jta.ManagedTransactional;
import org.amdatu.jta.Transactional;
import org.apache.felix.dm.annotation.api.Component;
import org.apache.felix.dm.annotation.api.ServiceDependency;

import de.popts.verein.person.Person;
import de.popts.verein.person.PersonApi;
import de.popts.verein.person.PersonException;

@Transactional
@Component(provides = ManagedTransactional.class)
public class PersonApiJpaImpl implements PersonApi, ManagedTransactional {

	@ServiceDependency
	private volatile EntityManager em;
	
	@Override
	public Class<?>[] getManagedInterfaces() {
		return new Class[] {PersonApi.class};
	}

	@Override
	public Person personAnlegen(Person person) throws PersonException {
		// set oid
		if (person.getOid() == null) {
			person.setOid(UUID.randomUUID().toString());
		}
		
		// trivial implementation
		em.persist(JpaPerson.fromPerson(person));
		
		// return person
		return person;
	}

	@Override
	public void personLoeschen(Person person) throws PersonException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Person personAendern(Person person) throws PersonException {
		// trivial implementation
		em.persist(JpaPerson.fromPerson(person));
		
		// return person
		return person;
	}

	@Override
	public List<Person> listAll() {
		// execute query
		TypedQuery<JpaPerson> query = em.createQuery("select p from JpaPerson p", JpaPerson.class);
		
		// return result
		return query.getResultList().stream().map(JpaPerson::toPerson).collect(Collectors.toList());
	}

	@Override
	public List<Person> list4Jahr(int jahr) {
		// execute query
		TypedQuery<JpaPerson> query = em.createQuery("select p from JpaPerson p where (p.geburtsDatum >= :year1) and (p.geburtsDatum < :year2)", JpaPerson.class);
		
		// set params
		Calendar year1 = GregorianCalendar.getInstance();
		year1.clear();
		year1.set(jahr, Calendar.JANUARY, 1);
		
		Calendar year2 = GregorianCalendar.getInstance();
		year2.clear();
		year2.set(jahr+1, Calendar.JANUARY, 1);
		
		query.setParameter("year1", year1, TemporalType.DATE);
		query.setParameter("year2", year2, TemporalType.DATE);
		
		// return result
		return query.getResultList().stream().map(JpaPerson::toPerson).collect(Collectors.toList());
	}<commit_msg>OSGI-3 implemented list4Jahr() using SQL EXTRACT<commit_after>package de.popts.verein.person.jpa;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import javax.persistence.EntityManager;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;

import org.amdatu.jta.ManagedTransactional;
import org.amdatu.jta.Transactional;
import org.apache.felix.dm.annotation.api.Component;
import org.apache.felix.dm.annotation.api.ServiceDependency;

import de.popts.verein.person.Person;
import de.popts.verein.person.PersonApi;
import de.popts.verein.person.PersonException;

@Transactional
@Component(provides = ManagedTransactional.class)
public class PersonApiJpaImpl implements PersonApi, ManagedTransactional {

	@ServiceDependency
	private volatile EntityManager em;
	
	@Override
	public Class<?>[] getManagedInterfaces() {
		return new Class[] {PersonApi.class};
	}

	@Override
	public Person personAnlegen(Person person) throws PersonException {
		// set oid
		if (person.getOid() == null) {
			person.setOid(UUID.randomUUID().toString());
		}
		
		// trivial implementation
		em.persist(JpaPerson.fromPerson(person));
		
		// return person
		return person;
	}

	@Override
	public void personLoeschen(Person person) throws PersonException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Person personAendern(Person person) throws PersonException {
		// trivial implementation
		em.persist(JpaPerson.fromPerson(person));
		
		// return person
		return person;
	}

	@Override
	public List<Person> listAll() {
		// execute query
		TypedQuery<JpaPerson> query = em.createQuery("select p from JpaPerson p", JpaPerson.class);
		
		// return result
		return query.getResultList().stream().map(JpaPerson::toPerson).collect(Collectors.toList());
	}

	@Override
	public List<Person> list4Jahr(int jahr) {
		// execute query
		TypedQuery<JpaPerson> query = em.createQuery("select p from JpaPerson p WHERE SQL('EXTRACT (YEAR FROM ?)', p.geburtsDatum) = :year", JpaPerson.class);
		
		query.setParameter("year", jahr);
		
		// return result
		return query.getResultList().stream().map(JpaPerson::toPerson).collect(Collectors.toList());
	}
{'fraction_non_alphanumeric': 0.11724706367195549, 'fraction_numerical': 0.003915104059344735, 'mean_word_length': 4.919512195121952, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, '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': '3137764', 'n_tokens_mistral': 1634, 'n_tokens_neox': 1520, 'n_words': 317}