All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.day.cq.dam.core.process.AbstractScheduledReplicationBPProcess Maven / Gradle / Ivy

There is a newer version: 6.5.21
Show newest version
/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2018 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 *************************************************************************/
package com.day.cq.dam.core.process;

import java.util.Collections;

import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.framework.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.cq.dam.mac.sync.helper.FolderSyncHelper;
import com.adobe.cq.dam.mac.sync.helper.FolderSyncRequest;
import com.adobe.cq.dam.mac.sync.helper.FolderSyncResponse;
import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;

/**
 * The ScheduledPublishBPProcess is called in a workflow process step.
 * This process will publish(or unpublish) content to Brand Portal after a specified time.
 *
 */
@Component(metatype = false)
@Service(WorkflowProcess.class)
@Properties({
    @Property(name = Constants.SERVICE_DESCRIPTION, value = "Scheduled Publish/Unpublish of Assets to Brand Portal"),
    @Property(name = "process.label", value = "Scheduled Publish Brand Portal")
})

public abstract class AbstractScheduledReplicationBPProcess implements WorkflowProcess{

    protected enum REPTYPE{PUBLISH, UNPUBLISH};
    
    private final Logger log = LoggerFactory.getLogger(getClass());
    
    private static final String repType = "mediaportal";
    
    private static final String operation = "dam.mac.sync";
    
    private static final String SERVICE_USER_ID = "assetidhelper";
    
    @Reference
    private SlingRepository repository;
    
    @Reference
    private FolderSyncHelper folderSyncHelper;
    
    @Reference
    private ResourceResolverFactory resourceResolverFactory;
    
    @Override
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metadataMap) throws WorkflowException {
        ResourceResolver userResourceResolver = null;
        try {
            log.info("Stating scheduled publish/unpublish to BrandPortal ..");
            WorkflowData data = workItem.getWorkflowData();
            String path = (String)data.getPayload();
            userResourceResolver = getInitiatorResolver(workItem);
            FolderSyncRequest request = new FolderSyncRequest();
            request.setType(repType);
            request.setOperation(operation);
            request.setContextPath("");
            request.setPaths(new String[] {path});
            request.setUserResourceResolver(userResourceResolver);
            if(getReplicationType() == REPTYPE.UNPUBLISH) {
                request.setDisableSync("disable");
            }
            FolderSyncResponse response = folderSyncHelper.process(request);
            log.info("Finished scheduled publish/unpublish to BrandPortal. success = {}", (response.getStatus() == null));
        } catch (Exception ex) {
            log.error("An error occurred while scheduled publish/unpublish to BrandPortal ..{}", ex);
        } finally {
            if (userResourceResolver != null) {
                userResourceResolver.close();
            }
        }
    }
    
    private ResourceResolver getInitiatorResolver(WorkItem workItem) throws javax.jcr.LoginException, RepositoryException, LoginException {
        final String initiator = workItem.getWorkflow().getInitiator();
        Session initiatorSession = getUserSession(initiator);
        return getResourceResolver(initiatorSession);
    }

    private Session getUserSession(String userId) throws javax.jcr.LoginException, RepositoryException {
        final SimpleCredentials credentials = new SimpleCredentials(userId, new char[0]);
        return repository.impersonateFromService(SERVICE_USER_ID, credentials, null);
    }
    protected ResourceResolver getResourceResolver(final Session session) throws LoginException {
        return  resourceResolverFactory.getResourceResolver(Collections.singletonMap("user.jcr.session", (Object) session));
    }
    
    protected abstract REPTYPE getReplicationType();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy