WorkflowStatusProvider.java 9.64 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.PageInfoProvider
 *  com.day.cq.workflow.exec.WorkItem
 *  com.day.cq.workflow.exec.Workflow
 *  com.day.cq.workflow.exec.WorkflowData
 *  com.day.cq.workflow.exec.filter.WorkItemFilter
 *  com.day.cq.workflow.metadata.MetaDataMap
 *  com.day.cq.workflow.model.WorkflowModel
 *  com.day.cq.workflow.model.WorkflowNode
 *  com.day.cq.workflow.status.WorkflowStatus
 *  com.day.cq.xss.ProtectionContext
 *  com.day.cq.xss.XSSProtectionException
 *  com.day.cq.xss.XSSProtectionService
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.jackrabbit.api.JackrabbitSession
 *  org.apache.jackrabbit.api.security.user.Authorizable
 *  org.apache.jackrabbit.api.security.user.Group
 *  org.apache.jackrabbit.api.security.user.UserManager
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.workflow.impl;

import com.day.cq.wcm.api.PageInfoProvider;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.exec.Workflow;
import com.day.cq.workflow.exec.WorkflowData;
import com.day.cq.workflow.exec.filter.WorkItemFilter;
import com.day.cq.workflow.metadata.MetaDataMap;
import com.day.cq.workflow.model.WorkflowModel;
import com.day.cq.workflow.model.WorkflowNode;
import com.day.cq.workflow.status.WorkflowStatus;
import com.day.cq.xss.ProtectionContext;
import com.day.cq.xss.XSSProtectionException;
import com.day.cq.xss.XSSProtectionService;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service
public class WorkflowStatusProvider
implements PageInfoProvider {
    private static final Logger log = LoggerFactory.getLogger(WorkflowStatusProvider.class);
    @Reference
    private SlingRepository repository;
    @Reference
    private XSSProtectionService xss;

    public void updatePageInfo(SlingHttpServletRequest request, JSONObject info, Resource resource) throws JSONException {
        JSONObject workflow = new JSONObject();
        WorkflowStatus wfState = (WorkflowStatus)resource.adaptTo(WorkflowStatus.class);
        if (wfState != null) {
            WorkflowModel wfModel;
            Workflow wf;
            workflow.put("isRunning", wfState.isInRunningWorkflow(true));
            List wfList = wfState.getWorkflows(true);
            Workflow workflow2 = wf = wfList.size() > 0 ? (Workflow)wfList.get(0) : null;
            if (wf != null && null != (wfModel = wf.getWorkflowModel())) {
                workflow.put("id", (Object)wf.getId());
                workflow.put("state", (Object)wf.getState());
                workflow.put("model", (Object)new JSONObject().put("title", (Object)wfModel.getTitle()));
                String workflowTitle = (String)wf.getWorkflowData().getMetaDataMap().get("workflowTitle", String.class);
                if (workflowTitle != null) {
                    workflow.put("title", (Object)workflowTitle);
                }
                JSONArray workitems = new JSONArray();
                if (wf.getWorkItems().size() > 0) {
                    Session userSession = (Session)request.getResourceResolver().adaptTo(Session.class);
                    if (userSession instanceof JackrabbitSession) {
                        List items = wf.getWorkItems((WorkItemFilter)new UserFilter((JackrabbitSession)userSession));
                        for (WorkItem wi : items) {
                            WorkflowNode wn = wi.getNode();
                            if (!wn.getType().equals("PARTICIPANT") && !wn.getType().equals("DYNAMIC_PARTICIPANT")) continue;
                            JSONObject obj = new JSONObject();
                            obj.put("id", (Object)wi.getId());
                            obj.put("title", (Object)wn.getTitle());
                            if (this.xss != null) {
                                try {
                                    obj.put("title_xss", (Object)this.xss.protectForContext(ProtectionContext.PLAIN_HTML_CONTENT, wn.getTitle()));
                                }
                                catch (XSSProtectionException e) {
                                    log.warn("Unable to write protected value for workflow node title {}", (Object)wn.getTitle());
                                }
                            }
                            obj.put("description", (Object)wn.getDescription());
                            if (this.xss != null) {
                                try {
                                    obj.put("description_xss", (Object)this.xss.protectForContext(ProtectionContext.PLAIN_HTML_CONTENT, wn.getDescription()));
                                }
                                catch (XSSProtectionException e) {
                                    log.warn("Unable to write protected value for workflow node description {}", (Object)wn.getDescription());
                                }
                            }
                            obj.put("startTime", wi.getTimeStarted().getTime());
                            MetaDataMap metaMap = wn.getMetaDataMap();
                            String comment = (String)metaMap.get("comment", String.class);
                            obj.put("comment", (Object)(null == comment ? "" : comment));
                            String dialogPath = (String)metaMap.get("DIALOG_PATH", String.class);
                            obj.put("dialog_path", (Object)(null == dialogPath ? "" : dialogPath));
                            workitems.put((Object)obj);
                        }
                    } else {
                        log.warn("Unable to obtain JackrabbitSession at path {}", (Object)resource.getPath());
                    }
                }
                workflow.put("workitems", (Object)workitems);
            }
        }
        info.put("workflowInfo", (Object)workflow);
    }

    protected void activate(ComponentContext context) {
    }

    protected void deactivate(ComponentContext context) {
    }

    protected void bindRepository(SlingRepository slingRepository) {
        this.repository = slingRepository;
    }

    protected void unbindRepository(SlingRepository slingRepository) {
        if (this.repository == slingRepository) {
            this.repository = null;
        }
    }

    protected void bindXss(XSSProtectionService xSSProtectionService) {
        this.xss = xSSProtectionService;
    }

    protected void unbindXss(XSSProtectionService xSSProtectionService) {
        if (this.xss == xSSProtectionService) {
            this.xss = null;
        }
    }

    public class UserFilter
    implements WorkItemFilter {
        private JackrabbitSession jackrabbitSession;
        private List<Authorizable> authList;

        public UserFilter(JackrabbitSession jackrabbitSession) {
            this.authList = null;
            this.jackrabbitSession = jackrabbitSession;
        }

        public boolean doInclude(WorkItem workItem) {
            String assignee = workItem.getCurrentAssignee();
            if (assignee != null) {
                try {
                    for (Authorizable auth : this.getAuthorizables()) {
                        if (!assignee.equals(auth.getID())) continue;
                        return true;
                    }
                }
                catch (RepositoryException e) {
                    log.error(e.getMessage(), (Throwable)e);
                }
            }
            return false;
        }

        private List<Authorizable> getAuthorizables() throws RepositoryException {
            if (this.authList == null) {
                ArrayList<Authorizable> auths = new ArrayList<Authorizable>();
                Authorizable user = this.jackrabbitSession.getUserManager().getAuthorizable(this.jackrabbitSession.getUserID());
                auths.add(user);
                this.getGroups(user, auths);
                this.authList = auths;
            }
            return this.authList;
        }

        private void getGroups(Authorizable authorizable, List<Authorizable> auths) throws RepositoryException {
            Iterator groups = authorizable.memberOf();
            while (groups.hasNext()) {
                Group group = (Group)groups.next();
                auths.add((Authorizable)group);
                this.getGroups((Authorizable)group, auths);
            }
        }
    }

}