Search in sources :

Example 6 with WorkflowData

use of com.day.cq.workflow.exec.WorkflowData in project acs-aem-commons by Adobe-Consulting-Services.

the class ReplicatedByWorkflowProcess method execute.

@Override
public final void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {
    final WorkflowData workflowData = workItem.getWorkflowData();
    final String type = workflowData.getPayloadType();
    // Check if the payload is a path in the JCR
    if (!StringUtils.equals(type, "JCR_PATH")) {
        return;
    }
    // Get the path to the JCR resource from the payload
    final String payloadPath = workflowData.getPayload().toString();
    // Get ResourceResolver
    final Map<String, Object> authInfo = new HashMap<String, Object>();
    authInfo.put(AUTHENTICATION_INFO_SESSION, workflowSession.getSession());
    try (ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(authInfo)) {
        // Get replicated by value
        final String replicatedBy = StringUtils.defaultIfEmpty(workItem.getWorkflow().getInitiator(), "Unknown Workflow User");
        final List<String> paths = workflowPackageManager.getPaths(resourceResolver, payloadPath);
        for (final String path : paths) {
            // For each item in the WF Package, or if not a WF Package, path = payloadPath
            Resource resource = replStatusManager.getReplicationStatusResource(path, resourceResolver);
            final ModifiableValueMap mvm = resource.adaptTo(ModifiableValueMap.class);
            if (StringUtils.isNotBlank(mvm.get(ReplicationStatus.NODE_PROPERTY_LAST_REPLICATED_BY, String.class))) {
                mvm.put(ReplicationStatus.NODE_PROPERTY_LAST_REPLICATED_BY, replicatedBy);
                resourceResolver.commit();
                log.trace("Set replicateBy to [ {} ] on resource  [ {} ]", replicatedBy, resource.getPath());
            } else {
                log.trace("Skipping; Resource does not have replicateBy property set  [ {} ]", resource.getPath());
            }
        }
    } catch (LoginException e) {
        log.error("Could not acquire a ResourceResolver object from the Workflow Session's JCR Session: {}", e);
    } catch (PersistenceException e) {
        log.error("Could not save replicateBy property for payload [ {} ] due to: {}", payloadPath, e);
    } catch (RepositoryException e) {
        log.error("Could not collect Workflow Package items for payload [ {} ] due to: {}", payloadPath, e);
    }
}
Also used : HashMap(java.util.HashMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) LoginException(org.apache.sling.api.resource.LoginException) RepositoryException(javax.jcr.RepositoryException) WorkflowData(com.day.cq.workflow.exec.WorkflowData) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 7 with WorkflowData

use of com.day.cq.workflow.exec.WorkflowData in project acs-aem-commons by Adobe-Consulting-Services.

the class RenditionModifyingProcessTest method test_with_rendition_arg_getting_real_rendition.

@Test
public void test_with_rendition_arg_getting_real_rendition() throws Exception {
    String path = "/content/dam/some/path.ext";
    WorkItem workItem = mock(WorkItem.class);
    WorkflowData data = mock(WorkflowData.class);
    when(workItem.getWorkflowData()).thenReturn(data);
    when(data.getPayloadType()).thenReturn(WorkflowHelper.TYPE_JCR_PATH);
    when(data.getPayload()).thenReturn(path);
    Resource resource = mock(Resource.class);
    Asset asset = mock(Asset.class);
    Rendition rendition = mock(Rendition.class);
    when(resource.adaptTo(Asset.class)).thenReturn(asset);
    when(resource.getResourceType()).thenReturn(DamConstants.NT_DAM_ASSET);
    when(resourceResolver.getResource(path)).thenReturn(resource);
    when(asset.getRendition(isA(RenditionPicker.class))).thenReturn(rendition);
    when(rendition.getStream()).then(new Answer<InputStream>() {

        @Override
        public InputStream answer(InvocationOnMock invocation) throws Throwable {
            return getClass().getResourceAsStream("/img/test.png");
        }
    });
    when(harness.processLayer(any(Layer.class), eq(rendition), eq(workflowSession), any(String[].class))).thenAnswer(new Answer<Layer>() {

        @Override
        public Layer answer(InvocationOnMock invocation) throws Throwable {
            return (Layer) invocation.getArguments()[0];
        }
    });
    MetaDataMap metaData = new SimpleMetaDataMap();
    metaData.put("PROCESS_ARGS", "renditionName:test");
    process.execute(workItem, workflowSession, metaData, workflowHelper);
    verify(harness, times(1)).processLayer(any(Layer.class), eq(rendition), eq(workflowSession), any(String[].class));
    verify(harness, times(1)).saveImage(eq(asset), eq(rendition), any(Layer.class), eq("image/png"), eq(0.6));
}
Also used : Rendition(com.day.cq.dam.api.Rendition) InputStream(java.io.InputStream) Resource(org.apache.sling.api.resource.Resource) RenditionPicker(com.day.cq.dam.api.RenditionPicker) WorkItem(com.day.cq.workflow.exec.WorkItem) Layer(com.day.image.Layer) WorkflowData(com.day.cq.workflow.exec.WorkflowData) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SimpleMetaDataMap(com.day.cq.workflow.metadata.SimpleMetaDataMap) MetaDataMap(com.day.cq.workflow.metadata.MetaDataMap) SimpleMetaDataMap(com.day.cq.workflow.metadata.SimpleMetaDataMap) Asset(com.day.cq.dam.api.Asset) Test(org.junit.Test)

Example 8 with WorkflowData

use of com.day.cq.workflow.exec.WorkflowData in project acs-aem-commons by Adobe-Consulting-Services.

the class SendTemplatedEmailProcessTest method test_with_correct_args_PagePayload.

@Test
public void test_with_correct_args_PagePayload() throws Exception {
    String editPageUrl = "http://localhost:4502/editor.html" + WCM_PAYLOAD_PATH;
    String publishUrl = "http://localhost:4503" + WCM_PAYLOAD_PATH;
    Map<String, String> expectedEmailParams = new HashMap<String, String>();
    expectedEmailParams.put(SendTemplatedEmailConstants.JCR_PATH, WCM_PAYLOAD_PATH);
    expectedEmailParams.put(SendTemplatedEmailConstants.AUTHOR_LINK, editPageUrl + ".html");
    expectedEmailParams.put(SendTemplatedEmailConstants.PUBLISH_LINK, publishUrl + ".html");
    WorkflowData workflowData = mock(WorkflowData.class);
    when(workItem.getWorkflowData()).thenReturn(workflowData);
    when(workflowData.getPayloadType()).thenReturn("JCR_PATH");
    MetaDataMap metaData = new SimpleMetaDataMap();
    metaData.put("PROCESS_ARGS", "sendTo:" + GROUP_PATH + ",emailTemplate:" + EMAIL_TEMPLATE);
    // expected build args
    String[] expectedBuildArgs = new String[] { "sendTo:" + GROUP_PATH, "emailTemplate:" + EMAIL_TEMPLATE };
    Object payload = mock(Object.class);
    when(workflowData.getPayload()).thenReturn(payload);
    when(payload.toString()).thenReturn(WCM_PAYLOAD_PATH);
    // mock payload resource
    Resource payloadRes = mock(Resource.class);
    when(resourceResolver.getResource(WCM_PAYLOAD_PATH)).thenReturn(payloadRes);
    when(payloadRes.getPath()).thenReturn(WCM_PAYLOAD_PATH);
    when(payloadRes.getResourceResolver()).thenReturn(resourceResolver);
    // mock authorUI and externalizer
    when(authorUIHelper.generateEditPageLink(WCM_PAYLOAD_PATH, true, resourceResolver)).thenReturn(editPageUrl + ".html");
    when(externalizer.publishLink(resourceResolver, WCM_PAYLOAD_PATH + ".html")).thenReturn(publishUrl + ".html");
    when(harness.getEmailAddrs(workItem, payloadRes, expectedBuildArgs)).thenReturn(GROUP_MEMBERS);
    process.execute(workItem, workflowSession, metaData);
    verify(harness, times(1)).getEmailAddrs(workItem, payloadRes, expectedBuildArgs);
    verify(harness, times(1)).getAdditionalParams(workItem, workflowSession, payloadRes);
    verify(emailService, times(1)).sendEmail(EMAIL_TEMPLATE, expectedEmailParams, GROUP_MEMBERS);
}
Also used : HashMap(java.util.HashMap) SimpleMetaDataMap(com.day.cq.workflow.metadata.SimpleMetaDataMap) MetaDataMap(com.day.cq.workflow.metadata.MetaDataMap) SimpleMetaDataMap(com.day.cq.workflow.metadata.SimpleMetaDataMap) Resource(org.apache.sling.api.resource.Resource) WorkflowData(com.day.cq.workflow.exec.WorkflowData) Test(org.junit.Test)

Aggregations

WorkflowData (com.day.cq.workflow.exec.WorkflowData)8 MetaDataMap (com.day.cq.workflow.metadata.MetaDataMap)6 SimpleMetaDataMap (com.day.cq.workflow.metadata.SimpleMetaDataMap)6 Resource (org.apache.sling.api.resource.Resource)6 Test (org.junit.Test)6 HashMap (java.util.HashMap)4 Asset (com.day.cq.dam.api.Asset)2 WorkItem (com.day.cq.workflow.exec.WorkItem)2 LoginException (org.apache.sling.api.resource.LoginException)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 Rendition (com.day.cq.dam.api.Rendition)1 RenditionPicker (com.day.cq.dam.api.RenditionPicker)1 Layer (com.day.image.Layer)1 InputStream (java.io.InputStream)1 SimpleDateFormat (java.text.SimpleDateFormat)1 RepositoryException (javax.jcr.RepositoryException)1 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)1 PersistenceException (org.apache.sling.api.resource.PersistenceException)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1