/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2006 Danet GmbH (www.danet.de), BU BTS.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: ProcDefUpload.java 2442 2007-09-06 06:08:40Z mlipp $
*
* $Log$
*/
package de.danet.an.workflow.clients.mgmtportlets.procdefupload;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.portlet.PortletRequest;
import org.apache.commons.fileupload.FileItem;
import de.danet.an.util.jsf.JSFUtil;
import de.danet.an.util.jsf.MyFacesAdaptedPortlet;
import de.danet.an.workflow.api.ImportException;
import de.danet.an.workflow.api.PrioritizedMessage;
import de.danet.an.workflow.api.ProcessDefinitionDirectory;
import de.danet.an.workflow.clients.mgmtportlets.ApplicationContext;
import de.danet.an.workflow.clients.mgmtportlets.WorkflowServiceConnection;
/**
* This class provides the session scoped model for process definition
* upload.
*
* @author mnl
*
*/
public class ProcDefUpload {
private static final org.apache.commons.logging.Log logger
= org.apache.commons.logging.LogFactory.getLog (ProcDefUpload.class);
private static String L10N_MSGS
= "de.danet.an.workflow.clients.mgmtportlets.procdefupload.L10n";
private ApplicationContext applicationContext = null;
private WorkflowServiceConnection wsc = null;
/**
* @return Returns the applicationContext.
*/
public ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* @param applicationContext The applicationContext to set.
*/
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
/**
* @return Returns the workflow service connection.
*/
public WorkflowServiceConnection getWorkflowServiceConnection() {
return wsc;
}
/**
* @param wsc The workflow service connection to set.
*/
public void setWorkflowServiceConnection(WorkflowServiceConnection wsc) {
this.wsc = wsc;
if (logger.isDebugEnabled ()) {
logger.debug ("Workflow service connection set to: " + wsc);
}
}
/**
* Process the uploaded file.
* @return the next page
*/
public String processProcDef ()
throws RemoteException {
FacesContext context = FacesContext.getCurrentInstance();
Map fileItems = (Map)((PortletRequest)context
.getExternalContext().getRequest()).getPortletSession()
.getAttribute(MyFacesAdaptedPortlet.FILE_ITEMS);
if (fileItems == null || !fileItems.containsKey("file")
|| ((FileItem)fileItems.get("file")).getSize() <= 0) {
JSFUtil.addMessage(FacesMessage.SEVERITY_ERROR, L10N_MSGS,
"missingProcDefFile", null);
return "OK";
}
FileItem fileItem = (FileItem)fileItems.get("file");
ProcessDefinitionDirectory pdd = null;
try {
pdd = wsc.getWorkflowService().processDefinitionDirectory();
if (logger.isDebugEnabled ()) {
logger.debug ("Importing from " + fileItem.getName());
}
InputStream is = fileItem.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream ();
byte[] buf = new byte[10000];
int cnt = 0;
while ((cnt = is.read(buf)) >= 0) {
bos.write (buf, 0, cnt);
}
is.close();
List pms = pdd.importProcessDefinitions(bos.toByteArray());
JSFUtil.addMessage(FacesMessage.SEVERITY_INFO, L10N_MSGS,
"procDefsImported", new Object[] { fileItem.getName() });
if (pms.size() > 0) {
addMessages(pms);
}
applicationContext.setReloadDefinitionsBeforeRendering(true);
} catch (ImportException iex) {
addMessages(iex.messages());
} catch (Exception ioe) {
JSFUtil.addMessage(FacesMessage.SEVERITY_ERROR, L10N_MSGS,
"resourceCurrentlyUnavailable", null);
logger.error (ioe);
} finally {
wsc.getWorkflowService().release(pdd);
}
return "OK";
}
/**
* Add prioritized messages included in the ImportException.
* The messages are rendered differently based on the priority of its
* priority.
* @param pms the given prioritized messages
*/
public void addMessages (List pms) {
for (Iterator i = pms.iterator(); i.hasNext();) {
PrioritizedMessage s = (PrioritizedMessage)i.next();
FacesMessage.Severity severity = null;
if (s.priority() == PrioritizedMessage.Priority.DEBUG) {
severity = FacesMessage.SEVERITY_INFO;
} else if (s.priority() == PrioritizedMessage.Priority.INFO) {
severity = FacesMessage.SEVERITY_INFO;
} else if (s.priority() == PrioritizedMessage.Priority.WARN) {
severity = FacesMessage.SEVERITY_WARN;
} else if (s.priority() == PrioritizedMessage.Priority.ERROR) {
severity = FacesMessage.SEVERITY_ERROR;
} else if (s.priority() == PrioritizedMessage.Priority.FATAL) {
severity = FacesMessage.SEVERITY_FATAL;
}
String text = s.message(JSFUtil.activeLocale());
FacesMessage msg = new FacesMessage (severity, text, text);
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
}