View Javadoc

1   /**
2    * This file Copyright (c) 2011-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.templating.functions;
35  
36  import info.magnolia.cms.beans.config.ServerConfiguration;
37  import info.magnolia.cms.core.AggregationState;
38  import info.magnolia.cms.core.MetaData;
39  import info.magnolia.cms.core.MgnlNodeType;
40  import info.magnolia.cms.core.NodeData;
41  import info.magnolia.cms.i18n.I18nContentSupportFactory;
42  import info.magnolia.cms.util.ContentUtil;
43  import info.magnolia.cms.util.QueryUtil;
44  import info.magnolia.cms.util.SiblingsHelper;
45  import info.magnolia.jcr.inheritance.InheritanceNodeWrapper;
46  import info.magnolia.jcr.util.ContentMap;
47  import info.magnolia.jcr.util.NodeUtil;
48  import info.magnolia.jcr.util.PropertyUtil;
49  import info.magnolia.jcr.util.SessionUtil;
50  import info.magnolia.jcr.wrapper.HTMLEscapingNodeWrapper;
51  import info.magnolia.link.LinkUtil;
52  import info.magnolia.objectfactory.Components;
53  import info.magnolia.rendering.template.configured.ConfiguredInheritance;
54  import info.magnolia.repository.RepositoryConstants;
55  import info.magnolia.templating.inheritance.DefaultInheritanceContentDecorator;
56  
57  import java.util.ArrayList;
58  import java.util.Collection;
59  import java.util.List;
60  
61  import javax.inject.Inject;
62  import javax.inject.Provider;
63  import javax.jcr.Node;
64  import javax.jcr.PathNotFoundException;
65  import javax.jcr.Property;
66  import javax.jcr.PropertyType;
67  import javax.jcr.RepositoryException;
68  
69  import org.apache.commons.lang.StringUtils;
70  import org.slf4j.Logger;
71  import org.slf4j.LoggerFactory;
72  
73  /**
74   * An object exposing several methods useful for templates. It is exposed in templates as <code>cmsfn</code>.
75   *
76   * @version $Id$
77   */
78  public class TemplatingFunctions {
79  
80      private static final Logger log = LoggerFactory.getLogger(TemplatingFunctions.class);
81  
82      private final Provider<AggregationState> aggregationStateProvider;
83  
84      //TODO: To review with Philipp. Should not use Provider, but has deep impact on CategorizationSupportImpl PageSyndicator CategorySyndicator....
85      @Inject
86      public TemplatingFunctions(Provider<AggregationState> aggregationStateProvider) {
87          this.aggregationStateProvider = aggregationStateProvider;
88      }
89  
90  
91      public Node asJCRNode(ContentMap contentMap) {
92          return contentMap == null ? null : contentMap.getJCRNode();
93      }
94  
95      public ContentMap asContentMap(Node content) {
96          return content == null ? null : new ContentMap(content);
97      }
98  
99      public List<Node> children(Node content) throws RepositoryException {
100         return content == null ? null : asNodeList(NodeUtil.getNodes(content, NodeUtil.EXCLUDE_META_DATA_FILTER));
101     }
102 
103     public List<Node> children(Node content, String nodeTypeName) throws RepositoryException {
104         return content == null ? null : asNodeList(NodeUtil.getNodes(content, nodeTypeName));
105     }
106 
107     public List<ContentMap> children(ContentMap content) throws RepositoryException {
108         return content == null ? null : asContentMapList(NodeUtil.getNodes(asJCRNode(content), NodeUtil.EXCLUDE_META_DATA_FILTER));
109     }
110 
111     public List<ContentMap> children(ContentMap content, String nodeTypeName) throws RepositoryException {
112         return content == null ? null : asContentMapList(NodeUtil.getNodes(asJCRNode(content), nodeTypeName));
113     }
114 
115     public ContentMap root(ContentMap contentMap) throws RepositoryException {
116         return contentMap == null ? null : asContentMap(this.root(contentMap.getJCRNode()));
117     }
118 
119     public ContentMap root(ContentMap contentMap, String nodeTypeName) throws RepositoryException {
120         return contentMap == null ? null : asContentMap(this.root(contentMap.getJCRNode(), nodeTypeName));
121     }
122 
123     public Node root(Node content) throws RepositoryException {
124         return this.root(content, null);
125     }
126 
127     public Node root(Node content, String nodeTypeName) throws RepositoryException {
128         if (content == null) {
129             return null;
130         }
131         if (nodeTypeName == null) {
132             return (Node) content.getAncestor(0);
133         }
134         if (isRoot(content) && content.isNodeType(nodeTypeName)) {
135             return content;
136         }
137 
138         Node parentNode = this.parent(content, nodeTypeName);
139         while (parent(parentNode, nodeTypeName) != null) {
140             parentNode = this.parent(parentNode, nodeTypeName);
141         }
142         return parentNode;
143     }
144 
145     public ContentMap parent(ContentMap contentMap) throws RepositoryException {
146         return contentMap == null ? null : asContentMap(this.parent(contentMap.getJCRNode()));
147     }
148 
149     public ContentMap parent(ContentMap contentMap, String nodeTypeName) throws RepositoryException {
150         return contentMap == null ? null : asContentMap(this.parent(contentMap.getJCRNode(), nodeTypeName));
151     }
152 
153     public Node parent(Node content) throws RepositoryException {
154         return this.parent(content, null);
155     }
156 
157     public Node parent(Node content, String nodeTypeName) throws RepositoryException {
158         if (content == null) {
159             return null;
160         }
161         if (isRoot(content)) {
162             return null;
163         }
164         if (nodeTypeName == null) {
165             return content.getParent();
166         }
167         Node parent = content.getParent();
168         while (!parent.isNodeType(nodeTypeName)) {
169             if (isRoot(parent)) {
170                 return null;
171             }
172             parent = parent.getParent();
173         }
174         return parent;
175     }
176 
177     /**
178      * Returns the page's {@link ContentMap} of the passed {@link ContentMap}. If the passed {@link ContentMap} represents a page, the passed {@link ContentMap} will be returned.
179      * If the passed {@link ContentMap} has no parent page at all, null is returned.
180      *
181      * @param content the {@link ContentMap} to get the page's {@link ContentMap} from.
182      * @return returns the page {@link ContentMap} of the passed content {@link ContentMap}.
183      * @throws RepositoryException
184      */
185     public ContentMap page(ContentMap content) throws RepositoryException {
186         return content == null ? null : asContentMap(page(content.getJCRNode()));
187     }
188 
189     /**
190      * Returns the page {@link Node} of the passed node. If the passed {@link Node} is a page, the passed {@link Node} will be returned.
191      * If the passed Node has no parent page at all, null is returned.
192      *
193      * @param content the {@link Node} to get the page from.
194      * @return returns the page {@link Node} of the passed content {@link Node}.
195      * @throws RepositoryException
196      */
197     public Node page(Node content) throws RepositoryException {
198         if (content == null) {
199             return null;
200         }
201         if (content.isNodeType(MgnlNodeType.NT_PAGE)) {
202             return content;
203         }
204         return parent(content, MgnlNodeType.NT_PAGE);
205     }
206 
207     public List<ContentMap> ancestors(ContentMap contentMap) throws RepositoryException {
208         return ancestors(contentMap, null);
209     }
210 
211     public List<ContentMap> ancestors(ContentMap contentMap, String nodeTypeName) throws RepositoryException {
212         List<Node> ancestorsAsNodes = this.ancestors(contentMap.getJCRNode(), nodeTypeName);
213         return asContentMapList(ancestorsAsNodes);
214     }
215 
216     public List<Node> ancestors(Node content) throws RepositoryException {
217         return content == null ? null : this.ancestors(content, null);
218     }
219 
220     public List<Node> ancestors(Node content, String nodeTypeName) throws RepositoryException {
221         if (content == null) {
222             return null;
223         }
224         List<Node> ancestors = new ArrayList<Node>();
225         int depth = content.getDepth();
226         for (int i = 1; i < depth; ++i) {
227             Node possibleAncestor = (Node) content.getAncestor(i);
228             if (nodeTypeName == null) {
229                 ancestors.add(possibleAncestor);
230             } else {
231                 if (possibleAncestor.isNodeType(nodeTypeName)) {
232                     ancestors.add(possibleAncestor);
233                 }
234             }
235         }
236         return ancestors;
237     }
238 
239     public Node inherit(Node content) throws RepositoryException {
240         return inherit(content, null);
241     }
242 
243     public Node inherit(Node content, String relPath) throws RepositoryException {
244         if (content == null) {
245             return null;
246         }
247         Node inheritedNode = wrapForInheritance(content);
248 
249         if (StringUtils.isBlank(relPath)) {
250             return inheritedNode;
251         }
252 
253         try {
254             Node subNode = inheritedNode.getNode(relPath);
255             return NodeUtil.unwrap(subNode);
256         } catch (PathNotFoundException e) {
257             // TODO fgrilli: rethrow exception?
258         }
259         return null;
260     }
261 
262     public ContentMap inherit(ContentMap content) throws RepositoryException {
263         return inherit(content, null);
264     }
265 
266     public ContentMap inherit(ContentMap content, String relPath) throws RepositoryException {
267         if (content == null) {
268             return null;
269         }
270         Node node = inherit(content.getJCRNode(), relPath);
271         return node == null ? null : new ContentMap(node);
272     }
273 
274 
275     public Property inheritProperty(Node content, String relPath) throws RepositoryException {
276         if (content == null) {
277             return null;
278         }
279         if (StringUtils.isBlank(relPath)) {
280             throw new IllegalArgumentException("relative path cannot be null or empty");
281         }
282         try {
283             Node inheritedNode = wrapForInheritance(content);
284             return inheritedNode.getProperty(relPath);
285 
286         } catch (PathNotFoundException e) {
287             // TODO fgrilli: rethrow exception?
288         } catch (RepositoryException e) {
289             // TODO fgrilli:rethrow exception?
290         }
291 
292         return null;
293     }
294 
295     public Property inheritProperty(ContentMap content, String relPath) throws RepositoryException {
296         if (content == null) {
297             return null;
298         }
299         return inheritProperty(content.getJCRNode(), relPath);
300     }
301 
302     public List<Node> inheritList(Node content, String relPath) throws RepositoryException {
303         if (content == null) {
304             return null;
305         }
306         if (StringUtils.isBlank(relPath)) {
307             throw new IllegalArgumentException("relative path cannot be null or empty");
308         }
309         Node inheritedNode = wrapForInheritance(content);
310         Node subNode = inheritedNode.getNode(relPath);
311         return children(subNode);
312     }
313 
314     public List<ContentMap> inheritList(ContentMap content, String relPath) throws RepositoryException {
315         if (content == null) {
316             return null;
317         }
318         if (StringUtils.isBlank(relPath)) {
319             throw new IllegalArgumentException("relative path cannot be null or empty");
320         }
321         Node node = asJCRNode(content);
322         Node inheritedNode = wrapForInheritance(node);
323         Node subNode = inheritedNode.getNode(relPath);
324         return children(new ContentMap(subNode));
325     }
326 
327     public boolean isInherited(Node content) {
328         if (content instanceof InheritanceNodeWrapper) {
329             return ((InheritanceNodeWrapper) content).isInherited();
330         }
331         return false;
332     }
333 
334     public boolean isInherited(ContentMap content) {
335         return isInherited(asJCRNode(content));
336     }
337 
338     public boolean isFromCurrentPage(Node content) {
339         return !isInherited(content);
340     }
341 
342     public boolean isFromCurrentPage(ContentMap content) {
343         return isFromCurrentPage(asJCRNode(content));
344     }
345 
346     /**
347      * Create link for the Node identified by nodeIdentifier in the specified workspace.
348      */
349     public String link(String workspace, String nodeIdentifier) {
350         try {
351             return LinkUtil.createLink(workspace, nodeIdentifier);
352         } catch (RepositoryException e) {
353             return null;
354         }
355     }
356 
357     /**
358      * There should be no real reason to use this method except to produce link to binary content stored in jcr:data property in which case one should call {@link #link(Node)} while passing parent node as a parameter. In case you find other valid reason to use this method, please raise it in a forum discussion or create issue. Otherwise this method will be removed in the future.
359      * 
360      * @deprecated since 4.5.4. There is no valid use case for this method.
361      */
362     @Deprecated
363     public String link(Property property) {
364         try {
365             Node parentNode = null;
366             String propertyName = null;
367             if (property.getType() == PropertyType.BINARY) {
368                 parentNode = property.getParent().getParent();
369                 propertyName = property.getParent().getName();
370             } else {
371                 parentNode = property.getParent();
372                 propertyName = property.getName();
373             }
374             NodeData equivNodeData = ContentUtil.asContent(parentNode).getNodeData(propertyName);
375             return LinkUtil.createLink(equivNodeData);
376         } catch (Exception e) {
377             return null;
378         }
379     }
380 
381     // TODO fgrilli: LinkUtil needs to be Node capable and not only Content. Switch to node based impl when SCRUM-242
382     // will be done.
383     public String link(Node content) {
384         return content == null ? null : LinkUtil.createLink(ContentUtil.asContent(content));
385     }
386 
387     public String link(ContentMap contentMap) throws RepositoryException {
388         return contentMap == null ? null : this.link(asJCRNode(contentMap));
389     }
390 
391     /**
392      * Get the language used currently.
393      * @return The language as a String.
394      */
395     public String language(){
396         return I18nContentSupportFactory.getI18nSupport().getLocale().toString();
397     }
398     /**
399      * Returns an external link prepended with <code>http://</code> in case the protocol is missing or an empty String
400      * if the link does not exist.
401      *
402      * @param content The node where the link property is stored on.
403      * @param linkPropertyName The property where the link value is stored in.
404      * @return The link prepended with <code>http://</code>
405      */
406     public String externalLink(Node content, String linkPropertyName) {
407         String externalLink = PropertyUtil.getString(content, linkPropertyName);
408         if (StringUtils.isBlank(externalLink)) {
409             return StringUtils.EMPTY;
410         }
411         if (!hasProtocol(externalLink)) {
412             externalLink = "http://" + externalLink;
413         }
414         return externalLink;
415     }
416 
417     /**
418      * Returns an external link prepended with <code>http://</code> in case the protocol is missing or an empty String
419      * if the link does not exist.
420      *
421      * @param content The node's map representation where the link property is stored on.
422      * @param linkPropertyName The property where the link value is stored in.
423      * @return The link prepended with <code>http://</code>
424      */
425     public String externalLink(ContentMap content, String linkPropertyName) {
426         return externalLink(asJCRNode(content), linkPropertyName);
427     }
428 
429     /**
430      * Return a link title based on the @param linkTitlePropertyName. When property @param linkTitlePropertyName is
431      * empty or null, the link itself is provided as the linkTitle (prepended with <code>http://</code>).
432      *
433      * @param content The node where the link property is stored on.
434      * @param linkPropertyName The property where the link value is stored in.
435      * @param linkTitlePropertyName The property where the link title value is stored
436      * @return the resolved link title value
437      */
438     public String externalLinkTitle(Node content, String linkPropertyName, String linkTitlePropertyName) {
439         String linkTitle = PropertyUtil.getString(content, linkTitlePropertyName);
440         if (StringUtils.isNotEmpty(linkTitle)) {
441             return linkTitle;
442         }
443         return externalLink(content, linkPropertyName);
444     }
445 
446     /**
447      * Return a link title based on the @param linkTitlePropertyName. When property @param linkTitlePropertyName is
448      * empty or null, the link itself is provided as the linkTitle (prepended with <code>http://</code>).
449      *
450      * @param content The node where the link property is stored on.
451      * @param linkPropertyName The property where the link value is stored in.
452      * @param linkTitlePropertyName The property where the link title value is stored
453      * @return the resolved link title value
454      */
455     public String externalLinkTitle(ContentMap content, String linkPropertyName, String linkTitlePropertyName) {
456         return externalLinkTitle(asJCRNode(content), linkPropertyName, linkTitlePropertyName);
457     }
458 
459     public boolean isEditMode() {
460         // TODO : see CmsFunctions.isEditMode, which checks a couple of other properties.
461         return isAuthorInstance() && !isPreviewMode();
462     }
463 
464     public boolean isPreviewMode() {
465         return this.aggregationStateProvider.get().isPreviewMode();
466     }
467 
468     public boolean isAuthorInstance() {
469         return Components.getComponent(ServerConfiguration.class).isAdmin();
470     }
471 
472     public boolean isPublicInstance() {
473         return !isAuthorInstance();
474     }
475 
476     /**
477      * Util method to create html attributes <code>name="value"</code>. If the value is empty an empty string will be returned.
478      * This is mainly helpful to avoid empty attributes.
479      */
480     public String createHtmlAttribute(String name, String value) {
481         value = StringUtils.trim(value);
482         if (StringUtils.isNotEmpty(value)) {
483             return new StringBuffer().append(name).append("=\"").append(value).append("\"").toString();
484         }
485         return StringUtils.EMPTY;
486     }
487 
488     /**
489      * Returns an instance of SiblingsHelper for the given node.
490      */
491     public SiblingsHelper siblings(Node node) throws RepositoryException {
492         return SiblingsHelper.of(ContentUtil.asContent(node));
493     }
494 
495     public SiblingsHelper siblings(ContentMap node) throws RepositoryException {
496         return siblings(asJCRNode(node));
497     }
498 
499     /**
500      * Return the Node for the Given Path
501      * from the website repository.
502      * @deprecated use {@link #contentByPath(String path)} or {@link #nodeByPath(String path)}
503      */
504     public Node content(String path){
505         return content(RepositoryConstants.WEBSITE, path);
506     }
507 
508     /**
509      * Return the Node for the Given Path
510      * from the given repository.
511      * @deprecated use {@link #contentByPath(String path, String workspace)} or {@link #nodeByPath(String path, String workspace)}
512      */
513     public Node content(String repository, String path){
514         return SessionUtil.getNode(repository, path);
515     }
516 
517     /**
518      * Return the Node by the given identifier
519      * from the website repository.
520      * @deprecated use {@link #contentById(String id)} or {@link #nodeById(String id)
521 
522      */
523     public Node contentByIdentifier(String id){
524         return contentByIdentifier(RepositoryConstants.WEBSITE, id);
525     }
526 
527     /**
528      * Return the Node by the given identifier
529      * from the given repository.
530      * @deprecated use {@link #contentById(String id, String workspace)} or #contentByPath(String id, String workspace)}
531      */
532     public Node contentByIdentifier(String repository, String id) {
533         return SessionUtil.getNodeByIdentifier(repository, id);
534     }
535 
536     /**
537      * Return the ContentMap for the Given Path
538      * from the website repository.
539      */
540     public ContentMap contentByPath(String path) {
541         return contentByPath(path, RepositoryConstants.WEBSITE);
542     }
543 
544     /**
545      * Return the ContentMap for the Given Path
546      * from the given repository.
547      */
548     public ContentMap contentByPath(String path, String workspace) {
549         return asContentMap(SessionUtil.getNode(workspace, path));
550     }
551 
552     /**
553      * Return the ContentMap by the given identifier
554      * from the website repository.
555      */
556     public ContentMap contentById(String id) {
557         return contentById(id, RepositoryConstants.WEBSITE);
558     }
559 
560     /**
561      * Return the ContentMap by the given identifier
562      * from the given repository.
563      */
564     public ContentMap contentById(String id, String workspace) {
565         return asContentMap(SessionUtil.getNodeByIdentifier(workspace, id));
566     }
567 
568     /**
569      * Return the Node for the Given Path
570      * from the website repository.
571      */
572     public Node nodeByPath(String path) {
573         return nodeByPath(path, RepositoryConstants.WEBSITE);
574     }
575 
576     /**
577      * Return the Node for the Given Path
578      * from the given repository.
579      */
580     public Node nodeByPath(String path, String workspace) {
581         return SessionUtil.getNode(workspace, path);
582     }
583 
584     /**
585      * Return the Node by the given identifier
586      * from the website repository.
587      */
588     public Node nodeById(String id) {
589         return nodeById(id, RepositoryConstants.WEBSITE);
590     }
591 
592     /**
593      * Return the Node by the given identifier
594      * from the given repository.
595      */
596     public Node nodeById(String id, String workspace) {
597         return SessionUtil.getNodeByIdentifier(workspace, id);
598     }
599 
600     public List<ContentMap> asContentMapList(Collection<Node> nodeList) {
601         if (nodeList != null) {
602             List<ContentMap> contentMapList = new ArrayList<ContentMap>();
603             for (Node node : nodeList) {
604                 contentMapList.add(asContentMap(node));
605             }
606             return contentMapList;
607         }
608         return null;
609     }
610 
611     public List<Node> asNodeList(Collection<ContentMap> contentMapList) {
612         if (contentMapList != null) {
613             List<Node> nodeList = new ArrayList<Node>();
614             for (ContentMap node : contentMapList) {
615                 nodeList.add(node.getJCRNode());
616             }
617             return nodeList;
618         }
619         return null;
620     }
621 
622     // TODO fgrilli: should we unwrap children?
623     protected List<Node> asNodeList(Iterable<Node> nodes) {
624         List<Node> childList = new ArrayList<Node>();
625         for (Node child : nodes) {
626             childList.add(child);
627         }
628         return childList;
629     }
630 
631     // TODO fgrilli: should we unwrap children?
632     protected List<ContentMap> asContentMapList(Iterable<Node> nodes) {
633         List<ContentMap> childList = new ArrayList<ContentMap>();
634         for (Node child : nodes) {
635             childList.add(new ContentMap(child));
636         }
637         return childList;
638     }
639 
640     /**
641      * Checks if passed string has a <code>http://</code> protocol.
642      *
643      * @param link The link to check
644      * @return If @param link contains a <code>http://</code> protocol
645      */
646     private boolean hasProtocol(String link) {
647         return link != null && link.contains("://");
648     }
649 
650     /**
651      * Checks if the passed {@link Node} is the jcr root '/' of the workspace.
652      * @param content {@link Node} to check if its root.
653      * @return if @param content is the jcr workspace root.
654      * @throws RepositoryException
655      */
656     private boolean isRoot(Node content) throws RepositoryException {
657         return content.getDepth() == 0;
658     }
659 
660     /**
661      * Removes escaping of HTML on properties.
662      */
663     public ContentMap decode(ContentMap content){
664         return asContentMap(decode(content.getJCRNode()));
665     }
666 
667     /**
668      * Removes escaping of HTML on properties.
669      */
670     public Node decode(Node content) {
671         return NodeUtil.deepUnwrap(content, HTMLEscapingNodeWrapper.class);
672     }
673 
674     /**
675      * Adds escaping of HTML on properties as well as changing line breaks into &lt;br/&gt; tags.
676      */
677     public Node encode(Node content) {
678         return content != null ? new HTMLEscapingNodeWrapper(content, true) : null;
679     }
680 
681     /**
682      * Adds escaping of HTML on properties as well as changing line breaks into &lt;br/&gt; tags.
683      */
684     public ContentMap encode(ContentMap content) {
685         return content != null ? new ContentMap(new HTMLEscapingNodeWrapper(content.getJCRNode(), true)) : null;
686     }
687 
688     private Node wrapForInheritance(Node destination) throws RepositoryException {
689         ConfiguredInheritance inheritanceConfiguration = new ConfiguredInheritance();
690         inheritanceConfiguration.setEnabled(true);
691         return new DefaultInheritanceContentDecorator(destination, inheritanceConfiguration).wrapNode(destination);
692     }
693 
694     /**
695      * Returns the string representation of a property from the metaData of the node or <code>null</code> if the node has no Magnolia metaData or if no matching property is found.
696      */
697     public String metaData(Node content, String property){
698         try {
699             if (content.hasNode(MetaData.DEFAULT_META_NODE)) {
700                 Node node = content.getNode(MetaData.DEFAULT_META_NODE);
701                 if (node.hasProperty(property)) {
702                     return PropertyUtil.getProperty(node, property).getString();
703                 } else if (node.hasProperty(RepositoryConstants.NAMESPACE_PREFIX + ":" + property)) {
704                     return PropertyUtil.getProperty(node, RepositoryConstants.NAMESPACE_PREFIX + ":" + property).getString();
705                 }
706             }
707         } catch (RepositoryException e) {
708             throw new RuntimeException(e);
709         }
710         return null;
711     }
712 
713     /**
714      * @see {@link TemplatingFunctions#metaData(Node, String)}.
715      */
716     public String metaData(ContentMap content, String property){
717         return metaData(content.getJCRNode(), property);
718     }
719 
720     /**
721      * Executes query and returns result as Collection of Nodes.
722      * 
723      * @param workspace
724      * @param statement has to be in formal form for chosen language
725      * @param language
726      * @param returnItemType
727      */
728     public Collection<Node> search(String workspace, String statement, String language, String returnItemType){
729         try {
730             return NodeUtil.getCollectionFromNodeIterator(QueryUtil.search(workspace, statement, language, returnItemType));
731         } catch (Exception e) {
732             log.error(e.getMessage(), e);
733         }
734         return null;
735     }
736 
737     /**
738      * Executes simple SQL2 query and returns result as Collection of Nodes.
739      * 
740      * @param workspace
741      * @param statement should be set of labels target has to contain inserted as one string each separated by comma
742      * @param returnItemType
743      * @param startPath can be inserted, for results without limitation set it to slash
744      */
745     public Collection<Node> simpleSearch(String workspace, String statement, String returnItemType, String startPath){
746         if(StringUtils.isEmpty(statement)){
747             log.error("Cannot search with empty statement.");
748             return null;
749         }
750         String query = QueryUtil.buildQuery(statement, startPath);
751         try {
752             return NodeUtil.getCollectionFromNodeIterator(QueryUtil.search(workspace, query, "JCR-SQL2", returnItemType));
753         } catch (Exception e) {
754             log.error(e.getMessage(), e);
755         }
756         return null;
757     }
758 }