package com.lifiti.portlet;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.Portlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.osgi.service.component.annotations.Component;
import com.liferay.portal.kernel.servlet.ServletResponseUtil;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PortalUtil;
import com.lifiti.util.ActivitiUtils;
import com.lifiti.util.FilenameUtils;
@Component(immediate = true, property = { "com.liferay.portlet.display-category=category.sample",
"com.liferay.portlet.instanceable=true", "javax.portlet.display-name=Process List",
"javax.portlet.init-param.template-path=/", "javax.portlet.init-param.view-template=/process/processList.jsp",
"javax.portlet.resource-bundle=content.Language",
"javax.portlet.security-role-ref=power-user,user" }, service = Portlet.class)
public class ProcessListPortlet extends BpmBasePortlet {
@Override
public void render(RenderRequest request, RenderResponse response) throws PortletException, IOException {
List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery().list();
request.setAttribute("processDefinitionList", processDefinitionList);
super.render(request, response);
}
public void deployUploadAction(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception{
UploadPortletRequest upr = PortalUtil.getUploadPortletRequest(actionRequest);
String fileFieldName = "file";
if (upr.getSize(fileFieldName) == 0)
{
throw new Exception("空文件!");
}
String fileName = upr.getFileName(fileFieldName);
try {
// 得到输入流(字节流)对象
InputStream fileInputStream = upr.getFileAsStream(fileFieldName);//file.getInputStream();
// 文件的扩展名
String extension = FilenameUtils.getExtension(fileName);
// zip或者bar类型的文件用ZipInputStream方式部署
DeploymentBuilder deployment = repositoryService.createDeployment();
if (extension.equals("zip") || extension.equals("bar")) {
ZipInputStream zip = new ZipInputStream(fileInputStream);
deployment.addZipInputStream(zip);
} else if (extension.equals("xml")){
// xml类型的文件
deployment.addInputStream(fileName, fileInputStream);
}
deployment.deploy();
_log.info(fileName +" deploy success!");
} catch (Exception e) {
_log.error(fileName + " error on deploy process, because of file input stream");
}
String message = "BPMN file upload success!";
actionRequest.setAttribute("RETURN_MESSAGE", message);
}
public void imageAction(ActionRequest actionRequest, ActionResponse actionResponse) {
System.out.println("$$$$ begin $$$$ ");
processEngine = ActivitiUtils.getProcessEngine();
repositoryService = processEngine.getRepositoryService();
String processDefinitionId = ParamUtil.getString(actionRequest, "pdid");
String diagramResourceName = ParamUtil.getString(actionRequest, "diagramResourceName");
System.out.println("$$$$ pdid = $$$$ " + processDefinitionId);
System.out.println("$$$$ diagramResourceName = $$$$ " + diagramResourceName);
ProcessDefinitionQuery pdq = repositoryService.createProcessDefinitionQuery();
ProcessDefinition pd = pdq.processDefinitionId(processDefinitionId).singleResult();
// 通过接口读取
InputStream resourceAsStream = repositoryService.getResourceAsStream(pd.getDeploymentId(), diagramResourceName);
HttpServletResponse httpServletResponse = (HttpServletResponse) PortalUtil
.getHttpServletResponse(actionResponse);
HttpServletRequest httpServletRequest = (HttpServletRequest) PortalUtil.getHttpServletRequest(actionRequest);
// 另存输出
//ByteArrayOutputStream baos;
try {
// baos = inputStream2ByteArrayOutputStream(resourceAsStream);
httpServletResponse.reset();
httpServletResponse.setHeader("Expires", "0");
httpServletResponse.setHeader("Pragma", "public");
httpServletResponse.setHeader("cache-Control", "must-revalidate,post-check=0,pre-check=0");
httpServletResponse.setHeader("Content-disposition", "inline=filename=temp.png");
httpServletResponse.setContentType("image/png");
// httpServletResponse.setContentLength(baos.size());
ServletResponseUtil.sendFile(httpServletRequest, httpServletResponse, "temp.png", resourceAsStream,
"image/png");
// ServletResponseUtil.sendFile(httpServletRequest,
// httpServletResponse,
// "temp.png", baos.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}