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 package info.magnolia.module.delta;
35
36 import static java.util.stream.Collectors.joining;
37
38 import info.magnolia.cms.util.QueryUtil;
39 import info.magnolia.jcr.function.NodeFunctions;
40 import info.magnolia.jcr.util.NodeUtil;
41 import info.magnolia.module.InstallContext;
42
43 import java.util.List;
44 import java.util.function.Function;
45 import java.util.function.Predicate;
46
47 import javax.jcr.Node;
48 import javax.jcr.RepositoryException;
49 import javax.jcr.query.Query;
50
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 import com.google.common.collect.Lists;
55
56
57
58
59
60
61 public class QueryElementsAndDisplayWarningTask extends AbstractRepositoryTask {
62
63 private static final Logger log = LoggerFactory.getLogger(QueryElementsAndDisplayWarningTask.class);
64
65 private final String workspace;
66 private final String queryStatement;
67 private final String nodeType;
68 private final String baseMessageFormat;
69 private final List<String> exclusionLists;
70
71
72
73
74
75
76
77
78
79
80
81
82 public QueryElementsAndDisplayWarningTask(String name, String description, String workspace, String queryStatement, String nodeType, String baseMessageFormat, List<String> exclusionLists) {
83 super(name, description);
84 this.workspace = workspace;
85 this.queryStatement = queryStatement;
86 this.baseMessageFormat = baseMessageFormat;
87 this.exclusionLists = exclusionLists;
88 this.nodeType = nodeType;
89 }
90
91 public QueryElementsAndDisplayWarningTask(String name, String description, String workspace, String queryStatement, String baseMessageFormat, List<String> exclusionLists) {
92 this(name, description, workspace, queryStatement, null, baseMessageFormat, exclusionLists);
93 }
94
95 @SuppressWarnings("unchecked")
96 @Override
97 protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
98 List<Node> nodes = Lists.newArrayList(QueryUtil.search(workspace, queryStatement, Query.JCR_SQL2));
99
100 String matchingPaths = nodes.stream()
101 .map(nodeType == null ? NodeFunctions::toPath : toPathOfNearestAncestorOf(nodeType))
102 .filter(filterPaths(exclusionLists))
103 .distinct()
104 .collect(joining("\n"));
105
106 if (!matchingPaths.isEmpty()) {
107 String message = String.format(baseMessageFormat, matchingPaths);
108 installContext.warn(message);
109 }
110 }
111
112 private Function<Node, String> toPathOfNearestAncestorOf(final String nodeType) {
113 return node -> {
114 try {
115 Node nearestNode = NodeUtil.getNearestAncestorOfType(node, nodeType);
116 return nearestNode != null ? nearestNode.getPath() : node.getPath();
117 } catch (RepositoryException e) {
118 log.warn("An error occurred when trying to get the nearest ancestor of type {} for node {}.", nodeType, node, e);
119 return null;
120 }
121 };
122 }
123
124 private Predicate<String> filterPaths(final List<String> exclusionLists) {
125 return path -> {
126 for (String exclusionPath : exclusionLists) {
127 if (path.equals(exclusionPath)) {
128 return false;
129 }
130 }
131 return true;
132 };
133 }
134 }