NotificationImpl.java 12.6 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.security.user.UserProperties
 *  com.adobe.granite.security.user.UserPropertiesManager
 *  com.adobe.granite.workflow.exec.InboxItem
 *  com.adobe.granite.workflow.exec.WorkItem
 *  com.adobe.granite.workflow.payload.PayloadInfo
 *  com.adobe.granite.workflow.payload.PayloadInfoBuilderContext
 *  com.adobe.granite.workflow.payload.PayloadInfoBuilderContext$INITIATOR_HINT
 *  com.adobe.granite.workflow.payload.PayloadInfoBuilderManager
 *  com.day.cq.workflow.event.WorkflowEvent
 *  com.day.cq.workflow.exec.WorkItem
 *  com.day.cq.workflow.exec.Workflow
 *  com.day.cq.workflow.exec.WorkflowData
 *  com.day.cq.workflow.metadata.MetaDataMap
 *  com.day.cq.workflow.model.WorkflowModel
 *  com.day.cq.workflow.model.WorkflowNode
 *  javax.jcr.RepositoryException
 *  javax.jcr.Value
 *  org.apache.jackrabbit.api.security.user.Authorizable
 *  org.apache.jackrabbit.api.security.user.UserManager
 *  org.apache.jackrabbit.util.Text
 *  org.apache.sling.api.adapter.Adaptable
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.api.wrappers.ValueMapDecorator
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.workflow.impl.email;

import com.adobe.granite.security.user.UserProperties;
import com.adobe.granite.security.user.UserPropertiesManager;
import com.adobe.granite.workflow.exec.InboxItem;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.payload.PayloadInfo;
import com.adobe.granite.workflow.payload.PayloadInfoBuilderContext;
import com.adobe.granite.workflow.payload.PayloadInfoBuilderManager;
import com.day.cq.workflow.event.WorkflowEvent;
import com.day.cq.workflow.exec.Workflow;
import com.day.cq.workflow.exec.WorkflowData;
import com.day.cq.workflow.impl.ServiceLoginUtil;
import com.day.cq.workflow.impl.email.EMailNotificationService;
import com.day.cq.workflow.impl.email.Notification;
import com.day.cq.workflow.impl.email.NotificationHelper;
import com.day.cq.workflow.metadata.MetaDataMap;
import com.day.cq.workflow.model.WorkflowModel;
import com.day.cq.workflow.model.WorkflowNode;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.TreeSet;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.adapter.Adaptable;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NotificationImpl
implements Notification {
    private static final Logger log = LoggerFactory.getLogger(NotificationImpl.class);
    private final Workflow workflow;
    private final com.day.cq.workflow.exec.WorkItem workItem;
    private final WorkflowEvent event;
    private final ValueMap properties = new ValueMapDecorator(new HashMap());
    private String emailFromAddress;
    private String hostPrefix;
    private Authorizable initiator;
    private Authorizable participant;
    private ResourceResolverFactory resolverFactory;

    public NotificationImpl(Workflow wf, com.day.cq.workflow.exec.WorkItem wi, WorkflowEvent event, ResourceResolverFactory resolverFactory) {
        this.workflow = wf;
        this.workItem = wi;
        this.event = event;
        this.resolverFactory = resolverFactory;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void load(EMailNotificationService svc, Authorizable participant) {
        this.putAll(this.event, "event.");
        this.participant = participant;
        ResourceBundle bundle = null;
        if (participant != null) {
            try {
                this.putAll(participant, "participant.");
            }
            catch (RepositoryException e) {
                log.error("Unable to add properties for the participant.", (Throwable)e);
            }
            String language = null;
            try {
                language = svc.getUserLanguage(participant.getID());
            }
            catch (RepositoryException e) {
                log.error("Unable to lookup language for participant.", (Throwable)e);
            }
            if (language != null) {
                bundle = svc.getUserResourceBundle(language);
            }
            if (bundle != null) {
                this.put("participant.language", bundle.getLocale().getLanguage());
            } else if (language != null) {
                this.put("participant.language", language);
            }
        }
        if (this.workItem != null) {
            this.put("item.id", this.workItem.getId());
            this.put("item.node.id", this.workItem.getNode().getId());
            this.put("item.node.title", this.workItem.getNode().getTitle());
            this.put("item.node.type", this.workItem.getNode().getType());
            this.putAll(this.workItem.getNode().getMetaDataMap(), "item.node.data.");
            this.putAll(this.workItem.getWorkflowData().getMetaDataMap(), "item.workflow.data.");
            this.putAll(this.workItem.getMetaDataMap(), "item.data.");
        } else {
            this.put("item.id", bundle != null ? bundle.getString("[Information not available]") : "[Information not available]");
            this.put("item.node.id", bundle != null ? bundle.getString("[Information not available]") : "[Information not available]");
            this.put("item.node.title", bundle != null ? bundle.getString("[Information not available]") : "[Information not available]");
            this.put("item.node.type", bundle != null ? bundle.getString("[Information not available]") : "[Information not available]");
        }
        this.put("instance.id", this.workflow.getId());
        this.put("instance.state", this.workflow.getState());
        this.putAll(this.workflow.getMetaDataMap(), "instance.data.");
        this.put("model.title", this.workflow.getWorkflowModel().getTitle());
        this.put("model.id", this.workflow.getWorkflowModel().getId());
        this.put("model.version", this.workflow.getWorkflowModel().getVersion());
        this.putAll(this.workflow.getWorkflowModel().getMetaDataMap(), "model.data.");
        String initiatorId = this.workflow.getInitiator();
        ResourceResolver userResolver = null;
        try {
            userResolver = ServiceLoginUtil.createWorkflowUserReaderResolver(this.resolverFactory);
            this.initiator = NotificationHelper.getAuthorizable(this.getUserManager(userResolver), initiatorId);
            this.putAll(this.initiator, "initiator.");
        }
        catch (RepositoryException e) {
            log.error("Unable to query the properties for the initiator with ID [{}].", (Object)initiatorId, (Object)e);
        }
        finally {
            if (userResolver != null && userResolver.isLive()) {
                userResolver.close();
            }
        }
        WorkflowData workflowData = this.workflow.getWorkflowData();
        this.putAll(workflowData.getMetaDataMap(), "data");
        this.put("payload.data", workflowData.getPayload());
        this.put("payload.type", workflowData.getPayloadType());
        if ("JCR_PATH".equals(workflowData.getPayloadType())) {
            String payloadOpenPath;
            ResourceResolver repoReaderResolver = null;
            payloadOpenPath = null;
            this.put("payload.path", workflowData.getPayload());
            try {
                repoReaderResolver = ServiceLoginUtil.createRepositoryReaderResolver(this.resolverFactory);
                PayloadInfoBuilderManager payloadBuilder = (PayloadInfoBuilderManager)repoReaderResolver.adaptTo(PayloadInfoBuilderManager.class);
                WorkItem wi = null;
                if (this.workItem instanceof Adaptable) {
                    wi = (WorkItem)((Adaptable)this.workItem).adaptTo(WorkItem.class);
                }
                if (wi != null) {
                    payloadOpenPath = payloadBuilder.getPayloadInfo((InboxItem)wi, PayloadInfoBuilderContext.INITIATOR_HINT.NOTIFICATION.name()).getBrowserPath();
                }
            }
            finally {
                if (repoReaderResolver != null && repoReaderResolver.isLive()) {
                    repoReaderResolver.close();
                }
            }
            if (payloadOpenPath == null && workflowData.getPayload() instanceof String) {
                payloadOpenPath = (String)workflowData.getPayload();
            }
            this.put("payload.path.open", Text.escapePath((String)payloadOpenPath));
        }
        this.hostPrefix = svc.getHostPrefix();
        this.put("host.prefix", this.hostPrefix);
        if (log.isDebugEnabled()) {
            StringBuilder props = new StringBuilder();
            TreeSet names = new TreeSet(this.properties.keySet());
            for (String name : names) {
                Object value = this.properties.get((Object)name);
                log.debug("- {}:{}", (Object)name, value);
                props.append(name).append(":").append(value).append("\n");
            }
            this.put("properties", props.toString());
        }
    }

    public void put(String name, Object value) {
        if (value != null) {
            this.properties.put((Object)name, value);
        }
    }

    public void putAll(MetaDataMap metaData, String prefix) {
        for (String name : metaData.keySet()) {
            this.properties.put((Object)(prefix + name), metaData.get(name, String.class));
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void putAll(Authorizable auth, String prefix) throws RepositoryException {
        Iterator iter = auth.getPropertyNames();
        while (iter.hasNext()) {
            String name = (String)iter.next();
            this.properties.put((Object)(prefix + name), (Object)auth.getProperty(name));
        }
        ResourceResolver userResolver = null;
        try {
            userResolver = ServiceLoginUtil.createWorkflowUserReaderResolver(this.resolverFactory);
            UserPropertiesManager upManager = (UserPropertiesManager)userResolver.adaptTo(UserPropertiesManager.class);
            if (upManager != null) {
                UserProperties up = upManager.getUserProperties(auth, "profile");
                String[] propNames = up.getPropertyNames();
                for (int i = 0; i < propNames.length; ++i) {
                    this.properties.put((Object)(prefix + propNames[i]), (Object)up.getProperty(propNames[i]));
                }
                this.properties.put((Object)(prefix + "name"), (Object)up.getDisplayName());
            }
        }
        finally {
            if (userResolver != null && userResolver.isLive()) {
                userResolver.close();
            }
        }
        this.properties.put((Object)(prefix + "id"), (Object)auth.getID());
        this.properties.put((Object)(prefix + "home"), (Object)auth.getPath());
    }

    public void putAll(WorkflowEvent evt, String prefix) {
        for (String name : evt.getPropertyNames()) {
            this.properties.put((Object)(prefix + name), evt.getProperty(name));
        }
    }

    @Override
    public ValueMap getProperties() {
        return this.properties;
    }

    @Override
    public com.day.cq.workflow.exec.WorkItem getWorkItem() {
        return this.workItem;
    }

    @Override
    public WorkflowEvent getEvent() {
        return this.event;
    }

    @Override
    public Workflow getWorkflow() {
        return this.workflow;
    }

    @Override
    public String getEmailFromAddress() {
        return this.emailFromAddress;
    }

    public void setEmailFromAddress(String emailFromAddress) {
        this.emailFromAddress = emailFromAddress;
    }

    @Override
    public String getHostPrefix() {
        return this.hostPrefix;
    }

    @Override
    public Authorizable getInitiator() {
        return this.initiator;
    }

    @Override
    public Authorizable getParticipant() {
        return this.participant;
    }

    private UserManager getUserManager(ResourceResolver resolver) {
        UserManager um = null;
        if (resolver != null) {
            um = (UserManager)resolver.adaptTo(UserManager.class);
        }
        return um;
    }
}