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

org.smallmind.persistence.orm.jpa.JPADao Maven / Gradle / Ivy

/*
 * Copyright (c) 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 David Berkman
 * 
 * This file is part of the SmallMind Code Project.
 * 
 * The SmallMind Code Project is free software, you can redistribute
 * it and/or modify it under either, at your discretion...
 * 
 * 1) The terms of GNU Affero General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * ...or...
 * 
 * 2) The terms of the Apache License, Version 2.0.
 * 
 * The SmallMind Code Project is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License or Apache License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * and the Apache License along with the SmallMind Code Project. If not, see
 *  or .
 * 
 * Additional permission under the GNU Affero GPL version 3 section 7
 * ------------------------------------------------------------------
 * If you modify this Program, or any covered work, by linking or
 * combining it with other code, such other code is not for that reason
 * alone subject to any of the requirements of the GNU Affero GPL
 * version 3.
 */
package org.smallmind.persistence.orm.jpa;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import org.smallmind.persistence.Durable;
import org.smallmind.persistence.UpdateMode;
import org.smallmind.persistence.cache.VectoredDao;
import org.smallmind.persistence.orm.ORMDao;

public abstract class JPADao, D extends Durable> extends ORMDao {

  public JPADao (JPAProxySession proxySession) {

    this(proxySession, null);
  }

  public JPADao (JPAProxySession proxySession, VectoredDao vectoredDao) {

    super(proxySession, vectoredDao);
  }

  public D get (Class durableClass, I id) {

    VectoredDao vectoredDao;
    D durable;

    if ((vectoredDao = getVectoredDao()) == null) {
      if ((durable = acquire(durableClass, id)) != null) {

        return durable;
      }
    }
    else {
      if ((durable = vectoredDao.get(durableClass, id)) != null) {

        return durable;
      }

      if ((durable = acquire(durableClass, id)) != null) {

        return vectoredDao.persist(durableClass, durable, UpdateMode.SOFT);
      }
    }

    return null;
  }

  @Override
  public D acquire (Class durableClass, I id) {

    return durableClass.cast(getSession().getNativeSession().find(durableClass, id));
  }

  public D persist (Class durableClass, D durable) {

    D persistentDurable;
    VectoredDao vectoredDao = getVectoredDao();

    if (getSession().getNativeSession().contains(durable)) {
      persistentDurable = durable;
    }
    else {
      persistentDurable = getManagedClass().cast(getSession().getNativeSession().merge(durable));
      getSession().flush();
    }

    if (vectoredDao != null) {

      return vectoredDao.persist(durableClass, persistentDurable, UpdateMode.HARD);
    }

    return persistentDurable;
  }

  public void delete (Class durableClass, D durable) {

    VectoredDao vectoredDao = getVectoredDao();

    if (!getSession().getNativeSession().contains(durable)) {
      getSession().getNativeSession().remove(getSession().getNativeSession().find(durable.getClass(), durable.getId()));
    }
    else {
      getSession().getNativeSession().remove(durable);
    }

    getSession().flush();

    if (vectoredDao != null) {
      vectoredDao.delete(durableClass, durable);
    }
  }

  public List list () {

    return listByQuery(new QueryDetails() {

      @Override
      public String getQueryString () {

        return "select entity from " + getManagedClass().getSimpleName() + " entity";
      }

      @Override
      public Query completeQuery (Query query) {

        return query;
      }
    });
  }

  public List list (final int fetchSize) {

    return listByQuery(new QueryDetails() {

      @Override
      public String getQueryString () {

        return "select entity from " + getManagedClass().getSimpleName() + " entity";
      }

      @Override
      public Query completeQuery (Query query) {

        return query.setMaxResults(fetchSize);
      }
    });
  }

  public List list (final I greaterThan, final int fetchSize) {

    return listByQuery(new QueryDetails() {

      @Override
      public String getQueryString () {

        return "select entity from " + getManagedClass().getSimpleName() + " entity where entity.id > ?";
      }

      @Override
      public Query completeQuery (Query query) {

        return query.setParameter("id", greaterThan).setMaxResults(fetchSize);
      }
    });
  }

  public Iterable scroll () {

    throw new UnsupportedOperationException();
  }

  public Iterable scroll (int fetchSize) {

    throw new UnsupportedOperationException();
  }

  public Iterable scrollById (I greaterThan, int fetchSize) {

    throw new UnsupportedOperationException();
  }

  public long size () {

    throw new UnsupportedOperationException();
  }

  public D detach (D object) {

    throw new UnsupportedOperationException("JPA has no explicit detached state");
  }

  public int executeWithQuery (QueryDetails queryDetails) {

    return constructQuery(queryDetails).executeUpdate();
  }

  public  T findByQuery (Class returnType, QueryDetails queryDetails) {

    return returnType.cast(constructQuery(queryDetails).getSingleResult());
  }

  public D findByQuery (QueryDetails queryDetails) {

    return getManagedClass().cast(constructQuery(queryDetails).getSingleResult());
  }

  public  List listByQuery (Class returnType, QueryDetails queryDetails) {

    return Collections.checkedList(constructQuery(queryDetails).getResultList(), returnType);
  }

  public List listByQuery (QueryDetails queryDetails) {

    return Collections.checkedList(constructQuery(queryDetails).getResultList(), getManagedClass());
  }

  public Query constructQuery (QueryDetails queryDetails) {

    return queryDetails.completeQuery(getSession().getNativeSession().createQuery(queryDetails.getQueryString()));
  }
}