View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2019 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.handler.mediasource.dam.impl.metadata;
21  
22  import static com.day.cq.commons.jcr.JcrConstants.JCR_CONTENT;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import javax.jcr.Node;
28  import javax.jcr.RepositoryException;
29  import javax.jcr.Session;
30  
31  import org.apache.commons.lang3.StringUtils;
32  import org.apache.sling.api.resource.Resource;
33  import org.apache.sling.api.resource.ResourceResolver;
34  import org.jetbrains.annotations.NotNull;
35  import org.jetbrains.annotations.Nullable;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  import com.adobe.granite.workflow.PayloadMap;
40  import com.adobe.granite.workflow.collection.ResourceCollection;
41  import com.adobe.granite.workflow.collection.ResourceCollectionManager;
42  import com.adobe.granite.workflow.exec.WorkItem;
43  import com.adobe.granite.workflow.exec.WorkflowData;
44  import com.day.cq.wcm.api.NameConstants;
45  
46  import io.wcm.sling.commons.adapter.AdaptTo;
47  
48  /**
49   * Helper methods for processing workflow payload.
50   */
51  final class WorkflowProcessUtil {
52  
53    private static final Logger log = LoggerFactory.getLogger(WorkflowProcessUtil.class);
54  
55    static final String RT_WORKFLOW_PACKAGE = "cq/workflow/components/collection/page";
56  
57    private WorkflowProcessUtil() {
58      // static methods only
59    }
60  
61    /**
62     * Checks if the payload points to a resource path and returns it.
63     * @param workItem Work item
64     * @return Payload resource path or null
65     */
66    public static @Nullable String getPayloadResourcePath(@NotNull WorkItem workItem) {
67      WorkflowData data = workItem.getWorkflowData();
68      if (StringUtils.equals(data.getPayloadType(), PayloadMap.TYPE_JCR_PATH)) {
69        return data.getPayload().toString();
70      }
71      else {
72        return null;
73      }
74    }
75  
76    /**
77     * Get all resource paths paths form workflow payload - either directly referenced in the payload,
78     * or a collection of resources referenced via a workflow package.
79     * @param payloadPath Payload path
80     * @param primaryTypeResourceType JCR primary type or node type, in case of workflow package the result is filtered
81     *          to return only matching resources
82     * @param resourceResolver Resource resolver
83     * @param resourceCollectionManager Resource collection manager
84     * @return List of asset paths
85     */
86    public static @NotNull List<String> getPayloadResourcePaths(@NotNull String payloadPath,
87        @NotNull String primaryTypeResourceType,
88        @NotNull ResourceResolver resourceResolver,
89        @NotNull ResourceCollectionManager resourceCollectionManager) {
90      Session session = AdaptTo.notNull(resourceResolver, Session.class);
91      List<String> assetPaths = new ArrayList<>();
92      try {
93        if (session.nodeExists(payloadPath)) {
94          Node node = session.getNode(payloadPath);
95  
96          // check if payload node is a workflow package - collect all matching resources from it
97          if (isWorkflowPackagePage(node, resourceResolver)) {
98            List<ResourceCollection> resourceCollections = resourceCollectionManager.getCollectionsForNode(node);
99            for (ResourceCollection resourceCollection : resourceCollections) {
100             for (Node memberNode : resourceCollection.list(new String[] { primaryTypeResourceType })) {
101               assetPaths.add(memberNode.getPath());
102             }
103           }
104         }
105 
106         // otherwise directly return the payload path
107         else {
108           assetPaths.add(payloadPath);
109         }
110 
111       }
112     }
113     catch (RepositoryException ex) {
114       log.warn("Unable to resolve resource paths from workflow payload: {}", payloadPath, ex);
115     }
116     return assetPaths;
117   }
118 
119   private static boolean isWorkflowPackagePage(Node node, ResourceResolver resourceResolver) throws RepositoryException {
120     if (node.isNodeType(NameConstants.NT_PAGE)) {
121       Resource resource = resourceResolver.getResource(node.getPath() + "/" + JCR_CONTENT);
122       if (resource != null) {
123         return resource.isResourceType(RT_WORKFLOW_PACKAGE);
124       }
125     }
126     return false;
127   }
128 
129 }