ConsoleUtil.java
5.31 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.observation.EventIterator
* javax.jcr.observation.EventListener
* javax.jcr.observation.ObservationManager
* javax.jcr.query.Query
* javax.jcr.query.QueryManager
* javax.jcr.query.QueryResult
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.commons;
import com.day.cq.commons.Console;
import com.day.cq.commons.SlingRepositoryException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.ObservationManager;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Deprecated
public class ConsoleUtil
implements EventListener {
private static final String NT_CONSOLE = "cq:Console";
private static final String PR_CONSOLE_TITLE = "consoleTitle";
private static final String PR_CONSOLE_DESCRIPTION = "consoleDescription";
private static final String PR_VANITY_PATH = "sling:vanityPath";
private static final String QUERY_BASE = "SELECT * FROM ";
private static final String QUERY_CONSOLE = "SELECT * FROM cq:Console";
private static final String CLAUSE_ORDER = " ORDER BY sling:vanityOrder DESC";
private final QueryManager queryManager;
private final String[] queries;
private Collection<Console> consoles;
private static final Logger log = LoggerFactory.getLogger(ConsoleUtil.class);
public ConsoleUtil(QueryManager queryManager, ObservationManager observationManger, String[] searchPath) {
this.queryManager = queryManager;
this.queries = new String[searchPath.length];
if (searchPath.length > 0) {
for (int i = 0; i < searchPath.length; ++i) {
StringBuffer whereClaus = new StringBuffer(" WHERE ");
String path = searchPath[i];
whereClaus.append(" jcr:path LIKE '").append(path);
if (!path.endsWith("/")) {
whereClaus.append("/");
}
whereClaus.append("%'");
this.queries[i] = "SELECT * FROM cq:Console" + whereClaus.toString() + " ORDER BY sling:vanityOrder DESC";
}
}
}
@Deprecated
public void dispose() {
}
public Collection<Console> getPaths() {
if (this.consoles == null) {
try {
this.consoles = this.collectConsoles();
}
catch (RepositoryException e) {
log.error("Failed to build paths to Console: {}", (Throwable)e);
throw new SlingRepositoryException(e.getMessage(), e);
}
}
return Collections.unmodifiableCollection(this.consoles);
}
public void onEvent(EventIterator eventIterator) {
}
private Collection<Console> collectConsoles() throws RepositoryException {
ArrayList<String> relativePaths = new ArrayList<String>();
ArrayList<Console> consoles = new ArrayList<Console>();
for (String query : this.queries) {
Query q = this.queryManager.createQuery(query, "sql");
NodeIterator itr = q.execute().getNodes();
while (itr.hasNext()) {
Node node = itr.nextNode();
String path = node.getPath();
String appName = null;
if (node.hasProperty("consoleTitle")) {
appName = node.getProperty("consoleTitle").getString();
}
if (appName == null && node.hasProperty("jcr:title")) {
appName = node.getProperty("jcr:title").getString();
}
if (appName == null) {
appName = node.getName();
}
String appDescription = null;
if (node.hasProperty("consoleDescription")) {
appDescription = node.getProperty("consoleDescription").getString();
}
if (appDescription == null && node.hasProperty("jcr:description")) {
appDescription = node.getProperty("jcr:description").getString();
}
if (appDescription == null) {
appDescription = "";
}
String iconClass = null;
if (node.hasProperty("iconClass")) {
iconClass = node.getProperty("iconClass").getString();
}
String vanityPath = null;
if (node.hasProperty("sling:vanityPath")) {
vanityPath = node.getProperty("sling:vanityPath").getString();
}
String relativePath = path.substring(1);
if (relativePaths.contains(relativePath = relativePath.substring(relativePath.indexOf("/") + 1))) continue;
consoles.add(new Console(appName, appDescription, iconClass, path, vanityPath));
relativePaths.add(relativePath);
}
}
return consoles;
}
}