View Javadoc
1   /**
2    * This file Copyright (c) 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.admincentral.findbar.sniff;
35  
36  import static info.magnolia.periscope.operation.OperationResult.successfulResult;
37  
38  import info.magnolia.admincentral.findbar.PeriscopeAppDetailCreator;
39  import info.magnolia.admincentral.findbar.PeriscopeAppDetailCreator.AppDetail;
40  import info.magnolia.cms.util.QueryUtil;
41  import info.magnolia.periscope.operation.OperationResult;
42  import info.magnolia.periscope.sniff.PatternCommandSniffer;
43  import info.magnolia.ui.api.location.LocationController;
44  import info.magnolia.ui.contentapp.detail.DetailLocation;
45  import info.magnolia.ui.contentapp.detail.DetailView;
46  
47  import java.util.Optional;
48  import java.util.regex.Matcher;
49  import java.util.regex.Pattern;
50  
51  import javax.jcr.Node;
52  import javax.jcr.NodeIterator;
53  import javax.jcr.RepositoryException;
54  import javax.jcr.query.Query;
55  
56  /**
57   * Abstract class which takes care of executing a {@link Query#JCR_SQL2 JCR-SQL2} and returning a request
58   * as a {@link OperationResult}.
59   *
60   * Implementors of this sniff should override {@link #buildQuery(String)} method in order to execute different
61   * {@link Query#JCR_SQL2 JCR-SQL2} queries.
62   */
63  public abstract class AbstractFindCommandSniffer extends PatternCommandSniffer {
64  
65      private final LocationController locationController;
66      private final PeriscopeAppDetailCreator periscopeAppDetailCreator;
67      private final Pattern commandPattern;
68  
69      public AbstractFindCommandSniffer(LocationController locationController, PeriscopeAppDetailCreator periscopeAppDetailCreator, Pattern commandPattern) {
70          this.locationController = locationController;
71          this.periscopeAppDetailCreator = periscopeAppDetailCreator;
72          this.commandPattern = commandPattern;
73      }
74  
75      protected abstract String buildQuery(String content);
76  
77      @Override
78      protected Pattern getPattern() {
79          return commandPattern;
80      }
81  
82      @Override
83      protected OperationResult execute(Matcher matcher) {
84          // As can be seen from the example, users are suppose to search like 'find page about conditions'
85          // Therefore, we have to pluralise 'page' to 'pages' to match with Magnolia standard workspace naming conventions.
86          String appName = matcher.group("appName").toLowerCase();
87          String nodeName = matcher.group("content").toLowerCase();
88  
89          Optional<AppDetail> appDetailOptional = periscopeAppDetailCreator.createAppDetail(appName);
90          if (!appDetailOptional.isPresent()) {
91              return new OperationResult(false, String.format("App configuration cannot be found for app: '%s'.", appName));
92          }
93  
94          String searchQuery = buildQuery(nodeName);
95  
96          try {
97              AppDetail appDetail = appDetailOptional.get();
98              String workspaceName = appDetail.getWorkspace();
99              NodeIterator searchResults = QueryUtil.search(workspaceName, searchQuery, Query.JCR_SQL2);
100             if (!searchResults.hasNext()) {
101                 return new OperationResult(false, "No matching node found.");
102             }
103 
104             Node matchingNode = searchResults.nextNode();
105             String path = matchingNode.getPath();
106 
107             DetailLocation location = new DetailLocation(appDetail.getAppName(), appDetail.getSubAppName(), DetailView.ViewType.VIEW, path, null);
108             locationController.goTo(location);
109             return successfulResult();
110         } catch (RepositoryException e) {
111             return new OperationResult(false, e.getMessage());
112         }
113     }
114 }