FormReplicationScheduler.java
6.14 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Session
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.event.jobs.Job
* org.apache.sling.event.jobs.JobManager
* org.apache.sling.event.jobs.consumer.JobConsumer
* org.apache.sling.event.jobs.consumer.JobConsumer$JobResult
* org.apache.sling.jcr.api.SlingRepository
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.aem.formsndocuments.scheduler;
import com.adobe.aem.formsndocuments.publish.PublishService;
import com.adobe.aem.formsndocuments.transferobjects.AssetInfo;
import com.adobe.aem.formsndocuments.util.FMUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
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.ResourceResolverFactory;
import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.JobManager;
import org.apache.sling.event.jobs.consumer.JobConsumer;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, immediate=1, label="FormReplicationScheduler")
@Service(value={JobConsumer.class})
@Property(name="job.topics", value={"com/adobe/aem/formsndocuments/scheduler/formreplication"})
public class FormReplicationScheduler
implements JobConsumer {
private final Logger log = LoggerFactory.getLogger(FormReplicationScheduler.class);
@Reference
private SlingRepository repository = null;
@Reference(referenceInterface=ResourceResolverFactory.class)
private ResourceResolverFactory resourceResolverFactory;
@Reference(referenceInterface=PublishService.class)
private PublishService publishService;
@Reference
private JobManager jobManager;
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public JobConsumer.JobResult process(Job job) {
String replicationAtribute;
String formPath;
this.log.debug("Processing Replication Job with id {}", (Object)job.getId());
formPath = (String)job.getProperty("event.form.path");
replicationAtribute = (String)job.getProperty("event.replication.attribute");
Session jobSession = null;
ResourceResolver resourceResolver = null;
try {
jobSession = FMUtils.getFnDServiceUserSession(this.repository);
if ("activationDate".equals(replicationAtribute)) {
resourceResolver = FMUtils.getResourceResolver(this.resourceResolverFactory, jobSession);
this.log.info("Executing timed topic event to activate the form " + formPath + " and its related assets.");
Set<AssetInfo> assetsToPublish = this.publishService.getRelatedAssetsToPublish(resourceResolver, formPath);
ArrayList<String> permittedAssetsToPublish = new ArrayList<String>();
boolean publishPermission = true;
for (AssetInfo formInfo : assetsToPublish) {
Resource resource = formInfo.getResource();
if (!FMUtils.canReplicate(resource.getPath(), jobSession)) {
this.log.error("User does not have permissions to publish asset:" + resource.getPath());
publishPermission = false;
}
permittedAssetsToPublish.add(resource.getPath());
}
if (publishPermission) {
this.publishService.publish(jobSession, permittedAssetsToPublish);
}
} else if ("expiryDate".equals(replicationAtribute)) {
this.log.info("Executing timed topic event to deactivate the form " + formPath);
this.publishService.deactivate(jobSession, Collections.singletonList(formPath));
}
}
catch (Exception e) {
this.log.error("Exception occurred while carrying out scheduled activation/deactivation of form : " + formPath, (Throwable)e);
}
finally {
if (resourceResolver != null && resourceResolver.isLive()) {
resourceResolver.close();
}
if (jobSession != null) {
jobSession.logout();
}
}
FMUtils.unscheduleJob(this.jobManager, formPath, replicationAtribute);
return JobConsumer.JobResult.OK;
}
protected void bindRepository(SlingRepository slingRepository) {
this.repository = slingRepository;
}
protected void unbindRepository(SlingRepository slingRepository) {
if (this.repository == slingRepository) {
this.repository = null;
}
}
protected void bindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resourceResolverFactory = resourceResolverFactory;
}
protected void unbindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resourceResolverFactory == resourceResolverFactory) {
this.resourceResolverFactory = null;
}
}
protected void bindPublishService(PublishService publishService) {
this.publishService = publishService;
}
protected void unbindPublishService(PublishService publishService) {
if (this.publishService == publishService) {
this.publishService = null;
}
}
protected void bindJobManager(JobManager jobManager) {
this.jobManager = jobManager;
}
protected void unbindJobManager(JobManager jobManager) {
if (this.jobManager == jobManager) {
this.jobManager = null;
}
}
}