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

org.smallmind.persistence.orm.jdo.JDODao 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.jdo;

import java.io.Serializable;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import org.smallmind.nutsnbolts.util.IterableIterator;
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 JDODao, D extends Durable> extends ORMDao {

  public JDODao (JDOProxySession proxySession) {

    this(proxySession, null);
  }

  public JDODao (JDOProxySession 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().getObjectById(durableClass, id));
  }

  public List list () {

    LinkedList instanceList;
    Iterator instanceIter;

    instanceIter = getSession().getNativeSession().getExtent(getManagedClass()).iterator();
    instanceList = new LinkedList();
    while (instanceIter.hasNext()) {
      instanceList.add(getManagedClass().cast(instanceIter.next()));
    }

    return instanceList;
  }

  public Iterable scroll () {

    return new IterableIterator(getSession().getNativeSession().getExtent(getManagedClass()).iterator());
  }

  public D detach (D object) {

    return getManagedClass().cast(getSession().getNativeSession().detachCopy(object));
  }

  public D persist (Class durableClass, D durable) {

    D persistentDurable;
    VectoredDao vectoredDao = getVectoredDao();

    persistentDurable = durableClass.cast(getSession().getNativeSession().makePersistent(durable));

    if (vectoredDao != null) {

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

    return persistentDurable;
  }

  public void delete (Class durableClass, D durable) {

    VectoredDao vectoredDao = getVectoredDao();

    getSession().getNativeSession().deletePersistent(durable);

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

  public D find (QueryDetails queryDetails) {

    Query query;

    query = constructQuery(queryDetails);
    query.setUnique(true);

    return getManagedClass().cast(query.executeWithMap(queryDetails.getParameterMap()));
  }

  public List list (QueryDetails queryDetails) {

    return Collections.checkedList((List)constructQuery(queryDetails).executeWithMap(queryDetails.getParameterMap()), getManagedClass());
  }

  private Query constructQuery (QueryDetails queryDetails) {

    Query query;
    Class[] importClasses;

    query = getSession().getNativeSession().newQuery(queryDetails.getQuery());
    query.setIgnoreCache(queryDetails.getIgnoreCache());

    if ((importClasses = queryDetails.getImports()) != null) {
      if (importClasses.length > 0) {

        StringBuilder importBuilder;

        importBuilder = new StringBuilder("import ");
        for (int count = 0; count < importClasses.length; count++) {
          if (count > 0) {
            importBuilder.append("; ");
          }

          importBuilder.append(importClasses[count].getCanonicalName());
        }

        query.declareImports(importBuilder.toString());
      }
    }

    return query;
  }
}