FormsManagerImpl.java
5.01 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* 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.day.cq.wcm.foundation.forms.impl;
import com.day.cq.wcm.foundation.forms.FormsManager;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
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 class FormsManagerImpl
implements FormsManager {
private final Logger logger;
private ResourceResolver resolver;
private String[] searchPaths;
protected FormsManagerImpl(ResourceResolver resolver) {
this.logger = LoggerFactory.getLogger(this.getClass());
this.resolver = resolver;
this.searchPaths = resolver.getSearchPath();
for (int i = 0; i < this.searchPaths.length; ++i) {
this.searchPaths[i] = this.searchPaths[i].substring(0, this.searchPaths[i].length() - 1);
}
}
@Override
public Iterator<FormsManager.ComponentDescription> getActions() {
return this.search("foundation/components/form/action").iterator();
}
@Override
public Iterator<FormsManager.ComponentDescription> getConstraints() {
return this.search("foundation/components/form/constraint").iterator();
}
@Override
public String getDialogPathForAction(String resourceType) {
if (resourceType == null) {
return null;
}
for (String path : this.searchPaths) {
String dialogPath = path + '/' + resourceType + "/dialog";
if (this.resolver.getResource(dialogPath) == null) continue;
return dialogPath;
}
return null;
}
private Collection<FormsManager.ComponentDescription> search(String type) {
HashMap<String, ComponentDescriptionImpl> map = new HashMap<String, ComponentDescriptionImpl>();
ArrayList<String> disabledComponents = new ArrayList<String>();
for (String path : this.searchPaths) {
StringBuilder buffer = new StringBuilder("/jcr:root");
buffer.append(path);
buffer.append("//* [@");
buffer.append("sling:resourceType");
buffer.append("='");
buffer.append(type);
buffer.append("']");
this.logger.debug("Query: {}", (Object)buffer.toString());
Iterator i = this.resolver.findResources(buffer.toString(), "xpath");
while (i.hasNext()) {
Resource rsrc = (Resource)i.next();
ValueMap properties = ResourceUtil.getValueMap((Resource)rsrc);
String rt = rsrc.getPath().substring(path.length() + 1);
if (((Boolean)properties.get("enabled", (Object)Boolean.TRUE)).booleanValue()) {
if (map.containsKey(rt) || disabledComponents.contains(rt)) continue;
map.put(rt, new ComponentDescriptionImpl(rt, ResourceUtil.getName((Resource)rsrc), properties));
continue;
}
disabledComponents.add(rt);
}
}
ArrayList<FormsManager.ComponentDescription> entries = new ArrayList<FormsManager.ComponentDescription>(map.values());
Collections.sort(entries);
return entries;
}
public static final class ComponentDescriptionImpl
implements FormsManager.ComponentDescription,
Comparable<FormsManager.ComponentDescription> {
private final String resourceType;
private final String title;
private final String hint;
private final int order;
public ComponentDescriptionImpl(String rt, String defaultName, ValueMap props) {
this.resourceType = rt;
this.title = (String)props.get("jcr:title", (Object)defaultName);
this.order = (Integer)props.get("order", (Object)0);
this.hint = (String)props.get("hint", String.class);
}
@Override
public String getResourceType() {
return this.resourceType;
}
@Override
public String getTitle() {
return this.title;
}
public int getOrder() {
return this.order;
}
@Override
public String getHint() {
return this.hint;
}
@Override
public int compareTo(FormsManager.ComponentDescription o) {
ComponentDescriptionImpl obj = (ComponentDescriptionImpl)o;
if (this.order < obj.order) {
return -1;
}
if (this.order == obj.order) {
return this.title.compareTo(obj.title);
}
return 1;
}
}
}