/** * This file Copyright (c) 2003-2010 Magnolia International * Ltd. (http://www.magnolia-cms.com). All rights reserved. * * * This file is dual-licensed under both the Magnolia * Network Agreement and the GNU General Public License. * You may elect to use one or the other of these licenses. * * This file is distributed in the hope that it will be * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the * implied warranty of MERCHANTABILITY or FITNESS FOR A * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. * Redistribution, except as permitted by whichever of the GPL * or MNA you select, is prohibited. * * 1. For the GPL license (GPL), you can redistribute and/or * modify this file under the terms of the GNU General * Public License, Version 3, as published by the Free Software * Foundation. You should have received a copy of the GNU * General Public License, Version 3 along with this program; * if not, write to the Free Software Foundation, Inc., 51 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * 2. For the Magnolia Network Agreement (MNA), this file * and the accompanying materials are made available under the * terms of the MNA which accompanies this distribution, and * is available at http://www.magnolia-cms.com/mna.html * * Any modifications to this file must keep this entire header * intact. * */ package info.magnolia.module.workflow.pages; import info.magnolia.cms.core.Content; import info.magnolia.module.admininterface.TemplatedMVCHandler; import info.magnolia.cms.core.HierarchyManager; import info.magnolia.cms.security.Security; import info.magnolia.cms.security.UserManager; import info.magnolia.context.MgnlContext; import java.io.IOException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; import javax.jcr.RepositoryException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.jdesktop.dom.SimpleDocument; import org.jdesktop.dom.SimpleDocumentBuilder; import org.jdesktop.dom.SimpleNodeList; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; /** * */ public class WorkflowStatusPage extends TemplatedMVCHandler { /** * */ private static final long serialVersionUID = 222L; /** * Logger */ private static Logger log = LoggerFactory.getLogger(WorkflowStatusPage.class); /** * @param name * @param request * @param response */ public WorkflowStatusPage(String name, HttpServletRequest request, HttpServletResponse response) { super(name, request, response); } public List getFlowList() throws RepositoryException { HierarchyManager hm = MgnlContext.getHierarchyManager("Expressions"); if (!hm.getRoot().hasContent("owfe")) { return Collections.EMPTY_LIST; } List flowList = new ArrayList(); Collection flows = hm.getContent("owfe").getChildren(); for (Iterator iterator = flows.iterator(); iterator.hasNext();) { Content flow = iterator.next(); Collection flowInst = flow.getChildren(); FlowDescription descr = null; // ordering? for (Iterator iterator2 = flowInst.iterator(); iterator2.hasNext();) { Content instance = iterator2.next(); int idx = instance.getName().indexOf('.'); if (idx == -1) { descr = new FlowDescription(); descr.setId(instance.getName()); descr.setFlowName(flow.getName()); flowList.add(descr); } else if (descr.getId().equals(instance.getName().substring(0, idx))) { descr.setStep(descr.getStep() + 1); } Collection entries = instance.getChildren(); for (Iterator iterator3 = entries.iterator(); iterator3.hasNext();) { Content entry = iterator3.next(); if (entry.getName().endsWith("participant")) { try { SimpleDocument doc = SimpleDocumentBuilder.simpleParse(entry.getNodeData("value").getString()); Node participantNode = doc.getElement("/bean/field[@name='appliedWorkitem']/bean/field[@name='participantName']/primitive"); descr.setLastParticipant(participantNode == null ? null : participantNode.getTextContent()); SimpleNodeList attrs = doc.getElements("/bean/field[@name='appliedWorkitem']/bean/field[@name='attributes']/map/entry"); int len = attrs.getLength(); Method m = null; for (int i = 0; i < len ; i++) { Node beanAttrName = getFirstNonTextChild(attrs.item(i)); if (getNonTextSibling(beanAttrName) == null) { continue; } descr.addAttribute(getFirstNonTextChild(getFirstNonTextChild(beanAttrName)).getTextContent(), getFirstNonTextChild(getFirstNonTextChild(getNonTextSibling(beanAttrName))).getTextContent()); } String lastMod = doc.getElement("/bean/field[@name='appliedWorkitem']/bean/field[@name='lastModified']/primitive").getTextContent(); descr.setLastModified(lastMod); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } return flowList; } /** * @param item * @return */ private Node getFirstNonTextChild(Node item) { NodeList nodes = item.getChildNodes(); int len = nodes.getLength(); for (int i = 0; i < len; i++) { if (nodes.item(i).hasAttributes() || nodes.item(i).getLocalName() != null) { return nodes.item(i); } } return null; } /** * @param item * @return */ private Node getNonTextSibling(Node item) { item = item.getNextSibling(); while (item != null) { if (item.hasAttributes() || item.getLocalName() != null) { return item; } item = item.getNextSibling(); }; return item; } public String getUser(String userName) { if (userName == null) { return null; } UserManager man = Security.getUserManager(); return man.getUser(userName).getProperty("title"); } }