⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hibernatesession.java

📁 一个java工作流引擎
💻 JAVA
字号:
package org.jbpm.persistence.hibernate;

import java.sql.Timestamp;
import java.util.*;
import net.sf.hibernate.*;
import net.sf.hibernate.type.*;
import org.apache.commons.logging.*;
import org.jbpm.*;
import org.jbpm.model.definition.impl.*;
import org.jbpm.persistence.*;

public class HibernateSession implements PersistenceSession {

  protected Session session = null;

  public HibernateSession(Session session) {
    this.session = session;
  }

  public HibernateSession(SessionFactory sessionFactory) {
    try {
      this.session = sessionFactory.openSession();
    } catch (HibernateException e) {
      e.printStackTrace();
      throw new PersistenceException("couldn't open a hibernate database connection", e);
    }
  }

  public Object load(Class clazz, Long id) {
    Object object = null;
    try {
      object = session.load(clazz, id);
    } catch (HibernateException e) {
      throw new PersistenceException("couldn't load " + clazz.getName() + "(" + id + ") from the database", e);
    }
    return object;
  }
  
  public void lock(Object o) {
    try {
      session.lock(o, LockMode.UPGRADE);
    } catch (HibernateException e) {
      throw new PersistenceException("couldn't lock " + o + " in the database", e);
    }
  }

  public void save(Object o) {
    try {
      if (o instanceof Collection) {
        Iterator iter = ((Collection) o).iterator();
        while (iter.hasNext()) {
          save(iter.next());
        }
      } else {
        session.save(o);
      }
    } catch (HibernateException e) {
      throw new PersistenceException("couldn't save " + o + " in the database", e);
    }
  }
  
  public void delete(Object o) {
    try {
      session.delete( o );
    } catch (HibernateException e) {
      throw new PersistenceException("couldn't delete" + o + " in the database", e);
    }
  }

  private static final String allDefinitionsQuery =
    "select d from d in class org.jbpm.model.definition.impl.DefinitionImpl";
  public List findAllDefinitions() {
    List definitions = null;
    try {
      definitions = session.find(allDefinitionsQuery);
    } catch (HibernateException e) {
      throw new PersistenceException("hibernate exception while searching the db for all the definitions", e);
    }
    return definitions;
  }
  
  // TODO : introduce a jbpm config property to switch off the use of the nested subquery
  //      and replace it with a loop over all definitions with that name.
  //      because MySQL versions under 4.1 do not support subqueries.
  //      See also the SubQueryTest
  private static final String latestDefinitionQuery =
    "select d "
      + "from d in class org.jbpm.model.definition.impl.DefinitionImpl "
      + "where d.name = ? "
      + "  and d.version = ( "
      + "    select max(d2.version) "
      + "    from d2 in class org.jbpm.model.definition.impl.DefinitionImpl "
      + "    where d2.name = d.name )";

  public DefinitionImpl findLatestDefinition(String name) {
    DefinitionImpl definition = null;
    try {
      List l = session.find(latestDefinitionQuery, name, Hibernate.STRING);
      if (l.size() > 0) {
        definition = (DefinitionImpl) l.get(0);
      }
    } catch (HibernateException e) {
      throw new PersistenceException("hibernate exception while searching the db for the latest definition of process " + name, e);
    }
    return definition;
  }

  private static final String findTokensByActor =
    "select t "
    + "from t in class org.jbpm.model.execution.impl.TokenImpl "
    + "where t.actorId = ?";
  public Collection findTokensByActor(String actorId) {
    List tokens = null;
    try {
      tokens = session.find(findTokensByActor, actorId, Hibernate.STRING);
    } catch (HibernateException e) {
      throw new PersistenceException("hibernate exception while finding tokens by swimlane", e);
    }
    return tokens;
  }

  private static final String findUndoableInvocationLogs =
    "select il " + 
    "from il in class org.jbpm.model.log.impl.InvocationLogImpl " + 
    "where il.token.processInstance.id = ? " +
    "  and il.date >= ? " +
    "  and il.isUndoable = true " +
    "order by il.date desc ";
  public List findUndoableInvocationLogs(Long processInstanceId, Date date) {
    List logs = null;
    try {
      Object[] args = new Object[]{processInstanceId, new Timestamp(date.getTime())};
      Type[] types = new Type[]{Hibernate.LONG, Hibernate.TIMESTAMP};
      logs = session.find(findUndoableInvocationLogs, args, types);
    } catch (HibernateException e) {
      throw new PersistenceException("hibernate exception while finding undoable logs", e);
    }
    return logs;
	}

  private static final String findJobsToDo =
    "select j " + 
    "from j in class org.jbpm.model.scheduler.impl.JobImpl " + 
    "order by j.dueDate ";
  public Iterator findJobsToDo() {
    Iterator jobIter = null;
    try {
      jobIter = session.iterate(findJobsToDo);
    } catch (HibernateException e) {
      throw new PersistenceException("hibernate exception while finding jobs to do", e);
    }
    return jobIter;
  }
  
  private static final String findJobsByReference =
    "select j " + 
    "from j in class org.jbpm.model.scheduler.impl.JobImpl " + 
    "where j.reference = ? ";
  public List findJobsByReference( String reference ) {
    List jobs = null;
    try {
      Object[] args = new Object[]{reference};
      Type[] types = new Type[]{Hibernate.STRING};
      jobs = session.find(findJobsByReference, args, types);
    } catch (HibernateException e) {
      throw new PersistenceException("hibernate exception while finding jobs by reference", e);
    }
    return jobs;
  }

  private static final String findFile =
    "select f " + 
    "from f in class org.jbpm.model.definition.impl.FileImpl " + 
    "where f.definitionId = ? " +
    "  and f.name = ? ";
  public FileImpl findFile(Long definitionId, String fileName) {
    FileImpl file = null;
    try {
      Object[] args = new Object[]{definitionId, fileName};
      Type[] types = new Type[]{Hibernate.LONG, Hibernate.STRING};
      List files = session.find(findFile, args, types);
      
      if ( files != null  ) {
        if ( files.size() == 1 ) {
          file = (FileImpl) files.get( 0 );
        } else if ( files.size() > 1 ) {
          throw new PersistenceException( "file '" + fileName + "' is stored more then once in the database for definition '" + definitionId + "'" );
        }
      }
    } catch (HibernateException e) {
      throw new PersistenceException("hibernate exception while finding file '" + fileName + "' in definition '" + definitionId + "'", e);
    }
    return file;
  }
  
  public void close() {
    try {
      session.close();
      session = null;
    } catch (HibernateException e) {
      e.printStackTrace();
      throw new PersistenceException("jbpm couldn't close a hibernate database connection", e);
    }
  }
  
  private static Log log = LogFactory.getLog(HibernateSession.class);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -