ProjectUtils.java
8.43 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.projects.api.Project
* com.adobe.cq.projects.api.ProjectException
* com.adobe.granite.asset.api.Asset
* com.adobe.granite.asset.api.AssetManager
* com.adobe.granite.asset.api.Rendition
* com.day.cq.commons.jcr.JcrUtil
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang3.StringUtils
* org.apache.jackrabbit.commons.JcrUtils
* org.apache.sling.api.resource.ModifiableValueMap
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.projects.impl.util;
import com.adobe.cq.projects.api.Project;
import com.adobe.cq.projects.api.ProjectException;
import com.adobe.granite.asset.api.Asset;
import com.adobe.granite.asset.api.AssetManager;
import com.adobe.granite.asset.api.Rendition;
import com.day.cq.commons.jcr.JcrUtil;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class ProjectUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(ProjectUtils.class);
private static final String PATH_NODENAME_DASHBOARD = "dashboard";
private static final String PATH_NODENAME_GADGETS = "gadgets";
private ProjectUtils() {
}
public static Asset createProjectAsset(Project project, String mimeType, InputStream thumbnailStream, ResourceResolver resourceResolver, String coverAssetName) throws PersistenceException {
AssetManager assetManager = (AssetManager)resourceResolver.adaptTo(AssetManager.class);
return ProjectUtils.createOrReplaceAsset(project, assetManager, coverAssetName, mimeType, thumbnailStream, resourceResolver);
}
public static Asset createOrReplaceAsset(Project project, AssetManager assetManager, String name, String mimeType, InputStream source, ResourceResolver resourceResolver) {
Asset asset;
Resource targetFolder = project.getAssetFolder();
String targetPath = targetFolder.getPath();
if (!targetPath.endsWith("/")) {
targetPath = targetPath + "/";
}
if ((asset = assetManager.getAsset(targetPath = targetPath + name)) == null) {
asset = assetManager.createAsset(targetPath);
} else {
assetManager.removeAsset(asset.getPath());
asset = assetManager.createAsset(asset.getPath());
}
HashMap<String, String> map = new HashMap<String, String>();
if (mimeType != null) {
map.put("rendition.mime", mimeType);
}
asset.setRendition("original", source, map);
return asset;
}
public static void setCoverImage(Resource targetResource, Project project, String mimeType, InputStream stream, String name) {
try {
Asset thumbnailAsset = ProjectUtils.createProjectAsset(project, mimeType, stream, targetResource.getResourceResolver(), name);
ProjectUtils.setStringProperty(targetResource, "coverUrl", thumbnailAsset.getPath());
}
catch (Exception e) {
throw new ProjectException("Failed to update project cover", (Throwable)e);
}
}
public static Resource getCoverImage(Resource resource) {
Resource imageResource;
String coverPath = ProjectUtils.getProjectProperty(resource, "coverUrl", String.class);
if (!StringUtils.isBlank((CharSequence)coverPath) && (imageResource = resource.getResourceResolver().getResource(coverPath)) != null && !ResourceUtil.isNonExistingResource((Resource)imageResource)) {
return imageResource;
}
return null;
}
public static String getStringProperty(Resource resource, String name) {
ValueMap map = (ValueMap)resource.adaptTo(ValueMap.class);
return (String)map.get(name, String.class);
}
public static String getStringProperty(Node node, String name) throws RepositoryException {
Property property = node.getProperty(name);
if (property != null) {
return property.getString();
}
return null;
}
public static void setStringProperty(Resource resource, String name, String value) throws RepositoryException {
Node node = (Node)resource.adaptTo(Node.class);
node.setProperty(name, value);
}
public static void setProjectProperty(Resource project, String name, Object value) throws RepositoryException {
Resource content = project.getChild("jcr:content");
ModifiableValueMap map = (ModifiableValueMap)content.adaptTo(ModifiableValueMap.class);
map.put((Object)name, value);
}
public static <T> T getProjectProperty(Resource project, String property, Class<T> type) {
Resource content = project.getChild("jcr:content");
ValueMap contentVM = (ValueMap)content.adaptTo(ValueMap.class);
return (T)contentVM.get(property, type);
}
public static Resource getGadgetFolder(Resource projectResource) {
Resource projectContent = projectResource.getChild("jcr:content");
Resource dashboardResource = projectContent.getChild("dashboard");
if (dashboardResource == null) {
return null;
}
return dashboardResource.getChild("gadgets");
}
public static Resource getOrCreateGadgetFolder(Resource projectResource) throws PersistenceException {
Resource gadgetResource;
ResourceResolver resolver = projectResource.getResourceResolver();
Resource projectContent = projectResource.getChild("jcr:content");
Resource dashboardResource = projectContent.getChild("dashboard");
if (dashboardResource == null) {
HashMap dashboardResourceProps = new HashMap();
dashboardResource = resolver.create(projectContent, "dashboard", dashboardResourceProps);
}
if ((gadgetResource = dashboardResource.getChild("gadgets")) == null) {
HashMap gadgetsResourceProps = new HashMap();
gadgetResource = resolver.create(dashboardResource, "gadgets", gadgetsResourceProps);
}
return gadgetResource;
}
public static String createFolder(String parentPath, String title, String nameHint, String nodeType, String strThumbnailPath, ResourceResolver resourceResolver) {
String resultingPath = "";
try {
Node parentFolderNode = ((Session)resourceResolver.adaptTo(Session.class)).getNode(parentPath);
String uniqueName = JcrUtil.createValidName((String)nameHint);
Node folderNode = JcrUtils.getOrCreateByPath((Node)parentFolderNode, (String)uniqueName, (boolean)true, (String)nodeType, (String)nodeType, (boolean)false);
resultingPath = folderNode.getPath();
folderNode.setProperty("jcr:title", title);
folderNode.setProperty("sling:resourceType", "cq/gui/components/projects/admin/card/foldercard");
if (!StringUtils.isEmpty((CharSequence)strThumbnailPath)) {
folderNode.setProperty("folderthumbnailurl", strThumbnailPath);
}
}
catch (RepositoryException exception) {
throw new ProjectException("Could not create project folder: " + resultingPath, (Throwable)exception);
}
return resultingPath;
}
public static boolean isEmptyFolder(Resource folderResource) {
Iterator children = folderResource.listChildren();
while (children.hasNext()) {
Resource child = (Resource)children.next();
if (!child.isResourceType("cq/gui/components/projects/admin/card/projectcard") && !child.isResourceType("cq/gui/components/projects/admin/card/foldercard")) continue;
return false;
}
return true;
}
}