?? abstracthibernateworkflowstore.java
字號:
/* * Copyright (c) 2002-2003 by OpenSymphony * All rights reserved. */package com.opensymphony.workflow.spi.hibernate;import com.opensymphony.module.propertyset.PropertySet;import com.opensymphony.workflow.QueryNotSupportedException;import com.opensymphony.workflow.StoreException;import com.opensymphony.workflow.query.FieldExpression;import com.opensymphony.workflow.query.NestedExpression;import com.opensymphony.workflow.query.WorkflowExpressionQuery;import com.opensymphony.workflow.query.WorkflowQuery;import com.opensymphony.workflow.spi.Step;import com.opensymphony.workflow.spi.WorkflowEntry;import com.opensymphony.workflow.spi.WorkflowStore;import com.opensymphony.workflow.util.PropertySetDelegate;import net.sf.hibernate.Criteria;import net.sf.hibernate.HibernateException;import net.sf.hibernate.Session;import net.sf.hibernate.expression.Criterion;import net.sf.hibernate.expression.Expression;import java.util.ArrayList;import java.util.Collection;import java.util.Date;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Set;/** * @author Luca Masini * @since 2005-9-23 * */public abstract class AbstractHibernateWorkflowStore implements WorkflowStore { //~ Instance fields //////////////////////////////////////////////////////// private PropertySetDelegate propertySetDelegate; private String cacheRegion = null; private boolean cacheable = false; //~ Methods //////////////////////////////////////////////////////////////// // ~ Getter/Setter //////////////////////////////////////////////////////////////// public void setCacheRegion(String cacheRegion) { this.cacheRegion = cacheRegion; } public void setCacheable(boolean cacheable) { this.cacheable = cacheable; } public void setEntryState(final long entryId, final int state) throws StoreException { loadEntry(entryId).setState(state); } public PropertySet getPropertySet(long entryId) throws StoreException { if (getPropertySetDelegate() == null) { throw new StoreException("PropertySetDelegate is not properly configured"); } return getPropertySetDelegate().getPropertySet(entryId); } public void setPropertySetDelegate(PropertySetDelegate propertySetDelegate) { this.propertySetDelegate = propertySetDelegate; } public PropertySetDelegate getPropertySetDelegate() { return propertySetDelegate; } public Step createCurrentStep(final long entryId, final int stepId, final String owner, final Date startDate, final Date dueDate, final String status, final long[] previousIds) throws StoreException { final HibernateWorkflowEntry entry = loadEntry(entryId); final HibernateCurrentStep step = new HibernateCurrentStep(); step.setStepId(stepId); step.setOwner(owner); step.setStartDate(startDate); step.setDueDate(dueDate); step.setStatus(status); // This is for backward compatibility, but current Store doesn't // persist this collection, nor is such property visibile outside // OSWF internal classes List previousSteps = new ArrayList(previousIds.length); for (int i = 0; i < previousIds.length; i++) { HibernateCurrentStep previousStep = new HibernateCurrentStep(); previousSteps.add(previousStep); } step.setPreviousSteps(previousSteps); entry.addCurrentSteps(step); // We need to save here because we soon will need the stepId // that hibernate calculate on save or flush save(step); return step; } public WorkflowEntry createEntry(String workflowName) throws StoreException { final HibernateWorkflowEntry entry = new HibernateWorkflowEntry(); entry.setState(WorkflowEntry.CREATED); entry.setWorkflowName(workflowName); save(entry); return entry; } public List findCurrentSteps(final long entryId) throws StoreException { // We are asking for current step list, so here we have an anti-lazy // copy of the Hibernate array in memory. This also prevents problem // in case the use is going with a pattern that span a session // for method call return new ArrayList(loadEntry(entryId).getCurrentSteps()); } public WorkflowEntry findEntry(long entryId) throws StoreException { return loadEntry(entryId); } public List findHistorySteps(final long entryId) throws StoreException { // We are asking for current step list, so here we have an anti-lazy // copy of the Hibernate array in memory. This also prevents problem // in case the use is going with a pattern that span a session // for method call return new ArrayList(loadEntry(entryId).getHistorySteps()); } public Step markFinished(Step step, int actionId, Date finishDate, String status, String caller) throws StoreException { final HibernateCurrentStep currentStep = (HibernateCurrentStep) step; currentStep.setActionId(actionId); currentStep.setFinishDate(finishDate); currentStep.setStatus(status); currentStep.setCaller(caller); return currentStep; } public void moveToHistory(final Step step) throws StoreException { final HibernateCurrentStep currentStep = (HibernateCurrentStep) step; final HibernateWorkflowEntry entry = currentStep.getEntry(); final HibernateHistoryStep hStep = new HibernateHistoryStep(currentStep); entry.removeCurrentSteps(currentStep); delete(currentStep); entry.addHistorySteps(hStep); // We need to save here because we soon will need the stepId // that hibernate calculate on save or flush save(hStep); } public List query(final WorkflowQuery query) throws StoreException { return (List) execute(new InternalCallback() { public Object doInHibernate(Session session) throws HibernateException, StoreException { Class entityClass; int qtype = query.getType(); if (qtype == 0) { // then not set, so look in sub queries if (query.getLeft() != null) { qtype = query.getLeft().getType(); } } if (qtype == WorkflowQuery.CURRENT) { entityClass = HibernateCurrentStep.class; } else { entityClass = HibernateHistoryStep.class; } Criteria criteria = session.createCriteria(entityClass); Criterion expression = buildExpression(query); criteria.setCacheable(isCacheable()); if (isCacheable()) { criteria.setCacheRegion(getCacheRegion()); } criteria.add(expression); Set results = new HashSet(); Iterator iter = criteria.list().iterator(); while (iter.hasNext()) { HibernateStep step = (HibernateStep) iter.next(); results.add(new Long(step.getEntryId())); } return new ArrayList(results); } }); } /* * (non-Javadoc) * * @see com.opensymphony.workflow.spi.WorkflowStore#query(com.opensymphony.workflow.query.WorkflowExpressionQuery) */ public List query(final WorkflowExpressionQuery query) throws StoreException { return (List) execute(new InternalCallback() { public Object doInHibernate(Session session) throws HibernateException { com.opensymphony.workflow.query.Expression expression = query.getExpression(); Criterion expr; Class entityClass = getQueryClass(expression, null); if (expression.isNested()) { expr = buildNested((NestedExpression) expression); } else { expr = queryComparison((FieldExpression) expression); } Criteria criteria = session.createCriteria(entityClass); criteria.setCacheable(isCacheable()); if (isCacheable()) { criteria.setCacheRegion(getCacheRegion()); } criteria.add(expr); Set results = new HashSet(); Iterator iter = criteria.list().iterator(); while (iter.hasNext()) { Object next = iter.next(); Object item; if (next instanceof HibernateStep) { HibernateStep step = (HibernateStep) next; item = new Long(step.getEntryId()); } else { WorkflowEntry entry = (WorkflowEntry) next; item = new Long(entry.getId()); } results.add(item); } return new ArrayList(results);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -