TaskNotificationImpl.java 9.37 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.taskmanagement.Task
 *  com.adobe.granite.taskmanagement.TaskEvent
 *  com.adobe.granite.taskmanagement.TaskEventType
 *  com.adobe.granite.workflow.exec.InboxItem
 *  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
 *  javax.jcr.RepositoryException
 *  javax.jcr.Value
 *  org.apache.jackrabbit.api.security.user.Authorizable
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.api.wrappers.ValueMapDecorator
 *  org.osgi.service.event.Event
 *  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.taskmanagement.Task;
import com.adobe.granite.taskmanagement.TaskEvent;
import com.adobe.granite.taskmanagement.TaskEventType;
import com.adobe.granite.workflow.exec.InboxItem;
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.WorkItem;
import com.day.cq.workflow.exec.Workflow;
import com.day.cq.workflow.impl.email.Notification;
import com.day.cq.workflow.impl.email.TaskEMailNotificationService;
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.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.osgi.service.event.Event;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TaskNotificationImpl
implements Notification {
    private static final Logger log = LoggerFactory.getLogger(TaskNotificationImpl.class);
    private Task task;
    private final TaskEvent event;
    private final ValueMap properties = new ValueMapDecorator(new HashMap());
    private String emailFromAddress;
    private String hostPrefix;
    private Authorizable participant;
    private ResourceResolver resolver;

    public TaskNotificationImpl(Task task, TaskEvent event, ResourceResolver resolver) {
        this.task = task;
        this.event = event;
        this.resolver = resolver;
    }

    public void load(TaskEMailNotificationService svc, Authorizable participant) {
        this.putAll((Event)this.event, "event.");
        this.participant = participant;
        ResourceBundle bundle = null;
        if (participant != null) {
            this.putAll(participant, "participant.");
            String language = null;
            try {
                language = svc.getUserLanguage(participant.getID());
            }
            catch (RepositoryException e) {
                log.warn("Unable to query for user's preferred language.", (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);
            }
        }
        switch (this.event.getEventType()) {
            case TASK_CREATED: {
                this.put("event.type.title", bundle != null ? bundle.getString("Task Created") : "Task Created");
                break;
            }
            case TASK_COMPLETED: {
                this.put("event.type.title", bundle != null ? bundle.getString("Task Completed") : "Task Completed");
                break;
            }
            case TASK_SAVED: {
                this.put("event.type.title", bundle != null ? bundle.getString("Task Updated") : "Task Updated");
                break;
            }
            default: {
                this.put("event.type.title", bundle != null ? bundle.getString("Task Event") : "Task Event");
            }
        }
        if (this.task != null) {
            this.put("item.id", this.task.getId());
            this.put("item.name", this.task.getName());
            this.put("item.description", this.task.getDescription());
            String taskComment = "";
            Object taskCommentObject = this.task.getProperty("comment");
            if (taskCommentObject instanceof String) {
                taskComment = (String)taskCommentObject;
            }
            this.put("item.comment", taskComment);
            this.put("payload.path", this.task.getContentPath());
            PayloadInfoBuilderManager payloadBuilder = (PayloadInfoBuilderManager)this.resolver.adaptTo(PayloadInfoBuilderManager.class);
            String payloadOpenPath = payloadBuilder.getPayloadInfo((InboxItem)this.task, PayloadInfoBuilderContext.INITIATOR_HINT.NOTIFICATION.name()).getBrowserPath();
            if (payloadOpenPath == null) {
                payloadOpenPath = this.task.getContentPath();
            }
            this.put("payload.path.open", payloadOpenPath);
            Iterator propertyNames = this.task.getPropertyNames();
            if (propertyNames != null) {
                while (propertyNames.hasNext()) {
                    String key = (String)propertyNames.next();
                    Object property = this.task.getProperty(key);
                    if (property == null) continue;
                    this.put("task.property." + key, property.toString());
                }
            }
        } else {
            this.put("item.id", bundle != null ? bundle.getString("[Information not available]") : "[Information not available]");
        }
        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(Authorizable auth, String prefix) {
        try {
            Iterator iter = auth.getPropertyNames();
            while (iter.hasNext()) {
                String name = (String)iter.next();
                this.properties.put((Object)(prefix + name), (Object)auth.getProperty(name));
            }
            this.properties.put((Object)(prefix + "id"), (Object)auth.getID());
            this.properties.put((Object)(prefix + "home"), (Object)auth.getPath());
        }
        catch (RepositoryException e) {
            log.error("Unable to load the properties from the Authorizable.", (Throwable)e);
        }
        try {
            UserProperties profile;
            UserPropertiesManager upm = (UserPropertiesManager)this.resolver.adaptTo(UserPropertiesManager.class);
            if (upm != null && (profile = upm.getUserProperties(auth, "profile")) != null) {
                for (String name : profile.getPropertyNames()) {
                    this.properties.put((Object)(prefix + name), profile.getProperty(name, (Object)"", String.class));
                }
                this.properties.put((Object)(prefix + "name"), (Object)profile.getDisplayName());
            }
        }
        catch (RepositoryException e) {
            log.error("Unable to load teh properties from the Authorizable's profile.", (Throwable)e);
        }
    }

    public void putAll(Event 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 String getEmailFromAddress() {
        return this.emailFromAddress;
    }

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

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

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

    @Override
    public WorkflowEvent getEvent() {
        return null;
    }

    @Override
    public Workflow getWorkflow() {
        return null;
    }

    @Override
    public WorkItem getWorkItem() {
        return null;
    }

    @Override
    public Authorizable getInitiator() {
        return null;
    }

}