AssetSetLastModifiedProcess.java
7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.dam.api.Asset
* com.day.cq.dam.api.Rendition
* com.day.cq.dam.commons.process.AbstractAssetWorkflowProcess
* com.day.cq.workflow.WorkflowException
* com.day.cq.workflow.WorkflowSession
* com.day.cq.workflow.exec.WorkItem
* com.day.cq.workflow.exec.WorkflowData
* com.day.cq.workflow.metadata.MetaDataMap
* com.day.text.Text
* javax.jcr.Item
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.core.process;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.commons.process.AbstractAssetWorkflowProcess;
import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.exec.WorkflowData;
import com.day.cq.workflow.metadata.MetaDataMap;
import com.day.text.Text;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service
@Property(name="process.label", value={"Set Last Modified"})
public class AssetSetLastModifiedProcess
extends AbstractAssetWorkflowProcess {
private static final Logger log = LoggerFactory.getLogger(AssetSetLastModifiedProcess.class);
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaData) throws WorkflowException {
String[] args = this.buildArguments(metaData);
Session session = workflowSession.getSession();
Asset asset = this.getAssetFromPayload(workItem, session);
if (null != asset) {
try {
Rendition rendition;
Node payloadNode;
List relLastModifiedByPath;
Node assetNode = (Node)asset.adaptTo(Node.class);
String userId = (String)workItem.getWorkflowData().getMetaDataMap().get("userId", String.class);
Node content = assetNode.getNode("jcr:content");
Node metadata = assetNode.getNode("jcr:content/metadata");
String resolvedUser = userId;
String payloadPath = null;
if (workItem.getWorkflowData().getPayloadType().equals("JCR_PATH")) {
payloadPath = workItem.getWorkflowData().getPayload().toString();
}
if (null != payloadPath && (payloadNode = session.getNode(payloadPath)).hasProperty("newRendition")) {
return;
}
if (content.hasProperty("newRendition") && (rendition = asset.getRendition("original")) != null) {
String lastModified = (String)rendition.getProperties().get((Object)"jcr:lastModifiedBy");
if (StringUtils.isNotBlank((String)lastModified)) {
resolvedUser = lastModified;
content.setProperty("jcr:lastModifiedBy", resolvedUser);
content.setProperty("jcr:lastModified", Calendar.getInstance());
}
return;
}
List relLastModifiedPath = this.getValuesFromArgs("relativeLastModifiedPath", args);
if (relLastModifiedPath.size() > 0) {
String nodePath = Text.getRelativeParent((String)(asset.getPath() + "/" + "jcr:content" + "/" + (String)relLastModifiedPath.get(0)), (int)1);
Node node = (Node)session.getItem(nodePath);
node.setProperty(Text.getName((String)((String)relLastModifiedPath.get(0))), Calendar.getInstance());
}
if ((relLastModifiedByPath = this.getValuesFromArgs("relativeLastModifiedByPath", args)).size() > 0) {
String nodePath = Text.getRelativeParent((String)(asset.getPath() + "/" + "jcr:content" + "/" + (String)relLastModifiedByPath.get(0)), (int)1);
Node node = (Node)session.getItem(nodePath);
node.setProperty(Text.getName((String)((String)relLastModifiedByPath.get(0))), resolvedUser);
}
content.setProperty("jcr:lastModifiedBy", resolvedUser);
content.setProperty("jcr:lastModified", Calendar.getInstance());
String dcModifiedPath = "jcr:content/metadata/dc:modified";
if (!assetNode.hasProperty(dcModifiedPath)) {
assetNode.getNode("jcr:content/metadata").setProperty("dc:modified", Calendar.getInstance());
}
}
catch (RepositoryException e) {
log.error("execute: repository error while setting last modified for asset [{}] in workflow [" + workItem.getId() + "]", (Object)asset.getPath(), (Object)e);
}
} else {
String wfPayload = workItem.getWorkflowData().getPayload().toString();
String message = "execute: cannot set last modified, asset [{" + wfPayload + "}] in payload doesn't exist for workflow [{" + workItem.getId() + "}].";
throw new WorkflowException(message);
}
}
public String[] buildArguments(MetaDataMap metaData) {
String processArgs = (String)metaData.get(Arguments.PROCESS_ARGS.name(), String.class);
if (processArgs != null && !processArgs.equals("")) {
return processArgs.split(",");
}
ArrayList<String> arguments = new ArrayList<String>();
String relativeLastModifiedPath = (String)metaData.get(Arguments.RELATIVE_LAST_MODIFIED_PATH.name(), String.class);
String relativeLastModifiedByPath = (String)metaData.get(Arguments.RELATIVE_LAST_MODIFIED_BY_PATH.name(), String.class);
if (StringUtils.isNotBlank((String)relativeLastModifiedPath)) {
arguments.add(Arguments.RELATIVE_LAST_MODIFIED_PATH.getArgumentPrefix() + relativeLastModifiedPath);
}
if (StringUtils.isNotBlank((String)relativeLastModifiedByPath)) {
arguments.add(Arguments.RELATIVE_LAST_MODIFIED_BY_PATH.getArgumentPrefix() + relativeLastModifiedByPath);
}
return arguments.toArray(new String[arguments.size()]);
}
public static enum Arguments {
PROCESS_ARGS("PROCESS_ARGS"),
RELATIVE_LAST_MODIFIED_PATH("relativeLastModifiedPath"),
RELATIVE_LAST_MODIFIED_BY_PATH("relativeLastModifiedByPath");
private String argumentName;
private Arguments(String argumentName) {
this.argumentName = argumentName;
}
public String getArgumentName() {
return this.argumentName;
}
public String getArgumentPrefix() {
return this.argumentName + ":";
}
}
}