CreateManuscriptWorkflowProcess.java
8.12 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
157
158
159
160
161
162
163
164
165
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.projects.api.Project
* com.adobe.granite.workflow.WorkflowException
* com.adobe.granite.workflow.WorkflowSession
* com.adobe.granite.workflow.exec.WorkItem
* com.adobe.granite.workflow.exec.Workflow
* com.adobe.granite.workflow.exec.WorkflowData
* com.adobe.granite.workflow.exec.WorkflowProcess
* com.adobe.granite.workflow.metadata.MetaDataMap
* com.day.cq.commons.jcr.JcrUtil
* com.day.cq.dam.api.Asset
* com.day.cq.dam.api.AssetManager
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang3.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
*/
package com.adobe.cq.projects.impl.workflow;
import com.adobe.cq.projects.api.Project;
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.Workflow;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.AssetManager;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Set;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang3.StringUtils;
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.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
@Component
@Service(value={WorkflowProcess.class})
@Properties(value={@Property(name="service.description", value={"Create Manuscript Workflow Process"}), @Property(name="service.vendor", value={"Adobe"}), @Property(name="process.label", value={"Create Manuscript Process"})})
public class CreateManuscriptWorkflowProcess
implements WorkflowProcess {
private static final String ARTICLE_MIME_TYPE = "text/plain";
private static final String ARTICLE_FILE_EXTENSION = ".txt";
private static final String DEFAULT_PATH = "/libs/media/articles/admin/content/default.txt";
private static final String DEFAULT_BODY = "Article goes here";
private static final String ASSIGNMENT = "assignment";
private static final String TARGET_WORD_COUNT = "targetWordCount";
private static final String BRIEF = "brief";
public void execute(WorkItem item, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {
Asset createdManuscriptAsset;
ResourceResolver resourceResolver = (ResourceResolver)workflowSession.adaptTo(ResourceResolver.class);
String projectPath = (String)item.getWorkflowData().getMetaDataMap().get("project.path", String.class);
if (projectPath == null) {
throw new WorkflowException("Failed to load project path identified by metadata: project.path");
}
Resource projectResource = resourceResolver.getResource(projectPath);
if (projectResource == null || ResourceUtil.isNonExistingResource((Resource)projectResource)) {
throw new WorkflowException("Failed to load project identified by project path " + projectPath);
}
Project project = (Project)projectResource.adaptTo(Project.class);
Resource assetFolder = project.getAssetFolder();
AssetManager assetManager = (AssetManager)resourceResolver.adaptTo(AssetManager.class);
String title = (String)item.getWorkflowData().getMetaDataMap().get("manuscripttitle", String.class);
if (StringUtils.isBlank((CharSequence)title)) {
title = (String)item.getWorkflowData().getMetaDataMap().get("workflowTitle", String.class);
}
String brief = (String)item.getWorkflowData().getMetaDataMap().get("manuscriptbrief", String.class);
String targetWordCount = (String)item.getWorkflowData().getMetaDataMap().get("manuscripttargetwordcount", String.class);
Node target = (Node)assetFolder.adaptTo(Node.class);
try {
String name = JcrUtil.createValidChildName((Node)target, (String)title);
if (target.hasNode(name + ".txt")) {
String leafNodeName;
int i = 0;
do {
leafNodeName = name + String.valueOf(i) + ".txt";
++i;
} while (target.hasNode(leafNodeName));
name = leafNodeName;
} else {
name = name + ".txt";
}
Session session = target.getSession();
String fullManuscriptPath = target.getPath() + "/" + name;
if (session.itemExists("/libs/media/articles/admin/content/default.txt/jcr:content")) {
Node defaultNode = session.getNode("/libs/media/articles/admin/content/default.txt/jcr:content");
createdManuscriptAsset = assetManager.createAsset(fullManuscriptPath, defaultNode.getProperty("jcr:data").getBinary().getStream(), "text/plain", true);
} else {
createdManuscriptAsset = assetManager.createAsset(fullManuscriptPath, (InputStream)new ByteArrayInputStream("Article goes here".getBytes()), "text/plain", true);
}
Node assetNode = (Node)createdManuscriptAsset.adaptTo(Node.class);
Node jcrContent = assetNode.getNode("jcr:content");
Node assignment = jcrContent.addNode("assignment", "nt:unstructured");
assignment.setProperty("brief", brief);
try {
if (targetWordCount == null) {
targetWordCount = "";
}
assignment.setProperty("targetWordCount", Long.parseLong(targetWordCount));
}
catch (NumberFormatException ex) {
assignment.setProperty("targetWordCount", (String)null);
}
Node metadata = jcrContent.getNode("metadata");
metadata.setProperty("dc:title", title);
jcrContent.setProperty("jcr:lastModified", (Calendar)new GregorianCalendar());
assetNode.getSession().save();
}
catch (RepositoryException ex) {
throw new WorkflowException("Could not create Article", (Throwable)ex);
}
if (createdManuscriptAsset != null) {
String newPayloadPath = createdManuscriptAsset.getPath();
WorkflowData data = this.cloneWorkflowDataWithNewPayload(workflowSession, newPayloadPath, item.getWorkflowData());
workflowSession.updateWorkflowData(item.getWorkflow(), data);
try {
Workflow instance = item.getWorkflow();
Method method = instance.getClass().getMethod("setWorkflowData", WorkflowData.class);
if (method != null) {
method.invoke((Object)instance, new Object[]{data});
}
}
catch (Exception e) {
throw new WorkflowException("failed to update in-memory payload", (Throwable)e);
}
}
}
private WorkflowData cloneWorkflowDataWithNewPayload(WorkflowSession wfSession, String contentPath, WorkflowData wfToClone) {
WorkflowData result = wfSession.newWorkflowData("JCR_PATH", (Object)contentPath);
MetaDataMap sourceMap = wfToClone.getMetaDataMap();
MetaDataMap targetMap = result.getMetaDataMap();
for (String key : sourceMap.keySet()) {
targetMap.put((Object)key, sourceMap.get((Object)key));
}
return result;
}
}