SchemaFormHelper.java
9.04 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.confmgr.Conf
* com.day.text.Text
* javax.jcr.RepositoryException
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.request.RequestPathInfo
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.tenant.Tenant
*/
package com.day.cq.dam.commons.util;
import com.adobe.granite.confmgr.Conf;
import com.day.cq.dam.commons.schemaforms.internal.TabList;
import com.day.text.Text;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.jcr.RepositoryException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.tenant.Tenant;
public class SchemaFormHelper {
@Deprecated
public static List<Resource> getMasterForms(Resource currentForm, String formsBaseDirPath) throws RepositoryException {
ResourceResolver resolver = currentForm.getResourceResolver();
String currentResourcePath = currentForm.getPath();
String appsDIr = SchemaFormHelper.getAppsDir(resolver);
String APPS_BASE_DIR = appsDIr + "/" + formsBaseDirPath;
String APPS_BASE_DIR_NON_TENANT = "/apps/" + formsBaseDirPath;
String LIBS_BASE_DIR = "/libs/" + formsBaseDirPath;
ArrayList<Resource> masterFormsList = new ArrayList<Resource>();
String path = currentResourcePath;
Resource res = currentForm;
while (!(path.equals(APPS_BASE_DIR) || path.equals(LIBS_BASE_DIR) || path.equals(APPS_BASE_DIR_NON_TENANT))) {
path = (res = res.getParent()).getPath();
String relativePath = path.startsWith("/libs") ? path.substring("/libs".length()) : (path.startsWith(appsDIr) ? path.substring(appsDIr.length()) : path.substring("/apps".length()));
Resource resToVerify = resolver.getResource(appsDIr + relativePath);
if (resToVerify != null && resToVerify.getChild("items/tabs") != null) {
masterFormsList.add(resToVerify);
continue;
}
resToVerify = resolver.getResource("/apps" + relativePath);
if (resToVerify != null && resToVerify.getChild("items/tabs") != null) {
masterFormsList.add(resToVerify);
continue;
}
resToVerify = resolver.getResource("/libs" + relativePath);
if (resToVerify == null) continue;
masterFormsList.add(resToVerify);
}
Collections.reverse(masterFormsList);
return masterFormsList;
}
public static String[] getBaseFormPaths(ResourceResolver resourceResolver) {
Tenant tenant = (Tenant)resourceResolver.adaptTo(Tenant.class);
String schemaExtHome = null != tenant ? (String)tenant.getProperty("metadataschema.home") : "/conf/global/settings/dam/adminui-extension/metadataschema";
String schemaHome = "/libs/dam/content/schemaeditors/forms";
Resource schemaExtHomeRes = resourceResolver.getResource(schemaExtHome);
if (null == schemaExtHome || schemaExtHome.trim().isEmpty() || schemaExtHomeRes == null) {
schemaExtHome = "/conf/global/settings/dam/adminui-extension/metadataschema";
} else {
Conf conf = (Conf)schemaExtHomeRes.adaptTo(Conf.class);
schemaHome = (String)conf.getItem("dam/adminui").get("metadataschema.home", (Object)"/libs/dam/content/schemaeditors/forms");
}
return new String[]{schemaExtHome, schemaHome};
}
private static String getRelativeFormPath(String formPath, String[] baseFormPaths) {
for (String baseFormPath : baseFormPaths) {
if (!formPath.startsWith(baseFormPath)) continue;
String relativePath = formPath.substring(baseFormPath.length());
relativePath = relativePath.startsWith("/") ? relativePath : relativePath + "/";
relativePath = relativePath.endsWith("/") ? relativePath.substring(relativePath.length() - 1) : relativePath;
return relativePath;
}
return formPath;
}
public static List<Resource> getMasterForms(Resource currentForm) throws RepositoryException {
ResourceResolver resourceResolver = currentForm.getResourceResolver();
String[] baseFormPaths = SchemaFormHelper.getBaseFormPaths(resourceResolver);
String relativePath = SchemaFormHelper.getRelativeFormPath(currentForm.getPath(), baseFormPaths);
ArrayList<Resource> masterFormsList = new ArrayList<Resource>();
String string = relativePath = relativePath.trim().isEmpty() ? "" : relativePath.substring(0, relativePath.lastIndexOf(47));
while (!relativePath.trim().isEmpty()) {
for (int i = 0; i < baseFormPaths.length; ++i) {
String baseFormPath = baseFormPaths[i];
Resource resToVerify = resourceResolver.getResource(baseFormPath + relativePath);
if (resToVerify == null || i != baseFormPaths.length - 1 && resToVerify.getChild("items/tabs") == null) continue;
masterFormsList.add(resToVerify);
break;
}
relativePath = relativePath.trim().isEmpty() ? "" : relativePath.substring(0, relativePath.lastIndexOf(47));
}
Collections.reverse(masterFormsList);
return masterFormsList;
}
public static Resource mergeFormTabResource(Resource oneTabList, Resource otherTabList) {
if (oneTabList == null) {
return otherTabList;
}
if (otherTabList == null) {
return oneTabList;
}
TabList aTab = new TabList(oneTabList);
TabList oTab = new TabList(otherTabList);
aTab.merge((Resource)oTab);
return aTab;
}
public static Resource getSchemaResource(SlingHttpServletRequest request) {
String[] baseFormPaths;
String suffix = request.getRequestPathInfo().getSuffix();
suffix = suffix == null ? "" : suffix;
ResourceResolver resourceResolver = request.getResourceResolver();
for (String baseFormPath : baseFormPaths = SchemaFormHelper.getBaseFormPaths(resourceResolver)) {
Resource res = resourceResolver.getResource(baseFormPath + suffix);
if (null == res) continue;
return res;
}
return null;
}
private static String getAppsDir(ResourceResolver resolver) {
String appsDir = "/apps";
Tenant tenant = (Tenant)resolver.adaptTo(Tenant.class);
if (tenant != null) {
appsDir = appsDir + "/" + tenant.getId();
}
return appsDir;
}
public static Iterator getSchemaFormsIterator(SlingHttpServletRequest request, int rows, int offset) {
String resName;
ArrayList<Resource> resourceList = new ArrayList<Resource>();
ResourceResolver resolver = request.getResourceResolver();
String[] baseFormPaths = SchemaFormHelper.getBaseFormPaths(resolver);
String suffix = request.getRequestPathInfo().getSuffix();
suffix = null == suffix ? "" : suffix;
suffix = SchemaFormHelper.getRelativeFormPath(suffix, baseFormPaths);
String appsResPath = baseFormPaths[0] + suffix;
String libsResPath = baseFormPaths[1] + suffix;
Resource appRes = resolver.getResource(appsResPath);
Resource libsRes = resolver.getResource(libsResPath);
if (appRes != null) {
Iterator appIter = appRes.listChildren();
while (appIter.hasNext()) {
Resource ar = (Resource)appIter.next();
resName = Text.getName((String)ar.getPath());
if (libsRes != null && libsRes.getChild(resName) != null || !ar.isResourceType("nt:folder") && !ar.isResourceType("sling:OrderedFolder") && !ar.isResourceType("sling:Folder")) continue;
resourceList.add(ar);
}
}
if (libsRes != null) {
Iterator libsIter = libsRes.listChildren();
while (libsIter.hasNext()) {
Resource lr = (Resource)libsIter.next();
resName = Text.getName((String)lr.getPath());
if (appRes != null && appRes.getChild(resName) != null && appRes.getChild(resName).getChild("items/tabs") != null) {
Resource ar = resolver.getResource(appRes.getPath() + "/" + resName);
if (!ar.isResourceType("nt:folder") && !ar.isResourceType("sling:OrderedFolder") && !ar.isResourceType("sling:Folder")) continue;
resourceList.add(ar);
continue;
}
if ((lr.getChild("items/tabs") == null || !lr.isResourceType("nt:folder")) && !lr.isResourceType("sling:OrderedFolder") && !lr.isResourceType("sling:Folder")) continue;
resourceList.add(lr);
}
}
return resourceList.iterator();
}
}