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