TallyOperationExtensionImpl.java
4.11 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.social.scf.Operation
* com.adobe.cq.social.scf.OperationException
* com.adobe.cq.social.scf.SocialComponent
* com.adobe.cq.social.tally.client.api.RatingSocialComponent
* com.adobe.cq.social.tally.client.api.TallyOperationExtension
* com.adobe.cq.social.tally.client.api.TallyOperationExtension$TallyOperation
* com.adobe.cq.social.tally.client.api.TallySocialComponent
* com.adobe.cq.social.ugcbase.SocialUtils
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.commons.rating.impl;
import com.adobe.cq.social.scf.Operation;
import com.adobe.cq.social.scf.OperationException;
import com.adobe.cq.social.scf.SocialComponent;
import com.adobe.cq.social.tally.client.api.RatingSocialComponent;
import com.adobe.cq.social.tally.client.api.TallyOperationExtension;
import com.adobe.cq.social.tally.client.api.TallySocialComponent;
import com.adobe.cq.social.ugcbase.SocialUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Service
@Component
public class TallyOperationExtensionImpl
implements TallyOperationExtension {
private static final Logger LOG = LoggerFactory.getLogger(TallyOperationExtensionImpl.class);
private final String NAME = "assets.rating";
public List<TallyOperationExtension.TallyOperation> getOperationsToHookInto() {
ArrayList<TallyOperationExtension.TallyOperation> operations = new ArrayList<TallyOperationExtension.TallyOperation>();
operations.add(TallyOperationExtension.TallyOperation.SET);
operations.add(TallyOperationExtension.TallyOperation.UNSET);
return operations;
}
public void afterAction(Operation operation, Session session, TallySocialComponent tally, Map<String, Object> map) throws OperationException {
if (!map.containsKey("tallyGenerator") || !((String)map.get("tallyGenerator")).equals("assets")) {
return;
}
if (!(tally instanceof RatingSocialComponent)) {
return;
}
RatingSocialComponent rating = (RatingSocialComponent)tally;
SocialUtils socialutils = (SocialUtils)rating.getSourceComponent().getResource().getResourceResolver().adaptTo(SocialUtils.class);
Resource tallyTarget = rating.getSourceComponent().getResource();
Resource assetUGCResource = tallyTarget.getParent();
if (assetUGCResource == null) {
LOG.info("Could not get parent of source component for rating: {}", (Object)tallyTarget.getPath());
return;
}
String assetPath = socialutils.UGCToResourcePath(assetUGCResource);
try {
if (!session.itemExists(assetPath)) {
LOG.info("Could not get asset node for: {}", (Object)assetPath);
return;
}
Node asset = session.getNode(assetPath);
asset.getNode("jcr:content").setProperty("averageRating", (double)rating.getAverageRating().floatValue());
session.save();
}
catch (RepositoryException e) {
throw new OperationException("Error in setting property for the node", (Throwable)e, -1);
}
}
public void beforeAction(Operation operation, Session session, Resource resource, Map<String, Object> map) throws OperationException {
}
public String getName() {
this.getClass();
return "assets.rating";
}
public int getOrder() {
return Integer.MAX_VALUE;
}
}