AssetIDProvider.java
5.26 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* aQute.bnd.annotation.ProviderType
* com.day.cq.dam.api.Asset
* com.day.cq.dam.commons.util.DamUtil
* javax.jcr.Node
* javax.jcr.PathNotFoundException
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Workspace
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.jcr.api.SlingRepository
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.core.impl;
import aQute.bnd.annotation.ProviderType;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.commons.util.DamUtil;
import java.util.Calendar;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
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.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(immediate=1)
@Service(value={AssetIDProvider.class})
@ProviderType
public class AssetIDProvider {
private static final Logger log = LoggerFactory.getLogger(AssetIDProvider.class);
private static final String ASSET_ID_HELPER_SUBSERVICE = "assetidhelper";
private static final String DAM_COPY_TIMESTAMP = "dam:copiedAt";
@Reference
private SlingRepository repository;
public String getAssetID(Asset asset) throws PathNotFoundException, RepositoryException {
try {
return ((Node)asset.adaptTo(Node.class)).getProperty("jcr:uuid").getString();
}
catch (PathNotFoundException e) {
log.error("Error while getting Asset ID for asset at {}", (Object)asset.getPath(), (Object)e);
throw e;
}
catch (RepositoryException e) {
log.error("Error while getting Asset ID for asset at {}", (Object)asset.getPath(), (Object)e);
throw e;
}
}
public void establishParentage(Asset asset) {
String relPathFromJcrContent = "";
String assetsRoot = DamUtil.getTenantAssetsRoot((Resource)((Resource)asset.adaptTo(Resource.class)));
try {
Node assetNode = (Node)asset.adaptTo(Node.class);
Session userSession = assetNode.getSession();
Node assetJcrContent = null;
assetJcrContent = assetNode.getNode("jcr:content");
try {
relPathFromJcrContent = assetJcrContent.getProperty("dam:relativePath").getString();
}
catch (PathNotFoundException e) {
relPathFromJcrContent = "";
}
String assetRelPath = DamUtil.findRelativePathOfAssetNode((Node)assetNode, (String)assetsRoot);
if (!StringUtils.equals((String)assetRelPath, (String)relPathFromJcrContent)) {
String parentAssetID = this.findParentAsset(userSession, assetsRoot, relPathFromJcrContent);
this.updateAssetsRelativePath(userSession, assetJcrContent, assetRelPath, parentAssetID);
}
}
catch (Exception e) {
log.warn("Couldn't establish parentage for asset at {}", (Object)asset.getPath());
}
}
private String findParentAsset(Session userSession, String assetsRoot, String assetRelPathFromJcrContent) throws RepositoryException {
String assetPathFromJcrContent = assetsRoot + "/" + assetRelPathFromJcrContent;
Node parentAssetNode = null;
try {
parentAssetNode = userSession.getNode(assetPathFromJcrContent);
return parentAssetNode.getProperty("jcr:uuid").getString();
}
catch (PathNotFoundException ign) {
return "";
}
}
private void updateAssetsRelativePath(Session userSession, Node assetJcrContent, String assetRelPath, String parentAssetID) {
try {
Session serviceSession = this.repository.loginService("assetidhelper", userSession.getWorkspace().getName());
Node jcrContentForWrite = serviceSession.getNode(assetJcrContent.getPath());
jcrContentForWrite.setProperty("dam:relativePath", assetRelPath);
if (!parentAssetID.isEmpty()) {
jcrContentForWrite.setProperty("dam:parentAssetID", parentAssetID);
jcrContentForWrite.setProperty("dam:copiedAt", Calendar.getInstance());
}
serviceSession.save();
serviceSession.logout();
userSession.refresh(true);
}
catch (RepositoryException unauthorizedException) {
log.error("Session doesn't have appropriate privileges", (Throwable)unauthorizedException);
}
}
protected void bindRepository(SlingRepository slingRepository) {
this.repository = slingRepository;
}
protected void unbindRepository(SlingRepository slingRepository) {
if (this.repository == slingRepository) {
this.repository = null;
}
}
}