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

org.superbiz.moviefun.MoviesImpl Maven / Gradle / Ivy

There is a newer version: 4.13.5
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.superbiz.moviefun;

import javax.ejb.Stateless;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.EntityType;
import java.util.List;

@Stateless(name = "Movies")
@WebService(portName = "MoviesPort", serviceName = "MoviesWebService", targetNamespace = "http://superbiz.org/wsdl")
public class MoviesImpl implements Movies, MoviesRemote {

    @PersistenceContext(unitName = "movie-unit")
    private EntityManager entityManager;

    @Override
    public Movie find(Long id) {
        return entityManager.find(Movie.class, id);
    }

    @Override
    public void clean() {
        entityManager.createQuery("delete from Movie").executeUpdate();
    }

    @Override
    public void addMovie(Movie movie) {
        entityManager.persist(movie);
    }

    @Override
    public void editMovie(Movie movie) {
        entityManager.merge(movie);
    }

    @Override
    public void deleteMovie(Movie movie) {
        entityManager.remove(movie);
    }

    @Override
    public void deleteMovieId(long id) {
        Movie movie = entityManager.find(Movie.class, id);
        deleteMovie(movie);
    }

    @Override
    public List getMovies() {
        CriteriaQuery cq = entityManager.getCriteriaBuilder().createQuery(Movie.class);
        cq.select(cq.from(Movie.class));
        return entityManager.createQuery(cq).getResultList();
    }

    @Override
    public List findByTitle(String title) {
        return findByStringField("title", title);
    }

    @Override
    public List findByGenre(String genre) {
        return findByStringField("genre", genre);
    }

    @Override
    public List findByDirector(String director) {
        return findByStringField("director", director);
    }

    private List findByStringField(String fieldname, String param) {
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery query = builder.createQuery(Movie.class);
        Root root = query.from(Movie.class);
        EntityType type = entityManager.getMetamodel().entity(Movie.class);

        Path path = root.get(type.getDeclaredSingularAttribute(fieldname, String.class));
        Predicate condition = builder.like(path, "%" + param + "%");

        query.where(condition);

        return entityManager.createQuery(query).getResultList();
    }

    @Override
    public List findRange(int[] range) {
        CriteriaQuery cq = entityManager.getCriteriaBuilder().createQuery(Movie.class);
        cq.select(cq.from(Movie.class));
        TypedQuery q = entityManager.createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    @Override
    public int count() {
        CriteriaQuery cq = entityManager.getCriteriaBuilder().createQuery(Long.class);
        Root rt = cq.from(Movie.class);
        cq.select(entityManager.getCriteriaBuilder().count(rt));
        TypedQuery q = entityManager.createQuery(cq);
        return (q.getSingleResult()).intValue();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy