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 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   * Displays user a warning message based on the query statement and workspace passed in.
62   *
63   * <p>A warning is displayed <b>only</b> if there is at least one node found by given query.</p>
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       * Default constructor.
77       *
78       * @param name Name of the task
79       * @param description Description of the task
80       * @param workspace Name of the workspace to search in
81       * @param queryStatement Full query statement
82       * @param nodeType The node type of the nodes to aggregate; provide {@code null} to display every item
83       * @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."}
84       * @param exclusionLists A list of paths to exclude from being mentioned in warning
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 }