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

org.opencastproject.db.Queries Maven / Gradle / Ivy

There is a newer version: 16.6
Show newest version
/*
 * Licensed to The Apereo Foundation under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 *
 * The Apereo Foundation licenses this file to you under the Educational
 * Community 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://opensource.org/licenses/ecl2.txt
 *
 * 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.opencastproject.db;

import org.apache.commons.lang3.tuple.Pair;
import org.joda.time.base.AbstractInstant;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;

/**
 * Helper class defining common query functions that can be used with DBSession query execution methods.
 */
public final class Queries {
  private Queries() {
  }

  /**
   * Execute a typed named query.
   */
  public static final TypedQueriesBase namedQuery = new TypedQueriesBase() {
    @Override
    protected Query createQuery(EntityManager em, String queryName, Object... params) {
      return configureQuery(em.createNamedQuery(queryName), params);
    }

    @Override
    protected  TypedQuery createTypedQuery(EntityManager em, String queryName, Class clazz, Object... params) {
      return configureQuery(em.createNamedQuery(queryName, clazz), params);
    }
  };

  /**
   * Execute a native SQL query.
   */
  public static final QueriesBase nativeQuery = new QueriesBase() {
    @Override
    protected Query createQuery(EntityManager em, String sql, Object... params) {
      return configureQuery(em.createNativeQuery(sql), params);
    }
  };

  public abstract static class TypedQueriesBase extends QueriesBase {
    /**
     * Find entity by its id.
     *
     * @param clazz Entity class.
     * @param id ID of the entity.
     * @return The entity or null if not found.
     * @param  Entity type.
     */
    public  Function findById(Class clazz, Object id) {
      return em -> em.find(clazz, id);
    }

    /**
     * Find entity by its id.
     *
     * @param clazz Entity class.
     * @param id ID of the entity.
     * @return An Optional with the entity or an empty Optional if not found.
     * @param  Entity type.
     */
    public  Function> findByIdOpt(Class clazz, Object id) {
      return em -> {
        try {
          T e = em.find(clazz, id);
          if (e == null) {
            return Optional.empty();
          }
          return Optional.of(e);
        } catch (NoResultException e) {
          return Optional.empty();
        }
      };
    }

    /**
     * Execute a named query and return a single result.
     *
     * @param q Name of the query.
     * @param clazz Entity class.
     * @param params Parameters passed to the query.
     * @return The entity. An exception is thrown if not found.
     * @param  Entity type.
     */
    public  Function find(String q, Class clazz, Object... params) {
      return em -> createTypedQuery(em, q, clazz, params).getSingleResult();
    }

    /**
     * Execute a named query and return a single result.
     *
     * @param q Name of the query.
     * @param clazz Entity class.
     * @param params Parameters passed to the query.
     * @return An Optional with the entity or an empty Optional if not found.
     * @param  Entity type.
     */
    public  Function> findOpt(String q, Class clazz, Object... params) {
      return em -> {
        try {
          return Optional.of(createTypedQuery(em, q, clazz, params).getSingleResult());
        } catch (NoResultException | NonUniqueResultException e) {
          return Optional.empty();
        }
      };
    }

    /**
     * Execute a named query and return all results.
     *
     * @param q Name of the query.
     * @param clazz Entity class.
     * @param params Parameters passed to the query.
     * @return A list of entities.
     * @param  Entity type.
     */
    public  Function> findAll(String q, Class clazz, Object... params) {
      return em -> createTypedQuery(em, q, clazz, params).getResultList();
    }

    /**
     * Execute a named query and return a subset of the results.
     *
     * @param q Name of the query.
     * @param firstResult The index of the first result returned.  Gets passed into the JPA internals.
     * @param maxResults How many results to return
     * @param clazz Entity class.
     * @param params Parameters passed to the query.
     * @return A list of entities.
     * @param  Entity type.
     */
    public  Function> findSome(String q, int firstResult, int maxResults, Class clazz, Object... params) {
      return em -> createTypedQuery(em, q, clazz, params)
          .setFirstResult(firstResult)
          .setMaxResults(maxResults)
          .getResultList();
    }

    protected abstract  TypedQuery createTypedQuery(EntityManager em, String queryName, Class clazz,
        Object... params);

