View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.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   *
61   * @see JcrThumbnailItemIdProvider#getQueryWhereClauseNodeTypes for details.
62   * @see JcrThumbnailContainer
63   * @deprecated since 5.3.10 should be avoided since it does not lazy load the items.
64   */
65  @Deprecated
66  public class JcrThumbnailItemIdProvider implements ThumbnailContainer.IdProvider {
67  
68      protected static final String WHERE_TEMPLATE_FOR_PATH = " WHERE (%s) %s ";
69  
70      private Logger log = LoggerFactory.getLogger(getClass());
71  
72      private JcrContentConnectorDefinition definition;
73  
74      public JcrThumbnailItemIdProvider(JcrContentConnectorDefinition definition) {
75          this.definition = definition;
76      }
77  
78      @Override
79      public List<?> getItemIds() {
80          List<JcrItemId> uuids = new ArrayList<JcrItemId>();
81          String workspaceName =  definition.getWorkspace();
82          final String query = constructQuery();
83          try {
84              QueryManager qm = MgnlContext.getJCRSession(workspaceName).getWorkspace().getQueryManager();
85              Query q = qm.createQuery(query, Query.JCR_SQL2);
86  
87              log.debug("Executing query statement [{}] on workspace [{}]", query, workspaceName);
88              long start = System.currentTimeMillis();
89  
90              QueryResult queryResult = q.execute();
91              NodeIterator iter = queryResult.getNodes();
92  
93              while (iter.hasNext()) {
94                  uuids.add(JcrItemUtil.getItemId(iter.nextNode()));
95              }
96  
97              log.debug("Done collecting {} nodes in {}ms", uuids.size(), System.currentTimeMillis() - start);
98  
99          } catch (RepositoryException e) {
100             throw new RuntimeRepositoryException(e);
101         }
102         return uuids;
103     }
104 
105     /**
106      * Hint: could be dropped once this type bases on AbstractJcrContainer as well (BL-153).
107      */
108     protected String getMainNodeType() {
109         final List<NodeTypeDefinition> nodeTypes = definition.getNodeTypes();
110         return nodeTypes.isEmpty() ? AbstractJcrContainer.DEFAULT_NODE_TYPE : nodeTypes.get(0).getName();
111     }
112 
113     protected String prepareSelectQueryStatement() {
114         return String.format("select * from [nt:base] as t ", getMainNodeType());
115     }
116 
117     protected String prepareFilterQueryStatement() {
118         String nodeTypes = getQueryWhereClauseNodeTypes();
119         String path = definition.getRootPath();
120         boolean pathIsNotRoot = StringUtils.isNotBlank(path) && !"/".equals(path);
121         return String.format(WHERE_TEMPLATE_FOR_PATH, nodeTypes, pathIsNotRoot ? " AND ISDESCENDANTNODE('" + path + "')" : "");
122 
123     }
124 
125     protected String prepareOrderQueryStatement() {
126         return " order by name(t)";
127     }
128 
129     /**
130      * @return a String containing the node types to be searched for in a query. All node types declared in a workbench definition are returned
131      * 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
132      *
133      * <pre>
134      * ...
135      * + workbench
136      *  + nodeTypes
137      *   + foo
138      *    * name = nt:foo
139      *   + bar
140      *    * name = nt:bar
141      *    * hideInList = true
142      *   + baz
143      *    * name = nt:baz
144      * ...
145      * </pre>
146      *
147      * 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
148      * in list and search views, i.e. <code>select * from [nt:base] where ([jcr:primaryType] = 'nt:foo' or [jcr:primaryType] = 'baz')</code>.
149      */
150     protected String getQueryWhereClauseNodeTypes() {
151         List<String> defs = new ArrayList<String>();
152         for (NodeTypeDefinition type : definition.getNodeTypes()) {
153             if (type.isHideInList() || NodeTypes.Folder.NAME.equals(type.getName())) {
154                 log.debug("Skipping {} node type. Nodes of such type won't be searched for.", type.getName());
155                 continue;
156             }
157             defs.add("[jcr:primaryType] = '" + type.getName() + "'");
158         }
159         return StringUtils.join(defs, " or ");
160     }
161 
162     private String constructQuery() {
163         return prepareSelectQueryStatement() + prepareFilterQueryStatement() + prepareOrderQueryStatement();
164     }
165 }