vente.service.ArticleServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for vente.service.ArticleServiceImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vente.service;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vente.entity.Article;
import vente.enumeration.TypeArticle;

/**
 *
 * @author ajc
 */
@Service
public class ArticleServiceImpl {
    @Autowired
    private ArticleService articleService;
    private EntityManager em = Persistence.createEntityManagerFactory("PU").createEntityManager();

    public List<Article> findByTypeArticle(TypeArticle typeArticle) {
        List<Article> articles = new ArrayList<>();
        if (typeArticle == TypeArticle.MIXTE) {
            TypeArticle enfant = TypeArticle.ENFANT;
            Query query = em.createQuery(
                    "SELECT a FROM Article a EXCEPT SELECT a1 FROM Article a1 WHERE a1.typeArticle = :enfant");
            query.setParameter("enfant", enfant);
            articles = query.getResultList();

        } else {
            Query query = em.createQuery("SELECT a FROM Article a WHERE a.typeArticle = :typeArticle");
            query.setParameter("typeArticle", typeArticle);
            articles = query.getResultList();
        }
        return articles;
    }
}