    protected  TypedQuery configureQuery(TypedQuery q, Object... params) {
      return (TypedQuery) configureQuery((Query) q, params);
    }
  }

  public abstract static class QueriesBase {
    /**
     * Execute a named query and return a single result.
     *
     * @param q Name of the query.
     * @param params Parameters passed to the query.
     * @return The entity. An exception is thrown if not found.
     */
    public Function find(String q, Object... params) {
      return em -> createQuery(em, q, params).getSingleResult();
    }

    /**
     * Execute a named query and return a single result.
     *
     * @param q Name of the query.
     * @param params Parameters passed to the query.
     * @return An Optional with the entity or an empty Optional if not found.
     */
    public Function> findOpt(String q, Object... params) {
      return em -> {
        try {
          return Optional.of(createQuery(em, q, params).getSingleResult());
        } catch (NoResultException | NonUniqueResultException e) {
          return Optional.empty();
        }
      };
    }

    /**
     * Execute a named query and return all results.
     *
     * @param q Name of the query.
     * @param params Parameters passed to the query.
     * @return A list of entities.
     */
    public Function findAll(String q, Object... params) {
      return em -> createQuery(em, q, params).getResultList();
    }

    /**
     * Execute a named update query.
     *
     * @param q Name of the query.
     * @param params Parameters passed to the query.
     * @return The number of updated entities.
     */
    public Function update(String q, Object... params) {
      return em -> createQuery(em, q, params).executeUpdate();
    }

    /**
     * Execute a named delete query.
     *
     * @param q Name of the query.
     * @param params Parameters passed to the query.
     * @return The number of deleted entities.
     */
    public Function delete(String q, Object... params) {
      return em -> createQuery(em, q, params).executeUpdate();
    }

    /**
     * Create or update passed entity.
     *
     * @param entity Entity to create or update.
     * @return Created or updated entity.
     * @param  Entity type.
     */
    public  Function persistOrUpdate(final E entity) {
      return em -> {
        final Object id = em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier(entity);
        if (id == null) {
          em.persist(entity);
          return entity;
        } else {
          @SuppressWarnings("unchecked")
          final E dto = (E) em.find(entity.getClass(), id);
          if (dto == null) {
            em.persist(entity);
            return entity;
          } else {
            return em.merge(entity);
          }
        }
      };
    }

    /**
     * Delete passed entity.
     *
     * @param entity Entity to delete.
     * @return Deleted entity.
     * @param  Entity type.
     */
    public  Consumer remove(final E entity) {
      return em -> {
        em.remove(entity);
      };
    }

    /**
     * Create passed entity.
     *
     * @param entity Entity to create.
     * @return Created entity.
     * @param  Entity type.
     */
    public  Function persist(final E entity) {
      return em -> {
        em.persist(entity);
        return entity;
      };
    }

    /**
     * Create passed entity.
     *
     * @param entity Entity to create.
     * @return Optional with the created entity.
     * @param  Entity type.
     */
    public  Function> persistOpt(final E entity) {
      return em -> {
        em.persist(entity);
        return Optional.of(entity);
      };
    }

    protected abstract Query createQuery(EntityManager em, String q, Object... params);

    protected Query configureQuery(Query q, Object... params) {
      for (int i = 0; i < params.length; i++) {
        Object p = params[i];

        if (p instanceof Pair) { // named parameters
          Pair pair = (Pair) p;
          String key = pair.getKey();
          Object value = pair.getValue();
          if (value instanceof Date) {
            q.setParameter(key, (Date) value, TemporalType.TIMESTAMP);
          } else if (value instanceof Calendar) {
            q.setParameter(key, (Calendar) value, TemporalType.TIMESTAMP);
          } else if (value instanceof AbstractInstant) {
            q.setParameter(key, ((AbstractInstant) value).toDate(), TemporalType.TIMESTAMP);
          } else {
            q.setParameter(key, value);
          }
        } else { // positional parameters
          if (p instanceof Date) {
            q.setParameter(i + 1, (Date) p, TemporalType.TIMESTAMP);
          } else if (p instanceof AbstractInstant) {
            q.setParameter(i + 1, ((AbstractInstant) p).toDate(), TemporalType.TIMESTAMP);
          } else {
            q.setParameter(i + 1, p);
          }
        }
      }

      return q;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy