1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
59 }
60
61
62
63
64
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
78
79
80
81
82
83
84
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
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
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 }