View Javadoc
1   /**
2    * This file Copyright (c) 2016-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Displays user a warning message based on the query statement and workspace passed in.
58   *
59   * <p>A warning is displayed <b>only</b> if there is at least one node found by given query.</p>
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       * Default constructor.
73       *
74       * @param name Name of the task
75       * @param description Description of the task
76       * @param workspace Name of the workspace to search in
77       * @param queryStatement Full query statement
78       * @param nodeType The node type of the nodes to aggregate; provide {@code null} to display every item
79       * @param baseMessageFormat The message format to be displayed, make sure to provide it with a {@code %s} placeholder, e.g. {@code "Message start\n%s\nMessage end."}
80       * @param exclusionLists A list of paths to exclude from being mentioned in warning
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 }