View Javadoc

1   /**
2    * This file Copyright (c) 2013 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.ui.workbench.thumbnail;
35  
36  import info.magnolia.context.MgnlContext;
37  import info.magnolia.jcr.RuntimeRepositoryException;
38  import info.magnolia.jcr.util.NodeTypes;
39  import info.magnolia.ui.vaadin.integration.contentconnector.JcrContentConnectorDefinition;
40  import info.magnolia.ui.vaadin.integration.contentconnector.NodeTypeDefinition;
41  import info.magnolia.ui.vaadin.integration.jcr.JcrItemId;
42  import info.magnolia.ui.vaadin.integration.jcr.JcrItemUtil;
43  import info.magnolia.ui.workbench.container.AbstractJcrContainer;
44  
45  import java.util.ArrayList;
46  import java.util.List;
47  
48  import javax.jcr.NodeIterator;
49  import javax.jcr.RepositoryException;
50  import javax.jcr.query.Query;
51  import javax.jcr.query.QueryManager;
52  import javax.jcr.query.QueryResult;
53  
54  import org.apache.commons.lang3.StringUtils;
55  import org.slf4j.Logger;
56  import org.slf4j.LoggerFactory;
57  
58  /**
59   * {@link ThumbnailContainer.IdProvider} which queries item ids from JCR workspace.
60   * @see info.magnolia.ui.workbench.thumbnail.JcrThumbnailItemIdProvider#getQueryWhereClauseNodeTypes for details.
61   */
62  public class JcrThumbnailItemIdProvider implements ThumbnailContainer.IdProvider {
63  
64      protected static final String WHERE_TEMPLATE_FOR_PATH = " WHERE (%s) %s ";
65  
66      private Logger log = LoggerFactory.getLogger(getClass());
67  
68      private JcrContentConnectorDefinition definition;
69  
70      public JcrThumbnailItemIdProvider(JcrContentConnectorDefinition definition) {
71          this.definition = definition;
72      }
73  
74      @Override
75      public List<?> getItemIds() {
76          List<JcrItemId> uuids = new ArrayList<JcrItemId>();
77          String workspaceName =  definition.getWorkspace();
78          final String query = constructQuery();
79          try {
80              QueryManager qm = MgnlContext.getJCRSession(workspaceName).getWorkspace().getQueryManager();
81              Query q = qm.createQuery(query, Query.JCR_SQL2);
82  
83              log.debug("Executing query statement [{}] on workspace [{}]", query, workspaceName);
84              long start = System.currentTimeMillis();
85  
86              QueryResult queryResult = q.execute();
87              NodeIterator iter = queryResult.getNodes();
88  
89              while (iter.hasNext()) {
90                  uuids.add(JcrItemUtil.getItemId(iter.nextNode()));
91              }
92  
93              log.debug("Done collecting {} nodes in {}ms", uuids.size(), System.currentTimeMillis() - start);
94  
95          } catch (RepositoryException e) {
96              throw new RuntimeRepositoryException(e);
97          }
98          return uuids;
99      }
100 
101     /**
102      * Hint: could be dropped once this type bases on AbstractJcrContainer as well (BL-153).
103      */
104     protected String getMainNodeType() {
105         final List<NodeTypeDefinition> nodeTypes = definition.getNodeTypes();
106         return nodeTypes.isEmpty() ? AbstractJcrContainer.DEFAULT_NODE_TYPE : nodeTypes.get(0).getName();
107     }
108 
109     protected String prepareSelectQueryStatement() {
110         return String.format("select * from [nt:base] as t ", getMainNodeType());
111     }
112 
113     protected String prepareFilterQueryStatement() {
114         String nodeTypes = getQueryWhereClauseNodeTypes();
115         String path = definition.getRootPath();
116         boolean pathIsNotRoot = StringUtils.isNotBlank(path) && !"/".equals(path);
117         return String.format(WHERE_TEMPLATE_FOR_PATH, nodeTypes, pathIsNotRoot ? " AND ISDESCENDANTNODE('" + path + "')" : "");
118 
119     }
120 
121     protected String prepareOrderQueryStatement() {
122         return " order by name(t)";
123     }
124 
125     /**
126      * @return a String containing the node types to be searched for in a query. All node types declared in a workbench definition are returned
127      * unless their <code>hideInList</code> property is true or they are of type <code>mgnl:folder</code>. E.g. assuming a node types declaration like the following
128      *
129      * <pre>
130      * ...
131      * + workbench
132      *  + nodeTypes
133      *   + foo
134      *    * name = nt:foo
135      *   + bar
136      *    * name = nt:bar
137      *    * hideInList = true
138      *   + baz
139      *    * name = nt:baz
140      * ...
141      * </pre>
142      *
143      * this method will return the following string <code>[jcr:primaryType] = 'nt:foo' or [jcr:primaryType] = 'baz'</code>. This will eventually be used to restrict the node types to be searched for
144      * in list and search views, i.e. <code>select * from [nt:base] where ([jcr:primaryType] = 'nt:foo' or [jcr:primaryType] = 'baz')</code>.
145      */
146     protected String getQueryWhereClauseNodeTypes() {
147         List<String> defs = new ArrayList<String>();
148         for (NodeTypeDefinition type : definition.getNodeTypes()) {
149             if (type.isHideInList() || NodeTypes.Folder.NAME.equals(type.getName())) {
150                 log.debug("Skipping {} node type. Nodes of such type won't be searched for.", type.getName());
151                 continue;
152             }
153             defs.add("[jcr:primaryType] = '" + type.getName() + "'");
154         }
155         return StringUtils.join(defs, " or ");
156     }
157 
158     private String constructQuery() {
159         return prepareSelectQueryStatement() + prepareFilterQueryStatement() + prepareOrderQueryStatement();
160     }
161 }