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

📄 workflowserviceimpl.java

📁 采用tapestry的简单OA系统
💻 JAVA
字号:
package com.ejsun.entapps.service.impl.simpleoa;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ejsun.entapps.domain.organization.Group;
import com.ejsun.entapps.domain.simpleoa.Comment;
import com.ejsun.entapps.domain.simpleoa.Form;
import com.ejsun.entapps.domain.simpleoa.WorkflowDescriptor;
import com.ejsun.entapps.service.GenericServiceException;
import com.ejsun.entapps.service.impl.AbstractService;
import com.ejsun.entapps.service.organization.OrganizationService;
import com.ejsun.entapps.service.simpleoa.FormService;
import com.ejsun.entapps.service.simpleoa.WorkflowService;
import com.ejsun.entapps.util.CurrentUser;
import com.ejsun.entapps.util.workflow.Constants;
import com.opensymphony.workflow.Workflow;
import com.opensymphony.workflow.WorkflowException;
import com.opensymphony.workflow.basic.BasicWorkflow;
import com.opensymphony.workflow.config.Configuration;
import com.opensymphony.workflow.loader.ActionDescriptor;
import com.opensymphony.workflow.query.Expression;
import com.opensymphony.workflow.query.FieldExpression;
import com.opensymphony.workflow.query.NestedExpression;
import com.opensymphony.workflow.query.WorkflowExpressionQuery;
import com.opensymphony.workflow.spi.WorkflowEntry;

/**
 * @author	Quake Wang
 * @since	2004-5-9
 * @version $Revision: 1.1 $
 * 
 **/
public class WorkflowServiceImpl extends AbstractService implements WorkflowService {
	private static final Log log = LogFactory.getLog(WorkflowServiceImpl.class);
	private OrganizationService organizationService;
	private FormService formService;
	private Configuration workflowConfiguration;
	
    public Form[] getQueued() {
		return formService.findFormByWorkflowIds(queryWorkflowId("Queued"));
    }
    
	public Form[] getUnderway() {
		return formService.findFormByWorkflowIds(queryWorkflowId("Underway"));
	}
    
    private List queryWorkflowId(String status) {
        Expression expUserOwner =
        	new FieldExpression(
        		FieldExpression.OWNER,
        		FieldExpression.CURRENT_STEPS,
        		FieldExpression.EQUALS,
        		Constants.convertToUserOwner(CurrentUser.get().getId()));
        Expression expOwner = expUserOwner;
        
        Group group = organizationService.findGroupByMember(CurrentUser.get());
        if (group != null) {
        	Expression expGroupMember =
        		new FieldExpression(
        			FieldExpression.OWNER,
        			FieldExpression.CURRENT_STEPS,
        			FieldExpression.EQUALS,
        			Constants.convertToGroupOwner(group.getId()));
        
        	expOwner =
        		new NestedExpression(
        			new Expression[] { expUserOwner, expGroupMember },
        			NestedExpression.OR);
        }
        
        Expression expStatus =
        	new FieldExpression(
        		FieldExpression.STATUS,
        		FieldExpression.CURRENT_STEPS,
        		FieldExpression.EQUALS,
        		status);
        Expression query =
        	new NestedExpression(
        		new Expression[] { expOwner, expStatus },
        		NestedExpression.AND);
        List wfIdList = null;
        try {
            wfIdList = getWorkflow().query(new WorkflowExpressionQuery(query));
        } catch (WorkflowException e) {
        	log.error(e);
        	throw new GenericServiceException(e.getMessage());
        }
        return wfIdList;
    }

    /* (non-Javadoc)
     * @see com.ejsun.entapps.service.simpleoa.WorkflowService#getFinished()
     */
    public Form[] getFinished() {
        // TODO Auto-generated method stub
        return null;
    }

    public Form initializeWorkflow(WorkflowDescriptor workflowDescriptor) {
        Form form = formService.generateFormDraft(workflowDescriptor.getTemplate());
		try {
			//TODO for some special cases, may initialize with specified stepId and actionMap
			//TODO implement an IoC friendly WorkflowFactory, DO NOT hardcode name / file mapping here.
			String workflowName = getClass().getResource("/" +workflowDescriptor.getName() + ".xml").toString();			
            form.setWorkflowId(getWorkflow().initialize(workflowName, 0, null));
        } catch (WorkflowException e) {
            log.error(e);
			throw new GenericServiceException(e.getMessage());
        }
        formService.updateForm(form);
        return form;
    }

    public ActionDescriptor[] getAvailableActions(Form form) {
		Workflow wf = getWorkflow();
		int[] actions = wf.getAvailableActions(form.getWorkflowId(), null);
		//TODO godddddamn duplicate WorkflowDescriptor name.
		com.opensymphony.workflow.loader.WorkflowDescriptor wd = wf.getWorkflowDescriptor(wf.getWorkflowName(form.getWorkflowId()));

		ActionDescriptor[] result = new ActionDescriptor[actions.length];
		for (int i = 0; i < actions.length; i++) {
			result[i] = wd.getAction(actions[i]);
		}

		return result;
    }

    public void completeAction(Form form, int actionId, String commentContent) {
    	Workflow workflow = getWorkflow();
		try {
            workflow.doAction(form.getWorkflowId(), actionId, null);
		} catch (WorkflowException e) {
			log.error(e);
			throw new GenericServiceException(e.getMessage());
		}
		//TODO find a better way to set form draft flag only once.
		form.setDraft(false);
		formService.updateForm(form);
		if(commentContent != null && commentContent.trim().length() > 0) {
			Comment comment = new Comment();
			comment.setAuthor(CurrentUser.get());
			comment.setCreateDate(new Date());
			comment.setContent(commentContent);
			formService.addComment(form, comment);			
		}
		
		if(WorkflowEntry.COMPLETED == workflow.getEntryState(form.getWorkflowId())){
			form.setEffectiveDate(new Date());
			formService.updateForm(form);
		}
    }


    public void createWorkflowDescriptor(WorkflowDescriptor workflowDescriptor) {
        pm.create(workflowDescriptor);
    }

    public void updateWorkflowDescriptor(WorkflowDescriptor workflowDescriptor) {
        pm.update(workflowDescriptor);
    }

    public void deleteWorkflowDescriptor(WorkflowDescriptor workflowDescriptor) {
        pm.delete(workflowDescriptor);
    }

    public WorkflowDescriptor[] getAllWorkflowDescriptors() {
        List result = qm.findAll(WorkflowDescriptor.class);
		return (WorkflowDescriptor[]) result.toArray(new WorkflowDescriptor[result.size()]);
    }

    public WorkflowDescriptor loadWorkflowDescriptorByName(String name) {
        return (WorkflowDescriptor) qm.loadByName(WorkflowDescriptor.class, name);
    }

	private Workflow getWorkflow(){
		//TODO implement a IoC friendly Workflow
		Workflow workflow = new BasicWorkflow(Constants.convertToUserOwner(CurrentUser.get().getId()));
		workflow.setConfiguration(workflowConfiguration);
		return workflow;		
	}
	
    public void setFormService(FormService service) {
        formService = service;
    }

    public void setOrganizationService(OrganizationService service) {
        organizationService = service;
    }

    public void setWorkflowConfiguration(Configuration configuration) {
		workflowConfiguration = configuration;
    }

}

⌨️ 快捷键说明

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