001: package org.emforge.jbpm.web.bean;
002:
003: import java.io.InputStream;
004:
005: import javax.faces.application.FacesMessage;
006: import javax.faces.context.FacesContext;
007:
008: import org.apache.commons.io.FileUtils;
009: import org.apache.commons.io.IOUtils;
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012: import org.emforge.BpmService;
013: import org.emforge.xfer.WorkflowTO;
014: import org.richfaces.model.UploadItem;
015:
016: import ru.emdev.EmForge.web.bean.BaseControllerImpl;
017: import ru.emdev.EmForge.web.bean.EditTextController;
018: import ru.emdev.EmForge.web.bean.UploadForm;
019: import ru.emdev.EmForge.web.bean.MainMenuController.MainMenuItem;
020: import ru.emdev.EmForge.wiki.web.bean.Crumb;
021:
022: public class DeployForm extends BaseControllerImpl {
023:
024: public static final String DEPLOY_PAGE_NAME = "main.faces";
025: public static final String TITLE = "Deploy New Workflow";
026:
027: protected final Log m_logger = LogFactory.getLog(getClass());
028:
029: static final String DEPLOY_ERRMSG = "Error: cannot deploy new workflow, possibly some required files are missed or the description is incorrect.";
030:
031: BpmService m_bpmService;
032: WorkflowController m_workflowController;
033: EditTextController m_editTextController;
034: UploadForm m_uploadForm;
035:
036: /** TODO !!!
037: public UploadedFile getProcessArchive() {
038: return m_processArchive;
039: }
040:
041: public void setProcessArchive(UploadedFile i_processArchive) {
042: m_processArchive = i_processArchive;
043: }
044: */
045:
046: public void setBpmService(BpmService i_bpmContext) {
047: m_bpmService = i_bpmContext;
048: }
049:
050: public void setWorkflowController(
051: WorkflowController i_workflowController) {
052: m_workflowController = i_workflowController;
053: }
054:
055: public void setEditTextController(
056: EditTextController i_editTextController) {
057: m_editTextController = i_editTextController;
058: }
059:
060: public void setUploadForm(UploadForm i_uploadForm) {
061: m_uploadForm = i_uploadForm;
062: }
063:
064: public String deployWorkflow() {
065: UploadItem uploadItem = m_uploadForm.getUploadItem();
066: InputStream is = null;
067:
068: if (uploadItem != null && uploadItem.getFile() != null) {
069: try {
070: is = FileUtils.openInputStream(uploadItem.getFile());
071: } catch (Exception ex) {
072: // just ignore it here
073: }
074: }
075:
076: if (is != null) {
077: try {
078: // get comment text
079: String message = m_editTextController.getWikiText();
080:
081: byte[] archiveasBytes = IOUtils.toByteArray(is);
082:
083: // save process definition
084: WorkflowTO workflow = m_bpmService.addNewWorkflow(
085: archiveasBytes, message);
086:
087: m_workflowController.setWorkflow(workflow);
088: return "workflow";
089: } catch (Exception ex) {
090: m_logger.error("Cannot deploy new process definition:",
091: ex);
092: FacesContext context = FacesContext
093: .getCurrentInstance();
094: FacesMessage message = new FacesMessage(DEPLOY_ERRMSG,
095: DEPLOY_ERRMSG);
096: message.setSeverity(FacesMessage.SEVERITY_ERROR);
097: context.addMessage("deployForm", message);
098: return null;
099: } finally {
100: if (is != null) {
101: try {
102: is.close();
103: } catch (Exception ex) {
104: // just ignore it here
105: }
106: }
107:
108: m_uploadForm.reset();
109: }
110: }
111:
112: return null;
113: }
114:
115: @Override
116: public Crumb getTrailCrumbInfo() {
117: return new Crumb(TITLE, DEPLOY_PAGE_NAME);
118: }
119:
120: @Override
121: protected void init() {
122: }
123:
124: @Override
125: public String getTitleImpl() {
126: return TITLE;
127: }
128:
129: @Override
130: public MainMenuItem getSelectionItemOnMainMenu() {
131: return null;
132: }
133:
134: }
|