All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.yadaframework.persistence.repository.YadaClauseDao Maven / Gradle / Ivy

There is a newer version: 0.7.7.R4
Show newest version
package net.yadaframework.persistence.repository;


import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import net.yadaframework.persistence.entity.YadaClause;

/**
 * 
 */
@Repository
@Transactional(readOnly = true) 
public class YadaClauseDao {
	private final transient Logger log = LoggerFactory.getLogger(getClass());

    @PersistenceContext
	EntityManager em;
    
	public List findByName(String name) {
		String sql = "select yc from YadaClause yc where yc.name = :name";
		List resultList = em.createQuery(sql, YadaClause.class)
			.setParameter("name", name)
			.getResultList();
		return resultList;
	}

	/**
	 * Ritorno la clausola con nome 'trattamentoDati' avente versione più alta
	 */
	// TODO: this must be moved to the application because it's not generic
	public YadaClause getTrattamentoDati() {
		String sql = "select * from YadaClause yc where yc.name='trattamentoDati' order by yc.clauseVersion desc limit 1";
		List resultList = em.createNativeQuery(sql, YadaClause.class).getResultList();
		return normaliseSingleResult(resultList);
	}

	/**
	 * Ritorno la clausola con nome 'pubblicazioneSito' avente versione più alta
	 */
	// TODO: this must be moved to the application because it's not generic
	public YadaClause getPubblicazioneSito() {
		String sql = "select * from YadaClause yc where yc.name='pubblicazioneSito' order by yc.clauseVersion desc limit 1";
		List resultList = em.createNativeQuery(sql, YadaClause.class).getResultList();
		return normaliseSingleResult(resultList);
	}
	
	// TODO: this must be moved to the application because it's not generic
	public YadaClause getPubblicazioneRaccolta() {
		String sql = "select * from YadaClause yc where yc.name='pubblicazioneRaccolta' order by yc.clauseVersion desc limit 1";
		List resultList = em.createNativeQuery(sql, YadaClause.class).getResultList();
		return normaliseSingleResult(resultList);
	}

    /**
     * For backwards compatibility, returns null when no result is found
     * @param resultList
     * @return
     */
    private YadaClause normaliseSingleResult(List resultList) {
		// Need to keep the contract of the Spring Data Repository, so we return null when no value found.
		if (resultList.isEmpty()) {
			return null;
		} else {
			return resultList.get(0);
		}
    }
    
    public long count() {
    	return em.createQuery("select count(*) from YadaClause", Long.class).getSingleResult().longValue();
    }

	@Transactional(readOnly = false)
	public YadaClause save(YadaClause entity) {
		if (entity==null) {
			return null;
		}
		if (entity.getId()==null) {
			em.persist(entity);
			return entity;
		}
		return em.merge(entity);
	}

	@Transactional(readOnly = false)
	public void saveAll(List yadaClauses) {
		for (YadaClause yadaClause : yadaClauses) {
			save(yadaClause);
		}
	}
    

	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy