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

📄 hibernateworkflowstore.java

📁 osworkflow修改版本
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) 2002-2003 by OpenSymphony * All rights reserved. */package com.opensymphony.workflow.spi.hibernate;import com.opensymphony.module.propertyset.PropertySet;import com.opensymphony.module.propertyset.PropertySetManager;import com.opensymphony.module.propertyset.hibernate.DefaultHibernateConfigurationProvider;import com.opensymphony.util.TextUtils;import com.opensymphony.workflow.StoreException;import com.opensymphony.workflow.query.*;import com.opensymphony.workflow.spi.Step;import com.opensymphony.workflow.spi.WorkflowEntry;import com.opensymphony.workflow.spi.WorkflowStore;import net.sf.hibernate.Criteria;import net.sf.hibernate.Hibernate;import net.sf.hibernate.HibernateException;import net.sf.hibernate.Session;import net.sf.hibernate.SessionFactory;import net.sf.hibernate.Transaction;import net.sf.hibernate.expression.Criterion;import net.sf.hibernate.expression.Expression;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import java.util.*;/** * A workflow store backed by Hibernate for persistence.  To use this with the standard * persistence factory, pass to the DefaultConfiguration.persistenceArgs the SessionFactory to * use: * <code>DefaultConfiguration.persistenceArgs.put("sessionFactory", DatabaseHelper.getSessionFactory());</code> * See the HibernateFunctionalWorkflowTestCase for more help. * * @author $Author: hani $ * @version $Revision: 1.18 $ */public class HibernateWorkflowStore implements WorkflowStore {    //~ Static fields/initializers /////////////////////////////////////////////    private static final Log log = LogFactory.getLog(HibernateWorkflowStore.class);    //~ Instance fields ////////////////////////////////////////////////////////    Session session;    SessionFactory sessionFactory;    //~ Constructors ///////////////////////////////////////////////////////////    public HibernateWorkflowStore() {    }    public HibernateWorkflowStore(SessionFactory sessionFactory) throws StoreException {        this.sessionFactory = sessionFactory;        try {            this.session = sessionFactory.openSession();        } catch (HibernateException he) {            log.error("constructor", he);            throw new StoreException("constructor", he);        }    }    //~ Methods ////////////////////////////////////////////////////////////////    public void setEntryState(long entryId, int state) throws StoreException {        try {            HibernateWorkflowEntry entry = (HibernateWorkflowEntry) session.find("FROM entry IN CLASS " + HibernateWorkflowEntry.class.getName() + " WHERE entry.id = ?", new Long(entryId), Hibernate.LONG).get(0);            entry.setState(state);            session.save(entry);        } catch (HibernateException e) {            log.error("An exception occured", e);            return;        }    }    public PropertySet getPropertySet(long entryId) {        HashMap args = new HashMap();        args.put("entityName", "OSWorkflowEntry");        args.put("entityId", new Long(entryId));        DefaultHibernateConfigurationProvider configurationProvider = new DefaultHibernateConfigurationProvider();        configurationProvider.setSessionFactory(sessionFactory);        args.put("configurationProvider", configurationProvider);        return PropertySetManager.getInstance("hibernate", args);    }    public Step createCurrentStep(long entryId, int stepId, String owner, Date startDate, Date dueDate, String status, long[] previousIds) throws StoreException {        HibernateCurrentStep step = new HibernateCurrentStep();        HibernateWorkflowEntry entry;        Transaction tx;        try {            tx = session.beginTransaction();            entry = (HibernateWorkflowEntry) session.find("FROM entry in CLASS " + HibernateWorkflowEntry.class.getName() + " WHERE entry.id = ?", new Long(entryId), Hibernate.LONG).get(0);        } catch (HibernateException he) {            log.error("Looking for workflow entry " + entryId, he);            throw new StoreException("Looking for workflow entry " + entryId, he);        }        step.setEntry(entry);        step.setStepId(stepId);        step.setOwner(owner);        step.setStartDate(startDate);        step.setDueDate(dueDate);        step.setStatus(status);        List stepIdList = new ArrayList(previousIds.length);        for (int i = 0; i < previousIds.length; i++) {            long previousId = previousIds[i];            stepIdList.add(new Long(previousId));        }        if (!stepIdList.isEmpty()) {            String stepIds = TextUtils.join(", ", stepIdList);            try {                step.setPreviousSteps(session.find("FROM step in CLASS " + HibernateCurrentStep.class.getName() + " WHERE step.id IN (" + stepIds + ")"));            } catch (HibernateException he) {                log.error("Looking for step in " + stepIds, he);                throw new StoreException("Looking for step in " + stepIds, he);            }        } else {            step.setPreviousSteps(Collections.EMPTY_LIST);        }        if (entry.getCurrentSteps() == null) {            ArrayList cSteps = new ArrayList(1);            cSteps.add(step);            entry.setCurrentSteps(cSteps);        } else {            entry.getCurrentSteps().add(step);        }        try {            session.save(entry);            tx.commit();            //session.save(step);            return step;        } catch (HibernateException he) {            log.error("Saving new workflow entry", he);            throw new StoreException("Saving new workflow entry", he);        }    }    public WorkflowEntry createEntry(String workflowName) throws StoreException {        HibernateWorkflowEntry entry = new HibernateWorkflowEntry();        entry.setState(WorkflowEntry.CREATED);        entry.setWorkflowName(workflowName);        Transaction tx;        try {            tx = session.beginTransaction();            session.save(entry);            tx.commit();        } catch (HibernateException he) {            log.error("Saving new workflow entry", he);            throw new StoreException("Saving new workflow entry", he);        }        return entry;    }    public List findCurrentSteps(long entryId) throws StoreException {        HibernateWorkflowEntry entry;        try {            entry = (HibernateWorkflowEntry) session.find("FROM entry in CLASS " + HibernateWorkflowEntry.class.getName() + " WHERE entry.id = ?", new Long(entryId), Hibernate.LONG).get(0);        } catch (HibernateException he) {            log.error("Looking for entryId " + entryId, he);            throw new StoreException("Looking for entryId " + entryId, he);        }        try {            return session.find("FROM step IN CLASS " + HibernateCurrentStep.class.getName() + " WHERE step.entry = ?", entry, Hibernate.entity(entry.getClass()));        } catch (HibernateException he) {            log.error("Looking for step id" + entry, he);            throw new StoreException("Looking for step id" + entry, he);        }    }    public WorkflowEntry findEntry(long entryId) throws StoreException {        try {            List result = session.find("FROM entry IN CLASS " + HibernateWorkflowEntry.class.getName() + " WHERE entry.id = ?", new Long(entryId), Hibernate.LONG);            return (WorkflowEntry) result.get(0);        } catch (HibernateException he) {            log.error("Looking for entry " + entryId, he);            throw new StoreException("Loooking for entry " + entryId, he);        }    }    public List findHistorySteps(long entryId) throws StoreException {        HibernateWorkflowEntry entry;        try {            entry = (HibernateWorkflowEntry) session.find("FROM entry in CLASS " + HibernateWorkflowEntry.class.getName() + " WHERE entry.id = ?", new Long(entryId), Hibernate.LONG).get(0);        } catch (HibernateException he) {            log.error("Finding entry " + entryId, he);            throw new StoreException("Finding entry " + entryId, he);        }        try {            return session.find("FROM step IN CLASS " + HibernateHistoryStep.class.getName() + " WHERE step.entry = ?", entry, Hibernate.entity(entry.getClass()));        } catch (HibernateException he) {            log.error("Looking for step with entry " + entry, he);            throw new StoreException("Looking for step with entry " + entry, he);        }    }    public void init(Map props) throws StoreException {        try {            //if(1==2){            sessionFactory = (SessionFactory) props.get("sessionFactory");            session = sessionFactory.openSession();            //}        } catch (HibernateException he) {            log.error("Setting sessionFactory", he);            throw new StoreException("Setting sessionFactory", he);        }    }    public Step markFinished(Step step, int actionId, Date finishDate, String status, String caller) throws StoreException {        HibernateCurrentStep currentStep = (HibernateCurrentStep) step;        currentStep.setActionId(actionId);        currentStep.setFinishDate(finishDate);        currentStep.setStatus(status);        currentStep.setCaller(caller);        try {            Transaction tx = session.beginTransaction();            session.save(currentStep);            tx.commit();            return currentStep;        } catch (HibernateException he) {            log.error("Saving current step with action " + actionId, he);            throw new StoreException("Saving current step with action " + actionId, he);        }    }    public void moveToHistory(Step step) throws StoreException {        HibernateWorkflowEntry entry;        Transaction tx;        try {            tx = session.beginTransaction();            entry = (HibernateWorkflowEntry) session.find("FROM entry IN CLASS " + HibernateWorkflowEntry.class.getName() + " WHERE entry.id = ?", new Long(step.getEntryId()), Hibernate.LONG).get(0);        } catch (HibernateException he) {            log.error("Looking for workflow entry " + step.getEntryId(), he);            throw new StoreException("Looking for workflow entry " + step.getEntryId(), he);        }        HibernateHistoryStep hStep = new HibernateHistoryStep((HibernateStep) step);        entry.getCurrentSteps().remove(step);        if (entry.getHistorySteps() == null) {            ArrayList hSteps = new ArrayList(1);            hSteps.add(hStep);            entry.setHistorySteps(hSteps);        } else {            entry.getHistorySteps().add(hStep);        }        try {            session.save(hStep);            session.save(entry);            tx.commit();            //session.delete(step);            //session.save(hStep, new Long(hStep.getId()));        } catch (HibernateException he) {            log.error("Saving workflow entry " + entry.getId(), he);

⌨️ 快捷键说明

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