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.NodeData;
39  import info.magnolia.cms.i18n.I18nContentSupportFactory;
40  import info.magnolia.cms.util.ContentUtil;
41  import info.magnolia.cms.util.QueryUtil;
42  import info.magnolia.cms.util.SiblingsHelper;
43  import info.magnolia.jcr.inheritance.InheritanceNodeWrapper;
44  import info.magnolia.jcr.util.ContentMap;
45  import info.magnolia.jcr.util.MetaDataUtil;
46  import info.magnolia.jcr.util.NodeTypes;
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.Calendar;
59  import java.util.Collection;
60  import java.util.List;
61  
62  import javax.inject.Inject;
63  import javax.inject.Provider;
64  import javax.jcr.Node;
65  import javax.jcr.PathNotFoundException;
66  import javax.jcr.Property;
67  import javax.jcr.PropertyType;
68  import javax.jcr.RepositoryException;
69  
70  import org.apache.commons.lang.StringUtils;
71  import org.apache.jackrabbit.util.ISO8601;
72  import org.slf4j.Logger;
73  import org.slf4j.LoggerFactory;
74  
75  /**
76   * An object exposing several methods useful for templates. It is exposed in templates as <code>cmsfn</code>.
77   *
78   * @version $Id$
79   */
80  public class TemplatingFunctions {
81  
82      private static final Logger log = LoggerFactory.getLogger(TemplatingFunctions.class);
83  
84      private final Provider<AggregationState> aggregationStateProvider;
85  
86      //TODO: To review with Philipp. Should not use Provider, but has deep impact on CategorizationSupportImpl PageSyndicator CategorySyndicator....
87      @Inject
88      public TemplatingFunctions(Provider<AggregationState> aggregationStateProvider) {
89          this.aggregationStateProvider = aggregationStateProvider;
90      }
91  
92  
93      public Node asJCRNode(ContentMap contentMap) {
94          return contentMap == null ? null : contentMap.getJCRNode();
95      }
96  
97      public ContentMap asContentMap(Node content) {
98          return content == null ? null : new ContentMap(content);
99      }
100 
101     public List<Node> children(Node content) throws RepositoryException {
102         return content == null ? null : asNodeList(NodeUtil.getNodes(content, NodeUtil.EXCLUDE_META_DATA_FILTER));
103     }
104 
105     public List<Node> children(Node content, String nodeTypeName) throws RepositoryException {
106         return content == null ? null : asNodeList(NodeUtil.getNodes(content, nodeTypeName));
107     }
108 
109     public List<ContentMap> children(ContentMap content) throws RepositoryException {
110         return content == null ? null : asContentMapList(NodeUtil.getNodes(asJCRNode(content), NodeUtil.EXCLUDE_META_DATA_FILTER));
111     }
112 
113     public List<ContentMap> children(ContentMap content, String nodeTypeName) throws RepositoryException {
114         return content == null ? null : asContentMapList(NodeUtil.getNodes(asJCRNode(content), nodeTypeName));
115     }
116 
117     public ContentMap root(ContentMap contentMap) throws RepositoryException {
118         return contentMap == null ? null : asContentMap(this.root(contentMap.getJCRNode()));
119     }
120 
121     public ContentMap root(ContentMap contentMap, String nodeTypeName) throws RepositoryException {
122         return contentMap == null ? null : asContentMap(this.root(contentMap.getJCRNode(), nodeTypeName));
123     }
124 
125     public Node root(Node content) throws RepositoryException {
126         return this.root(content, null);
127     }
128 
129     public Node root(Node content, String nodeTypeName) throws RepositoryException {
130         if (content == null) {
131             return null;
132         }
133         if (nodeTypeName == null) {
134             return (Node) content.getAncestor(0);
135         }
136         if (isRoot(content) && content.isNodeType(nodeTypeName)) {
137             return content;
138         }
139 
140         Node parentNode = this.parent(content, nodeTypeName);
141         while (parent(parentNode, nodeTypeName) != null) {
142             parentNode = this.parent(parentNode, nodeTypeName);
143         }
144         return parentNode;
145     }
146 
147     public ContentMap parent(ContentMap contentMap) throws RepositoryException {
148         return contentMap == null ? null : asContentMap(this.parent(contentMap.getJCRNode()));
149     }
150 
151     public ContentMap parent(ContentMap contentMap, String nodeTypeName) throws RepositoryException {
152         return contentMap == null ? null : asContentMap(this.parent(contentMap.getJCRNode(), nodeTypeName));
153     }
154 
155     public Node parent(Node content) throws RepositoryException {
156         return this.parent(content, null);
157     }
158 
159     public Node parent(Node content, String nodeTypeName) throws RepositoryException {
160         if (content == null) {
161             return null;
162         }
163         if (isRoot(content)) {
164             return null;
165         }
166         if (nodeTypeName == null) {
167             return content.getParent();
168         }
169         Node parent = content.getParent();
170         while (!parent.isNodeType(nodeTypeName)) {
171             if (isRoot(parent)) {
172                 return null;
173             }
174             parent = parent.getParent();
175         }
176         return parent;
177     }
178 
179     /**
180      * 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.
181      * If the passed {@link ContentMap} has no parent page at all, null is returned.
182      *
183      * @param content the {@link ContentMap} to get the page's {@link ContentMap} from.
184      * @return returns the page {@link ContentMap} of the passed content {@link ContentMap}.
185      * @throws RepositoryException
186      */
187     public ContentMap page(ContentMap content) throws RepositoryException {
188         return content == null ? null : asContentMap(page(content.getJCRNode()));
189     }
190 
191     /**
192      * Returns the page {@link Node} of the passed node. If the passed {@link Node} is a page, the passed {@link Node} will be returned.
193      * If the passed Node has no parent page at all, null is returned.
194      *
195      * @param content the {@link Node} to get the page from.
196      * @return returns the page {@link Node} of the passed content {@link Node}.
197      * @throws RepositoryException
198      */
199     public Node page(Node content) throws RepositoryException {
200         if (content == null) {
201             return null;
202         }
203         if (content.isNodeType(NodeTypes.Page.NAME)) {
204             return content;
205         }
206         return parent(content, NodeTypes.Page.NAME);
207     }
208 
209     public List<ContentMap> ancestors(ContentMap contentMap) throws RepositoryException {
210         return ancestors(contentMap, null);
211     }
212 
213     public List<ContentMap> ancestors(ContentMap contentMap, String nodeTypeName) throws RepositoryException {
214         List<Node> ancestorsAsNodes = this.ancestors(contentMap.getJCRNode(), nodeTypeName);
215         return asContentMapList(ancestorsAsNodes);
216     }
217 
218     public List<Node> ancestors(Node content) throws RepositoryException {
219         return content == null ? null : this.ancestors(content, null);
220     }
221 
222     public List<Node> ancestors(Node content, String nodeTypeName) throws RepositoryException {
223         if (content == null) {
224             return null;
225         }
226         List<Node> ancestors = new ArrayList<Node>();
227         int depth = content.getDepth();
228         for (int i = 1; i < depth; ++i) {
229             Node possibleAncestor = (Node) content.getAncestor(i);
230             if (nodeTypeName == null) {
231                 ancestors.add(possibleAncestor);
232             } else {
233                 if (possibleAncestor.isNodeType(nodeTypeName)) {
234                     ancestors.add(possibleAncestor);
235                 }
236             }
237         }
238         return ancestors;
239     }
240 
241     public Node inherit(Node content) throws RepositoryException {
242         return inherit(content, null);
243     }
244 
245     public Node inherit(Node content, String relPath) throws RepositoryException {
246         if (content == null) {
247             return null;
248         }
249         Node inheritedNode = wrapForInheritance(content);
250 
251         if (StringUtils.isBlank(relPath)) {
252             return inheritedNode;
253         }
254 
255         try {
256             Node subNode = inheritedNode.getNode(relPath);
257             return NodeUtil.unwrap(subNode);
258         } catch (PathNotFoundException e) {
259             // TODO fgrilli: rethrow exception?
260         }
261         return null;
262     }
263 
264     public ContentMap inherit(ContentMap content) throws RepositoryException {
265         return inherit(content, null);
266     }
267 
268     public ContentMap inherit(ContentMap content, String relPath) throws RepositoryException {
269         if (content == null) {
270             return null;
271         }
272         Node node = inherit(content.getJCRNode(), relPath);
273         return node == null ? null : new ContentMap(node);
274     }
275 
276 
277     public Property inheritProperty(Node content, String relPath) throws RepositoryException {
278         if (content == null) {
279             return null;
280         }
281         if (StringUtils.isBlank(relPath)) {
282             throw new IllegalArgumentException("relative path cannot be null or empty");
283         }
284         try {
285             Node inheritedNode = wrapForInheritance(content);
286             return inheritedNode.getProperty(relPath);
287 
288         } catch (PathNotFoundException e) {
289             // TODO fgrilli: rethrow exception?
290         } catch (RepositoryException e) {
291             // TODO fgrilli:rethrow exception?
292         }
293 
294         return null;
295     }
296 
297     public Property inheritProperty(ContentMap content, String relPath) throws RepositoryException {
298         if (content == null) {
299             return null;
300         }
301         return inheritProperty(content.getJCRNode(), relPath);
302     }
303 
304     public List<Node> inheritList(Node content, String relPath) throws RepositoryException {
305         if (content == null) {
306             return null;
307         }
308         if (StringUtils.isBlank(relPath)) {
309             throw new IllegalArgumentException("relative path cannot be null or empty");
310         }
311         Node inheritedNode = wrapForInheritance(content);
312         Node subNode = inheritedNode.getNode(relPath);
313         return children(subNode);
314     }
315 
316     public List<ContentMap> inheritList(ContentMap content, String relPath) throws RepositoryException {
317         if (content == null) {
318             return null;
319         }
320         if (StringUtils.isBlank(relPath)) {
321             throw new IllegalArgumentException("relative path cannot be null or empty");
322         }
323         Node node = asJCRNode(content);
324         Node inheritedNode = wrapForInheritance(node);
325         Node subNode = inheritedNode.getNode(relPath);
326         return children(new ContentMap(subNode));
327     }
328 
329     public boolean isInherited(Node content) {
330         if (content instanceof InheritanceNodeWrapper) {
331             return ((InheritanceNodeWrapper) content).isInherited();
332         }
333         return false;
334     }
335 
336     public boolean isInherited(ContentMap content) {
337         return isInherited(asJCRNode(content));
338     }
339 
340     public boolean isFromCurrentPage(Node content) {
341         return !isInherited(content);
342     }
343 
344     public boolean isFromCurrentPage(ContentMap content) {
345         return isFromCurrentPage(asJCRNode(content));
346     }
347 
348     /**
349      * Create link for the Node identified by nodeIdentifier in the specified workspace.
350      */
351     public String link(String workspace, String nodeIdentifier) {
352         try {
353             return LinkUtil.createLink(workspace, nodeIdentifier);
354         } catch (RepositoryException e) {
355             return null;
356         }
357     }
358 
359     /**
360      * 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.
361      *
362      * @deprecated since 4.5.4. There is no valid use case for this method.
363      */
364     @Deprecated
365     public String link(Property property) {
366         try {
367             Node parentNode = null;
368             String propertyName = null;
369             if (property.getType() == PropertyType.BINARY) {
370                 parentNode = property.getParent().getParent();
371                 propertyName = property.getParent().getName();
372             } else {
373                 parentNode = property.getParent();
374                 propertyName = property.getName();
375             }
376             NodeData equivNodeData = ContentUtil.asContent(parentNode).getNodeData(propertyName);
377             return LinkUtil.createLink(equivNodeData);
378         } catch (Exception e) {
379             return null;
380         }
381     }
382 
383     public String link(Node content) {
384         return content == null ? null : LinkUtil.createLink(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      *
503      * @deprecated use {@link TemplatingFunctions.contentByPath(String path)} or {@link TemplatingFunctions.nodeByPath(String path)}
504      */
505     public Node content(String path){
506         return content(RepositoryConstants.WEBSITE, path);
507     }
508 
509     /**
510      * Return the Node for the Given Path
511      * from the given repository.
512      * @deprecated use {@link TemplatingFunctions.contentByPath(String path, String workspace)} or {@link TemplatingFunctions.nodeByPath(String path, String workspace)}
513      */
514     public Node content(String repository, String path){
515         return SessionUtil.getNode(repository, path);
516     }
517 
518     /**
519      * Return the Node by the given identifier
520      * from the website repository.
521      *
522      * @deprecated use {@link TemplatingFunctions.contentById(String id)} or {@link TemplatingFunctions.nodeById(String id)
523      */
524     public Node contentByIdentifier(String id){
525         return contentByIdentifier(RepositoryConstants.WEBSITE, id);
526     }
527 
528     /**
529      * Return the Node by the given identifier
530      * from the given repository.
531      *
532      * @deprecated use {@link TemplatingFunctions.contentById(String id, String workspace)} or {@link TemplatingFunctions.contentByPath(String id, String workspace)}
533      */
534     public Node contentByIdentifier(String repository, String id) {
535         return SessionUtil.getNodeByIdentifier(repository, id);
536     }
537 
538     /**
539      * Return the ContentMap for the Given Path
540      * from the website repository.
541      */
542     public ContentMap contentByPath(String path) {
543         return contentByPath(path, RepositoryConstants.WEBSITE);
544     }
545 
546     /**
547      * Return the ContentMap for the Given Path
548      * from the given repository.
549      */
550     public ContentMap contentByPath(String path, String workspace) {
551         return asContentMap(SessionUtil.getNode(workspace, path));
552     }
553 
554     /**
555      * Return the ContentMap by the given identifier
556      * from the website repository.
557      */
558     public ContentMap contentById(String id) {
559         return contentById(id, RepositoryConstants.WEBSITE);
560     }
561 
562     /**
563      * Return the ContentMap by the given identifier
564      * from the given repository.
565      */
566     public ContentMap contentById(String id, String workspace) {
567         return asContentMap(SessionUtil.getNodeByIdentifier(workspace, id));
568     }
569 
570     /**
571      * Return the Node for the Given Path
572      * from the website repository.
573      */
574     public Node nodeByPath(String path) {
575         return nodeByPath(path, RepositoryConstants.WEBSITE);
576     }
577 
578     /**
579      * Return the Node for the Given Path
580      * from the given repository.
581      */
582     public Node nodeByPath(String path, String workspace) {
583         return SessionUtil.getNode(workspace, path);
584     }
585 
586     /**
587      * Return the Node by the given identifier
588      * from the website repository.
589      */
590     public Node nodeById(String id) {
591         return nodeById(id, RepositoryConstants.WEBSITE);
592     }
593 
594     /**
595      * Return the Node by the given identifier
596      * from the given repository.
597      */
598     public Node nodeById(String id, String workspace) {
599         return SessionUtil.getNodeByIdentifier(workspace, id);
600     }
601 
602     public List<ContentMap> asContentMapList(Collection<Node> nodeList) {
603         if (nodeList != null) {
604             List<ContentMap> contentMapList = new ArrayList<ContentMap>();
605             for (Node node : nodeList) {
606                 contentMapList.add(asContentMap(node));
607             }
608             return contentMapList;
609         }
610         return null;
611     }
612 
613     public List<Node> asNodeList(Collection<ContentMap> contentMapList) {
614         if (contentMapList != null) {
615             List<Node> nodeList = new ArrayList<Node>();
616             for (ContentMap node : contentMapList) {
617                 nodeList.add(node.getJCRNode());
618             }
619             return nodeList;
620         }
621         return null;
622     }
623 
624     // TODO fgrilli: should we unwrap children?
625     protected List<Node> asNodeList(Iterable<Node> nodes) {
626         List<Node> childList = new ArrayList<Node>();
627         for (Node child : nodes) {
628             childList.add(child);
629         }
630         return childList;
631     }
632 
633     // TODO fgrilli: should we unwrap children?
634     protected List<ContentMap> asContentMapList(Iterable<Node> nodes) {
635         List<ContentMap> childList = new ArrayList<ContentMap>();
636         for (Node child : nodes) {
637             childList.add(new ContentMap(child));
638         }
639         return childList;
640     }
641 
642     /**
643      * Checks if passed string has a <code>http://</code> protocol.
644      *
645      * @param link The link to check
646      * @return If @param link contains a <code>http://</code> protocol
647      */
648     private boolean hasProtocol(String link) {
649         return link != null && link.contains("://");
650     }
651 
652     /**
653      * Checks if the passed {@link Node} is the jcr root '/' of the workspace.
654      * @param content {@link Node} to check if its root.
655      * @return if @param content is the jcr workspace root.
656      * @throws RepositoryException
657      */
658     private boolean isRoot(Node content) throws RepositoryException {
659         return content.getDepth() == 0;
660     }
661 
662     /**
663      * Removes escaping of HTML on properties.
664      */
665     public ContentMap decode(ContentMap content){
666         return asContentMap(decode(content.getJCRNode()));
667     }
668 
669     /**
670      * Removes escaping of HTML on properties.
671      */
672     public Node decode(Node content) {
673         return NodeUtil.deepUnwrap(content, HTMLEscapingNodeWrapper.class);
674     }
675 
676     /**
677      * Adds escaping of HTML on properties as well as changing line breaks into &lt;br/&gt; tags.
678      */
679     public ContentMap encode(ContentMap content) {
680         return content != null ? new ContentMap(new HTMLEscapingNodeWrapper(content.getJCRNode(), true)) : null;
681     }
682 
683     /**
684      * Adds escaping of HTML on properties as well as changing line breaks into &lt;br/&gt; tags.
685      */
686     public Node encode(Node content) {
687         return content != null ? new HTMLEscapingNodeWrapper(content, true) : null;
688     }
689 
690     private Node wrapForInheritance(Node destination) throws RepositoryException {
691         ConfiguredInheritance inheritanceConfiguration = new ConfiguredInheritance();
692         inheritanceConfiguration.setEnabled(true);
693         return new DefaultInheritanceContentDecorator(destination, inheritanceConfiguration).wrapNode(destination);
694     }
695 
696     /**
697      * 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.
698      */
699     public String metaData(Node content, String property){
700 
701         Object returnValue;
702         try {
703             if (property.equals(NodeTypes.Created.CREATED)) {
704                 returnValue = NodeTypes.Created.getCreated(content);
705             } else if (property.equals(NodeTypes.Created.CREATED_BY)) {
706                 returnValue = NodeTypes.Created.getCreatedBy(content);
707             } else if (property.equals(NodeTypes.LastModified.LAST_MODIFIED)) {
708                 returnValue = NodeTypes.LastModified.getLastModified(content);
709             } else if (property.equals(NodeTypes.LastModified.LAST_MODIFIED_BY)) {
710                 returnValue = NodeTypes.LastModified.getLastModifiedBy(content);
711             } else if (property.equals(NodeTypes.Renderable.TEMPLATE)) {
712                 returnValue = NodeTypes.Renderable.getTemplate(content);
713             } else if (property.equals(NodeTypes.Activatable.LAST_ACTIVATED)) {
714                 returnValue = NodeTypes.Activatable.getLastActivated(content);
715             } else if (property.equals(NodeTypes.Activatable.LAST_ACTIVATED_BY)) {
716                 returnValue = NodeTypes.Activatable.getLastActivatedBy(content);
717             } else if (property.equals(NodeTypes.Activatable.ACTIVATION_STATUS)) {
718                 returnValue = NodeTypes.Activatable.getActivationStatus(content);
719             } else if (property.equals(NodeTypes.Deleted.DELETED)) {
720                 returnValue = NodeTypes.Deleted.getDeleted(content);
721             } else if (property.equals(NodeTypes.Deleted.DELETED_BY)) {
722                 returnValue = NodeTypes.Deleted.getDeletedBy(content);
723             } else if (property.equals(NodeTypes.Deleted.COMMENT)) {
724                 // Since NodeTypes.Deleted.COMMENT and NodeTypes.Versionable.COMMENT have identical names this will work for both
725                 returnValue = NodeTypes.Deleted.getComment(content);
726             } else {
727 
728                 // Try to get the value using one of the deprecated names in MetaData.
729                 // This throws an IllegalArgumentException if its not one of those constants
730                 returnValue = MetaDataUtil.getMetaData(content).getStringProperty(property);
731 
732                 // If no exception was thrown then warn that a legacy constant was used
733                 log.warn("Deprecated constant [" + property+"] used to query for meta data property on node [" + NodeUtil.getPathIfPossible(content) + "]");
734             }
735         } catch (RepositoryException e) {
736             return "";
737         }
738 
739         return returnValue instanceof Calendar ? ISO8601.format((Calendar) returnValue) : returnValue.toString();
740     }
741 
742     /**
743      * @see {@link TemplatingFunctions#metaData(Node, String)}.
744      */
745     public String metaData(ContentMap content, String property){
746         return metaData(content.getJCRNode(), property);
747     }
748 
749     /**
750      * Executes query and returns result as Collection of Nodes.
751      *
752      * @param workspace
753      * @param statement has to be in formal form for chosen language
754      * @param language
755      * @param returnItemType
756      */
757     public Collection<Node> search(String workspace, String statement, String language, String returnItemType){
758         try {
759             return NodeUtil.getCollectionFromNodeIterator(QueryUtil.search(workspace, statement, language, returnItemType));
760         } catch (Exception e) {
761             log.error(e.getMessage(), e);
762         }
763         return null;
764     }
765 
766     /**
767      * Executes simple SQL2 query and returns result as Collection of Nodes.
768      *
769      * @param workspace
770      * @param statement should be set of labels target has to contain inserted as one string each separated by comma
771      * @param returnItemType
772      * @param startPath can be inserted, for results without limitation set it to slash
773      */
774     public Collection<Node> simpleSearch(String workspace, String statement, String returnItemType, String startPath){
775         if(StringUtils.isEmpty(statement)){
776             log.error("Cannot search with empty statement.");
777             return null;
778         }
779         String query = QueryUtil.buildQuery(statement, startPath);
780         try {
781             return NodeUtil.getCollectionFromNodeIterator(QueryUtil.search(workspace, query, "JCR-SQL2", returnItemType));
782         } catch (Exception e) {
783             log.error(e.getMessage(), e);
784         }
785         return null;
786     }
787 }